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