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