]> begriffs open source - freertos/blob - tasks.c
Make taskYIELD available to unprivileged tasks (#817)
[freertos] / tasks.c
1 /*
2  * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3  * Copyright (C) 2021 Amazon.com, Inc. or its affiliates.  All Rights Reserved.
4  *
5  * SPDX-License-Identifier: MIT
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in all
15  * copies or substantial portions of the Software.
16  *
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.
23  *
24  * https://www.FreeRTOS.org
25  * https://github.com/FreeRTOS
26  *
27  */
28
29 /* Standard includes. */
30 #include <stdlib.h>
31 #include <string.h>
32
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
37
38 /* FreeRTOS includes. */
39 #include "FreeRTOS.h"
40 #include "task.h"
41 #include "timers.h"
42 #include "stack_macros.h"
43
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. */
49
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 )
53
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. */
58     #include <stdio.h>
59 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
60
61 #if ( configUSE_PREEMPTION == 0 )
62
63 /* If the cooperative scheduler is being used then a yield should not be
64  * performed just because a higher priority task has been woken. */
65     #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB )
66     #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB )
67 #else
68
69     #if ( configNUMBER_OF_CORES == 1 )
70
71 /* This macro requests the running task pxTCB to yield. In single core
72  * scheduler, a running task always runs on core 0 and portYIELD_WITHIN_API()
73  * can be used to request the task running on core 0 to yield. Therefore, pxTCB
74  * is not used in this macro. */
75         #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB ) \
76     do {                                                         \
77         ( void ) ( pxTCB );                                      \
78         portYIELD_WITHIN_API();                                  \
79     } while( 0 )
80
81         #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB ) \
82     do {                                                        \
83         if( pxCurrentTCB->uxPriority < ( pxTCB )->uxPriority )  \
84         {                                                       \
85             portYIELD_WITHIN_API();                             \
86         }                                                       \
87         else                                                    \
88         {                                                       \
89             mtCOVERAGE_TEST_MARKER();                           \
90         }                                                       \
91     } while( 0 )
92
93     #else /* if ( configNUMBER_OF_CORES == 1 ) */
94
95 /* Yield the core on which this task is running. */
96         #define taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB )    prvYieldCore( ( pxTCB )->xTaskRunState )
97
98 /* Yield for the task if a running task has priority lower than this task. */
99         #define taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB )     prvYieldForTask( pxTCB )
100
101     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
102
103 #endif /* if ( configUSE_PREEMPTION == 0 ) */
104
105 /* Values that can be assigned to the ucNotifyState member of the TCB. */
106 #define taskNOT_WAITING_NOTIFICATION              ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
107 #define taskWAITING_NOTIFICATION                  ( ( uint8_t ) 1 )
108 #define taskNOTIFICATION_RECEIVED                 ( ( uint8_t ) 2 )
109
110 /*
111  * The value used to fill the stack of a task when the task is created.  This
112  * is used purely for checking the high water mark for tasks.
113  */
114 #define tskSTACK_FILL_BYTE                        ( 0xa5U )
115
116 /* Bits used to record how a task's stack and TCB were allocated. */
117 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB    ( ( uint8_t ) 0 )
118 #define tskSTATICALLY_ALLOCATED_STACK_ONLY        ( ( uint8_t ) 1 )
119 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB     ( ( uint8_t ) 2 )
120
121 /* If any of the following are set then task stacks are filled with a known
122  * value so the high water mark can be determined.  If none of the following are
123  * set then don't fill the stack so there is no unnecessary dependency on memset. */
124 #if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
125     #define tskSET_NEW_STACKS_TO_KNOWN_VALUE    1
126 #else
127     #define tskSET_NEW_STACKS_TO_KNOWN_VALUE    0
128 #endif
129
130 /*
131  * Macros used by vListTask to indicate which state a task is in.
132  */
133 #define tskRUNNING_CHAR      ( 'X' )
134 #define tskBLOCKED_CHAR      ( 'B' )
135 #define tskREADY_CHAR        ( 'R' )
136 #define tskDELETED_CHAR      ( 'D' )
137 #define tskSUSPENDED_CHAR    ( 'S' )
138
139 /*
140  * Some kernel aware debuggers require the data the debugger needs access to to
141  * be global, rather than file scope.
142  */
143 #ifdef portREMOVE_STATIC_QUALIFIER
144     #define static
145 #endif
146
147 /* The name allocated to the Idle task.  This can be overridden by defining
148  * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
149 #ifndef configIDLE_TASK_NAME
150     #define configIDLE_TASK_NAME    "IDLE"
151 #endif
152
153 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
154
155 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
156  * performed in a generic way that is not optimised to any particular
157  * microcontroller architecture. */
158
159 /* uxTopReadyPriority holds the priority of the highest priority ready
160  * state task. */
161     #define taskRECORD_READY_PRIORITY( uxPriority ) \
162     do {                                            \
163         if( ( uxPriority ) > uxTopReadyPriority )   \
164         {                                           \
165             uxTopReadyPriority = ( uxPriority );    \
166         }                                           \
167     } while( 0 ) /* taskRECORD_READY_PRIORITY */
168
169 /*-----------------------------------------------------------*/
170
171     #if ( configNUMBER_OF_CORES == 1 )
172         #define taskSELECT_HIGHEST_PRIORITY_TASK()                            \
173     do {                                                                      \
174         UBaseType_t uxTopPriority = uxTopReadyPriority;                       \
175                                                                               \
176         /* Find the highest priority queue that contains ready tasks. */      \
177         while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
178         {                                                                     \
179             configASSERT( uxTopPriority );                                    \
180             --uxTopPriority;                                                  \
181         }                                                                     \
182                                                                               \
183         /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
184          * the  same priority get an equal share of the processor time. */                    \
185         listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
186         uxTopReadyPriority = uxTopPriority;                                                   \
187     } while( 0 ) /* taskSELECT_HIGHEST_PRIORITY_TASK */
188     #else /* if ( configNUMBER_OF_CORES == 1 ) */
189
190         #define taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID )    prvSelectHighestPriorityTask( xCoreID )
191
192     #endif /* if ( configNUMBER_OF_CORES == 1 ) */
193
194 /*-----------------------------------------------------------*/
195
196 /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
197  * they are only required when a port optimised method of task selection is
198  * being used. */
199     #define taskRESET_READY_PRIORITY( uxPriority )
200     #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
201
202 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
203
204 /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
205  * performed in a way that is tailored to the particular microcontroller
206  * architecture being used. */
207
208 /* A port optimised version is provided.  Call the port defined macros. */
209     #define taskRECORD_READY_PRIORITY( uxPriority )    portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
210
211 /*-----------------------------------------------------------*/
212
213     #define taskSELECT_HIGHEST_PRIORITY_TASK()                                                  \
214     do {                                                                                        \
215         UBaseType_t uxTopPriority;                                                              \
216                                                                                                 \
217         /* Find the highest priority list that contains ready tasks. */                         \
218         portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );                          \
219         configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
220         listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) );   \
221     } while( 0 )
222
223 /*-----------------------------------------------------------*/
224
225 /* A port optimised version is provided, call it only if the TCB being reset
226  * is being referenced from a ready list.  If it is referenced from a delayed
227  * or suspended list then it won't be in a ready list. */
228     #define taskRESET_READY_PRIORITY( uxPriority )                                                     \
229     do {                                                                                               \
230         if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
231         {                                                                                              \
232             portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) );                        \
233         }                                                                                              \
234     } while( 0 )
235
236 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
237
238 /*-----------------------------------------------------------*/
239
240 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
241  * count overflows. */
242 #define taskSWITCH_DELAYED_LISTS()                                                \
243     do {                                                                          \
244         List_t * pxTemp;                                                          \
245                                                                                   \
246         /* The delayed tasks list should be empty when the lists are switched. */ \
247         configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );               \
248                                                                                   \
249         pxTemp = pxDelayedTaskList;                                               \
250         pxDelayedTaskList = pxOverflowDelayedTaskList;                            \
251         pxOverflowDelayedTaskList = pxTemp;                                       \
252         xNumOfOverflows++;                                                        \
253         prvResetNextTaskUnblockTime();                                            \
254     } while( 0 )
255
256 /*-----------------------------------------------------------*/
257
258 /*
259  * Place the task represented by pxTCB into the appropriate ready list for
260  * the task.  It is inserted at the end of the list.
261  */
262 #define prvAddTaskToReadyList( pxTCB )                                                                     \
263     do {                                                                                                   \
264         traceMOVED_TASK_TO_READY_STATE( pxTCB );                                                           \
265         taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority );                                                \
266         listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
267         tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB );                                                      \
268     } while( 0 )
269 /*-----------------------------------------------------------*/
270
271 /*
272  * Several functions take a TaskHandle_t parameter that can optionally be NULL,
273  * where NULL is used to indicate that the handle of the currently executing
274  * task should be used in place of the parameter.  This macro simply checks to
275  * see if the parameter is NULL and returns a pointer to the appropriate TCB.
276  */
277 #define prvGetTCBFromHandle( pxHandle )    ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
278
279 /* The item value of the event list item is normally used to hold the priority
280  * of the task to which it belongs (coded to allow it to be held in reverse
281  * priority order).  However, it is occasionally borrowed for other purposes.  It
282  * is important its value is not updated due to a task priority change while it is
283  * being used for another purpose.  The following bit definition is used to inform
284  * the scheduler that the value should not be changed - in which case it is the
285  * responsibility of whichever module is using the value to ensure it gets set back
286  * to its original value when it is released. */
287 #if ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_16_BITS )
288     #define taskEVENT_LIST_ITEM_VALUE_IN_USE    0x8000U
289 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_32_BITS )
290     #define taskEVENT_LIST_ITEM_VALUE_IN_USE    0x80000000UL
291 #elif ( configTICK_TYPE_WIDTH_IN_BITS == TICK_TYPE_WIDTH_64_BITS )
292     #define taskEVENT_LIST_ITEM_VALUE_IN_USE    0x8000000000000000ULL
293 #endif
294
295 /* Indicates that the task is not actively running on any core. */
296 #define taskTASK_NOT_RUNNING           ( ( BaseType_t ) ( -1 ) )
297
298 /* Indicates that the task is actively running but scheduled to yield. */
299 #define taskTASK_SCHEDULED_TO_YIELD    ( ( BaseType_t ) ( -2 ) )
300
301 /* Returns pdTRUE if the task is actively running and not scheduled to yield. */
302 #if ( configNUMBER_OF_CORES == 1 )
303     #define taskTASK_IS_RUNNING( pxTCB )                          ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
304     #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB )    ( ( ( pxTCB ) == pxCurrentTCB ) ? ( pdTRUE ) : ( pdFALSE ) )
305 #else
306     #define taskTASK_IS_RUNNING( pxTCB )                          ( ( ( ( pxTCB )->xTaskRunState >= ( BaseType_t ) 0 ) && ( ( pxTCB )->xTaskRunState < ( BaseType_t ) configNUMBER_OF_CORES ) ) ? ( pdTRUE ) : ( pdFALSE ) )
307     #define taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB )    ( ( ( pxTCB )->xTaskRunState != taskTASK_NOT_RUNNING ) ? ( pdTRUE ) : ( pdFALSE ) )
308 #endif
309
310 /* Indicates that the task is an Idle task. */
311 #define taskATTRIBUTE_IS_IDLE    ( UBaseType_t ) ( 1UL << 0UL )
312
313 #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) )
314     #define portGET_CRITICAL_NESTING_COUNT()          ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting )
315     #define portSET_CRITICAL_NESTING_COUNT( x )       ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting = ( x ) )
316     #define portINCREMENT_CRITICAL_NESTING_COUNT()    ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting++ )
317     #define portDECREMENT_CRITICAL_NESTING_COUNT()    ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxCriticalNesting-- )
318 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( portCRITICAL_NESTING_IN_TCB == 1 ) ) */
319
320 #define taskBITS_PER_BYTE    ( ( size_t ) 8 )
321
322 #if ( configNUMBER_OF_CORES > 1 )
323
324 /* Yields the given core. This must be called from a critical section and xCoreID
325  * must be valid. This macro is not required in single core since there is only
326  * one core to yield. */
327     #define prvYieldCore( xCoreID )                                                      \
328     do {                                                                                 \
329         if( xCoreID == ( BaseType_t ) portGET_CORE_ID() )                                \
330         {                                                                                \
331             /* Pending a yield for this core since it is in the critical section. */     \
332             xYieldPendings[ xCoreID ] = pdTRUE;                                          \
333         }                                                                                \
334         else                                                                             \
335         {                                                                                \
336             /* Request other core to yield if it is not requested before. */             \
337             if( pxCurrentTCBs[ xCoreID ]->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD ) \
338             {                                                                            \
339                 portYIELD_CORE( xCoreID );                                               \
340                 pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_SCHEDULED_TO_YIELD;   \
341             }                                                                            \
342         }                                                                                \
343     } while( 0 )
344 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
345 /*-----------------------------------------------------------*/
346
347 /*
348  * Task control block.  A task control block (TCB) is allocated for each task,
349  * and stores task state information, including a pointer to the task's context
350  * (the task's run time environment, including register values)
351  */
352 typedef struct tskTaskControlBlock       /* The old naming convention is used to prevent breaking kernel aware debuggers. */
353 {
354     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. */
355
356     #if ( portUSING_MPU_WRAPPERS == 1 )
357         xMPU_SETTINGS xMPUSettings; /**< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
358     #endif
359
360     #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
361         UBaseType_t uxCoreAffinityMask; /**< Used to link the task to certain cores.  UBaseType_t must have greater than or equal to the number of bits as configNUMBER_OF_CORES. */
362     #endif
363
364     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 ). */
365     ListItem_t xEventListItem;                  /**< Used to reference a task from an event list. */
366     UBaseType_t uxPriority;                     /**< The priority of the task.  0 is the lowest priority. */
367     StackType_t * pxStack;                      /**< Points to the start of the stack. */
368     #if ( configNUMBER_OF_CORES > 1 )
369         volatile BaseType_t xTaskRunState;      /**< Used to identify the core the task is running on, if the task is running. Otherwise, identifies the task's state - not running or yielding. */
370         UBaseType_t uxTaskAttributes;           /**< Task's attributes - currently used to identify the idle tasks. */
371     #endif
372     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. */
373
374     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
375         BaseType_t xPreemptionDisable; /**< Used to prevent the task from being preempted. */
376     #endif
377
378     #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
379         StackType_t * pxEndOfStack; /**< Points to the highest valid address for the stack. */
380     #endif
381
382     #if ( portCRITICAL_NESTING_IN_TCB == 1 )
383         UBaseType_t uxCriticalNesting; /**< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
384     #endif
385
386     #if ( configUSE_TRACE_FACILITY == 1 )
387         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. */
388         UBaseType_t uxTaskNumber; /**< Stores a number specifically for use by third party trace code. */
389     #endif
390
391     #if ( configUSE_MUTEXES == 1 )
392         UBaseType_t uxBasePriority; /**< The priority last assigned to the task - used by the priority inheritance mechanism. */
393         UBaseType_t uxMutexesHeld;
394     #endif
395
396     #if ( configUSE_APPLICATION_TASK_TAG == 1 )
397         TaskHookFunction_t pxTaskTag;
398     #endif
399
400     #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
401         void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
402     #endif
403
404     #if ( configGENERATE_RUN_TIME_STATS == 1 )
405         configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /**< Stores the amount of time the task has spent in the Running state. */
406     #endif
407
408     #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
409         configTLS_BLOCK_TYPE xTLSBlock; /**< Memory block used as Thread Local Storage (TLS) Block for the task. */
410     #endif
411
412     #if ( configUSE_TASK_NOTIFICATIONS == 1 )
413         volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
414         volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
415     #endif
416
417     /* See the comments in FreeRTOS.h with the definition of
418      * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
419     #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
420         uint8_t ucStaticallyAllocated;                     /**< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
421     #endif
422
423     #if ( INCLUDE_xTaskAbortDelay == 1 )
424         uint8_t ucDelayAborted;
425     #endif
426
427     #if ( configUSE_POSIX_ERRNO == 1 )
428         int iTaskErrno;
429     #endif
430 } tskTCB;
431
432 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
433  * below to enable the use of older kernel aware debuggers. */
434 typedef tskTCB TCB_t;
435
436 /*lint -save -e956 A manual analysis and inspection has been used to determine
437  * which static variables must be declared volatile. */
438 #if ( configNUMBER_OF_CORES == 1 )
439     portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
440 #else
441     /* MISRA Ref 8.4.1 [Declaration shall be visible] */
442     /* More details at: https://github.com/FreeRTOS/FreeRTOS-Kernel/blob/main/MISRA.md#rule-84 */
443     /* coverity[misra_c_2012_rule_8_4_violation] */
444     portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCBs[ configNUMBER_OF_CORES ];
445     #define pxCurrentTCB    xTaskGetCurrentTaskHandle()
446 #endif
447
448 /* Lists for ready and blocked tasks. --------------------
449  * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
450  * doing so breaks some kernel aware debuggers and debuggers that rely on removing
451  * the static qualifier. */
452 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /**< Prioritised ready tasks. */
453 PRIVILEGED_DATA static List_t xDelayedTaskList1;                         /**< Delayed tasks. */
454 PRIVILEGED_DATA static List_t xDelayedTaskList2;                         /**< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
455 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList;              /**< Points to the delayed task list currently being used. */
456 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. */
457 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. */
458
459 #if ( INCLUDE_vTaskDelete == 1 )
460
461     PRIVILEGED_DATA static List_t xTasksWaitingTermination; /**< Tasks that have been deleted - but their memory not yet freed. */
462     PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
463
464 #endif
465
466 #if ( INCLUDE_vTaskSuspend == 1 )
467
468     PRIVILEGED_DATA static List_t xSuspendedTaskList; /**< Tasks that are currently suspended. */
469
470 #endif
471
472 /* Global POSIX errno. Its value is changed upon context switching to match
473  * the errno of the currently running task. */
474 #if ( configUSE_POSIX_ERRNO == 1 )
475     int FreeRTOS_errno = 0;
476 #endif
477
478 /* Other file private variables. --------------------------------*/
479 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
480 PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
481 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
482 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
483 PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
484 PRIVILEGED_DATA static volatile BaseType_t xYieldPendings[ configNUMBER_OF_CORES ] = { pdFALSE };
485 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
486 PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
487 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
488 PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandles[ configNUMBER_OF_CORES ];       /**< Holds the handles of the idle tasks.  The idle tasks are created automatically when the scheduler is started. */
489
490 /* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
491  * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
492  * to determine the number of priority lists to read back from the remote target. */
493 const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
494
495 /* Context switches are held pending while the scheduler is suspended.  Also,
496  * interrupts must not manipulate the xStateListItem of a TCB, or any of the
497  * lists the xStateListItem can be referenced from, if the scheduler is suspended.
498  * If an interrupt needs to unblock a task while the scheduler is suspended then it
499  * moves the task's event list item into the xPendingReadyList, ready for the
500  * kernel to move the task from the pending ready list into the real ready list
501  * when the scheduler is unsuspended.  The pending ready list itself can only be
502  * accessed from a critical section.
503  *
504  * Updates to uxSchedulerSuspended must be protected by both the task lock and the ISR lock
505  * and must not be done from an ISR. Reads must be protected by either lock and may be done
506  * from either an ISR or a task. */
507 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) 0U;
508
509 #if ( configGENERATE_RUN_TIME_STATS == 1 )
510
511 /* Do not move these variables to function scope as doing so prevents the
512  * code working with debuggers that need to remove the static qualifier. */
513 PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime[ configNUMBER_OF_CORES ] = { 0U };    /**< Holds the value of a timer/counter the last time a task was switched in. */
514 PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime[ configNUMBER_OF_CORES ] = { 0U }; /**< Holds the total amount of execution time as defined by the run time counter clock. */
515
516 #endif
517
518 #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configNUMBER_OF_CORES > 1 )
519
520 /* Do not move these variables to function scope as doing so prevents the
521  * code working with debuggers that need to remove the static qualifier. */
522     static StaticTask_t xIdleTCBBuffers[ configNUMBER_OF_CORES - 1 ];
523     static StackType_t xIdleTaskStackBuffers[ configNUMBER_OF_CORES - 1 ][ configMINIMAL_STACK_SIZE ];
524
525 #endif /* #if ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
526
527 /*lint -restore */
528
529 /*-----------------------------------------------------------*/
530
531 /* File private functions. --------------------------------*/
532
533 /*
534  * Creates the idle tasks during scheduler start.
535  */
536 static BaseType_t prvCreateIdleTasks( void );
537
538 #if ( configNUMBER_OF_CORES > 1 )
539
540 /*
541  * Checks to see if another task moved the current task out of the ready
542  * list while it was waiting to enter a critical section and yields, if so.
543  */
544     static void prvCheckForRunStateChange( void );
545 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
546
547 #if ( configNUMBER_OF_CORES > 1 )
548
549 /*
550  * Yields a core, or cores if multiple priorities are not allowed to run
551  * simultaneously, to allow the task pxTCB to run.
552  */
553     static void prvYieldForTask( const TCB_t * pxTCB );
554 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
555
556 #if ( configNUMBER_OF_CORES > 1 )
557
558 /*
559  * Selects the highest priority available task for the given core.
560  */
561     static void prvSelectHighestPriorityTask( BaseType_t xCoreID );
562 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
563
564 /**
565  * Utility task that simply returns pdTRUE if the task referenced by xTask is
566  * currently in the Suspended state, or pdFALSE if the task referenced by xTask
567  * is in any other state.
568  */
569 #if ( INCLUDE_vTaskSuspend == 1 )
570
571     static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
572
573 #endif /* INCLUDE_vTaskSuspend */
574
575 /*
576  * Utility to ready all the lists used by the scheduler.  This is called
577  * automatically upon the creation of the first task.
578  */
579 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
580
581 /*
582  * The idle task, which as all tasks is implemented as a never ending loop.
583  * The idle task is automatically created and added to the ready lists upon
584  * creation of the first user task.
585  *
586  * In the FreeRTOS SMP, configNUMBER_OF_CORES - 1 minimal idle tasks are also
587  * created to ensure that each core has an idle task to run when no other
588  * task is available to run.
589  *
590  * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
591  * language extensions.  The equivalent prototype for these functions are:
592  *
593  * void prvIdleTask( void *pvParameters );
594  * void prvMinimalIdleTask( void *pvParameters );
595  *
596  */
597 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
598 #if ( configNUMBER_OF_CORES > 1 )
599     static portTASK_FUNCTION_PROTO( prvMinimalIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
600 #endif
601
602 /*
603  * Utility to free all memory allocated by the scheduler to hold a TCB,
604  * including the stack pointed to by the TCB.
605  *
606  * This does not free memory allocated by the task itself (i.e. memory
607  * allocated by calls to pvPortMalloc from within the tasks application code).
608  */
609 #if ( INCLUDE_vTaskDelete == 1 )
610
611     static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
612
613 #endif
614
615 /*
616  * Used only by the idle task.  This checks to see if anything has been placed
617  * in the list of tasks waiting to be deleted.  If so the task is cleaned up
618  * and its TCB deleted.
619  */
620 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
621
622 /*
623  * The currently executing task is entering the Blocked state.  Add the task to
624  * either the current or the overflow delayed task list.
625  */
626 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
627                                             const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
628
629 /*
630  * Fills an TaskStatus_t structure with information on each task that is
631  * referenced from the pxList list (which may be a ready list, a delayed list,
632  * a suspended list, etc.).
633  *
634  * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
635  * NORMAL APPLICATION CODE.
636  */
637 #if ( configUSE_TRACE_FACILITY == 1 )
638
639     static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
640                                                      List_t * pxList,
641                                                      eTaskState eState ) PRIVILEGED_FUNCTION;
642
643 #endif
644
645 /*
646  * Searches pxList for a task with name pcNameToQuery - returning a handle to
647  * the task if it is found, or NULL if the task is not found.
648  */
649 #if ( INCLUDE_xTaskGetHandle == 1 )
650
651     static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
652                                                      const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
653
654 #endif
655
656 /*
657  * When a task is created, the stack of the task is filled with a known value.
658  * This function determines the 'high water mark' of the task stack by
659  * determining how much of the stack remains at the original preset value.
660  */
661 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
662
663     static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
664
665 #endif
666
667 /*
668  * Return the amount of time, in ticks, that will pass before the kernel will
669  * next move a task from the Blocked state to the Running state.
670  *
671  * This conditional compilation should use inequality to 0, not equality to 1.
672  * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
673  * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
674  * set to a value other than 1.
675  */
676 #if ( configUSE_TICKLESS_IDLE != 0 )
677
678     static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
679
680 #endif
681
682 /*
683  * Set xNextTaskUnblockTime to the time at which the next Blocked state task
684  * will exit the Blocked state.
685  */
686 static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
687
688 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
689
690 /*
691  * Helper function used to pad task names with spaces when printing out
692  * human readable tables of task information.
693  */
694     static char * prvWriteNameToBuffer( char * pcBuffer,
695                                         const char * pcTaskName ) PRIVILEGED_FUNCTION;
696
697 #endif
698
699 /*
700  * Called after a Task_t structure has been allocated either statically or
701  * dynamically to fill in the structure's members.
702  */
703 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
704                                   const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
705                                   const uint32_t ulStackDepth,
706                                   void * const pvParameters,
707                                   UBaseType_t uxPriority,
708                                   TaskHandle_t * const pxCreatedTask,
709                                   TCB_t * pxNewTCB,
710                                   const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
711
712 /*
713  * Called after a new task has been created and initialised to place the task
714  * under the control of the scheduler.
715  */
716 static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
717
718 /*
719  * freertos_tasks_c_additions_init() should only be called if the user definable
720  * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
721  * called by the function.
722  */
723 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
724
725     static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
726
727 #endif
728
729 #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
730     extern void vApplicationMinimalIdleHook( void );
731 #endif /* #if ( configUSE_MINIMAL_IDLE_HOOK == 1 ) */
732
733 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
734
735 /*
736  * Convert the snprintf return value to the number of characters
737  * written. The following are the possible cases:
738  *
739  * 1. The buffer supplied to snprintf is large enough to hold the
740  *    generated string. The return value in this case is the number
741  *    of characters actually written, not counting the terminating
742  *    null character.
743  * 2. The buffer supplied to snprintf is NOT large enough to hold
744  *    the generated string. The return value in this case is the
745  *    number of characters that would have been written if the
746  *    buffer had been sufficiently large, not counting the
747  *    terminating null character.
748  * 3. Encoding error. The return value in this case is a negative
749  *    number.
750  *
751  * From 1 and 2 above ==> Only when the return value is non-negative
752  * and less than the supplied buffer length, the string has been
753  * completely written.
754  */
755     static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
756                                                         size_t n );
757
758 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
759 /*-----------------------------------------------------------*/
760
761 #if ( configNUMBER_OF_CORES > 1 )
762     static void prvCheckForRunStateChange( void )
763     {
764         UBaseType_t uxPrevCriticalNesting;
765         const TCB_t * pxThisTCB;
766
767         /* This must only be called from within a task. */
768         portASSERT_IF_IN_ISR();
769
770         /* This function is always called with interrupts disabled
771          * so this is safe. */
772         pxThisTCB = pxCurrentTCBs[ portGET_CORE_ID() ];
773
774         while( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD )
775         {
776             /* We are only here if we just entered a critical section
777             * or if we just suspended the scheduler, and another task
778             * has requested that we yield.
779             *
780             * This is slightly complicated since we need to save and restore
781             * the suspension and critical nesting counts, as well as release
782             * and reacquire the correct locks. And then, do it all over again
783             * if our state changed again during the reacquisition. */
784             uxPrevCriticalNesting = portGET_CRITICAL_NESTING_COUNT();
785
786             if( uxPrevCriticalNesting > 0U )
787             {
788                 portSET_CRITICAL_NESTING_COUNT( 0U );
789                 portRELEASE_ISR_LOCK();
790             }
791             else
792             {
793                 /* The scheduler is suspended. uxSchedulerSuspended is updated
794                  * only when the task is not requested to yield. */
795                 mtCOVERAGE_TEST_MARKER();
796             }
797
798             portRELEASE_TASK_LOCK();
799             portMEMORY_BARRIER();
800             configASSERT( pxThisTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD );
801
802             portENABLE_INTERRUPTS();
803
804             /* Enabling interrupts should cause this core to immediately
805              * service the pending interrupt and yield. If the run state is still
806              * yielding here then that is a problem. */
807             configASSERT( pxThisTCB->xTaskRunState != taskTASK_SCHEDULED_TO_YIELD );
808
809             portDISABLE_INTERRUPTS();
810             portGET_TASK_LOCK();
811             portGET_ISR_LOCK();
812
813             portSET_CRITICAL_NESTING_COUNT( uxPrevCriticalNesting );
814
815             if( uxPrevCriticalNesting == 0U )
816             {
817                 portRELEASE_ISR_LOCK();
818             }
819         }
820     }
821 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
822
823 /*-----------------------------------------------------------*/
824
825 #if ( configNUMBER_OF_CORES > 1 )
826     static void prvYieldForTask( const TCB_t * pxTCB )
827     {
828         BaseType_t xLowestPriorityToPreempt;
829         BaseType_t xCurrentCoreTaskPriority;
830         BaseType_t xLowestPriorityCore = ( BaseType_t ) -1;
831         BaseType_t xCoreID;
832
833         #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
834             BaseType_t xYieldCount = 0;
835         #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
836
837         /* This must be called from a critical section. */
838         configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
839
840         #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
841
842             /* No task should yield for this one if it is a lower priority
843              * than priority level of currently ready tasks. */
844             if( pxTCB->uxPriority >= uxTopReadyPriority )
845         #else
846             /* Yield is not required for a task which is already running. */
847             if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
848         #endif
849         {
850             xLowestPriorityToPreempt = ( BaseType_t ) pxTCB->uxPriority;
851
852             /* xLowestPriorityToPreempt will be decremented to -1 if the priority of pxTCB
853              * is 0. This is ok as we will give system idle tasks a priority of -1 below. */
854             --xLowestPriorityToPreempt;
855
856             for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
857             {
858                 xCurrentCoreTaskPriority = ( BaseType_t ) pxCurrentTCBs[ xCoreID ]->uxPriority;
859
860                 /* System idle tasks are being assigned a priority of tskIDLE_PRIORITY - 1 here. */
861                 if( ( pxCurrentTCBs[ xCoreID ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
862                 {
863                     xCurrentCoreTaskPriority = xCurrentCoreTaskPriority - 1;
864                 }
865
866                 if( ( taskTASK_IS_RUNNING( pxCurrentTCBs[ xCoreID ] ) != pdFALSE ) && ( xYieldPendings[ xCoreID ] == pdFALSE ) )
867                 {
868                     #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
869                         if( taskTASK_IS_RUNNING( pxTCB ) == pdFALSE )
870                     #endif
871                     {
872                         if( xCurrentCoreTaskPriority <= xLowestPriorityToPreempt )
873                         {
874                             #if ( configUSE_CORE_AFFINITY == 1 )
875                                 if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
876                             #endif
877                             {
878                                 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
879                                     if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
880                                 #endif
881                                 {
882                                     xLowestPriorityToPreempt = xCurrentCoreTaskPriority;
883                                     xLowestPriorityCore = xCoreID;
884                                 }
885                             }
886                         }
887                         else
888                         {
889                             mtCOVERAGE_TEST_MARKER();
890                         }
891                     }
892
893                     #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
894                     {
895                         /* Yield all currently running non-idle tasks with a priority lower than
896                          * the task that needs to run. */
897                         if( ( xCurrentCoreTaskPriority > ( ( BaseType_t ) tskIDLE_PRIORITY - 1 ) ) &&
898                             ( xCurrentCoreTaskPriority < ( BaseType_t ) pxTCB->uxPriority ) )
899                         {
900                             prvYieldCore( xCoreID );
901                             xYieldCount++;
902                         }
903                         else
904                         {
905                             mtCOVERAGE_TEST_MARKER();
906                         }
907                     }
908                     #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
909                 }
910                 else
911                 {
912                     mtCOVERAGE_TEST_MARKER();
913                 }
914             }
915
916             #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
917                 if( ( xYieldCount == 0 ) && ( xLowestPriorityCore >= 0 ) )
918             #else /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
919                 if( xLowestPriorityCore >= 0 )
920             #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
921             {
922                 prvYieldCore( xLowestPriorityCore );
923             }
924
925             #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
926                 /* Verify that the calling core always yields to higher priority tasks. */
927                 if( ( ( pxCurrentTCBs[ portGET_CORE_ID() ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0 ) &&
928                     ( pxTCB->uxPriority > pxCurrentTCBs[ portGET_CORE_ID() ]->uxPriority ) )
929                 {
930                     configASSERT( ( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE ) ||
931                                   ( taskTASK_IS_RUNNING( pxCurrentTCBs[ portGET_CORE_ID() ] ) == pdFALSE ) );
932                 }
933             #endif
934         }
935     }
936 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
937 /*-----------------------------------------------------------*/
938
939 #if ( configNUMBER_OF_CORES > 1 )
940     static void prvSelectHighestPriorityTask( BaseType_t xCoreID )
941     {
942         UBaseType_t uxCurrentPriority = uxTopReadyPriority;
943         BaseType_t xTaskScheduled = pdFALSE;
944         BaseType_t xDecrementTopPriority = pdTRUE;
945
946         #if ( configUSE_CORE_AFFINITY == 1 )
947             const TCB_t * pxPreviousTCB = NULL;
948         #endif
949         #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
950             BaseType_t xPriorityDropped = pdFALSE;
951         #endif
952
953         /* This function should be called when scheduler is running. */
954         configASSERT( xSchedulerRunning == pdTRUE );
955
956         /* A new task is created and a running task with the same priority yields
957          * itself to run the new task. When a running task yields itself, it is still
958          * in the ready list. This running task will be selected before the new task
959          * since the new task is always added to the end of the ready list.
960          * The other problem is that the running task still in the same position of
961          * the ready list when it yields itself. It is possible that it will be selected
962          * earlier then other tasks which waits longer than this task.
963          *
964          * To fix these problems, the running task should be put to the end of the
965          * ready list before searching for the ready task in the ready list. */
966         if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
967                                      &pxCurrentTCBs[ xCoreID ]->xStateListItem ) == pdTRUE )
968         {
969             ( void ) uxListRemove( &pxCurrentTCBs[ xCoreID ]->xStateListItem );
970             vListInsertEnd( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ),
971                             &pxCurrentTCBs[ xCoreID ]->xStateListItem );
972         }
973
974         while( xTaskScheduled == pdFALSE )
975         {
976             #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
977             {
978                 if( uxCurrentPriority < uxTopReadyPriority )
979                 {
980                     /* We can't schedule any tasks, other than idle, that have a
981                      * priority lower than the priority of a task currently running
982                      * on another core. */
983                     uxCurrentPriority = tskIDLE_PRIORITY;
984                 }
985             }
986             #endif
987
988             if( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxCurrentPriority ] ) ) == pdFALSE )
989             {
990                 const List_t * const pxReadyList = &( pxReadyTasksLists[ uxCurrentPriority ] );
991                 const ListItem_t * pxEndMarker = listGET_END_MARKER( pxReadyList );
992                 ListItem_t * pxIterator;
993
994                 /* The ready task list for uxCurrentPriority is not empty, so uxTopReadyPriority
995                  * must not be decremented any further. */
996                 xDecrementTopPriority = pdFALSE;
997
998                 for( pxIterator = listGET_HEAD_ENTRY( pxReadyList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
999                 {
1000                     TCB_t * pxTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxIterator );
1001
1002                     #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1003                     {
1004                         /* When falling back to the idle priority because only one priority
1005                          * level is allowed to run at a time, we should ONLY schedule the true
1006                          * idle tasks, not user tasks at the idle priority. */
1007                         if( uxCurrentPriority < uxTopReadyPriority )
1008                         {
1009                             if( ( pxTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) == 0 )
1010                             {
1011                                 continue;
1012                             }
1013                         }
1014                     }
1015                     #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1016
1017                     if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
1018                     {
1019                         #if ( configUSE_CORE_AFFINITY == 1 )
1020                             if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1021                         #endif
1022                         {
1023                             /* If the task is not being executed by any core swap it in. */
1024                             pxCurrentTCBs[ xCoreID ]->xTaskRunState = taskTASK_NOT_RUNNING;
1025                             #if ( configUSE_CORE_AFFINITY == 1 )
1026                                 pxPreviousTCB = pxCurrentTCBs[ xCoreID ];
1027                             #endif
1028                             pxTCB->xTaskRunState = xCoreID;
1029                             pxCurrentTCBs[ xCoreID ] = pxTCB;
1030                             xTaskScheduled = pdTRUE;
1031                         }
1032                     }
1033                     else if( pxTCB == pxCurrentTCBs[ xCoreID ] )
1034                     {
1035                         configASSERT( ( pxTCB->xTaskRunState == xCoreID ) || ( pxTCB->xTaskRunState == taskTASK_SCHEDULED_TO_YIELD ) );
1036
1037                         #if ( configUSE_CORE_AFFINITY == 1 )
1038                             if( ( pxTCB->uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1039                         #endif
1040                         {
1041                             /* The task is already running on this core, mark it as scheduled. */
1042                             pxTCB->xTaskRunState = xCoreID;
1043                             xTaskScheduled = pdTRUE;
1044                         }
1045                     }
1046                     else
1047                     {
1048                         /* This task is running on the core other than xCoreID. */
1049                         mtCOVERAGE_TEST_MARKER();
1050                     }
1051
1052                     if( xTaskScheduled != pdFALSE )
1053                     {
1054                         /* A task has been selected to run on this core. */
1055                         break;
1056                     }
1057                 }
1058             }
1059             else
1060             {
1061                 if( xDecrementTopPriority != pdFALSE )
1062                 {
1063                     uxTopReadyPriority--;
1064                     #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1065                     {
1066                         xPriorityDropped = pdTRUE;
1067                     }
1068                     #endif
1069                 }
1070             }
1071
1072             /* There are configNUMBER_OF_CORES Idle tasks created when scheduler started.
1073              * The scheduler should be able to select a task to run when uxCurrentPriority
1074              * is tskIDLE_PRIORITY. uxCurrentPriority is never decreased to value blow
1075              * tskIDLE_PRIORITY. */
1076             if( uxCurrentPriority > tskIDLE_PRIORITY )
1077             {
1078                 uxCurrentPriority--;
1079             }
1080             else
1081             {
1082                 /* This function is called when idle task is not created. Break the
1083                  * loop to prevent uxCurrentPriority overrun. */
1084                 break;
1085             }
1086         }
1087
1088         #if ( configRUN_MULTIPLE_PRIORITIES == 0 )
1089         {
1090             if( xTaskScheduled == pdTRUE )
1091             {
1092                 if( xPriorityDropped != pdFALSE )
1093                 {
1094                     /* There may be several ready tasks that were being prevented from running because there was
1095                      * a higher priority task running. Now that the last of the higher priority tasks is no longer
1096                      * running, make sure all the other idle tasks yield. */
1097                     BaseType_t x;
1098
1099                     for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configNUMBER_OF_CORES; x++ )
1100                     {
1101                         if( ( pxCurrentTCBs[ x ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0 )
1102                         {
1103                             prvYieldCore( x );
1104                         }
1105                     }
1106                 }
1107             }
1108         }
1109         #endif /* #if ( configRUN_MULTIPLE_PRIORITIES == 0 ) */
1110
1111         #if ( configUSE_CORE_AFFINITY == 1 )
1112         {
1113             if( xTaskScheduled == pdTRUE )
1114             {
1115                 if( ( pxPreviousTCB != NULL ) && ( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxPreviousTCB->uxPriority ] ), &( pxPreviousTCB->xStateListItem ) ) != pdFALSE ) )
1116                 {
1117                     /* A ready task was just evicted from this core. See if it can be
1118                      * scheduled on any other core. */
1119                     UBaseType_t uxCoreMap = pxPreviousTCB->uxCoreAffinityMask;
1120                     BaseType_t xLowestPriority = ( BaseType_t ) pxPreviousTCB->uxPriority;
1121                     BaseType_t xLowestPriorityCore = -1;
1122                     BaseType_t x;
1123
1124                     if( ( pxPreviousTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1125                     {
1126                         xLowestPriority = xLowestPriority - 1;
1127                     }
1128
1129                     if( ( uxCoreMap & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) != 0U )
1130                     {
1131                         /* pxPreviousTCB was removed from this core and this core is not excluded
1132                          * from it's core affinity mask.
1133                          *
1134                          * pxPreviousTCB is preempted by the new higher priority task
1135                          * pxCurrentTCBs[ xCoreID ]. When searching a new core for pxPreviousTCB,
1136                          * we do not need to look at the cores on which pxCurrentTCBs[ xCoreID ]
1137                          * is allowed to run. The reason is - when more than one cores are
1138                          * eligible for an incoming task, we preempt the core with the minimum
1139                          * priority task. Because this core (i.e. xCoreID) was preempted for
1140                          * pxCurrentTCBs[ xCoreID ], this means that all the others cores
1141                          * where pxCurrentTCBs[ xCoreID ] can run, are running tasks with priority
1142                          * no lower than pxPreviousTCB's priority. Therefore, the only cores where
1143                          * which can be preempted for pxPreviousTCB are the ones where
1144                          * pxCurrentTCBs[ xCoreID ] is not allowed to run (and obviously,
1145                          * pxPreviousTCB is allowed to run).
1146                          *
1147                          * This is an optimization which reduces the number of cores needed to be
1148                          * searched for pxPreviousTCB to run. */
1149                         uxCoreMap &= ~( pxCurrentTCBs[ xCoreID ]->uxCoreAffinityMask );
1150                     }
1151                     else
1152                     {
1153                         /* pxPreviousTCB's core affinity mask is changed and it is no longer
1154                          * allowed to run on this core. Searching all the cores in pxPreviousTCB's
1155                          * new core affinity mask to find a core on which it can run. */
1156                     }
1157
1158                     uxCoreMap &= ( ( 1U << configNUMBER_OF_CORES ) - 1U );
1159
1160                     for( x = ( ( BaseType_t ) configNUMBER_OF_CORES - 1 ); x >= ( BaseType_t ) 0; x-- )
1161                     {
1162                         UBaseType_t uxCore = ( UBaseType_t ) x;
1163                         BaseType_t xTaskPriority;
1164
1165                         if( ( uxCoreMap & ( ( UBaseType_t ) 1U << uxCore ) ) != 0U )
1166                         {
1167                             xTaskPriority = ( BaseType_t ) pxCurrentTCBs[ uxCore ]->uxPriority;
1168
1169                             if( ( pxCurrentTCBs[ uxCore ]->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1170                             {
1171                                 xTaskPriority = xTaskPriority - ( BaseType_t ) 1;
1172                             }
1173
1174                             uxCoreMap &= ~( ( UBaseType_t ) 1U << uxCore );
1175
1176                             if( ( xTaskPriority < xLowestPriority ) &&
1177                                 ( taskTASK_IS_RUNNING( pxCurrentTCBs[ uxCore ] ) != pdFALSE ) &&
1178                                 ( xYieldPendings[ uxCore ] == pdFALSE ) )
1179                             {
1180                                 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1181                                     if( pxCurrentTCBs[ uxCore ]->xPreemptionDisable == pdFALSE )
1182                                 #endif
1183                                 {
1184                                     xLowestPriority = xTaskPriority;
1185                                     xLowestPriorityCore = ( BaseType_t ) uxCore;
1186                                 }
1187                             }
1188                         }
1189                     }
1190
1191                     if( xLowestPriorityCore >= 0 )
1192                     {
1193                         prvYieldCore( xLowestPriorityCore );
1194                     }
1195                 }
1196             }
1197         }
1198         #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) */
1199     }
1200
1201 #endif /* ( configNUMBER_OF_CORES > 1 ) */
1202
1203 /*-----------------------------------------------------------*/
1204
1205 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1206
1207     TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
1208                                     const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1209                                     const uint32_t ulStackDepth,
1210                                     void * const pvParameters,
1211                                     UBaseType_t uxPriority,
1212                                     StackType_t * const puxStackBuffer,
1213                                     StaticTask_t * const pxTaskBuffer )
1214     #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1215     {
1216         return xTaskCreateStaticAffinitySet( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer, tskNO_AFFINITY );
1217     }
1218
1219     TaskHandle_t xTaskCreateStaticAffinitySet( TaskFunction_t pxTaskCode,
1220                                                const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1221                                                const uint32_t ulStackDepth,
1222                                                void * const pvParameters,
1223                                                UBaseType_t uxPriority,
1224                                                StackType_t * const puxStackBuffer,
1225                                                StaticTask_t * const pxTaskBuffer,
1226                                                UBaseType_t uxCoreAffinityMask )
1227     #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1228     {
1229         TCB_t * pxNewTCB;
1230         TaskHandle_t xReturn;
1231
1232         traceENTER_xTaskCreateStatic( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, puxStackBuffer, pxTaskBuffer );
1233
1234         configASSERT( puxStackBuffer != NULL );
1235         configASSERT( pxTaskBuffer != NULL );
1236
1237         #if ( configASSERT_DEFINED == 1 )
1238         {
1239             /* Sanity check that the size of the structure used to declare a
1240              * variable of type StaticTask_t equals the size of the real task
1241              * structure. */
1242             volatile size_t xSize = sizeof( StaticTask_t );
1243             configASSERT( xSize == sizeof( TCB_t ) );
1244             ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
1245         }
1246         #endif /* configASSERT_DEFINED */
1247
1248         if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
1249         {
1250             /* The memory used for the task's TCB and stack are passed into this
1251              * function - use them. */
1252             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. */
1253             ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1254             pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
1255
1256             #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
1257             {
1258                 /* Tasks can be created statically or dynamically, so note this
1259                  * task was created statically in case the task is later deleted. */
1260                 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1261             }
1262             #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1263
1264             prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
1265
1266             #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1267             {
1268                 /* Set the task's affinity before scheduling it. */
1269                 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1270             }
1271             #endif
1272
1273             prvAddNewTaskToReadyList( pxNewTCB );
1274         }
1275         else
1276         {
1277             xReturn = NULL;
1278         }
1279
1280         traceRETURN_xTaskCreateStatic( xReturn );
1281
1282         return xReturn;
1283     }
1284
1285 #endif /* SUPPORT_STATIC_ALLOCATION */
1286 /*-----------------------------------------------------------*/
1287
1288 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
1289
1290     BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
1291                                             TaskHandle_t * pxCreatedTask )
1292     #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1293     {
1294         return xTaskCreateRestrictedStaticAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1295     }
1296
1297     BaseType_t xTaskCreateRestrictedStaticAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1298                                                        UBaseType_t uxCoreAffinityMask,
1299                                                        TaskHandle_t * pxCreatedTask )
1300     #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1301     {
1302         TCB_t * pxNewTCB;
1303         BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1304
1305         traceENTER_xTaskCreateRestrictedStatic( pxTaskDefinition, pxCreatedTask );
1306
1307         configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
1308         configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
1309
1310         if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
1311         {
1312             /* Allocate space for the TCB.  Where the memory comes from depends
1313              * on the implementation of the port malloc function and whether or
1314              * not static allocation is being used. */
1315             pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
1316             ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1317
1318             /* Store the stack location in the TCB. */
1319             pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1320
1321             #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1322             {
1323                 /* Tasks can be created statically or dynamically, so note this
1324                  * task was created statically in case the task is later deleted. */
1325                 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
1326             }
1327             #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1328
1329             prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1330                                   pxTaskDefinition->pcName,
1331                                   ( uint32_t ) pxTaskDefinition->usStackDepth,
1332                                   pxTaskDefinition->pvParameters,
1333                                   pxTaskDefinition->uxPriority,
1334                                   pxCreatedTask, pxNewTCB,
1335                                   pxTaskDefinition->xRegions );
1336
1337             #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1338             {
1339                 /* Set the task's affinity before scheduling it. */
1340                 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1341             }
1342             #endif
1343
1344             prvAddNewTaskToReadyList( pxNewTCB );
1345             xReturn = pdPASS;
1346         }
1347
1348         traceRETURN_xTaskCreateRestrictedStatic( xReturn );
1349
1350         return xReturn;
1351     }
1352
1353 #endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1354 /*-----------------------------------------------------------*/
1355
1356 #if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
1357
1358     BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
1359                                       TaskHandle_t * pxCreatedTask )
1360     #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1361     {
1362         return xTaskCreateRestrictedAffinitySet( pxTaskDefinition, tskNO_AFFINITY, pxCreatedTask );
1363     }
1364
1365     BaseType_t xTaskCreateRestrictedAffinitySet( const TaskParameters_t * const pxTaskDefinition,
1366                                                  UBaseType_t uxCoreAffinityMask,
1367                                                  TaskHandle_t * pxCreatedTask )
1368     #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1369     {
1370         TCB_t * pxNewTCB;
1371         BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1372
1373         traceENTER_xTaskCreateRestricted( pxTaskDefinition, pxCreatedTask );
1374
1375         configASSERT( pxTaskDefinition->puxStackBuffer );
1376
1377         if( pxTaskDefinition->puxStackBuffer != NULL )
1378         {
1379             pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1380
1381             if( pxNewTCB != NULL )
1382             {
1383                 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1384
1385                 /* Store the stack location in the TCB. */
1386                 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
1387
1388                 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
1389                 {
1390                     /* Tasks can be created statically or dynamically, so note
1391                      * this task had a statically allocated stack in case it is
1392                      * later deleted.  The TCB was allocated dynamically. */
1393                     pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
1394                 }
1395                 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1396
1397                 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
1398                                       pxTaskDefinition->pcName,
1399                                       ( uint32_t ) pxTaskDefinition->usStackDepth,
1400                                       pxTaskDefinition->pvParameters,
1401                                       pxTaskDefinition->uxPriority,
1402                                       pxCreatedTask, pxNewTCB,
1403                                       pxTaskDefinition->xRegions );
1404
1405                 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1406                 {
1407                     /* Set the task's affinity before scheduling it. */
1408                     pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1409                 }
1410                 #endif
1411
1412                 prvAddNewTaskToReadyList( pxNewTCB );
1413                 xReturn = pdPASS;
1414             }
1415         }
1416
1417         traceRETURN_xTaskCreateRestricted( xReturn );
1418
1419         return xReturn;
1420     }
1421
1422 #endif /* portUSING_MPU_WRAPPERS */
1423 /*-----------------------------------------------------------*/
1424
1425 #if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
1426
1427     BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
1428                             const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1429                             const configSTACK_DEPTH_TYPE usStackDepth,
1430                             void * const pvParameters,
1431                             UBaseType_t uxPriority,
1432                             TaskHandle_t * const pxCreatedTask )
1433     #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1434     {
1435         return xTaskCreateAffinitySet( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, tskNO_AFFINITY, pxCreatedTask );
1436     }
1437
1438     BaseType_t xTaskCreateAffinitySet( TaskFunction_t pxTaskCode,
1439                                        const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1440                                        const configSTACK_DEPTH_TYPE usStackDepth,
1441                                        void * const pvParameters,
1442                                        UBaseType_t uxPriority,
1443                                        UBaseType_t uxCoreAffinityMask,
1444                                        TaskHandle_t * const pxCreatedTask )
1445     #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
1446     {
1447         TCB_t * pxNewTCB;
1448         BaseType_t xReturn;
1449
1450         traceENTER_xTaskCreate( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask );
1451
1452         /* If the stack grows down then allocate the stack then the TCB so the stack
1453          * does not grow into the TCB.  Likewise if the stack grows up then allocate
1454          * the TCB then the stack. */
1455         #if ( portSTACK_GROWTH > 0 )
1456         {
1457             /* Allocate space for the TCB.  Where the memory comes from depends on
1458              * the implementation of the port malloc function and whether or not static
1459              * allocation is being used. */
1460             pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
1461
1462             if( pxNewTCB != NULL )
1463             {
1464                 ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1465
1466                 /* Allocate space for the stack used by the task being created.
1467                  * The base of the stack memory stored in the TCB so the task can
1468                  * be deleted later if required. */
1469                 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1470
1471                 if( pxNewTCB->pxStack == NULL )
1472                 {
1473                     /* Could not allocate the stack.  Delete the allocated TCB. */
1474                     vPortFree( pxNewTCB );
1475                     pxNewTCB = NULL;
1476                 }
1477             }
1478         }
1479         #else /* portSTACK_GROWTH */
1480         {
1481             StackType_t * pxStack;
1482
1483             /* Allocate space for the stack used by the task being created. */
1484             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. */
1485
1486             if( pxStack != NULL )
1487             {
1488                 /* Allocate space for the TCB. */
1489                 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. */
1490
1491                 if( pxNewTCB != NULL )
1492                 {
1493                     ( void ) memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
1494
1495                     /* Store the stack location in the TCB. */
1496                     pxNewTCB->pxStack = pxStack;
1497                 }
1498                 else
1499                 {
1500                     /* The stack cannot be used as the TCB was not created.  Free
1501                      * it again. */
1502                     vPortFreeStack( pxStack );
1503                 }
1504             }
1505             else
1506             {
1507                 pxNewTCB = NULL;
1508             }
1509         }
1510         #endif /* portSTACK_GROWTH */
1511
1512         if( pxNewTCB != NULL )
1513         {
1514             #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
1515             {
1516                 /* Tasks can be created statically or dynamically, so note this
1517                  * task was created dynamically in case it is later deleted. */
1518                 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
1519             }
1520             #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
1521
1522             prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
1523
1524             #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1525             {
1526                 /* Set the task's affinity before scheduling it. */
1527                 pxNewTCB->uxCoreAffinityMask = uxCoreAffinityMask;
1528             }
1529             #endif
1530
1531             prvAddNewTaskToReadyList( pxNewTCB );
1532             xReturn = pdPASS;
1533         }
1534         else
1535         {
1536             xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
1537         }
1538
1539         traceRETURN_xTaskCreate( xReturn );
1540
1541         return xReturn;
1542     }
1543
1544 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
1545 /*-----------------------------------------------------------*/
1546
1547 static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
1548                                   const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
1549                                   const uint32_t ulStackDepth,
1550                                   void * const pvParameters,
1551                                   UBaseType_t uxPriority,
1552                                   TaskHandle_t * const pxCreatedTask,
1553                                   TCB_t * pxNewTCB,
1554                                   const MemoryRegion_t * const xRegions )
1555 {
1556     StackType_t * pxTopOfStack;
1557     UBaseType_t x;
1558
1559     #if ( portUSING_MPU_WRAPPERS == 1 )
1560         /* Should the task be created in privileged mode? */
1561         BaseType_t xRunPrivileged;
1562
1563         if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
1564         {
1565             xRunPrivileged = pdTRUE;
1566         }
1567         else
1568         {
1569             xRunPrivileged = pdFALSE;
1570         }
1571         uxPriority &= ~portPRIVILEGE_BIT;
1572     #endif /* portUSING_MPU_WRAPPERS == 1 */
1573
1574     /* Avoid dependency on memset() if it is not required. */
1575     #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
1576     {
1577         /* Fill the stack with a known value to assist debugging. */
1578         ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
1579     }
1580     #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
1581
1582     /* Calculate the top of stack address.  This depends on whether the stack
1583      * grows from high memory to low (as per the 80x86) or vice versa.
1584      * portSTACK_GROWTH is used to make the result positive or negative as required
1585      * by the port. */
1586     #if ( portSTACK_GROWTH < 0 )
1587     {
1588         pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
1589         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(). */
1590
1591         /* Check the alignment of the calculated top of stack is correct. */
1592         configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1593
1594         #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
1595         {
1596             /* Also record the stack's high address, which may assist
1597              * debugging. */
1598             pxNewTCB->pxEndOfStack = pxTopOfStack;
1599         }
1600         #endif /* configRECORD_STACK_HIGH_ADDRESS */
1601     }
1602     #else /* portSTACK_GROWTH */
1603     {
1604         pxTopOfStack = pxNewTCB->pxStack;
1605         pxTopOfStack = ( StackType_t * ) ( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) + portBYTE_ALIGNMENT_MASK ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 !e9033 !e9078 MISRA exception.  Avoiding casts between pointers and integers is not practical.  Size differences accounted for using portPOINTER_SIZE_TYPE type.  Checked by assert(). */
1606
1607         /* Check the alignment of the calculated top of stack is correct. */
1608         configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
1609
1610         /* The other extreme of the stack space is required if stack checking is
1611          * performed. */
1612         pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
1613     }
1614     #endif /* portSTACK_GROWTH */
1615
1616     /* Store the task name in the TCB. */
1617     if( pcName != NULL )
1618     {
1619         for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
1620         {
1621             pxNewTCB->pcTaskName[ x ] = pcName[ x ];
1622
1623             /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
1624              * configMAX_TASK_NAME_LEN characters just in case the memory after the
1625              * string is not accessible (extremely unlikely). */
1626             if( pcName[ x ] == ( char ) 0x00 )
1627             {
1628                 break;
1629             }
1630             else
1631             {
1632                 mtCOVERAGE_TEST_MARKER();
1633             }
1634         }
1635
1636         /* Ensure the name string is terminated in the case that the string length
1637          * was greater or equal to configMAX_TASK_NAME_LEN. */
1638         pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
1639     }
1640     else
1641     {
1642         mtCOVERAGE_TEST_MARKER();
1643     }
1644
1645     /* This is used as an array index so must ensure it's not too large. */
1646     configASSERT( uxPriority < configMAX_PRIORITIES );
1647
1648     if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1649     {
1650         uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1651     }
1652     else
1653     {
1654         mtCOVERAGE_TEST_MARKER();
1655     }
1656
1657     pxNewTCB->uxPriority = uxPriority;
1658     #if ( configUSE_MUTEXES == 1 )
1659     {
1660         pxNewTCB->uxBasePriority = uxPriority;
1661     }
1662     #endif /* configUSE_MUTEXES */
1663
1664     vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
1665     vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
1666
1667     /* Set the pxNewTCB as a link back from the ListItem_t.  This is so we can get
1668      * back to  the containing TCB from a generic item in a list. */
1669     listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
1670
1671     /* Event lists are always in priority order. */
1672     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. */
1673     listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
1674
1675     #if ( portUSING_MPU_WRAPPERS == 1 )
1676     {
1677         vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
1678     }
1679     #else
1680     {
1681         /* Avoid compiler warning about unreferenced parameter. */
1682         ( void ) xRegions;
1683     }
1684     #endif
1685
1686     #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
1687     {
1688         /* Allocate and initialize memory for the task's TLS Block. */
1689         configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock, pxTopOfStack );
1690     }
1691     #endif
1692
1693     #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
1694     {
1695         pxNewTCB->uxCoreAffinityMask = tskNO_AFFINITY;
1696     }
1697     #endif
1698
1699     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
1700     {
1701         pxNewTCB->xPreemptionDisable = 0;
1702     }
1703     #endif
1704
1705     /* Initialize the TCB stack to look as if the task was already running,
1706      * but had been interrupted by the scheduler.  The return address is set
1707      * to the start of the task function. Once the stack has been initialised
1708      * the top of stack variable is updated. */
1709     #if ( portUSING_MPU_WRAPPERS == 1 )
1710     {
1711         /* If the port has capability to detect stack overflow,
1712          * pass the stack end address to the stack initialization
1713          * function as well. */
1714         #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1715         {
1716             #if ( portSTACK_GROWTH < 0 )
1717             {
1718                 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1719             }
1720             #else /* portSTACK_GROWTH */
1721             {
1722                 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1723             }
1724             #endif /* portSTACK_GROWTH */
1725         }
1726         #else /* portHAS_STACK_OVERFLOW_CHECKING */
1727         {
1728             pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged, &( pxNewTCB->xMPUSettings ) );
1729         }
1730         #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1731     }
1732     #else /* portUSING_MPU_WRAPPERS */
1733     {
1734         /* If the port has capability to detect stack overflow,
1735          * pass the stack end address to the stack initialization
1736          * function as well. */
1737         #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1738         {
1739             #if ( portSTACK_GROWTH < 0 )
1740             {
1741                 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1742             }
1743             #else /* portSTACK_GROWTH */
1744             {
1745                 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1746             }
1747             #endif /* portSTACK_GROWTH */
1748         }
1749         #else /* portHAS_STACK_OVERFLOW_CHECKING */
1750         {
1751             pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1752         }
1753         #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1754     }
1755     #endif /* portUSING_MPU_WRAPPERS */
1756
1757     /* Initialize task state and task attributes. */
1758     #if ( configNUMBER_OF_CORES > 1 )
1759     {
1760         pxNewTCB->xTaskRunState = taskTASK_NOT_RUNNING;
1761
1762         /* Is this an idle task? */
1763         if( ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvIdleTask ) || ( ( TaskFunction_t ) pxTaskCode == ( TaskFunction_t ) prvMinimalIdleTask ) )
1764         {
1765             pxNewTCB->uxTaskAttributes |= taskATTRIBUTE_IS_IDLE;
1766         }
1767     }
1768     #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
1769
1770     if( pxCreatedTask != NULL )
1771     {
1772         /* Pass the handle out in an anonymous way.  The handle can be used to
1773          * change the created task's priority, delete the created task, etc.*/
1774         *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1775     }
1776     else
1777     {
1778         mtCOVERAGE_TEST_MARKER();
1779     }
1780 }
1781 /*-----------------------------------------------------------*/
1782
1783 #if ( configNUMBER_OF_CORES == 1 )
1784
1785     static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1786     {
1787         /* Ensure interrupts don't access the task lists while the lists are being
1788          * updated. */
1789         taskENTER_CRITICAL();
1790         {
1791             uxCurrentNumberOfTasks++;
1792
1793             if( pxCurrentTCB == NULL )
1794             {
1795                 /* There are no other tasks, or all the other tasks are in
1796                  * the suspended state - make this the current task. */
1797                 pxCurrentTCB = pxNewTCB;
1798
1799                 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1800                 {
1801                     /* This is the first task to be created so do the preliminary
1802                      * initialisation required.  We will not recover if this call
1803                      * fails, but we will report the failure. */
1804                     prvInitialiseTaskLists();
1805                 }
1806                 else
1807                 {
1808                     mtCOVERAGE_TEST_MARKER();
1809                 }
1810             }
1811             else
1812             {
1813                 /* If the scheduler is not already running, make this task the
1814                  * current task if it is the highest priority task to be created
1815                  * so far. */
1816                 if( xSchedulerRunning == pdFALSE )
1817                 {
1818                     if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
1819                     {
1820                         pxCurrentTCB = pxNewTCB;
1821                     }
1822                     else
1823                     {
1824                         mtCOVERAGE_TEST_MARKER();
1825                     }
1826                 }
1827                 else
1828                 {
1829                     mtCOVERAGE_TEST_MARKER();
1830                 }
1831             }
1832
1833             uxTaskNumber++;
1834
1835             #if ( configUSE_TRACE_FACILITY == 1 )
1836             {
1837                 /* Add a counter into the TCB for tracing only. */
1838                 pxNewTCB->uxTCBNumber = uxTaskNumber;
1839             }
1840             #endif /* configUSE_TRACE_FACILITY */
1841             traceTASK_CREATE( pxNewTCB );
1842
1843             prvAddTaskToReadyList( pxNewTCB );
1844
1845             portSETUP_TCB( pxNewTCB );
1846         }
1847         taskEXIT_CRITICAL();
1848
1849         if( xSchedulerRunning != pdFALSE )
1850         {
1851             /* If the created task is of a higher priority than the current task
1852              * then it should run now. */
1853             taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
1854         }
1855         else
1856         {
1857             mtCOVERAGE_TEST_MARKER();
1858         }
1859     }
1860
1861 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
1862
1863     static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
1864     {
1865         /* Ensure interrupts don't access the task lists while the lists are being
1866          * updated. */
1867         taskENTER_CRITICAL();
1868         {
1869             uxCurrentNumberOfTasks++;
1870
1871             if( xSchedulerRunning == pdFALSE )
1872             {
1873                 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1874                 {
1875                     /* This is the first task to be created so do the preliminary
1876                      * initialisation required.  We will not recover if this call
1877                      * fails, but we will report the failure. */
1878                     prvInitialiseTaskLists();
1879                 }
1880                 else
1881                 {
1882                     mtCOVERAGE_TEST_MARKER();
1883                 }
1884
1885                 if( ( pxNewTCB->uxTaskAttributes & taskATTRIBUTE_IS_IDLE ) != 0U )
1886                 {
1887                     BaseType_t xCoreID;
1888
1889                     /* Check if a core is free. */
1890                     for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
1891                     {
1892                         if( pxCurrentTCBs[ xCoreID ] == NULL )
1893                         {
1894                             pxNewTCB->xTaskRunState = xCoreID;
1895                             pxCurrentTCBs[ xCoreID ] = pxNewTCB;
1896                             break;
1897                         }
1898                         else
1899                         {
1900                             mtCOVERAGE_TEST_MARKER();
1901                         }
1902                     }
1903                 }
1904                 else
1905                 {
1906                     mtCOVERAGE_TEST_MARKER();
1907                 }
1908             }
1909
1910             uxTaskNumber++;
1911
1912             #if ( configUSE_TRACE_FACILITY == 1 )
1913             {
1914                 /* Add a counter into the TCB for tracing only. */
1915                 pxNewTCB->uxTCBNumber = uxTaskNumber;
1916             }
1917             #endif /* configUSE_TRACE_FACILITY */
1918             traceTASK_CREATE( pxNewTCB );
1919
1920             prvAddTaskToReadyList( pxNewTCB );
1921
1922             portSETUP_TCB( pxNewTCB );
1923
1924             if( xSchedulerRunning != pdFALSE )
1925             {
1926                 /* If the created task is of a higher priority than another
1927                  * currently running task and preemption is on then it should
1928                  * run now. */
1929                 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxNewTCB );
1930             }
1931             else
1932             {
1933                 mtCOVERAGE_TEST_MARKER();
1934             }
1935         }
1936         taskEXIT_CRITICAL();
1937     }
1938
1939 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
1940 /*-----------------------------------------------------------*/
1941
1942 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
1943
1944     static size_t prvSnprintfReturnValueToCharsWritten( int iSnprintfReturnValue,
1945                                                         size_t n )
1946     {
1947         size_t uxCharsWritten;
1948
1949         if( iSnprintfReturnValue < 0 )
1950         {
1951             /* Encoding error - Return 0 to indicate that nothing
1952              * was written to the buffer. */
1953             uxCharsWritten = 0;
1954         }
1955         else if( iSnprintfReturnValue >= ( int ) n )
1956         {
1957             /* This is the case when the supplied buffer is not
1958              * large to hold the generated string. Return the
1959              * number of characters actually written without
1960              * counting the terminating NULL character. */
1961             uxCharsWritten = n - 1;
1962         }
1963         else
1964         {
1965             /* Complete string was written to the buffer. */
1966             uxCharsWritten = ( size_t ) iSnprintfReturnValue;
1967         }
1968
1969         return uxCharsWritten;
1970     }
1971
1972 #endif /* #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
1973 /*-----------------------------------------------------------*/
1974
1975 #if ( INCLUDE_vTaskDelete == 1 )
1976
1977     void vTaskDelete( TaskHandle_t xTaskToDelete )
1978     {
1979         TCB_t * pxTCB;
1980
1981         traceENTER_vTaskDelete( xTaskToDelete );
1982
1983         taskENTER_CRITICAL();
1984         {
1985             /* If null is passed in here then it is the calling task that is
1986              * being deleted. */
1987             pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1988
1989             /* Remove task from the ready/delayed list. */
1990             if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1991             {
1992                 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1993             }
1994             else
1995             {
1996                 mtCOVERAGE_TEST_MARKER();
1997             }
1998
1999             /* Is the task waiting on an event also? */
2000             if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2001             {
2002                 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2003             }
2004             else
2005             {
2006                 mtCOVERAGE_TEST_MARKER();
2007             }
2008
2009             /* Increment the uxTaskNumber also so kernel aware debuggers can
2010              * detect that the task lists need re-generating.  This is done before
2011              * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
2012              * not return. */
2013             uxTaskNumber++;
2014
2015             /* If the task is running (or yielding), we must add it to the
2016              * termination list so that an idle task can delete it when it is
2017              * no longer running. */
2018             if( taskTASK_IS_RUNNING_OR_SCHEDULED_TO_YIELD( pxTCB ) != pdFALSE )
2019             {
2020                 /* A running task or a task which is scheduled to yield is being
2021                  * deleted. This cannot complete when the task is still running
2022                  * on a core, as a context switch to another task is required.
2023                  * Place the task in the termination list. The idle task will check
2024                  * the termination list and free up any memory allocated by the
2025                  * scheduler for the TCB and stack of the deleted task. */
2026                 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
2027
2028                 /* Increment the ucTasksDeleted variable so the idle task knows
2029                  * there is a task that has been deleted and that it should therefore
2030                  * check the xTasksWaitingTermination list. */
2031                 ++uxDeletedTasksWaitingCleanUp;
2032
2033                 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
2034                  * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
2035                 traceTASK_DELETE( pxTCB );
2036
2037                 /* The pre-delete hook is primarily for the Windows simulator,
2038                  * in which Windows specific clean up operations are performed,
2039                  * after which it is not possible to yield away from this task -
2040                  * hence xYieldPending is used to latch that a context switch is
2041                  * required. */
2042                 #if ( configNUMBER_OF_CORES == 1 )
2043                     portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ 0 ] ) );
2044                 #else
2045                     portPRE_TASK_DELETE_HOOK( pxTCB, &( xYieldPendings[ pxTCB->xTaskRunState ] ) );
2046                 #endif
2047             }
2048             else
2049             {
2050                 --uxCurrentNumberOfTasks;
2051                 traceTASK_DELETE( pxTCB );
2052
2053                 /* Reset the next expected unblock time in case it referred to
2054                  * the task that has just been deleted. */
2055                 prvResetNextTaskUnblockTime();
2056             }
2057         }
2058
2059         #if ( configNUMBER_OF_CORES == 1 )
2060         {
2061             taskEXIT_CRITICAL();
2062
2063             /* If the task is not deleting itself, call prvDeleteTCB from outside of
2064              * critical section. If a task deletes itself, prvDeleteTCB is called
2065              * from prvCheckTasksWaitingTermination which is called from Idle task. */
2066             if( pxTCB != pxCurrentTCB )
2067             {
2068                 prvDeleteTCB( pxTCB );
2069             }
2070
2071             /* Force a reschedule if it is the currently running task that has just
2072              * been deleted. */
2073             if( xSchedulerRunning != pdFALSE )
2074             {
2075                 if( pxTCB == pxCurrentTCB )
2076                 {
2077                     configASSERT( uxSchedulerSuspended == 0 );
2078                     portYIELD_WITHIN_API();
2079                 }
2080                 else
2081                 {
2082                     mtCOVERAGE_TEST_MARKER();
2083                 }
2084             }
2085         }
2086         #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2087         {
2088             /* If a running task is not deleting itself, call prvDeleteTCB. If a running
2089              * task deletes itself, prvDeleteTCB is called from prvCheckTasksWaitingTermination
2090              * which is called from Idle task. */
2091             if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
2092             {
2093                 prvDeleteTCB( pxTCB );
2094             }
2095
2096             /* Force a reschedule if the task that has just been deleted was running. */
2097             if( ( xSchedulerRunning != pdFALSE ) && ( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE ) )
2098             {
2099                 if( pxTCB->xTaskRunState == ( BaseType_t ) portGET_CORE_ID() )
2100                 {
2101                     configASSERT( uxSchedulerSuspended == 0 );
2102                     vTaskYieldWithinAPI();
2103                 }
2104                 else
2105                 {
2106                     prvYieldCore( pxTCB->xTaskRunState );
2107                 }
2108             }
2109
2110             taskEXIT_CRITICAL();
2111         }
2112         #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2113
2114         traceRETURN_vTaskDelete();
2115     }
2116
2117 #endif /* INCLUDE_vTaskDelete */
2118 /*-----------------------------------------------------------*/
2119
2120 #if ( INCLUDE_xTaskDelayUntil == 1 )
2121
2122     BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
2123                                 const TickType_t xTimeIncrement )
2124     {
2125         TickType_t xTimeToWake;
2126         BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
2127
2128         traceENTER_xTaskDelayUntil( pxPreviousWakeTime, xTimeIncrement );
2129
2130         configASSERT( pxPreviousWakeTime );
2131         configASSERT( ( xTimeIncrement > 0U ) );
2132
2133         vTaskSuspendAll();
2134         {
2135             /* Minor optimisation.  The tick count cannot change in this
2136              * block. */
2137             const TickType_t xConstTickCount = xTickCount;
2138
2139             configASSERT( uxSchedulerSuspended == 1U );
2140
2141             /* Generate the tick time at which the task wants to wake. */
2142             xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
2143
2144             if( xConstTickCount < *pxPreviousWakeTime )
2145             {
2146                 /* The tick count has overflowed since this function was
2147                  * lasted called.  In this case the only time we should ever
2148                  * actually delay is if the wake time has also  overflowed,
2149                  * and the wake time is greater than the tick time.  When this
2150                  * is the case it is as if neither time had overflowed. */
2151                 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
2152                 {
2153                     xShouldDelay = pdTRUE;
2154                 }
2155                 else
2156                 {
2157                     mtCOVERAGE_TEST_MARKER();
2158                 }
2159             }
2160             else
2161             {
2162                 /* The tick time has not overflowed.  In this case we will
2163                  * delay if either the wake time has overflowed, and/or the
2164                  * tick time is less than the wake time. */
2165                 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
2166                 {
2167                     xShouldDelay = pdTRUE;
2168                 }
2169                 else
2170                 {
2171                     mtCOVERAGE_TEST_MARKER();
2172                 }
2173             }
2174
2175             /* Update the wake time ready for the next call. */
2176             *pxPreviousWakeTime = xTimeToWake;
2177
2178             if( xShouldDelay != pdFALSE )
2179             {
2180                 traceTASK_DELAY_UNTIL( xTimeToWake );
2181
2182                 /* prvAddCurrentTaskToDelayedList() needs the block time, not
2183                  * the time to wake, so subtract the current tick count. */
2184                 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
2185             }
2186             else
2187             {
2188                 mtCOVERAGE_TEST_MARKER();
2189             }
2190         }
2191         xAlreadyYielded = xTaskResumeAll();
2192
2193         /* Force a reschedule if xTaskResumeAll has not already done so, we may
2194          * have put ourselves to sleep. */
2195         if( xAlreadyYielded == pdFALSE )
2196         {
2197             taskYIELD_WITHIN_API();
2198         }
2199         else
2200         {
2201             mtCOVERAGE_TEST_MARKER();
2202         }
2203
2204         traceRETURN_xTaskDelayUntil( xShouldDelay );
2205
2206         return xShouldDelay;
2207     }
2208
2209 #endif /* INCLUDE_xTaskDelayUntil */
2210 /*-----------------------------------------------------------*/
2211
2212 #if ( INCLUDE_vTaskDelay == 1 )
2213
2214     void vTaskDelay( const TickType_t xTicksToDelay )
2215     {
2216         BaseType_t xAlreadyYielded = pdFALSE;
2217
2218         traceENTER_vTaskDelay( xTicksToDelay );
2219
2220         /* A delay time of zero just forces a reschedule. */
2221         if( xTicksToDelay > ( TickType_t ) 0U )
2222         {
2223             vTaskSuspendAll();
2224             {
2225                 configASSERT( uxSchedulerSuspended == 1U );
2226
2227                 traceTASK_DELAY();
2228
2229                 /* A task that is removed from the event list while the
2230                  * scheduler is suspended will not get placed in the ready
2231                  * list or removed from the blocked list until the scheduler
2232                  * is resumed.
2233                  *
2234                  * This task cannot be in an event list as it is the currently
2235                  * executing task. */
2236                 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
2237             }
2238             xAlreadyYielded = xTaskResumeAll();
2239         }
2240         else
2241         {
2242             mtCOVERAGE_TEST_MARKER();
2243         }
2244
2245         /* Force a reschedule if xTaskResumeAll has not already done so, we may
2246          * have put ourselves to sleep. */
2247         if( xAlreadyYielded == pdFALSE )
2248         {
2249             taskYIELD_WITHIN_API();
2250         }
2251         else
2252         {
2253             mtCOVERAGE_TEST_MARKER();
2254         }
2255
2256         traceRETURN_vTaskDelay();
2257     }
2258
2259 #endif /* INCLUDE_vTaskDelay */
2260 /*-----------------------------------------------------------*/
2261
2262 #if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
2263
2264     eTaskState eTaskGetState( TaskHandle_t xTask )
2265     {
2266         eTaskState eReturn;
2267         List_t const * pxStateList;
2268         List_t const * pxEventList;
2269         List_t const * pxDelayedList;
2270         List_t const * pxOverflowedDelayedList;
2271         const TCB_t * const pxTCB = xTask;
2272
2273         traceENTER_eTaskGetState( xTask );
2274
2275         configASSERT( pxTCB );
2276
2277         #if ( configNUMBER_OF_CORES == 1 )
2278             if( pxTCB == pxCurrentTCB )
2279             {
2280                 /* The task calling this function is querying its own state. */
2281                 eReturn = eRunning;
2282             }
2283             else
2284         #endif
2285         {
2286             taskENTER_CRITICAL();
2287             {
2288                 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
2289                 pxEventList = listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) );
2290                 pxDelayedList = pxDelayedTaskList;
2291                 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
2292             }
2293             taskEXIT_CRITICAL();
2294
2295             if( pxEventList == &xPendingReadyList )
2296             {
2297                 /* The task has been placed on the pending ready list, so its
2298                  * state is eReady regardless of what list the task's state list
2299                  * item is currently placed on. */
2300                 eReturn = eReady;
2301             }
2302             else if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
2303             {
2304                 /* The task being queried is referenced from one of the Blocked
2305                  * lists. */
2306                 eReturn = eBlocked;
2307             }
2308
2309             #if ( INCLUDE_vTaskSuspend == 1 )
2310                 else if( pxStateList == &xSuspendedTaskList )
2311                 {
2312                     /* The task being queried is referenced from the suspended
2313                      * list.  Is it genuinely suspended or is it blocked
2314                      * indefinitely? */
2315                     if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
2316                     {
2317                         #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2318                         {
2319                             BaseType_t x;
2320
2321                             /* The task does not appear on the event list item of
2322                              * and of the RTOS objects, but could still be in the
2323                              * blocked state if it is waiting on its notification
2324                              * rather than waiting on an object.  If not, is
2325                              * suspended. */
2326                             eReturn = eSuspended;
2327
2328                             for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2329                             {
2330                                 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2331                                 {
2332                                     eReturn = eBlocked;
2333                                     break;
2334                                 }
2335                             }
2336                         }
2337                         #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2338                         {
2339                             eReturn = eSuspended;
2340                         }
2341                         #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2342                     }
2343                     else
2344                     {
2345                         eReturn = eBlocked;
2346                     }
2347                 }
2348             #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
2349
2350             #if ( INCLUDE_vTaskDelete == 1 )
2351                 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
2352                 {
2353                     /* The task being queried is referenced from the deleted
2354                      * tasks list, or it is not referenced from any lists at
2355                      * all. */
2356                     eReturn = eDeleted;
2357                 }
2358             #endif
2359
2360             else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
2361             {
2362                 #if ( configNUMBER_OF_CORES == 1 )
2363                 {
2364                     /* If the task is not in any other state, it must be in the
2365                      * Ready (including pending ready) state. */
2366                     eReturn = eReady;
2367                 }
2368                 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2369                 {
2370                     if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2371                     {
2372                         /* Is it actively running on a core? */
2373                         eReturn = eRunning;
2374                     }
2375                     else
2376                     {
2377                         /* If the task is not in any other state, it must be in the
2378                          * Ready (including pending ready) state. */
2379                         eReturn = eReady;
2380                     }
2381                 }
2382                 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2383             }
2384         }
2385
2386         traceRETURN_eTaskGetState( eReturn );
2387
2388         return eReturn;
2389     } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
2390
2391 #endif /* INCLUDE_eTaskGetState */
2392 /*-----------------------------------------------------------*/
2393
2394 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2395
2396     UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
2397     {
2398         TCB_t const * pxTCB;
2399         UBaseType_t uxReturn;
2400
2401         traceENTER_uxTaskPriorityGet( xTask );
2402
2403         taskENTER_CRITICAL();
2404         {
2405             /* If null is passed in here then it is the priority of the task
2406              * that called uxTaskPriorityGet() that is being queried. */
2407             pxTCB = prvGetTCBFromHandle( xTask );
2408             uxReturn = pxTCB->uxPriority;
2409         }
2410         taskEXIT_CRITICAL();
2411
2412         traceRETURN_uxTaskPriorityGet( uxReturn );
2413
2414         return uxReturn;
2415     }
2416
2417 #endif /* INCLUDE_uxTaskPriorityGet */
2418 /*-----------------------------------------------------------*/
2419
2420 #if ( INCLUDE_uxTaskPriorityGet == 1 )
2421
2422     UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
2423     {
2424         TCB_t const * pxTCB;
2425         UBaseType_t uxReturn;
2426         UBaseType_t uxSavedInterruptStatus;
2427
2428         traceENTER_uxTaskPriorityGetFromISR( xTask );
2429
2430         /* RTOS ports that support interrupt nesting have the concept of a
2431          * maximum  system call (or maximum API call) interrupt priority.
2432          * Interrupts that are  above the maximum system call priority are keep
2433          * permanently enabled, even when the RTOS kernel is in a critical section,
2434          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
2435          * is defined in FreeRTOSConfig.h then
2436          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2437          * failure if a FreeRTOS API function is called from an interrupt that has
2438          * been assigned a priority above the configured maximum system call
2439          * priority.  Only FreeRTOS functions that end in FromISR can be called
2440          * from interrupts  that have been assigned a priority at or (logically)
2441          * below the maximum system call interrupt priority.  FreeRTOS maintains a
2442          * separate interrupt safe API to ensure interrupt entry is as fast and as
2443          * simple as possible.  More information (albeit Cortex-M specific) is
2444          * provided on the following link:
2445          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2446         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
2447
2448         uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
2449         {
2450             /* If null is passed in here then it is the priority of the calling
2451              * task that is being queried. */
2452             pxTCB = prvGetTCBFromHandle( xTask );
2453             uxReturn = pxTCB->uxPriority;
2454         }
2455         taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
2456
2457         traceRETURN_uxTaskPriorityGetFromISR( uxReturn );
2458
2459         return uxReturn;
2460     }
2461
2462 #endif /* INCLUDE_uxTaskPriorityGet */
2463 /*-----------------------------------------------------------*/
2464
2465 #if ( INCLUDE_vTaskPrioritySet == 1 )
2466
2467     void vTaskPrioritySet( TaskHandle_t xTask,
2468                            UBaseType_t uxNewPriority )
2469     {
2470         TCB_t * pxTCB;
2471         UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
2472         BaseType_t xYieldRequired = pdFALSE;
2473
2474         #if ( configNUMBER_OF_CORES > 1 )
2475             BaseType_t xYieldForTask = pdFALSE;
2476         #endif
2477
2478         traceENTER_vTaskPrioritySet( xTask, uxNewPriority );
2479
2480         configASSERT( uxNewPriority < configMAX_PRIORITIES );
2481
2482         /* Ensure the new priority is valid. */
2483         if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
2484         {
2485             uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
2486         }
2487         else
2488         {
2489             mtCOVERAGE_TEST_MARKER();
2490         }
2491
2492         taskENTER_CRITICAL();
2493         {
2494             /* If null is passed in here then it is the priority of the calling
2495              * task that is being changed. */
2496             pxTCB = prvGetTCBFromHandle( xTask );
2497
2498             traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
2499
2500             #if ( configUSE_MUTEXES == 1 )
2501             {
2502                 uxCurrentBasePriority = pxTCB->uxBasePriority;
2503             }
2504             #else
2505             {
2506                 uxCurrentBasePriority = pxTCB->uxPriority;
2507             }
2508             #endif
2509
2510             if( uxCurrentBasePriority != uxNewPriority )
2511             {
2512                 /* The priority change may have readied a task of higher
2513                  * priority than a running task. */
2514                 if( uxNewPriority > uxCurrentBasePriority )
2515                 {
2516                     #if ( configNUMBER_OF_CORES == 1 )
2517                     {
2518                         if( pxTCB != pxCurrentTCB )
2519                         {
2520                             /* The priority of a task other than the currently
2521                              * running task is being raised.  Is the priority being
2522                              * raised above that of the running task? */
2523                             if( uxNewPriority > pxCurrentTCB->uxPriority )
2524                             {
2525                                 xYieldRequired = pdTRUE;
2526                             }
2527                             else
2528                             {
2529                                 mtCOVERAGE_TEST_MARKER();
2530                             }
2531                         }
2532                         else
2533                         {
2534                             /* The priority of the running task is being raised,
2535                              * but the running task must already be the highest
2536                              * priority task able to run so no yield is required. */
2537                         }
2538                     }
2539                     #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2540                     {
2541                         /* The priority of a task is being raised so
2542                          * perform a yield for this task later. */
2543                         xYieldForTask = pdTRUE;
2544                     }
2545                     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2546                 }
2547                 else if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2548                 {
2549                     /* Setting the priority of a running task down means
2550                      * there may now be another task of higher priority that
2551                      * is ready to execute. */
2552                     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2553                         if( pxTCB->xPreemptionDisable == pdFALSE )
2554                     #endif
2555                     {
2556                         xYieldRequired = pdTRUE;
2557                     }
2558                 }
2559                 else
2560                 {
2561                     /* Setting the priority of any other task down does not
2562                      * require a yield as the running task must be above the
2563                      * new priority of the task being modified. */
2564                 }
2565
2566                 /* Remember the ready list the task might be referenced from
2567                  * before its uxPriority member is changed so the
2568                  * taskRESET_READY_PRIORITY() macro can function correctly. */
2569                 uxPriorityUsedOnEntry = pxTCB->uxPriority;
2570
2571                 #if ( configUSE_MUTEXES == 1 )
2572                 {
2573                     /* Only change the priority being used if the task is not
2574                      * currently using an inherited priority or the new priority
2575                      * is bigger than the inherited priority. */
2576                     if( ( pxTCB->uxBasePriority == pxTCB->uxPriority ) || ( uxNewPriority > pxTCB->uxPriority ) )
2577                     {
2578                         pxTCB->uxPriority = uxNewPriority;
2579                     }
2580                     else
2581                     {
2582                         mtCOVERAGE_TEST_MARKER();
2583                     }
2584
2585                     /* The base priority gets set whatever. */
2586                     pxTCB->uxBasePriority = uxNewPriority;
2587                 }
2588                 #else /* if ( configUSE_MUTEXES == 1 ) */
2589                 {
2590                     pxTCB->uxPriority = uxNewPriority;
2591                 }
2592                 #endif /* if ( configUSE_MUTEXES == 1 ) */
2593
2594                 /* Only reset the event list item value if the value is not
2595                  * being used for anything else. */
2596                 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
2597                 {
2598                     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. */
2599                 }
2600                 else
2601                 {
2602                     mtCOVERAGE_TEST_MARKER();
2603                 }
2604
2605                 /* If the task is in the blocked or suspended list we need do
2606                  * nothing more than change its priority variable. However, if
2607                  * the task is in a ready list it needs to be removed and placed
2608                  * in the list appropriate to its new priority. */
2609                 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
2610                 {
2611                     /* The task is currently in its ready list - remove before
2612                      * adding it to its new ready list.  As we are in a critical
2613                      * section we can do this even if the scheduler is suspended. */
2614                     if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2615                     {
2616                         /* It is known that the task is in its ready list so
2617                          * there is no need to check again and the port level
2618                          * reset macro can be called directly. */
2619                         portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
2620                     }
2621                     else
2622                     {
2623                         mtCOVERAGE_TEST_MARKER();
2624                     }
2625
2626                     prvAddTaskToReadyList( pxTCB );
2627                 }
2628                 else
2629                 {
2630                     #if ( configNUMBER_OF_CORES == 1 )
2631                     {
2632                         mtCOVERAGE_TEST_MARKER();
2633                     }
2634                     #else
2635                     {
2636                         /* It's possible that xYieldForTask was already set to pdTRUE because
2637                          * its priority is being raised. However, since it is not in a ready list
2638                          * we don't actually need to yield for it. */
2639                         xYieldForTask = pdFALSE;
2640                     }
2641                     #endif
2642                 }
2643
2644                 if( xYieldRequired != pdFALSE )
2645                 {
2646                     /* The running task priority is set down. Request the task to yield. */
2647                     taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxTCB );
2648                 }
2649                 else
2650                 {
2651                     #if ( configNUMBER_OF_CORES > 1 )
2652                         if( xYieldForTask != pdFALSE )
2653                         {
2654                             /* The priority of the task is being raised. If a running
2655                              * task has priority lower than this task, it should yield
2656                              * for this task. */
2657                             taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
2658                         }
2659                         else
2660                     #endif /* if ( configNUMBER_OF_CORES > 1 ) */
2661                     {
2662                         mtCOVERAGE_TEST_MARKER();
2663                     }
2664                 }
2665
2666                 /* Remove compiler warning about unused variables when the port
2667                  * optimised task selection is not being used. */
2668                 ( void ) uxPriorityUsedOnEntry;
2669             }
2670         }
2671         taskEXIT_CRITICAL();
2672
2673         traceRETURN_vTaskPrioritySet();
2674     }
2675
2676 #endif /* INCLUDE_vTaskPrioritySet */
2677 /*-----------------------------------------------------------*/
2678
2679 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2680     void vTaskCoreAffinitySet( const TaskHandle_t xTask,
2681                                UBaseType_t uxCoreAffinityMask )
2682     {
2683         TCB_t * pxTCB;
2684         BaseType_t xCoreID;
2685         UBaseType_t uxPrevCoreAffinityMask;
2686
2687         #if ( configUSE_PREEMPTION == 1 )
2688             UBaseType_t uxPrevNotAllowedCores;
2689         #endif
2690
2691         traceENTER_vTaskCoreAffinitySet( xTask, uxCoreAffinityMask );
2692
2693         taskENTER_CRITICAL();
2694         {
2695             pxTCB = prvGetTCBFromHandle( xTask );
2696
2697             uxPrevCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2698             pxTCB->uxCoreAffinityMask = uxCoreAffinityMask;
2699
2700             if( xSchedulerRunning != pdFALSE )
2701             {
2702                 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2703                 {
2704                     xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2705
2706                     /* If the task can no longer run on the core it was running,
2707                      * request the core to yield. */
2708                     if( ( uxCoreAffinityMask & ( ( UBaseType_t ) 1U << ( UBaseType_t ) xCoreID ) ) == 0U )
2709                     {
2710                         prvYieldCore( xCoreID );
2711                     }
2712                 }
2713                 else
2714                 {
2715                     #if ( configUSE_PREEMPTION == 1 )
2716                     {
2717                         /* Calculate the cores on which this task was not allowed to
2718                          * run previously. */
2719                         uxPrevNotAllowedCores = ( ~uxPrevCoreAffinityMask ) & ( ( 1U << configNUMBER_OF_CORES ) - 1U );
2720
2721                         /* Does the new core mask enables this task to run on any of the
2722                          * previously not allowed cores? If yes, check if this task can be
2723                          * scheduled on any of those cores. */
2724                         if( ( uxPrevNotAllowedCores & uxCoreAffinityMask ) != 0U )
2725                         {
2726                             prvYieldForTask( pxTCB );
2727                         }
2728                     }
2729                     #else /* #if( configUSE_PREEMPTION == 1 ) */
2730                     {
2731                         mtCOVERAGE_TEST_MARKER();
2732                     }
2733                     #endif /* #if( configUSE_PREEMPTION == 1 ) */
2734                 }
2735             }
2736         }
2737         taskEXIT_CRITICAL();
2738
2739         traceRETURN_vTaskCoreAffinitySet();
2740     }
2741 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
2742 /*-----------------------------------------------------------*/
2743
2744 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) )
2745     UBaseType_t vTaskCoreAffinityGet( ConstTaskHandle_t xTask )
2746     {
2747         const TCB_t * pxTCB;
2748         UBaseType_t uxCoreAffinityMask;
2749
2750         traceENTER_vTaskCoreAffinityGet( xTask );
2751
2752         taskENTER_CRITICAL();
2753         {
2754             pxTCB = prvGetTCBFromHandle( xTask );
2755             uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
2756         }
2757         taskEXIT_CRITICAL();
2758
2759         traceRETURN_vTaskCoreAffinityGet( uxCoreAffinityMask );
2760
2761         return uxCoreAffinityMask;
2762     }
2763 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_CORE_AFFINITY == 1 ) ) */
2764
2765 /*-----------------------------------------------------------*/
2766
2767 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2768
2769     void vTaskPreemptionDisable( const TaskHandle_t xTask )
2770     {
2771         TCB_t * pxTCB;
2772
2773         traceENTER_vTaskPreemptionDisable( xTask );
2774
2775         taskENTER_CRITICAL();
2776         {
2777             pxTCB = prvGetTCBFromHandle( xTask );
2778
2779             pxTCB->xPreemptionDisable = pdTRUE;
2780         }
2781         taskEXIT_CRITICAL();
2782
2783         traceRETURN_vTaskPreemptionDisable();
2784     }
2785
2786 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
2787 /*-----------------------------------------------------------*/
2788
2789 #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
2790
2791     void vTaskPreemptionEnable( const TaskHandle_t xTask )
2792     {
2793         TCB_t * pxTCB;
2794         BaseType_t xCoreID;
2795
2796         traceENTER_vTaskPreemptionEnable( xTask );
2797
2798         taskENTER_CRITICAL();
2799         {
2800             pxTCB = prvGetTCBFromHandle( xTask );
2801
2802             pxTCB->xPreemptionDisable = pdFALSE;
2803
2804             if( xSchedulerRunning != pdFALSE )
2805             {
2806                 if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2807                 {
2808                     xCoreID = ( BaseType_t ) pxTCB->xTaskRunState;
2809                     prvYieldCore( xCoreID );
2810                 }
2811             }
2812         }
2813         taskEXIT_CRITICAL();
2814
2815         traceRETURN_vTaskPreemptionEnable();
2816     }
2817
2818 #endif /* #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 ) */
2819 /*-----------------------------------------------------------*/
2820
2821 #if ( INCLUDE_vTaskSuspend == 1 )
2822
2823     void vTaskSuspend( TaskHandle_t xTaskToSuspend )
2824     {
2825         TCB_t * pxTCB;
2826
2827         #if ( configNUMBER_OF_CORES > 1 )
2828             BaseType_t xTaskRunningOnCore;
2829         #endif
2830
2831         traceENTER_vTaskSuspend( xTaskToSuspend );
2832
2833         taskENTER_CRITICAL();
2834         {
2835             /* If null is passed in here then it is the running task that is
2836              * being suspended. */
2837             pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
2838
2839             traceTASK_SUSPEND( pxTCB );
2840
2841             #if ( configNUMBER_OF_CORES > 1 )
2842                 xTaskRunningOnCore = pxTCB->xTaskRunState;
2843             #endif
2844
2845             /* Remove task from the ready/delayed list and place in the
2846              * suspended list. */
2847             if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
2848             {
2849                 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
2850             }
2851             else
2852             {
2853                 mtCOVERAGE_TEST_MARKER();
2854             }
2855
2856             /* Is the task waiting on an event also? */
2857             if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2858             {
2859                 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2860             }
2861             else
2862             {
2863                 mtCOVERAGE_TEST_MARKER();
2864             }
2865
2866             vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
2867
2868             #if ( configUSE_TASK_NOTIFICATIONS == 1 )
2869             {
2870                 BaseType_t x;
2871
2872                 for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
2873                 {
2874                     if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
2875                     {
2876                         /* The task was blocked to wait for a notification, but is
2877                          * now suspended, so no notification was received. */
2878                         pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
2879                     }
2880                 }
2881             }
2882             #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
2883         }
2884
2885         #if ( configNUMBER_OF_CORES == 1 )
2886         {
2887             taskEXIT_CRITICAL();
2888
2889             if( xSchedulerRunning != pdFALSE )
2890             {
2891                 /* Reset the next expected unblock time in case it referred to the
2892                  * task that is now in the Suspended state. */
2893                 taskENTER_CRITICAL();
2894                 {
2895                     prvResetNextTaskUnblockTime();
2896                 }
2897                 taskEXIT_CRITICAL();
2898             }
2899             else
2900             {
2901                 mtCOVERAGE_TEST_MARKER();
2902             }
2903
2904             if( pxTCB == pxCurrentTCB )
2905             {
2906                 if( xSchedulerRunning != pdFALSE )
2907                 {
2908                     /* The current task has just been suspended. */
2909                     configASSERT( uxSchedulerSuspended == 0 );
2910                     portYIELD_WITHIN_API();
2911                 }
2912                 else
2913                 {
2914                     /* The scheduler is not running, but the task that was pointed
2915                      * to by pxCurrentTCB has just been suspended and pxCurrentTCB
2916                      * must be adjusted to point to a different task. */
2917                     if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
2918                     {
2919                         /* No other tasks are ready, so set pxCurrentTCB back to
2920                          * NULL so when the next task is created pxCurrentTCB will
2921                          * be set to point to it no matter what its relative priority
2922                          * is. */
2923                         pxCurrentTCB = NULL;
2924                     }
2925                     else
2926                     {
2927                         vTaskSwitchContext();
2928                     }
2929                 }
2930             }
2931             else
2932             {
2933                 mtCOVERAGE_TEST_MARKER();
2934             }
2935         }
2936         #else /* #if ( configNUMBER_OF_CORES == 1 ) */
2937         {
2938             if( xSchedulerRunning != pdFALSE )
2939             {
2940                 /* Reset the next expected unblock time in case it referred to the
2941                  * task that is now in the Suspended state. */
2942                 prvResetNextTaskUnblockTime();
2943             }
2944             else
2945             {
2946                 mtCOVERAGE_TEST_MARKER();
2947             }
2948
2949             if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
2950             {
2951                 if( xSchedulerRunning != pdFALSE )
2952                 {
2953                     if( xTaskRunningOnCore == ( BaseType_t ) portGET_CORE_ID() )
2954                     {
2955                         /* The current task has just been suspended. */
2956                         configASSERT( uxSchedulerSuspended == 0 );
2957                         vTaskYieldWithinAPI();
2958                     }
2959                     else
2960                     {
2961                         prvYieldCore( xTaskRunningOnCore );
2962                     }
2963                 }
2964                 else
2965                 {
2966                     /* This code path is not possible because only Idle tasks are
2967                      * assigned a core before the scheduler is started ( i.e.
2968                      * taskTASK_IS_RUNNING is only true for idle tasks before
2969                      * the scheduler is started ) and idle tasks cannot be
2970                      * suspended. */
2971                     mtCOVERAGE_TEST_MARKER();
2972                 }
2973             }
2974             else
2975             {
2976                 mtCOVERAGE_TEST_MARKER();
2977             }
2978
2979             taskEXIT_CRITICAL();
2980         }
2981         #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
2982
2983         traceRETURN_vTaskSuspend();
2984     }
2985
2986 #endif /* INCLUDE_vTaskSuspend */
2987 /*-----------------------------------------------------------*/
2988
2989 #if ( INCLUDE_vTaskSuspend == 1 )
2990
2991     static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
2992     {
2993         BaseType_t xReturn = pdFALSE;
2994         const TCB_t * const pxTCB = xTask;
2995
2996         /* Accesses xPendingReadyList so must be called from a critical
2997          * section. */
2998
2999         /* It does not make sense to check if the calling task is suspended. */
3000         configASSERT( xTask );
3001
3002         /* Is the task being resumed actually in the suspended list? */
3003         if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
3004         {
3005             /* Has the task already been resumed from within an ISR? */
3006             if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
3007             {
3008                 /* Is it in the suspended list because it is in the Suspended
3009                  * state, or because is is blocked with no timeout? */
3010                 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961.  The cast is only redundant when NULL is used. */
3011                 {
3012                     xReturn = pdTRUE;
3013                 }
3014                 else
3015                 {
3016                     mtCOVERAGE_TEST_MARKER();
3017                 }
3018             }
3019             else
3020             {
3021                 mtCOVERAGE_TEST_MARKER();
3022             }
3023         }
3024         else
3025         {
3026             mtCOVERAGE_TEST_MARKER();
3027         }
3028
3029         return xReturn;
3030     } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
3031
3032 #endif /* INCLUDE_vTaskSuspend */
3033 /*-----------------------------------------------------------*/
3034
3035 #if ( INCLUDE_vTaskSuspend == 1 )
3036
3037     void vTaskResume( TaskHandle_t xTaskToResume )
3038     {
3039         TCB_t * const pxTCB = xTaskToResume;
3040
3041         traceENTER_vTaskResume( xTaskToResume );
3042
3043         /* It does not make sense to resume the calling task. */
3044         configASSERT( xTaskToResume );
3045
3046         #if ( configNUMBER_OF_CORES == 1 )
3047
3048             /* The parameter cannot be NULL as it is impossible to resume the
3049              * currently executing task. */
3050             if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
3051         #else
3052
3053             /* The parameter cannot be NULL as it is impossible to resume the
3054              * currently executing task. It is also impossible to resume a task
3055              * that is actively running on another core but it is not safe
3056              * to check their run state here. Therefore, we get into a critical
3057              * section and check if the task is actually suspended or not. */
3058             if( pxTCB != NULL )
3059         #endif
3060         {
3061             taskENTER_CRITICAL();
3062             {
3063                 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3064                 {
3065                     traceTASK_RESUME( pxTCB );
3066
3067                     /* The ready list can be accessed even if the scheduler is
3068                      * suspended because this is inside a critical section. */
3069                     ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3070                     prvAddTaskToReadyList( pxTCB );
3071
3072                     /* This yield may not cause the task just resumed to run,
3073                      * but will leave the lists in the correct state for the
3074                      * next yield. */
3075                     taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
3076                 }
3077                 else
3078                 {
3079                     mtCOVERAGE_TEST_MARKER();
3080                 }
3081             }
3082             taskEXIT_CRITICAL();
3083         }
3084         else
3085         {
3086             mtCOVERAGE_TEST_MARKER();
3087         }
3088
3089         traceRETURN_vTaskResume();
3090     }
3091
3092 #endif /* INCLUDE_vTaskSuspend */
3093
3094 /*-----------------------------------------------------------*/
3095
3096 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
3097
3098     BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
3099     {
3100         BaseType_t xYieldRequired = pdFALSE;
3101         TCB_t * const pxTCB = xTaskToResume;
3102         UBaseType_t uxSavedInterruptStatus;
3103
3104         traceENTER_xTaskResumeFromISR( xTaskToResume );
3105
3106         configASSERT( xTaskToResume );
3107
3108         /* RTOS ports that support interrupt nesting have the concept of a
3109          * maximum  system call (or maximum API call) interrupt priority.
3110          * Interrupts that are  above the maximum system call priority are keep
3111          * permanently enabled, even when the RTOS kernel is in a critical section,
3112          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
3113          * is defined in FreeRTOSConfig.h then
3114          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3115          * failure if a FreeRTOS API function is called from an interrupt that has
3116          * been assigned a priority above the configured maximum system call
3117          * priority.  Only FreeRTOS functions that end in FromISR can be called
3118          * from interrupts  that have been assigned a priority at or (logically)
3119          * below the maximum system call interrupt priority.  FreeRTOS maintains a
3120          * separate interrupt safe API to ensure interrupt entry is as fast and as
3121          * simple as possible.  More information (albeit Cortex-M specific) is
3122          * provided on the following link:
3123          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3124         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3125
3126         uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
3127         {
3128             if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
3129             {
3130                 traceTASK_RESUME_FROM_ISR( pxTCB );
3131
3132                 /* Check the ready lists can be accessed. */
3133                 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3134                 {
3135                     #if ( configNUMBER_OF_CORES == 1 )
3136                     {
3137                         /* Ready lists can be accessed so move the task from the
3138                          * suspended list to the ready list directly. */
3139                         if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3140                         {
3141                             xYieldRequired = pdTRUE;
3142
3143                             /* Mark that a yield is pending in case the user is not
3144                              * using the return value to initiate a context switch
3145                              * from the ISR using portYIELD_FROM_ISR. */
3146                             xYieldPendings[ 0 ] = pdTRUE;
3147                         }
3148                         else
3149                         {
3150                             mtCOVERAGE_TEST_MARKER();
3151                         }
3152                     }
3153                     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3154
3155                     ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3156                     prvAddTaskToReadyList( pxTCB );
3157                 }
3158                 else
3159                 {
3160                     /* The delayed or ready lists cannot be accessed so the task
3161                      * is held in the pending ready list until the scheduler is
3162                      * unsuspended. */
3163                     vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
3164                 }
3165
3166                 #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) )
3167                 {
3168                     prvYieldForTask( pxTCB );
3169
3170                     if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
3171                     {
3172                         xYieldRequired = pdTRUE;
3173                     }
3174                 }
3175                 #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_PREEMPTION == 1 ) ) */
3176             }
3177             else
3178             {
3179                 mtCOVERAGE_TEST_MARKER();
3180             }
3181         }
3182         taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
3183
3184         traceRETURN_xTaskResumeFromISR( xYieldRequired );
3185
3186         return xYieldRequired;
3187     }
3188
3189 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
3190 /*-----------------------------------------------------------*/
3191
3192 static BaseType_t prvCreateIdleTasks( void )
3193 {
3194     BaseType_t xReturn = pdPASS;
3195
3196     #if ( configNUMBER_OF_CORES == 1 )
3197     {
3198         /* Add the idle task at the lowest priority. */
3199         #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3200         {
3201             StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3202             StackType_t * pxIdleTaskStackBuffer = NULL;
3203             uint32_t ulIdleTaskStackSize;
3204
3205             /* The Idle task is created using user provided RAM - obtain the
3206              * address of the RAM then create the idle task. */
3207             vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3208             xIdleTaskHandles[ 0 ] = xTaskCreateStatic( prvIdleTask,
3209                                                        configIDLE_TASK_NAME,
3210                                                        ulIdleTaskStackSize,
3211                                                        ( void * ) NULL,       /*lint !e961.  The cast is not redundant for all compilers. */
3212                                                        portPRIVILEGE_BIT,     /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3213                                                        pxIdleTaskStackBuffer,
3214                                                        pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3215
3216             if( xIdleTaskHandles[ 0 ] != NULL )
3217             {
3218                 xReturn = pdPASS;
3219             }
3220             else
3221             {
3222                 xReturn = pdFAIL;
3223             }
3224         }
3225         #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3226         {
3227             /* The Idle task is being created using dynamically allocated RAM. */
3228             xReturn = xTaskCreate( prvIdleTask,
3229                                    configIDLE_TASK_NAME,
3230                                    configMINIMAL_STACK_SIZE,
3231                                    ( void * ) NULL,
3232                                    portPRIVILEGE_BIT,        /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3233                                    &xIdleTaskHandles[ 0 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3234         }
3235         #endif /* configSUPPORT_STATIC_ALLOCATION */
3236     }
3237     #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3238     {
3239         BaseType_t xCoreID;
3240         char cIdleName[ configMAX_TASK_NAME_LEN ];
3241
3242         /* Add each idle task at the lowest priority. */
3243         for( xCoreID = ( BaseType_t ) 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
3244         {
3245             BaseType_t x;
3246
3247             if( xReturn == pdFAIL )
3248             {
3249                 break;
3250             }
3251             else
3252             {
3253                 mtCOVERAGE_TEST_MARKER();
3254             }
3255
3256             for( x = ( BaseType_t ) 0; x < ( BaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3257             {
3258                 cIdleName[ x ] = configIDLE_TASK_NAME[ x ];
3259
3260                 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
3261                  * configMAX_TASK_NAME_LEN characters just in case the memory after the
3262                  * string is not accessible (extremely unlikely). */
3263                 if( cIdleName[ x ] == ( char ) 0x00 )
3264                 {
3265                     break;
3266                 }
3267                 else
3268                 {
3269                     mtCOVERAGE_TEST_MARKER();
3270                 }
3271             }
3272
3273             /* Append the idle task number to the end of the name if there is space. */
3274             if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3275             {
3276                 cIdleName[ x ] = ( char ) ( xCoreID + '0' );
3277                 x++;
3278
3279                 /* And append a null character if there is space. */
3280                 if( x < ( BaseType_t ) configMAX_TASK_NAME_LEN )
3281                 {
3282                     cIdleName[ x ] = '\0';
3283                 }
3284                 else
3285                 {
3286                     mtCOVERAGE_TEST_MARKER();
3287                 }
3288             }
3289             else
3290             {
3291                 mtCOVERAGE_TEST_MARKER();
3292             }
3293
3294             #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
3295             {
3296                 if( xCoreID == 0 )
3297                 {
3298                     StaticTask_t * pxIdleTaskTCBBuffer = NULL;
3299                     StackType_t * pxIdleTaskStackBuffer = NULL;
3300                     uint32_t ulIdleTaskStackSize;
3301
3302                     /* The Idle task is created using user provided RAM - obtain the
3303                      * address of the RAM then create the idle task. */
3304                     vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
3305                     xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( prvIdleTask,
3306                                                                      cIdleName,
3307                                                                      ulIdleTaskStackSize,
3308                                                                      ( void * ) NULL,       /*lint !e961.  The cast is not redundant for all compilers. */
3309                                                                      portPRIVILEGE_BIT,     /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3310                                                                      pxIdleTaskStackBuffer,
3311                                                                      pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3312                 }
3313                 else
3314                 {
3315                     xIdleTaskHandles[ xCoreID ] = xTaskCreateStatic( prvMinimalIdleTask,
3316                                                                      cIdleName,
3317                                                                      configMINIMAL_STACK_SIZE,
3318                                                                      ( void * ) NULL,                   /*lint !e961.  The cast is not redundant for all compilers. */
3319                                                                      portPRIVILEGE_BIT,                 /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3320                                                                      xIdleTaskStackBuffers[ xCoreID - 1 ],
3321                                                                      &xIdleTCBBuffers[ xCoreID - 1 ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3322                 }
3323
3324                 if( xIdleTaskHandles[ xCoreID ] != NULL )
3325                 {
3326                     xReturn = pdPASS;
3327                 }
3328                 else
3329                 {
3330                     xReturn = pdFAIL;
3331                 }
3332             }
3333             #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
3334             {
3335                 if( xCoreID == 0 )
3336                 {
3337                     /* The Idle task is being created using dynamically allocated RAM. */
3338                     xReturn = xTaskCreate( prvIdleTask,
3339                                            cIdleName,
3340                                            configMINIMAL_STACK_SIZE,
3341                                            ( void * ) NULL,
3342                                            portPRIVILEGE_BIT,              /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3343                                            &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3344                 }
3345                 else
3346                 {
3347                     xReturn = xTaskCreate( prvMinimalIdleTask,
3348                                            cIdleName,
3349                                            configMINIMAL_STACK_SIZE,
3350                                            ( void * ) NULL,
3351                                            portPRIVILEGE_BIT,              /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
3352                                            &xIdleTaskHandles[ xCoreID ] ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
3353                 }
3354             }
3355             #endif /* configSUPPORT_STATIC_ALLOCATION */
3356         }
3357     }
3358     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3359
3360     return xReturn;
3361 }
3362
3363 /*-----------------------------------------------------------*/
3364
3365 void vTaskStartScheduler( void )
3366 {
3367     BaseType_t xReturn;
3368
3369     traceENTER_vTaskStartScheduler();
3370
3371     #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 )
3372     {
3373         /* Sanity check that the UBaseType_t must have greater than or equal to
3374          * the number of bits as confNUMBER_OF_CORES. */
3375         configASSERT( ( sizeof( UBaseType_t ) * taskBITS_PER_BYTE ) >= configNUMBER_OF_CORES );
3376     }
3377     #endif /* #if ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
3378
3379     xReturn = prvCreateIdleTasks();
3380
3381     #if ( configUSE_TIMERS == 1 )
3382     {
3383         if( xReturn == pdPASS )
3384         {
3385             xReturn = xTimerCreateTimerTask();
3386         }
3387         else
3388         {
3389             mtCOVERAGE_TEST_MARKER();
3390         }
3391     }
3392     #endif /* configUSE_TIMERS */
3393
3394     if( xReturn == pdPASS )
3395     {
3396         /* freertos_tasks_c_additions_init() should only be called if the user
3397          * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
3398          * the only macro called by the function. */
3399         #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
3400         {
3401             freertos_tasks_c_additions_init();
3402         }
3403         #endif
3404
3405         /* Interrupts are turned off here, to ensure a tick does not occur
3406          * before or during the call to xPortStartScheduler().  The stacks of
3407          * the created tasks contain a status word with interrupts switched on
3408          * so interrupts will automatically get re-enabled when the first task
3409          * starts to run. */
3410         portDISABLE_INTERRUPTS();
3411
3412         #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
3413         {
3414             /* Switch C-Runtime's TLS Block to point to the TLS
3415              * block specific to the task that will run first. */
3416             configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3417         }
3418         #endif
3419
3420         xNextTaskUnblockTime = portMAX_DELAY;
3421         xSchedulerRunning = pdTRUE;
3422         xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
3423
3424         /* If configGENERATE_RUN_TIME_STATS is defined then the following
3425          * macro must be defined to configure the timer/counter used to generate
3426          * the run time counter time base.   NOTE:  If configGENERATE_RUN_TIME_STATS
3427          * is set to 0 and the following line fails to build then ensure you do not
3428          * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
3429          * FreeRTOSConfig.h file. */
3430         portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
3431
3432         traceTASK_SWITCHED_IN();
3433
3434         /* Setting up the timer tick is hardware specific and thus in the
3435          * portable interface. */
3436         xPortStartScheduler();
3437
3438         /* In most cases, xPortStartScheduler() will not return. If it
3439          * returns pdTRUE then there was not enough heap memory available
3440          * to create either the Idle or the Timer task. If it returned
3441          * pdFALSE, then the application called xTaskEndScheduler().
3442          * Most ports don't implement xTaskEndScheduler() as there is
3443          * nothing to return to. */
3444     }
3445     else
3446     {
3447         /* This line will only be reached if the kernel could not be started,
3448          * because there was not enough FreeRTOS heap to create the idle task
3449          * or the timer task. */
3450         configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
3451     }
3452
3453     /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
3454      * meaning xIdleTaskHandles are not used anywhere else. */
3455     ( void ) xIdleTaskHandles;
3456
3457     /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
3458      * from getting optimized out as it is no longer used by the kernel. */
3459     ( void ) uxTopUsedPriority;
3460
3461     traceRETURN_vTaskStartScheduler();
3462 }
3463 /*-----------------------------------------------------------*/
3464
3465 void vTaskEndScheduler( void )
3466 {
3467     traceENTER_vTaskEndScheduler();
3468
3469     /* Stop the scheduler interrupts and call the portable scheduler end
3470      * routine so the original ISRs can be restored if necessary.  The port
3471      * layer must ensure interrupts enable  bit is left in the correct state. */
3472     portDISABLE_INTERRUPTS();
3473     xSchedulerRunning = pdFALSE;
3474     vPortEndScheduler();
3475
3476     traceRETURN_vTaskEndScheduler();
3477 }
3478 /*----------------------------------------------------------*/
3479
3480 void vTaskSuspendAll( void )
3481 {
3482     traceENTER_vTaskSuspendAll();
3483
3484     #if ( configNUMBER_OF_CORES == 1 )
3485     {
3486         /* A critical section is not required as the variable is of type
3487          * BaseType_t.  Please read Richard Barry's reply in the following link to a
3488          * post in the FreeRTOS support forum before reporting this as a bug! -
3489          * https://goo.gl/wu4acr */
3490
3491         /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
3492          * do not otherwise exhibit real time behaviour. */
3493         portSOFTWARE_BARRIER();
3494
3495         /* The scheduler is suspended if uxSchedulerSuspended is non-zero.  An increment
3496          * is used to allow calls to vTaskSuspendAll() to nest. */
3497         ++uxSchedulerSuspended;
3498
3499         /* Enforces ordering for ports and optimised compilers that may otherwise place
3500          * the above increment elsewhere. */
3501         portMEMORY_BARRIER();
3502     }
3503     #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3504     {
3505         UBaseType_t ulState;
3506
3507         /* This must only be called from within a task. */
3508         portASSERT_IF_IN_ISR();
3509
3510         if( xSchedulerRunning != pdFALSE )
3511         {
3512             /* Writes to uxSchedulerSuspended must be protected by both the task AND ISR locks.
3513              * We must disable interrupts before we grab the locks in the event that this task is
3514              * interrupted and switches context before incrementing uxSchedulerSuspended.
3515              * It is safe to re-enable interrupts after releasing the ISR lock and incrementing
3516              * uxSchedulerSuspended since that will prevent context switches. */
3517             ulState = portSET_INTERRUPT_MASK();
3518
3519             /* portSOFRWARE_BARRIER() is only implemented for emulated/simulated ports that
3520              * do not otherwise exhibit real time behaviour. */
3521             portSOFTWARE_BARRIER();
3522
3523             portGET_TASK_LOCK();
3524
3525             /* uxSchedulerSuspended is increased after prvCheckForRunStateChange. The
3526              * purpose is to prevent altering the variable when fromISR APIs are readying
3527              * it. */
3528             if( uxSchedulerSuspended == 0U )
3529             {
3530                 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
3531                 {
3532                     prvCheckForRunStateChange();
3533                 }
3534                 else
3535                 {
3536                     mtCOVERAGE_TEST_MARKER();
3537                 }
3538             }
3539             else
3540             {
3541                 mtCOVERAGE_TEST_MARKER();
3542             }
3543
3544             portGET_ISR_LOCK();
3545
3546             /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
3547              * is used to allow calls to vTaskSuspendAll() to nest. */
3548             ++uxSchedulerSuspended;
3549             portRELEASE_ISR_LOCK();
3550
3551             portCLEAR_INTERRUPT_MASK( ulState );
3552         }
3553         else
3554         {
3555             mtCOVERAGE_TEST_MARKER();
3556         }
3557     }
3558     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3559
3560     traceRETURN_vTaskSuspendAll();
3561 }
3562
3563 /*----------------------------------------------------------*/
3564
3565 #if ( configUSE_TICKLESS_IDLE != 0 )
3566
3567     static TickType_t prvGetExpectedIdleTime( void )
3568     {
3569         TickType_t xReturn;
3570         UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
3571
3572         /* uxHigherPriorityReadyTasks takes care of the case where
3573          * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
3574          * task that are in the Ready state, even though the idle task is
3575          * running. */
3576         #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
3577         {
3578             if( uxTopReadyPriority > tskIDLE_PRIORITY )
3579             {
3580                 uxHigherPriorityReadyTasks = pdTRUE;
3581             }
3582         }
3583         #else
3584         {
3585             const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
3586
3587             /* When port optimised task selection is used the uxTopReadyPriority
3588              * variable is used as a bit map.  If bits other than the least
3589              * significant bit are set then there are tasks that have a priority
3590              * above the idle priority that are in the Ready state.  This takes
3591              * care of the case where the co-operative scheduler is in use. */
3592             if( uxTopReadyPriority > uxLeastSignificantBit )
3593             {
3594                 uxHigherPriorityReadyTasks = pdTRUE;
3595             }
3596         }
3597         #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
3598
3599         if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
3600         {
3601             xReturn = 0;
3602         }
3603         else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
3604         {
3605             /* There are other idle priority tasks in the ready state.  If
3606              * time slicing is used then the very next tick interrupt must be
3607              * processed. */
3608             xReturn = 0;
3609         }
3610         else if( uxHigherPriorityReadyTasks != pdFALSE )
3611         {
3612             /* There are tasks in the Ready state that have a priority above the
3613              * idle priority.  This path can only be reached if
3614              * configUSE_PREEMPTION is 0. */
3615             xReturn = 0;
3616         }
3617         else
3618         {
3619             xReturn = xNextTaskUnblockTime - xTickCount;
3620         }
3621
3622         return xReturn;
3623     }
3624
3625 #endif /* configUSE_TICKLESS_IDLE */
3626 /*----------------------------------------------------------*/
3627
3628 BaseType_t xTaskResumeAll( void )
3629 {
3630     TCB_t * pxTCB = NULL;
3631     BaseType_t xAlreadyYielded = pdFALSE;
3632
3633     traceENTER_xTaskResumeAll();
3634
3635     #if ( configNUMBER_OF_CORES > 1 )
3636         if( xSchedulerRunning != pdFALSE )
3637     #endif
3638     {
3639         /* It is possible that an ISR caused a task to be removed from an event
3640          * list while the scheduler was suspended.  If this was the case then the
3641          * removed task will have been added to the xPendingReadyList.  Once the
3642          * scheduler has been resumed it is safe to move all the pending ready
3643          * tasks from this list into their appropriate ready list. */
3644         taskENTER_CRITICAL();
3645         {
3646             BaseType_t xCoreID;
3647             xCoreID = ( BaseType_t ) portGET_CORE_ID();
3648
3649             /* If uxSchedulerSuspended is zero then this function does not match a
3650              * previous call to vTaskSuspendAll(). */
3651             configASSERT( uxSchedulerSuspended != 0U );
3652
3653             --uxSchedulerSuspended;
3654             portRELEASE_TASK_LOCK();
3655
3656             if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
3657             {
3658                 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
3659                 {
3660                     /* Move any readied tasks from the pending list into the
3661                      * appropriate ready list. */
3662                     while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
3663                     {
3664                         pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) ); /*lint !e9079 void * is used as this macro is used with timers too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3665                         listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
3666                         portMEMORY_BARRIER();
3667                         listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
3668                         prvAddTaskToReadyList( pxTCB );
3669
3670                         #if ( configNUMBER_OF_CORES == 1 )
3671                         {
3672                             /* If the moved task has a priority higher than the current
3673                              * task then a yield must be performed. */
3674                             if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
3675                             {
3676                                 xYieldPendings[ xCoreID ] = pdTRUE;
3677                             }
3678                             else
3679                             {
3680                                 mtCOVERAGE_TEST_MARKER();
3681                             }
3682                         }
3683                         #else /* #if ( configNUMBER_OF_CORES == 1 ) */
3684                         {
3685                             /* All appropriate tasks yield at the moment a task is added to xPendingReadyList.
3686                              * If the current core yielded then vTaskSwitchContext() has already been called
3687                              * which sets xYieldPendings for the current core to pdTRUE. */
3688                         }
3689                         #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3690                     }
3691
3692                     if( pxTCB != NULL )
3693                     {
3694                         /* A task was unblocked while the scheduler was suspended,
3695                          * which may have prevented the next unblock time from being
3696                          * re-calculated, in which case re-calculate it now.  Mainly
3697                          * important for low power tickless implementations, where
3698                          * this can prevent an unnecessary exit from low power
3699                          * state. */
3700                         prvResetNextTaskUnblockTime();
3701                     }
3702
3703                     /* If any ticks occurred while the scheduler was suspended then
3704                      * they should be processed now.  This ensures the tick count does
3705                      * not  slip, and that any delayed tasks are resumed at the correct
3706                      * time.
3707                      *
3708                      * It should be safe to call xTaskIncrementTick here from any core
3709                      * since we are in a critical section and xTaskIncrementTick itself
3710                      * protects itself within a critical section. Suspending the scheduler
3711                      * from any core causes xTaskIncrementTick to increment uxPendedCounts. */
3712                     {
3713                         TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
3714
3715                         if( xPendedCounts > ( TickType_t ) 0U )
3716                         {
3717                             do
3718                             {
3719                                 if( xTaskIncrementTick() != pdFALSE )
3720                                 {
3721                                     /* Other cores are interrupted from
3722                                      * within xTaskIncrementTick(). */
3723                                     xYieldPendings[ xCoreID ] = pdTRUE;
3724                                 }
3725                                 else
3726                                 {
3727                                     mtCOVERAGE_TEST_MARKER();
3728                                 }
3729
3730                                 --xPendedCounts;
3731                             } while( xPendedCounts > ( TickType_t ) 0U );
3732
3733                             xPendedTicks = 0;
3734                         }
3735                         else
3736                         {
3737                             mtCOVERAGE_TEST_MARKER();
3738                         }
3739                     }
3740
3741                     if( xYieldPendings[ xCoreID ] != pdFALSE )
3742                     {
3743                         #if ( configUSE_PREEMPTION != 0 )
3744                         {
3745                             xAlreadyYielded = pdTRUE;
3746                         }
3747                         #endif /* #if ( configUSE_PREEMPTION != 0 ) */
3748
3749                         #if ( configNUMBER_OF_CORES == 1 )
3750                         {
3751                             taskYIELD_TASK_CORE_IF_USING_PREEMPTION( pxCurrentTCB );
3752                         }
3753                         #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3754                     }
3755                     else
3756                     {
3757                         mtCOVERAGE_TEST_MARKER();
3758                     }
3759                 }
3760             }
3761             else
3762             {
3763                 mtCOVERAGE_TEST_MARKER();
3764             }
3765         }
3766         taskEXIT_CRITICAL();
3767     }
3768
3769     traceRETURN_xTaskResumeAll( xAlreadyYielded );
3770
3771     return xAlreadyYielded;
3772 }
3773 /*-----------------------------------------------------------*/
3774
3775 TickType_t xTaskGetTickCount( void )
3776 {
3777     TickType_t xTicks;
3778
3779     traceENTER_xTaskGetTickCount();
3780
3781     /* Critical section required if running on a 16 bit processor. */
3782     portTICK_TYPE_ENTER_CRITICAL();
3783     {
3784         xTicks = xTickCount;
3785     }
3786     portTICK_TYPE_EXIT_CRITICAL();
3787
3788     traceRETURN_xTaskGetTickCount( xTicks );
3789
3790     return xTicks;
3791 }
3792 /*-----------------------------------------------------------*/
3793
3794 TickType_t xTaskGetTickCountFromISR( void )
3795 {
3796     TickType_t xReturn;
3797     UBaseType_t uxSavedInterruptStatus;
3798
3799     traceENTER_xTaskGetTickCountFromISR();
3800
3801     /* RTOS ports that support interrupt nesting have the concept of a maximum
3802      * system call (or maximum API call) interrupt priority.  Interrupts that are
3803      * above the maximum system call priority are kept permanently enabled, even
3804      * when the RTOS kernel is in a critical section, but cannot make any calls to
3805      * FreeRTOS API functions.  If configASSERT() is defined in FreeRTOSConfig.h
3806      * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
3807      * failure if a FreeRTOS API function is called from an interrupt that has been
3808      * assigned a priority above the configured maximum system call priority.
3809      * Only FreeRTOS functions that end in FromISR can be called from interrupts
3810      * that have been assigned a priority at or (logically) below the maximum
3811      * system call  interrupt priority.  FreeRTOS maintains a separate interrupt
3812      * safe API to ensure interrupt entry is as fast and as simple as possible.
3813      * More information (albeit Cortex-M specific) is provided on the following
3814      * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
3815     portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
3816
3817     uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
3818     {
3819         xReturn = xTickCount;
3820     }
3821     portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
3822
3823     traceRETURN_xTaskGetTickCountFromISR( xReturn );
3824
3825     return xReturn;
3826 }
3827 /*-----------------------------------------------------------*/
3828
3829 UBaseType_t uxTaskGetNumberOfTasks( void )
3830 {
3831     traceENTER_uxTaskGetNumberOfTasks();
3832
3833     /* A critical section is not required because the variables are of type
3834      * BaseType_t. */
3835     traceRETURN_uxTaskGetNumberOfTasks( uxCurrentNumberOfTasks );
3836
3837     return uxCurrentNumberOfTasks;
3838 }
3839 /*-----------------------------------------------------------*/
3840
3841 char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3842 {
3843     TCB_t * pxTCB;
3844
3845     traceENTER_pcTaskGetName( xTaskToQuery );
3846
3847     /* If null is passed in here then the name of the calling task is being
3848      * queried. */
3849     pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3850     configASSERT( pxTCB );
3851
3852     traceRETURN_pcTaskGetName( &( pxTCB->pcTaskName[ 0 ] ) );
3853
3854     return &( pxTCB->pcTaskName[ 0 ] );
3855 }
3856 /*-----------------------------------------------------------*/
3857
3858 #if ( INCLUDE_xTaskGetHandle == 1 )
3859
3860     #if ( configNUMBER_OF_CORES == 1 )
3861         static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3862                                                          const char pcNameToQuery[] )
3863         {
3864             TCB_t * pxNextTCB;
3865             TCB_t * pxFirstTCB;
3866             TCB_t * pxReturn = NULL;
3867             UBaseType_t x;
3868             char cNextChar;
3869             BaseType_t xBreakLoop;
3870
3871             /* This function is called with the scheduler suspended. */
3872
3873             if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3874             {
3875                 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3876
3877                 do
3878                 {
3879                     listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3880
3881                     /* Check each character in the name looking for a match or
3882                      * mismatch. */
3883                     xBreakLoop = pdFALSE;
3884
3885                     for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3886                     {
3887                         cNextChar = pxNextTCB->pcTaskName[ x ];
3888
3889                         if( cNextChar != pcNameToQuery[ x ] )
3890                         {
3891                             /* Characters didn't match. */
3892                             xBreakLoop = pdTRUE;
3893                         }
3894                         else if( cNextChar == ( char ) 0x00 )
3895                         {
3896                             /* Both strings terminated, a match must have been
3897                              * found. */
3898                             pxReturn = pxNextTCB;
3899                             xBreakLoop = pdTRUE;
3900                         }
3901                         else
3902                         {
3903                             mtCOVERAGE_TEST_MARKER();
3904                         }
3905
3906                         if( xBreakLoop != pdFALSE )
3907                         {
3908                             break;
3909                         }
3910                     }
3911
3912                     if( pxReturn != NULL )
3913                     {
3914                         /* The handle has been found. */
3915                         break;
3916                     }
3917                 } while( pxNextTCB != pxFirstTCB );
3918             }
3919             else
3920             {
3921                 mtCOVERAGE_TEST_MARKER();
3922             }
3923
3924             return pxReturn;
3925         }
3926     #else /* if ( configNUMBER_OF_CORES == 1 ) */
3927         static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
3928                                                          const char pcNameToQuery[] )
3929         {
3930             TCB_t * pxReturn = NULL;
3931             UBaseType_t x;
3932             char cNextChar;
3933             BaseType_t xBreakLoop;
3934             const ListItem_t * pxEndMarker = listGET_END_MARKER( pxList );
3935             ListItem_t * pxIterator;
3936
3937             /* This function is called with the scheduler suspended. */
3938
3939             if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3940             {
3941                 for( pxIterator = listGET_HEAD_ENTRY( pxList ); pxIterator != pxEndMarker; pxIterator = listGET_NEXT( pxIterator ) )
3942                 {
3943                     TCB_t * pxTCB = listGET_LIST_ITEM_OWNER( pxIterator );
3944
3945                     /* Check each character in the name looking for a match or
3946                      * mismatch. */
3947                     xBreakLoop = pdFALSE;
3948
3949                     for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
3950                     {
3951                         cNextChar = pxTCB->pcTaskName[ x ];
3952
3953                         if( cNextChar != pcNameToQuery[ x ] )
3954                         {
3955                             /* Characters didn't match. */
3956                             xBreakLoop = pdTRUE;
3957                         }
3958                         else if( cNextChar == ( char ) 0x00 )
3959                         {
3960                             /* Both strings terminated, a match must have been
3961                              * found. */
3962                             pxReturn = pxTCB;
3963                             xBreakLoop = pdTRUE;
3964                         }
3965                         else
3966                         {
3967                             mtCOVERAGE_TEST_MARKER();
3968                         }
3969
3970                         if( xBreakLoop != pdFALSE )
3971                         {
3972                             break;
3973                         }
3974                     }
3975
3976                     if( pxReturn != NULL )
3977                     {
3978                         /* The handle has been found. */
3979                         break;
3980                     }
3981                 }
3982             }
3983             else
3984             {
3985                 mtCOVERAGE_TEST_MARKER();
3986             }
3987
3988             return pxReturn;
3989         }
3990     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
3991
3992 #endif /* INCLUDE_xTaskGetHandle */
3993 /*-----------------------------------------------------------*/
3994
3995 #if ( INCLUDE_xTaskGetHandle == 1 )
3996
3997     TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
3998     {
3999         UBaseType_t uxQueue = configMAX_PRIORITIES;
4000         TCB_t * pxTCB;
4001
4002         traceENTER_xTaskGetHandle( pcNameToQuery );
4003
4004         /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
4005         configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
4006
4007         vTaskSuspendAll();
4008         {
4009             /* Search the ready lists. */
4010             do
4011             {
4012                 uxQueue--;
4013                 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
4014
4015                 if( pxTCB != NULL )
4016                 {
4017                     /* Found the handle. */
4018                     break;
4019                 }
4020             } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4021
4022             /* Search the delayed lists. */
4023             if( pxTCB == NULL )
4024             {
4025                 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
4026             }
4027
4028             if( pxTCB == NULL )
4029             {
4030                 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
4031             }
4032
4033             #if ( INCLUDE_vTaskSuspend == 1 )
4034             {
4035                 if( pxTCB == NULL )
4036                 {
4037                     /* Search the suspended list. */
4038                     pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
4039                 }
4040             }
4041             #endif
4042
4043             #if ( INCLUDE_vTaskDelete == 1 )
4044             {
4045                 if( pxTCB == NULL )
4046                 {
4047                     /* Search the deleted list. */
4048                     pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
4049                 }
4050             }
4051             #endif
4052         }
4053         ( void ) xTaskResumeAll();
4054
4055         traceRETURN_xTaskGetHandle( pxTCB );
4056
4057         return pxTCB;
4058     }
4059
4060 #endif /* INCLUDE_xTaskGetHandle */
4061 /*-----------------------------------------------------------*/
4062
4063 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
4064
4065     BaseType_t xTaskGetStaticBuffers( TaskHandle_t xTask,
4066                                       StackType_t ** ppuxStackBuffer,
4067                                       StaticTask_t ** ppxTaskBuffer )
4068     {
4069         BaseType_t xReturn;
4070         TCB_t * pxTCB;
4071
4072         traceENTER_xTaskGetStaticBuffers( xTask, ppuxStackBuffer, ppxTaskBuffer );
4073
4074         configASSERT( ppuxStackBuffer != NULL );
4075         configASSERT( ppxTaskBuffer != NULL );
4076
4077         pxTCB = prvGetTCBFromHandle( xTask );
4078
4079         #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
4080         {
4081             if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB )
4082             {
4083                 *ppuxStackBuffer = pxTCB->pxStack;
4084                 *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4085                 xReturn = pdTRUE;
4086             }
4087             else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
4088             {
4089                 *ppuxStackBuffer = pxTCB->pxStack;
4090                 *ppxTaskBuffer = NULL;
4091                 xReturn = pdTRUE;
4092             }
4093             else
4094             {
4095                 xReturn = pdFALSE;
4096             }
4097         }
4098         #else /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4099         {
4100             *ppuxStackBuffer = pxTCB->pxStack;
4101             *ppxTaskBuffer = ( StaticTask_t * ) pxTCB;
4102             xReturn = pdTRUE;
4103         }
4104         #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 */
4105
4106         traceRETURN_xTaskGetStaticBuffers( xReturn );
4107
4108         return xReturn;
4109     }
4110
4111 #endif /* configSUPPORT_STATIC_ALLOCATION */
4112 /*-----------------------------------------------------------*/
4113
4114 #if ( configUSE_TRACE_FACILITY == 1 )
4115
4116     UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
4117                                       const UBaseType_t uxArraySize,
4118                                       configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
4119     {
4120         UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
4121
4122         traceENTER_uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, pulTotalRunTime );
4123
4124         vTaskSuspendAll();
4125         {
4126             /* Is there a space in the array for each task in the system? */
4127             if( uxArraySize >= uxCurrentNumberOfTasks )
4128             {
4129                 /* Fill in an TaskStatus_t structure with information on each
4130                  * task in the Ready state. */
4131                 do
4132                 {
4133                     uxQueue--;
4134                     uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady ) );
4135                 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4136
4137                 /* Fill in an TaskStatus_t structure with information on each
4138                  * task in the Blocked state. */
4139                 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked ) );
4140                 uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked ) );
4141
4142                 #if ( INCLUDE_vTaskDelete == 1 )
4143                 {
4144                     /* Fill in an TaskStatus_t structure with information on
4145                      * each task that has been deleted but not yet cleaned up. */
4146                     uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted ) );
4147                 }
4148                 #endif
4149
4150                 #if ( INCLUDE_vTaskSuspend == 1 )
4151                 {
4152                     /* Fill in an TaskStatus_t structure with information on
4153                      * each task in the Suspended state. */
4154                     uxTask = ( UBaseType_t ) ( uxTask + prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended ) );
4155                 }
4156                 #endif
4157
4158                 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4159                 {
4160                     if( pulTotalRunTime != NULL )
4161                     {
4162                         #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4163                             portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
4164                         #else
4165                             *pulTotalRunTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
4166                         #endif
4167                     }
4168                 }
4169                 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4170                 {
4171                     if( pulTotalRunTime != NULL )
4172                     {
4173                         *pulTotalRunTime = 0;
4174                     }
4175                 }
4176                 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
4177             }
4178             else
4179             {
4180                 mtCOVERAGE_TEST_MARKER();
4181             }
4182         }
4183         ( void ) xTaskResumeAll();
4184
4185         traceRETURN_uxTaskGetSystemState( uxTask );
4186
4187         return uxTask;
4188     }
4189
4190 #endif /* configUSE_TRACE_FACILITY */
4191 /*----------------------------------------------------------*/
4192
4193 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
4194
4195 /* SMP_TODO : This function returns only idle task handle for core 0.
4196  * Consider to add another function to return the idle task handles. */
4197     TaskHandle_t xTaskGetIdleTaskHandle( void )
4198     {
4199         traceENTER_xTaskGetIdleTaskHandle();
4200
4201         /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
4202          * started, then xIdleTaskHandles will be NULL. */
4203         configASSERT( ( xIdleTaskHandles[ 0 ] != NULL ) );
4204
4205         traceRETURN_xTaskGetIdleTaskHandle( xIdleTaskHandles[ 0 ] );
4206
4207         return xIdleTaskHandles[ 0 ];
4208     }
4209
4210 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
4211 /*----------------------------------------------------------*/
4212
4213 /* This conditional compilation should use inequality to 0, not equality to 1.
4214  * This is to ensure vTaskStepTick() is available when user defined low power mode
4215  * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
4216  * 1. */
4217 #if ( configUSE_TICKLESS_IDLE != 0 )
4218
4219     void vTaskStepTick( TickType_t xTicksToJump )
4220     {
4221         traceENTER_vTaskStepTick( xTicksToJump );
4222
4223         /* Correct the tick count value after a period during which the tick
4224          * was suppressed.  Note this does *not* call the tick hook function for
4225          * each stepped tick. */
4226         configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
4227
4228         if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
4229         {
4230             /* Arrange for xTickCount to reach xNextTaskUnblockTime in
4231              * xTaskIncrementTick() when the scheduler resumes.  This ensures
4232              * that any delayed tasks are resumed at the correct time. */
4233             configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4234             configASSERT( xTicksToJump != ( TickType_t ) 0 );
4235
4236             /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4237             taskENTER_CRITICAL();
4238             {
4239                 xPendedTicks++;
4240             }
4241             taskEXIT_CRITICAL();
4242             xTicksToJump--;
4243         }
4244         else
4245         {
4246             mtCOVERAGE_TEST_MARKER();
4247         }
4248
4249         xTickCount += xTicksToJump;
4250
4251         traceINCREASE_TICK_COUNT( xTicksToJump );
4252         traceRETURN_vTaskStepTick();
4253     }
4254
4255 #endif /* configUSE_TICKLESS_IDLE */
4256 /*----------------------------------------------------------*/
4257
4258 BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
4259 {
4260     BaseType_t xYieldOccurred;
4261
4262     traceENTER_xTaskCatchUpTicks( xTicksToCatchUp );
4263
4264     /* Must not be called with the scheduler suspended as the implementation
4265      * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
4266     configASSERT( uxSchedulerSuspended == ( UBaseType_t ) 0U );
4267
4268     /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
4269      * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
4270     vTaskSuspendAll();
4271
4272     /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
4273     taskENTER_CRITICAL();
4274     {
4275         xPendedTicks += xTicksToCatchUp;
4276     }
4277     taskEXIT_CRITICAL();
4278     xYieldOccurred = xTaskResumeAll();
4279
4280     traceRETURN_xTaskCatchUpTicks( xYieldOccurred );
4281
4282     return xYieldOccurred;
4283 }
4284 /*----------------------------------------------------------*/
4285
4286 #if ( INCLUDE_xTaskAbortDelay == 1 )
4287
4288     BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
4289     {
4290         TCB_t * pxTCB = xTask;
4291         BaseType_t xReturn;
4292
4293         traceENTER_xTaskAbortDelay( xTask );
4294
4295         configASSERT( pxTCB );
4296
4297         vTaskSuspendAll();
4298         {
4299             /* A task can only be prematurely removed from the Blocked state if
4300              * it is actually in the Blocked state. */
4301             if( eTaskGetState( xTask ) == eBlocked )
4302             {
4303                 xReturn = pdPASS;
4304
4305                 /* Remove the reference to the task from the blocked list.  An
4306                  * interrupt won't touch the xStateListItem because the
4307                  * scheduler is suspended. */
4308                 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
4309
4310                 /* Is the task waiting on an event also?  If so remove it from
4311                  * the event list too.  Interrupts can touch the event list item,
4312                  * even though the scheduler is suspended, so a critical section
4313                  * is used. */
4314                 taskENTER_CRITICAL();
4315                 {
4316                     if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4317                     {
4318                         ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
4319
4320                         /* This lets the task know it was forcibly removed from the
4321                          * blocked state so it should not re-evaluate its block time and
4322                          * then block again. */
4323                         pxTCB->ucDelayAborted = pdTRUE;
4324                     }
4325                     else
4326                     {
4327                         mtCOVERAGE_TEST_MARKER();
4328                     }
4329                 }
4330                 taskEXIT_CRITICAL();
4331
4332                 /* Place the unblocked task into the appropriate ready list. */
4333                 prvAddTaskToReadyList( pxTCB );
4334
4335                 /* A task being unblocked cannot cause an immediate context
4336                  * switch if preemption is turned off. */
4337                 #if ( configUSE_PREEMPTION == 1 )
4338                 {
4339                     #if ( configNUMBER_OF_CORES == 1 )
4340                     {
4341                         /* Preemption is on, but a context switch should only be
4342                          * performed if the unblocked task has a priority that is
4343                          * higher than the currently executing task. */
4344                         if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4345                         {
4346                             /* Pend the yield to be performed when the scheduler
4347                              * is unsuspended. */
4348                             xYieldPendings[ 0 ] = pdTRUE;
4349                         }
4350                         else
4351                         {
4352                             mtCOVERAGE_TEST_MARKER();
4353                         }
4354                     }
4355                     #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4356                     {
4357                         taskENTER_CRITICAL();
4358                         {
4359                             prvYieldForTask( pxTCB );
4360                         }
4361                         taskEXIT_CRITICAL();
4362                     }
4363                     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4364                 }
4365                 #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4366             }
4367             else
4368             {
4369                 xReturn = pdFAIL;
4370             }
4371         }
4372         ( void ) xTaskResumeAll();
4373
4374         traceRETURN_xTaskAbortDelay( xReturn );
4375
4376         return xReturn;
4377     }
4378
4379 #endif /* INCLUDE_xTaskAbortDelay */
4380 /*----------------------------------------------------------*/
4381
4382 BaseType_t xTaskIncrementTick( void )
4383 {
4384     TCB_t * pxTCB;
4385     TickType_t xItemValue;
4386     BaseType_t xSwitchRequired = pdFALSE;
4387
4388     #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 )
4389     BaseType_t xYieldRequiredForCore[ configNUMBER_OF_CORES ] = { pdFALSE };
4390     #endif /* #if ( configUSE_PREEMPTION == 1 ) && ( configNUMBER_OF_CORES > 1 ) */
4391
4392     traceENTER_xTaskIncrementTick();
4393
4394     /* Called by the portable layer each time a tick interrupt occurs.
4395      * Increments the tick then checks to see if the new tick value will cause any
4396      * tasks to be unblocked. */
4397     traceTASK_INCREMENT_TICK( xTickCount );
4398
4399     /* Tick increment should occur on every kernel timer event. Core 0 has the
4400      * responsibility to increment the tick, or increment the pended ticks if the
4401      * scheduler is suspended.  If pended ticks is greater than zero, the core that
4402      * calls xTaskResumeAll has the responsibility to increment the tick. */
4403     if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
4404     {
4405         /* Minor optimisation.  The tick count cannot change in this
4406          * block. */
4407         const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
4408
4409         /* Increment the RTOS tick, switching the delayed and overflowed
4410          * delayed lists if it wraps to 0. */
4411         xTickCount = xConstTickCount;
4412
4413         if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
4414         {
4415             taskSWITCH_DELAYED_LISTS();
4416         }
4417         else
4418         {
4419             mtCOVERAGE_TEST_MARKER();
4420         }
4421
4422         /* See if this tick has made a timeout expire.  Tasks are stored in
4423          * the  queue in the order of their wake time - meaning once one task
4424          * has been found whose block time has not expired there is no need to
4425          * look any further down the list. */
4426         if( xConstTickCount >= xNextTaskUnblockTime )
4427         {
4428             for( ; ; )
4429             {
4430                 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4431                 {
4432                     /* The delayed list is empty.  Set xNextTaskUnblockTime
4433                      * to the maximum possible value so it is extremely
4434                      * unlikely that the
4435                      * if( xTickCount >= xNextTaskUnblockTime ) test will pass
4436                      * next time through. */
4437                     xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4438                     break;
4439                 }
4440                 else
4441                 {
4442                     /* The delayed list is not empty, get the value of the
4443                      * item at the head of the delayed list.  This is the time
4444                      * at which the task at the head of the delayed list must
4445                      * be removed from the Blocked state. */
4446                     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. */
4447                     xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
4448
4449                     if( xConstTickCount < xItemValue )
4450                     {
4451                         /* It is not time to unblock this item yet, but the
4452                          * item value is the time at which the task at the head
4453                          * of the blocked list must be removed from the Blocked
4454                          * state -  so record the item value in
4455                          * xNextTaskUnblockTime. */
4456                         xNextTaskUnblockTime = xItemValue;
4457                         break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
4458                     }
4459                     else
4460                     {
4461                         mtCOVERAGE_TEST_MARKER();
4462                     }
4463
4464                     /* It is time to remove the item from the Blocked state. */
4465                     listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4466
4467                     /* Is the task waiting on an event also?  If so remove
4468                      * it from the event list. */
4469                     if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
4470                     {
4471                         listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
4472                     }
4473                     else
4474                     {
4475                         mtCOVERAGE_TEST_MARKER();
4476                     }
4477
4478                     /* Place the unblocked task into the appropriate ready
4479                      * list. */
4480                     prvAddTaskToReadyList( pxTCB );
4481
4482                     /* A task being unblocked cannot cause an immediate
4483                      * context switch if preemption is turned off. */
4484                     #if ( configUSE_PREEMPTION == 1 )
4485                     {
4486                         #if ( configNUMBER_OF_CORES == 1 )
4487                         {
4488                             /* Preemption is on, but a context switch should
4489                              * only be performed if the unblocked task's
4490                              * priority is higher than the currently executing
4491                              * task.
4492                              * The case of equal priority tasks sharing
4493                              * processing time (which happens when both
4494                              * preemption and time slicing are on) is
4495                              * handled below.*/
4496                             if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4497                             {
4498                                 xSwitchRequired = pdTRUE;
4499                             }
4500                             else
4501                             {
4502                                 mtCOVERAGE_TEST_MARKER();
4503                             }
4504                         }
4505                         #else /* #if( configNUMBER_OF_CORES == 1 ) */
4506                         {
4507                             prvYieldForTask( pxTCB );
4508                         }
4509                         #endif /* #if( configNUMBER_OF_CORES == 1 ) */
4510                     }
4511                     #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4512                 }
4513             }
4514         }
4515
4516         /* Tasks of equal priority to the currently running task will share
4517          * processing time (time slice) if preemption is on, and the application
4518          * writer has not explicitly turned time slicing off. */
4519         #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
4520         {
4521             #if ( configNUMBER_OF_CORES == 1 )
4522             {
4523                 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
4524                 {
4525                     xSwitchRequired = pdTRUE;
4526                 }
4527                 else
4528                 {
4529                     mtCOVERAGE_TEST_MARKER();
4530                 }
4531             }
4532             #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4533             {
4534                 BaseType_t xCoreID;
4535
4536                 for( xCoreID = 0; xCoreID < ( ( BaseType_t ) configNUMBER_OF_CORES ); xCoreID++ )
4537                 {
4538                     if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCBs[ xCoreID ]->uxPriority ] ) ) > 1 )
4539                     {
4540                         xYieldRequiredForCore[ xCoreID ] = pdTRUE;
4541                     }
4542                     else
4543                     {
4544                         mtCOVERAGE_TEST_MARKER();
4545                     }
4546                 }
4547             }
4548             #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4549         }
4550         #endif /* #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
4551
4552         #if ( configUSE_TICK_HOOK == 1 )
4553         {
4554             /* Guard against the tick hook being called when the pended tick
4555              * count is being unwound (when the scheduler is being unlocked). */
4556             if( xPendedTicks == ( TickType_t ) 0 )
4557             {
4558                 vApplicationTickHook();
4559             }
4560             else
4561             {
4562                 mtCOVERAGE_TEST_MARKER();
4563             }
4564         }
4565         #endif /* configUSE_TICK_HOOK */
4566
4567         #if ( configUSE_PREEMPTION == 1 )
4568         {
4569             #if ( configNUMBER_OF_CORES == 1 )
4570             {
4571                 /* For single core the core ID is always 0. */
4572                 if( xYieldPendings[ 0 ] != pdFALSE )
4573                 {
4574                     xSwitchRequired = pdTRUE;
4575                 }
4576                 else
4577                 {
4578                     mtCOVERAGE_TEST_MARKER();
4579                 }
4580             }
4581             #else /* #if ( configNUMBER_OF_CORES == 1 ) */
4582             {
4583                 BaseType_t xCoreID, xCurrentCoreID;
4584                 xCurrentCoreID = ( BaseType_t ) portGET_CORE_ID();
4585
4586                 for( xCoreID = 0; xCoreID < ( BaseType_t ) configNUMBER_OF_CORES; xCoreID++ )
4587                 {
4588                     #if ( configUSE_TASK_PREEMPTION_DISABLE == 1 )
4589                         if( pxCurrentTCBs[ xCoreID ]->xPreemptionDisable == pdFALSE )
4590                     #endif
4591                     {
4592                         if( ( xYieldRequiredForCore[ xCoreID ] != pdFALSE ) || ( xYieldPendings[ xCoreID ] != pdFALSE ) )
4593                         {
4594                             if( xCoreID == xCurrentCoreID )
4595                             {
4596                                 xSwitchRequired = pdTRUE;
4597                             }
4598                             else
4599                             {
4600                                 prvYieldCore( xCoreID );
4601                             }
4602                         }
4603                         else
4604                         {
4605                             mtCOVERAGE_TEST_MARKER();
4606                         }
4607                     }
4608                 }
4609             }
4610             #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
4611         }
4612         #endif /* #if ( configUSE_PREEMPTION == 1 ) */
4613     }
4614     else
4615     {
4616         ++xPendedTicks;
4617
4618         /* The tick hook gets called at regular intervals, even if the
4619          * scheduler is locked. */
4620         #if ( configUSE_TICK_HOOK == 1 )
4621         {
4622             vApplicationTickHook();
4623         }
4624         #endif
4625     }
4626
4627     traceRETURN_xTaskIncrementTick( xSwitchRequired );
4628
4629     return xSwitchRequired;
4630 }
4631 /*-----------------------------------------------------------*/
4632
4633 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4634
4635     void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
4636                                      TaskHookFunction_t pxHookFunction )
4637     {
4638         TCB_t * xTCB;
4639
4640         traceENTER_vTaskSetApplicationTaskTag( xTask, pxHookFunction );
4641
4642         /* If xTask is NULL then it is the task hook of the calling task that is
4643          * getting set. */
4644         if( xTask == NULL )
4645         {
4646             xTCB = ( TCB_t * ) pxCurrentTCB;
4647         }
4648         else
4649         {
4650             xTCB = xTask;
4651         }
4652
4653         /* Save the hook function in the TCB.  A critical section is required as
4654          * the value can be accessed from an interrupt. */
4655         taskENTER_CRITICAL();
4656         {
4657             xTCB->pxTaskTag = pxHookFunction;
4658         }
4659         taskEXIT_CRITICAL();
4660
4661         traceRETURN_vTaskSetApplicationTaskTag();
4662     }
4663
4664 #endif /* configUSE_APPLICATION_TASK_TAG */
4665 /*-----------------------------------------------------------*/
4666
4667 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4668
4669     TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
4670     {
4671         TCB_t * pxTCB;
4672         TaskHookFunction_t xReturn;
4673
4674         traceENTER_xTaskGetApplicationTaskTag( xTask );
4675
4676         /* If xTask is NULL then set the calling task's hook. */
4677         pxTCB = prvGetTCBFromHandle( xTask );
4678
4679         /* Save the hook function in the TCB.  A critical section is required as
4680          * the value can be accessed from an interrupt. */
4681         taskENTER_CRITICAL();
4682         {
4683             xReturn = pxTCB->pxTaskTag;
4684         }
4685         taskEXIT_CRITICAL();
4686
4687         traceRETURN_xTaskGetApplicationTaskTag( xReturn );
4688
4689         return xReturn;
4690     }
4691
4692 #endif /* configUSE_APPLICATION_TASK_TAG */
4693 /*-----------------------------------------------------------*/
4694
4695 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4696
4697     TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
4698     {
4699         TCB_t * pxTCB;
4700         TaskHookFunction_t xReturn;
4701         UBaseType_t uxSavedInterruptStatus;
4702
4703         traceENTER_xTaskGetApplicationTaskTagFromISR( xTask );
4704
4705         /* If xTask is NULL then set the calling task's hook. */
4706         pxTCB = prvGetTCBFromHandle( xTask );
4707
4708         /* Save the hook function in the TCB.  A critical section is required as
4709          * the value can be accessed from an interrupt. */
4710         uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
4711         {
4712             xReturn = pxTCB->pxTaskTag;
4713         }
4714         taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
4715
4716         traceRETURN_xTaskGetApplicationTaskTagFromISR( xReturn );
4717
4718         return xReturn;
4719     }
4720
4721 #endif /* configUSE_APPLICATION_TASK_TAG */
4722 /*-----------------------------------------------------------*/
4723
4724 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
4725
4726     BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
4727                                              void * pvParameter )
4728     {
4729         TCB_t * xTCB;
4730         BaseType_t xReturn;
4731
4732         traceENTER_xTaskCallApplicationTaskHook( xTask, pvParameter );
4733
4734         /* If xTask is NULL then we are calling our own task hook. */
4735         if( xTask == NULL )
4736         {
4737             xTCB = pxCurrentTCB;
4738         }
4739         else
4740         {
4741             xTCB = xTask;
4742         }
4743
4744         if( xTCB->pxTaskTag != NULL )
4745         {
4746             xReturn = xTCB->pxTaskTag( pvParameter );
4747         }
4748         else
4749         {
4750             xReturn = pdFAIL;
4751         }
4752
4753         traceRETURN_xTaskCallApplicationTaskHook( xReturn );
4754
4755         return xReturn;
4756     }
4757
4758 #endif /* configUSE_APPLICATION_TASK_TAG */
4759 /*-----------------------------------------------------------*/
4760
4761 #if ( configNUMBER_OF_CORES == 1 )
4762     void vTaskSwitchContext( void )
4763     {
4764         traceENTER_vTaskSwitchContext();
4765
4766         if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
4767         {
4768             /* The scheduler is currently suspended - do not allow a context
4769              * switch. */
4770             xYieldPendings[ 0 ] = pdTRUE;
4771         }
4772         else
4773         {
4774             xYieldPendings[ 0 ] = pdFALSE;
4775             traceTASK_SWITCHED_OUT();
4776
4777             #if ( configGENERATE_RUN_TIME_STATS == 1 )
4778             {
4779                 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4780                     portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ 0 ] );
4781                 #else
4782                     ulTotalRunTime[ 0 ] = portGET_RUN_TIME_COUNTER_VALUE();
4783                 #endif
4784
4785                 /* Add the amount of time the task has been running to the
4786                  * accumulated time so far.  The time the task started running was
4787                  * stored in ulTaskSwitchedInTime.  Note that there is no overflow
4788                  * protection here so count values are only valid until the timer
4789                  * overflows.  The guard against negative values is to protect
4790                  * against suspect run time stat counter implementations - which
4791                  * are provided by the application, not the kernel. */
4792                 if( ulTotalRunTime[ 0 ] > ulTaskSwitchedInTime[ 0 ] )
4793                 {
4794                     pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ 0 ] - ulTaskSwitchedInTime[ 0 ] );
4795                 }
4796                 else
4797                 {
4798                     mtCOVERAGE_TEST_MARKER();
4799                 }
4800
4801                 ulTaskSwitchedInTime[ 0 ] = ulTotalRunTime[ 0 ];
4802             }
4803             #endif /* configGENERATE_RUN_TIME_STATS */
4804
4805             /* Check for stack overflow, if configured. */
4806             taskCHECK_FOR_STACK_OVERFLOW();
4807
4808             /* Before the currently running task is switched out, save its errno. */
4809             #if ( configUSE_POSIX_ERRNO == 1 )
4810             {
4811                 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
4812             }
4813             #endif
4814
4815             /* Select a new task to run using either the generic C or port
4816              * optimised asm code. */
4817             taskSELECT_HIGHEST_PRIORITY_TASK(); /*lint !e9079 void * is used as this macro is used with timers too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
4818             traceTASK_SWITCHED_IN();
4819
4820             /* After the new task is switched in, update the global errno. */
4821             #if ( configUSE_POSIX_ERRNO == 1 )
4822             {
4823                 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
4824             }
4825             #endif
4826
4827             #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4828             {
4829                 /* Switch C-Runtime's TLS Block to point to the TLS
4830                  * Block specific to this task. */
4831                 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4832             }
4833             #endif
4834         }
4835
4836         traceRETURN_vTaskSwitchContext();
4837     }
4838 #else /* if ( configNUMBER_OF_CORES == 1 ) */
4839     void vTaskSwitchContext( BaseType_t xCoreID )
4840     {
4841         traceENTER_vTaskSwitchContext();
4842
4843         /* Acquire both locks:
4844          * - The ISR lock protects the ready list from simultaneous access by
4845          *   both other ISRs and tasks.
4846          * - We also take the task lock to pause here in case another core has
4847          *   suspended the scheduler. We don't want to simply set xYieldPending
4848          *   and move on if another core suspended the scheduler. We should only
4849          *   do that if the current core has suspended the scheduler. */
4850
4851         portGET_TASK_LOCK(); /* Must always acquire the task lock first. */
4852         portGET_ISR_LOCK();
4853         {
4854             /* vTaskSwitchContext() must never be called from within a critical section.
4855              * This is not necessarily true for single core FreeRTOS, but it is for this
4856              * SMP port. */
4857             configASSERT( portGET_CRITICAL_NESTING_COUNT() == 0 );
4858
4859             if( uxSchedulerSuspended != ( UBaseType_t ) 0U )
4860             {
4861                 /* The scheduler is currently suspended - do not allow a context
4862                  * switch. */
4863                 xYieldPendings[ xCoreID ] = pdTRUE;
4864             }
4865             else
4866             {
4867                 xYieldPendings[ xCoreID ] = pdFALSE;
4868                 traceTASK_SWITCHED_OUT();
4869
4870                 #if ( configGENERATE_RUN_TIME_STATS == 1 )
4871                 {
4872                     #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
4873                         portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime[ xCoreID ] );
4874                     #else
4875                         ulTotalRunTime[ xCoreID ] = portGET_RUN_TIME_COUNTER_VALUE();
4876                     #endif
4877
4878                     /* Add the amount of time the task has been running to the
4879                      * accumulated time so far.  The time the task started running was
4880                      * stored in ulTaskSwitchedInTime.  Note that there is no overflow
4881                      * protection here so count values are only valid until the timer
4882                      * overflows.  The guard against negative values is to protect
4883                      * against suspect run time stat counter implementations - which
4884                      * are provided by the application, not the kernel. */
4885                     if( ulTotalRunTime[ xCoreID ] > ulTaskSwitchedInTime[ xCoreID ] )
4886                     {
4887                         pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime[ xCoreID ] - ulTaskSwitchedInTime[ xCoreID ] );
4888                     }
4889                     else
4890                     {
4891                         mtCOVERAGE_TEST_MARKER();
4892                     }
4893
4894                     ulTaskSwitchedInTime[ xCoreID ] = ulTotalRunTime[ xCoreID ];
4895                 }
4896                 #endif /* configGENERATE_RUN_TIME_STATS */
4897
4898                 /* Check for stack overflow, if configured. */
4899                 taskCHECK_FOR_STACK_OVERFLOW();
4900
4901                 /* Before the currently running task is switched out, save its errno. */
4902                 #if ( configUSE_POSIX_ERRNO == 1 )
4903                 {
4904                     pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
4905                 }
4906                 #endif
4907
4908                 /* Select a new task to run. */
4909                 taskSELECT_HIGHEST_PRIORITY_TASK( xCoreID );
4910                 traceTASK_SWITCHED_IN();
4911
4912                 /* After the new task is switched in, update the global errno. */
4913                 #if ( configUSE_POSIX_ERRNO == 1 )
4914                 {
4915                     FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
4916                 }
4917                 #endif
4918
4919                 #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
4920                 {
4921                     /* Switch C-Runtime's TLS Block to point to the TLS
4922                      * Block specific to this task. */
4923                     configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
4924                 }
4925                 #endif
4926             }
4927         }
4928         portRELEASE_ISR_LOCK();
4929         portRELEASE_TASK_LOCK();
4930
4931         traceRETURN_vTaskSwitchContext();
4932     }
4933 #endif /* if ( configNUMBER_OF_CORES > 1 ) */
4934 /*-----------------------------------------------------------*/
4935
4936 void vTaskPlaceOnEventList( List_t * const pxEventList,
4937                             const TickType_t xTicksToWait )
4938 {
4939     traceENTER_vTaskPlaceOnEventList( pxEventList, xTicksToWait );
4940
4941     configASSERT( pxEventList );
4942
4943     /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
4944      * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
4945
4946     /* Place the event list item of the TCB in the appropriate event list.
4947      * This is placed in the list in priority order so the highest priority task
4948      * is the first to be woken by the event.
4949      *
4950      * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
4951      * Normally, the xItemValue of a TCB's ListItem_t members is:
4952      *      xItemValue = ( configMAX_PRIORITIES - uxPriority )
4953      * Therefore, the event list is sorted in descending priority order.
4954      *
4955      * The queue that contains the event list is locked, preventing
4956      * simultaneous access from interrupts. */
4957     vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4958
4959     prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4960
4961     traceRETURN_vTaskPlaceOnEventList();
4962 }
4963 /*-----------------------------------------------------------*/
4964
4965 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
4966                                      const TickType_t xItemValue,
4967                                      const TickType_t xTicksToWait )
4968 {
4969     traceENTER_vTaskPlaceOnUnorderedEventList( pxEventList, xItemValue, xTicksToWait );
4970
4971     configASSERT( pxEventList );
4972
4973     /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED.  It is used by
4974      * the event groups implementation. */
4975     configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
4976
4977     /* Store the item value in the event list item.  It is safe to access the
4978      * event list item here as interrupts won't access the event list item of a
4979      * task that is not in the Blocked state. */
4980     listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
4981
4982     /* Place the event list item of the TCB at the end of the appropriate event
4983      * list.  It is safe to access the event list here because it is part of an
4984      * event group implementation - and interrupts don't access event groups
4985      * directly (instead they access them indirectly by pending function calls to
4986      * the task level). */
4987     listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
4988
4989     prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4990
4991     traceRETURN_vTaskPlaceOnUnorderedEventList();
4992 }
4993 /*-----------------------------------------------------------*/
4994
4995 #if ( configUSE_TIMERS == 1 )
4996
4997     void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
4998                                           TickType_t xTicksToWait,
4999                                           const BaseType_t xWaitIndefinitely )
5000     {
5001         traceENTER_vTaskPlaceOnEventListRestricted( pxEventList, xTicksToWait, xWaitIndefinitely );
5002
5003         configASSERT( pxEventList );
5004
5005         /* This function should not be called by application code hence the
5006          * 'Restricted' in its name.  It is not part of the public API.  It is
5007          * designed for use by kernel code, and has special calling requirements -
5008          * it should be called with the scheduler suspended. */
5009
5010
5011         /* Place the event list item of the TCB in the appropriate event list.
5012          * In this case it is assume that this is the only task that is going to
5013          * be waiting on this event list, so the faster vListInsertEnd() function
5014          * can be used in place of vListInsert. */
5015         listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
5016
5017         /* If the task should block indefinitely then set the block time to a
5018          * value that will be recognised as an indefinite delay inside the
5019          * prvAddCurrentTaskToDelayedList() function. */
5020         if( xWaitIndefinitely != pdFALSE )
5021         {
5022             xTicksToWait = portMAX_DELAY;
5023         }
5024
5025         traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
5026         prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
5027
5028         traceRETURN_vTaskPlaceOnEventListRestricted();
5029     }
5030
5031 #endif /* configUSE_TIMERS */
5032 /*-----------------------------------------------------------*/
5033
5034 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
5035 {
5036     TCB_t * pxUnblockedTCB;
5037     BaseType_t xReturn;
5038
5039     traceENTER_xTaskRemoveFromEventList( pxEventList );
5040
5041     /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION.  It can also be
5042      * called from a critical section within an ISR. */
5043
5044     /* The event list is sorted in priority order, so the first in the list can
5045      * be removed as it is known to be the highest priority.  Remove the TCB from
5046      * the delayed list, and add it to the ready list.
5047      *
5048      * If an event is for a queue that is locked then this function will never
5049      * get called - the lock count on the queue will get modified instead.  This
5050      * means exclusive access to the event list is guaranteed here.
5051      *
5052      * This function assumes that a check has already been made to ensure that
5053      * pxEventList is not empty. */
5054     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. */
5055     configASSERT( pxUnblockedTCB );
5056     listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
5057
5058     if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
5059     {
5060         listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5061         prvAddTaskToReadyList( pxUnblockedTCB );
5062
5063         #if ( configUSE_TICKLESS_IDLE != 0 )
5064         {
5065             /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5066              * might be set to the blocked task's time out time.  If the task is
5067              * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5068              * normally left unchanged, because it is automatically reset to a new
5069              * value when the tick count equals xNextTaskUnblockTime.  However if
5070              * tickless idling is used it might be more important to enter sleep mode
5071              * at the earliest possible time - so reset xNextTaskUnblockTime here to
5072              * ensure it is updated at the earliest possible time. */
5073             prvResetNextTaskUnblockTime();
5074         }
5075         #endif
5076     }
5077     else
5078     {
5079         /* The delayed and ready lists cannot be accessed, so hold this task
5080          * pending until the scheduler is resumed. */
5081         listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
5082     }
5083
5084     #if ( configNUMBER_OF_CORES == 1 )
5085     {
5086         if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5087         {
5088             /* Return true if the task removed from the event list has a higher
5089              * priority than the calling task.  This allows the calling task to know if
5090              * it should force a context switch now. */
5091             xReturn = pdTRUE;
5092
5093             /* Mark that a yield is pending in case the user is not using the
5094              * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
5095             xYieldPendings[ 0 ] = pdTRUE;
5096         }
5097         else
5098         {
5099             xReturn = pdFALSE;
5100         }
5101     }
5102     #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5103     {
5104         xReturn = pdFALSE;
5105
5106         #if ( configUSE_PREEMPTION == 1 )
5107         {
5108             prvYieldForTask( pxUnblockedTCB );
5109
5110             if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5111             {
5112                 xReturn = pdTRUE;
5113             }
5114         }
5115         #endif /* #if ( configUSE_PREEMPTION == 1 ) */
5116     }
5117     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5118
5119     traceRETURN_xTaskRemoveFromEventList( xReturn );
5120     return xReturn;
5121 }
5122 /*-----------------------------------------------------------*/
5123
5124 void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
5125                                         const TickType_t xItemValue )
5126 {
5127     TCB_t * pxUnblockedTCB;
5128
5129     traceENTER_vTaskRemoveFromUnorderedEventList( pxEventListItem, xItemValue );
5130
5131     /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED.  It is used by
5132      * the event flags implementation. */
5133     configASSERT( uxSchedulerSuspended != ( UBaseType_t ) 0U );
5134
5135     /* Store the new item value in the event list. */
5136     listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
5137
5138     /* Remove the event list form the event flag.  Interrupts do not access
5139      * event flags. */
5140     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. */
5141     configASSERT( pxUnblockedTCB );
5142     listREMOVE_ITEM( pxEventListItem );
5143
5144     #if ( configUSE_TICKLESS_IDLE != 0 )
5145     {
5146         /* If a task is blocked on a kernel object then xNextTaskUnblockTime
5147          * might be set to the blocked task's time out time.  If the task is
5148          * unblocked for a reason other than a timeout xNextTaskUnblockTime is
5149          * normally left unchanged, because it is automatically reset to a new
5150          * value when the tick count equals xNextTaskUnblockTime.  However if
5151          * tickless idling is used it might be more important to enter sleep mode
5152          * at the earliest possible time - so reset xNextTaskUnblockTime here to
5153          * ensure it is updated at the earliest possible time. */
5154         prvResetNextTaskUnblockTime();
5155     }
5156     #endif
5157
5158     /* Remove the task from the delayed list and add it to the ready list.  The
5159      * scheduler is suspended so interrupts will not be accessing the ready
5160      * lists. */
5161     listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
5162     prvAddTaskToReadyList( pxUnblockedTCB );
5163
5164     #if ( configNUMBER_OF_CORES == 1 )
5165     {
5166         if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
5167         {
5168             /* The unblocked task has a priority above that of the calling task, so
5169              * a context switch is required.  This function is called with the
5170              * scheduler suspended so xYieldPending is set so the context switch
5171              * occurs immediately that the scheduler is resumed (unsuspended). */
5172             xYieldPendings[ 0 ] = pdTRUE;
5173         }
5174     }
5175     #else /* #if ( configNUMBER_OF_CORES == 1 ) */
5176     {
5177         #if ( configUSE_PREEMPTION == 1 )
5178         {
5179             taskENTER_CRITICAL();
5180             {
5181                 prvYieldForTask( pxUnblockedTCB );
5182             }
5183             taskEXIT_CRITICAL();
5184         }
5185         #endif
5186     }
5187     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
5188
5189     traceRETURN_vTaskRemoveFromUnorderedEventList();
5190 }
5191 /*-----------------------------------------------------------*/
5192
5193 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
5194 {
5195     traceENTER_vTaskSetTimeOutState( pxTimeOut );
5196
5197     configASSERT( pxTimeOut );
5198     taskENTER_CRITICAL();
5199     {
5200         pxTimeOut->xOverflowCount = xNumOfOverflows;
5201         pxTimeOut->xTimeOnEntering = xTickCount;
5202     }
5203     taskEXIT_CRITICAL();
5204
5205     traceRETURN_vTaskSetTimeOutState();
5206 }
5207 /*-----------------------------------------------------------*/
5208
5209 void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
5210 {
5211     traceENTER_vTaskInternalSetTimeOutState( pxTimeOut );
5212
5213     /* For internal use only as it does not use a critical section. */
5214     pxTimeOut->xOverflowCount = xNumOfOverflows;
5215     pxTimeOut->xTimeOnEntering = xTickCount;
5216
5217     traceRETURN_vTaskInternalSetTimeOutState();
5218 }
5219 /*-----------------------------------------------------------*/
5220
5221 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
5222                                  TickType_t * const pxTicksToWait )
5223 {
5224     BaseType_t xReturn;
5225
5226     traceENTER_xTaskCheckForTimeOut( pxTimeOut, pxTicksToWait );
5227
5228     configASSERT( pxTimeOut );
5229     configASSERT( pxTicksToWait );
5230
5231     taskENTER_CRITICAL();
5232     {
5233         /* Minor optimisation.  The tick count cannot change in this block. */
5234         const TickType_t xConstTickCount = xTickCount;
5235         const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
5236
5237         #if ( INCLUDE_xTaskAbortDelay == 1 )
5238             if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
5239             {
5240                 /* The delay was aborted, which is not the same as a time out,
5241                  * but has the same result. */
5242                 pxCurrentTCB->ucDelayAborted = pdFALSE;
5243                 xReturn = pdTRUE;
5244             }
5245             else
5246         #endif
5247
5248         #if ( INCLUDE_vTaskSuspend == 1 )
5249             if( *pxTicksToWait == portMAX_DELAY )
5250             {
5251                 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
5252                  * specified is the maximum block time then the task should block
5253                  * indefinitely, and therefore never time out. */
5254                 xReturn = pdFALSE;
5255             }
5256             else
5257         #endif
5258
5259         if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
5260         {
5261             /* The tick count is greater than the time at which
5262              * vTaskSetTimeout() was called, but has also overflowed since
5263              * vTaskSetTimeOut() was called.  It must have wrapped all the way
5264              * around and gone past again. This passed since vTaskSetTimeout()
5265              * was called. */
5266             xReturn = pdTRUE;
5267             *pxTicksToWait = ( TickType_t ) 0;
5268         }
5269         else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
5270         {
5271             /* Not a genuine timeout. Adjust parameters for time remaining. */
5272             *pxTicksToWait -= xElapsedTime;
5273             vTaskInternalSetTimeOutState( pxTimeOut );
5274             xReturn = pdFALSE;
5275         }
5276         else
5277         {
5278             *pxTicksToWait = ( TickType_t ) 0;
5279             xReturn = pdTRUE;
5280         }
5281     }
5282     taskEXIT_CRITICAL();
5283
5284     traceRETURN_xTaskCheckForTimeOut( xReturn );
5285
5286     return xReturn;
5287 }
5288 /*-----------------------------------------------------------*/
5289
5290 void vTaskMissedYield( void )
5291 {
5292     traceENTER_vTaskMissedYield();
5293
5294     /* Must be called from within a critical section. */
5295     xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
5296
5297     traceRETURN_vTaskMissedYield();
5298 }
5299 /*-----------------------------------------------------------*/
5300
5301 #if ( configUSE_TRACE_FACILITY == 1 )
5302
5303     UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
5304     {
5305         UBaseType_t uxReturn;
5306         TCB_t const * pxTCB;
5307
5308         traceENTER_uxTaskGetTaskNumber( xTask );
5309
5310         if( xTask != NULL )
5311         {
5312             pxTCB = xTask;
5313             uxReturn = pxTCB->uxTaskNumber;
5314         }
5315         else
5316         {
5317             uxReturn = 0U;
5318         }
5319
5320         traceRETURN_uxTaskGetTaskNumber( uxReturn );
5321
5322         return uxReturn;
5323     }
5324
5325 #endif /* configUSE_TRACE_FACILITY */
5326 /*-----------------------------------------------------------*/
5327
5328 #if ( configUSE_TRACE_FACILITY == 1 )
5329
5330     void vTaskSetTaskNumber( TaskHandle_t xTask,
5331                              const UBaseType_t uxHandle )
5332     {
5333         TCB_t * pxTCB;
5334
5335         traceENTER_vTaskSetTaskNumber( xTask, uxHandle );
5336
5337         if( xTask != NULL )
5338         {
5339             pxTCB = xTask;
5340             pxTCB->uxTaskNumber = uxHandle;
5341         }
5342
5343         traceRETURN_vTaskSetTaskNumber();
5344     }
5345
5346 #endif /* configUSE_TRACE_FACILITY */
5347 /*-----------------------------------------------------------*/
5348
5349 /*
5350  * -----------------------------------------------------------
5351  * The MinimalIdle task.
5352  * ----------------------------------------------------------
5353  *
5354  * The minimal idle task is used for all the additional cores in a SMP
5355  * system. There must be only 1 idle task and the rest are minimal idle
5356  * tasks.
5357  *
5358  * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5359  * language extensions.  The equivalent prototype for this function is:
5360  *
5361  * void prvMinimalIdleTask( void *pvParameters );
5362  */
5363
5364 #if ( configNUMBER_OF_CORES > 1 )
5365     static portTASK_FUNCTION( prvMinimalIdleTask, pvParameters )
5366     {
5367         ( void ) pvParameters;
5368
5369         taskYIELD();
5370
5371         for( ; configCONTROL_INFINITE_LOOP(); )
5372         {
5373             #if ( configUSE_PREEMPTION == 0 )
5374             {
5375                 /* If we are not using preemption we keep forcing a task switch to
5376                  * see if any other task has become available.  If we are using
5377                  * preemption we don't need to do this as any task becoming available
5378                  * will automatically get the processor anyway. */
5379                 taskYIELD();
5380             }
5381             #endif /* configUSE_PREEMPTION */
5382
5383             #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5384             {
5385                 /* When using preemption tasks of equal priority will be
5386                  * timesliced.  If a task that is sharing the idle priority is ready
5387                  * to run then the idle task should yield before the end of the
5388                  * timeslice.
5389                  *
5390                  * A critical region is not required here as we are just reading from
5391                  * the list, and an occasional incorrect value will not matter.  If
5392                  * the ready list at the idle priority contains one more task than the
5393                  * number of idle tasks, which is equal to the configured numbers of cores
5394                  * then a task other than the idle task is ready to execute. */
5395                 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5396                 {
5397                     taskYIELD();
5398                 }
5399                 else
5400                 {
5401                     mtCOVERAGE_TEST_MARKER();
5402                 }
5403             }
5404             #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5405
5406             #if ( configUSE_MINIMAL_IDLE_HOOK == 1 )
5407             {
5408                 /* Call the user defined function from within the idle task.  This
5409                  * allows the application designer to add background functionality
5410                  * without the overhead of a separate task.
5411                  *
5412                  * This hook is intended to manage core activity such as disabling cores that go idle.
5413                  *
5414                  * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5415                  * CALL A FUNCTION THAT MIGHT BLOCK. */
5416                 vApplicationMinimalIdleHook();
5417             }
5418             #endif /* configUSE_MINIMAL_IDLE_HOOK */
5419         }
5420     }
5421 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5422
5423 /*
5424  * -----------------------------------------------------------
5425  * The Idle task.
5426  * ----------------------------------------------------------
5427  *
5428  * The portTASK_FUNCTION() macro is used to allow port/compiler specific
5429  * language extensions.  The equivalent prototype for this function is:
5430  *
5431  * void prvIdleTask( void *pvParameters );
5432  *
5433  */
5434
5435 static portTASK_FUNCTION( prvIdleTask, pvParameters )
5436 {
5437     /* Stop warnings. */
5438     ( void ) pvParameters;
5439
5440     /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
5441      * SCHEDULER IS STARTED. **/
5442
5443     /* In case a task that has a secure context deletes itself, in which case
5444      * the idle task is responsible for deleting the task's secure context, if
5445      * any. */
5446     portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
5447
5448     #if ( configNUMBER_OF_CORES > 1 )
5449     {
5450         /* SMP all cores start up in the idle task. This initial yield gets the application
5451          * tasks started. */
5452         taskYIELD();
5453     }
5454     #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
5455
5456     for( ; configCONTROL_INFINITE_LOOP(); )
5457     {
5458         /* See if any tasks have deleted themselves - if so then the idle task
5459          * is responsible for freeing the deleted task's TCB and stack. */
5460         prvCheckTasksWaitingTermination();
5461
5462         #if ( configUSE_PREEMPTION == 0 )
5463         {
5464             /* If we are not using preemption we keep forcing a task switch to
5465              * see if any other task has become available.  If we are using
5466              * preemption we don't need to do this as any task becoming available
5467              * will automatically get the processor anyway. */
5468             taskYIELD();
5469         }
5470         #endif /* configUSE_PREEMPTION */
5471
5472         #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
5473         {
5474             /* When using preemption tasks of equal priority will be
5475              * timesliced.  If a task that is sharing the idle priority is ready
5476              * to run then the idle task should yield before the end of the
5477              * timeslice.
5478              *
5479              * A critical region is not required here as we are just reading from
5480              * the list, and an occasional incorrect value will not matter.  If
5481              * the ready list at the idle priority contains one more task than the
5482              * number of idle tasks, which is equal to the configured numbers of cores
5483              * then a task other than the idle task is ready to execute. */
5484             if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) configNUMBER_OF_CORES )
5485             {
5486                 taskYIELD();
5487             }
5488             else
5489             {
5490                 mtCOVERAGE_TEST_MARKER();
5491             }
5492         }
5493         #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
5494
5495         #if ( configUSE_IDLE_HOOK == 1 )
5496         {
5497             /* Call the user defined function from within the idle task. */
5498             vApplicationIdleHook();
5499         }
5500         #endif /* configUSE_IDLE_HOOK */
5501
5502         /* This conditional compilation should use inequality to 0, not equality
5503          * to 1.  This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
5504          * user defined low power mode  implementations require
5505          * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
5506         #if ( configUSE_TICKLESS_IDLE != 0 )
5507         {
5508             TickType_t xExpectedIdleTime;
5509
5510             /* It is not desirable to suspend then resume the scheduler on
5511              * each iteration of the idle task.  Therefore, a preliminary
5512              * test of the expected idle time is performed without the
5513              * scheduler suspended.  The result here is not necessarily
5514              * valid. */
5515             xExpectedIdleTime = prvGetExpectedIdleTime();
5516
5517             if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5518             {
5519                 vTaskSuspendAll();
5520                 {
5521                     /* Now the scheduler is suspended, the expected idle
5522                      * time can be sampled again, and this time its value can
5523                      * be used. */
5524                     configASSERT( xNextTaskUnblockTime >= xTickCount );
5525                     xExpectedIdleTime = prvGetExpectedIdleTime();
5526
5527                     /* Define the following macro to set xExpectedIdleTime to 0
5528                      * if the application does not want
5529                      * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
5530                     configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
5531
5532                     if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
5533                     {
5534                         traceLOW_POWER_IDLE_BEGIN();
5535                         portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
5536                         traceLOW_POWER_IDLE_END();
5537                     }
5538                     else
5539                     {
5540                         mtCOVERAGE_TEST_MARKER();
5541                     }
5542                 }
5543                 ( void ) xTaskResumeAll();
5544             }
5545             else
5546             {
5547                 mtCOVERAGE_TEST_MARKER();
5548             }
5549         }
5550         #endif /* configUSE_TICKLESS_IDLE */
5551
5552         #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_MINIMAL_IDLE_HOOK == 1 ) )
5553         {
5554             /* Call the user defined function from within the idle task.  This
5555              * allows the application designer to add background functionality
5556              * without the overhead of a separate task.
5557              *
5558              * This hook is intended to manage core activity such as disabling cores that go idle.
5559              *
5560              * NOTE: vApplicationMinimalIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
5561              * CALL A FUNCTION THAT MIGHT BLOCK. */
5562             vApplicationMinimalIdleHook();
5563         }
5564         #endif /* #if ( ( configNUMBER_OF_CORES > 1 ) && ( configUSE_MINIMAL_IDLE_HOOK == 1 ) ) */
5565     }
5566 }
5567 /*-----------------------------------------------------------*/
5568
5569 #if ( configUSE_TICKLESS_IDLE != 0 )
5570
5571     eSleepModeStatus eTaskConfirmSleepModeStatus( void )
5572     {
5573         #if ( INCLUDE_vTaskSuspend == 1 )
5574             /* The idle task exists in addition to the application tasks. */
5575             const UBaseType_t uxNonApplicationTasks = 1;
5576         #endif /* INCLUDE_vTaskSuspend */
5577
5578         eSleepModeStatus eReturn = eStandardSleep;
5579
5580         traceENTER_eTaskConfirmSleepModeStatus();
5581
5582         /* This function must be called from a critical section. */
5583
5584         if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
5585         {
5586             /* A task was made ready while the scheduler was suspended. */
5587             eReturn = eAbortSleep;
5588         }
5589         else if( xYieldPendings[ portGET_CORE_ID() ] != pdFALSE )
5590         {
5591             /* A yield was pended while the scheduler was suspended. */
5592             eReturn = eAbortSleep;
5593         }
5594         else if( xPendedTicks != 0 )
5595         {
5596             /* A tick interrupt has already occurred but was held pending
5597              * because the scheduler is suspended. */
5598             eReturn = eAbortSleep;
5599         }
5600
5601         #if ( INCLUDE_vTaskSuspend == 1 )
5602             else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
5603             {
5604                 /* If all the tasks are in the suspended list (which might mean they
5605                  * have an infinite block time rather than actually being suspended)
5606                  * then it is safe to turn all clocks off and just wait for external
5607                  * interrupts. */
5608                 eReturn = eNoTasksWaitingTimeout;
5609             }
5610         #endif /* INCLUDE_vTaskSuspend */
5611         else
5612         {
5613             mtCOVERAGE_TEST_MARKER();
5614         }
5615
5616         traceRETURN_eTaskConfirmSleepModeStatus( eReturn );
5617
5618         return eReturn;
5619     }
5620
5621 #endif /* configUSE_TICKLESS_IDLE */
5622 /*-----------------------------------------------------------*/
5623
5624 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5625
5626     void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
5627                                             BaseType_t xIndex,
5628                                             void * pvValue )
5629     {
5630         TCB_t * pxTCB;
5631
5632         traceENTER_vTaskSetThreadLocalStoragePointer( xTaskToSet, xIndex, pvValue );
5633
5634         if( ( xIndex >= 0 ) &&
5635             ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5636         {
5637             pxTCB = prvGetTCBFromHandle( xTaskToSet );
5638             configASSERT( pxTCB != NULL );
5639             pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
5640         }
5641
5642         traceRETURN_vTaskSetThreadLocalStoragePointer();
5643     }
5644
5645 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5646 /*-----------------------------------------------------------*/
5647
5648 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
5649
5650     void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
5651                                                BaseType_t xIndex )
5652     {
5653         void * pvReturn = NULL;
5654         TCB_t * pxTCB;
5655
5656         traceENTER_pvTaskGetThreadLocalStoragePointer( xTaskToQuery, xIndex );
5657
5658         if( ( xIndex >= 0 ) &&
5659             ( xIndex < ( BaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
5660         {
5661             pxTCB = prvGetTCBFromHandle( xTaskToQuery );
5662             pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
5663         }
5664         else
5665         {
5666             pvReturn = NULL;
5667         }
5668
5669         traceRETURN_pvTaskGetThreadLocalStoragePointer( pvReturn );
5670
5671         return pvReturn;
5672     }
5673
5674 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
5675 /*-----------------------------------------------------------*/
5676
5677 #if ( portUSING_MPU_WRAPPERS == 1 )
5678
5679     void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
5680                                   const MemoryRegion_t * const pxRegions )
5681     {
5682         TCB_t * pxTCB;
5683
5684         traceENTER_vTaskAllocateMPURegions( xTaskToModify, pxRegions );
5685
5686         /* If null is passed in here then we are modifying the MPU settings of
5687          * the calling task. */
5688         pxTCB = prvGetTCBFromHandle( xTaskToModify );
5689
5690         vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), pxRegions, NULL, 0 );
5691
5692         traceRETURN_vTaskAllocateMPURegions();
5693     }
5694
5695 #endif /* portUSING_MPU_WRAPPERS */
5696 /*-----------------------------------------------------------*/
5697
5698 static void prvInitialiseTaskLists( void )
5699 {
5700     UBaseType_t uxPriority;
5701
5702     for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
5703     {
5704         vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
5705     }
5706
5707     vListInitialise( &xDelayedTaskList1 );
5708     vListInitialise( &xDelayedTaskList2 );
5709     vListInitialise( &xPendingReadyList );
5710
5711     #if ( INCLUDE_vTaskDelete == 1 )
5712     {
5713         vListInitialise( &xTasksWaitingTermination );
5714     }
5715     #endif /* INCLUDE_vTaskDelete */
5716
5717     #if ( INCLUDE_vTaskSuspend == 1 )
5718     {
5719         vListInitialise( &xSuspendedTaskList );
5720     }
5721     #endif /* INCLUDE_vTaskSuspend */
5722
5723     /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
5724      * using list2. */
5725     pxDelayedTaskList = &xDelayedTaskList1;
5726     pxOverflowDelayedTaskList = &xDelayedTaskList2;
5727 }
5728 /*-----------------------------------------------------------*/
5729
5730 static void prvCheckTasksWaitingTermination( void )
5731 {
5732     /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
5733
5734     #if ( INCLUDE_vTaskDelete == 1 )
5735     {
5736         TCB_t * pxTCB;
5737
5738         /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
5739          * being called too often in the idle task. */
5740         while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
5741         {
5742             #if ( configNUMBER_OF_CORES == 1 )
5743             {
5744                 taskENTER_CRITICAL();
5745                 {
5746                     {
5747                         pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*lint !e9079 void * is used as this macro is used with timers too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
5748                         ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5749                         --uxCurrentNumberOfTasks;
5750                         --uxDeletedTasksWaitingCleanUp;
5751                     }
5752                 }
5753                 taskEXIT_CRITICAL();
5754
5755                 prvDeleteTCB( pxTCB );
5756             }
5757             #else /* #if( configNUMBER_OF_CORES == 1 ) */
5758             {
5759                 pxTCB = NULL;
5760
5761                 taskENTER_CRITICAL();
5762                 {
5763                     /* For SMP, multiple idles can be running simultaneously
5764                      * and we need to check that other idles did not cleanup while we were
5765                      * waiting to enter the critical section. */
5766                     if( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
5767                     {
5768                         pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*lint !e9079 void * is used as this macro is used with timers too.  Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
5769
5770                         if( pxTCB->xTaskRunState == taskTASK_NOT_RUNNING )
5771                         {
5772                             ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
5773                             --uxCurrentNumberOfTasks;
5774                             --uxDeletedTasksWaitingCleanUp;
5775                         }
5776                         else
5777                         {
5778                             /* The TCB to be deleted still has not yet been switched out
5779                              * by the scheduler, so we will just exit this loop early and
5780                              * try again next time. */
5781                             taskEXIT_CRITICAL();
5782                             break;
5783                         }
5784                     }
5785                 }
5786                 taskEXIT_CRITICAL();
5787
5788                 if( pxTCB != NULL )
5789                 {
5790                     prvDeleteTCB( pxTCB );
5791                 }
5792             }
5793             #endif /* #if( configNUMBER_OF_CORES == 1 ) */
5794         }
5795     }
5796     #endif /* INCLUDE_vTaskDelete */
5797 }
5798 /*-----------------------------------------------------------*/
5799
5800 #if ( configUSE_TRACE_FACILITY == 1 )
5801
5802     void vTaskGetInfo( TaskHandle_t xTask,
5803                        TaskStatus_t * pxTaskStatus,
5804                        BaseType_t xGetFreeStackSpace,
5805                        eTaskState eState )
5806     {
5807         TCB_t * pxTCB;
5808
5809         traceENTER_vTaskGetInfo( xTask, pxTaskStatus, xGetFreeStackSpace, eState );
5810
5811         /* xTask is NULL then get the state of the calling task. */
5812         pxTCB = prvGetTCBFromHandle( xTask );
5813
5814         pxTaskStatus->xHandle = pxTCB;
5815         pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
5816         pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
5817         pxTaskStatus->pxStackBase = pxTCB->pxStack;
5818         #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
5819             pxTaskStatus->pxTopOfStack = ( StackType_t * ) pxTCB->pxTopOfStack;
5820             pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
5821         #endif
5822         pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
5823
5824         #if ( ( configUSE_CORE_AFFINITY == 1 ) && ( configNUMBER_OF_CORES > 1 ) )
5825         {
5826             pxTaskStatus->uxCoreAffinityMask = pxTCB->uxCoreAffinityMask;
5827         }
5828         #endif
5829
5830         #if ( configUSE_MUTEXES == 1 )
5831         {
5832             pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
5833         }
5834         #else
5835         {
5836             pxTaskStatus->uxBasePriority = 0;
5837         }
5838         #endif
5839
5840         #if ( configGENERATE_RUN_TIME_STATS == 1 )
5841         {
5842             pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
5843         }
5844         #else
5845         {
5846             pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
5847         }
5848         #endif
5849
5850         /* Obtaining the task state is a little fiddly, so is only done if the
5851          * value of eState passed into this function is eInvalid - otherwise the
5852          * state is just set to whatever is passed in. */
5853         if( eState != eInvalid )
5854         {
5855             if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
5856             {
5857                 pxTaskStatus->eCurrentState = eRunning;
5858             }
5859             else
5860             {
5861                 pxTaskStatus->eCurrentState = eState;
5862
5863                 #if ( INCLUDE_vTaskSuspend == 1 )
5864                 {
5865                     /* If the task is in the suspended list then there is a
5866                      *  chance it is actually just blocked indefinitely - so really
5867                      *  it should be reported as being in the Blocked state. */
5868                     if( eState == eSuspended )
5869                     {
5870                         vTaskSuspendAll();
5871                         {
5872                             if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
5873                             {
5874                                 pxTaskStatus->eCurrentState = eBlocked;
5875                             }
5876                         }
5877                         ( void ) xTaskResumeAll();
5878                     }
5879                 }
5880                 #endif /* INCLUDE_vTaskSuspend */
5881
5882                 /* Tasks can be in pending ready list and other state list at the
5883                  * same time. These tasks are in ready state no matter what state
5884                  * list the task is in. */
5885                 taskENTER_CRITICAL();
5886                 {
5887                     if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) != pdFALSE )
5888                     {
5889                         pxTaskStatus->eCurrentState = eReady;
5890                     }
5891                 }
5892                 taskEXIT_CRITICAL();
5893             }
5894         }
5895         else
5896         {
5897             pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
5898         }
5899
5900         /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
5901          * parameter is provided to allow it to be skipped. */
5902         if( xGetFreeStackSpace != pdFALSE )
5903         {
5904             #if ( portSTACK_GROWTH > 0 )
5905             {
5906                 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
5907             }
5908             #else
5909             {
5910                 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
5911             }
5912             #endif
5913         }
5914         else
5915         {
5916             pxTaskStatus->usStackHighWaterMark = 0;
5917         }
5918
5919         traceRETURN_vTaskGetInfo();
5920     }
5921
5922 #endif /* configUSE_TRACE_FACILITY */
5923 /*-----------------------------------------------------------*/
5924
5925 #if ( configUSE_TRACE_FACILITY == 1 )
5926
5927     static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
5928                                                      List_t * pxList,
5929                                                      eTaskState eState )
5930     {
5931         configLIST_VOLATILE TCB_t * pxNextTCB;
5932         configLIST_VOLATILE TCB_t * pxFirstTCB;
5933         UBaseType_t uxTask = 0;
5934
5935         if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
5936         {
5937             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. */
5938
5939             /* Populate an TaskStatus_t structure within the
5940              * pxTaskStatusArray array for each task that is referenced from
5941              * pxList.  See the definition of TaskStatus_t in task.h for the
5942              * meaning of each TaskStatus_t structure member. */
5943             do
5944             {
5945                 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. */
5946                 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
5947                 uxTask++;
5948             } while( pxNextTCB != pxFirstTCB );
5949         }
5950         else
5951         {
5952             mtCOVERAGE_TEST_MARKER();
5953         }
5954
5955         return uxTask;
5956     }
5957
5958 #endif /* configUSE_TRACE_FACILITY */
5959 /*-----------------------------------------------------------*/
5960
5961 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
5962
5963     static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
5964     {
5965         uint32_t ulCount = 0U;
5966
5967         while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
5968         {
5969             pucStackByte -= portSTACK_GROWTH;
5970             ulCount++;
5971         }
5972
5973         ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
5974
5975         return ( configSTACK_DEPTH_TYPE ) ulCount;
5976     }
5977
5978 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
5979 /*-----------------------------------------------------------*/
5980
5981 #if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
5982
5983 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
5984  * same except for their return type.  Using configSTACK_DEPTH_TYPE allows the
5985  * user to determine the return type.  It gets around the problem of the value
5986  * overflowing on 8-bit types without breaking backward compatibility for
5987  * applications that expect an 8-bit return type. */
5988     configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
5989     {
5990         TCB_t * pxTCB;
5991         uint8_t * pucEndOfStack;
5992         configSTACK_DEPTH_TYPE uxReturn;
5993
5994         traceENTER_uxTaskGetStackHighWaterMark2( xTask );
5995
5996         /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
5997          * the same except for their return type.  Using configSTACK_DEPTH_TYPE
5998          * allows the user to determine the return type.  It gets around the
5999          * problem of the value overflowing on 8-bit types without breaking
6000          * backward compatibility for applications that expect an 8-bit return
6001          * type. */
6002
6003         pxTCB = prvGetTCBFromHandle( xTask );
6004
6005         #if portSTACK_GROWTH < 0
6006         {
6007             pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6008         }
6009         #else
6010         {
6011             pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6012         }
6013         #endif
6014
6015         uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
6016
6017         traceRETURN_uxTaskGetStackHighWaterMark2( uxReturn );
6018
6019         return uxReturn;
6020     }
6021
6022 #endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
6023 /*-----------------------------------------------------------*/
6024
6025 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
6026
6027     UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
6028     {
6029         TCB_t * pxTCB;
6030         uint8_t * pucEndOfStack;
6031         UBaseType_t uxReturn;
6032
6033         traceENTER_uxTaskGetStackHighWaterMark( xTask );
6034
6035         pxTCB = prvGetTCBFromHandle( xTask );
6036
6037         #if portSTACK_GROWTH < 0
6038         {
6039             pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
6040         }
6041         #else
6042         {
6043             pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
6044         }
6045         #endif
6046
6047         uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
6048
6049         traceRETURN_uxTaskGetStackHighWaterMark( uxReturn );
6050
6051         return uxReturn;
6052     }
6053
6054 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
6055 /*-----------------------------------------------------------*/
6056
6057 #if ( INCLUDE_vTaskDelete == 1 )
6058
6059     static void prvDeleteTCB( TCB_t * pxTCB )
6060     {
6061         /* This call is required specifically for the TriCore port.  It must be
6062          * above the vPortFree() calls.  The call is also used by ports/demos that
6063          * want to allocate and clean RAM statically. */
6064         portCLEAN_UP_TCB( pxTCB );
6065
6066         #if ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 )
6067         {
6068             /* Free up the memory allocated for the task's TLS Block. */
6069             configDEINIT_TLS_BLOCK( pxTCB->xTLSBlock );
6070         }
6071         #endif
6072
6073         #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
6074         {
6075             /* The task can only have been allocated dynamically - free both
6076              * the stack and TCB. */
6077             vPortFreeStack( pxTCB->pxStack );
6078             vPortFree( pxTCB );
6079         }
6080         #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
6081         {
6082             /* The task could have been allocated statically or dynamically, so
6083              * check what was statically allocated before trying to free the
6084              * memory. */
6085             if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
6086             {
6087                 /* Both the stack and TCB were allocated dynamically, so both
6088                  * must be freed. */
6089                 vPortFreeStack( pxTCB->pxStack );
6090                 vPortFree( pxTCB );
6091             }
6092             else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
6093             {
6094                 /* Only the stack was statically allocated, so the TCB is the
6095                  * only memory that must be freed. */
6096                 vPortFree( pxTCB );
6097             }
6098             else
6099             {
6100                 /* Neither the stack nor the TCB were allocated dynamically, so
6101                  * nothing needs to be freed. */
6102                 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
6103                 mtCOVERAGE_TEST_MARKER();
6104             }
6105         }
6106         #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
6107     }
6108
6109 #endif /* INCLUDE_vTaskDelete */
6110 /*-----------------------------------------------------------*/
6111
6112 static void prvResetNextTaskUnblockTime( void )
6113 {
6114     if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
6115     {
6116         /* The new current delayed list is empty.  Set xNextTaskUnblockTime to
6117          * the maximum possible value so it is  extremely unlikely that the
6118          * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
6119          * there is an item in the delayed list. */
6120         xNextTaskUnblockTime = portMAX_DELAY;
6121     }
6122     else
6123     {
6124         /* The new current delayed list is not empty, get the value of
6125          * the item at the head of the delayed list.  This is the time at
6126          * which the task at the head of the delayed list should be removed
6127          * from the Blocked state. */
6128         xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
6129     }
6130 }
6131 /*-----------------------------------------------------------*/
6132
6133 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) || ( configNUMBER_OF_CORES > 1 )
6134
6135     #if ( configNUMBER_OF_CORES == 1 )
6136         TaskHandle_t xTaskGetCurrentTaskHandle( void )
6137         {
6138             TaskHandle_t xReturn;
6139
6140             traceENTER_xTaskGetCurrentTaskHandle();
6141
6142             /* A critical section is not required as this is not called from
6143              * an interrupt and the current TCB will always be the same for any
6144              * individual execution thread. */
6145             xReturn = pxCurrentTCB;
6146
6147             traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6148
6149             return xReturn;
6150         }
6151     #else /* #if ( configNUMBER_OF_CORES == 1 ) */
6152         TaskHandle_t xTaskGetCurrentTaskHandle( void )
6153         {
6154             TaskHandle_t xReturn;
6155             UBaseType_t uxSavedInterruptStatus;
6156
6157             traceENTER_xTaskGetCurrentTaskHandle();
6158
6159             uxSavedInterruptStatus = portSET_INTERRUPT_MASK();
6160             {
6161                 xReturn = pxCurrentTCBs[ portGET_CORE_ID() ];
6162             }
6163             portCLEAR_INTERRUPT_MASK( uxSavedInterruptStatus );
6164
6165             traceRETURN_xTaskGetCurrentTaskHandle( xReturn );
6166
6167             return xReturn;
6168         }
6169
6170         TaskHandle_t xTaskGetCurrentTaskHandleCPU( BaseType_t xCoreID )
6171         {
6172             TaskHandle_t xReturn = NULL;
6173
6174             traceENTER_xTaskGetCurrentTaskHandleCPU( xCoreID );
6175
6176             if( taskVALID_CORE_ID( xCoreID ) != pdFALSE )
6177             {
6178                 xReturn = pxCurrentTCBs[ xCoreID ];
6179             }
6180
6181             traceRETURN_xTaskGetCurrentTaskHandleCPU( xReturn );
6182
6183             return xReturn;
6184         }
6185     #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
6186
6187 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
6188 /*-----------------------------------------------------------*/
6189
6190 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
6191
6192     BaseType_t xTaskGetSchedulerState( void )
6193     {
6194         BaseType_t xReturn;
6195
6196         traceENTER_xTaskGetSchedulerState();
6197
6198         if( xSchedulerRunning == pdFALSE )
6199         {
6200             xReturn = taskSCHEDULER_NOT_STARTED;
6201         }
6202         else
6203         {
6204             #if ( configNUMBER_OF_CORES > 1 )
6205                 taskENTER_CRITICAL();
6206             #endif
6207             {
6208                 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
6209                 {
6210                     xReturn = taskSCHEDULER_RUNNING;
6211                 }
6212                 else
6213                 {
6214                     xReturn = taskSCHEDULER_SUSPENDED;
6215                 }
6216             }
6217             #if ( configNUMBER_OF_CORES > 1 )
6218                 taskEXIT_CRITICAL();
6219             #endif
6220         }
6221
6222         traceRETURN_xTaskGetSchedulerState( xReturn );
6223
6224         return xReturn;
6225     }
6226
6227 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
6228 /*-----------------------------------------------------------*/
6229
6230 #if ( configUSE_MUTEXES == 1 )
6231
6232     BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
6233     {
6234         TCB_t * const pxMutexHolderTCB = pxMutexHolder;
6235         BaseType_t xReturn = pdFALSE;
6236
6237         traceENTER_xTaskPriorityInherit( pxMutexHolder );
6238
6239         /* If the mutex is taken by an interrupt, the mutex holder is NULL. Priority
6240          * inheritance is not applied in this scenario. */
6241         if( pxMutexHolder != NULL )
6242         {
6243             /* If the holder of the mutex has a priority below the priority of
6244              * the task attempting to obtain the mutex then it will temporarily
6245              * inherit the priority of the task attempting to obtain the mutex. */
6246             if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
6247             {
6248                 /* Adjust the mutex holder state to account for its new
6249                  * priority.  Only reset the event list item value if the value is
6250                  * not being used for anything else. */
6251                 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
6252                 {
6253                     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. */
6254                 }
6255                 else
6256                 {
6257                     mtCOVERAGE_TEST_MARKER();
6258                 }
6259
6260                 /* If the task being modified is in the ready state it will need
6261                  * to be moved into a new list. */
6262                 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
6263                 {
6264                     if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6265                     {
6266                         /* It is known that the task is in its ready list so
6267                          * there is no need to check again and the port level
6268                          * reset macro can be called directly. */
6269                         portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
6270                     }
6271                     else
6272                     {
6273                         mtCOVERAGE_TEST_MARKER();
6274                     }
6275
6276                     /* Inherit the priority before being moved into the new list. */
6277                     pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6278                     prvAddTaskToReadyList( pxMutexHolderTCB );
6279                     #if ( configNUMBER_OF_CORES > 1 )
6280                     {
6281                         /* The priority of the task is raised. Yield for this task
6282                          * if it is not running. */
6283                         if( taskTASK_IS_RUNNING( pxMutexHolderTCB ) != pdTRUE )
6284                         {
6285                             prvYieldForTask( pxMutexHolderTCB );
6286                         }
6287                     }
6288                     #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6289                 }
6290                 else
6291                 {
6292                     /* Just inherit the priority. */
6293                     pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
6294                 }
6295
6296                 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
6297
6298                 /* Inheritance occurred. */
6299                 xReturn = pdTRUE;
6300             }
6301             else
6302             {
6303                 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
6304                 {
6305                     /* The base priority of the mutex holder is lower than the
6306                      * priority of the task attempting to take the mutex, but the
6307                      * current priority of the mutex holder is not lower than the
6308                      * priority of the task attempting to take the mutex.
6309                      * Therefore the mutex holder must have already inherited a
6310                      * priority, but inheritance would have occurred if that had
6311                      * not been the case. */
6312                     xReturn = pdTRUE;
6313                 }
6314                 else
6315                 {
6316                     mtCOVERAGE_TEST_MARKER();
6317                 }
6318             }
6319         }
6320         else
6321         {
6322             mtCOVERAGE_TEST_MARKER();
6323         }
6324
6325         traceRETURN_xTaskPriorityInherit( xReturn );
6326
6327         return xReturn;
6328     }
6329
6330 #endif /* configUSE_MUTEXES */
6331 /*-----------------------------------------------------------*/
6332
6333 #if ( configUSE_MUTEXES == 1 )
6334
6335     BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
6336     {
6337         TCB_t * const pxTCB = pxMutexHolder;
6338         BaseType_t xReturn = pdFALSE;
6339
6340         traceENTER_xTaskPriorityDisinherit( pxMutexHolder );
6341
6342         if( pxMutexHolder != NULL )
6343         {
6344             /* A task can only have an inherited priority if it holds the mutex.
6345              * If the mutex is held by a task then it cannot be given from an
6346              * interrupt, and if a mutex is given by the holding task then it must
6347              * be the running state task. */
6348             configASSERT( pxTCB == pxCurrentTCB );
6349             configASSERT( pxTCB->uxMutexesHeld );
6350             ( pxTCB->uxMutexesHeld )--;
6351
6352             /* Has the holder of the mutex inherited the priority of another
6353              * task? */
6354             if( pxTCB->uxPriority != pxTCB->uxBasePriority )
6355             {
6356                 /* Only disinherit if no other mutexes are held. */
6357                 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
6358                 {
6359                     /* A task can only have an inherited priority if it holds
6360                      * the mutex.  If the mutex is held by a task then it cannot be
6361                      * given from an interrupt, and if a mutex is given by the
6362                      * holding task then it must be the running state task.  Remove
6363                      * the holding task from the ready list. */
6364                     if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6365                     {
6366                         portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6367                     }
6368                     else
6369                     {
6370                         mtCOVERAGE_TEST_MARKER();
6371                     }
6372
6373                     /* Disinherit the priority before adding the task into the
6374                      * new  ready list. */
6375                     traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
6376                     pxTCB->uxPriority = pxTCB->uxBasePriority;
6377
6378                     /* Reset the event list item value.  It cannot be in use for
6379                      * any other purpose if this task is running, and it must be
6380                      * running to give back the mutex. */
6381                     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. */
6382                     prvAddTaskToReadyList( pxTCB );
6383                     #if ( configNUMBER_OF_CORES > 1 )
6384                     {
6385                         /* The priority of the task is dropped. Yield the core on
6386                          * which the task is running. */
6387                         if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6388                         {
6389                             prvYieldCore( pxTCB->xTaskRunState );
6390                         }
6391                     }
6392                     #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6393
6394                     /* Return true to indicate that a context switch is required.
6395                      * This is only actually required in the corner case whereby
6396                      * multiple mutexes were held and the mutexes were given back
6397                      * in an order different to that in which they were taken.
6398                      * If a context switch did not occur when the first mutex was
6399                      * returned, even if a task was waiting on it, then a context
6400                      * switch should occur when the last mutex is returned whether
6401                      * a task is waiting on it or not. */
6402                     xReturn = pdTRUE;
6403                 }
6404                 else
6405                 {
6406                     mtCOVERAGE_TEST_MARKER();
6407                 }
6408             }
6409             else
6410             {
6411                 mtCOVERAGE_TEST_MARKER();
6412             }
6413         }
6414         else
6415         {
6416             mtCOVERAGE_TEST_MARKER();
6417         }
6418
6419         traceRETURN_xTaskPriorityDisinherit( xReturn );
6420
6421         return xReturn;
6422     }
6423
6424 #endif /* configUSE_MUTEXES */
6425 /*-----------------------------------------------------------*/
6426
6427 #if ( configUSE_MUTEXES == 1 )
6428
6429     void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
6430                                               UBaseType_t uxHighestPriorityWaitingTask )
6431     {
6432         TCB_t * const pxTCB = pxMutexHolder;
6433         UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
6434         const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
6435
6436         traceENTER_vTaskPriorityDisinheritAfterTimeout( pxMutexHolder, uxHighestPriorityWaitingTask );
6437
6438         if( pxMutexHolder != NULL )
6439         {
6440             /* If pxMutexHolder is not NULL then the holder must hold at least
6441              * one mutex. */
6442             configASSERT( pxTCB->uxMutexesHeld );
6443
6444             /* Determine the priority to which the priority of the task that
6445              * holds the mutex should be set.  This will be the greater of the
6446              * holding task's base priority and the priority of the highest
6447              * priority task that is waiting to obtain the mutex. */
6448             if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
6449             {
6450                 uxPriorityToUse = uxHighestPriorityWaitingTask;
6451             }
6452             else
6453             {
6454                 uxPriorityToUse = pxTCB->uxBasePriority;
6455             }
6456
6457             /* Does the priority need to change? */
6458             if( pxTCB->uxPriority != uxPriorityToUse )
6459             {
6460                 /* Only disinherit if no other mutexes are held.  This is a
6461                  * simplification in the priority inheritance implementation.  If
6462                  * the task that holds the mutex is also holding other mutexes then
6463                  * the other mutexes may have caused the priority inheritance. */
6464                 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
6465                 {
6466                     /* If a task has timed out because it already holds the
6467                      * mutex it was trying to obtain then it cannot of inherited
6468                      * its own priority. */
6469                     configASSERT( pxTCB != pxCurrentTCB );
6470
6471                     /* Disinherit the priority, remembering the previous
6472                      * priority to facilitate determining the subject task's
6473                      * state. */
6474                     traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
6475                     uxPriorityUsedOnEntry = pxTCB->uxPriority;
6476                     pxTCB->uxPriority = uxPriorityToUse;
6477
6478                     /* Only reset the event list item value if the value is not
6479                      * being used for anything else. */
6480                     if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
6481                     {
6482                         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. */
6483                     }
6484                     else
6485                     {
6486                         mtCOVERAGE_TEST_MARKER();
6487                     }
6488
6489                     /* If the running task is not the task that holds the mutex
6490                      * then the task that holds the mutex could be in either the
6491                      * Ready, Blocked or Suspended states.  Only remove the task
6492                      * from its current state list if it is in the Ready state as
6493                      * the task's priority is going to change and there is one
6494                      * Ready list per priority. */
6495                     if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
6496                     {
6497                         if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
6498                         {
6499                             /* It is known that the task is in its ready list so
6500                              * there is no need to check again and the port level
6501                              * reset macro can be called directly. */
6502                             portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
6503                         }
6504                         else
6505                         {
6506                             mtCOVERAGE_TEST_MARKER();
6507                         }
6508
6509                         prvAddTaskToReadyList( pxTCB );
6510                         #if ( configNUMBER_OF_CORES > 1 )
6511                         {
6512                             /* The priority of the task is dropped. Yield the core on
6513                              * which the task is running. */
6514                             if( taskTASK_IS_RUNNING( pxTCB ) == pdTRUE )
6515                             {
6516                                 prvYieldCore( pxTCB->xTaskRunState );
6517                             }
6518                         }
6519                         #endif /* if ( configNUMBER_OF_CORES > 1 ) */
6520                     }
6521                     else
6522                     {
6523                         mtCOVERAGE_TEST_MARKER();
6524                     }
6525                 }
6526                 else
6527                 {
6528                     mtCOVERAGE_TEST_MARKER();
6529                 }
6530             }
6531             else
6532             {
6533                 mtCOVERAGE_TEST_MARKER();
6534             }
6535         }
6536         else
6537         {
6538             mtCOVERAGE_TEST_MARKER();
6539         }
6540
6541         traceRETURN_vTaskPriorityDisinheritAfterTimeout();
6542     }
6543
6544 #endif /* configUSE_MUTEXES */
6545 /*-----------------------------------------------------------*/
6546
6547 #if ( configNUMBER_OF_CORES > 1 )
6548
6549 /* If not in a critical section then yield immediately.
6550  * Otherwise set xYieldPendings to true to wait to
6551  * yield until exiting the critical section.
6552  */
6553     void vTaskYieldWithinAPI( void )
6554     {
6555         traceENTER_vTaskYieldWithinAPI();
6556
6557         if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6558         {
6559             portYIELD();
6560         }
6561         else
6562         {
6563             xYieldPendings[ portGET_CORE_ID() ] = pdTRUE;
6564         }
6565
6566         traceRETURN_vTaskYieldWithinAPI();
6567     }
6568 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6569
6570 /*-----------------------------------------------------------*/
6571
6572 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6573
6574     void vTaskEnterCritical( void )
6575     {
6576         traceENTER_vTaskEnterCritical();
6577
6578         portDISABLE_INTERRUPTS();
6579
6580         if( xSchedulerRunning != pdFALSE )
6581         {
6582             ( pxCurrentTCB->uxCriticalNesting )++;
6583
6584             /* This is not the interrupt safe version of the enter critical
6585              * function so  assert() if it is being called from an interrupt
6586              * context.  Only API functions that end in "FromISR" can be used in an
6587              * interrupt.  Only assert if the critical nesting count is 1 to
6588              * protect against recursive calls if the assert function also uses a
6589              * critical section. */
6590             if( pxCurrentTCB->uxCriticalNesting == 1 )
6591             {
6592                 portASSERT_IF_IN_ISR();
6593             }
6594         }
6595         else
6596         {
6597             mtCOVERAGE_TEST_MARKER();
6598         }
6599
6600         traceRETURN_vTaskEnterCritical();
6601     }
6602
6603 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6604 /*-----------------------------------------------------------*/
6605
6606 #if ( configNUMBER_OF_CORES > 1 )
6607
6608     void vTaskEnterCritical( void )
6609     {
6610         traceENTER_vTaskEnterCritical();
6611
6612         portDISABLE_INTERRUPTS();
6613
6614         if( xSchedulerRunning != pdFALSE )
6615         {
6616             if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6617             {
6618                 portGET_TASK_LOCK();
6619                 portGET_ISR_LOCK();
6620             }
6621
6622             portINCREMENT_CRITICAL_NESTING_COUNT();
6623
6624             /* This is not the interrupt safe version of the enter critical
6625              * function so  assert() if it is being called from an interrupt
6626              * context.  Only API functions that end in "FromISR" can be used in an
6627              * interrupt.  Only assert if the critical nesting count is 1 to
6628              * protect against recursive calls if the assert function also uses a
6629              * critical section. */
6630             if( portGET_CRITICAL_NESTING_COUNT() == 1U )
6631             {
6632                 portASSERT_IF_IN_ISR();
6633
6634                 if( uxSchedulerSuspended == 0U )
6635                 {
6636                     /* The only time there would be a problem is if this is called
6637                      * before a context switch and vTaskExitCritical() is called
6638                      * after pxCurrentTCB changes. Therefore this should not be
6639                      * used within vTaskSwitchContext(). */
6640                     prvCheckForRunStateChange();
6641                 }
6642             }
6643         }
6644         else
6645         {
6646             mtCOVERAGE_TEST_MARKER();
6647         }
6648
6649         traceRETURN_vTaskEnterCritical();
6650     }
6651
6652 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6653
6654 /*-----------------------------------------------------------*/
6655
6656 #if ( configNUMBER_OF_CORES > 1 )
6657
6658     UBaseType_t vTaskEnterCriticalFromISR( void )
6659     {
6660         UBaseType_t uxSavedInterruptStatus = 0;
6661
6662         traceENTER_vTaskEnterCriticalFromISR();
6663
6664         if( xSchedulerRunning != pdFALSE )
6665         {
6666             uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
6667
6668             if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6669             {
6670                 portGET_ISR_LOCK();
6671             }
6672
6673             portINCREMENT_CRITICAL_NESTING_COUNT();
6674         }
6675         else
6676         {
6677             mtCOVERAGE_TEST_MARKER();
6678         }
6679
6680         traceRETURN_vTaskEnterCriticalFromISR( uxSavedInterruptStatus );
6681
6682         return uxSavedInterruptStatus;
6683     }
6684
6685 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6686 /*-----------------------------------------------------------*/
6687
6688 #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) )
6689
6690     void vTaskExitCritical( void )
6691     {
6692         traceENTER_vTaskExitCritical();
6693
6694         if( xSchedulerRunning != pdFALSE )
6695         {
6696             /* If pxCurrentTCB->uxCriticalNesting is zero then this function
6697              * does not match a previous call to vTaskEnterCritical(). */
6698             configASSERT( pxCurrentTCB->uxCriticalNesting > 0U );
6699
6700             /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
6701              * to exit critical section from ISR. */
6702             portASSERT_IF_IN_ISR();
6703
6704             if( pxCurrentTCB->uxCriticalNesting > 0U )
6705             {
6706                 ( pxCurrentTCB->uxCriticalNesting )--;
6707
6708                 if( pxCurrentTCB->uxCriticalNesting == 0U )
6709                 {
6710                     portENABLE_INTERRUPTS();
6711                 }
6712                 else
6713                 {
6714                     mtCOVERAGE_TEST_MARKER();
6715                 }
6716             }
6717             else
6718             {
6719                 mtCOVERAGE_TEST_MARKER();
6720             }
6721         }
6722         else
6723         {
6724             mtCOVERAGE_TEST_MARKER();
6725         }
6726
6727         traceRETURN_vTaskExitCritical();
6728     }
6729
6730 #endif /* #if ( ( portCRITICAL_NESTING_IN_TCB == 1 ) && ( configNUMBER_OF_CORES == 1 ) ) */
6731 /*-----------------------------------------------------------*/
6732
6733 #if ( configNUMBER_OF_CORES > 1 )
6734
6735     void vTaskExitCritical( void )
6736     {
6737         traceENTER_vTaskExitCritical();
6738
6739         if( xSchedulerRunning != pdFALSE )
6740         {
6741             /* If critical nesting count is zero then this function
6742              * does not match a previous call to vTaskEnterCritical(). */
6743             configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
6744
6745             /* This function should not be called in ISR. Use vTaskExitCriticalFromISR
6746              * to exit critical section from ISR. */
6747             portASSERT_IF_IN_ISR();
6748
6749             if( portGET_CRITICAL_NESTING_COUNT() > 0U )
6750             {
6751                 portDECREMENT_CRITICAL_NESTING_COUNT();
6752
6753                 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6754                 {
6755                     BaseType_t xYieldCurrentTask;
6756
6757                     /* Get the xYieldPending stats inside the critical section. */
6758                     xYieldCurrentTask = xYieldPendings[ portGET_CORE_ID() ];
6759
6760                     portRELEASE_ISR_LOCK();
6761                     portRELEASE_TASK_LOCK();
6762                     portENABLE_INTERRUPTS();
6763
6764                     /* When a task yields in a critical section it just sets
6765                      * xYieldPending to true. So now that we have exited the
6766                      * critical section check if xYieldPending is true, and
6767                      * if so yield. */
6768                     if( xYieldCurrentTask != pdFALSE )
6769                     {
6770                         portYIELD();
6771                     }
6772                 }
6773                 else
6774                 {
6775                     mtCOVERAGE_TEST_MARKER();
6776                 }
6777             }
6778             else
6779             {
6780                 mtCOVERAGE_TEST_MARKER();
6781             }
6782         }
6783         else
6784         {
6785             mtCOVERAGE_TEST_MARKER();
6786         }
6787
6788         traceRETURN_vTaskExitCritical();
6789     }
6790
6791 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6792 /*-----------------------------------------------------------*/
6793
6794 #if ( configNUMBER_OF_CORES > 1 )
6795
6796     void vTaskExitCriticalFromISR( UBaseType_t uxSavedInterruptStatus )
6797     {
6798         traceENTER_vTaskExitCriticalFromISR( uxSavedInterruptStatus );
6799
6800         if( xSchedulerRunning != pdFALSE )
6801         {
6802             /* If critical nesting count is zero then this function
6803              * does not match a previous call to vTaskEnterCritical(). */
6804             configASSERT( portGET_CRITICAL_NESTING_COUNT() > 0U );
6805
6806             if( portGET_CRITICAL_NESTING_COUNT() > 0U )
6807             {
6808                 portDECREMENT_CRITICAL_NESTING_COUNT();
6809
6810                 if( portGET_CRITICAL_NESTING_COUNT() == 0U )
6811                 {
6812                     portRELEASE_ISR_LOCK();
6813                     portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
6814                 }
6815                 else
6816                 {
6817                     mtCOVERAGE_TEST_MARKER();
6818                 }
6819             }
6820             else
6821             {
6822                 mtCOVERAGE_TEST_MARKER();
6823             }
6824         }
6825         else
6826         {
6827             mtCOVERAGE_TEST_MARKER();
6828         }
6829
6830         traceRETURN_vTaskExitCriticalFromISR();
6831     }
6832
6833 #endif /* #if ( configNUMBER_OF_CORES > 1 ) */
6834 /*-----------------------------------------------------------*/
6835
6836 #if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
6837
6838     static char * prvWriteNameToBuffer( char * pcBuffer,
6839                                         const char * pcTaskName )
6840     {
6841         size_t x;
6842
6843         /* Start by copying the entire string. */
6844         ( void ) strcpy( pcBuffer, pcTaskName );
6845
6846         /* Pad the end of the string with spaces to ensure columns line up when
6847          * printed out. */
6848         for( x = strlen( pcBuffer ); x < ( size_t ) ( ( size_t ) configMAX_TASK_NAME_LEN - 1U ); x++ )
6849         {
6850             pcBuffer[ x ] = ' ';
6851         }
6852
6853         /* Terminate. */
6854         pcBuffer[ x ] = ( char ) 0x00;
6855
6856         /* Return the new end of string. */
6857         return &( pcBuffer[ x ] );
6858     }
6859
6860 #endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
6861 /*-----------------------------------------------------------*/
6862
6863 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
6864
6865     void vTaskListTasks( char * pcWriteBuffer,
6866                          size_t uxBufferLength )
6867     {
6868         TaskStatus_t * pxTaskStatusArray;
6869         size_t uxConsumedBufferLength = 0;
6870         size_t uxCharsWrittenBySnprintf;
6871         int iSnprintfReturnValue;
6872         BaseType_t xOutputBufferFull = pdFALSE;
6873         UBaseType_t uxArraySize, x;
6874         char cStatus;
6875
6876         traceENTER_vTaskListTasks( pcWriteBuffer, uxBufferLength );
6877
6878         /*
6879          * PLEASE NOTE:
6880          *
6881          * This function is provided for convenience only, and is used by many
6882          * of the demo applications.  Do not consider it to be part of the
6883          * scheduler.
6884          *
6885          * vTaskListTasks() calls uxTaskGetSystemState(), then formats part of the
6886          * uxTaskGetSystemState() output into a human readable table that
6887          * displays task: names, states, priority, stack usage and task number.
6888          * Stack usage specified as the number of unused StackType_t words stack can hold
6889          * on top of stack - not the number of bytes.
6890          *
6891          * vTaskListTasks() has a dependency on the snprintf() C library function that
6892          * might bloat the code size, use a lot of stack, and provide different
6893          * results on different platforms.  An alternative, tiny, third party,
6894          * and limited functionality implementation of snprintf() is provided in
6895          * many of the FreeRTOS/Demo sub-directories in a file called
6896          * printf-stdarg.c (note printf-stdarg.c does not provide a full
6897          * snprintf() implementation!).
6898          *
6899          * It is recommended that production systems call uxTaskGetSystemState()
6900          * directly to get access to raw stats data, rather than indirectly
6901          * through a call to vTaskListTasks().
6902          */
6903
6904
6905         /* Make sure the write buffer does not contain a string. */
6906         *pcWriteBuffer = ( char ) 0x00;
6907
6908         /* Take a snapshot of the number of tasks in case it changes while this
6909          * function is executing. */
6910         uxArraySize = uxCurrentNumberOfTasks;
6911
6912         /* Allocate an array index for each task.  NOTE!  if
6913          * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
6914          * equate to NULL. */
6915         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. */
6916
6917         if( pxTaskStatusArray != NULL )
6918         {
6919             /* Generate the (binary) data. */
6920             uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
6921
6922             /* Create a human readable table from the binary data. */
6923             for( x = 0; ( x < uxArraySize ) && ( xOutputBufferFull == pdFALSE ); x++ )
6924             {
6925                 switch( pxTaskStatusArray[ x ].eCurrentState )
6926                 {
6927                     case eRunning:
6928                         cStatus = tskRUNNING_CHAR;
6929                         break;
6930
6931                     case eReady:
6932                         cStatus = tskREADY_CHAR;
6933                         break;
6934
6935                     case eBlocked:
6936                         cStatus = tskBLOCKED_CHAR;
6937                         break;
6938
6939                     case eSuspended:
6940                         cStatus = tskSUSPENDED_CHAR;
6941                         break;
6942
6943                     case eDeleted:
6944                         cStatus = tskDELETED_CHAR;
6945                         break;
6946
6947                     case eInvalid: /* Fall through. */
6948                     default:       /* Should not get here, but it is included
6949                                     * to prevent static checking errors. */
6950                         cStatus = ( char ) 0x00;
6951                         break;
6952                 }
6953
6954                 /* Is there enough space in the buffer to hold task name? */
6955                 if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
6956                 {
6957                     /* Write the task name to the string, padding with spaces so it
6958                      * can be printed in tabular form more easily. */
6959                     pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
6960                     /* Do not count the terminating null character. */
6961                     uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1 );
6962
6963                     /* Is there space left in the buffer? -1 is done because snprintf
6964                      * writes a terminating null character. So we are essentially
6965                      * checking if the buffer has space to write at least one non-null
6966                      * character. */
6967                     if( uxConsumedBufferLength < ( uxBufferLength - 1 ) )
6968                     {
6969                         /* Write the rest of the string. */
6970                         iSnprintfReturnValue = snprintf( pcWriteBuffer,
6971                                                          uxBufferLength - uxConsumedBufferLength,
6972                                                          "\t%c\t%u\t%u\t%u\r\n",
6973                                                          cStatus,
6974                                                          ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority,
6975                                                          ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark,
6976                                                          ( 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. */
6977                         uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
6978
6979                         uxConsumedBufferLength += uxCharsWrittenBySnprintf;
6980                         pcWriteBuffer += uxCharsWrittenBySnprintf; /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
6981                     }
6982                     else
6983                     {
6984                         xOutputBufferFull = pdTRUE;
6985                     }
6986                 }
6987                 else
6988                 {
6989                     xOutputBufferFull = pdTRUE;
6990                 }
6991             }
6992
6993             /* Free the array again.  NOTE!  If configSUPPORT_DYNAMIC_ALLOCATION
6994              * is 0 then vPortFree() will be #defined to nothing. */
6995             vPortFree( pxTaskStatusArray );
6996         }
6997         else
6998         {
6999             mtCOVERAGE_TEST_MARKER();
7000         }
7001
7002         traceRETURN_vTaskListTasks();
7003     }
7004
7005 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7006 /*----------------------------------------------------------*/
7007
7008 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
7009
7010     void vTaskGetRunTimeStatistics( char * pcWriteBuffer,
7011                                     size_t uxBufferLength )
7012     {
7013         TaskStatus_t * pxTaskStatusArray;
7014         size_t uxConsumedBufferLength = 0;
7015         size_t uxCharsWrittenBySnprintf;
7016         int iSnprintfReturnValue;
7017         BaseType_t xOutputBufferFull = pdFALSE;
7018         UBaseType_t uxArraySize, x;
7019         configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
7020
7021         traceENTER_vTaskGetRunTimeStatistics( pcWriteBuffer, uxBufferLength );
7022
7023         /*
7024          * PLEASE NOTE:
7025          *
7026          * This function is provided for convenience only, and is used by many
7027          * of the demo applications.  Do not consider it to be part of the
7028          * scheduler.
7029          *
7030          * vTaskGetRunTimeStatistics() calls uxTaskGetSystemState(), then formats part
7031          * of the uxTaskGetSystemState() output into a human readable table that
7032          * displays the amount of time each task has spent in the Running state
7033          * in both absolute and percentage terms.
7034          *
7035          * vTaskGetRunTimeStatistics() has a dependency on the snprintf() C library
7036          * function that might bloat the code size, use a lot of stack, and
7037          * provide different results on different platforms.  An alternative,
7038          * tiny, third party, and limited functionality implementation of
7039          * snprintf() is provided in many of the FreeRTOS/Demo sub-directories in
7040          * a file called printf-stdarg.c (note printf-stdarg.c does not provide
7041          * a full snprintf() implementation!).
7042          *
7043          * It is recommended that production systems call uxTaskGetSystemState()
7044          * directly to get access to raw stats data, rather than indirectly
7045          * through a call to vTaskGetRunTimeStatistics().
7046          */
7047
7048         /* Make sure the write buffer does not contain a string. */
7049         *pcWriteBuffer = ( char ) 0x00;
7050
7051         /* Take a snapshot of the number of tasks in case it changes while this
7052          * function is executing. */
7053         uxArraySize = uxCurrentNumberOfTasks;
7054
7055         /* Allocate an array index for each task.  NOTE!  If
7056          * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
7057          * equate to NULL. */
7058         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. */
7059
7060         if( pxTaskStatusArray != NULL )
7061         {
7062             /* Generate the (binary) data. */
7063             uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
7064
7065             /* For percentage calculations. */
7066             ulTotalTime /= 100UL;
7067
7068             /* Avoid divide by zero errors. */
7069             if( ulTotalTime > 0UL )
7070             {
7071                 /* Create a human readable table from the binary data. */
7072                 for( x = 0; ( x < uxArraySize ) && ( xOutputBufferFull == pdFALSE ); x++ )
7073                 {
7074                     /* What percentage of the total run time has the task used?
7075                      * This will always be rounded down to the nearest integer.
7076                      * ulTotalRunTime has already been divided by 100. */
7077                     ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
7078
7079                     /* Is there enough space in the buffer to hold task name? */
7080                     if( ( uxConsumedBufferLength + configMAX_TASK_NAME_LEN ) <= uxBufferLength )
7081                     {
7082                         /* Write the task name to the string, padding with
7083                          * spaces so it can be printed in tabular form more
7084                          * easily. */
7085                         pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
7086                         /* Do not count the terminating null character. */
7087                         uxConsumedBufferLength = uxConsumedBufferLength + ( configMAX_TASK_NAME_LEN - 1 );
7088
7089                         /* Is there space left in the buffer? -1 is done because snprintf
7090                          * writes a terminating null character. So we are essentially
7091                          * checking if the buffer has space to write at least one non-null
7092                          * character. */
7093                         if( uxConsumedBufferLength < ( uxBufferLength - 1 ) )
7094                         {
7095                             if( ulStatsAsPercentage > 0UL )
7096                             {
7097                                 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7098                                 {
7099                                     iSnprintfReturnValue = snprintf( pcWriteBuffer,
7100                                                                      uxBufferLength - uxConsumedBufferLength,
7101                                                                      "\t%lu\t\t%lu%%\r\n",
7102                                                                      pxTaskStatusArray[ x ].ulRunTimeCounter,
7103                                                                      ulStatsAsPercentage );
7104                                 }
7105                                 #else
7106                                 {
7107                                     /* sizeof( int ) == sizeof( long ) so a smaller
7108                                      * printf() library can be used. */
7109                                     iSnprintfReturnValue = snprintf( pcWriteBuffer,
7110                                                                      uxBufferLength - uxConsumedBufferLength,
7111                                                                      "\t%u\t\t%u%%\r\n",
7112                                                                      ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter,
7113                                                                      ( 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. */
7114                                 }
7115                                 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7116                             }
7117                             else
7118                             {
7119                                 /* If the percentage is zero here then the task has
7120                                  * consumed less than 1% of the total run time. */
7121                                 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
7122                                 {
7123                                     iSnprintfReturnValue = snprintf( pcWriteBuffer,
7124                                                                      uxBufferLength - uxConsumedBufferLength,
7125                                                                      "\t%lu\t\t<1%%\r\n",
7126                                                                      pxTaskStatusArray[ x ].ulRunTimeCounter );
7127                                 }
7128                                 #else
7129                                 {
7130                                     /* sizeof( int ) == sizeof( long ) so a smaller
7131                                      * printf() library can be used. */
7132                                     iSnprintfReturnValue = snprintf( pcWriteBuffer,
7133                                                                      uxBufferLength - uxConsumedBufferLength,
7134                                                                      "\t%u\t\t<1%%\r\n",
7135                                                                      ( 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. */
7136                                 }
7137                                 #endif /* ifdef portLU_PRINTF_SPECIFIER_REQUIRED */
7138                             }
7139
7140                             uxCharsWrittenBySnprintf = prvSnprintfReturnValueToCharsWritten( iSnprintfReturnValue, uxBufferLength - uxConsumedBufferLength );
7141                             uxConsumedBufferLength += uxCharsWrittenBySnprintf;
7142                             pcWriteBuffer += uxCharsWrittenBySnprintf; /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
7143                         }
7144                         else
7145                         {
7146                             xOutputBufferFull = pdTRUE;
7147                         }
7148                     }
7149                     else
7150                     {
7151                         xOutputBufferFull = pdTRUE;
7152                     }
7153                 }
7154             }
7155             else
7156             {
7157                 mtCOVERAGE_TEST_MARKER();
7158             }
7159
7160             /* Free the array again.  NOTE!  If configSUPPORT_DYNAMIC_ALLOCATION
7161              * is 0 then vPortFree() will be #defined to nothing. */
7162             vPortFree( pxTaskStatusArray );
7163         }
7164         else
7165         {
7166             mtCOVERAGE_TEST_MARKER();
7167         }
7168
7169         traceRETURN_vTaskGetRunTimeStatistics();
7170     }
7171
7172 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
7173 /*-----------------------------------------------------------*/
7174
7175 TickType_t uxTaskResetEventItemValue( void )
7176 {
7177     TickType_t uxReturn;
7178
7179     traceENTER_uxTaskResetEventItemValue();
7180
7181     uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
7182
7183     /* Reset the event list item to its normal value - so it can be used with
7184      * queues and semaphores. */
7185     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. */
7186
7187     traceRETURN_uxTaskResetEventItemValue( uxReturn );
7188
7189     return uxReturn;
7190 }
7191 /*-----------------------------------------------------------*/
7192
7193 #if ( configUSE_MUTEXES == 1 )
7194
7195     TaskHandle_t pvTaskIncrementMutexHeldCount( void )
7196     {
7197         TCB_t * pxTCB;
7198
7199         traceENTER_pvTaskIncrementMutexHeldCount();
7200
7201         pxTCB = pxCurrentTCB;
7202
7203         /* If xSemaphoreCreateMutex() is called before any tasks have been created
7204          * then pxCurrentTCB will be NULL. */
7205         if( pxTCB != NULL )
7206         {
7207             ( pxTCB->uxMutexesHeld )++;
7208         }
7209
7210         traceRETURN_pvTaskIncrementMutexHeldCount( pxTCB );
7211
7212         return pxTCB;
7213     }
7214
7215 #endif /* configUSE_MUTEXES */
7216 /*-----------------------------------------------------------*/
7217
7218 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7219
7220     uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWaitOn,
7221                                       BaseType_t xClearCountOnExit,
7222                                       TickType_t xTicksToWait )
7223     {
7224         uint32_t ulReturn;
7225
7226         traceENTER_ulTaskGenericNotifyTake( uxIndexToWaitOn, xClearCountOnExit, xTicksToWait );
7227
7228         configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7229
7230         taskENTER_CRITICAL();
7231         {
7232             /* Only block if the notification count is not already non-zero. */
7233             if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] == 0UL )
7234             {
7235                 /* Mark this task as waiting for a notification. */
7236                 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7237
7238                 if( xTicksToWait > ( TickType_t ) 0 )
7239                 {
7240                     prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7241                     traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWaitOn );
7242
7243                     /* All ports are written to allow a yield in a critical
7244                      * section (some will yield immediately, others wait until the
7245                      * critical section exits) - but it is not something that
7246                      * application code should ever do. */
7247                     taskYIELD_WITHIN_API();
7248                 }
7249                 else
7250                 {
7251                     mtCOVERAGE_TEST_MARKER();
7252                 }
7253             }
7254             else
7255             {
7256                 mtCOVERAGE_TEST_MARKER();
7257             }
7258         }
7259         taskEXIT_CRITICAL();
7260
7261         taskENTER_CRITICAL();
7262         {
7263             traceTASK_NOTIFY_TAKE( uxIndexToWaitOn );
7264             ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7265
7266             if( ulReturn != 0UL )
7267             {
7268                 if( xClearCountOnExit != pdFALSE )
7269                 {
7270                     pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = 0UL;
7271                 }
7272                 else
7273                 {
7274                     pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] = ulReturn - ( uint32_t ) 1;
7275                 }
7276             }
7277             else
7278             {
7279                 mtCOVERAGE_TEST_MARKER();
7280             }
7281
7282             pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7283         }
7284         taskEXIT_CRITICAL();
7285
7286         traceRETURN_ulTaskGenericNotifyTake( ulReturn );
7287
7288         return ulReturn;
7289     }
7290
7291 #endif /* configUSE_TASK_NOTIFICATIONS */
7292 /*-----------------------------------------------------------*/
7293
7294 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7295
7296     BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWaitOn,
7297                                        uint32_t ulBitsToClearOnEntry,
7298                                        uint32_t ulBitsToClearOnExit,
7299                                        uint32_t * pulNotificationValue,
7300                                        TickType_t xTicksToWait )
7301     {
7302         BaseType_t xReturn;
7303
7304         traceENTER_xTaskGenericNotifyWait( uxIndexToWaitOn, ulBitsToClearOnEntry, ulBitsToClearOnExit, pulNotificationValue, xTicksToWait );
7305
7306         configASSERT( uxIndexToWaitOn < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7307
7308         taskENTER_CRITICAL();
7309         {
7310             /* Only block if a notification is not already pending. */
7311             if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7312             {
7313                 /* Clear bits in the task's notification value as bits may get
7314                  * set  by the notifying task or interrupt.  This can be used to
7315                  * clear the value to zero. */
7316                 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnEntry;
7317
7318                 /* Mark this task as waiting for a notification. */
7319                 pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskWAITING_NOTIFICATION;
7320
7321                 if( xTicksToWait > ( TickType_t ) 0 )
7322                 {
7323                     prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
7324                     traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWaitOn );
7325
7326                     /* All ports are written to allow a yield in a critical
7327                      * section (some will yield immediately, others wait until the
7328                      * critical section exits) - but it is not something that
7329                      * application code should ever do. */
7330                     taskYIELD_WITHIN_API();
7331                 }
7332                 else
7333                 {
7334                     mtCOVERAGE_TEST_MARKER();
7335                 }
7336             }
7337             else
7338             {
7339                 mtCOVERAGE_TEST_MARKER();
7340             }
7341         }
7342         taskEXIT_CRITICAL();
7343
7344         taskENTER_CRITICAL();
7345         {
7346             traceTASK_NOTIFY_WAIT( uxIndexToWaitOn );
7347
7348             if( pulNotificationValue != NULL )
7349             {
7350                 /* Output the current notification value, which may or may not
7351                  * have changed. */
7352                 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ];
7353             }
7354
7355             /* If ucNotifyValue is set then either the task never entered the
7356              * blocked state (because a notification was already pending) or the
7357              * task unblocked because of a notification.  Otherwise the task
7358              * unblocked because of a timeout. */
7359             if( pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] != taskNOTIFICATION_RECEIVED )
7360             {
7361                 /* A notification was not received. */
7362                 xReturn = pdFALSE;
7363             }
7364             else
7365             {
7366                 /* A notification was already pending or a notification was
7367                  * received while the task was waiting. */
7368                 pxCurrentTCB->ulNotifiedValue[ uxIndexToWaitOn ] &= ~ulBitsToClearOnExit;
7369                 xReturn = pdTRUE;
7370             }
7371
7372             pxCurrentTCB->ucNotifyState[ uxIndexToWaitOn ] = taskNOT_WAITING_NOTIFICATION;
7373         }
7374         taskEXIT_CRITICAL();
7375
7376         traceRETURN_xTaskGenericNotifyWait( xReturn );
7377
7378         return xReturn;
7379     }
7380
7381 #endif /* configUSE_TASK_NOTIFICATIONS */
7382 /*-----------------------------------------------------------*/
7383
7384 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7385
7386     BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
7387                                    UBaseType_t uxIndexToNotify,
7388                                    uint32_t ulValue,
7389                                    eNotifyAction eAction,
7390                                    uint32_t * pulPreviousNotificationValue )
7391     {
7392         TCB_t * pxTCB;
7393         BaseType_t xReturn = pdPASS;
7394         uint8_t ucOriginalNotifyState;
7395
7396         traceENTER_xTaskGenericNotify( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue );
7397
7398         configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7399         configASSERT( xTaskToNotify );
7400         pxTCB = xTaskToNotify;
7401
7402         taskENTER_CRITICAL();
7403         {
7404             if( pulPreviousNotificationValue != NULL )
7405             {
7406                 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7407             }
7408
7409             ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7410
7411             pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7412
7413             switch( eAction )
7414             {
7415                 case eSetBits:
7416                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7417                     break;
7418
7419                 case eIncrement:
7420                     ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7421                     break;
7422
7423                 case eSetValueWithOverwrite:
7424                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7425                     break;
7426
7427                 case eSetValueWithoutOverwrite:
7428
7429                     if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7430                     {
7431                         pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7432                     }
7433                     else
7434                     {
7435                         /* The value could not be written to the task. */
7436                         xReturn = pdFAIL;
7437                     }
7438
7439                     break;
7440
7441                 case eNoAction:
7442
7443                     /* The task is being notified without its notify value being
7444                      * updated. */
7445                     break;
7446
7447                 default:
7448
7449                     /* Should not get here if all enums are handled.
7450                      * Artificially force an assert by testing a value the
7451                      * compiler can't assume is const. */
7452                     configASSERT( xTickCount == ( TickType_t ) 0 );
7453
7454                     break;
7455             }
7456
7457             traceTASK_NOTIFY( uxIndexToNotify );
7458
7459             /* If the task is in the blocked state specifically to wait for a
7460              * notification then unblock it now. */
7461             if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7462             {
7463                 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7464                 prvAddTaskToReadyList( pxTCB );
7465
7466                 /* The task should not have been on an event list. */
7467                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7468
7469                 #if ( configUSE_TICKLESS_IDLE != 0 )
7470                 {
7471                     /* If a task is blocked waiting for a notification then
7472                      * xNextTaskUnblockTime might be set to the blocked task's time
7473                      * out time.  If the task is unblocked for a reason other than
7474                      * a timeout xNextTaskUnblockTime is normally left unchanged,
7475                      * because it will automatically get reset to a new value when
7476                      * the tick count equals xNextTaskUnblockTime.  However if
7477                      * tickless idling is used it might be more important to enter
7478                      * sleep mode at the earliest possible time - so reset
7479                      * xNextTaskUnblockTime here to ensure it is updated at the
7480                      * earliest possible time. */
7481                     prvResetNextTaskUnblockTime();
7482                 }
7483                 #endif
7484
7485                 /* Check if the notified task has a priority above the currently
7486                  * executing task. */
7487                 taskYIELD_ANY_CORE_IF_USING_PREEMPTION( pxTCB );
7488             }
7489             else
7490             {
7491                 mtCOVERAGE_TEST_MARKER();
7492             }
7493         }
7494         taskEXIT_CRITICAL();
7495
7496         traceRETURN_xTaskGenericNotify( xReturn );
7497
7498         return xReturn;
7499     }
7500
7501 #endif /* configUSE_TASK_NOTIFICATIONS */
7502 /*-----------------------------------------------------------*/
7503
7504 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7505
7506     BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
7507                                           UBaseType_t uxIndexToNotify,
7508                                           uint32_t ulValue,
7509                                           eNotifyAction eAction,
7510                                           uint32_t * pulPreviousNotificationValue,
7511                                           BaseType_t * pxHigherPriorityTaskWoken )
7512     {
7513         TCB_t * pxTCB;
7514         uint8_t ucOriginalNotifyState;
7515         BaseType_t xReturn = pdPASS;
7516         UBaseType_t uxSavedInterruptStatus;
7517
7518         traceENTER_xTaskGenericNotifyFromISR( xTaskToNotify, uxIndexToNotify, ulValue, eAction, pulPreviousNotificationValue, pxHigherPriorityTaskWoken );
7519
7520         configASSERT( xTaskToNotify );
7521         configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7522
7523         /* RTOS ports that support interrupt nesting have the concept of a
7524          * maximum  system call (or maximum API call) interrupt priority.
7525          * Interrupts that are  above the maximum system call priority are keep
7526          * permanently enabled, even when the RTOS kernel is in a critical section,
7527          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
7528          * is defined in FreeRTOSConfig.h then
7529          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7530          * failure if a FreeRTOS API function is called from an interrupt that has
7531          * been assigned a priority above the configured maximum system call
7532          * priority.  Only FreeRTOS functions that end in FromISR can be called
7533          * from interrupts  that have been assigned a priority at or (logically)
7534          * below the maximum system call interrupt priority.  FreeRTOS maintains a
7535          * separate interrupt safe API to ensure interrupt entry is as fast and as
7536          * simple as possible.  More information (albeit Cortex-M specific) is
7537          * provided on the following link:
7538          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7539         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7540
7541         pxTCB = xTaskToNotify;
7542
7543         uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7544         {
7545             if( pulPreviousNotificationValue != NULL )
7546             {
7547                 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
7548             }
7549
7550             ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7551             pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7552
7553             switch( eAction )
7554             {
7555                 case eSetBits:
7556                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
7557                     break;
7558
7559                 case eIncrement:
7560                     ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7561                     break;
7562
7563                 case eSetValueWithOverwrite:
7564                     pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7565                     break;
7566
7567                 case eSetValueWithoutOverwrite:
7568
7569                     if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
7570                     {
7571                         pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
7572                     }
7573                     else
7574                     {
7575                         /* The value could not be written to the task. */
7576                         xReturn = pdFAIL;
7577                     }
7578
7579                     break;
7580
7581                 case eNoAction:
7582
7583                     /* The task is being notified without its notify value being
7584                      * updated. */
7585                     break;
7586
7587                 default:
7588
7589                     /* Should not get here if all enums are handled.
7590                      * Artificially force an assert by testing a value the
7591                      * compiler can't assume is const. */
7592                     configASSERT( xTickCount == ( TickType_t ) 0 );
7593                     break;
7594             }
7595
7596             traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
7597
7598             /* If the task is in the blocked state specifically to wait for a
7599              * notification then unblock it now. */
7600             if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7601             {
7602                 /* The task should not have been on an event list. */
7603                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7604
7605                 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
7606                 {
7607                     listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7608                     prvAddTaskToReadyList( pxTCB );
7609                 }
7610                 else
7611                 {
7612                     /* The delayed and ready lists cannot be accessed, so hold
7613                      * this task pending until the scheduler is resumed. */
7614                     listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
7615                 }
7616
7617                 #if ( configNUMBER_OF_CORES == 1 )
7618                 {
7619                     if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7620                     {
7621                         /* The notified task has a priority above the currently
7622                          * executing task so a yield is required. */
7623                         if( pxHigherPriorityTaskWoken != NULL )
7624                         {
7625                             *pxHigherPriorityTaskWoken = pdTRUE;
7626                         }
7627
7628                         /* Mark that a yield is pending in case the user is not
7629                          * using the "xHigherPriorityTaskWoken" parameter to an ISR
7630                          * safe FreeRTOS function. */
7631                         xYieldPendings[ 0 ] = pdTRUE;
7632                     }
7633                     else
7634                     {
7635                         mtCOVERAGE_TEST_MARKER();
7636                     }
7637                 }
7638                 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7639                 {
7640                     #if ( configUSE_PREEMPTION == 1 )
7641                     {
7642                         prvYieldForTask( pxTCB );
7643
7644                         if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
7645                         {
7646                             if( pxHigherPriorityTaskWoken != NULL )
7647                             {
7648                                 *pxHigherPriorityTaskWoken = pdTRUE;
7649                             }
7650                         }
7651                     }
7652                     #endif /* if ( configUSE_PREEMPTION == 1 ) */
7653                 }
7654                 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7655             }
7656         }
7657         taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
7658
7659         traceRETURN_xTaskGenericNotifyFromISR( xReturn );
7660
7661         return xReturn;
7662     }
7663
7664 #endif /* configUSE_TASK_NOTIFICATIONS */
7665 /*-----------------------------------------------------------*/
7666
7667 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7668
7669     void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
7670                                         UBaseType_t uxIndexToNotify,
7671                                         BaseType_t * pxHigherPriorityTaskWoken )
7672     {
7673         TCB_t * pxTCB;
7674         uint8_t ucOriginalNotifyState;
7675         UBaseType_t uxSavedInterruptStatus;
7676
7677         traceENTER_vTaskGenericNotifyGiveFromISR( xTaskToNotify, uxIndexToNotify, pxHigherPriorityTaskWoken );
7678
7679         configASSERT( xTaskToNotify );
7680         configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7681
7682         /* RTOS ports that support interrupt nesting have the concept of a
7683          * maximum  system call (or maximum API call) interrupt priority.
7684          * Interrupts that are  above the maximum system call priority are keep
7685          * permanently enabled, even when the RTOS kernel is in a critical section,
7686          * but cannot make any calls to FreeRTOS API functions.  If configASSERT()
7687          * is defined in FreeRTOSConfig.h then
7688          * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
7689          * failure if a FreeRTOS API function is called from an interrupt that has
7690          * been assigned a priority above the configured maximum system call
7691          * priority.  Only FreeRTOS functions that end in FromISR can be called
7692          * from interrupts  that have been assigned a priority at or (logically)
7693          * below the maximum system call interrupt priority.  FreeRTOS maintains a
7694          * separate interrupt safe API to ensure interrupt entry is as fast and as
7695          * simple as possible.  More information (albeit Cortex-M specific) is
7696          * provided on the following link:
7697          * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
7698         portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
7699
7700         pxTCB = xTaskToNotify;
7701
7702         uxSavedInterruptStatus = taskENTER_CRITICAL_FROM_ISR();
7703         {
7704             ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
7705             pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
7706
7707             /* 'Giving' is equivalent to incrementing a count in a counting
7708              * semaphore. */
7709             ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
7710
7711             traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
7712
7713             /* If the task is in the blocked state specifically to wait for a
7714              * notification then unblock it now. */
7715             if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
7716             {
7717                 /* The task should not have been on an event list. */
7718                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
7719
7720                 if( uxSchedulerSuspended == ( UBaseType_t ) 0U )
7721                 {
7722                     listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
7723                     prvAddTaskToReadyList( pxTCB );
7724                 }
7725                 else
7726                 {
7727                     /* The delayed and ready lists cannot be accessed, so hold
7728                      * this task pending until the scheduler is resumed. */
7729                     listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
7730                 }
7731
7732                 #if ( configNUMBER_OF_CORES == 1 )
7733                 {
7734                     if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
7735                     {
7736                         /* The notified task has a priority above the currently
7737                          * executing task so a yield is required. */
7738                         if( pxHigherPriorityTaskWoken != NULL )
7739                         {
7740                             *pxHigherPriorityTaskWoken = pdTRUE;
7741                         }
7742
7743                         /* Mark that a yield is pending in case the user is not
7744                          * using the "xHigherPriorityTaskWoken" parameter in an ISR
7745                          * safe FreeRTOS function. */
7746                         xYieldPendings[ 0 ] = pdTRUE;
7747                     }
7748                     else
7749                     {
7750                         mtCOVERAGE_TEST_MARKER();
7751                     }
7752                 }
7753                 #else /* #if ( configNUMBER_OF_CORES == 1 ) */
7754                 {
7755                     #if ( configUSE_PREEMPTION == 1 )
7756                     {
7757                         prvYieldForTask( pxTCB );
7758
7759                         if( xYieldPendings[ portGET_CORE_ID() ] == pdTRUE )
7760                         {
7761                             if( pxHigherPriorityTaskWoken != NULL )
7762                             {
7763                                 *pxHigherPriorityTaskWoken = pdTRUE;
7764                             }
7765                         }
7766                     }
7767                     #endif /* #if ( configUSE_PREEMPTION == 1 ) */
7768                 }
7769                 #endif /* #if ( configNUMBER_OF_CORES == 1 ) */
7770             }
7771         }
7772         taskEXIT_CRITICAL_FROM_ISR( uxSavedInterruptStatus );
7773
7774         traceRETURN_vTaskGenericNotifyGiveFromISR();
7775     }
7776
7777 #endif /* configUSE_TASK_NOTIFICATIONS */
7778 /*-----------------------------------------------------------*/
7779
7780 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7781
7782     BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
7783                                              UBaseType_t uxIndexToClear )
7784     {
7785         TCB_t * pxTCB;
7786         BaseType_t xReturn;
7787
7788         traceENTER_xTaskGenericNotifyStateClear( xTask, uxIndexToClear );
7789
7790         configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7791
7792         /* If null is passed in here then it is the calling task that is having
7793          * its notification state cleared. */
7794         pxTCB = prvGetTCBFromHandle( xTask );
7795
7796         taskENTER_CRITICAL();
7797         {
7798             if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
7799             {
7800                 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
7801                 xReturn = pdPASS;
7802             }
7803             else
7804             {
7805                 xReturn = pdFAIL;
7806             }
7807         }
7808         taskEXIT_CRITICAL();
7809
7810         traceRETURN_xTaskGenericNotifyStateClear( xReturn );
7811
7812         return xReturn;
7813     }
7814
7815 #endif /* configUSE_TASK_NOTIFICATIONS */
7816 /*-----------------------------------------------------------*/
7817
7818 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
7819
7820     uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
7821                                             UBaseType_t uxIndexToClear,
7822                                             uint32_t ulBitsToClear )
7823     {
7824         TCB_t * pxTCB;
7825         uint32_t ulReturn;
7826
7827         traceENTER_ulTaskGenericNotifyValueClear( xTask, uxIndexToClear, ulBitsToClear );
7828
7829         configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
7830
7831         /* If null is passed in here then it is the calling task that is having
7832          * its notification state cleared. */
7833         pxTCB = prvGetTCBFromHandle( xTask );
7834
7835         taskENTER_CRITICAL();
7836         {
7837             /* Return the notification as it was before the bits were cleared,
7838              * then clear the bit mask. */
7839             ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
7840             pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
7841         }
7842         taskEXIT_CRITICAL();
7843
7844         traceRETURN_ulTaskGenericNotifyValueClear( ulReturn );
7845
7846         return ulReturn;
7847     }
7848
7849 #endif /* configUSE_TASK_NOTIFICATIONS */
7850 /*-----------------------------------------------------------*/
7851
7852 #if ( configGENERATE_RUN_TIME_STATS == 1 )
7853
7854     configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimeCounter( const TaskHandle_t xTask )
7855     {
7856         TCB_t * pxTCB;
7857
7858         traceENTER_ulTaskGetRunTimeCounter( xTask );
7859
7860         pxTCB = prvGetTCBFromHandle( xTask );
7861
7862         traceRETURN_ulTaskGetRunTimeCounter( pxTCB->ulRunTimeCounter );
7863
7864         return pxTCB->ulRunTimeCounter;
7865     }
7866
7867 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
7868 /*-----------------------------------------------------------*/
7869
7870 #if ( configGENERATE_RUN_TIME_STATS == 1 )
7871
7872     configRUN_TIME_COUNTER_TYPE ulTaskGetRunTimePercent( const TaskHandle_t xTask )
7873     {
7874         TCB_t * pxTCB;
7875         configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
7876
7877         traceENTER_ulTaskGetRunTimePercent( xTask );
7878
7879         ulTotalTime = ( configRUN_TIME_COUNTER_TYPE ) portGET_RUN_TIME_COUNTER_VALUE();
7880
7881         /* For percentage calculations. */
7882         ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
7883
7884         /* Avoid divide by zero errors. */
7885         if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
7886         {
7887             pxTCB = prvGetTCBFromHandle( xTask );
7888             ulReturn = pxTCB->ulRunTimeCounter / ulTotalTime;
7889         }
7890         else
7891         {
7892             ulReturn = 0;
7893         }
7894
7895         traceRETURN_ulTaskGetRunTimePercent( ulReturn );
7896
7897         return ulReturn;
7898     }
7899
7900 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
7901 /*-----------------------------------------------------------*/
7902
7903 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
7904
7905     configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
7906     {
7907         configRUN_TIME_COUNTER_TYPE ulReturn = 0;
7908         BaseType_t i;
7909
7910         traceENTER_ulTaskGetIdleRunTimeCounter();
7911
7912         for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
7913         {
7914             ulReturn += xIdleTaskHandles[ i ]->ulRunTimeCounter;
7915         }
7916
7917         traceRETURN_ulTaskGetIdleRunTimeCounter( ulReturn );
7918
7919         return ulReturn;
7920     }
7921
7922 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
7923 /*-----------------------------------------------------------*/
7924
7925 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
7926
7927     configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
7928     {
7929         configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
7930         configRUN_TIME_COUNTER_TYPE ulRunTimeCounter = 0;
7931         BaseType_t i;
7932
7933         traceENTER_ulTaskGetIdleRunTimePercent();
7934
7935         ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE() * configNUMBER_OF_CORES;
7936
7937         /* For percentage calculations. */
7938         ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
7939
7940         /* Avoid divide by zero errors. */
7941         if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
7942         {
7943             for( i = 0; i < ( BaseType_t ) configNUMBER_OF_CORES; i++ )
7944             {
7945                 ulRunTimeCounter += xIdleTaskHandles[ i ]->ulRunTimeCounter;
7946             }
7947
7948             ulReturn = ulRunTimeCounter / ulTotalTime;
7949         }
7950         else
7951         {
7952             ulReturn = 0;
7953         }
7954
7955         traceRETURN_ulTaskGetIdleRunTimePercent( ulReturn );
7956
7957         return ulReturn;
7958     }
7959
7960 #endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
7961 /*-----------------------------------------------------------*/
7962
7963 static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
7964                                             const BaseType_t xCanBlockIndefinitely )
7965 {
7966     TickType_t xTimeToWake;
7967     const TickType_t xConstTickCount = xTickCount;
7968
7969     #if ( INCLUDE_xTaskAbortDelay == 1 )
7970     {
7971         /* About to enter a delayed list, so ensure the ucDelayAborted flag is
7972          * reset to pdFALSE so it can be detected as having been set to pdTRUE
7973          * when the task leaves the Blocked state. */
7974         pxCurrentTCB->ucDelayAborted = pdFALSE;
7975     }
7976     #endif
7977
7978     /* Remove the task from the ready list before adding it to the blocked list
7979      * as the same list item is used for both lists. */
7980     if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
7981     {
7982         /* The current task must be in a ready list, so there is no need to
7983          * check, and the port reset macro can be called directly. */
7984         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. */
7985     }
7986     else
7987     {
7988         mtCOVERAGE_TEST_MARKER();
7989     }
7990
7991     #if ( INCLUDE_vTaskSuspend == 1 )
7992     {
7993         if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
7994         {
7995             /* Add the task to the suspended task list instead of a delayed task
7996              * list to ensure it is not woken by a timing event.  It will block
7997              * indefinitely. */
7998             listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
7999         }
8000         else
8001         {
8002             /* Calculate the time at which the task should be woken if the event
8003              * does not occur.  This may overflow but this doesn't matter, the
8004              * kernel will manage it correctly. */
8005             xTimeToWake = xConstTickCount + xTicksToWait;
8006
8007             /* The list item will be inserted in wake time order. */
8008             listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8009
8010             if( xTimeToWake < xConstTickCount )
8011             {
8012                 /* Wake time has overflowed.  Place this item in the overflow
8013                  * list. */
8014                 traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8015                 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8016             }
8017             else
8018             {
8019                 /* The wake time has not overflowed, so the current block list
8020                  * is used. */
8021                 traceMOVED_TASK_TO_DELAYED_LIST();
8022                 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8023
8024                 /* If the task entering the blocked state was placed at the
8025                  * head of the list of blocked tasks then xNextTaskUnblockTime
8026                  * needs to be updated too. */
8027                 if( xTimeToWake < xNextTaskUnblockTime )
8028                 {
8029                     xNextTaskUnblockTime = xTimeToWake;
8030                 }
8031                 else
8032                 {
8033                     mtCOVERAGE_TEST_MARKER();
8034                 }
8035             }
8036         }
8037     }
8038     #else /* INCLUDE_vTaskSuspend */
8039     {
8040         /* Calculate the time at which the task should be woken if the event
8041          * does not occur.  This may overflow but this doesn't matter, the kernel
8042          * will manage it correctly. */
8043         xTimeToWake = xConstTickCount + xTicksToWait;
8044
8045         /* The list item will be inserted in wake time order. */
8046         listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
8047
8048         if( xTimeToWake < xConstTickCount )
8049         {
8050             traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
8051             /* Wake time has overflowed.  Place this item in the overflow list. */
8052             vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8053         }
8054         else
8055         {
8056             traceMOVED_TASK_TO_DELAYED_LIST();
8057             /* The wake time has not overflowed, so the current block list is used. */
8058             vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
8059
8060             /* If the task entering the blocked state was placed at the head of the
8061              * list of blocked tasks then xNextTaskUnblockTime needs to be updated
8062              * too. */
8063             if( xTimeToWake < xNextTaskUnblockTime )
8064             {
8065                 xNextTaskUnblockTime = xTimeToWake;
8066             }
8067             else
8068             {
8069                 mtCOVERAGE_TEST_MARKER();
8070             }
8071         }
8072
8073         /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
8074         ( void ) xCanBlockIndefinitely;
8075     }
8076     #endif /* INCLUDE_vTaskSuspend */
8077 }
8078 /*-----------------------------------------------------------*/
8079
8080 #if ( portUSING_MPU_WRAPPERS == 1 )
8081
8082     xMPU_SETTINGS * xTaskGetMPUSettings( TaskHandle_t xTask )
8083     {
8084         TCB_t * pxTCB;
8085
8086         traceENTER_xTaskGetMPUSettings( xTask );
8087
8088         pxTCB = prvGetTCBFromHandle( xTask );
8089
8090         traceRETURN_xTaskGetMPUSettings( &( pxTCB->xMPUSettings ) );
8091
8092         return &( pxTCB->xMPUSettings );
8093     }
8094
8095 #endif /* portUSING_MPU_WRAPPERS */
8096 /*-----------------------------------------------------------*/
8097
8098 /* Code below here allows additional code to be inserted into this source file,
8099  * especially where access to file scope functions and data is needed (for example
8100  * when performing module tests). */
8101
8102 #ifdef FREERTOS_MODULE_TEST
8103     #include "tasks_test_access_functions.h"
8104 #endif
8105
8106
8107 #if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
8108
8109     #include "freertos_tasks_c_additions.h"
8110
8111     #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
8112         static void freertos_tasks_c_additions_init( void )
8113         {
8114             FREERTOS_TASKS_C_ADDITIONS_INIT();
8115         }
8116     #endif
8117
8118 #endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
8119 /*-----------------------------------------------------------*/
8120
8121 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8122
8123 /*
8124  * This is the kernel provided implementation of vApplicationGetIdleTaskMemory()
8125  * to provide the memory that is used by the Idle task. It is used when
8126  * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8127  * it's own implementation of vApplicationGetIdleTaskMemory by setting
8128  * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8129  */
8130     void vApplicationGetIdleTaskMemory( StaticTask_t ** ppxIdleTaskTCBBuffer,
8131                                         StackType_t ** ppxIdleTaskStackBuffer,
8132                                         uint32_t * pulIdleTaskStackSize )
8133     {
8134         static StaticTask_t xIdleTaskTCB;
8135         static StackType_t uxIdleTaskStack[ configMINIMAL_STACK_SIZE ];
8136
8137         *ppxIdleTaskTCBBuffer = &( xIdleTaskTCB );
8138         *ppxIdleTaskStackBuffer = &( uxIdleTaskStack[ 0 ] );
8139         *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
8140     }
8141
8142 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8143 /*-----------------------------------------------------------*/
8144
8145 #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
8146
8147 /*
8148  * This is the kernel provided implementation of vApplicationGetTimerTaskMemory()
8149  * to provide the memory that is used by the Timer service task. It is used when
8150  * configKERNEL_PROVIDED_STATIC_MEMORY is set to 1. The application can provide
8151  * it's own implementation of vApplicationGetTimerTaskMemory by setting
8152  * configKERNEL_PROVIDED_STATIC_MEMORY to 0 or leaving it undefined.
8153  */
8154     void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
8155                                          StackType_t ** ppxTimerTaskStackBuffer,
8156                                          uint32_t * pulTimerTaskStackSize )
8157     {
8158         static StaticTask_t xTimerTaskTCB;
8159         static StackType_t uxTimerTaskStack[ configTIMER_TASK_STACK_DEPTH ];
8160
8161         *ppxTimerTaskTCBBuffer = &( xTimerTaskTCB );
8162         *ppxTimerTaskStackBuffer = &( uxTimerTaskStack[ 0 ] );
8163         *pulTimerTaskStackSize = configTIMER_TASK_STACK_DEPTH;
8164     }
8165
8166 #endif /* #if ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configKERNEL_PROVIDED_STATIC_MEMORY == 1 ) && ( portUSING_MPU_WRAPPERS == 0 ) ) */
8167 /*-----------------------------------------------------------*/