]> granicus.if.org Git - esp-idf/blob - components/freertos/tasks.c
Added: display xCoreID in vTaskList
[esp-idf] / components / freertos / tasks.c
1 /*
2     FreeRTOS V8.2.0 - Copyright (C) 2015 Real Time Engineers Ltd.
3     All rights reserved
4
5     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
6
7     This file is part of the FreeRTOS distribution.
8
9     FreeRTOS is free software; you can redistribute it and/or modify it under
10     the terms of the GNU General Public License (version 2) as published by the
11     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
12
13         ***************************************************************************
14     >>!   NOTE: The modification to the GPL is included to allow you to     !<<
15     >>!   distribute a combined work that includes FreeRTOS without being   !<<
16     >>!   obliged to provide the source code for proprietary components     !<<
17     >>!   outside of the FreeRTOS kernel.                                   !<<
18         ***************************************************************************
19
20     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
21     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
22     FOR A PARTICULAR PURPOSE.  Full license text is available on the following
23     link: http://www.freertos.org/a00114.html
24
25     ***************************************************************************
26      *                                                                       *
27      *    FreeRTOS provides completely free yet professionally developed,    *
28      *    robust, strictly quality controlled, supported, and cross          *
29      *    platform software that is more than just the market leader, it     *
30      *    is the industry's de facto standard.                               *
31      *                                                                       *
32      *    Help yourself get started quickly while simultaneously helping     *
33      *    to support the FreeRTOS project by purchasing a FreeRTOS           *
34      *    tutorial book, reference manual, or both:                          *
35      *    http://www.FreeRTOS.org/Documentation                              *
36      *                                                                       *
37     ***************************************************************************
38
39     http://www.FreeRTOS.org/FAQHelp.html - Having a problem?  Start by reading
40         the FAQ page "My application does not run, what could be wrong?".  Have you
41         defined configASSERT()?
42
43         http://www.FreeRTOS.org/support - In return for receiving this top quality
44         embedded software for free we request you assist our global community by
45         participating in the support forum.
46
47         http://www.FreeRTOS.org/training - Investing in training allows your team to
48         be as productive as possible as early as possible.  Now you can receive
49         FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
50         Ltd, and the world's leading authority on the world's leading RTOS.
51
52     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
53     including FreeRTOS+Trace - an indispensable productivity tool, a DOS
54     compatible FAT file system, and our tiny thread aware UDP/IP stack.
55
56     http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
57     Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
58
59     http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
60     Integrity Systems ltd. to sell under the OpenRTOS brand.  Low cost OpenRTOS
61     licenses offer ticketed support, indemnification and commercial middleware.
62
63     http://www.SafeRTOS.com - High Integrity Systems also provide a safety
64     engineered and independently SIL3 certified version for use in safety and
65     mission critical applications that require provable dependability.
66
67     1 tab == 4 spaces!
68 */
69
70 /* Standard includes. */
71 #include <stdlib.h>
72 #include <string.h>
73
74 /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
75 all the API functions to use the MPU wrappers.  That should only be done when
76 task.h is included from an application file. */
77 #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
78
79 #include "rom/ets_sys.h"
80 #include "esp_newlib.h"
81 #include "esp_panic.h"
82
83 /* FreeRTOS includes. */
84 #include "FreeRTOS.h"
85 #include "task.h"
86 #include "timers.h"
87 #include "StackMacros.h"
88 #include "portmacro.h"
89 #include "semphr.h"
90
91 /* Lint e961 and e750 are suppressed as a MISRA exception justified because the
92 MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined for the
93 header files above, but not in this file, in order to generate the correct
94 privileged Vs unprivileged linkage and placement. */
95 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750. */
96
97 /* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
98 functions but without including stdio.h here. */
99 #if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
100         /* At the bottom of this file are two optional functions that can be used
101         to generate human readable text from the raw data generated by the
102         uxTaskGetSystemState() function.  Note the formatting functions are provided
103         for convenience only, and are NOT considered part of the kernel. */
104         #include <stdio.h>
105 #endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
106
107 /* Sanity check the configuration. */
108 #if configUSE_TICKLESS_IDLE != 0
109         #if INCLUDE_vTaskSuspend != 1
110                 #error INCLUDE_vTaskSuspend must be set to 1 if configUSE_TICKLESS_IDLE is not set to 0
111         #endif /* INCLUDE_vTaskSuspend */
112 #endif /* configUSE_TICKLESS_IDLE */
113
114 /*
115  * Defines the size, in bytes, of the stack allocated to the idle task.
116  */
117 #define tskIDLE_STACK_SIZE      configIDLE_TASK_STACK_SIZE
118
119 #if( configUSE_PREEMPTION == 0 )
120         /* If the cooperative scheduler is being used then a yield should not be
121         performed just because a higher priority task has been woken. */
122         #define taskYIELD_IF_USING_PREEMPTION()
123 #else
124         #define taskYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
125 #endif
126
127
128
129
130 /* Value that can be assigned to the eNotifyState member of the TCB. */
131 typedef enum
132 {
133         eNotWaitingNotification = 0,
134         eWaitingNotification,
135         eNotified
136 } eNotifyValue;
137
138 /* Sometimes the FreeRTOSConfig.h settings only allow a task to be created using
139 dynamically allocated RAM, in which case when any task is deleted it is known
140 that both the task's stack and TCB need to be freed.  Sometimes the
141 FreeRTOSConfig.h settings only allow a task to be created using statically
142 allocated RAM, in which case when any task is deleted it is known that neither
143 the task's stack or TCB should be freed.  Sometimes the FreeRTOSConfig.h
144 settings allow a task to be created using either statically or dynamically
145 allocated RAM, in which case a member of the TCB is used to record whether the
146 stack and/or TCB were allocated statically or dynamically, so when a task is
147 deleted the RAM that was allocated dynamically is freed again and no attempt is
148 made to free the RAM that was allocated statically.
149 tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE is only true if it is possible for a
150 task to be created using either statically or dynamically allocated RAM.  Note
151 that if portUSING_MPU_WRAPPERS is 1 then a protected task can be created with
152 a statically allocated stack and a dynamically allocated TCB. */
153 #define tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE ( ( ( configSUPPORT_STATIC_ALLOCATION == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) ) || ( portUSING_MPU_WRAPPERS == 1 ) )
154 #define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB          ( ( uint8_t ) 0 )
155 #define tskSTATICALLY_ALLOCATED_STACK_ONLY                      ( ( uint8_t ) 1 )
156 #define tskSTATICALLY_ALLOCATED_STACK_AND_TCB           ( ( uint8_t ) 2 )
157
158 /*
159  * Task control block.  A task control block (TCB) is allocated for each task,
160  * and stores task state information, including a pointer to the task's context
161  * (the task's run time environment, including register values)
162  */
163 typedef struct tskTaskControlBlock
164 {
165         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. */
166
167         #if ( portUSING_MPU_WRAPPERS == 1 )
168                 xMPU_SETTINGS   xMPUSettings;           /*< The MPU settings are defined as part of the port layer.  THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
169         #endif
170
171         ListItem_t                      xGenericListItem;       /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
172         ListItem_t                      xEventListItem;         /*< Used to reference a task from an event list. */
173         UBaseType_t                     uxPriority;                     /*< The priority of the task.  0 is the lowest priority. */
174         StackType_t                     *pxStack;                       /*< Points to the start of the stack. */
175         char                            pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created.  Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
176         BaseType_t                      xCoreID;                        /*< Core this task is pinned to */
177                                                                                         /* If this moves around (other than pcTaskName size changes), please change the define in xtensa_vectors.S as well. */
178         #if ( portSTACK_GROWTH > 0 || configENABLE_TASK_SNAPSHOT == 1 )
179                 StackType_t             *pxEndOfStack;          /*< Points to the end of the stack on architectures where the stack grows up from low memory. */
180         #endif
181
182         #if ( portCRITICAL_NESTING_IN_TCB == 1 )
183                 UBaseType_t     uxCriticalNesting;      /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
184                 uint32_t                uxOldInterruptState; /*< Interrupt state before the outer taskEnterCritical was called */
185         #endif
186
187         #if ( configUSE_TRACE_FACILITY == 1 )
188                 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. */
189                 UBaseType_t     uxTaskNumber;           /*< Stores a number specifically for use by third party trace code. */
190         #endif
191
192         #if ( configUSE_MUTEXES == 1 )
193                 UBaseType_t     uxBasePriority;         /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
194                 UBaseType_t     uxMutexesHeld;
195         #endif
196
197         #if ( configUSE_APPLICATION_TASK_TAG == 1 )
198                 TaskHookFunction_t pxTaskTag;
199         #endif
200
201         #if( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
202                 void *pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
203         #if ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
204                 TlsDeleteCallbackFunction_t pvThreadLocalStoragePointersDelCallback[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
205         #endif
206         #endif
207
208         #if ( configGENERATE_RUN_TIME_STATS == 1 )
209                 uint32_t                ulRunTimeCounter;       /*< Stores the amount of time the task has spent in the Running state. */
210         #endif
211
212         #if ( configUSE_NEWLIB_REENTRANT == 1 )
213                 /* Allocate a Newlib reent structure that is specific to this task.
214                 Note Newlib support has been included by popular demand, but is not
215                 used by the FreeRTOS maintainers themselves.  FreeRTOS is not
216                 responsible for resulting newlib operation.  User must be familiar with
217                 newlib and must provide system-wide implementations of the necessary
218                 stubs. Be warned that (at the time of writing) the current newlib design
219                 implements a system-wide malloc() that must be provided with locks. */
220                 struct  _reent xNewLib_reent;
221         #endif
222
223         #if ( configUSE_TASK_NOTIFICATIONS == 1 )
224                 volatile uint32_t ulNotifiedValue;
225                 volatile eNotifyValue eNotifyState;
226         #endif
227
228         /* See the comments above the definition of
229         tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
230         #if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
231                 uint8_t ucStaticallyAllocated;          /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
232         #endif
233
234 } tskTCB;
235
236 /* The old tskTCB name is maintained above then typedefed to the new TCB_t name
237 below to enable the use of older kernel aware debuggers. */
238 typedef tskTCB TCB_t;
239
240 #if __GNUC_PREREQ(4, 6)
241 _Static_assert(sizeof(StaticTask_t) == sizeof(TCB_t), "StaticTask_t != TCB_t");
242 #endif
243
244 /*
245  * Some kernel aware debuggers require the data the debugger needs access to to
246  * be global, rather than file scope.
247  */
248 #ifdef portREMOVE_STATIC_QUALIFIER
249         #define static
250 #endif
251
252 /*lint -e956 A manual analysis and inspection has been used to determine which
253 static variables must be declared volatile. */
254
255 PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB[ portNUM_PROCESSORS ] = { NULL };
256
257 /* Lists for ready and blocked tasks. --------------------*/
258 PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ];/*< Prioritised ready tasks. */
259 PRIVILEGED_DATA static List_t xDelayedTaskList1;                                                /*< Delayed tasks. */
260 PRIVILEGED_DATA static List_t xDelayedTaskList2;                                                /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
261 PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList;                             /*< Points to the delayed task list currently being used. */
262 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. */
263 PRIVILEGED_DATA static List_t xPendingReadyList[ portNUM_PROCESSORS ];                                          /*< Tasks that have been readied while the scheduler was suspended.  They will be moved to the ready list when the scheduler is resumed. */
264
265 #if ( INCLUDE_vTaskDelete == 1 )
266
267         PRIVILEGED_DATA static List_t xTasksWaitingTermination;                         /*< Tasks that have been deleted - but their memory not yet freed. Protected by xTaskQueueMutex.*/
268         PRIVILEGED_DATA static volatile UBaseType_t uxTasksDeleted = ( UBaseType_t ) 0U;
269
270 #endif
271
272 #if ( INCLUDE_vTaskSuspend == 1 )
273
274         PRIVILEGED_DATA static List_t xSuspendedTaskList;                                       /*< Tasks that are currently suspended. */
275
276 #endif
277
278 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
279
280         PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle[portNUM_PROCESSORS] = {NULL};                       /*< Holds the handle of the idle task.  The idle task is created automatically when the scheduler is started. */
281
282 #endif
283
284 /* Other file private variables. --------------------------------*/
285 PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks      = ( UBaseType_t ) 0U;
286 PRIVILEGED_DATA static volatile TickType_t xTickCount                           = ( TickType_t ) 0U;
287 PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority          = tskIDLE_PRIORITY;
288 PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning            = pdFALSE;
289 PRIVILEGED_DATA static volatile UBaseType_t uxPendedTicks                       = ( UBaseType_t ) 0U;
290 PRIVILEGED_DATA static volatile BaseType_t xYieldPending[portNUM_PROCESSORS]            = {pdFALSE};
291 PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows                      = ( BaseType_t ) 0;
292 PRIVILEGED_DATA static UBaseType_t uxTaskNumber                                         = ( UBaseType_t ) 0U;
293 PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime         = portMAX_DELAY;
294
295 /* Context switches are held pending while the scheduler is suspended.  Also,
296 interrupts must not manipulate the xGenericListItem of a TCB, or any of the
297 lists the xGenericListItem can be referenced from, if the scheduler is suspended.
298 If an interrupt needs to unblock a task while the scheduler is suspended then it
299 moves the task's event list item into the xPendingReadyList, ready for the
300 kernel to move the task from the pending ready list into the real ready list
301 when the scheduler is unsuspended.  The pending ready list itself can only be
302 accessed from a critical section. */
303 PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended[ portNUM_PROCESSORS ]  = { ( UBaseType_t ) pdFALSE };
304
305 /* For now, we use just one mux for all the critical sections. ToDo: give everything a bit more granularity;
306   that could improve performance by not needlessly spinning in spinlocks for unrelated resources. */
307 PRIVILEGED_DATA static portMUX_TYPE xTaskQueueMutex = portMUX_INITIALIZER_UNLOCKED;
308 PRIVILEGED_DATA static portMUX_TYPE xTickCountMutex = portMUX_INITIALIZER_UNLOCKED;
309
310 #if ( configGENERATE_RUN_TIME_STATS == 1 )
311
312         PRIVILEGED_DATA static uint32_t ulTaskSwitchedInTime[portNUM_PROCESSORS] = {0U};        /*< Holds the value of a timer/counter the last time a task was switched in on a particular core. */
313         PRIVILEGED_DATA static uint32_t ulTotalRunTime = 0UL;           /*< Holds the total amount of execution time as defined by the run time counter clock. */
314
315 #endif
316
317
318 // per-CPU flags indicating that we are doing context switch, it is used by apptrace and sysview modules
319 // in order to avoid calls of vPortYield from traceTASK_SWITCHED_IN/OUT when waiting
320 // for locks to be free or for host to read full trace buffer
321 PRIVILEGED_DATA static volatile BaseType_t xSwitchingContext[ portNUM_PROCESSORS ]  = { pdFALSE };
322
323 /*lint +e956 */
324
325 /* Debugging and trace facilities private variables and macros. ------------*/
326
327 /*
328  * The value used to fill the stack of a task when the task is created.  This
329  * is used purely for checking the high water mark for tasks.
330  */
331 #define tskSTACK_FILL_BYTE      ( 0xa5U )
332
333 /*
334  * Macros used by vListTask to indicate which state a task is in.
335  */
336 #define tskBLOCKED_CHAR         ( 'B' )
337 #define tskREADY_CHAR           ( 'R' )
338 #define tskDELETED_CHAR         ( 'D' )
339 #define tskSUSPENDED_CHAR       ( 'S' )
340
341 /*-----------------------------------------------------------*/
342
343
344 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
345
346         /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
347         performed in a generic way that is not optimised to any particular
348         microcontroller architecture. */
349
350         /* uxTopReadyPriority holds the priority of the highest priority ready
351         state task. */
352         #define taskRECORD_READY_PRIORITY( uxPriority )                                                                                                         \
353         {                                                                                                                                                                                                       \
354                 if( ( uxPriority ) > uxTopReadyPriority )                                                                                                               \
355                 {                                                                                                                                                                                               \
356                         uxTopReadyPriority = ( uxPriority );                                                                                                            \
357                 }                                                                                                                                                                                               \
358         } /* taskRECORD_READY_PRIORITY */
359
360         /*-----------------------------------------------------------*/
361
362         #define taskSELECT_HIGHEST_PRIORITY_TASK()                                                                                                                      \
363         {                                                                                                                                                                                                       \
364                 /* Find the highest priority queue that contains ready tasks. */                                                                \
365                 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopReadyPriority ] ) ) )                                              \
366                 {                                                                                                                                                                                               \
367                         configASSERT( uxTopReadyPriority );                                                                                                                     \
368                         --uxTopReadyPriority;                                                                                                                                           \
369                 }                                                                                                                                                                                               \
370                                                                                                                                                                                                                 \
371                 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of                                                \
372                 the     same priority get an equal share of the processor time. */                                                                      \
373                 listGET_OWNER_OF_NEXT_ENTRY( xTaskGetCurrentTaskHandle(), &( pxReadyTasksLists[ uxTopReadyPriority ] ) );               \
374         } /* taskSELECT_HIGHEST_PRIORITY_TASK */
375
376         /*-----------------------------------------------------------*/
377
378         /* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
379         they are only required when a port optimised method of task selection is
380         being used. */
381         #define taskRESET_READY_PRIORITY( uxPriority )
382         #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
383
384 #else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
385
386         /* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
387         performed in a way that is tailored to the particular microcontroller
388         architecture being used. */
389
390         /* A port optimised version is provided.  Call the port defined macros. */
391         #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( uxPriority, uxTopReadyPriority )
392
393         /*-----------------------------------------------------------*/
394
395         #define taskSELECT_HIGHEST_PRIORITY_TASK()                                                                                                              \
396         {                                                                                                                                                                                               \
397         UBaseType_t uxTopPriority;                                                                                                                                              \
398                                                                                                                                                                                                         \
399                 /* Find the highest priority queue that contains ready tasks. */                                                        \
400                 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority );                                                          \
401                 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 );         \
402                 listGET_OWNER_OF_NEXT_ENTRY( xTaskGetCurrentTaskHandle(), &( pxReadyTasksLists[ uxTopPriority ] ) );            \
403         } /* taskSELECT_HIGHEST_PRIORITY_TASK() */
404
405         /*-----------------------------------------------------------*/
406
407         /* A port optimised version is provided, call it only if the TCB being reset
408         is being referenced from a ready list.  If it is referenced from a delayed
409         or suspended list then it won't be in a ready list. */
410         #define taskRESET_READY_PRIORITY( uxPriority )                                                                                                          \
411         {                                                                                                                                                                                                       \
412                 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 )  \
413                 {                                                                                                                                                                                               \
414                         portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) );                                                     \
415                 }                                                                                                                                                                                               \
416         }
417
418 #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
419
420 /*-----------------------------------------------------------*/
421
422 /* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
423 count overflows. */
424 #define taskSWITCH_DELAYED_LISTS()                                                                                                                                      \
425 {                                                                                                                                                                                                       \
426         List_t *pxTemp;                                                                                                                                                                 \
427                                                                                                                                                                                                         \
428         /* The delayed tasks list should be empty when the lists are switched. */                                               \
429         configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) );                                                                             \
430                                                                                                                                                                                                         \
431         pxTemp = pxDelayedTaskList;                                                                                                                                             \
432         pxDelayedTaskList = pxOverflowDelayedTaskList;                                                                                                  \
433         pxOverflowDelayedTaskList = pxTemp;                                                                                                                             \
434         xNumOfOverflows++;                                                                                                                                                              \
435         prvResetNextTaskUnblockTime();                                                                                                                                  \
436 }
437
438 /*-----------------------------------------------------------*/
439
440 /*
441  * Place the task represented by pxTCB into the appropriate ready list for
442  * the task.  It is inserted at the end of the list.
443  */
444 #define prvAddTaskToReadyList( pxTCB )                                                                                                                          \
445         traceMOVED_TASK_TO_READY_STATE( pxTCB );                                                                                                                \
446         taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority );                                                                                             \
447         vListInsertEnd( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xGenericListItem ) )
448 /*
449  * Place the task represented by pxTCB which has been in a ready list before
450  * into the appropriate ready list for the task.
451  * It is inserted at the end of the list.
452  */
453 #define prvReaddTaskToReadyList( pxTCB )                                                               \
454    traceREADDED_TASK_TO_READY_STATE( pxTCB );                                                      \
455    taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority );                                             \
456    vListInsertEnd( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xGenericListItem ) )
457 /*-----------------------------------------------------------*/
458
459 #define tskCAN_RUN_HERE( cpuid ) ( cpuid==xPortGetCoreID() || cpuid==tskNO_AFFINITY )
460
461 /*
462  * Several functions take an TaskHandle_t parameter that can optionally be NULL,
463  * where NULL is used to indicate that the handle of the currently executing
464  * task should be used in place of the parameter.  This macro simply checks to
465  * see if the parameter is NULL and returns a pointer to the appropriate TCB.
466  */
467 /* ToDo: See if this still works for multicore. */
468 #define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? ( TCB_t * ) xTaskGetCurrentTaskHandle() : ( TCB_t * ) ( pxHandle ) )
469
470 /* The item value of the event list item is normally used to hold the priority
471 of the task to which it belongs (coded to allow it to be held in reverse
472 priority order).  However, it is occasionally borrowed for other purposes.  It
473 is important its value is not updated due to a task priority change while it is
474 being used for another purpose.  The following bit definition is used to inform
475 the scheduler that the value should not be changed - in which case it is the
476 responsibility of whichever module is using the value to ensure it gets set back
477 to its original value when it is released. */
478 #if configUSE_16_BIT_TICKS == 1
479         #define taskEVENT_LIST_ITEM_VALUE_IN_USE        0x8000U
480 #else
481         #define taskEVENT_LIST_ITEM_VALUE_IN_USE        0x80000000UL
482 #endif
483
484 /* Callback function prototypes. --------------------------*/
485 #if configCHECK_FOR_STACK_OVERFLOW > 0
486         extern void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName );
487 #endif
488
489 #if configUSE_TICK_HOOK > 0
490         extern void vApplicationTickHook( void );
491 #endif
492 extern void esp_vApplicationTickHook( void );
493
494 #if  portFIRST_TASK_HOOK
495         extern void vPortFirstTaskHook(TaskFunction_t taskfn);
496 #endif
497
498
499 /* File private functions. --------------------------------*/
500
501 /**
502  * Utility task that simply returns pdTRUE if the task referenced by xTask is
503  * currently in the Suspended state, or pdFALSE if the task referenced by xTask
504  * is in any other state.
505  *
506  * Caller must hold xTaskQueueMutex before calling this function.
507  */
508 #if ( INCLUDE_vTaskSuspend == 1 )
509         static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
510 #endif /* INCLUDE_vTaskSuspend */
511
512 /*
513  * Utility to ready all the lists used by the scheduler.  This is called
514  * automatically upon the creation of the first task.
515  */
516 static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
517
518 /*
519  * The idle task, which as all tasks is implemented as a never ending loop.
520  * The idle task is automatically created and added to the ready lists upon
521  * creation of the first user task.
522  *
523  * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
524  * language extensions.  The equivalent prototype for this function is:
525  *
526  * void prvIdleTask( void *pvParameters );
527  *
528  */
529 static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
530
531 /*
532  * Utility to free all memory allocated by the scheduler to hold a TCB,
533  * including the stack pointed to by the TCB.
534  *
535  * This does not free memory allocated by the task itself (i.e. memory
536  * allocated by calls to pvPortMalloc from within the tasks application code).
537  */
538 #if ( INCLUDE_vTaskDelete == 1 )
539
540         static void prvDeleteTCB( TCB_t *pxTCB ) PRIVILEGED_FUNCTION;
541
542 #endif
543
544 //Function to call the Thread Local Storage Pointer Deletion Callbacks. Will be
545 //called during task deletion before prvDeleteTCB is called.
546 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 ) && ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
547         static void prvDeleteTLS( TCB_t *pxTCB );
548 #endif
549
550 /*
551  * Used only by the idle task.  This checks to see if anything has been placed
552  * in the list of tasks waiting to be deleted.  If so the task is cleaned up
553  * and its TCB deleted.
554  */
555 static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
556
557 /*
558  * The currently executing task is entering the Blocked state.  Add the task to
559  * either the current or the overflow delayed task list.
560  */
561 static void prvAddCurrentTaskToDelayedList( const portBASE_TYPE xCoreID, const TickType_t xTimeToWake ) PRIVILEGED_FUNCTION;
562
563 /*
564  * Fills an TaskStatus_t structure with information on each task that is
565  * referenced from the pxList list (which may be a ready list, a delayed list,
566  * a suspended list, etc.).
567  *
568  * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
569  * NORMAL APPLICATION CODE.
570  */
571 #if ( configUSE_TRACE_FACILITY == 1 )
572
573         static UBaseType_t prvListTaskWithinSingleList( TaskStatus_t *pxTaskStatusArray, List_t *pxList, eTaskState eState ) PRIVILEGED_FUNCTION;
574
575 #endif
576
577 /*
578  * When a task is created, the stack of the task is filled with a known value.
579  * This function determines the 'high water mark' of the task stack by
580  * determining how much of the stack remains at the original preset value.
581  */
582 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
583
584         static uint32_t prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
585
586 #endif
587
588 /*
589  * Return the amount of time, in ticks, that will pass before the kernel will
590  * next move a task from the Blocked state to the Running state.
591  *
592  * This conditional compilation should use inequality to 0, not equality to 1.
593  * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
594  * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
595  * set to a value other than 1.
596  */
597 #if ( configUSE_TICKLESS_IDLE != 0 )
598
599         static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
600
601 #endif
602
603 /*
604  * Set xNextTaskUnblockTime to the time at which the next Blocked state task
605  * will exit the Blocked state.
606  */
607 static void prvResetNextTaskUnblockTime( void );
608
609 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
610
611         /*
612          * Helper function used to pad task names with spaces when printing out
613          * human readable tables of task information.
614          */
615         static char *prvWriteNameToBuffer( char *pcBuffer, const char *pcTaskName );
616
617 #endif
618
619 /*
620  * Called after a Task_t structure has been allocated either statically or
621  * dynamically to fill in the structure's members.
622  */
623 static void prvInitialiseNewTask(       TaskFunction_t pxTaskCode,
624                                                                         const char * const pcName,
625                                                                         const uint32_t ulStackDepth,
626                                                                         void * const pvParameters,
627                                                                         UBaseType_t uxPriority,
628                                                                         TaskHandle_t * const pxCreatedTask,
629                                                                         TCB_t *pxNewTCB,
630                                                                         const MemoryRegion_t * const xRegions, const BaseType_t xCoreID) PRIVILEGED_FUNCTION; /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
631
632 /*
633  * Called after a new task has been created and initialised to place the task
634  * under the control of the scheduler.
635  */
636 static void prvAddNewTaskToReadyList( TCB_t *pxNewTCB, TaskFunction_t pxTaskCode, const BaseType_t xCoreID ) PRIVILEGED_FUNCTION;
637
638
639
640 /*-----------------------------------------------------------*/
641
642 /*
643  * This routine tries to send an interrupt to another core if needed to make it execute a task
644  * of higher priority. We try to figure out if needed first by inspecting the pxTCB of the
645  * other CPU first. Specifically for Xtensa, we can do this because pxTCB is an atomic pointer. It
646  * is possible that it is inaccurate because the other CPU just did a task switch, but in that case
647  * at most a superfluous interrupt is generated.
648 */
649 void taskYIELD_OTHER_CORE( BaseType_t xCoreID, UBaseType_t uxPriority )
650 {
651         TCB_t *curTCB = pxCurrentTCB[xCoreID];
652         BaseType_t i;
653
654         if (xCoreID != tskNO_AFFINITY) {
655                 if ( curTCB->uxPriority < uxPriority ) {
656                         vPortYieldOtherCore( xCoreID );
657                 }
658         }
659         else
660         {
661                 /* The task has no affinity. See if we can find a CPU to put it on.*/
662                 for (i=0; i<portNUM_PROCESSORS; i++) {
663                         if (i != xPortGetCoreID() && pxCurrentTCB[ i ]->uxPriority < uxPriority)
664                         {
665                                 vPortYieldOtherCore( i );
666                                 break;
667                         }
668                 }
669         }
670 }
671
672 #if( configSUPPORT_STATIC_ALLOCATION == 1 )
673
674         TaskHandle_t xTaskCreateStaticPinnedToCore(     TaskFunction_t pxTaskCode,
675                                                                         const char * const pcName,
676                                                                         const uint32_t ulStackDepth,
677                                                                         void * const pvParameters,
678                                                                         UBaseType_t uxPriority,
679                                                                         StackType_t * const puxStackBuffer,
680                                                                         StaticTask_t * const pxTaskBuffer,
681                                     const BaseType_t xCoreID )
682         {
683         TCB_t *pxNewTCB;
684         TaskHandle_t xReturn;
685
686                 configASSERT( portVALID_TCB_MEM(pxTaskBuffer) );
687                 configASSERT( portVALID_STACK_MEM(puxStackBuffer) );
688                 configASSERT( (xCoreID>=0 && xCoreID<portNUM_PROCESSORS) || (xCoreID==tskNO_AFFINITY) );
689
690                 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
691                 {
692                         /* The memory used for the task's TCB and stack are passed into this
693                         function - use them. */
694                         pxNewTCB = ( TCB_t * ) pxTaskBuffer; /*lint !e740 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
695                         pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
696
697                         #if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
698                         {
699                                 /* Tasks can be created statically or dynamically, so note this
700                                 task was created statically in case the task is later deleted. */
701                                 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
702                         }
703                         #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
704
705                         prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL, xCoreID );
706                         prvAddNewTaskToReadyList( pxNewTCB, pxTaskCode, xCoreID );
707                 }
708                 else
709                 {
710                         xReturn = NULL;
711                 }
712
713                 return xReturn;
714         }
715
716 #endif /* SUPPORT_STATIC_ALLOCATION */
717 /*-----------------------------------------------------------*/
718
719 #if( portUSING_MPU_WRAPPERS == 1 )
720
721         BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition, TaskHandle_t *pxCreatedTask )
722         {
723         TCB_t *pxNewTCB;
724         BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
725
726                 configASSERT( pxTaskDefinition->puxStackBuffer );
727
728                 if( pxTaskDefinition->puxStackBuffer != NULL )
729                 {
730                         /* Allocate space for the TCB.  Where the memory comes from depends
731                         on the implementation of the port malloc function and whether or
732                         not static allocation is being used. */
733                         pxNewTCB = ( TCB_t * ) pvPortMallocTcbMem( sizeof( TCB_t ) );
734
735                         if( pxNewTCB != NULL )
736                         {
737                                 /* Store the stack location in the TCB. */
738                                 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
739
740                                 /* Tasks can be created statically or dynamically, so note
741                                 this task had a statically allocated stack in case it is
742                                 later deleted.  The TCB was allocated dynamically. */
743                                 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
744
745                                 prvInitialiseNewTask(   pxTaskDefinition->pvTaskCode,
746                                                                                 pxTaskDefinition->pcName,
747                                                                                 pxTaskDefinition->usStackDepth,
748                                                                                 pxTaskDefinition->pvParameters,
749                                                                                 pxTaskDefinition->uxPriority,
750                                                                                 pxCreatedTask, pxNewTCB,
751                                                                                 pxTaskDefinition->xRegions,
752                                                                                 tskNO_AFFINITY );
753
754                                 prvAddNewTaskToReadyList( pxNewTCB, pxTaskDefinition->pvTaskCode, tskNO_AFFINITY );
755                                 xReturn = pdPASS;
756                         }
757                 }
758
759                 return xReturn;
760         }
761
762 #endif /* portUSING_MPU_WRAPPERS */
763 /*-----------------------------------------------------------*/
764
765 #if( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
766
767         BaseType_t xTaskCreatePinnedToCore(     TaskFunction_t pxTaskCode,
768                                                         const char * const pcName,
769                                                         const uint32_t usStackDepth,
770                                                         void * const pvParameters,
771                                                         UBaseType_t uxPriority,
772                                                         TaskHandle_t * const pxCreatedTask,
773                             const BaseType_t xCoreID )
774         {
775         TCB_t *pxNewTCB;
776         BaseType_t xReturn;
777
778                 /* If the stack grows down then allocate the stack then the TCB so the stack
779                 does not grow into the TCB.  Likewise if the stack grows up then allocate
780                 the TCB then the stack. */
781                 #if( portSTACK_GROWTH > 0 )
782                 {
783                         /* Allocate space for the TCB.  Where the memory comes from depends on
784                         the implementation of the port malloc function and whether or not static
785                         allocation is being used. */
786                         pxNewTCB = ( TCB_t * ) pvPortMallocTcbMem( sizeof( TCB_t ) );
787
788                         if( pxNewTCB != NULL )
789                         {
790                                 /* Allocate space for the stack used by the task being created.
791                                 The base of the stack memory stored in the TCB so the task can
792                                 be deleted later if required. */
793                                 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStackMem( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
794
795                                 if( pxNewTCB->pxStack == NULL )
796                                 {
797                                         /* Could not allocate the stack.  Delete the allocated TCB. */
798                                         vPortFree( pxNewTCB );
799                                         pxNewTCB = NULL;
800                                 }
801                         }
802                 }
803                 #else /* portSTACK_GROWTH */
804                 {
805                 StackType_t *pxStack;
806
807                         /* Allocate space for the stack used by the task being created. */
808                         pxStack = ( StackType_t * ) pvPortMallocStackMem( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
809
810                         if( pxStack != NULL )
811                         {
812                                 /* Allocate space for the TCB. */
813                                 pxNewTCB = ( TCB_t * ) pvPortMallocTcbMem( sizeof( TCB_t ) ); /*lint !e961 MISRA exception as the casts are only redundant for some paths. */
814
815                                 if( pxNewTCB != NULL )
816                                 {
817                                         /* Store the stack location in the TCB. */
818                                         pxNewTCB->pxStack = pxStack;
819                                 }
820                                 else
821                                 {
822                                         /* The stack cannot be used as the TCB was not created.  Free
823                                         it again. */
824                                         vPortFree( pxStack );
825                                 }
826                         }
827                         else
828                         {
829                                 pxNewTCB = NULL;
830                         }
831                 }
832                 #endif /* portSTACK_GROWTH */
833
834                 if( pxNewTCB != NULL )
835                 {
836                         #if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
837                         {
838                                 /* Tasks can be created statically or dynamically, so note this
839                                 task was created dynamically in case it is later deleted. */
840                                 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
841                         }
842                         #endif /* configSUPPORT_STATIC_ALLOCATION */
843
844                         prvInitialiseNewTask( pxTaskCode, pcName, usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL, xCoreID );
845                         prvAddNewTaskToReadyList( pxNewTCB, pxTaskCode, xCoreID );
846                         xReturn = pdPASS;
847                 }
848                 else
849                 {
850                         xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
851                 }
852
853                 return xReturn;
854         }
855
856 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
857 /*-----------------------------------------------------------*/
858
859 static void prvInitialiseNewTask(       TaskFunction_t pxTaskCode,
860                                                                         const char * const pcName,
861                                                                         const uint32_t ulStackDepth,
862                                                                         void * const pvParameters,
863                                                                         UBaseType_t uxPriority,
864                                                                         TaskHandle_t * const pxCreatedTask,
865                                                                         TCB_t *pxNewTCB,
866                                                                         const MemoryRegion_t * const xRegions, const BaseType_t xCoreID ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
867 {
868 StackType_t *pxTopOfStack;
869 UBaseType_t x;
870
871         #if( portUSING_MPU_WRAPPERS == 1 )
872                 /* Should the task be created in privileged mode? */
873                 BaseType_t xRunPrivileged;
874                 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
875                 {
876                         xRunPrivileged = pdTRUE;
877                 }
878                 else
879                 {
880                         xRunPrivileged = pdFALSE;
881                 }
882                 uxPriority &= ~portPRIVILEGE_BIT;
883         #endif /* portUSING_MPU_WRAPPERS == 1 */
884
885         /* Avoid dependency on memset() if it is not required. */
886         #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
887         {
888                 /* Fill the stack with a known value to assist debugging. */
889                 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
890         }
891         #endif /* ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) ) ) */
892
893         /* Calculate the top of stack address.  This depends on whether the stack
894         grows from high memory to low (as per the 80x86) or vice versa.
895         portSTACK_GROWTH is used to make the result positive or negative as required
896         by the port. */
897         #if( portSTACK_GROWTH < 0 )
898         {
899                 pxTopOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
900                 pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 MISRA exception.  Avoiding casts between pointers and integers is not practical.  Size differences accounted for using portPOINTER_SIZE_TYPE type. */
901
902                 /* Check the alignment of the calculated top of stack is correct. */
903                 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
904                 #if ( configENABLE_TASK_SNAPSHOT == 1 )
905                 {
906                         /* need stack end for core dumps */
907                         pxNewTCB->pxEndOfStack = pxTopOfStack;
908                 }
909 #endif
910         }
911         #else /* portSTACK_GROWTH */
912         {
913                 pxTopOfStack = pxNewTCB->pxStack;
914
915                 /* Check the alignment of the stack buffer is correct. */
916                 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
917
918                 /* The other extreme of the stack space is required if stack checking is
919                 performed. */
920                 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
921         }
922         #endif /* portSTACK_GROWTH */
923
924         /* Store the task name in the TCB. */
925         for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
926         {
927                 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
928
929                 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
930                 configMAX_TASK_NAME_LEN characters just in case the memory after the
931                 string is not accessible (extremely unlikely). */
932                 if( pcName[ x ] == 0x00 )
933                 {
934                         break;
935                 }
936                 else
937                 {
938                         mtCOVERAGE_TEST_MARKER();
939                 }
940         }
941
942         /* Ensure the name string is terminated in the case that the string length
943         was greater or equal to configMAX_TASK_NAME_LEN. */
944         pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
945
946         /* This is used as an array index so must ensure it's not too large.  First
947         remove the privilege bit if one is present. */
948         if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
949         {
950                 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
951         }
952         else
953         {
954                 mtCOVERAGE_TEST_MARKER();
955         }
956
957         pxNewTCB->uxPriority = uxPriority;
958         pxNewTCB->xCoreID = xCoreID;
959         #if ( configUSE_MUTEXES == 1 )
960         {
961                 pxNewTCB->uxBasePriority = uxPriority;
962                 pxNewTCB->uxMutexesHeld = 0;
963         }
964         #endif /* configUSE_MUTEXES */
965
966         vListInitialiseItem( &( pxNewTCB->xGenericListItem ) );
967         vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
968
969         /* Set the pxNewTCB as a link back from the ListItem_t.  This is so we can get
970         back to the containing TCB from a generic item in a list. */
971         listSET_LIST_ITEM_OWNER( &( pxNewTCB->xGenericListItem ), pxNewTCB );
972
973         /* Event lists are always in priority order. */
974         listSET_LIST_ITEM_VALUE( &( pxNewTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
975         listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
976
977         #if ( portCRITICAL_NESTING_IN_TCB == 1 )
978         {
979                 pxNewTCB->uxCriticalNesting = ( UBaseType_t ) 0U;
980         }
981         #endif /* portCRITICAL_NESTING_IN_TCB */
982
983         #if ( configUSE_APPLICATION_TASK_TAG == 1 )
984         {
985                 pxNewTCB->pxTaskTag = NULL;
986         }
987         #endif /* configUSE_APPLICATION_TASK_TAG */
988
989         #if ( configGENERATE_RUN_TIME_STATS == 1 )
990         {
991                 pxNewTCB->ulRunTimeCounter = 0UL;
992         }
993         #endif /* configGENERATE_RUN_TIME_STATS */
994
995         #if ( portUSING_MPU_WRAPPERS == 1 )
996         {
997                 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
998         }
999         #else
1000         {
1001                 /* Avoid compiler warning about unreferenced parameter. */
1002                 ( void ) xRegions;
1003         }
1004         #endif
1005
1006         #if( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
1007         {
1008                 for( x = 0; x < ( UBaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS; x++ )
1009                 {
1010                         pxNewTCB->pvThreadLocalStoragePointers[ x ] = NULL;
1011                         #if ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS == 1)
1012                         pxNewTCB->pvThreadLocalStoragePointersDelCallback[ x ] = NULL;
1013                         #endif
1014                 }
1015         }
1016         #endif
1017
1018         #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1019         {
1020                 pxNewTCB->ulNotifiedValue = 0;
1021                 pxNewTCB->eNotifyState = eNotWaitingNotification;
1022         }
1023         #endif
1024
1025         #if ( configUSE_NEWLIB_REENTRANT == 1 )
1026         {
1027                 /* Initialise this task's Newlib reent structure. */
1028                 esp_reent_init(&pxNewTCB->xNewLib_reent);
1029         }
1030         #endif
1031
1032         #if( INCLUDE_xTaskAbortDelay == 1 )
1033         {
1034                 pxNewTCB->ucDelayAborted = pdFALSE;
1035         }
1036         #endif
1037
1038         /* Initialize the TCB stack to look as if the task was already running,
1039         but had been interrupted by the scheduler.  The return address is set
1040         to the start of the task function. Once the stack has been initialised
1041         the     top of stack variable is updated. */
1042         #if( portUSING_MPU_WRAPPERS == 1 )
1043         {
1044                 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
1045         }
1046         #else /* portUSING_MPU_WRAPPERS */
1047         {
1048                 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1049         }
1050         #endif /* portUSING_MPU_WRAPPERS */
1051
1052         if( ( void * ) pxCreatedTask != NULL )
1053         {
1054                 /* Pass the handle out in an anonymous way.  The handle can be used to
1055                 change the created task's priority, delete the created task, etc.*/
1056                 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1057         }
1058         else
1059         {
1060                 mtCOVERAGE_TEST_MARKER();
1061         }
1062 }
1063 /*-----------------------------------------------------------*/
1064
1065 static void prvAddNewTaskToReadyList( TCB_t *pxNewTCB, TaskFunction_t pxTaskCode, BaseType_t xCoreID )
1066 {
1067         TCB_t *curTCB, *tcb0, *tcb1;
1068
1069         /* Assure that xCoreID is valid or we'll have an out-of-bounds on pxCurrentTCB
1070            You will assert here if e.g. you only have one CPU enabled in menuconfig and
1071            are trying to start a task on core 1. */
1072         configASSERT( xCoreID == tskNO_AFFINITY || xCoreID < portNUM_PROCESSORS);
1073
1074     /* Ensure interrupts don't access the task lists while the lists are being
1075         updated. */
1076         taskENTER_CRITICAL(&xTaskQueueMutex);
1077         {
1078                 uxCurrentNumberOfTasks++;
1079
1080                 // Determine which core this task starts on
1081                 if ( xCoreID == tskNO_AFFINITY )
1082                 {
1083                         if ( portNUM_PROCESSORS == 1 )
1084                         {
1085                                 xCoreID = 0;
1086                         }
1087                         else
1088                         {
1089                                 // if the task has no affinity, put it on either core if nothing is currently scheduled there. Failing that,
1090                                 // put it on the core where it will preempt the lowest priority running task. If neither of these are true,
1091                                 // queue it on the currently running core.
1092                                 tcb0 = pxCurrentTCB[0];
1093                                 tcb1 = pxCurrentTCB[1];
1094                                 if ( tcb0 == NULL )
1095                                 {
1096                                         xCoreID = 0;
1097                                 }
1098                                 else if ( tcb1 == NULL )
1099                                 {
1100                                         xCoreID = 1;
1101                                 }
1102                                 else if ( tcb0->uxPriority < pxNewTCB->uxPriority && tcb0->uxPriority < tcb1->uxPriority )
1103                                 {
1104                                         xCoreID = 0;
1105                                 }
1106                                 else if ( tcb1->uxPriority < pxNewTCB->uxPriority )
1107                                 {
1108                                         xCoreID = 1;
1109                                 }
1110                                 else
1111                                 {
1112                                         xCoreID = xPortGetCoreID(); // Both CPU have higher priority tasks running on them, so this won't run yet
1113                                 }
1114                         }
1115                 }
1116
1117         // If nothing is running on this core, put the new task there now
1118                 if( pxCurrentTCB[ xCoreID ] == NULL )
1119                 {
1120                         /* There are no other tasks, or all the other tasks are in
1121                         the suspended state - make this the current task. */
1122                         pxCurrentTCB[ xCoreID ] = pxNewTCB;
1123
1124                         if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1125                         {
1126 #if portFIRST_TASK_HOOK
1127                                 if ( xPortGetCoreID() == 0 ) {
1128                                         vPortFirstTaskHook(pxTaskCode);
1129                                 }
1130 #endif /* configFIRST_TASK_HOOK */
1131                                 /* This is the first task to be created so do the preliminary
1132                                 initialisation required.  We will not recover if this call
1133                                 fails, but we will report the failure. */
1134                                 prvInitialiseTaskLists();
1135                         }
1136                         else
1137                         {
1138                                 mtCOVERAGE_TEST_MARKER();
1139                         }
1140                 }
1141                 else
1142                 {
1143                         /* If the scheduler is not already running, make this task the
1144                         current task if it is the highest priority task to be created
1145                         so far. */
1146                         if( xSchedulerRunning == pdFALSE )
1147                         {
1148                                 /* Scheduler isn't running yet. We need to determine on which CPU to run this task.
1149                                    Schedule now if either nothing is scheduled yet or we can replace a task of lower prio. */
1150                                 if ( pxCurrentTCB[xCoreID] == NULL || pxCurrentTCB[xCoreID]->uxPriority <= pxNewTCB->uxPriority )
1151                                 {
1152                                         pxCurrentTCB[xCoreID] = pxNewTCB;
1153                                 }
1154                         }
1155                         else
1156                         {
1157                                 mtCOVERAGE_TEST_MARKER();
1158                         }
1159                 }
1160
1161                 uxTaskNumber++;
1162
1163                 #if ( configUSE_TRACE_FACILITY == 1 )
1164                 {
1165                         /* Add a counter into the TCB for tracing only. */
1166                         pxNewTCB->uxTCBNumber = uxTaskNumber;
1167                 }
1168                 #endif /* configUSE_TRACE_FACILITY */
1169                 traceTASK_CREATE( pxNewTCB );
1170
1171                 prvAddTaskToReadyList( pxNewTCB );
1172
1173                 portSETUP_TCB( pxNewTCB );
1174         }
1175
1176         taskEXIT_CRITICAL(&xTaskQueueMutex);
1177
1178         if( xSchedulerRunning != pdFALSE )
1179         {
1180                 taskENTER_CRITICAL(&xTaskQueueMutex);
1181
1182                 curTCB = pxCurrentTCB[ xCoreID ];
1183                 /* Scheduler is running. If the created task is of a higher priority than an executing task
1184                    then it should run now.
1185                 */
1186                 if( curTCB == NULL || curTCB->uxPriority < pxNewTCB->uxPriority )
1187                 {
1188                         if( xCoreID == xPortGetCoreID() )
1189                         {
1190                                 taskYIELD_IF_USING_PREEMPTION();
1191                         }
1192                         else {
1193                                 taskYIELD_OTHER_CORE(xCoreID, pxNewTCB->uxPriority);
1194                         }
1195                 }
1196                 else
1197                 {
1198                         mtCOVERAGE_TEST_MARKER();
1199                 }
1200                 taskEXIT_CRITICAL(&xTaskQueueMutex);
1201         }
1202         else
1203         {
1204                 mtCOVERAGE_TEST_MARKER();
1205         }
1206 }
1207 /*-----------------------------------------------------------*/
1208
1209 #if ( INCLUDE_vTaskDelete == 1 )
1210
1211         void vTaskDelete( TaskHandle_t xTaskToDelete )
1212         {
1213         //The following vTaskDelete() is backported from FreeRTOS v9.0.0 and modified for SMP.
1214         //v9.0.0 vTaskDelete() will immediately free task memory if the task being deleted is
1215         //NOT currently running and not pinned to the other core. Otherwise, freeing of task memory
1216         //will still be delegated to the Idle Task.
1217
1218         TCB_t *pxTCB;
1219         int core = xPortGetCoreID();    //Current core
1220         UBaseType_t free_now;   //Flag to indicate if task memory can be freed immediately
1221
1222                 taskENTER_CRITICAL(&xTaskQueueMutex);
1223                 {
1224                         /* If null is passed in here then it is the calling task that is
1225                         being deleted. */
1226                         pxTCB = prvGetTCBFromHandle( xTaskToDelete );
1227
1228                         /* Remove task from the ready list. */
1229                         if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
1230                         {
1231                                 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1232                         }
1233                         else
1234                         {
1235                                 mtCOVERAGE_TEST_MARKER();
1236                         }
1237
1238                         /* Is the task waiting on an event also? */
1239                         if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1240                         {
1241                                 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1242                         }
1243                         else
1244                         {
1245                                 mtCOVERAGE_TEST_MARKER();
1246                         }
1247
1248                         /* Increment the uxTaskNumber also so kernel aware debuggers can
1249                         detect that the task lists need re-generating.  This is done before
1250                         portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1251                         not return. */
1252                         uxTaskNumber++;
1253
1254                         //If task to be deleted is currently running on either core or is pinned to the other core. Let Idle free memory
1255                         if( pxTCB == pxCurrentTCB[ core ] ||
1256                                 (portNUM_PROCESSORS > 1 && pxTCB == pxCurrentTCB[ !core ]) ||
1257                                 (portNUM_PROCESSORS > 1 && pxTCB->xCoreID == (!core)) )
1258                         {
1259                                 /* Deleting a currently running task. This cannot complete
1260                                 within the task itself, as a context switch to another task is
1261                                 required. Place the task in the termination list.  The idle task
1262                                 will check the termination list and free up any memory allocated
1263                                 by the scheduler for the TCB and stack of the deleted task. */
1264                                 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xGenericListItem ) );
1265
1266                                 /* Increment the ucTasksDeleted variable so the idle task knows
1267                                 there is a task that has been deleted and that it should therefore
1268                                 check the xTasksWaitingTermination list. */
1269                                 ++uxTasksDeleted;
1270
1271                                 /* The pre-delete hook is primarily for the Windows simulator,
1272                                 in which Windows specific clean up operations are performed,
1273                                 after which it is not possible to yield away from this task -
1274                                 hence xYieldPending is used to latch that a context switch is
1275                                 required. */
1276                                 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPending );
1277
1278                                 free_now = pdFALSE;             //Let Idle Task free task memory
1279                         }
1280                         else    //Task is not currently running and not pinned to the other core
1281                         {
1282                                 --uxCurrentNumberOfTasks;
1283
1284                                 /* Reset the next expected unblock time in case it referred to
1285                                 the task that has just been deleted. */
1286                                 prvResetNextTaskUnblockTime();
1287                                 free_now = pdTRUE;              //Set flag to free task memory immediately
1288                         }
1289
1290                         traceTASK_DELETE( pxTCB );
1291                 }
1292                 taskEXIT_CRITICAL(&xTaskQueueMutex);
1293
1294                 if(free_now == pdTRUE){         //Free task memory. Outside critical section due to deletion callbacks
1295                         #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 ) && ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
1296                                 prvDeleteTLS( pxTCB );  //Run deletion callbacks before deleting TCB
1297                         #endif
1298                         prvDeleteTCB( pxTCB );  //Must only be called after del cb
1299                 }
1300
1301                 /* Force a reschedule if it is the currently running task that has just
1302                 been deleted. */
1303                 if( xSchedulerRunning != pdFALSE )
1304                 {
1305                         //No mux; no harm done if this misfires. The deleted task won't get scheduled anyway.
1306                         if( pxTCB == pxCurrentTCB[ core ] )     //If task was currently running on this core
1307                         {
1308                                 configASSERT( uxSchedulerSuspended[ core ] == 0 );
1309
1310                                 /* The pre-delete hook is primarily for the Windows simulator,
1311                                 in which Windows specific clean up operations are performed,
1312                                 after which it is not possible to yield away from this task -
1313                                 hence xYieldPending is used to latch that a context switch is
1314                                 required. */
1315                                 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPending[xPortGetCoreID()] );
1316                                 portYIELD_WITHIN_API();
1317                         }
1318                         else if ( portNUM_PROCESSORS > 1 && pxTCB == pxCurrentTCB[ !core] )     //If task was currently running on the other core
1319                         {
1320                                 /* if task is running on the other CPU, force a yield on that CPU to take it off */
1321                                 vPortYieldOtherCore( !core );
1322                         }
1323                         else
1324                         {
1325                                 mtCOVERAGE_TEST_MARKER();
1326                         }
1327                 }
1328         }
1329
1330 #endif /* INCLUDE_vTaskDelete */
1331 /*-----------------------------------------------------------*/
1332
1333 #if ( INCLUDE_vTaskDelayUntil == 1 )
1334
1335 /* ToDo: Make this multicore-compatible. */
1336         void vTaskDelayUntil( TickType_t * const pxPreviousWakeTime, const TickType_t xTimeIncrement )
1337         {
1338         TickType_t xTimeToWake;
1339         BaseType_t xAlreadyYielded=pdFALSE, xShouldDelay = pdFALSE;
1340
1341                 configASSERT( pxPreviousWakeTime );
1342                 configASSERT( ( xTimeIncrement > 0U ) );
1343                 configASSERT( uxSchedulerSuspended[ xPortGetCoreID() ] == 0 );
1344
1345                 taskENTER_CRITICAL(&xTaskQueueMutex);
1346 //              vTaskSuspendAll();
1347                 {
1348                         /* Minor optimisation.  The tick count cannot change in this
1349                         block. */
1350 //                      portTICK_TYPE_ENTER_CRITICAL( &xTickCountMutex );
1351                         const TickType_t xConstTickCount = xTickCount;
1352 //                      portTICK_TYPE_EXIT_CRITICAL( &xTickCountMutex );
1353
1354                         /* Generate the tick time at which the task wants to wake. */
1355                         xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
1356
1357                         if( xConstTickCount < *pxPreviousWakeTime )
1358                         {
1359                                 /* The tick count has overflowed since this function was
1360                                 lasted called.  In this case the only time we should ever
1361                                 actually delay is if the wake time has also     overflowed,
1362                                 and the wake time is greater than the tick time.  When this
1363                                 is the case it is as if neither time had overflowed. */
1364                                 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1365                                 {
1366                                         xShouldDelay = pdTRUE;
1367                                 }
1368                                 else
1369                                 {
1370                                         mtCOVERAGE_TEST_MARKER();
1371                                 }
1372                         }
1373                         else
1374                         {
1375                                 /* The tick time has not overflowed.  In this case we will
1376                                 delay if either the wake time has overflowed, and/or the
1377                                 tick time is less than the wake time. */
1378                                 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1379                                 {
1380                                         xShouldDelay = pdTRUE;
1381                                 }
1382                                 else
1383                                 {
1384                                         mtCOVERAGE_TEST_MARKER();
1385                                 }
1386                         }
1387
1388                         /* Update the wake time ready for the next call. */
1389                         *pxPreviousWakeTime = xTimeToWake;
1390
1391                         if( xShouldDelay != pdFALSE )
1392                         {
1393                                 traceTASK_DELAY_UNTIL();
1394
1395                                 /* Remove the task from the ready list before adding it to the
1396                                 blocked list as the same list item is used for both lists. */
1397                                 if( uxListRemove( &( pxCurrentTCB[ xPortGetCoreID() ]->xGenericListItem ) ) == ( UBaseType_t ) 0 )
1398                                 {
1399                                         /* The current task must be in a ready list, so there is
1400                                         no need to check, and the port reset macro can be called
1401                                         directly. */
1402                                         portRESET_READY_PRIORITY( pxCurrentTCB[ xPortGetCoreID() ]->uxPriority, uxTopReadyPriority );
1403                                 }
1404                                 else
1405                                 {
1406                                         mtCOVERAGE_TEST_MARKER();
1407                                 }
1408
1409                                 prvAddCurrentTaskToDelayedList( xPortGetCoreID(), xTimeToWake );
1410                         }
1411                         else
1412                         {
1413                                 mtCOVERAGE_TEST_MARKER();
1414                         }
1415                 }
1416 //              xAlreadyYielded = xTaskResumeAll();
1417                 taskEXIT_CRITICAL(&xTaskQueueMutex);
1418
1419                 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1420                 have put ourselves to sleep. */
1421                 if( xAlreadyYielded == pdFALSE )
1422                 {
1423                         portYIELD_WITHIN_API();
1424                 }
1425                 else
1426                 {
1427                         mtCOVERAGE_TEST_MARKER();
1428                 }
1429         }
1430
1431 #endif /* INCLUDE_vTaskDelayUntil */
1432 /*-----------------------------------------------------------*/
1433
1434 #if ( INCLUDE_vTaskDelay == 1 )
1435         void vTaskDelay( const TickType_t xTicksToDelay )
1436         {
1437         TickType_t xTimeToWake;
1438         BaseType_t xAlreadyYielded = pdFALSE;
1439
1440                 /* A delay time of zero just forces a reschedule. */
1441                 if( xTicksToDelay > ( TickType_t ) 0U )
1442                 {
1443                         configASSERT( uxSchedulerSuspended[ xPortGetCoreID() ] == 0 );
1444                         taskENTER_CRITICAL(&xTaskQueueMutex);
1445 //                      vTaskSuspendAll();
1446                         {
1447                                 traceTASK_DELAY();
1448
1449                                 /* A task that is removed from the event list while the
1450                                 scheduler is suspended will not get placed in the ready
1451                                 list or removed from the blocked list until the scheduler
1452                                 is resumed.
1453
1454                                 This task cannot be in an event list as it is the currently
1455                                 executing task. */
1456
1457                                 /* Calculate the time to wake - this may overflow but this is
1458                                 not a problem. */
1459 //                              portTICK_TYPE_ENTER_CRITICAL( &xTickCountMutex );
1460                                 xTimeToWake = xTickCount + xTicksToDelay;
1461 //                              portTICK_TYPE_EXIT_CRITICAL( &xTickCountMutex );
1462
1463                                 /* We must remove ourselves from the ready list before adding
1464                                 ourselves to the blocked list as the same list item is used for
1465                                 both lists. */
1466                                 if( uxListRemove( &( pxCurrentTCB[ xPortGetCoreID() ]->xGenericListItem ) ) == ( UBaseType_t ) 0 )
1467                                 {
1468                                         /* The current task must be in a ready list, so there is
1469                                         no need to check, and the port reset macro can be called
1470                                         directly. */
1471                                         portRESET_READY_PRIORITY( pxCurrentTCB[ xPortGetCoreID() ]->uxPriority, uxTopReadyPriority );
1472                                 }
1473                                 else
1474                                 {
1475                                         mtCOVERAGE_TEST_MARKER();
1476                                 }
1477                                 prvAddCurrentTaskToDelayedList( xPortGetCoreID(), xTimeToWake );
1478                         }
1479 //                      xAlreadyYielded = xTaskResumeAll();
1480                         taskEXIT_CRITICAL(&xTaskQueueMutex);
1481                 }
1482                 else
1483                 {
1484                         mtCOVERAGE_TEST_MARKER();
1485                 }
1486
1487                 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1488                 have put ourselves to sleep. */
1489                 if( xAlreadyYielded == pdFALSE )
1490                 {
1491                         portYIELD_WITHIN_API();
1492                 }
1493                 else
1494                 {
1495                         mtCOVERAGE_TEST_MARKER();
1496                 }
1497         }
1498
1499 #endif /* INCLUDE_vTaskDelay */
1500 /*-----------------------------------------------------------*/
1501
1502 #if ( INCLUDE_eTaskGetState == 1 )
1503         eTaskState eTaskGetState( TaskHandle_t xTask )
1504         {
1505         eTaskState eReturn;
1506         List_t *pxStateList;
1507         const TCB_t * const pxTCB = ( TCB_t * ) xTask;
1508                 TCB_t * curTCBcurCore = xTaskGetCurrentTaskHandle();
1509                 TCB_t * curTCBothrCore = xTaskGetCurrentTaskHandleForCPU(!xPortGetCoreID());    //Returns NULL if Unicore
1510
1511                 configASSERT( pxTCB );
1512
1513                 if( pxTCB == curTCBcurCore || pxTCB == curTCBothrCore )
1514                 {
1515                         /* The task calling this function is querying its own state. */
1516                         eReturn = eRunning;
1517                 }
1518                 else
1519                 {
1520                         taskENTER_CRITICAL(&xTaskQueueMutex);
1521                         {
1522                                 pxStateList = ( List_t * ) listLIST_ITEM_CONTAINER( &( pxTCB->xGenericListItem ) );
1523                         }
1524                         taskEXIT_CRITICAL(&xTaskQueueMutex);
1525
1526                         if( ( pxStateList == pxDelayedTaskList ) || ( pxStateList == pxOverflowDelayedTaskList ) )
1527                         {
1528                                 /* The task being queried is referenced from one of the Blocked
1529                                 lists. */
1530                                 eReturn = eBlocked;
1531                         }
1532
1533                         #if ( INCLUDE_vTaskSuspend == 1 )
1534                                 else if( pxStateList == &xSuspendedTaskList )
1535                                 {
1536                                         /* The task being queried is referenced from the suspended
1537                                         list.  Is it genuinely suspended or is it block
1538                                         indefinitely? */
1539                                         if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
1540                                         {
1541                                                 eReturn = eSuspended;
1542                                         }
1543                                         else
1544                                         {
1545                                                 eReturn = eBlocked;
1546                                         }
1547                                 }
1548                         #endif
1549
1550                         #if ( INCLUDE_vTaskDelete == 1 )
1551                                 else if( pxStateList == &xTasksWaitingTermination )
1552                                 {
1553                                         /* The task being queried is referenced from the deleted
1554                                         tasks list. */
1555                                         eReturn = eDeleted;
1556                                 }
1557                         #endif
1558
1559                         else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
1560                         {
1561                                 /* If the task is not in any other state, it must be in the
1562                                 Ready (including pending ready) state. */
1563                                 eReturn = eReady;
1564                         }
1565                 }
1566
1567                 return eReturn;
1568         } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
1569
1570 #endif /* INCLUDE_eTaskGetState */
1571 /*-----------------------------------------------------------*/
1572
1573 #if ( INCLUDE_uxTaskPriorityGet == 1 )
1574         UBaseType_t uxTaskPriorityGet( TaskHandle_t xTask )
1575         {
1576         TCB_t *pxTCB;
1577         UBaseType_t uxReturn;
1578
1579                 taskENTER_CRITICAL(&xTaskQueueMutex);
1580                 {
1581                         /* If null is passed in here then we are changing the
1582                         priority of the calling function. */
1583                         pxTCB = prvGetTCBFromHandle( xTask );
1584                         uxReturn = pxTCB->uxPriority;
1585                 }
1586                 taskEXIT_CRITICAL(&xTaskQueueMutex);
1587
1588                 return uxReturn;
1589         }
1590
1591 #endif /* INCLUDE_uxTaskPriorityGet */
1592 /*-----------------------------------------------------------*/
1593
1594 #if ( INCLUDE_uxTaskPriorityGet == 1 )
1595         UBaseType_t uxTaskPriorityGetFromISR( TaskHandle_t xTask )
1596         {
1597         TCB_t *pxTCB;
1598         UBaseType_t uxReturn;
1599
1600                 taskENTER_CRITICAL_ISR(&xTaskQueueMutex);
1601                 {
1602                         /* If null is passed in here then it is the priority of the calling
1603                         task that is being queried. */
1604                         pxTCB = prvGetTCBFromHandle( xTask );
1605                         uxReturn = pxTCB->uxPriority;
1606                 }
1607                 taskEXIT_CRITICAL_ISR(&xTaskQueueMutex);
1608
1609                 return uxReturn;
1610         }
1611
1612 #endif /* INCLUDE_uxTaskPriorityGet */
1613 /*-----------------------------------------------------------*/
1614
1615 #if ( INCLUDE_vTaskPrioritySet == 1 )
1616
1617         void vTaskPrioritySet( TaskHandle_t xTask, UBaseType_t uxNewPriority )
1618         {
1619         TCB_t *pxTCB;
1620         UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
1621         BaseType_t xYieldRequired = pdFALSE;
1622
1623                 configASSERT( ( uxNewPriority < configMAX_PRIORITIES ) );
1624
1625                 /* Ensure the new priority is valid. */
1626                 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1627                 {
1628                         uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1629                 }
1630                 else
1631                 {
1632                         mtCOVERAGE_TEST_MARKER();
1633                 }
1634
1635                 taskENTER_CRITICAL(&xTaskQueueMutex);
1636                 {
1637                         /* If null is passed in here then it is the priority of the calling
1638                         task that is being changed. */
1639                         pxTCB = prvGetTCBFromHandle( xTask );
1640
1641                         traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
1642
1643                         #if ( configUSE_MUTEXES == 1 )
1644                         {
1645                                 uxCurrentBasePriority = pxTCB->uxBasePriority;
1646                         }
1647                         #else
1648                         {
1649                                 uxCurrentBasePriority = pxTCB->uxPriority;
1650                         }
1651                         #endif
1652
1653                         if( uxCurrentBasePriority != uxNewPriority )
1654                         {
1655                                 /* The priority change may have readied a task of higher
1656                                 priority than the calling task. */
1657                                 if( uxNewPriority > uxCurrentBasePriority )
1658                                 {
1659                                         if( pxTCB != pxCurrentTCB[ xPortGetCoreID() ] )
1660                                         {
1661                                                 /* The priority of a task other than the currently
1662                                                 running task is being raised.  Is the priority being
1663                                                 raised above that of the running task? */
1664                                                 if ( tskCAN_RUN_HERE(pxTCB->xCoreID) && uxNewPriority >= pxCurrentTCB[ xPortGetCoreID() ]->uxPriority )
1665                                                 {
1666                                                         xYieldRequired = pdTRUE;
1667                                                 }
1668                                                 else if ( pxTCB->xCoreID != xPortGetCoreID() )
1669                                                 {
1670                                                         taskYIELD_OTHER_CORE( pxTCB->xCoreID, uxNewPriority );
1671                                                 }
1672                                                 else
1673                                                 {
1674                                                         mtCOVERAGE_TEST_MARKER();
1675                                                 }
1676                                         }
1677                                         else
1678                                         {
1679                                                 /* The priority of the running task is being raised,
1680                                                 but the running task must already be the highest
1681                                                 priority task able to run so no yield is required. */
1682                                         }
1683                                 }
1684                                 else if( pxTCB == pxCurrentTCB[ xPortGetCoreID() ] )
1685                                 {
1686                                         /* Setting the priority of the running task down means
1687                                         there may now be another task of higher priority that
1688                                         is ready to execute. */
1689                                         xYieldRequired = pdTRUE;
1690                                 }
1691                                 else
1692                                 {
1693                                         /* Setting the priority of any other task down does not
1694                                         require a yield as the running task must be above the
1695                                         new priority of the task being modified. */
1696                                 }
1697
1698                                 /* Remember the ready list the task might be referenced from
1699                                 before its uxPriority member is changed so the
1700                                 taskRESET_READY_PRIORITY() macro can function correctly. */
1701                                 uxPriorityUsedOnEntry = pxTCB->uxPriority;
1702
1703                                 #if ( configUSE_MUTEXES == 1 )
1704                                 {
1705                                         /* Only change the priority being used if the task is not
1706                                         currently using an inherited priority. */
1707                                         if( pxTCB->uxBasePriority == pxTCB->uxPriority )
1708                                         {
1709                                                 pxTCB->uxPriority = uxNewPriority;
1710                                         }
1711                                         else
1712                                         {
1713                                                 mtCOVERAGE_TEST_MARKER();
1714                                         }
1715
1716                                         /* The base priority gets set whatever. */
1717                                         pxTCB->uxBasePriority = uxNewPriority;
1718                                 }
1719                                 #else
1720                                 {
1721                                         pxTCB->uxPriority = uxNewPriority;
1722                                 }
1723                                 #endif
1724
1725                                 /* Only reset the event list item value if the value is not
1726                                 being used for anything else. */
1727                                 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
1728                                 {
1729                                         listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxNewPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
1730                                 }
1731                                 else
1732                                 {
1733                                         mtCOVERAGE_TEST_MARKER();
1734                                 }
1735
1736                                 /* If the task is in the blocked or suspended list we need do
1737                                 nothing more than change it's priority variable. However, if
1738                                 the task is in a ready list it needs to be removed and placed
1739                                 in the list appropriate to its new priority. */
1740                                 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xGenericListItem ) ) != pdFALSE )
1741                                 {
1742                                         /* The task is currently in its ready list - remove before adding
1743                                         it to it's new ready list.  As we are in a critical section we
1744                                         can do this even if the scheduler is suspended. */
1745                                         if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
1746                                         {
1747                                                 /* It is known that the task is in its ready list so
1748                                                 there is no need to check again and the port level
1749                                                 reset macro can be called directly. */
1750                                                 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
1751                                         }
1752                                         else
1753                                         {
1754                                                 mtCOVERAGE_TEST_MARKER();
1755                                         }
1756                     prvReaddTaskToReadyList( pxTCB );
1757                                 }
1758                                 else
1759                                 {
1760                                         mtCOVERAGE_TEST_MARKER();
1761                                 }
1762
1763                                 if( xYieldRequired == pdTRUE )
1764                                 {
1765                                         taskYIELD_IF_USING_PREEMPTION();
1766                                 }
1767                                 else
1768                                 {
1769                                         mtCOVERAGE_TEST_MARKER();
1770                                 }
1771
1772                                 /* Remove compiler warning about unused variables when the port
1773                                 optimised task selection is not being used. */
1774                                 ( void ) uxPriorityUsedOnEntry;
1775                         }
1776                 }
1777                 taskEXIT_CRITICAL(&xTaskQueueMutex);
1778         }
1779
1780 #endif /* INCLUDE_vTaskPrioritySet */
1781 /*-----------------------------------------------------------*/
1782
1783 #if ( INCLUDE_vTaskSuspend == 1 )
1784         void vTaskSuspend( TaskHandle_t xTaskToSuspend )
1785         {
1786         TCB_t *pxTCB;
1787         TCB_t *curTCB;
1788
1789                 taskENTER_CRITICAL(&xTaskQueueMutex);
1790                 {
1791                         /* If null is passed in here then it is the running task that is
1792                         being suspended. */
1793                         pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
1794
1795                         traceTASK_SUSPEND( pxTCB );
1796
1797                         /* Remove task from the ready/delayed list and place in the
1798                         suspended list. */
1799                         if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
1800                         {
1801                                 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1802                         }
1803                         else
1804                         {
1805                                 mtCOVERAGE_TEST_MARKER();
1806                         }
1807
1808                         /* Is the task waiting on an event also? */
1809                         if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1810                         {
1811                                 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1812                         }
1813                         else
1814                         {
1815                                 mtCOVERAGE_TEST_MARKER();
1816                         }
1817             traceMOVED_TASK_TO_SUSPENDED_LIST(pxTCB);
1818                         vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) );
1819                         curTCB = pxCurrentTCB[ xPortGetCoreID() ];
1820                 }
1821                 taskEXIT_CRITICAL(&xTaskQueueMutex);
1822
1823                 if( pxTCB == curTCB )
1824                 {
1825                         if( xSchedulerRunning != pdFALSE )
1826                         {
1827                                 /* The current task has just been suspended. */
1828                                 configASSERT( uxSchedulerSuspended[ xPortGetCoreID() ] == 0 );
1829                                 portYIELD_WITHIN_API();
1830                         }
1831                         else
1832                         {
1833                                 /* The scheduler is not running, but the task that was pointed
1834                                 to by pxCurrentTCB has just been suspended and pxCurrentTCB
1835                                 must be adjusted to point to a different task. */
1836                                 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks )
1837                                 {
1838                                         /* No other tasks are ready, so set pxCurrentTCB back to
1839                                         NULL so when the next task is created pxCurrentTCB will
1840                                         be set to point to it no matter what its relative priority
1841                                         is. */
1842                                         taskENTER_CRITICAL(&xTaskQueueMutex);
1843                                         pxCurrentTCB[ xPortGetCoreID() ] = NULL;
1844                                         taskEXIT_CRITICAL(&xTaskQueueMutex);
1845                                 }
1846                                 else
1847                                 {
1848                                         vTaskSwitchContext();
1849                                 }
1850                         }
1851                 }
1852                 else
1853                 {
1854                         if( xSchedulerRunning != pdFALSE )
1855                         {
1856                                 /* A task other than the currently running task was suspended,
1857                                 reset the next expected unblock time in case it referred to the
1858                                 task that is now in the Suspended state. */
1859                                 taskENTER_CRITICAL(&xTaskQueueMutex);
1860                                 {
1861                                         prvResetNextTaskUnblockTime();
1862                                 }
1863                                 taskEXIT_CRITICAL(&xTaskQueueMutex);
1864                         }
1865                         else
1866                         {
1867                                 mtCOVERAGE_TEST_MARKER();
1868                         }
1869                 }
1870         }
1871
1872 #endif /* INCLUDE_vTaskSuspend */
1873 /*-----------------------------------------------------------*/
1874
1875 #if ( INCLUDE_vTaskSuspend == 1 )
1876         static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
1877         {
1878         BaseType_t xReturn = pdFALSE;
1879         const TCB_t * const pxTCB = ( TCB_t * ) xTask;
1880
1881                 /* Accesses xPendingReadyList so must be called from a critical
1882                    section (caller is required to hold xTaskQueueMutex). */
1883
1884                 /* It does not make sense to check if the calling task is suspended. */
1885                 configASSERT( xTask );
1886
1887                 /* Is the task being resumed actually in the suspended list? */
1888                 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xGenericListItem ) ) != pdFALSE )
1889                 {
1890                         /* Has the task already been resumed from within an ISR? */
1891                         if( listIS_CONTAINED_WITHIN( &xPendingReadyList[ xPortGetCoreID() ], &( pxTCB->xEventListItem ) ) == pdFALSE )
1892                         {
1893                                 /* Is it in the suspended list because it is in the     Suspended
1894                                 state, or because is is blocked with no timeout? */
1895                                 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE )
1896                                 {
1897                                         xReturn = pdTRUE;
1898                                 }
1899                                 else
1900                                 {
1901                                         mtCOVERAGE_TEST_MARKER();
1902                                 }
1903                         }
1904                         else
1905                         {
1906                                 mtCOVERAGE_TEST_MARKER();
1907                         }
1908                 }
1909                 else
1910                 {
1911                         mtCOVERAGE_TEST_MARKER();
1912                 }
1913
1914                 return xReturn;
1915         } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
1916
1917 #endif /* INCLUDE_vTaskSuspend */
1918 /*-----------------------------------------------------------*/
1919
1920 #if ( INCLUDE_vTaskSuspend == 1 )
1921
1922         void vTaskResume( TaskHandle_t xTaskToResume )
1923         {
1924         TCB_t * const pxTCB = ( TCB_t * ) xTaskToResume;
1925
1926                 /* It does not make sense to resume the calling task. */
1927                 configASSERT( xTaskToResume );
1928
1929                 taskENTER_CRITICAL(&xTaskQueueMutex);
1930                 /* The parameter cannot be NULL as it is impossible to resume the
1931                 currently executing task. */
1932                 if( ( pxTCB != NULL ) && ( pxTCB != pxCurrentTCB[ xPortGetCoreID() ] ) )
1933                 {
1934                         {
1935                                 if( prvTaskIsTaskSuspended( pxTCB ) == pdTRUE )
1936                                 {
1937                                         traceTASK_RESUME( pxTCB );
1938
1939                                         /* As we are in a critical section we can access the ready
1940                                         lists even if the scheduler is suspended. */
1941                                         ( void ) uxListRemove(  &( pxTCB->xGenericListItem ) );
1942                                         prvAddTaskToReadyList( pxTCB );
1943
1944                                         /* We may have just resumed a higher priority task. */
1945                                         if( tskCAN_RUN_HERE(pxTCB->xCoreID) && pxTCB->uxPriority >= pxCurrentTCB[ xPortGetCoreID() ]->uxPriority )
1946                                         {
1947                                                 /* This yield may not cause the task just resumed to run,
1948                                                 but will leave the lists in the correct state for the
1949                                                 next yield. */
1950                                                 taskYIELD_IF_USING_PREEMPTION();
1951                                         }
1952                                         else if( pxTCB->xCoreID != xPortGetCoreID() )
1953                                         {
1954                                                 taskYIELD_OTHER_CORE( pxTCB->xCoreID, pxTCB->uxPriority );
1955                                         }
1956                                         else
1957                                         {
1958                                                 mtCOVERAGE_TEST_MARKER();
1959                                         }
1960                                 }
1961                                 else
1962                                 {
1963                                         mtCOVERAGE_TEST_MARKER();
1964                                 }
1965                         }
1966                 }
1967                 else
1968                 {
1969                         mtCOVERAGE_TEST_MARKER();
1970                 }
1971                 taskEXIT_CRITICAL(&xTaskQueueMutex);
1972         }
1973
1974 #endif /* INCLUDE_vTaskSuspend */
1975
1976 /*-----------------------------------------------------------*/
1977
1978 #if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
1979
1980         BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
1981         {
1982         BaseType_t xYieldRequired = pdFALSE;
1983         TCB_t * const pxTCB = ( TCB_t * ) xTaskToResume;
1984
1985                 configASSERT( xTaskToResume );
1986
1987                 taskENTER_CRITICAL_ISR(&xTaskQueueMutex);
1988
1989                 {
1990                         if( prvTaskIsTaskSuspended( pxTCB ) == pdTRUE )
1991                         {
1992                                 traceTASK_RESUME_FROM_ISR( pxTCB );
1993
1994                                 /* Check the ready lists can be accessed. */
1995                                 if( uxSchedulerSuspended[ xPortGetCoreID() ] == ( UBaseType_t ) pdFALSE )
1996                                 {
1997                                         /* Ready lists can be accessed so move the task from the
1998                                         suspended list to the ready list directly. */
1999                                         ( void ) uxListRemove(  &( pxTCB->xGenericListItem ) );
2000                                         prvAddTaskToReadyList( pxTCB );
2001
2002                                         if( tskCAN_RUN_HERE( pxTCB->xCoreID ) && pxTCB->uxPriority >= pxCurrentTCB[ xPortGetCoreID() ]->uxPriority )
2003                                         {
2004                                                 xYieldRequired = pdTRUE;
2005                                         }
2006                                         else if ( pxTCB->xCoreID != xPortGetCoreID() )
2007                                         {
2008                                                 taskYIELD_OTHER_CORE( pxTCB->xCoreID, pxTCB->uxPriority);
2009                                         }
2010                                         else
2011                                         {
2012                                                 mtCOVERAGE_TEST_MARKER();
2013                                         }
2014                                 }
2015                                 else
2016                                 {
2017                                         /* The delayed or ready lists cannot be accessed so the task
2018                                         is held in the pending ready list until the scheduler is
2019                                         unsuspended. */
2020                                         vListInsertEnd( &( xPendingReadyList[ xPortGetCoreID() ] ), &( pxTCB->xEventListItem ) );
2021                                 }
2022                         }
2023                         else
2024                         {
2025                                 mtCOVERAGE_TEST_MARKER();
2026                         }
2027                 }
2028                 taskEXIT_CRITICAL_ISR(&xTaskQueueMutex);
2029
2030                 return xYieldRequired;
2031         }
2032
2033 #endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
2034 /*-----------------------------------------------------------*/
2035
2036 void vTaskStartScheduler( void )
2037 {
2038 BaseType_t xReturn;
2039 BaseType_t i;
2040
2041         /* Add the per-core idle tasks at the lowest priority. */
2042         for ( i=0; i<portNUM_PROCESSORS; i++) {
2043                 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
2044                 {
2045                         /* Create the idle task, storing its handle in xIdleTaskHandle so it can
2046                         be returned by the xTaskGetIdleTaskHandle() function. */
2047                         xReturn = xTaskCreatePinnedToCore( prvIdleTask, "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), &xIdleTaskHandle[i], i ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2048                 }
2049                 #else
2050                 {
2051                         /* Create the idle task without storing its handle. */
2052                         xReturn = xTaskCreatePinnedToCore( prvIdleTask, "IDLE", tskIDLE_STACK_SIZE, ( void * ) NULL, ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), NULL, i);  /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2053                 }
2054                 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
2055         }
2056
2057         #if ( configUSE_TIMERS == 1 )
2058         {
2059                 if( xReturn == pdPASS )
2060                 {
2061                         xReturn = xTimerCreateTimerTask();
2062                 }
2063                 else
2064                 {
2065                         mtCOVERAGE_TEST_MARKER();
2066                 }
2067         }
2068         #endif /* configUSE_TIMERS */
2069
2070         if( xReturn == pdPASS )
2071         {
2072                 /* Interrupts are turned off here, to ensure a tick does not occur
2073                 before or during the call to xPortStartScheduler().  The stacks of
2074                 the created tasks contain a status word with interrupts switched on
2075                 so interrupts will automatically get re-enabled when the first task
2076                 starts to run. */
2077                 portDISABLE_INTERRUPTS();
2078
2079
2080                 xTickCount = ( TickType_t ) 0U;
2081
2082                 /* If configGENERATE_RUN_TIME_STATS is defined then the following
2083                 macro must be defined to configure the timer/counter used to generate
2084                 the run time counter time base. */
2085                 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
2086                 xSchedulerRunning = pdTRUE;
2087
2088                 /* Setting up the timer tick is hardware specific and thus in the
2089                 portable interface. */
2090                 if( xPortStartScheduler() != pdFALSE )
2091                 {
2092                         /* Should not reach here as if the scheduler is running the
2093                         function will not return. */
2094                 }
2095                 else
2096                 {
2097                         /* Should only reach here if a task calls xTaskEndScheduler(). */
2098                 }
2099         }
2100         else
2101         {
2102                 /* This line will only be reached if the kernel could not be started,
2103                 because there was not enough FreeRTOS heap to create the idle task
2104                 or the timer task. */
2105                 configASSERT( xReturn );
2106         }
2107 }
2108 /*-----------------------------------------------------------*/
2109
2110 void vTaskEndScheduler( void )
2111 {
2112         /* Stop the scheduler interrupts and call the portable scheduler end
2113         routine so the original ISRs can be restored if necessary.  The port
2114         layer must ensure interrupts enable     bit is left in the correct state. */
2115         portDISABLE_INTERRUPTS();
2116         xSchedulerRunning = pdFALSE;
2117         vPortEndScheduler();
2118 }
2119 /*----------------------------------------------------------*/
2120
2121
2122 #if ( configUSE_NEWLIB_REENTRANT == 1 )
2123 //Return global reent struct if FreeRTOS isn't running,
2124 struct _reent* __getreent() {
2125         //No lock needed because if this changes, we won't be running anymore.
2126         TCB_t *currTask=xTaskGetCurrentTaskHandle();
2127         if (currTask==NULL) {
2128                 //No task running. Return global struct.
2129                 return _GLOBAL_REENT;
2130         } else {
2131                 //We have a task; return its reentrant struct.
2132                 return &currTask->xNewLib_reent;
2133         }
2134 }
2135 #endif
2136
2137
2138 void vTaskSuspendAll( void )
2139 {
2140         /* A critical section is not required as the variable is of type
2141         BaseType_t.  Please read Richard Barry's reply in the following link to a
2142         post in the FreeRTOS support forum before reporting this as a bug! -
2143         http://goo.gl/wu4acr */
2144         unsigned state;
2145
2146         state = portENTER_CRITICAL_NESTED();
2147         ++uxSchedulerSuspended[ xPortGetCoreID() ];
2148         portEXIT_CRITICAL_NESTED(state);
2149 }
2150 /*----------------------------------------------------------*/
2151
2152 #if ( configUSE_TICKLESS_IDLE != 0 )
2153
2154         static BaseType_t xHaveReadyTasks()
2155         {
2156                 for (int i = tskIDLE_PRIORITY + 1; i < configMAX_PRIORITIES; ++i)
2157                 {
2158                         if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ i ] ) ) > 0 )
2159                         {
2160                                 return pdTRUE;
2161                         }
2162                         else
2163                         {
2164                                 mtCOVERAGE_TEST_MARKER();
2165                         }
2166                 }
2167                 return pdFALSE;
2168         }
2169
2170
2171         static TickType_t prvGetExpectedIdleTime( void )
2172         {
2173         TickType_t xReturn;
2174
2175
2176                 taskENTER_CRITICAL(&xTaskQueueMutex);
2177                 if( pxCurrentTCB[ xPortGetCoreID() ]->uxPriority > tskIDLE_PRIORITY )
2178                 {
2179                         xReturn = 0;
2180                 }
2181 #if portNUM_PROCESSORS > 1
2182                 /* This function is called from Idle task; in single core case this
2183                  * means that no higher priority tasks are ready to run, and we can
2184                  * enter sleep. In SMP case, there might be ready tasks waiting for
2185                  * the other CPU, so need to check all ready lists.
2186                  */
2187                 else if( xHaveReadyTasks() )
2188                 {
2189                         xReturn = 0;
2190                 }
2191 #endif // portNUM_PROCESSORS > 1
2192                 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > portNUM_PROCESSORS )
2193                 {
2194                         /* There are other idle priority tasks in the ready state.  If
2195                         time slicing is used then the very next tick interrupt must be
2196                         processed. */
2197                         xReturn = 0;
2198                 }
2199                 else
2200                 {
2201                         portTICK_TYPE_ENTER_CRITICAL( &xTickCountMutex );
2202                         xReturn = xNextTaskUnblockTime - xTickCount;
2203                         portTICK_TYPE_EXIT_CRITICAL( &xTickCountMutex );
2204                 }
2205                 taskEXIT_CRITICAL(&xTaskQueueMutex);
2206
2207                 return xReturn;
2208         }
2209
2210 #endif /* configUSE_TICKLESS_IDLE */
2211 /*----------------------------------------------------------*/
2212
2213 BaseType_t xTaskResumeAll( void )
2214 {
2215 TCB_t *pxTCB;
2216 BaseType_t xAlreadyYielded = pdFALSE;
2217
2218         /* If uxSchedulerSuspended[ xPortGetCoreID() ] is zero then this function does not match a
2219         previous call to vTaskSuspendAll(). */
2220         configASSERT( uxSchedulerSuspended[ xPortGetCoreID() ] );
2221         /* It is possible that an ISR caused a task to be removed from an event
2222         list while the scheduler was suspended.  If this was the case then the
2223         removed task will have been added to the xPendingReadyList.  Once the
2224         scheduler has been resumed it is safe to move all the pending ready
2225         tasks from this list into their appropriate ready list. */
2226
2227         taskENTER_CRITICAL(&xTaskQueueMutex);
2228         {
2229                 --uxSchedulerSuspended[ xPortGetCoreID() ];
2230
2231                 if( uxSchedulerSuspended[ xPortGetCoreID() ] == ( UBaseType_t ) pdFALSE )
2232                 {
2233                         if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
2234                         {
2235                                 /* Move any readied tasks from the pending list into the
2236                                 appropriate ready list. */
2237                                 while( listLIST_IS_EMPTY( &xPendingReadyList[ xPortGetCoreID() ] ) == pdFALSE )
2238                                 {
2239                                         pxTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList[ xPortGetCoreID() ] ) );
2240                                         ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2241                                         ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );
2242                                         prvAddTaskToReadyList( pxTCB );
2243
2244                                         /* If the moved task has a priority higher than the current
2245                                         task then a yield must be performed. */
2246                                         if ( tskCAN_RUN_HERE(pxTCB->xCoreID) && pxTCB->uxPriority >= pxCurrentTCB[ xPortGetCoreID() ]->uxPriority )
2247                                         {
2248                                                 /* We can schedule the awoken task on this CPU. */
2249                                                 xYieldPending[xPortGetCoreID()] = pdTRUE;
2250                                         }
2251                                         else
2252                                         {
2253                                                 mtCOVERAGE_TEST_MARKER();
2254                                         }
2255                                 }
2256
2257                                 /* If any ticks occurred while the scheduler was suspended then
2258                                 they should be processed now.  This ensures the tick count does
2259                                 not     slip, and that any delayed tasks are resumed at the correct
2260                                 time. */
2261                                 if( uxPendedTicks > ( UBaseType_t ) 0U )
2262                                 {
2263                                         while( uxPendedTicks > ( UBaseType_t ) 0U )
2264                                         {
2265                                                 if( xTaskIncrementTick() != pdFALSE )
2266                                                 {
2267                                                         xYieldPending[ xPortGetCoreID() ] = pdTRUE;
2268                                                 }
2269                                                 else
2270                                                 {
2271                                                         mtCOVERAGE_TEST_MARKER();
2272                                                 }
2273                                                 --uxPendedTicks;
2274                                         }
2275                                 }
2276                                 else
2277                                 {
2278                                         mtCOVERAGE_TEST_MARKER();
2279                                 }
2280
2281                                 if( xYieldPending[ xPortGetCoreID() ] == pdTRUE )
2282                                 {
2283                                         #if( configUSE_PREEMPTION != 0 )
2284                                         {
2285                                                 xAlreadyYielded = pdTRUE;
2286                                         }
2287                                         #endif
2288                                         taskYIELD_IF_USING_PREEMPTION();
2289                                 }
2290                                 else
2291                                 {
2292                                         mtCOVERAGE_TEST_MARKER();
2293                                 }
2294                         }
2295                 }
2296                 else
2297                 {
2298                         mtCOVERAGE_TEST_MARKER();
2299                 }
2300         }
2301         taskEXIT_CRITICAL(&xTaskQueueMutex);
2302
2303         return xAlreadyYielded;
2304 }
2305 /*-----------------------------------------------------------*/
2306
2307 TickType_t xTaskGetTickCount( void )
2308 {
2309 TickType_t xTicks;
2310
2311         /* Critical section required if running on a 16 bit processor. */
2312         portTICK_TYPE_ENTER_CRITICAL( &xTickCountMutex );
2313         {
2314                 xTicks = xTickCount;
2315         }
2316         portTICK_TYPE_EXIT_CRITICAL( &xTickCountMutex );
2317
2318         return xTicks;
2319 }
2320 /*-----------------------------------------------------------*/
2321
2322 TickType_t xTaskGetTickCountFromISR( void )
2323 {
2324 TickType_t xReturn;
2325
2326         taskENTER_CRITICAL_ISR(&xTickCountMutex);
2327         {
2328                 xReturn = xTickCount;
2329 //              vPortCPUReleaseMutex( &xTickCountMutex );
2330         }
2331         taskEXIT_CRITICAL_ISR(&xTickCountMutex);
2332
2333         return xReturn;
2334 }
2335 /*-----------------------------------------------------------*/
2336
2337 UBaseType_t uxTaskGetNumberOfTasks( void )
2338 {
2339         /* A critical section is not required because the variables are of type
2340         BaseType_t. */
2341         return uxCurrentNumberOfTasks;
2342 }
2343 /*-----------------------------------------------------------*/
2344
2345 #if ( INCLUDE_pcTaskGetTaskName == 1 )
2346         char *pcTaskGetTaskName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2347         {
2348         TCB_t *pxTCB;
2349
2350                 /* If null is passed in here then the name of the calling task is being queried. */
2351                 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
2352                 configASSERT( pxTCB );
2353                 return &( pxTCB->pcTaskName[ 0 ] );
2354         }
2355
2356 #endif /* INCLUDE_pcTaskGetTaskName */
2357 /*-----------------------------------------------------------*/
2358
2359 #if ( configUSE_TRACE_FACILITY == 1 )
2360
2361         UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray, const UBaseType_t uxArraySize, uint32_t * const pulTotalRunTime )
2362         {
2363         UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
2364
2365                 taskENTER_CRITICAL(&xTaskQueueMutex);
2366                 {
2367                         /* Is there a space in the array for each task in the system? */
2368                         if( uxArraySize >= uxCurrentNumberOfTasks )
2369                         {
2370                                 /* Fill in an TaskStatus_t structure with information on each
2371                                 task in the Ready state. */
2372                                 do
2373                                 {
2374                                         uxQueue--;
2375                                         uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
2376
2377                                 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2378
2379                                 /* Fill in an TaskStatus_t structure with information on each
2380                                 task in the Blocked state. */
2381                                 uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
2382                                 uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
2383
2384                                 #if( INCLUDE_vTaskDelete == 1 )
2385                                 {
2386                                         /* Fill in an TaskStatus_t structure with information on
2387                                         each task that has been deleted but not yet cleaned up. */
2388                                         uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
2389                                 }
2390                                 #endif
2391
2392                                 #if ( INCLUDE_vTaskSuspend == 1 )
2393                                 {
2394                                         /* Fill in an TaskStatus_t structure with information on
2395                                         each task in the Suspended state. */
2396                                         uxTask += prvListTaskWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
2397                                 }
2398                                 #endif
2399
2400                                 #if ( configGENERATE_RUN_TIME_STATS == 1)
2401                                 {
2402                                         if( pulTotalRunTime != NULL )
2403                                         {
2404                                                 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
2405                                                         portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
2406                                                 #else
2407                                                         *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
2408                                                 #endif
2409                                         }
2410                                 }
2411                                 #else
2412                                 {
2413                                         if( pulTotalRunTime != NULL )
2414                                         {
2415                                                 *pulTotalRunTime = 0;
2416                                         }
2417                                 }
2418                                 #endif
2419                         }
2420                         else
2421                         {
2422                                 mtCOVERAGE_TEST_MARKER();
2423                         }
2424                 }
2425                 taskEXIT_CRITICAL(&xTaskQueueMutex);
2426                 return uxTask;
2427         }
2428
2429 #endif /* configUSE_TRACE_FACILITY */
2430 /*----------------------------------------------------------*/
2431
2432 #if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
2433
2434         TaskHandle_t xTaskGetIdleTaskHandle( void )
2435         {
2436                 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
2437                 started, then xIdleTaskHandle will be NULL. */
2438                 configASSERT( ( xIdleTaskHandle[ xPortGetCoreID() ] != NULL ) );
2439                 return xIdleTaskHandle[ xPortGetCoreID() ];
2440         }
2441
2442         TaskHandle_t xTaskGetIdleTaskHandleForCPU( UBaseType_t cpuid )
2443         {
2444             TaskHandle_t xReturn = NULL;
2445             /* If xTaskGetIdleTaskHandleForCPU() is called before the scheduler has been
2446         started, then xIdleTaskHandle will be NULL. */
2447             if (cpuid < portNUM_PROCESSORS) {
2448                 configASSERT( ( xIdleTaskHandle[ cpuid ] != NULL ) );
2449                 xReturn = xIdleTaskHandle[ cpuid ];
2450             }
2451             return xReturn;
2452         }
2453
2454 #endif /* INCLUDE_xTaskGetIdleTaskHandle */
2455 /*----------------------------------------------------------*/
2456
2457 /* This conditional compilation should use inequality to 0, not equality to 1.
2458 This is to ensure vTaskStepTick() is available when user defined low power mode
2459 implementations require configUSE_TICKLESS_IDLE to be set to a value other than
2460 1. */
2461 #if ( configUSE_TICKLESS_IDLE != 0 )
2462
2463         void vTaskStepTick( const TickType_t xTicksToJump )
2464         {
2465                 /* Correct the tick count value after a period during which the tick
2466                 was suppressed.  Note this does *not* call the tick hook function for
2467                 each stepped tick. */
2468                 portTICK_TYPE_ENTER_CRITICAL( &xTickCountMutex );
2469                 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
2470                 xTickCount += xTicksToJump;
2471                 portTICK_TYPE_EXIT_CRITICAL( &xTickCountMutex );
2472                 traceINCREASE_TICK_COUNT( xTicksToJump );
2473         }
2474
2475 #endif /* configUSE_TICKLESS_IDLE */
2476 /*----------------------------------------------------------*/
2477
2478 BaseType_t xTaskIncrementTick( void )
2479 {
2480 TCB_t * pxTCB;
2481 TickType_t xItemValue;
2482 BaseType_t xSwitchRequired = pdFALSE;
2483
2484         /* Called by the portable layer each time a tick interrupt occurs.
2485         Increments the tick then checks to see if the new tick value will cause any
2486         tasks to be unblocked. */
2487
2488         /* Only let core 0 increase the tick count, to keep accurate track of time. */
2489         /* ToDo: This doesn't really play nice with the logic below: it means when core 1 is
2490            running a low-priority task, it will keep running it until there is a context
2491            switch, even when this routine (running on core 0) unblocks a bunch of high-priority
2492            tasks... this is less than optimal -- JD. */
2493         if ( xPortGetCoreID()!=0 ) {
2494                 #if ( configUSE_TICK_HOOK == 1 )
2495                 vApplicationTickHook();
2496                 #endif /* configUSE_TICK_HOOK */
2497                 esp_vApplicationTickHook();
2498
2499                 /*
2500                   We can't really calculate what we need, that's done on core 0... just assume we need a switch.
2501                   ToDo: Make this more intelligent? -- JD
2502                 */
2503                 return pdTRUE;
2504         }
2505
2506
2507         traceTASK_INCREMENT_TICK( xTickCount );
2508
2509         if( uxSchedulerSuspended[ xPortGetCoreID() ] == ( UBaseType_t ) pdFALSE )
2510         {
2511                 portTICK_TYPE_ENTER_CRITICAL( &xTickCountMutex );
2512                 /* Increment the RTOS tick, switching the delayed and overflowed
2513                 delayed lists if it wraps to 0. */
2514                 ++xTickCount;
2515                 portTICK_TYPE_EXIT_CRITICAL( &xTickCountMutex );
2516
2517                 //The other CPU may decide to mess with the task queues, so this needs a mux.
2518                 taskENTER_CRITICAL_ISR(&xTaskQueueMutex);
2519                 {
2520                         /* Minor optimisation.  The tick count cannot change in this
2521                         block. */
2522                         const TickType_t xConstTickCount = xTickCount;
2523
2524                         if( xConstTickCount == ( TickType_t ) 0U )
2525                         {
2526                                 taskSWITCH_DELAYED_LISTS();
2527                         }
2528                         else
2529                         {
2530                                 mtCOVERAGE_TEST_MARKER();
2531                         }
2532
2533                         /* See if this tick has made a timeout expire.  Tasks are stored in
2534                         the     queue in the order of their wake time - meaning once one task
2535                         has been found whose block time has not expired there is no need to
2536                         look any further down the list. */
2537                         if( xConstTickCount >= xNextTaskUnblockTime )
2538                         {
2539                                 for( ;; )
2540                                 {
2541                                         if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
2542                                         {
2543                                                 /* The delayed list is empty.  Set xNextTaskUnblockTime
2544                                                 to the maximum possible value so it is extremely
2545                                                 unlikely that the
2546                                                 if( xTickCount >= xNextTaskUnblockTime ) test will pass
2547                                                 next time through. */
2548                                                 xNextTaskUnblockTime = portMAX_DELAY;
2549                                                 break;
2550                                         }
2551                                         else
2552                                         {
2553                                                 /* The delayed list is not empty, get the value of the
2554                                                 item at the head of the delayed list.  This is the time
2555                                                 at which the task at the head of the delayed list must
2556                                                 be removed from the Blocked state. */
2557                                                 pxTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
2558                                                 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xGenericListItem ) );
2559
2560                                                 if( xConstTickCount < xItemValue )
2561                                                 {
2562                                                         /* It is not time to unblock this item yet, but the
2563                                                         item value is the time at which the task at the head
2564                                                         of the blocked list must be removed from the Blocked
2565                                                         state - so record the item value in
2566                                                         xNextTaskUnblockTime. */
2567                                                         xNextTaskUnblockTime = xItemValue;
2568                                                         break;
2569                                                 }
2570                                                 else
2571                                                 {
2572                                                         mtCOVERAGE_TEST_MARKER();
2573                                                 }
2574
2575                                                 /* It is time to remove the item from the Blocked state. */
2576                                                 ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );
2577
2578                                                 /* Is the task waiting on an event also?  If so remove
2579                                                 it from the event list. */
2580                                                 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2581                                                 {
2582                                                         ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
2583                                                 }
2584                                                 else
2585                                                 {
2586                                                         mtCOVERAGE_TEST_MARKER();
2587                                                 }
2588
2589                                                 /* Place the unblocked task into the appropriate ready
2590                                                 list. */
2591                                                 prvAddTaskToReadyList( pxTCB );
2592
2593                                                 /* A task being unblocked cannot cause an immediate
2594                                                 context switch if preemption is turned off. */
2595                                                 #if (  configUSE_PREEMPTION == 1 )
2596                                                 {
2597                                                         /* Preemption is on, but a context switch should
2598                                                         only be performed if the unblocked task has a
2599                                                         priority that is equal to or higher than the
2600                                                         currently executing task. */
2601                                                         if( pxTCB->uxPriority >= pxCurrentTCB[ xPortGetCoreID() ]->uxPriority )
2602                                                         {
2603                                                                 xSwitchRequired = pdTRUE;
2604                                                         }
2605                                                         else
2606                                                         {
2607                                                                 mtCOVERAGE_TEST_MARKER();
2608                                                         }
2609                                                 }
2610                                                 #endif /* configUSE_PREEMPTION */
2611                                         }
2612                                 }
2613                         }
2614                 }
2615
2616                 /* Tasks of equal priority to the currently running task will share
2617                 processing time (time slice) if preemption is on, and the application
2618                 writer has not explicitly turned time slicing off. */
2619                 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
2620                 {
2621                         if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB[ xPortGetCoreID() ]->uxPriority ] ) ) > ( UBaseType_t ) 1 )
2622                         {
2623                                 xSwitchRequired = pdTRUE;
2624                         }
2625                         else
2626                         {
2627                                 mtCOVERAGE_TEST_MARKER();
2628                         }
2629                 }
2630                 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
2631
2632                 {
2633                         /* Guard against the tick hook being called when the pended tick
2634                         count is being unwound (when the scheduler is being unlocked). */
2635                         if( uxPendedTicks == ( UBaseType_t ) 0U )
2636                         {
2637                                 #if ( configUSE_TICK_HOOK == 1 )
2638                                 vApplicationTickHook();
2639                                 #endif /* configUSE_TICK_HOOK */
2640                                 esp_vApplicationTickHook();
2641                         }
2642                         else
2643                         {
2644                                 mtCOVERAGE_TEST_MARKER();
2645                         }
2646                 }
2647                 taskEXIT_CRITICAL_ISR(&xTaskQueueMutex);
2648         }
2649         else
2650         {
2651                 ++uxPendedTicks;
2652
2653                 /* The tick hook gets called at regular intervals, even if the
2654                 scheduler is locked. */
2655                 #if ( configUSE_TICK_HOOK == 1 )
2656                 {
2657                         vApplicationTickHook();
2658                 }
2659                 #endif
2660                 esp_vApplicationTickHook();
2661         }
2662
2663         #if ( configUSE_PREEMPTION == 1 )
2664         {
2665                 if( xYieldPending [ xPortGetCoreID() ] != pdFALSE )
2666                 {
2667                         xSwitchRequired = pdTRUE;
2668                 }
2669                 else
2670                 {
2671                         mtCOVERAGE_TEST_MARKER();
2672                 }
2673         }
2674         #endif /* configUSE_PREEMPTION */
2675
2676         return xSwitchRequired;
2677 }
2678 /*-----------------------------------------------------------*/
2679
2680 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
2681
2682         void vTaskSetApplicationTaskTag( TaskHandle_t xTask, TaskHookFunction_t pxHookFunction )
2683         {
2684         TCB_t *xTCB;
2685
2686                 /* If xTask is NULL then it is the task hook of the calling task that is
2687                 getting set. */
2688                 if( xTask == NULL )
2689                 {
2690                         xTCB = ( TCB_t * ) pxCurrentTCB[ xPortGetCoreID() ];
2691                 }
2692                 else
2693                 {
2694                         xTCB = ( TCB_t * ) xTask;
2695                 }
2696
2697                 /* Save the hook function in the TCB.  A critical section is required as
2698                 the value can be accessed from an interrupt. */
2699                 taskENTER_CRITICAL(&xTaskQueueMutex);
2700                         xTCB->pxTaskTag = pxHookFunction;
2701                 taskEXIT_CRITICAL(&xTaskQueueMutex);
2702         }
2703
2704 #endif /* configUSE_APPLICATION_TASK_TAG */
2705 /*-----------------------------------------------------------*/
2706
2707 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
2708
2709         TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
2710         {
2711         TCB_t *xTCB;
2712         TaskHookFunction_t xReturn;
2713
2714                 /* If xTask is NULL then we are setting our own task hook. */
2715                 if( xTask == NULL )
2716                 {
2717                         xTCB = ( TCB_t * ) xTaskGetCurrentTaskHandle();
2718                 }
2719                 else
2720                 {
2721                         xTCB = ( TCB_t * ) xTask;
2722                 }
2723
2724                 /* Save the hook function in the TCB.  A critical section is required as
2725                 the value can be accessed from an interrupt. */
2726                 taskENTER_CRITICAL(&xTaskQueueMutex);
2727                 {
2728                         xReturn = xTCB->pxTaskTag;
2729                 }
2730                 taskEXIT_CRITICAL(&xTaskQueueMutex);
2731
2732                 return xReturn;
2733         }
2734
2735 #endif /* configUSE_APPLICATION_TASK_TAG */
2736 /*-----------------------------------------------------------*/
2737
2738 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
2739
2740         BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask, void *pvParameter )
2741         {
2742         TCB_t *xTCB;
2743         BaseType_t xReturn;
2744
2745                 /* If xTask is NULL then we are calling our own task hook. */
2746                 if( xTask == NULL )
2747                 {
2748                         xTCB = ( TCB_t * ) xTaskGetCurrentTaskHandle();
2749                 }
2750                 else
2751                 {
2752                         xTCB = ( TCB_t * ) xTask;
2753                 }
2754
2755                 if( xTCB->pxTaskTag != NULL )
2756                 {
2757                         xReturn = xTCB->pxTaskTag( pvParameter );
2758                 }
2759                 else
2760                 {
2761                         xReturn = pdFAIL;
2762                 }
2763
2764                 return xReturn;
2765         }
2766
2767 #endif /* configUSE_APPLICATION_TASK_TAG */
2768 /*-----------------------------------------------------------*/
2769
2770 void vTaskSwitchContext( void )
2771 {
2772         //Theoretically, this is only called from either the tick interrupt or the crosscore interrupt, so disabling
2773         //interrupts shouldn't be necessary anymore. Still, for safety we'll leave it in for now.
2774         int irqstate=portENTER_CRITICAL_NESTED();
2775         tskTCB * pxTCB;
2776         if( uxSchedulerSuspended[ xPortGetCoreID() ] != ( UBaseType_t ) pdFALSE )
2777         {
2778                 /* The scheduler is currently suspended - do not allow a context
2779                 switch. */
2780                 xYieldPending[ xPortGetCoreID() ] = pdTRUE;
2781         }
2782         else
2783         {
2784                 xYieldPending[ xPortGetCoreID() ] = pdFALSE;
2785         xSwitchingContext[ xPortGetCoreID() ] = pdTRUE;
2786                 traceTASK_SWITCHED_OUT();
2787
2788                 #if ( configGENERATE_RUN_TIME_STATS == 1 )
2789                 {
2790                                 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
2791                                         portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
2792                                 #else
2793                                         ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
2794                                 #endif
2795
2796                                 /* Add the amount of time the task has been running to the
2797                                 accumulated time so far.  The time the task started running was
2798                                 stored in ulTaskSwitchedInTime.  Note that there is no overflow
2799                                 protection here so count values are only valid until the timer
2800                                 overflows.  The guard against negative values is to protect
2801                                 against suspect run time stat counter implementations - which
2802                                 are provided by the application, not the kernel. */
2803                                 taskENTER_CRITICAL_ISR(&xTaskQueueMutex);
2804                                 if( ulTotalRunTime > ulTaskSwitchedInTime[ xPortGetCoreID() ] )
2805                                 {
2806                                         pxCurrentTCB[ xPortGetCoreID() ]->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime[ xPortGetCoreID() ] );
2807                                 }
2808                                 else
2809                                 {
2810                                         mtCOVERAGE_TEST_MARKER();
2811                                 }
2812                                 taskEXIT_CRITICAL_ISR(&xTaskQueueMutex);
2813                                 ulTaskSwitchedInTime[ xPortGetCoreID() ] = ulTotalRunTime;
2814                 }
2815                 #endif /* configGENERATE_RUN_TIME_STATS */
2816
2817                 /* Check for stack overflow, if configured. */
2818                 taskFIRST_CHECK_FOR_STACK_OVERFLOW();
2819                 taskSECOND_CHECK_FOR_STACK_OVERFLOW();
2820
2821                 /* Select a new task to run */
2822
2823                 /*
2824                  We cannot do taskENTER_CRITICAL_ISR(&xTaskQueueMutex); here because it saves the interrupt context to the task tcb, and we're
2825                  swapping that out here. Instead, we're going to do the work here ourselves. Because interrupts are already disabled, we only
2826                  need to acquire the mutex.
2827                 */
2828 #ifdef CONFIG_FREERTOS_PORTMUX_DEBUG
2829                 vPortCPUAcquireMutex( &xTaskQueueMutex, __FUNCTION__, __LINE__ );
2830 #else
2831                 vPortCPUAcquireMutex( &xTaskQueueMutex );
2832 #endif
2833
2834                 unsigned portBASE_TYPE foundNonExecutingWaiter = pdFALSE, ableToSchedule = pdFALSE, resetListHead;
2835                 portBASE_TYPE uxDynamicTopReady = uxTopReadyPriority;
2836                 unsigned portBASE_TYPE holdTop=pdFALSE;
2837
2838                 /*
2839                  *  ToDo: This scheduler doesn't correctly implement the round-robin scheduling as done in the single-core
2840                  *  FreeRTOS stack when multiple tasks have the same priority and are all ready; it just keeps grabbing the
2841                  *  first one. ToDo: fix this.
2842                  *  (Is this still true? if any, there's the issue with one core skipping over the processes for the other
2843                  *  core, potentially not giving the skipped-over processes any time.)
2844                  */
2845
2846                 while ( ableToSchedule == pdFALSE && uxDynamicTopReady >= 0 )
2847                 {
2848                         resetListHead = pdFALSE;
2849                         // Nothing to do for empty lists
2850                         if (!listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxDynamicTopReady ] ) )) {
2851
2852                                 ableToSchedule = pdFALSE;
2853                                 tskTCB * pxRefTCB;
2854
2855                                 /* Remember the current list item so that we
2856                                 can detect if all items have been inspected.
2857                                 Once this happens, we move on to a lower
2858                                 priority list (assuming nothing is suitable
2859                                 for scheduling). Note: This can return NULL if
2860                                 the list index is at the listItem */
2861                                 pxRefTCB = pxReadyTasksLists[ uxDynamicTopReady ].pxIndex->pvOwner;
2862
2863                                 if ((void*)pxReadyTasksLists[ uxDynamicTopReady ].pxIndex==(void*)&pxReadyTasksLists[ uxDynamicTopReady ].xListEnd) {
2864                                         //pxIndex points to the list end marker. Skip that and just get the next item.
2865                                         listGET_OWNER_OF_NEXT_ENTRY( pxRefTCB, &( pxReadyTasksLists[ uxDynamicTopReady ] ) );
2866                                 }
2867
2868                                 do {
2869                                         listGET_OWNER_OF_NEXT_ENTRY( pxTCB, &( pxReadyTasksLists[ uxDynamicTopReady ] ) );
2870                                         /* Find out if the next task in the list is
2871                                         already being executed by another core */
2872                                         foundNonExecutingWaiter = pdTRUE;
2873                                         portBASE_TYPE i = 0;
2874                                         for ( i=0; i<portNUM_PROCESSORS; i++ ) {
2875                                                 if (i == xPortGetCoreID()) {
2876                                                         continue;
2877                                                 } else if (pxCurrentTCB[i] == pxTCB) {
2878                                                         holdTop=pdTRUE; //keep this as the top prio, for the other CPU
2879                                                         foundNonExecutingWaiter = pdFALSE;
2880                                                         break;
2881                                                 }
2882                                         }
2883
2884                                         if (foundNonExecutingWaiter == pdTRUE) {
2885                                                 /* If the task is not being executed
2886                                                 by another core and its affinity is
2887                                                 compatible with the current one,
2888                                                 prepare it to be swapped in */
2889                                                 if (pxTCB->xCoreID == tskNO_AFFINITY) {
2890                                                         pxCurrentTCB[xPortGetCoreID()] = pxTCB;
2891                                                         ableToSchedule = pdTRUE;
2892                                                 } else if (pxTCB->xCoreID == xPortGetCoreID()) {
2893                                                         pxCurrentTCB[xPortGetCoreID()] = pxTCB;
2894                                                         ableToSchedule = pdTRUE;
2895                                                 } else {
2896                                                         ableToSchedule = pdFALSE;
2897                                                         holdTop=pdTRUE; //keep this as the top prio, for the other CPU
2898                                                 }
2899                                         } else {
2900                                                 ableToSchedule = pdFALSE;
2901                                         }
2902
2903                                         if (ableToSchedule == pdFALSE) {
2904                                                 resetListHead = pdTRUE;
2905                                         } else if ((ableToSchedule == pdTRUE) && (resetListHead == pdTRUE)) {
2906                                                 tskTCB * pxResetTCB;
2907                                                 do {
2908                                                         listGET_OWNER_OF_NEXT_ENTRY( pxResetTCB, &( pxReadyTasksLists[ uxDynamicTopReady ] ) );
2909                                                 } while(pxResetTCB != pxRefTCB);
2910                                         }
2911                                 } while ((ableToSchedule == pdFALSE) && (pxTCB != pxRefTCB));
2912                         } else {
2913                                 if (!holdTop) --uxTopReadyPriority;
2914                         }
2915                         --uxDynamicTopReady;
2916                 }
2917
2918                 traceTASK_SWITCHED_IN();
2919         xSwitchingContext[ xPortGetCoreID() ] = pdFALSE;
2920
2921                 //Exit critical region manually as well: release the mux now, interrupts will be re-enabled when we
2922                 //exit the function.
2923 #ifdef CONFIG_FREERTOS_PORTMUX_DEBUG
2924                 vPortCPUReleaseMutex( &xTaskQueueMutex, __FUNCTION__, __LINE__ );
2925 #else
2926                 vPortCPUReleaseMutex( &xTaskQueueMutex );
2927 #endif
2928
2929 #if CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK
2930                 vPortSetStackWatchpoint(pxCurrentTCB[xPortGetCoreID()]->pxStack);
2931 #endif
2932
2933         }
2934         portEXIT_CRITICAL_NESTED(irqstate);
2935 }
2936 /*-----------------------------------------------------------*/
2937
2938 void vTaskPlaceOnEventList( List_t * const pxEventList, const TickType_t xTicksToWait )
2939 {
2940 TickType_t xTimeToWake;
2941
2942         configASSERT( pxEventList );
2943
2944         taskENTER_CRITICAL(&xTaskQueueMutex);
2945
2946         /* Place the event list item of the TCB in the appropriate event list.
2947         This is placed in the list in priority order so the highest priority task
2948         is the first to be woken by the event.  The queue that contains the event
2949         list is locked, preventing simultaneous access from interrupts. */
2950         vListInsert( pxEventList, &( pxCurrentTCB[ xPortGetCoreID() ]->xEventListItem ) );
2951
2952         /* The task must be removed from from the ready list before it is added to
2953         the blocked list as the same list item is used for both lists.  Exclusive
2954         access to the ready lists guaranteed because the scheduler is locked. */
2955         if( uxListRemove( &( pxCurrentTCB[ xPortGetCoreID() ]->xGenericListItem ) ) == ( UBaseType_t ) 0 )
2956         {
2957                 /* The current task must be in a ready list, so there is no need to
2958                 check, and the port reset macro can be called directly. */
2959                 portRESET_READY_PRIORITY( pxCurrentTCB[ xPortGetCoreID() ]->uxPriority, uxTopReadyPriority );
2960         }
2961         else
2962         {
2963                 mtCOVERAGE_TEST_MARKER();
2964         }
2965
2966         #if ( INCLUDE_vTaskSuspend == 1 )
2967         {
2968                 if( xTicksToWait == portMAX_DELAY )
2969                 {
2970                         /* Add the task to the suspended task list instead of a delayed task
2971                         list to ensure the task is not woken by a timing event.  It will
2972                         block indefinitely. */
2973             traceMOVED_TASK_TO_SUSPENDED_LIST(pxCurrentTCB);
2974                         vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB[ xPortGetCoreID() ]->xGenericListItem ) );
2975                 }
2976                 else
2977                 {
2978                         /* Calculate the time at which the task should be woken if the event
2979                         does not occur.  This may overflow but this doesn't matter, the
2980                         scheduler will handle it. */
2981                         xTimeToWake = xTickCount + xTicksToWait;
2982                         prvAddCurrentTaskToDelayedList( xPortGetCoreID(), xTimeToWake );
2983                 }
2984         }
2985         #else /* INCLUDE_vTaskSuspend */
2986         {
2987                         /* Calculate the time at which the task should be woken if the event does
2988                         not occur.  This may overflow but this doesn't matter, the scheduler
2989                         will handle it. */
2990                         xTimeToWake = xTickCount + xTicksToWait;
2991                         prvAddCurrentTaskToDelayedList( xTimeToWake );
2992         }
2993         #endif /* INCLUDE_vTaskSuspend */
2994
2995         taskEXIT_CRITICAL(&xTaskQueueMutex);
2996
2997 }
2998 /*-----------------------------------------------------------*/
2999
3000 void vTaskPlaceOnUnorderedEventList( List_t * pxEventList, const TickType_t xItemValue, const TickType_t xTicksToWait )
3001 {
3002 TickType_t xTimeToWake;
3003
3004         configASSERT( pxEventList );
3005
3006         taskENTER_CRITICAL(&xTaskQueueMutex);
3007
3008         /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED.  It is used by
3009         the event groups implementation. */
3010         configASSERT( uxSchedulerSuspended[ xPortGetCoreID() ] != 0 );
3011
3012         /* Store the item value in the event list item.  It is safe to access the
3013         event list item here as interrupts won't access the event list item of a
3014         task that is not in the Blocked state. */
3015         listSET_LIST_ITEM_VALUE( &( pxCurrentTCB[ xPortGetCoreID() ]->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
3016
3017         /* Place the event list item of the TCB at the end of the appropriate event
3018         list.  It is safe to access the event list here because it is part of an
3019         event group implementation - and interrupts don't access event groups
3020         directly (instead they access them indirectly by pending function calls to
3021         the task level). */
3022         vListInsertEnd( pxEventList, &( pxCurrentTCB[ xPortGetCoreID() ]->xEventListItem ) );
3023
3024         /* The task must be removed from the ready list before it is added to the
3025         blocked list.  Exclusive access can be assured to the ready list as the
3026         scheduler is locked. */
3027         if( uxListRemove( &( pxCurrentTCB[ xPortGetCoreID() ]->xGenericListItem ) ) == ( UBaseType_t ) 0 )
3028         {
3029                 /* The current task must be in a ready list, so there is no need to
3030                 check, and the port reset macro can be called directly. */
3031                 portRESET_READY_PRIORITY( pxCurrentTCB[ xPortGetCoreID() ]->uxPriority, uxTopReadyPriority );
3032         }
3033         else
3034         {
3035                 mtCOVERAGE_TEST_MARKER();
3036         }
3037
3038         #if ( INCLUDE_vTaskSuspend == 1 )
3039         {
3040                 if( xTicksToWait == portMAX_DELAY )
3041                 {
3042                         /* Add the task to the suspended task list instead of a delayed task
3043                         list to ensure it is not woken by a timing event.  It will block
3044                         indefinitely. */
3045                         vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB[ xPortGetCoreID() ]->xGenericListItem ) );
3046                 }
3047                 else
3048                 {
3049                         /* Calculate the time at which the task should be woken if the event
3050                         does not occur.  This may overflow but this doesn't matter, the
3051                         kernel will manage it correctly. */
3052                         xTimeToWake = xTickCount + xTicksToWait;
3053                         prvAddCurrentTaskToDelayedList( xPortGetCoreID(), xTimeToWake );
3054                 }
3055         }
3056         #else /* INCLUDE_vTaskSuspend */
3057         {
3058                         /* Calculate the time at which the task should be woken if the event does
3059                         not occur.  This may overflow but this doesn't matter, the kernel
3060                         will manage it correctly. */
3061                         xTimeToWake = xTickCount + xTicksToWait;
3062                         prvAddCurrentTaskToDelayedList( xTimeToWake );
3063         }
3064         #endif /* INCLUDE_vTaskSuspend */
3065
3066         taskEXIT_CRITICAL(&xTaskQueueMutex);
3067 }
3068 /*-----------------------------------------------------------*/
3069
3070 #if configUSE_TIMERS == 1
3071
3072         void vTaskPlaceOnEventListRestricted( List_t * const pxEventList, const TickType_t xTicksToWait )
3073         {
3074         TickType_t xTimeToWake;
3075
3076                 taskENTER_CRITICAL(&xTaskQueueMutex);
3077                 configASSERT( pxEventList );
3078
3079                 /* This function should not be called by application code hence the
3080                 'Restricted' in its name.  It is not part of the public API.  It is
3081                 designed for use by kernel code, and has special calling requirements -
3082                 it should be called from a critical section. */
3083
3084
3085                 /* Place the event list item of the TCB in the appropriate event list.
3086                 In this case it is assume that this is the only task that is going to
3087                 be waiting on this event list, so the faster vListInsertEnd() function
3088                 can be used in place of vListInsert. */
3089                 vListInsertEnd( pxEventList, &( pxCurrentTCB[ xPortGetCoreID() ]->xEventListItem ) );
3090
3091                 /* We must remove this task from the ready list before adding it to the
3092                 blocked list as the same list item is used for both lists.  This
3093                 function is called form a critical section. */
3094                 if( uxListRemove( &( pxCurrentTCB[ xPortGetCoreID() ]->xGenericListItem ) ) == ( UBaseType_t ) 0 )
3095                 {
3096                         /* The current task must be in a ready list, so there is no need to
3097                         check, and the port reset macro can be called directly. */
3098                         portRESET_READY_PRIORITY( pxCurrentTCB[ xPortGetCoreID() ]->uxPriority, uxTopReadyPriority );
3099                 }
3100                 else
3101                 {
3102                         mtCOVERAGE_TEST_MARKER();
3103                 }
3104
3105                 /* Calculate the time at which the task should be woken if the event does
3106                 not occur.  This may overflow but this doesn't matter. */
3107                 xTimeToWake = xTickCount + xTicksToWait;
3108
3109                 traceTASK_DELAY_UNTIL();
3110                 prvAddCurrentTaskToDelayedList( xPortGetCoreID(), xTimeToWake );
3111                 taskEXIT_CRITICAL(&xTaskQueueMutex);
3112
3113         }
3114
3115 #endif /* configUSE_TIMERS */
3116 /*-----------------------------------------------------------*/
3117
3118 BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
3119 {
3120 TCB_t *pxUnblockedTCB;
3121 BaseType_t xReturn;
3122 BaseType_t xTaskCanBeReady;
3123 UBaseType_t i, uxTargetCPU;
3124
3125         /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION.  It can also be
3126         called from a critical section within an ISR. */
3127         taskENTER_CRITICAL_ISR(&xTaskQueueMutex);
3128         /* The event list is sorted in priority order, so the first in the list can
3129         be removed as it is known to be the highest priority.  Remove the TCB from
3130         the delayed list, and add it to the ready list.
3131
3132         If an event is for a queue that is locked then this function will never
3133         get called - the lock count on the queue will get modified instead.  This
3134         means exclusive access to the event list is guaranteed here.
3135
3136         This function assumes that a check has already been made to ensure that
3137         pxEventList is not empty. */
3138         if ( ( listLIST_IS_EMPTY( pxEventList ) ) == pdFALSE ) {
3139                 pxUnblockedTCB = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxEventList );
3140                 configASSERT( pxUnblockedTCB );
3141                 ( void ) uxListRemove( &( pxUnblockedTCB->xEventListItem ) );
3142         } else {
3143                 taskEXIT_CRITICAL_ISR(&xTaskQueueMutex);
3144                 return pdFALSE;
3145         }
3146
3147         /* Determine if the task can possibly be run on either CPU now, either because the scheduler
3148            the task is pinned to is running or because a scheduler is running on any CPU. */
3149         xTaskCanBeReady = pdFALSE;
3150         if ( pxUnblockedTCB->xCoreID == tskNO_AFFINITY ) {
3151                 uxTargetCPU = xPortGetCoreID();
3152                 for (i = 0; i < portNUM_PROCESSORS; i++) {
3153                         if ( uxSchedulerSuspended[ i ] == ( UBaseType_t ) pdFALSE ) {
3154                                 xTaskCanBeReady = pdTRUE;
3155                                 break;
3156                         }
3157                 }
3158         } else {
3159                 uxTargetCPU = pxUnblockedTCB->xCoreID;
3160                 xTaskCanBeReady = uxSchedulerSuspended[ uxTargetCPU ] == ( UBaseType_t ) pdFALSE;
3161
3162         }
3163
3164         if( xTaskCanBeReady )
3165         {
3166                 ( void ) uxListRemove( &( pxUnblockedTCB->xGenericListItem ) );
3167                 prvAddTaskToReadyList( pxUnblockedTCB );
3168         }
3169         else
3170         {
3171                 /* The delayed and ready lists cannot be accessed, so hold this task
3172                 pending until the scheduler is resumed on this CPU. */
3173                 vListInsertEnd( &( xPendingReadyList[ uxTargetCPU ] ), &( pxUnblockedTCB->xEventListItem ) );
3174         }
3175
3176         if ( tskCAN_RUN_HERE(pxUnblockedTCB->xCoreID) && pxUnblockedTCB->uxPriority >= pxCurrentTCB[ xPortGetCoreID() ]->uxPriority )
3177         {
3178                 /* Return true if the task removed from the event list has a higher
3179                 priority than the calling task.  This allows the calling task to know if
3180                 it should force a context switch now. */
3181                 xReturn = pdTRUE;
3182
3183                 /* Mark that a yield is pending in case the user is not using the
3184                 "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
3185                 xYieldPending[ xPortGetCoreID() ] = pdTRUE;
3186         }
3187         else if ( pxUnblockedTCB->xCoreID != xPortGetCoreID() )
3188         {
3189                 taskYIELD_OTHER_CORE( pxUnblockedTCB->xCoreID, pxUnblockedTCB->uxPriority );
3190                 xReturn = pdFALSE;
3191         }
3192         else
3193         {
3194                 xReturn = pdFALSE;
3195         }
3196
3197         #if( configUSE_TICKLESS_IDLE == 1 )
3198         {
3199                 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3200                 might be set to the blocked task's time out time.  If the task is
3201                 unblocked for a reason other than a timeout xNextTaskUnblockTime is
3202                 normally left unchanged, because it is automatically get reset to a new
3203                 value when the tick count equals xNextTaskUnblockTime.  However if
3204                 tickless idling is used it might be more important to enter sleep mode
3205                 at the earliest possible time - so reset xNextTaskUnblockTime here to
3206                 ensure it is updated at the earliest possible time. */
3207                 prvResetNextTaskUnblockTime();
3208         }
3209         #endif
3210         taskEXIT_CRITICAL_ISR(&xTaskQueueMutex);
3211
3212         return xReturn;
3213 }
3214 /*-----------------------------------------------------------*/
3215
3216 BaseType_t xTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem, const TickType_t xItemValue )
3217 {
3218 TCB_t *pxUnblockedTCB;
3219 BaseType_t xReturn;
3220
3221         taskENTER_CRITICAL(&xTaskQueueMutex);
3222         /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED.  It is used by
3223         the event flags implementation. */
3224         configASSERT( uxSchedulerSuspended[ xPortGetCoreID() ] != pdFALSE );
3225
3226         /* Store the new item value in the event list. */
3227         listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
3228
3229         /* Remove the event list form the event flag.  Interrupts do not access
3230         event flags. */
3231         pxUnblockedTCB = ( TCB_t * ) listGET_LIST_ITEM_OWNER( pxEventListItem );
3232         configASSERT( pxUnblockedTCB );
3233         ( void ) uxListRemove( pxEventListItem );
3234
3235         /* Remove the task from the delayed list and add it to the ready list.  The
3236         scheduler is suspended so interrupts will not be accessing the ready
3237         lists. */
3238         ( void ) uxListRemove( &( pxUnblockedTCB->xGenericListItem ) );
3239         prvAddTaskToReadyList( pxUnblockedTCB );
3240
3241         if ( tskCAN_RUN_HERE(pxUnblockedTCB->xCoreID) && pxUnblockedTCB->uxPriority >= pxCurrentTCB[ xPortGetCoreID() ]->uxPriority )
3242         {
3243                 /* Return true if the task removed from the event list has
3244                 a higher priority than the calling task.  This allows
3245                 the calling task to know if it should force a context
3246                 switch now. */
3247                 xReturn = pdTRUE;
3248
3249                 /* Mark that a yield is pending in case the user is not using the
3250                 "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
3251                 xYieldPending[ xPortGetCoreID() ] = pdTRUE;
3252         }
3253         else if ( pxUnblockedTCB->xCoreID != xPortGetCoreID() )
3254         {
3255                 taskYIELD_OTHER_CORE( pxUnblockedTCB->xCoreID, pxUnblockedTCB->uxPriority );
3256                 xReturn = pdFALSE;
3257         }
3258         else
3259         {
3260                 xReturn = pdFALSE;
3261         }
3262
3263         taskEXIT_CRITICAL(&xTaskQueueMutex);
3264         return xReturn;
3265 }
3266 /*-----------------------------------------------------------*/
3267
3268 void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
3269 {
3270         configASSERT( pxTimeOut );
3271         pxTimeOut->xOverflowCount = xNumOfOverflows;
3272         pxTimeOut->xTimeOnEntering = xTickCount;
3273 }
3274 /*-----------------------------------------------------------*/
3275
3276 BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut, TickType_t * const pxTicksToWait )
3277 {
3278 BaseType_t xReturn;
3279
3280         configASSERT( pxTimeOut );
3281         configASSERT( pxTicksToWait );
3282
3283         taskENTER_CRITICAL(&xTickCountMutex);
3284         {
3285                 /* Minor optimisation.  The tick count cannot change in this block. */
3286                 const TickType_t xConstTickCount = xTickCount;
3287
3288                 #if ( INCLUDE_vTaskSuspend == 1 )
3289                         /* If INCLUDE_vTaskSuspend is set to 1 and the block time specified is
3290                         the maximum block time then the task should block indefinitely, and
3291                         therefore never time out. */
3292                         if( *pxTicksToWait == portMAX_DELAY )
3293                         {
3294                                 xReturn = pdFALSE;
3295                         }
3296                         else /* We are not blocking indefinitely, perform the checks below. */
3297                 #endif
3298
3299                 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
3300                 {
3301                         /* The tick count is greater than the time at which vTaskSetTimeout()
3302                         was called, but has also overflowed since vTaskSetTimeOut() was called.
3303                         It must have wrapped all the way around and gone past us again. This
3304                         passed since vTaskSetTimeout() was called. */
3305                         xReturn = pdTRUE;
3306                 }
3307                 else if( ( xConstTickCount - pxTimeOut->xTimeOnEntering ) < *pxTicksToWait )
3308                 {
3309                         /* Not a genuine timeout. Adjust parameters for time remaining. */
3310                         *pxTicksToWait -= ( xConstTickCount -  pxTimeOut->xTimeOnEntering );
3311                         vTaskSetTimeOutState( pxTimeOut );
3312                         xReturn = pdFALSE;
3313                 }
3314                 else
3315                 {
3316                         xReturn = pdTRUE;
3317                 }
3318         }
3319         taskEXIT_CRITICAL(&xTickCountMutex);
3320
3321         return xReturn;
3322 }
3323 /*-----------------------------------------------------------*/
3324
3325 void vTaskMissedYield( void )
3326 {
3327         xYieldPending[ xPortGetCoreID() ] = pdTRUE;
3328 }
3329 /*-----------------------------------------------------------*/
3330
3331 #if ( configUSE_TRACE_FACILITY == 1 )
3332
3333         UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
3334         {
3335         UBaseType_t uxReturn;
3336         TCB_t *pxTCB;
3337
3338                 if( xTask != NULL )
3339                 {
3340                         pxTCB = ( TCB_t * ) xTask;
3341                         uxReturn = pxTCB->uxTaskNumber;
3342                 }
3343                 else
3344                 {
3345                         uxReturn = 0U;
3346                 }
3347
3348                 return uxReturn;
3349         }
3350
3351 #endif /* configUSE_TRACE_FACILITY */
3352 /*-----------------------------------------------------------*/
3353
3354 #if ( configUSE_TRACE_FACILITY == 1 )
3355
3356         void vTaskSetTaskNumber( TaskHandle_t xTask, const UBaseType_t uxHandle )
3357         {
3358         TCB_t *pxTCB;
3359
3360                 if( xTask != NULL )
3361                 {
3362                         pxTCB = ( TCB_t * ) xTask;
3363                         pxTCB->uxTaskNumber = uxHandle;
3364                 }
3365         }
3366
3367 #endif /* configUSE_TRACE_FACILITY */
3368
3369 /*
3370  * -----------------------------------------------------------
3371  * The Idle task.
3372  * ----------------------------------------------------------
3373  *
3374  * The portTASK_FUNCTION() macro is used to allow port/compiler specific
3375  * language extensions.  The equivalent prototype for this function is:
3376  *
3377  * void prvIdleTask( void *pvParameters );
3378  *
3379  */
3380 static portTASK_FUNCTION( prvIdleTask, pvParameters )
3381 {
3382         /* Stop warnings. */
3383         ( void ) pvParameters;
3384
3385         for( ;; )
3386         {
3387                 /* See if any tasks have been deleted. */
3388                 prvCheckTasksWaitingTermination();
3389
3390                 #if ( configUSE_PREEMPTION == 0 )
3391                 {
3392                         /* If we are not using preemption we keep forcing a task switch to
3393                         see if any other task has become available.  If we are using
3394                         preemption we don't need to do this as any task becoming available
3395                         will automatically get the processor anyway. */
3396                         taskYIELD();
3397                 }
3398                 #endif /* configUSE_PREEMPTION */
3399
3400                 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
3401                 {
3402                         /* When using preemption tasks of equal priority will be
3403                         timesliced.  If a task that is sharing the idle priority is ready
3404                         to run then the idle task should yield before the end of the
3405                         timeslice.
3406
3407                         A critical region is not required here as we are just reading from
3408                         the list, and an occasional incorrect value will not matter.  If
3409                         the ready list at the idle priority contains more than one task
3410                         then a task other than the idle task is ready to execute. */
3411                         if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) 1 )
3412                         {
3413                                 taskYIELD();
3414                         }
3415                         else
3416                         {
3417                                 mtCOVERAGE_TEST_MARKER();
3418                         }
3419                 }
3420                 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
3421
3422                 #if ( configUSE_IDLE_HOOK == 1 )
3423                 {
3424                         extern void vApplicationIdleHook( void );
3425
3426                         /* Call the user defined function from within the idle task.  This
3427                         allows the application designer to add background functionality
3428                         without the overhead of a separate task.
3429                         NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
3430                         CALL A FUNCTION THAT MIGHT BLOCK. */
3431                         vApplicationIdleHook();
3432                 }
3433                 #endif /* configUSE_IDLE_HOOK */
3434                 {
3435                         /* Call the esp-idf hook system */
3436                         esp_vApplicationIdleHook();
3437                 }
3438
3439
3440                 /* This conditional compilation should use inequality to 0, not equality
3441                 to 1.  This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
3442                 user defined low power mode     implementations require
3443                 configUSE_TICKLESS_IDLE to be set to a value other than 1. */
3444                 #if ( configUSE_TICKLESS_IDLE != 0 )
3445                 {
3446                 TickType_t xExpectedIdleTime;
3447                 BaseType_t xEnteredSleep = pdFALSE;
3448
3449                         /* It is not desirable to suspend then resume the scheduler on
3450                         each iteration of the idle task.  Therefore, a preliminary
3451                         test of the expected idle time is performed without the
3452                         scheduler suspended.  The result here is not necessarily
3453                         valid. */
3454                         xExpectedIdleTime = prvGetExpectedIdleTime();
3455
3456                         if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
3457                         {
3458                                 taskENTER_CRITICAL(&xTaskQueueMutex);
3459                                 {
3460                                         /* Now the scheduler is suspended, the expected idle
3461                                         time can be sampled again, and this time its value can
3462                                         be used. */
3463                                         configASSERT( xNextTaskUnblockTime >= xTickCount );
3464                                         xExpectedIdleTime = prvGetExpectedIdleTime();
3465
3466                                         if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
3467                                         {
3468                                                 traceLOW_POWER_IDLE_BEGIN();
3469                                                 xEnteredSleep = portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
3470                                                 traceLOW_POWER_IDLE_END();
3471                                         }
3472                                         else
3473                                         {
3474                                                 mtCOVERAGE_TEST_MARKER();
3475                                         }
3476                                 }
3477                                 taskEXIT_CRITICAL(&xTaskQueueMutex);
3478                         }
3479                         else
3480                         {
3481                                 mtCOVERAGE_TEST_MARKER();
3482                         }
3483                         /* It might be possible to enter tickless idle again, so skip
3484                          * the fallback sleep hook if tickless idle was successful
3485                          */
3486                         if ( !xEnteredSleep )
3487                         {
3488                                 esp_vApplicationWaitiHook();
3489                         }
3490                 }
3491                 #else
3492                 esp_vApplicationWaitiHook();
3493                 #endif /* configUSE_TICKLESS_IDLE */
3494         }
3495 }
3496 /*-----------------------------------------------------------*/
3497
3498 #if configUSE_TICKLESS_IDLE != 0
3499
3500         eSleepModeStatus eTaskConfirmSleepModeStatus( void )
3501         {
3502         eSleepModeStatus eReturn = eStandardSleep;
3503                 taskENTER_CRITICAL(&xTaskQueueMutex);
3504
3505                 if( listCURRENT_LIST_LENGTH( &xPendingReadyList[ xPortGetCoreID() ] ) != 0 )
3506                 {
3507                         /* A task was made ready while the scheduler was suspended. */
3508                         eReturn = eAbortSleep;
3509                 }
3510                 else if( xYieldPending[ xPortGetCoreID() ] != pdFALSE )
3511                 {
3512                         /* A yield was pended while the scheduler was suspended. */
3513                         eReturn = eAbortSleep;
3514                 }
3515                 else
3516                 {
3517                         #if configUSE_TIMERS == 0
3518                         {
3519                                 /* The idle task exists in addition to the application tasks. */
3520                                 const UBaseType_t uxNonApplicationTasks = 1;
3521
3522                                 /* If timers are not being used and all the tasks are in the
3523                                 suspended list (which might mean they have an infinite block
3524                                 time rather than actually being suspended) then it is safe to
3525                                 turn all clocks off and just wait for external interrupts. */
3526                                 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
3527                                 {
3528                                         eReturn = eNoTasksWaitingTimeout;
3529                                 }
3530                                 else
3531                                 {
3532                                         mtCOVERAGE_TEST_MARKER();
3533                                 }
3534                         }
3535                         #endif /* configUSE_TIMERS */
3536                 }
3537                 taskEXIT_CRITICAL(&xTaskQueueMutex);
3538
3539                 return eReturn;
3540         }
3541 #endif /* configUSE_TICKLESS_IDLE */
3542 /*-----------------------------------------------------------*/
3543
3544 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
3545
3546 #if ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
3547
3548         void vTaskSetThreadLocalStoragePointerAndDelCallback( TaskHandle_t xTaskToSet, BaseType_t xIndex, void *pvValue , TlsDeleteCallbackFunction_t xDelCallback)
3549         {
3550         TCB_t *pxTCB;
3551
3552                 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
3553                 {
3554                         taskENTER_CRITICAL(&xTaskQueueMutex);
3555                         pxTCB = prvGetTCBFromHandle( xTaskToSet );
3556                         pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
3557                         pxTCB->pvThreadLocalStoragePointersDelCallback[ xIndex ] = xDelCallback;
3558                         taskEXIT_CRITICAL(&xTaskQueueMutex);
3559                 }
3560         }
3561
3562         void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet, BaseType_t xIndex, void *pvValue )
3563         {
3564                 vTaskSetThreadLocalStoragePointerAndDelCallback( xTaskToSet, xIndex, pvValue, (TlsDeleteCallbackFunction_t)NULL );
3565         }
3566
3567
3568 #else
3569         void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet, BaseType_t xIndex, void *pvValue )
3570         {
3571         TCB_t *pxTCB;
3572
3573                 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
3574                 {
3575                         taskENTER_CRITICAL(&xTaskQueueMutex);
3576                         pxTCB = prvGetTCBFromHandle( xTaskToSet );
3577                         pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
3578                         taskEXIT_CRITICAL(&xTaskQueueMutex);
3579                 }
3580         }
3581 #endif /* configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS */
3582
3583 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
3584 /*-----------------------------------------------------------*/
3585
3586 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
3587
3588         void *pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery, BaseType_t xIndex )
3589         {
3590         void *pvReturn = NULL;
3591         TCB_t *pxTCB;
3592
3593                 if( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS )
3594                 {
3595                         pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3596                         pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
3597                 }
3598                 else
3599                 {
3600                         pvReturn = NULL;
3601                 }
3602
3603                 return pvReturn;
3604         }
3605
3606 #endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
3607
3608
3609 #if ( portUSING_MPU_WRAPPERS == 1 )
3610 /* ToDo: Check for multicore */
3611         void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify, const MemoryRegion_t * const xRegions )
3612         {
3613         TCB_t *pxTCB;
3614
3615                 UNTESTED_FUNCTION();
3616                 /* If null is passed in here then we are deleting ourselves. */
3617                 pxTCB = prvGetTCBFromHandle( xTaskToModify );
3618
3619         vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
3620         }
3621
3622 #endif /* portUSING_MPU_WRAPPERS */
3623 /*-----------------------------------------------------------*/
3624
3625 static void prvInitialiseTaskLists( void )
3626 {
3627 UBaseType_t uxPriority;
3628
3629         for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
3630         {
3631                 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
3632         }
3633
3634         vListInitialise( &xDelayedTaskList1 );
3635         vListInitialise( &xDelayedTaskList2 );
3636         vListInitialise( &xPendingReadyList[ 0 ] );
3637         if (portNUM_PROCESSORS == 2) {
3638                 vListInitialise( &xPendingReadyList[ 1 ] );
3639         }
3640
3641         #if ( INCLUDE_vTaskDelete == 1 )
3642         {
3643                 vListInitialise( &xTasksWaitingTermination );
3644         }
3645         #endif /* INCLUDE_vTaskDelete */
3646
3647         #if ( INCLUDE_vTaskSuspend == 1 )
3648         {
3649                 vListInitialise( &xSuspendedTaskList );
3650         }
3651         #endif /* INCLUDE_vTaskSuspend */
3652
3653         /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
3654         using list2. */
3655         pxDelayedTaskList = &xDelayedTaskList1;
3656         pxOverflowDelayedTaskList = &xDelayedTaskList2;
3657 }
3658 /*-----------------------------------------------------------*/
3659
3660 static void prvCheckTasksWaitingTermination( void )
3661 {
3662         #if ( INCLUDE_vTaskDelete == 1 )
3663         {
3664                 BaseType_t xListIsEmpty;
3665                 int core = xPortGetCoreID();
3666
3667                 /* ucTasksDeleted is used to prevent vTaskSuspendAll() being called
3668                 too often in the idle task. */
3669                 while(uxTasksDeleted > ( UBaseType_t ) 0U )
3670                 {
3671                         TCB_t *pxTCB = NULL;
3672
3673                         taskENTER_CRITICAL(&xTaskQueueMutex);
3674                         {
3675                                 xListIsEmpty = listLIST_IS_EMPTY( &xTasksWaitingTermination );
3676                                 if( xListIsEmpty == pdFALSE )
3677                                 {
3678                                         /* We only want to kill tasks that ran on this core because e.g. _xt_coproc_release needs to
3679                                         be called on the core the process is pinned on, if any */
3680                                         ListItem_t *target = listGET_HEAD_ENTRY(&xTasksWaitingTermination);
3681                                         for( ; target != listGET_END_MARKER(&xTasksWaitingTermination); target = listGET_NEXT(target) ){        //Walk the list
3682                                                 TCB_t *tgt_tcb = ( TCB_t * )listGET_LIST_ITEM_OWNER(target);
3683                                                 int affinity = tgt_tcb->xCoreID;
3684                                                 //Self deleting tasks are added to Termination List before they switch context. Ensure they aren't still currently running
3685                                                 if( pxCurrentTCB[core] == tgt_tcb || (portNUM_PROCESSORS > 1 && pxCurrentTCB[!core] == tgt_tcb) ){
3686                                                         continue;       //Can't free memory of task that is still running
3687                                                 }
3688                                                 if(affinity == core || affinity == tskNO_AFFINITY){             //Find first item not pinned to other core
3689                                                         pxTCB = tgt_tcb;
3690                                                         break;
3691                                                 }
3692                                         }
3693                                         if(pxTCB != NULL){
3694                                                 ( void ) uxListRemove( target );        //Remove list item from list
3695                                                 --uxCurrentNumberOfTasks;
3696                                                 --uxTasksDeleted;
3697                                         }
3698                                 }
3699                         }
3700                         taskEXIT_CRITICAL(&xTaskQueueMutex);    //Need to call deletion callbacks outside critical section
3701
3702                         if (pxTCB != NULL) {    //Call deletion callbacks and free TCB memory
3703                                 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 ) && ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
3704                                         prvDeleteTLS( pxTCB );
3705                                 #endif
3706                                 prvDeleteTCB( pxTCB );
3707                         }
3708                         else
3709                         {
3710                                 mtCOVERAGE_TEST_MARKER();
3711                                 break;  //No TCB found that could be freed by this core, break out of loop
3712                         }
3713                 }
3714         }
3715         #endif /* vTaskDelete */
3716 }
3717 /*-----------------------------------------------------------*/
3718
3719 //This should be called with the taskqueuemutex grabbed. -JD
3720 static void prvAddCurrentTaskToDelayedList( const BaseType_t xCoreID, const TickType_t xTimeToWake )
3721 {
3722         /* The list item will be inserted in wake time order. */
3723         listSET_LIST_ITEM_VALUE( &( pxCurrentTCB[ xCoreID ]->xGenericListItem ), xTimeToWake );
3724
3725         if( xTimeToWake < xTickCount )
3726         {
3727         traceMOVED_TASK_TO_OVERFLOW_DELAYED_LIST();
3728                 /* Wake time has overflowed.  Place this item in the overflow list. */
3729                 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB[ xCoreID ]->xGenericListItem ) );
3730         }
3731         else
3732         {
3733         traceMOVED_TASK_TO_DELAYED_LIST();
3734                 /* The wake time has not overflowed, so the current block list is used. */
3735                 vListInsert( pxDelayedTaskList, &( pxCurrentTCB[ xCoreID ]->xGenericListItem ) );
3736
3737                 /* If the task entering the blocked state was placed at the head of the
3738                 list of blocked tasks then xNextTaskUnblockTime needs to be updated
3739                 too. */
3740                 if( xTimeToWake < xNextTaskUnblockTime )
3741                 {
3742                         xNextTaskUnblockTime = xTimeToWake;
3743                 }
3744                 else
3745                 {
3746                         mtCOVERAGE_TEST_MARKER();
3747                 }
3748         }
3749 }
3750 /*-----------------------------------------------------------*/
3751
3752 BaseType_t xTaskGetAffinity( TaskHandle_t xTask )
3753 {
3754         TCB_t *pxTCB;
3755
3756         pxTCB = prvGetTCBFromHandle( xTask );
3757
3758         return pxTCB->xCoreID;
3759 }
3760 /*-----------------------------------------------------------*/
3761
3762
3763 #if ( configUSE_TRACE_FACILITY == 1 )
3764
3765         static UBaseType_t prvListTaskWithinSingleList( TaskStatus_t *pxTaskStatusArray, List_t *pxList, eTaskState eState )
3766         {
3767         volatile TCB_t *pxNextTCB, *pxFirstTCB;
3768         UBaseType_t uxTask = 0;
3769
3770                 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3771                 {
3772                         listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
3773
3774                         /* Populate an TaskStatus_t structure within the
3775                         pxTaskStatusArray array for each task that is referenced from
3776                         pxList.  See the definition of TaskStatus_t in task.h for the
3777                         meaning of each TaskStatus_t structure member. */
3778                         do
3779                         {
3780                                 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
3781
3782                                 pxTaskStatusArray[ uxTask ].xHandle = ( TaskHandle_t ) pxNextTCB;
3783                                 pxTaskStatusArray[ uxTask ].pcTaskName = ( const char * ) &( pxNextTCB->pcTaskName [ 0 ] );
3784                                 pxTaskStatusArray[ uxTask ].xTaskNumber = pxNextTCB->uxTCBNumber;
3785                                 pxTaskStatusArray[ uxTask ].eCurrentState = eState;
3786                                 pxTaskStatusArray[ uxTask ].uxCurrentPriority = pxNextTCB->uxPriority;
3787                                 pxTaskStatusArray[ uxTask ].xCoreID = pxNextTCB->xCoreID;
3788
3789                                 #if ( INCLUDE_vTaskSuspend == 1 )
3790                                 {
3791                                         /* If the task is in the suspended list then there is a chance
3792                                         it is actually just blocked indefinitely - so really it should
3793                                         be reported as being in the Blocked state. */
3794                                         if( eState == eSuspended )
3795                                         {
3796                                                 if( listLIST_ITEM_CONTAINER( &( pxNextTCB->xEventListItem ) ) != NULL )
3797                                                 {
3798                                                         pxTaskStatusArray[ uxTask ].eCurrentState = eBlocked;
3799                                                 }
3800                                         }
3801                                 }
3802                                 #endif /* INCLUDE_vTaskSuspend */
3803
3804                                 #if ( configUSE_MUTEXES == 1 )
3805                                 {
3806                                         pxTaskStatusArray[ uxTask ].uxBasePriority = pxNextTCB->uxBasePriority;
3807                                 }
3808                                 #else
3809                                 {
3810                                         pxTaskStatusArray[ uxTask ].uxBasePriority = 0;
3811                                 }
3812                                 #endif
3813
3814                                 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3815                                 {
3816                                         pxTaskStatusArray[ uxTask ].ulRunTimeCounter = pxNextTCB->ulRunTimeCounter;
3817                                 }
3818                                 #else
3819                                 {
3820                                         pxTaskStatusArray[ uxTask ].ulRunTimeCounter = 0;
3821                                 }
3822                                 #endif
3823
3824                                 #if ( portSTACK_GROWTH > 0 )
3825                                 {
3826                                         pxTaskStatusArray[ uxTask ].usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxNextTCB->pxEndOfStack );
3827                                 }
3828                                 #else
3829                                 {
3830                                         pxTaskStatusArray[ uxTask ].usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxNextTCB->pxStack );
3831                                 }
3832                                 #endif
3833
3834                                 uxTask++;
3835
3836                         } while( pxNextTCB != pxFirstTCB );
3837                 }
3838                 else
3839                 {
3840                         mtCOVERAGE_TEST_MARKER();
3841                 }
3842
3843                 return uxTask;
3844         }
3845
3846 #endif /* configUSE_TRACE_FACILITY */
3847 /*-----------------------------------------------------------*/
3848
3849 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )
3850
3851         static uint32_t prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
3852         {
3853         uint32_t ulCount = 0U;
3854
3855                 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
3856                 {
3857                         pucStackByte -= portSTACK_GROWTH;
3858                         ulCount++;
3859                 }
3860
3861                 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
3862
3863                 return ( uint32_t ) ulCount;
3864         }
3865
3866 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) ) */
3867 /*-----------------------------------------------------------*/
3868
3869 #if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
3870
3871         UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
3872         {
3873         TCB_t *pxTCB;
3874         uint8_t *pucEndOfStack;
3875         UBaseType_t uxReturn;
3876
3877                 pxTCB = prvGetTCBFromHandle( xTask );
3878
3879                 #if portSTACK_GROWTH < 0
3880                 {
3881                         pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
3882                 }
3883                 #else
3884                 {
3885                         pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
3886                 }
3887                 #endif
3888
3889                 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
3890
3891                 return uxReturn;
3892         }
3893
3894 #endif /* INCLUDE_uxTaskGetStackHighWaterMark */
3895 /*-----------------------------------------------------------*/
3896
3897 #if (INCLUDE_pxTaskGetStackStart == 1)
3898
3899         uint8_t* pxTaskGetStackStart( TaskHandle_t xTask)
3900         {
3901                 TCB_t *pxTCB;
3902                 uint8_t* uxReturn;
3903
3904                 pxTCB = prvGetTCBFromHandle( xTask );
3905                 uxReturn = (uint8_t*)pxTCB->pxStack;
3906
3907                 return uxReturn;
3908         }
3909
3910 #endif /* INCLUDE_pxTaskGetStackStart */
3911 /*-----------------------------------------------------------*/
3912
3913 #if ( INCLUDE_vTaskDelete == 1 )
3914
3915         static void prvDeleteTCB( TCB_t *pxTCB )
3916         {
3917                 /* This call is required for any port specific cleanup related to task.
3918                 It must be above the vPortFree() calls. */
3919                 portCLEAN_UP_TCB( pxTCB );
3920
3921                 /* Free up the memory allocated by the scheduler for the task.  It is up
3922                 to the task to free any memory allocated at the application level. */
3923                 #if ( configUSE_NEWLIB_REENTRANT == 1 )
3924                 {
3925                         _reclaim_reent( &( pxTCB->xNewLib_reent ) );
3926                 }
3927                 #endif /* configUSE_NEWLIB_REENTRANT */
3928
3929                 #if ( portUSING_MPU_WRAPPERS == 1 )
3930                         vPortReleaseTaskMPUSettings( &( pxTCB->xMPUSettings) );
3931                 #endif
3932
3933                 #if( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
3934                 {
3935                         /* The task can only have been allocated dynamically - free both
3936                         the stack and TCB. */
3937                         vPortFreeAligned( pxTCB->pxStack );
3938                         vPortFree( pxTCB );
3939                 }
3940                 #elif( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE == 1 )
3941                 {
3942                         /* The task could have been allocated statically or dynamically, so
3943                         check what was statically allocated before trying to free the
3944                         memory. */
3945                         if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
3946                         {
3947                                 /* Both the stack and TCB were allocated dynamically, so both
3948                                 must be freed. */
3949                                 vPortFreeAligned( pxTCB->pxStack );
3950                                 vPortFree( pxTCB );
3951                         }
3952                         else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
3953                         {
3954                                 /* Only the stack was statically allocated, so the TCB is the
3955                                 only memory that must be freed. */
3956                                 vPortFree( pxTCB );
3957                         }
3958                         else
3959                         {
3960                                 /* Neither the stack nor the TCB were allocated dynamically, so
3961                                 nothing needs to be freed. */
3962                                 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB     )
3963                                 mtCOVERAGE_TEST_MARKER();
3964                         }
3965                 }
3966                 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
3967         }
3968
3969 #endif /* INCLUDE_vTaskDelete */
3970 /*-----------------------------------------------------------*/
3971
3972 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 ) && ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS )
3973
3974         static void prvDeleteTLS( TCB_t *pxTCB )
3975         {
3976                 configASSERT( pxTCB );
3977                 for( int x = 0; x < ( UBaseType_t ) configNUM_THREAD_LOCAL_STORAGE_POINTERS; x++ )
3978                 {
3979                         if (pxTCB->pvThreadLocalStoragePointersDelCallback[ x ] != NULL)        //If del cb is set
3980                         {
3981                                 pxTCB->pvThreadLocalStoragePointersDelCallback[ x ](x, pxTCB->pvThreadLocalStoragePointers[ x ]);       //Call del cb
3982                         }
3983                 }
3984         }
3985
3986 #endif /* ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 ) && ( configTHREAD_LOCAL_STORAGE_DELETE_CALLBACKS ) */
3987 /*-----------------------------------------------------------*/
3988
3989 static void prvResetNextTaskUnblockTime( void )
3990 {
3991 TCB_t *pxTCB;
3992
3993         if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
3994         {
3995                 /* The new current delayed list is empty.  Set
3996                 xNextTaskUnblockTime to the maximum possible value so it is
3997                 extremely unlikely that the
3998                 if( xTickCount >= xNextTaskUnblockTime ) test will pass until
3999                 there is an item in the delayed list. */
4000                 xNextTaskUnblockTime = portMAX_DELAY;
4001         }
4002         else
4003         {
4004                 /* The new current delayed list is not empty, get the value of
4005                 the item at the head of the delayed list.  This is the time at
4006                 which the task at the head of the delayed list should be removed
4007                 from the Blocked state. */
4008                 ( pxTCB ) = ( TCB_t * ) listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList );
4009                 xNextTaskUnblockTime = listGET_LIST_ITEM_VALUE( &( ( pxTCB )->xGenericListItem ) );
4010         }
4011 }
4012 /*-----------------------------------------------------------*/
4013
4014 #if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4015
4016         TaskHandle_t xTaskGetCurrentTaskHandle( void )
4017         {
4018         TaskHandle_t xReturn;
4019         unsigned state;
4020
4021                 state = portENTER_CRITICAL_NESTED();
4022                 xReturn = pxCurrentTCB[ xPortGetCoreID() ];
4023                 portEXIT_CRITICAL_NESTED(state);
4024
4025                 return xReturn;
4026         }
4027
4028         TaskHandle_t xTaskGetCurrentTaskHandleForCPU( BaseType_t cpuid )
4029         {
4030         TaskHandle_t xReturn=NULL;
4031
4032                 //Xtensa-specific: the pxCurrentPCB pointer is atomic so we shouldn't need a lock.
4033                 if (cpuid < portNUM_PROCESSORS) {
4034                         xReturn = pxCurrentTCB[ cpuid ];
4035                 }
4036
4037                 return xReturn;
4038         }
4039
4040
4041 #endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
4042 /*-----------------------------------------------------------*/
4043
4044 #if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
4045
4046         BaseType_t xTaskGetSchedulerState( void )
4047         {
4048         BaseType_t xReturn;
4049         unsigned state;
4050
4051                 state = portENTER_CRITICAL_NESTED();
4052                 if( xSchedulerRunning == pdFALSE )
4053                 {
4054                         xReturn = taskSCHEDULER_NOT_STARTED;
4055                 }
4056                 else
4057                 {
4058                         if( uxSchedulerSuspended[ xPortGetCoreID() ] == ( UBaseType_t ) pdFALSE )
4059                         {
4060                                 xReturn = taskSCHEDULER_RUNNING;
4061                         }
4062                         else
4063                         {
4064                                 xReturn = taskSCHEDULER_SUSPENDED;
4065                         }
4066                 }
4067                 portEXIT_CRITICAL_NESTED(state);
4068
4069                 return xReturn;
4070         }
4071
4072 #endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
4073 /*-----------------------------------------------------------*/
4074
4075 #if ( configUSE_MUTEXES == 1 )
4076
4077         void vTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
4078         {
4079         TCB_t * const pxTCB = ( TCB_t * ) pxMutexHolder;
4080
4081                 taskENTER_CRITICAL(&xTickCountMutex);
4082                 /* If the mutex was given back by an interrupt while the queue was
4083                 locked then the mutex holder might now be NULL. */
4084                 if( pxMutexHolder != NULL )
4085                 {
4086                         if( pxTCB->uxPriority < pxCurrentTCB[ xPortGetCoreID() ]->uxPriority )
4087                         {
4088                                 taskENTER_CRITICAL(&xTaskQueueMutex);
4089                                 /* Adjust the mutex holder state to account for its new
4090                                 priority.  Only reset the event list item value if the value is
4091                                 not     being used for anything else. */
4092                                 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4093                                 {
4094                                         listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB[ xPortGetCoreID() ]->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4095                                 }
4096                                 else
4097                                 {
4098                                         mtCOVERAGE_TEST_MARKER();
4099                                 }
4100
4101                                 /* If the task being modified is in the ready state it will need to
4102                                 be moved into a new list. */
4103                                 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxTCB->uxPriority ] ), &( pxTCB->xGenericListItem ) ) != pdFALSE )
4104                                 {
4105                                         if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
4106                                         {
4107                                                 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
4108                                         }
4109                                         else
4110                                         {
4111                                                 mtCOVERAGE_TEST_MARKER();
4112                                         }
4113
4114                                         /* Inherit the priority before being moved into the new list. */
4115                                         pxTCB->uxPriority = pxCurrentTCB[ xPortGetCoreID() ]->uxPriority;
4116                     prvReaddTaskToReadyList( pxTCB );
4117                                 }
4118                                 else
4119                                 {
4120                                         /* Just inherit the priority. */
4121                                         pxTCB->uxPriority = pxCurrentTCB[ xPortGetCoreID() ]->uxPriority;
4122                                 }
4123
4124                                 taskEXIT_CRITICAL(&xTaskQueueMutex);
4125
4126                                 traceTASK_PRIORITY_INHERIT( pxTCB, pxCurrentTCB[ xPortGetCoreID() ]->uxPriority );
4127                         }
4128                         else
4129                         {
4130                                 mtCOVERAGE_TEST_MARKER();
4131                         }
4132                 }
4133                 else
4134                 {
4135                         mtCOVERAGE_TEST_MARKER();
4136                 }
4137
4138                 taskEXIT_CRITICAL(&xTickCountMutex);
4139
4140         }
4141
4142 #endif /* configUSE_MUTEXES */
4143 /*-----------------------------------------------------------*/
4144
4145 #if ( configUSE_MUTEXES == 1 )
4146
4147         BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
4148         {
4149         TCB_t * const pxTCB = ( TCB_t * ) pxMutexHolder;
4150         BaseType_t xReturn = pdFALSE;
4151                 taskENTER_CRITICAL(&xTickCountMutex);
4152
4153                 if( pxMutexHolder != NULL )
4154                 {
4155                         configASSERT( pxTCB->uxMutexesHeld );
4156                         ( pxTCB->uxMutexesHeld )--;
4157
4158                         if( pxTCB->uxPriority != pxTCB->uxBasePriority )
4159                         {
4160                                 /* Only disinherit if no other mutexes are held. */
4161                                 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
4162                                 {
4163                                         taskENTER_CRITICAL(&xTaskQueueMutex);
4164                                         /* A task can only have an inhertied priority if it holds
4165                                         the mutex.  If the mutex is held by a task then it cannot be
4166                                         given from an interrupt, and if a mutex is given by the
4167                                         holding task then it must be the running state task.  Remove
4168                                         the     holding task from the ready     list. */
4169                                         if( uxListRemove( &( pxTCB->xGenericListItem ) ) == ( UBaseType_t ) 0 )
4170                                         {
4171                                                 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
4172                                         }
4173                                         else
4174                                         {
4175                                                 mtCOVERAGE_TEST_MARKER();
4176                                         }
4177
4178                                         /* Disinherit the priority before adding the task into the
4179                                         new     ready list. */
4180                                         traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
4181                                         pxTCB->uxPriority = pxTCB->uxBasePriority;
4182
4183                                         /* Reset the event list item value.  It cannot be in use for
4184                                         any other purpose if this task is running, and it must be
4185                                         running to give back the mutex. */
4186                                         listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4187                     prvReaddTaskToReadyList( pxTCB );
4188
4189                                         /* Return true to indicate that a context switch is required.
4190                                         This is only actually required in the corner case whereby
4191                                         multiple mutexes were held and the mutexes were given back
4192                                         in an order different to that in which they were taken.
4193                                         If a context switch did not occur when the first mutex was
4194                                         returned, even if a task was waiting on it, then a context
4195                                         switch should occur when the last mutex is returned whether
4196                                         a task is waiting on it or not. */
4197                                         xReturn = pdTRUE;
4198                                         taskEXIT_CRITICAL(&xTaskQueueMutex);
4199                                 }
4200                                 else
4201                                 {
4202                                         mtCOVERAGE_TEST_MARKER();
4203                                 }
4204                         }
4205                         else
4206                         {
4207                                 mtCOVERAGE_TEST_MARKER();
4208                         }
4209                 }
4210                 else
4211                 {
4212                         mtCOVERAGE_TEST_MARKER();
4213                 }
4214
4215                 taskEXIT_CRITICAL(&xTickCountMutex);
4216                 return xReturn;
4217         }
4218
4219 #endif /* configUSE_MUTEXES */
4220 /*-----------------------------------------------------------*/
4221
4222 /* For multicore, this assumes the vPortCPUAquireMutex is recursive, that is, it can be called multiple
4223    times and the release call will have to be called as many times for the mux to unlock. */
4224
4225 /* Gotcha (which seems to be deliberate in FreeRTOS, according to
4226 http://www.freertos.org/FreeRTOS_Support_Forum_Archive/December_2012/freertos_PIC32_Bug_-_vTaskEnterCritical_6400806.html
4227 ) is that calling vTaskEnterCritical followed by vTaskExitCritical will leave the interrupts DISABLED when the scheduler
4228 is not running.  Re-enabling the scheduler will re-enable the interrupts instead.
4229
4230 For ESP32 FreeRTOS, vTaskEnterCritical implements both portENTER_CRITICAL and portENTER_CRITICAL_ISR.
4231 */
4232
4233 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
4234
4235 #include "portmux_impl.h"
4236
4237 #ifdef CONFIG_FREERTOS_PORTMUX_DEBUG
4238         void vTaskEnterCritical( portMUX_TYPE *mux, const char *function, int line )
4239 #else
4240         void vTaskEnterCritical( portMUX_TYPE *mux )
4241 #endif
4242         {
4243                 BaseType_t oldInterruptLevel=0;
4244                 BaseType_t schedulerRunning = xSchedulerRunning;
4245                 if( schedulerRunning != pdFALSE )
4246                 {
4247                         //Interrupts may already be disabled (because we're doing this recursively) but we can't get the interrupt level after
4248                         //vPortCPUAquireMutex, because it also may mess with interrupts. Get it here first, then later figure out if we're nesting
4249                         //and save for real there.
4250                         oldInterruptLevel=portENTER_CRITICAL_NESTED();
4251                 }
4252 #ifdef CONFIG_FREERTOS_PORTMUX_DEBUG
4253                 vPortCPUAcquireMutexIntsDisabled( mux, portMUX_NO_TIMEOUT, function, line );
4254 #else
4255                 vPortCPUAcquireMutexIntsDisabled( mux, portMUX_NO_TIMEOUT );
4256 #endif
4257
4258                 if( schedulerRunning != pdFALSE )
4259                 {
4260                         TCB_t *tcb = pxCurrentTCB[xPortGetCoreID()];
4261                         BaseType_t newNesting = tcb->uxCriticalNesting + 1;
4262                         tcb->uxCriticalNesting = newNesting;
4263                         if( newNesting == 1 )
4264                         {
4265                                 //This is the first time we get called. Save original interrupt level.
4266                                 tcb->uxOldInterruptState = oldInterruptLevel;
4267                         }
4268
4269                         /* Original FreeRTOS comment, saved for reference:
4270                         This is not the interrupt safe version of the enter critical
4271                         function so     assert() if it is being called from an interrupt
4272                         context.  Only API functions that end in "FromISR" can be used in an
4273                         interrupt. Only assert if the critical nesting count is 1 to
4274                         protect against recursive calls if the assert function also uses a
4275                         critical section. */
4276
4277                         /* DISABLED in the esp32 port - because of SMP, For ESP32
4278                         FreeRTOS, vTaskEnterCritical implements both
4279                         portENTER_CRITICAL and portENTER_CRITICAL_ISR. vTaskEnterCritical
4280                         has to be used in way more places than before, and some are called
4281                         both from ISR as well as non-ISR code, thus we re-organized
4282                         vTaskEnterCritical to also work in ISRs. */
4283 #if 0
4284                         if( newNesting  == 1 )
4285                         {
4286                                 portASSERT_IF_IN_ISR();
4287                         }
4288 #endif
4289
4290                 }
4291                 else
4292                 {
4293                         mtCOVERAGE_TEST_MARKER();
4294                 }
4295         }
4296
4297 #endif /* portCRITICAL_NESTING_IN_TCB */
4298 /*-----------------------------------------------------------*/
4299
4300
4301 /*
4302 For ESP32 FreeRTOS, vTaskExitCritical implements both portEXIT_CRITICAL and portEXIT_CRITICAL_ISR.
4303 */
4304 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
4305
4306 #ifdef CONFIG_FREERTOS_PORTMUX_DEBUG
4307         void vTaskExitCritical( portMUX_TYPE *mux, const char *function, int line )
4308 #else
4309         void vTaskExitCritical( portMUX_TYPE *mux )
4310 #endif
4311         {
4312 #ifdef CONFIG_FREERTOS_PORTMUX_DEBUG
4313                 vPortCPUReleaseMutexIntsDisabled( mux, function, line );
4314 #else
4315                 vPortCPUReleaseMutexIntsDisabled( mux );
4316 #endif
4317                 if( xSchedulerRunning != pdFALSE )
4318                 {
4319                         TCB_t *tcb = pxCurrentTCB[xPortGetCoreID()];
4320                         BaseType_t nesting = tcb->uxCriticalNesting;
4321                         if( nesting      > 0U )
4322                         {
4323                                 nesting--;
4324                                 tcb->uxCriticalNesting = nesting;
4325
4326                                 if( nesting == 0U )
4327                                 {
4328                                         portEXIT_CRITICAL_NESTED(tcb->uxOldInterruptState);
4329                                 }
4330                                 else
4331                                 {
4332                                         mtCOVERAGE_TEST_MARKER();
4333                                 }
4334                         }
4335                         else
4336                         {
4337                                 mtCOVERAGE_TEST_MARKER();
4338                         }
4339                 }
4340                 else
4341                 {
4342                         mtCOVERAGE_TEST_MARKER();
4343                 }
4344         }
4345
4346 #endif /* portCRITICAL_NESTING_IN_TCB */
4347 /*-----------------------------------------------------------*/
4348
4349 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
4350
4351         static char *prvWriteNameToBuffer( char *pcBuffer, const char *pcTaskName )
4352         {
4353         BaseType_t x;
4354
4355                 /* Start by copying the entire string. */
4356                 strcpy( pcBuffer, pcTaskName );
4357
4358                 /* Pad the end of the string with spaces to ensure columns line up when
4359                 printed out. */
4360                 for( x = strlen( pcBuffer ); x < ( configMAX_TASK_NAME_LEN - 1 ); x++ )
4361                 {
4362                         pcBuffer[ x ] = ' ';
4363                 }
4364
4365                 /* Terminate. */
4366                 pcBuffer[ x ] = 0x00;
4367
4368                 /* Return the new end of string. */
4369                 return &( pcBuffer[ x ] );
4370         }
4371
4372 #endif /* ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
4373 /*-----------------------------------------------------------*/
4374
4375 #if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
4376
4377         void vTaskList( char * pcWriteBuffer )
4378         {
4379         TaskStatus_t *pxTaskStatusArray;
4380         volatile UBaseType_t uxArraySize, x;
4381         char cStatus;
4382
4383                 /*
4384                  * PLEASE NOTE:
4385                  *
4386                  * This function is provided for convenience only, and is used by many
4387                  * of the demo applications.  Do not consider it to be part of the
4388                  * scheduler.
4389                  *
4390                  * vTaskList() calls uxTaskGetSystemState(), then formats part of the
4391                  * uxTaskGetSystemState() output into a human readable table that
4392                  * displays task names, states and stack usage.
4393                  *
4394                  * vTaskList() has a dependency on the sprintf() C library function that
4395                  * might bloat the code size, use a lot of stack, and provide different
4396                  * results on different platforms.  An alternative, tiny, third party,
4397                  * and limited functionality implementation of sprintf() is provided in
4398                  * many of the FreeRTOS/Demo sub-directories in a file called
4399                  * printf-stdarg.c (note printf-stdarg.c does not provide a full
4400                  * snprintf() implementation!).
4401                  *
4402                  * It is recommended that production systems call uxTaskGetSystemState()
4403                  * directly to get access to raw stats data, rather than indirectly
4404                  * through a call to vTaskList().
4405                  */
4406
4407
4408                 /* Make sure the write buffer does not contain a string. */
4409                 *pcWriteBuffer = 0x00;
4410
4411                 /* Take a snapshot of the number of tasks in case it changes while this
4412                 function is executing. */
4413                 uxArraySize = uxCurrentNumberOfTasks;
4414
4415                 /* Allocate an array index for each task.  NOTE!  if
4416                 configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
4417                 equate to NULL. */
4418                 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
4419
4420                 if( pxTaskStatusArray != NULL )
4421                 {
4422                         /* Generate the (binary) data. */
4423                         uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
4424
4425                         /* Create a human readable table from the binary data. */
4426                         for( x = 0; x < uxArraySize; x++ )
4427                         {
4428                                 switch( pxTaskStatusArray[ x ].eCurrentState )
4429                                 {
4430                                         case eReady:            cStatus = tskREADY_CHAR;
4431                                                                                 break;
4432
4433                                         case eBlocked:          cStatus = tskBLOCKED_CHAR;
4434                                                                                 break;
4435
4436                                         case eSuspended:        cStatus = tskSUSPENDED_CHAR;
4437                                                                                 break;
4438
4439                                         case eDeleted:          cStatus = tskDELETED_CHAR;
4440                                                                                 break;
4441
4442                                         default:                        /* Should not get here, but it is included
4443                                                                                 to prevent static checking errors. */
4444                                                                                 cStatus = 0x00;
4445                                                                                 break;
4446                                 }
4447
4448                                 /* Write the task name to the string, padding with spaces so it
4449                                 can be printed in tabular form more easily. */
4450                                 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
4451
4452                                 /* Write the rest of the string. */
4453 #ifdef CONFIG_FREERTOS_VTASKLIST_INCLUDE_COREID
4454                                 sprintf( pcWriteBuffer, "\t%c\t%u\t%u\t%u\t%hd\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber, ( int ) pxTaskStatusArray[ x ].xCoreID );
4455 #else
4456                                 sprintf( pcWriteBuffer, "\t%c\t%u\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber );
4457 #endif
4458                                 pcWriteBuffer += strlen( pcWriteBuffer );
4459                         }
4460
4461                         /* Free the array again.  NOTE!  If configSUPPORT_DYNAMIC_ALLOCATION
4462                         is 0 then vPortFree() will be #defined to nothing. */
4463                         vPortFree( pxTaskStatusArray );
4464                 }
4465                 else
4466                 {
4467                         mtCOVERAGE_TEST_MARKER();
4468                 }
4469         }
4470
4471 #endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
4472 /*----------------------------------------------------------*/
4473
4474 #if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
4475
4476         void vTaskGetRunTimeStats( char *pcWriteBuffer )
4477         {
4478         TaskStatus_t *pxTaskStatusArray;
4479         volatile UBaseType_t uxArraySize, x;
4480         uint32_t ulTotalTime, ulStatsAsPercentage;
4481
4482                 #if( configUSE_TRACE_FACILITY != 1 )
4483                 {
4484                         #error configUSE_TRACE_FACILITY must also be set to 1 in FreeRTOSConfig.h to use vTaskGetRunTimeStats().
4485                 }
4486                 #endif
4487
4488                 /*
4489                  * PLEASE NOTE:
4490                  *
4491                  * This function is provided for convenience only, and is used by many
4492                  * of the demo applications.  Do not consider it to be part of the
4493                  * scheduler.
4494                  *
4495                  * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
4496                  * of the uxTaskGetSystemState() output into a human readable table that
4497                  * displays the amount of time each task has spent in the Running state
4498                  * in both absolute and percentage terms.
4499                  *
4500                  * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
4501                  * function that might bloat the code size, use a lot of stack, and
4502                  * provide different results on different platforms.  An alternative,
4503                  * tiny, third party, and limited functionality implementation of
4504                  * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
4505                  * a file called printf-stdarg.c (note printf-stdarg.c does not provide
4506                  * a full snprintf() implementation!).
4507                  *
4508                  * It is recommended that production systems call uxTaskGetSystemState()
4509                  * directly to get access to raw stats data, rather than indirectly
4510                  * through a call to vTaskGetRunTimeStats().
4511                  */
4512
4513                 /* Make sure the write buffer does not contain a string. */
4514                 *pcWriteBuffer = 0x00;
4515
4516                 /* Take a snapshot of the number of tasks in case it changes while this
4517                 function is executing. */
4518                 uxArraySize = uxCurrentNumberOfTasks;
4519
4520                 /* Allocate an array index for each task.  NOTE!  If
4521                 configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
4522                 equate to NULL. */
4523                 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) );
4524
4525                 if( pxTaskStatusArray != NULL )
4526                 {
4527                         /* Generate the (binary) data. */
4528                         uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
4529
4530                         /* For percentage calculations. */
4531                         ulTotalTime /= 100UL;
4532
4533                         /* Avoid divide by zero errors. */
4534                         if( ulTotalTime > 0 )
4535                         {
4536                                 /* Create a human readable table from the binary data. */
4537                                 for( x = 0; x < uxArraySize; x++ )
4538                                 {
4539                                         /* What percentage of the total run time has the task used?
4540                                         This will always be rounded down to the nearest integer.
4541                                         ulTotalRunTimeDiv100 has already been divided by 100. */
4542                                     /* Also need to consider total run time of all */
4543                                         ulStatsAsPercentage = (pxTaskStatusArray[ x ].ulRunTimeCounter/portNUM_PROCESSORS)/ ulTotalTime;
4544
4545                                         /* Write the task name to the string, padding with
4546                                         spaces so it can be printed in tabular form more
4547                                         easily. */
4548                                         pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
4549
4550                                         if( ulStatsAsPercentage > 0UL )
4551                                         {
4552                                                 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
4553                                                 {
4554                                                         sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
4555                                                 }
4556                                                 #else
4557                                                 {
4558                                                         /* sizeof( int ) == sizeof( long ) so a smaller
4559                                                         printf() library can be used. */
4560                                                         sprintf( pcWriteBuffer, "\t%u\t\t%u%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage );
4561                                                 }
4562                                                 #endif
4563                                         }
4564                                         else
4565                                         {
4566                                                 /* If the percentage is zero here then the task has
4567                                                 consumed less than 1% of the total run time. */
4568                                                 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
4569                                                 {
4570                                                         sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
4571                                                 }
4572                                                 #else
4573                                                 {
4574                                                         /* sizeof( int ) == sizeof( long ) so a smaller
4575                                                         printf() library can be used. */
4576                                                         sprintf( pcWriteBuffer, "\t%u\t\t<1%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter );
4577                                                 }
4578                                                 #endif
4579                                         }
4580
4581                                         pcWriteBuffer += strlen( pcWriteBuffer );
4582                                 }
4583                         }
4584                         else
4585                         {
4586                                 mtCOVERAGE_TEST_MARKER();
4587                         }
4588
4589                         /* Free the array again.  NOTE!  If configSUPPORT_DYNAMIC_ALLOCATION
4590                         is 0 then vPortFree() will be #defined to nothing. */
4591                         vPortFree( pxTaskStatusArray );
4592                 }
4593                 else
4594                 {
4595                         mtCOVERAGE_TEST_MARKER();
4596                 }
4597         }
4598
4599 #endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
4600 /*-----------------------------------------------------------*/
4601
4602 TickType_t uxTaskResetEventItemValue( void )
4603 {
4604 TickType_t uxReturn;
4605         taskENTER_CRITICAL(&xTaskQueueMutex);
4606         uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB[ xPortGetCoreID() ]->xEventListItem ) );
4607
4608         /* Reset the event list item to its normal value - so it can be used with
4609         queues and semaphores. */
4610         listSET_LIST_ITEM_VALUE( &( pxCurrentTCB[ xPortGetCoreID() ]->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB[ xPortGetCoreID() ]->uxPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4611         taskEXIT_CRITICAL(&xTaskQueueMutex);
4612
4613         return uxReturn;
4614 }
4615 /*-----------------------------------------------------------*/
4616
4617 #if ( configUSE_MUTEXES == 1 )
4618
4619         void *pvTaskIncrementMutexHeldCount( void )
4620         {
4621         TCB_t *curTCB;
4622
4623                 /* If xSemaphoreCreateMutex() is called before any tasks have been created
4624                 then pxCurrentTCB will be NULL. */
4625                 taskENTER_CRITICAL(&xTaskQueueMutex);
4626                 if( pxCurrentTCB[ xPortGetCoreID() ] != NULL )
4627                 {
4628                         ( pxCurrentTCB[ xPortGetCoreID() ]->uxMutexesHeld )++;
4629                 }
4630                 curTCB = pxCurrentTCB[ xPortGetCoreID() ];
4631                 taskEXIT_CRITICAL(&xTaskQueueMutex);
4632
4633                 return curTCB;
4634         }
4635
4636 #endif /* configUSE_MUTEXES */
4637 /*-----------------------------------------------------------*/
4638
4639 #if( configUSE_TASK_NOTIFICATIONS == 1 )
4640
4641         uint32_t ulTaskNotifyTake( BaseType_t xClearCountOnExit, TickType_t xTicksToWait )
4642         {
4643         TickType_t xTimeToWake;
4644         uint32_t ulReturn;
4645
4646                 taskENTER_CRITICAL(&xTaskQueueMutex);
4647                 {
4648                         /* Only block if the notification count is not already non-zero. */
4649                         if( pxCurrentTCB[ xPortGetCoreID() ]->ulNotifiedValue == 0UL )
4650                         {
4651                                 /* Mark this task as waiting for a notification. */
4652                                 pxCurrentTCB[ xPortGetCoreID() ]->eNotifyState = eWaitingNotification;
4653
4654                                 if( xTicksToWait > ( TickType_t ) 0 )
4655                                 {
4656                                         /* The task is going to block.  First it must be removed
4657                                         from the ready list. */
4658                                         if( uxListRemove( &( pxCurrentTCB[ xPortGetCoreID() ]->xGenericListItem ) ) == ( UBaseType_t ) 0 )
4659                                         {
4660                                                 /* The current task must be in a ready list, so there is
4661                                                 no need to check, and the port reset macro can be called
4662                                                 directly. */
4663                                                 portRESET_READY_PRIORITY( pxCurrentTCB[ xPortGetCoreID() ]->uxPriority, uxTopReadyPriority );
4664                                         }
4665                                         else
4666                                         {
4667                                                 mtCOVERAGE_TEST_MARKER();
4668                                         }
4669
4670                                         #if ( INCLUDE_vTaskSuspend == 1 )
4671                                         {
4672                                                 if( xTicksToWait == portMAX_DELAY )
4673                                                 {
4674                                                         /* Add the task to the suspended task list instead
4675                                                         of a delayed task list to ensure the task is not
4676                                                         woken by a timing event.  It will block
4677                                                         indefinitely. */
4678                             traceMOVED_TASK_TO_SUSPENDED_LIST(pxCurrentTCB);
4679                                                         vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB[ xPortGetCoreID() ]->xGenericListItem ) );
4680                                                 }
4681                                                 else
4682                                                 {
4683                                                         /* Calculate the time at which the task should be
4684                                                         woken if no notification events occur.  This may
4685                                                         overflow but this doesn't matter, the scheduler will
4686                                                         handle it. */
4687                                                         xTimeToWake = xTickCount + xTicksToWait;
4688                                                         prvAddCurrentTaskToDelayedList( xPortGetCoreID(), xTimeToWake );
4689                                                 }
4690                                         }
4691                                         #else /* INCLUDE_vTaskSuspend */
4692                                         {
4693                                                         /* Calculate the time at which the task should be
4694                                                         woken if the event does not occur.  This may
4695                                                         overflow but this doesn't matter, the scheduler will
4696                                                         handle it. */
4697                                                         xTimeToWake = xTickCount + xTicksToWait;
4698                                                         prvAddCurrentTaskToDelayedList( xTimeToWake );
4699                                         }
4700                                         #endif /* INCLUDE_vTaskSuspend */
4701
4702                                         /* All ports are written to allow a yield in a critical
4703                                         section (some will yield immediately, others wait until the
4704                                         critical section exits) - but it is not something that
4705                                         application code should ever do. */
4706                                         portYIELD_WITHIN_API();
4707                                 }
4708                                 else
4709                                 {
4710                                         mtCOVERAGE_TEST_MARKER();
4711                                 }
4712                         }
4713                         else
4714                         {
4715                                 mtCOVERAGE_TEST_MARKER();
4716                         }
4717                 }
4718                 taskEXIT_CRITICAL(&xTaskQueueMutex);
4719
4720                 taskENTER_CRITICAL(&xTaskQueueMutex);
4721                 {
4722                         ulReturn = pxCurrentTCB[ xPortGetCoreID() ]->ulNotifiedValue;
4723
4724                         if( ulReturn != 0UL )
4725                         {
4726                                 if( xClearCountOnExit != pdFALSE )
4727                                 {
4728                                         pxCurrentTCB[ xPortGetCoreID() ]->ulNotifiedValue = 0UL;
4729                                 }
4730                                 else
4731                                 {
4732                                         ( pxCurrentTCB[ xPortGetCoreID() ]->ulNotifiedValue )--;
4733                                 }
4734                         }
4735                         else
4736                         {
4737                                 mtCOVERAGE_TEST_MARKER();
4738                         }
4739
4740                         pxCurrentTCB[ xPortGetCoreID() ]->eNotifyState = eNotWaitingNotification;
4741                 }
4742                 taskEXIT_CRITICAL(&xTaskQueueMutex);
4743
4744                 return ulReturn;
4745         }
4746
4747 #endif /* configUSE_TASK_NOTIFICATIONS */
4748 /*-----------------------------------------------------------*/
4749
4750 #if( configUSE_TASK_NOTIFICATIONS == 1 )
4751
4752         BaseType_t xTaskNotifyWait( uint32_t ulBitsToClearOnEntry, uint32_t ulBitsToClearOnExit, uint32_t *pulNotificationValue, TickType_t xTicksToWait )
4753         {
4754         TickType_t xTimeToWake;
4755         BaseType_t xReturn;
4756
4757                 taskENTER_CRITICAL(&xTaskQueueMutex);
4758                 {
4759                         /* Only block if a notification is not already pending. */
4760                         if( pxCurrentTCB[ xPortGetCoreID() ]->eNotifyState != eNotified )
4761                         {
4762                                 /* Clear bits in the task's notification value as bits may get
4763                                 set     by the notifying task or interrupt.  This can be used to
4764                                 clear the value to zero. */
4765                                 pxCurrentTCB[ xPortGetCoreID() ]->ulNotifiedValue &= ~ulBitsToClearOnEntry;
4766
4767                                 /* Mark this task as waiting for a notification. */
4768                                 pxCurrentTCB[ xPortGetCoreID() ]->eNotifyState = eWaitingNotification;
4769
4770                                 if( xTicksToWait > ( TickType_t ) 0 )
4771                                 {
4772                                         /* The task is going to block.  First it must be removed
4773                                         from the        ready list. */
4774                                         if( uxListRemove( &( pxCurrentTCB[ xPortGetCoreID() ]->xGenericListItem ) ) == ( UBaseType_t ) 0 )
4775                                         {
4776                                                 /* The current task must be in a ready list, so there is
4777                                                 no need to check, and the port reset macro can be called
4778                                                 directly. */
4779                                                 portRESET_READY_PRIORITY( pxCurrentTCB[ xPortGetCoreID() ]->uxPriority, uxTopReadyPriority );
4780                                         }
4781                                         else
4782                                         {
4783                                                 mtCOVERAGE_TEST_MARKER();
4784                                         }
4785
4786                                         #if ( INCLUDE_vTaskSuspend == 1 )
4787                                         {
4788                                                 if( xTicksToWait == portMAX_DELAY )
4789                                                 {
4790                                                         /* Add the task to the suspended task list instead
4791                                                         of a delayed task list to ensure the task is not
4792                                                         woken by a timing event.  It will block
4793                                                         indefinitely. */
4794                             traceMOVED_TASK_TO_SUSPENDED_LIST(pxCurrentTCB);
4795                                                         vListInsertEnd( &xSuspendedTaskList, &( pxCurrentTCB[ xPortGetCoreID() ]->xGenericListItem ) );
4796                                                 }
4797                                                 else
4798                                                 {
4799                                                         /* Calculate the time at which the task should be
4800                                                         woken if no notification events occur.  This may
4801                                                         overflow but this doesn't matter, the scheduler will
4802                                                         handle it. */
4803                                                         xTimeToWake = xTickCount + xTicksToWait;
4804                                                         prvAddCurrentTaskToDelayedList( xPortGetCoreID(), xTimeToWake );
4805                                                 }
4806                                         }
4807                                         #else /* INCLUDE_vTaskSuspend */
4808                                         {
4809                                                         /* Calculate the time at which the task should be
4810                                                         woken if the event does not occur.  This may
4811                                                         overflow but this doesn't matter, the scheduler will
4812                                                         handle it. */
4813                                                         xTimeToWake = xTickCount + xTicksToWait;
4814                                                         prvAddCurrentTaskToDelayedList( xTimeToWake );
4815                                         }
4816                                         #endif /* INCLUDE_vTaskSuspend */
4817
4818                                         /* All ports are written to allow a yield in a critical
4819                                         section (some will yield immediately, others wait until the
4820                                         critical section exits) - but it is not something that
4821                                         application code should ever do. */
4822                                         portYIELD_WITHIN_API();
4823                                 }
4824                                 else
4825                                 {
4826                                         mtCOVERAGE_TEST_MARKER();
4827                                 }
4828                         }
4829                         else
4830                         {
4831                                 mtCOVERAGE_TEST_MARKER();
4832                         }
4833                 }
4834                 taskEXIT_CRITICAL(&xTaskQueueMutex);
4835
4836                 taskENTER_CRITICAL(&xTaskQueueMutex);
4837                 {
4838                         if( pulNotificationValue != NULL )
4839                         {
4840                                 /* Output the current notification value, which may or may not
4841                                 have changed. */
4842                                 *pulNotificationValue = pxCurrentTCB[ xPortGetCoreID() ]->ulNotifiedValue;
4843                         }
4844
4845                         /* If eNotifyValue is set then either the task never entered the
4846                         blocked state (because a notification was already pending) or the
4847                         task unblocked because of a notification.  Otherwise the task
4848                         unblocked because of a timeout. */
4849                         if( pxCurrentTCB[ xPortGetCoreID() ]->eNotifyState == eWaitingNotification )
4850                         {
4851                                 /* A notification was not received. */
4852                                 xReturn = pdFALSE;
4853                         }
4854                         else
4855                         {
4856                                 /* A notification was already pending or a notification was
4857                                 received while the task was waiting. */
4858                                 pxCurrentTCB[ xPortGetCoreID() ]->ulNotifiedValue &= ~ulBitsToClearOnExit;
4859                                 xReturn = pdTRUE;
4860                         }
4861
4862                         pxCurrentTCB[ xPortGetCoreID() ]->eNotifyState = eNotWaitingNotification;
4863                 }
4864                 taskEXIT_CRITICAL(&xTaskQueueMutex);
4865
4866                 return xReturn;
4867         }
4868
4869 #endif /* configUSE_TASK_NOTIFICATIONS */
4870 /*-----------------------------------------------------------*/
4871
4872 #if( configUSE_TASK_NOTIFICATIONS == 1 )
4873
4874         BaseType_t xTaskNotify( TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction )
4875         {
4876         TCB_t * pxTCB;
4877         eNotifyValue eOriginalNotifyState;
4878         BaseType_t xReturn = pdPASS;
4879
4880                 configASSERT( xTaskToNotify );
4881                 pxTCB = ( TCB_t * ) xTaskToNotify;
4882
4883                 taskENTER_CRITICAL(&xTaskQueueMutex);
4884                 {
4885                         eOriginalNotifyState = pxTCB->eNotifyState;
4886
4887                         pxTCB->eNotifyState = eNotified;
4888
4889                         switch( eAction )
4890                         {
4891                                 case eSetBits   :
4892                                         pxTCB->ulNotifiedValue |= ulValue;
4893                                         break;
4894
4895                                 case eIncrement :
4896                                         ( pxTCB->ulNotifiedValue )++;
4897                                         break;
4898
4899                                 case eSetValueWithOverwrite     :
4900                                         pxTCB->ulNotifiedValue = ulValue;
4901                                         break;
4902
4903                                 case eSetValueWithoutOverwrite :
4904                                         if( eOriginalNotifyState != eNotified )
4905                                         {
4906                                                 pxTCB->ulNotifiedValue = ulValue;
4907                                         }
4908                                         else
4909                                         {
4910                                                 /* The value could not be written to the task. */
4911                                                 xReturn = pdFAIL;
4912                                         }
4913                                         break;
4914
4915                                 case eNoAction:
4916                                         /* The task is being notified without its notify value being
4917                                         updated. */
4918                                         break;
4919                         }
4920
4921
4922                         /* If the task is in the blocked state specifically to wait for a
4923                         notification then unblock it now. */
4924                         if( eOriginalNotifyState == eWaitingNotification )
4925                         {
4926                                 ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );
4927                                 prvAddTaskToReadyList( pxTCB );
4928
4929                                 /* The task should not have been on an event list. */
4930                                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
4931
4932                                 if( tskCAN_RUN_HERE(pxTCB->xCoreID) && pxTCB->uxPriority > pxCurrentTCB[ xPortGetCoreID() ]->uxPriority )
4933                                 {
4934                                         /* The notified task has a priority above the currently
4935                                         executing task so a yield is required. */
4936                                         portYIELD_WITHIN_API();
4937                                 }
4938                                 else if ( pxTCB->xCoreID != xPortGetCoreID() )
4939                                 {
4940                                         taskYIELD_OTHER_CORE(pxTCB->xCoreID, pxTCB->uxPriority);
4941                                 }
4942                                 else
4943                                 {
4944                                         mtCOVERAGE_TEST_MARKER();
4945                                 }
4946                         }
4947                         else
4948                         {
4949                                 mtCOVERAGE_TEST_MARKER();
4950                         }
4951                 }
4952                 taskEXIT_CRITICAL(&xTaskQueueMutex);
4953
4954                 return xReturn;
4955         }
4956
4957 #endif /* configUSE_TASK_NOTIFICATIONS */
4958 /*-----------------------------------------------------------*/
4959
4960 #if( configUSE_TASK_NOTIFICATIONS == 1 )
4961
4962         BaseType_t xTaskNotifyFromISR( TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction, BaseType_t *pxHigherPriorityTaskWoken )
4963         {
4964         TCB_t * pxTCB;
4965         eNotifyValue eOriginalNotifyState;
4966         BaseType_t xReturn = pdPASS;
4967
4968                 configASSERT( xTaskToNotify );
4969
4970                 pxTCB = ( TCB_t * ) xTaskToNotify;
4971
4972                 taskENTER_CRITICAL_ISR(&xTaskQueueMutex);
4973
4974                 {
4975                         eOriginalNotifyState = pxTCB->eNotifyState;
4976
4977                         pxTCB->eNotifyState = eNotified;
4978
4979                         switch( eAction )
4980                         {
4981                                 case eSetBits   :
4982                                         pxTCB->ulNotifiedValue |= ulValue;
4983                                         break;
4984
4985                                 case eIncrement :
4986                                         ( pxTCB->ulNotifiedValue )++;
4987                                         break;
4988
4989                                 case eSetValueWithOverwrite     :
4990                                         pxTCB->ulNotifiedValue = ulValue;
4991                                         break;
4992
4993                                 case eSetValueWithoutOverwrite :
4994                                         if( eOriginalNotifyState != eNotified )
4995                                         {
4996                                                 pxTCB->ulNotifiedValue = ulValue;
4997                                         }
4998                                         else
4999                                         {
5000                                                 /* The value could not be written to the task. */
5001                                                 xReturn = pdFAIL;
5002                                         }
5003                                         break;
5004
5005                                 case eNoAction :
5006                                         /* The task is being notified without its notify value being
5007                                         updated. */
5008                                         break;
5009                         }
5010
5011
5012                         /* If the task is in the blocked state specifically to wait for a
5013                         notification then unblock it now. */
5014                         if( eOriginalNotifyState == eWaitingNotification )
5015                         {
5016                                 /* The task should not have been on an event list. */
5017                                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5018
5019                                 if( uxSchedulerSuspended[ xPortGetCoreID() ] == ( UBaseType_t ) pdFALSE )
5020                                 {
5021                                         ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );
5022                                         prvAddTaskToReadyList( pxTCB );
5023                                 }
5024                                 else
5025                                 {
5026                                         /* The delayed and ready lists cannot be accessed, so hold
5027                                         this task pending until the scheduler is resumed. */
5028                                         vListInsertEnd( &( xPendingReadyList[ xPortGetCoreID() ] ), &( pxTCB->xEventListItem ) );
5029                                 }
5030
5031                                 if( tskCAN_RUN_HERE(pxTCB->xCoreID) && pxTCB->uxPriority > pxCurrentTCB[ xPortGetCoreID() ]->uxPriority )
5032                                 {
5033                                         /* The notified task has a priority above the currently
5034                                         executing task so a yield is required. */
5035                                         if( pxHigherPriorityTaskWoken != NULL )
5036                                         {
5037                                                 *pxHigherPriorityTaskWoken = pdTRUE;
5038                                         }
5039                                 }
5040                                 else if ( pxTCB->xCoreID != xPortGetCoreID() )
5041                                 {
5042                                         taskYIELD_OTHER_CORE( pxTCB->xCoreID, pxTCB->uxPriority );
5043                                 }
5044                                 else
5045                                 {
5046                                         mtCOVERAGE_TEST_MARKER();
5047                                 }
5048                         }
5049                 }
5050                 taskEXIT_CRITICAL_ISR(&xTaskQueueMutex);
5051
5052                 return xReturn;
5053         }
5054
5055 #endif /* configUSE_TASK_NOTIFICATIONS */
5056 /*-----------------------------------------------------------*/
5057
5058 #if( configUSE_TASK_NOTIFICATIONS == 1 )
5059
5060         void vTaskNotifyGiveFromISR( TaskHandle_t xTaskToNotify, BaseType_t *pxHigherPriorityTaskWoken )
5061         {
5062         TCB_t * pxTCB;
5063         eNotifyValue eOriginalNotifyState;
5064
5065                 configASSERT( xTaskToNotify );
5066
5067
5068                 pxTCB = ( TCB_t * ) xTaskToNotify;
5069
5070                 taskENTER_CRITICAL_ISR(&xTaskQueueMutex);
5071                 {
5072                         eOriginalNotifyState = pxTCB->eNotifyState;
5073                         pxTCB->eNotifyState = eNotified;
5074
5075                         /* 'Giving' is equivalent to incrementing a count in a counting
5076                         semaphore. */
5077                         ( pxTCB->ulNotifiedValue )++;
5078
5079                         /* If the task is in the blocked state specifically to wait for a
5080                         notification then unblock it now. */
5081                         if( eOriginalNotifyState == eWaitingNotification )
5082                         {
5083                                 /* The task should not have been on an event list. */
5084                                 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5085
5086                                 if( uxSchedulerSuspended[ xPortGetCoreID() ] == ( UBaseType_t ) pdFALSE )
5087                                 {
5088                                         ( void ) uxListRemove( &( pxTCB->xGenericListItem ) );
5089                                         prvAddTaskToReadyList( pxTCB );
5090                                 }
5091                                 else
5092                                 {
5093                                         /* The delayed and ready lists cannot be accessed, so hold
5094                                         this task pending until the scheduler is resumed. */
5095                                         vListInsertEnd( &( xPendingReadyList[ xPortGetCoreID() ] ), &( pxTCB->xEventListItem ) );
5096                                 }
5097
5098                                 if( tskCAN_RUN_HERE(pxTCB->xCoreID) && pxTCB->uxPriority > pxCurrentTCB[ xPortGetCoreID() ]->uxPriority )
5099                                 {
5100                                         /* The notified task has a priority above the currently
5101                                         executing task so a yield is required. */
5102                                         if( pxHigherPriorityTaskWoken != NULL )
5103                                         {
5104                                                 *pxHigherPriorityTaskWoken = pdTRUE;
5105                                         }
5106                                 }
5107                                 else if ( pxTCB->xCoreID != xPortGetCoreID() )
5108                                 {
5109                                         taskYIELD_OTHER_CORE( pxTCB->xCoreID, pxTCB->uxPriority );
5110                                 }
5111                                 else
5112                                 {
5113                                         mtCOVERAGE_TEST_MARKER();
5114                                 }
5115                         }
5116                 }
5117                 taskEXIT_CRITICAL_ISR(&xTaskQueueMutex);
5118         }
5119
5120 #endif /* configUSE_TASK_NOTIFICATIONS */
5121
5122 #if ( configENABLE_TASK_SNAPSHOT == 1 )
5123         static void prvTaskGetSnapshot( TaskSnapshot_t *pxTaskSnapshotArray, UBaseType_t *uxTask, TCB_t *pxTCB )
5124         {
5125                 if (pxTCB == NULL) {
5126                         return;
5127                 }
5128                 pxTaskSnapshotArray[ *uxTask ].pxTCB = pxTCB;
5129                 pxTaskSnapshotArray[ *uxTask ].pxTopOfStack = (StackType_t *)pxTCB->pxTopOfStack;
5130                 #if( portSTACK_GROWTH < 0 )
5131                 {
5132                         pxTaskSnapshotArray[ *uxTask ].pxEndOfStack = pxTCB->pxEndOfStack;
5133                 }
5134                 #else
5135                 {
5136                         pxTaskSnapshotArray[ *uxTask ].pxEndOfStack = pxTCB->pxStack;
5137                 }
5138                 #endif
5139                 (*uxTask)++;
5140         }
5141
5142         static void prvTaskGetSnapshotsFromList( TaskSnapshot_t *pxTaskSnapshotArray, UBaseType_t *uxTask, const UBaseType_t uxArraySize, List_t *pxList )
5143         {
5144                 TCB_t *pxNextTCB, *pxFirstTCB;
5145
5146                 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
5147                 {
5148                         listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );
5149                         do
5150                         {
5151                                 if( *uxTask >= uxArraySize )
5152                                         break;
5153
5154                                 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );
5155                                 prvTaskGetSnapshot( pxTaskSnapshotArray, uxTask, pxNextTCB );
5156                         } while( pxNextTCB != pxFirstTCB );
5157                 }
5158                 else
5159                 {
5160                         mtCOVERAGE_TEST_MARKER();
5161                 }
5162         }
5163
5164         UBaseType_t uxTaskGetSnapshotAll( TaskSnapshot_t * const pxTaskSnapshotArray, const UBaseType_t uxArraySize, UBaseType_t * const pxTcbSz )
5165         {
5166                 UBaseType_t uxTask = 0, i = 0;
5167
5168
5169                 *pxTcbSz = sizeof(TCB_t);
5170                 /* Fill in an TaskStatus_t structure with information on each
5171                 task in the Ready state. */
5172                 i = configMAX_PRIORITIES;
5173                 do
5174                 {
5175                         i--;
5176                         prvTaskGetSnapshotsFromList( pxTaskSnapshotArray, &uxTask, uxArraySize, &( pxReadyTasksLists[ i ] ) );
5177                 } while( i > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
5178
5179                 /* Fill in an TaskStatus_t structure with information on each
5180                 task in the Blocked state. */
5181                 prvTaskGetSnapshotsFromList( pxTaskSnapshotArray, &uxTask, uxArraySize, ( List_t * ) pxDelayedTaskList );
5182                 prvTaskGetSnapshotsFromList( pxTaskSnapshotArray, &uxTask, uxArraySize, ( List_t * ) pxOverflowDelayedTaskList );
5183                 for (i = 0; i < portNUM_PROCESSORS; i++) {
5184                         if( uxTask >= uxArraySize )
5185                                 break;
5186                         prvTaskGetSnapshotsFromList( pxTaskSnapshotArray, &uxTask, uxArraySize, &( xPendingReadyList[ i ] ) );
5187                 }
5188
5189                 #if( INCLUDE_vTaskDelete == 1 )
5190                 {
5191                         prvTaskGetSnapshotsFromList( pxTaskSnapshotArray, &uxTask, uxArraySize, &xTasksWaitingTermination );
5192                 }
5193                 #endif
5194
5195                 #if ( INCLUDE_vTaskSuspend == 1 )
5196                 {
5197                         prvTaskGetSnapshotsFromList( pxTaskSnapshotArray, &uxTask, uxArraySize, &xSuspendedTaskList );
5198                 }
5199                 #endif
5200                 return uxTask;
5201         }
5202
5203 #endif
5204
5205 #ifdef FREERTOS_MODULE_TEST
5206         #include "tasks_test_access_functions.h"
5207 #endif
5208