blob: fcc371f68b59a6a3ce80bedb3b2e7097e8f685e5 [file] [log] [blame]
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001/*
xiaohu.huang58292b32024-01-03 14:09:51 +08002 * FreeRTOS Kernel V10.5.1
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4 *
5 * SPDX-License-Identifier: MIT
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08006 *
7 * Permission is hereby granted, free of charge, to any person obtaining a copy of
8 * this software and associated documentation files (the "Software"), to deal in
9 * the Software without restriction, including without limitation the rights to
10 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11 * the Software, and to permit persons to whom the Software is furnished to do so,
12 * subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
xiaohu.huang58292b32024-01-03 14:09:51 +080024 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080026 *
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080027 */
28
29/* Standard includes. */
30#include <stdlib.h>
31#include <string.h>
32
33/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
xiaohu.huang58292b32024-01-03 14:09:51 +080034 * all the API functions to use the MPU wrappers. That should only be done when
35 * task.h is included from an application file. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080036#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
37
38/* FreeRTOS includes. */
39#include "FreeRTOS.h"
40#include "task.h"
41#include "timers.h"
42#include "stack_macros.h"
43
44/* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
xiaohu.huang58292b32024-01-03 14:09:51 +080045 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
46 * for the header files above, but not in this file, in order to generate the
47 * correct privileged Vs unprivileged linkage and placement. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080048#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
49
50/* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
xiaohu.huang58292b32024-01-03 14:09:51 +080051 * functions but without including stdio.h here. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080052#if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
xiaohu.huang58292b32024-01-03 14:09:51 +080053
54/* At the bottom of this file are two optional functions that can be used
55 * to generate human readable text from the raw data generated by the
56 * uxTaskGetSystemState() function. Note the formatting functions are provided
57 * for convenience only, and are NOT considered part of the kernel. */
58 #include <stdio.h>
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080059#endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
60
xiaohu.huang58292b32024-01-03 14:09:51 +080061#if ( configUSE_PREEMPTION == 0 )
62
63/* If the cooperative scheduler is being used then a yield should not be
64 * performed just because a higher priority task has been woken. */
65 #define taskYIELD_IF_USING_PREEMPTION()
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080066#else
xiaohu.huang58292b32024-01-03 14:09:51 +080067 #define taskYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080068#endif
69
70/* Values that can be assigned to the ucNotifyState member of the TCB. */
xiaohu.huang58292b32024-01-03 14:09:51 +080071#define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
72#define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
73#define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080074
75/*
76 * The value used to fill the stack of a task when the task is created. This
77 * is used purely for checking the high water mark for tasks.
78 */
xiaohu.huang58292b32024-01-03 14:09:51 +080079#define tskSTACK_FILL_BYTE ( 0xa5U )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080080
xiaohu.huang58292b32024-01-03 14:09:51 +080081/* Bits used to record how a task's stack and TCB were allocated. */
82#define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
83#define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
84#define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080085
86/* If any of the following are set then task stacks are filled with a known
xiaohu.huang58292b32024-01-03 14:09:51 +080087 * value so the high water mark can be determined. If none of the following are
88 * set then don't fill the stack so there is no unnecessary dependency on memset. */
89#if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
90 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080091#else
xiaohu.huang58292b32024-01-03 14:09:51 +080092 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080093#endif
94
95/*
96 * Macros used by vListTask to indicate which state a task is in.
97 */
xiaohu.huang58292b32024-01-03 14:09:51 +080098#define tskRUNNING_CHAR ( 'X' )
99#define tskBLOCKED_CHAR ( 'B' )
100#define tskREADY_CHAR ( 'R' )
101#define tskDELETED_CHAR ( 'D' )
102#define tskSUSPENDED_CHAR ( 'S' )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800103
104/*
xiaohu.huang58292b32024-01-03 14:09:51 +0800105 * Some kernel aware debuggers require the data the debugger needs access to to
106 * be global, rather than file scope.
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800107 */
108#ifdef portREMOVE_STATIC_QUALIFIER
xiaohu.huang58292b32024-01-03 14:09:51 +0800109 #define static
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800110#endif
111
112/* The name allocated to the Idle task. This can be overridden by defining
xiaohu.huang58292b32024-01-03 14:09:51 +0800113 * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800114#ifndef configIDLE_TASK_NAME
xiaohu.huang58292b32024-01-03 14:09:51 +0800115 #define configIDLE_TASK_NAME "IDLE"
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800116#endif
117
118#if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
119
xiaohu.huang58292b32024-01-03 14:09:51 +0800120/* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
121 * performed in a generic way that is not optimised to any particular
122 * microcontroller architecture. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800123
xiaohu.huang58292b32024-01-03 14:09:51 +0800124/* uxTopReadyPriority holds the priority of the highest priority ready
125 * state task. */
126 #define taskRECORD_READY_PRIORITY( uxPriority ) \
127 { \
128 if( ( uxPriority ) > uxTopReadyPriority ) \
129 { \
130 uxTopReadyPriority = ( uxPriority ); \
131 } \
132 } /* taskRECORD_READY_PRIORITY */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800133
xiaohu.huang58292b32024-01-03 14:09:51 +0800134/*-----------------------------------------------------------*/
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800135
xiaohu.huang58292b32024-01-03 14:09:51 +0800136 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
137 { \
138 UBaseType_t uxTopPriority = uxTopReadyPriority; \
139 \
140 /* Find the highest priority queue that contains ready tasks. */ \
141 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
142 { \
143 configASSERT( uxTopPriority ); \
144 --uxTopPriority; \
145 } \
146 \
147 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
148 * the same priority get an equal share of the processor time. */ \
149 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
150 uxTopReadyPriority = uxTopPriority; \
151 } /* taskSELECT_HIGHEST_PRIORITY_TASK */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800152
xiaohu.huang58292b32024-01-03 14:09:51 +0800153/*-----------------------------------------------------------*/
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800154
xiaohu.huang58292b32024-01-03 14:09:51 +0800155/* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
156 * they are only required when a port optimised method of task selection is
157 * being used. */
158 #define taskRESET_READY_PRIORITY( uxPriority )
159 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800160
161#else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
162
xiaohu.huang58292b32024-01-03 14:09:51 +0800163/* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
164 * performed in a way that is tailored to the particular microcontroller
165 * architecture being used. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800166
xiaohu.huang58292b32024-01-03 14:09:51 +0800167/* A port optimised version is provided. Call the port defined macros. */
168 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800169
xiaohu.huang58292b32024-01-03 14:09:51 +0800170/*-----------------------------------------------------------*/
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800171
xiaohu.huang58292b32024-01-03 14:09:51 +0800172 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
173 { \
174 UBaseType_t uxTopPriority; \
175 \
176 /* Find the highest priority list that contains ready tasks. */ \
177 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
178 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
179 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
180 } /* taskSELECT_HIGHEST_PRIORITY_TASK() */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800181
xiaohu.huang58292b32024-01-03 14:09:51 +0800182/*-----------------------------------------------------------*/
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800183
xiaohu.huang58292b32024-01-03 14:09:51 +0800184/* A port optimised version is provided, call it only if the TCB being reset
185 * is being referenced from a ready list. If it is referenced from a delayed
186 * or suspended list then it won't be in a ready list. */
187 #define taskRESET_READY_PRIORITY( uxPriority ) \
188 { \
189 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
190 { \
191 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
192 } \
193 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800194
195#endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
196
197/*-----------------------------------------------------------*/
198
199/* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
xiaohu.huang58292b32024-01-03 14:09:51 +0800200 * count overflows. */
201#define taskSWITCH_DELAYED_LISTS() \
202 { \
203 List_t * pxTemp; \
204 \
205 /* The delayed tasks list should be empty when the lists are switched. */ \
206 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
207 \
208 pxTemp = pxDelayedTaskList; \
209 pxDelayedTaskList = pxOverflowDelayedTaskList; \
210 pxOverflowDelayedTaskList = pxTemp; \
211 xNumOfOverflows++; \
212 prvResetNextTaskUnblockTime(); \
213 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800214
215/*-----------------------------------------------------------*/
216
217/*
218 * Place the task represented by pxTCB into the appropriate ready list for
219 * the task. It is inserted at the end of the list.
220 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800221#define prvAddTaskToReadyList( pxTCB ) \
222 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
223 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
224 listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
225 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800226/*-----------------------------------------------------------*/
227
228/*
xiaohu.huang58292b32024-01-03 14:09:51 +0800229 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800230 * where NULL is used to indicate that the handle of the currently executing
231 * task should be used in place of the parameter. This macro simply checks to
232 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
233 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800234#define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800235
236/* The item value of the event list item is normally used to hold the priority
xiaohu.huang58292b32024-01-03 14:09:51 +0800237 * of the task to which it belongs (coded to allow it to be held in reverse
238 * priority order). However, it is occasionally borrowed for other purposes. It
239 * is important its value is not updated due to a task priority change while it is
240 * being used for another purpose. The following bit definition is used to inform
241 * the scheduler that the value should not be changed - in which case it is the
242 * responsibility of whichever module is using the value to ensure it gets set back
243 * to its original value when it is released. */
244#if ( configUSE_16_BIT_TICKS == 1 )
245 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800246#else
xiaohu.huang58292b32024-01-03 14:09:51 +0800247 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800248#endif
249
250/*
251 * Task control block. A task control block (TCB) is allocated for each task,
252 * and stores task state information, including a pointer to the task's context
253 * (the task's run time environment, including register values)
254 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800255typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800256{
xiaohu.huang58292b32024-01-03 14:09:51 +0800257 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. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800258
xiaohu.huang58292b32024-01-03 14:09:51 +0800259 #if ( portUSING_MPU_WRAPPERS == 1 )
260 xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
261 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800262
xiaohu.huang58292b32024-01-03 14:09:51 +0800263 ListItem_t xStateListItem; /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */
264 ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
265 UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
266 StackType_t * pxStack; /*< Points to the start of the stack. */
xiaohu.huang79d92512024-02-21 14:33:03 +0800267 configSTACK_DEPTH_TYPE xStackDepth; /* AML add the member and add dummy member to xSTATIC_TCB in FreeRTOS.h */
xiaohu.huang58292b32024-01-03 14:09:51 +0800268 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. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800269
xiaohu.huang58292b32024-01-03 14:09:51 +0800270 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
271 StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
272 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800273
xiaohu.huang58292b32024-01-03 14:09:51 +0800274 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
275 UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
276 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800277
xiaohu.huang58292b32024-01-03 14:09:51 +0800278 #if ( configUSE_TRACE_FACILITY == 1 )
279 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. */
280 UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
281 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800282
xiaohu.huang58292b32024-01-03 14:09:51 +0800283 #if ( configUSE_MUTEXES == 1 )
284 UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
285 UBaseType_t uxMutexesHeld;
286 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800287
xiaohu.huang58292b32024-01-03 14:09:51 +0800288 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
289 TaskHookFunction_t pxTaskTag;
290 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800291
xiaohu.huang58292b32024-01-03 14:09:51 +0800292 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
293 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
294 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800295
xiaohu.huang58292b32024-01-03 14:09:51 +0800296 #if ( configGENERATE_RUN_TIME_STATS == 1 )
297 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
298 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800299
xiaohu.huang58292b32024-01-03 14:09:51 +0800300 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
301 configTLS_BLOCK_TYPE xTLSBlock; /*< Memory block used as Thread Local Storage (TLS) Block for the task. */
302 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800303
xiaohu.huang58292b32024-01-03 14:09:51 +0800304 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
305 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
306 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
307 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800308
xiaohu.huang58292b32024-01-03 14:09:51 +0800309 /* See the comments in FreeRTOS.h with the definition of
310 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
311 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
312 uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
313 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800314
xiaohu.huang58292b32024-01-03 14:09:51 +0800315 #if ( INCLUDE_xTaskAbortDelay == 1 )
316 uint8_t ucDelayAborted;
317 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800318
xiaohu.huang58292b32024-01-03 14:09:51 +0800319 #if ( configUSE_POSIX_ERRNO == 1 )
320 int iTaskErrno;
321 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800322} tskTCB;
323
324/* The old tskTCB name is maintained above then typedefed to the new TCB_t name
xiaohu.huang58292b32024-01-03 14:09:51 +0800325 * below to enable the use of older kernel aware debuggers. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800326typedef tskTCB TCB_t;
327
328/*lint -save -e956 A manual analysis and inspection has been used to determine
xiaohu.huang58292b32024-01-03 14:09:51 +0800329 * which static variables must be declared volatile. */
330portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800331
332/* Lists for ready and blocked tasks. --------------------
xiaohu.huang58292b32024-01-03 14:09:51 +0800333 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
334 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
335 * the static qualifier. */
336PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
337PRIVILEGED_DATA static List_t xDelayedTaskList1; /*< Delayed tasks. */
338PRIVILEGED_DATA static List_t xDelayedTaskList2; /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
339PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /*< Points to the delayed task list currently being used. */
340PRIVILEGED_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. */
341PRIVILEGED_DATA static List_t xPendingReadyList; /*< Tasks that have been readied while the scheduler was suspended. They will be moved to the ready list when the scheduler is resumed. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800342
xiaohu.huang58292b32024-01-03 14:09:51 +0800343#if ( INCLUDE_vTaskDelete == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800344
xiaohu.huang58292b32024-01-03 14:09:51 +0800345 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /*< Tasks that have been deleted - but their memory not yet freed. */
346 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800347
348#endif
349
350#if ( INCLUDE_vTaskSuspend == 1 )
351
xiaohu.huang58292b32024-01-03 14:09:51 +0800352 PRIVILEGED_DATA static List_t xSuspendedTaskList; /*< Tasks that are currently suspended. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800353
354#endif
355
356/* Global POSIX errno. Its value is changed upon context switching to match
xiaohu.huang58292b32024-01-03 14:09:51 +0800357 * the errno of the currently running task. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800358#if ( configUSE_POSIX_ERRNO == 1 )
xiaohu.huang58292b32024-01-03 14:09:51 +0800359 int FreeRTOS_errno = 0;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800360#endif
361
362/* Other file private variables. --------------------------------*/
xiaohu.huang58292b32024-01-03 14:09:51 +0800363PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
364PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
365PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
366PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
367PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
368PRIVILEGED_DATA static volatile BaseType_t xYieldPending = pdFALSE;
369PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
370PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
371PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
372PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle = NULL; /*< Holds the handle of the idle task. The idle task is created automatically when the scheduler is started. */
373
374/* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
375 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
376 * to determine the number of priority lists to read back from the remote target. */
377const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800378
379/* Context switches are held pending while the scheduler is suspended. Also,
xiaohu.huang58292b32024-01-03 14:09:51 +0800380 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
381 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
382 * If an interrupt needs to unblock a task while the scheduler is suspended then it
383 * moves the task's event list item into the xPendingReadyList, ready for the
384 * kernel to move the task from the pending ready list into the real ready list
385 * when the scheduler is unsuspended. The pending ready list itself can only be
386 * accessed from a critical section. */
387PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800388
389#if ( configGENERATE_RUN_TIME_STATS == 1 )
390
xiaohu.huang58292b32024-01-03 14:09:51 +0800391/* Do not move these variables to function scope as doing so prevents the
392 * code working with debuggers that need to remove the static qualifier. */
393 PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime = 0UL; /*< Holds the value of a timer/counter the last time a task was switched in. */
394 PRIVILEGED_DATA static volatile configRUN_TIME_COUNTER_TYPE ulTotalRunTime = 0UL; /*< Holds the total amount of execution time as defined by the run time counter clock. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800395
396#endif
397
398/*lint -restore */
399
400/*-----------------------------------------------------------*/
401
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800402/* File private functions. --------------------------------*/
403
404/**
405 * Utility task that simply returns pdTRUE if the task referenced by xTask is
406 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
407 * is in any other state.
408 */
409#if ( INCLUDE_vTaskSuspend == 1 )
410
xiaohu.huang58292b32024-01-03 14:09:51 +0800411 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800412
413#endif /* INCLUDE_vTaskSuspend */
414
415/*
416 * Utility to ready all the lists used by the scheduler. This is called
417 * automatically upon the creation of the first task.
418 */
419static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
420
421/*
422 * The idle task, which as all tasks is implemented as a never ending loop.
423 * The idle task is automatically created and added to the ready lists upon
424 * creation of the first user task.
425 *
426 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
427 * language extensions. The equivalent prototype for this function is:
428 *
429 * void prvIdleTask( void *pvParameters );
430 *
431 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800432static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800433
434/*
435 * Utility to free all memory allocated by the scheduler to hold a TCB,
436 * including the stack pointed to by the TCB.
437 *
438 * This does not free memory allocated by the task itself (i.e. memory
439 * allocated by calls to pvPortMalloc from within the tasks application code).
440 */
441#if ( INCLUDE_vTaskDelete == 1 )
442
xiaohu.huang58292b32024-01-03 14:09:51 +0800443 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800444
445#endif
446
447/*
448 * Used only by the idle task. This checks to see if anything has been placed
449 * in the list of tasks waiting to be deleted. If so the task is cleaned up
450 * and its TCB deleted.
451 */
452static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
453
454/*
455 * The currently executing task is entering the Blocked state. Add the task to
456 * either the current or the overflow delayed task list.
457 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800458static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
459 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800460
461/*
462 * Fills an TaskStatus_t structure with information on each task that is
463 * referenced from the pxList list (which may be a ready list, a delayed list,
464 * a suspended list, etc.).
465 *
466 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
467 * NORMAL APPLICATION CODE.
468 */
469#if ( configUSE_TRACE_FACILITY == 1 )
470
xiaohu.huang58292b32024-01-03 14:09:51 +0800471 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
472 List_t * pxList,
473 eTaskState eState ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800474
475#endif
476
477/*
478 * Searches pxList for a task with name pcNameToQuery - returning a handle to
479 * the task if it is found, or NULL if the task is not found.
480 */
481#if ( INCLUDE_xTaskGetHandle == 1 )
482
xiaohu.huang58292b32024-01-03 14:09:51 +0800483 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
484 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800485
486#endif
487
488/*
489 * When a task is created, the stack of the task is filled with a known value.
490 * This function determines the 'high water mark' of the task stack by
491 * determining how much of the stack remains at the original preset value.
492 */
493#if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
494
xiaohu.huang58292b32024-01-03 14:09:51 +0800495 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800496
497#endif
498
499/*
500 * Return the amount of time, in ticks, that will pass before the kernel will
501 * next move a task from the Blocked state to the Running state.
502 *
503 * This conditional compilation should use inequality to 0, not equality to 1.
504 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
505 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
506 * set to a value other than 1.
507 */
508#if ( configUSE_TICKLESS_IDLE != 0 )
509
xiaohu.huang58292b32024-01-03 14:09:51 +0800510 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800511
512#endif
513
514/*
515 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
516 * will exit the Blocked state.
517 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800518static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800519
xiaohu.huang58292b32024-01-03 14:09:51 +0800520#if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800521
xiaohu.huang58292b32024-01-03 14:09:51 +0800522/*
523 * Helper function used to pad task names with spaces when printing out
524 * human readable tables of task information.
525 */
526 static char * prvWriteNameToBuffer( char * pcBuffer,
527 const char * pcTaskName ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800528
529#endif
530
531/*
532 * Called after a Task_t structure has been allocated either statically or
533 * dynamically to fill in the structure's members.
534 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800535static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
536 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
xiaohu.huang79d92512024-02-21 14:33:03 +0800537 const configSTACK_DEPTH_TYPE ulStackDepth,
xiaohu.huang58292b32024-01-03 14:09:51 +0800538 void * const pvParameters,
539 UBaseType_t uxPriority,
540 TaskHandle_t * const pxCreatedTask,
541 TCB_t * pxNewTCB,
542 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800543
544/*
545 * Called after a new task has been created and initialised to place the task
546 * under the control of the scheduler.
547 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800548static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800549
550/*
551 * freertos_tasks_c_additions_init() should only be called if the user definable
552 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
553 * called by the function.
554 */
555#ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
556
xiaohu.huang58292b32024-01-03 14:09:51 +0800557 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800558
559#endif
560
561/*-----------------------------------------------------------*/
562
xiaohu.huang58292b32024-01-03 14:09:51 +0800563#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800564
xiaohu.huang58292b32024-01-03 14:09:51 +0800565 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
566 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
xiaohu.huang79d92512024-02-21 14:33:03 +0800567 const configSTACK_DEPTH_TYPE ulStackDepth,
xiaohu.huang58292b32024-01-03 14:09:51 +0800568 void * const pvParameters,
569 UBaseType_t uxPriority,
570 StackType_t * const puxStackBuffer,
571 StaticTask_t * const pxTaskBuffer )
572 {
573 TCB_t * pxNewTCB;
574 TaskHandle_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800575
xiaohu.huang58292b32024-01-03 14:09:51 +0800576 configASSERT( puxStackBuffer != NULL );
577 configASSERT( pxTaskBuffer != NULL );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800578
xiaohu.huang58292b32024-01-03 14:09:51 +0800579 #if ( configASSERT_DEFINED == 1 )
580 {
581 /* Sanity check that the size of the structure used to declare a
582 * variable of type StaticTask_t equals the size of the real task
583 * structure. */
584 volatile size_t xSize = sizeof( StaticTask_t );
585 configASSERT( xSize == sizeof( TCB_t ) );
586 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
587 }
588 #endif /* configASSERT_DEFINED */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800589
xiaohu.huang58292b32024-01-03 14:09:51 +0800590 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
591 {
592 /* The memory used for the task's TCB and stack are passed into this
593 * function - use them. */
594 pxNewTCB = ( TCB_t * ) pxTaskBuffer; /*lint !e740 !e9087 Unusual cast is ok as the structures are designed to have the same alignment, and the size is checked by an assert. */
595 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
596 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800597
xiaohu.huang58292b32024-01-03 14:09:51 +0800598 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
599 {
600 /* Tasks can be created statically or dynamically, so note this
601 * task was created statically in case the task is later deleted. */
602 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
603 }
604 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800605
xiaohu.huang58292b32024-01-03 14:09:51 +0800606 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
607 prvAddNewTaskToReadyList( pxNewTCB );
608 }
609 else
610 {
611 xReturn = NULL;
612 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800613
xiaohu.huang58292b32024-01-03 14:09:51 +0800614 return xReturn;
615 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800616
617#endif /* SUPPORT_STATIC_ALLOCATION */
618/*-----------------------------------------------------------*/
619
xiaohu.huang58292b32024-01-03 14:09:51 +0800620#if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800621
xiaohu.huang58292b32024-01-03 14:09:51 +0800622 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
623 TaskHandle_t * pxCreatedTask )
624 {
625 TCB_t * pxNewTCB;
626 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800627
xiaohu.huang58292b32024-01-03 14:09:51 +0800628 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
629 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800630
xiaohu.huang58292b32024-01-03 14:09:51 +0800631 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
632 {
633 /* Allocate space for the TCB. Where the memory comes from depends
634 * on the implementation of the port malloc function and whether or
635 * not static allocation is being used. */
636 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
637 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800638
xiaohu.huang58292b32024-01-03 14:09:51 +0800639 /* Store the stack location in the TCB. */
640 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800641
xiaohu.huang58292b32024-01-03 14:09:51 +0800642 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
643 {
644 /* Tasks can be created statically or dynamically, so note this
645 * task was created statically in case the task is later deleted. */
646 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
647 }
648 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800649
xiaohu.huang58292b32024-01-03 14:09:51 +0800650 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
651 pxTaskDefinition->pcName,
xiaohu.huang79d92512024-02-21 14:33:03 +0800652 ( configSTACK_DEPTH_TYPE ) pxTaskDefinition->usStackDepth,
xiaohu.huang58292b32024-01-03 14:09:51 +0800653 pxTaskDefinition->pvParameters,
654 pxTaskDefinition->uxPriority,
655 pxCreatedTask, pxNewTCB,
656 pxTaskDefinition->xRegions );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800657
xiaohu.huang58292b32024-01-03 14:09:51 +0800658 prvAddNewTaskToReadyList( pxNewTCB );
659 xReturn = pdPASS;
660 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800661
xiaohu.huang58292b32024-01-03 14:09:51 +0800662 return xReturn;
663 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800664
665#endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
666/*-----------------------------------------------------------*/
667
xiaohu.huang58292b32024-01-03 14:09:51 +0800668#if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800669
xiaohu.huang58292b32024-01-03 14:09:51 +0800670 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
671 TaskHandle_t * pxCreatedTask )
672 {
673 TCB_t * pxNewTCB;
674 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800675
xiaohu.huang58292b32024-01-03 14:09:51 +0800676 configASSERT( pxTaskDefinition->puxStackBuffer );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800677
xiaohu.huang58292b32024-01-03 14:09:51 +0800678 if( pxTaskDefinition->puxStackBuffer != NULL )
679 {
680 /* Allocate space for the TCB. Where the memory comes from depends
681 * on the implementation of the port malloc function and whether or
682 * not static allocation is being used. */
683 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800684
xiaohu.huang58292b32024-01-03 14:09:51 +0800685 if( pxNewTCB != NULL )
686 {
687 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800688
xiaohu.huang58292b32024-01-03 14:09:51 +0800689 /* Store the stack location in the TCB. */
690 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800691
xiaohu.huang58292b32024-01-03 14:09:51 +0800692 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
693 {
694 /* Tasks can be created statically or dynamically, so note
695 * this task had a statically allocated stack in case it is
696 * later deleted. The TCB was allocated dynamically. */
697 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
698 }
699 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800700
xiaohu.huang58292b32024-01-03 14:09:51 +0800701 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
702 pxTaskDefinition->pcName,
xiaohu.huang79d92512024-02-21 14:33:03 +0800703 ( configSTACK_DEPTH_TYPE ) pxTaskDefinition->usStackDepth,
xiaohu.huang58292b32024-01-03 14:09:51 +0800704 pxTaskDefinition->pvParameters,
705 pxTaskDefinition->uxPriority,
706 pxCreatedTask, pxNewTCB,
707 pxTaskDefinition->xRegions );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800708
xiaohu.huang58292b32024-01-03 14:09:51 +0800709 prvAddNewTaskToReadyList( pxNewTCB );
710 xReturn = pdPASS;
711 }
712 }
713
714 return xReturn;
715 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800716
717#endif /* portUSING_MPU_WRAPPERS */
718/*-----------------------------------------------------------*/
719
xiaohu.huang58292b32024-01-03 14:09:51 +0800720#if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800721
xiaohu.huang58292b32024-01-03 14:09:51 +0800722 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
723 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
724 const configSTACK_DEPTH_TYPE usStackDepth,
725 void * const pvParameters,
726 UBaseType_t uxPriority,
727 TaskHandle_t * const pxCreatedTask )
728 {
729 TCB_t * pxNewTCB;
730 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800731
xiaohu.huang58292b32024-01-03 14:09:51 +0800732 /* If the stack grows down then allocate the stack then the TCB so the stack
733 * does not grow into the TCB. Likewise if the stack grows up then allocate
734 * the TCB then the stack. */
735 #if ( portSTACK_GROWTH > 0 )
736 {
737 /* Allocate space for the TCB. Where the memory comes from depends on
738 * the implementation of the port malloc function and whether or not static
739 * allocation is being used. */
740 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
Xiaohu.Huang2c96ef42021-10-15 16:12:27 +0800741
xiaohu.huang58292b32024-01-03 14:09:51 +0800742 if( pxNewTCB != NULL )
743 {
744 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800745
xiaohu.huang58292b32024-01-03 14:09:51 +0800746 /* Allocate space for the stack used by the task being created.
747 * The base of the stack memory stored in the TCB so the task can
748 * be deleted later if required. */
749 pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800750
xiaohu.huang58292b32024-01-03 14:09:51 +0800751 if( pxNewTCB->pxStack == NULL )
752 {
753 /* Could not allocate the stack. Delete the allocated TCB. */
754 vPortFree( pxNewTCB );
755 pxNewTCB = NULL;
756 }
757 }
758 }
759 #else /* portSTACK_GROWTH */
760 {
761 StackType_t * pxStack;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800762
xiaohu.huang58292b32024-01-03 14:09:51 +0800763 /* Allocate space for the stack used by the task being created. */
764 pxStack = pvPortMallocStack( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation is the stack. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800765
xiaohu.huang58292b32024-01-03 14:09:51 +0800766 if( pxStack != NULL )
767 {
768 /* Allocate space for the TCB. */
769 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) ); /*lint !e9087 !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack, and the first member of TCB_t is always a pointer to the task's stack. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800770
xiaohu.huang58292b32024-01-03 14:09:51 +0800771 if( pxNewTCB != NULL )
772 {
773 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800774
xiaohu.huang58292b32024-01-03 14:09:51 +0800775 /* Store the stack location in the TCB. */
776 pxNewTCB->pxStack = pxStack;
777 }
778 else
779 {
780 /* The stack cannot be used as the TCB was not created. Free
781 * it again. */
782 vPortFreeStack( pxStack );
783 }
784 }
785 else
786 {
787 pxNewTCB = NULL;
788 }
789 }
790 #endif /* portSTACK_GROWTH */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800791
xiaohu.huang58292b32024-01-03 14:09:51 +0800792 if( pxNewTCB != NULL )
793 {
794 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
795 {
796 /* Tasks can be created statically or dynamically, so note this
797 * task was created dynamically in case it is later deleted. */
798 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
799 }
800 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800801
xiaohu.huang79d92512024-02-21 14:33:03 +0800802 prvInitialiseNewTask( pxTaskCode, pcName, ( configSTACK_DEPTH_TYPE ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
xiaohu.huang58292b32024-01-03 14:09:51 +0800803 prvAddNewTaskToReadyList( pxNewTCB );
804 xReturn = pdPASS;
805 }
806 else
807 {
808 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
809 }
810
811 return xReturn;
812 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800813
814#endif /* configSUPPORT_DYNAMIC_ALLOCATION */
815/*-----------------------------------------------------------*/
xiaohu.huang58292b32024-01-03 14:09:51 +0800816
817static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
818 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
xiaohu.huang79d92512024-02-21 14:33:03 +0800819 const configSTACK_DEPTH_TYPE ulStackDepth,
xiaohu.huang58292b32024-01-03 14:09:51 +0800820 void * const pvParameters,
821 UBaseType_t uxPriority,
822 TaskHandle_t * const pxCreatedTask,
823 TCB_t * pxNewTCB,
824 const MemoryRegion_t * const xRegions )
Xiaohu.Huang2c96ef42021-10-15 16:12:27 +0800825{
xiaohu.huang58292b32024-01-03 14:09:51 +0800826 StackType_t * pxTopOfStack;
827 UBaseType_t x;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800828
xiaohu.huang58292b32024-01-03 14:09:51 +0800829 #if ( portUSING_MPU_WRAPPERS == 1 )
830 /* Should the task be created in privileged mode? */
831 BaseType_t xRunPrivileged;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800832
xiaohu.huang58292b32024-01-03 14:09:51 +0800833 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
834 {
835 xRunPrivileged = pdTRUE;
836 }
837 else
838 {
839 xRunPrivileged = pdFALSE;
840 }
841 uxPriority &= ~portPRIVILEGE_BIT;
842 #endif /* portUSING_MPU_WRAPPERS == 1 */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800843
shijie.xiong02c0d642024-02-20 10:37:19 +0800844 pxNewTCB->xStackDepth = ulStackDepth;
845
xiaohu.huang58292b32024-01-03 14:09:51 +0800846 /* Avoid dependency on memset() if it is not required. */
847 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
848 {
849 /* Fill the stack with a known value to assist debugging. */
850 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
851 }
852 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
Xiaohu.Huang2c96ef42021-10-15 16:12:27 +0800853
xiaohu.huang58292b32024-01-03 14:09:51 +0800854 /* Calculate the top of stack address. This depends on whether the stack
855 * grows from high memory to low (as per the 80x86) or vice versa.
856 * portSTACK_GROWTH is used to make the result positive or negative as required
857 * by the port. */
858 #if ( portSTACK_GROWTH < 0 )
859 {
xiaohu.huang79d92512024-02-21 14:33:03 +0800860 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( configSTACK_DEPTH_TYPE ) 1 ] );
xiaohu.huang58292b32024-01-03 14:09:51 +0800861 pxTopOfStack = ( StackType_t * ) ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); /*lint !e923 !e9033 !e9078 MISRA exception. Avoiding casts between pointers and integers is not practical. Size differences accounted for using portPOINTER_SIZE_TYPE type. Checked by assert(). */
Xiaohu.Huang2c96ef42021-10-15 16:12:27 +0800862
xiaohu.huang58292b32024-01-03 14:09:51 +0800863 /* Check the alignment of the calculated top of stack is correct. */
864 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800865
xiaohu.huang58292b32024-01-03 14:09:51 +0800866 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
867 {
868 /* Also record the stack's high address, which may assist
869 * debugging. */
870 pxNewTCB->pxEndOfStack = pxTopOfStack;
871 }
872 #endif /* configRECORD_STACK_HIGH_ADDRESS */
873 }
874 #else /* portSTACK_GROWTH */
875 {
876 pxTopOfStack = pxNewTCB->pxStack;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800877
xiaohu.huang58292b32024-01-03 14:09:51 +0800878 /* Check the alignment of the stack buffer is correct. */
879 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800880
xiaohu.huang58292b32024-01-03 14:09:51 +0800881 /* The other extreme of the stack space is required if stack checking is
882 * performed. */
xiaohu.huang79d92512024-02-21 14:33:03 +0800883 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( configSTACK_DEPTH_TYPE ) 1 );
xiaohu.huang58292b32024-01-03 14:09:51 +0800884 }
885 #endif /* portSTACK_GROWTH */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800886
xiaohu.huang58292b32024-01-03 14:09:51 +0800887 /* Store the task name in the TCB. */
888 if( pcName != NULL )
889 {
890 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
891 {
892 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800893
xiaohu.huang58292b32024-01-03 14:09:51 +0800894 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
895 * configMAX_TASK_NAME_LEN characters just in case the memory after the
896 * string is not accessible (extremely unlikely). */
897 if( pcName[ x ] == ( char ) 0x00 )
898 {
899 break;
900 }
901 else
902 {
903 mtCOVERAGE_TEST_MARKER();
904 }
905 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800906
xiaohu.huang58292b32024-01-03 14:09:51 +0800907 /* Ensure the name string is terminated in the case that the string length
908 * was greater or equal to configMAX_TASK_NAME_LEN. */
909 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
910 }
911 else
912 {
913 mtCOVERAGE_TEST_MARKER();
914 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800915
xiaohu.huang58292b32024-01-03 14:09:51 +0800916 /* This is used as an array index so must ensure it's not too large. */
917 configASSERT( uxPriority < configMAX_PRIORITIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800918
xiaohu.huang58292b32024-01-03 14:09:51 +0800919 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
920 {
921 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
922 }
923 else
924 {
925 mtCOVERAGE_TEST_MARKER();
926 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800927
xiaohu.huang58292b32024-01-03 14:09:51 +0800928 pxNewTCB->uxPriority = uxPriority;
929 #if ( configUSE_MUTEXES == 1 )
930 {
931 pxNewTCB->uxBasePriority = uxPriority;
932 }
933 #endif /* configUSE_MUTEXES */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800934
xiaohu.huang58292b32024-01-03 14:09:51 +0800935 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
936 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800937
xiaohu.huang58292b32024-01-03 14:09:51 +0800938 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
939 * back to the containing TCB from a generic item in a list. */
940 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800941
xiaohu.huang58292b32024-01-03 14:09:51 +0800942 /* Event lists are always in priority order. */
943 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. */
944 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800945
xiaohu.huang58292b32024-01-03 14:09:51 +0800946 #if ( portUSING_MPU_WRAPPERS == 1 )
947 {
948 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
949 }
950 #else
951 {
952 /* Avoid compiler warning about unreferenced parameter. */
953 ( void ) xRegions;
954 }
955 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800956
xiaohu.huang58292b32024-01-03 14:09:51 +0800957 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
958 {
959 /* Allocate and initialize memory for the task's TLS Block. */
960 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock );
961 }
962 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800963
xiaohu.huang58292b32024-01-03 14:09:51 +0800964 /* Initialize the TCB stack to look as if the task was already running,
965 * but had been interrupted by the scheduler. The return address is set
966 * to the start of the task function. Once the stack has been initialised
967 * the top of stack variable is updated. */
968 #if ( portUSING_MPU_WRAPPERS == 1 )
969 {
970 /* If the port has capability to detect stack overflow,
971 * pass the stack end address to the stack initialization
972 * function as well. */
973 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
974 {
975 #if ( portSTACK_GROWTH < 0 )
976 {
977 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
978 }
979 #else /* portSTACK_GROWTH */
980 {
981 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
982 }
983 #endif /* portSTACK_GROWTH */
984 }
985 #else /* portHAS_STACK_OVERFLOW_CHECKING */
986 {
987 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
988 }
989 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
990 }
991 #else /* portUSING_MPU_WRAPPERS */
992 {
993 /* If the port has capability to detect stack overflow,
994 * pass the stack end address to the stack initialization
995 * function as well. */
996 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
997 {
998 #if ( portSTACK_GROWTH < 0 )
999 {
1000 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1001 }
1002 #else /* portSTACK_GROWTH */
1003 {
1004 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1005 }
1006 #endif /* portSTACK_GROWTH */
1007 }
1008 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1009 {
1010 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1011 }
1012 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1013 }
1014 #endif /* portUSING_MPU_WRAPPERS */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001015
xiaohu.huang58292b32024-01-03 14:09:51 +08001016 if( pxCreatedTask != NULL )
1017 {
1018 /* Pass the handle out in an anonymous way. The handle can be used to
1019 * change the created task's priority, delete the created task, etc.*/
1020 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1021 }
1022 else
1023 {
1024 mtCOVERAGE_TEST_MARKER();
1025 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001026}
1027/*-----------------------------------------------------------*/
1028
xiaohu.huang58292b32024-01-03 14:09:51 +08001029static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001030{
xiaohu.huang58292b32024-01-03 14:09:51 +08001031 /* Ensure interrupts don't access the task lists while the lists are being
1032 * updated. */
1033 taskENTER_CRITICAL();
1034 {
1035 uxCurrentNumberOfTasks++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001036
xiaohu.huang58292b32024-01-03 14:09:51 +08001037 if( pxCurrentTCB == NULL )
1038 {
1039 /* There are no other tasks, or all the other tasks are in
1040 * the suspended state - make this the current task. */
1041 pxCurrentTCB = pxNewTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001042
xiaohu.huang58292b32024-01-03 14:09:51 +08001043 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1044 {
1045 /* This is the first task to be created so do the preliminary
1046 * initialisation required. We will not recover if this call
1047 * fails, but we will report the failure. */
1048 prvInitialiseTaskLists();
1049 }
1050 else
1051 {
1052 mtCOVERAGE_TEST_MARKER();
1053 }
1054 }
1055 else
1056 {
1057 /* If the scheduler is not already running, make this task the
1058 * current task if it is the highest priority task to be created
1059 * so far. */
1060 if( xSchedulerRunning == pdFALSE )
1061 {
1062 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
1063 {
1064 pxCurrentTCB = pxNewTCB;
1065 }
1066 else
1067 {
1068 mtCOVERAGE_TEST_MARKER();
1069 }
1070 }
1071 else
1072 {
1073 mtCOVERAGE_TEST_MARKER();
1074 }
1075 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001076
xiaohu.huang58292b32024-01-03 14:09:51 +08001077 uxTaskNumber++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001078
xiaohu.huang58292b32024-01-03 14:09:51 +08001079 #if ( configUSE_TRACE_FACILITY == 1 )
1080 {
1081 /* Add a counter into the TCB for tracing only. */
1082 pxNewTCB->uxTCBNumber = uxTaskNumber;
1083 }
1084 #endif /* configUSE_TRACE_FACILITY */
1085 traceTASK_CREATE( pxNewTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001086
xiaohu.huang58292b32024-01-03 14:09:51 +08001087 prvAddTaskToReadyList( pxNewTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001088
xiaohu.huang58292b32024-01-03 14:09:51 +08001089 portSETUP_TCB( pxNewTCB );
1090 }
1091 taskEXIT_CRITICAL();
1092
1093 if( xSchedulerRunning != pdFALSE )
1094 {
1095 /* If the created task is of a higher priority than the current task
1096 * then it should run now. */
1097 if( pxCurrentTCB->uxPriority < pxNewTCB->uxPriority )
1098 {
1099 taskYIELD_IF_USING_PREEMPTION();
1100 }
1101 else
1102 {
1103 mtCOVERAGE_TEST_MARKER();
1104 }
1105 }
1106 else
1107 {
1108 mtCOVERAGE_TEST_MARKER();
1109 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001110}
1111/*-----------------------------------------------------------*/
1112
1113#if ( INCLUDE_vTaskDelete == 1 )
1114
xiaohu.huang58292b32024-01-03 14:09:51 +08001115 void vTaskDelete( TaskHandle_t xTaskToDelete )
1116 {
1117 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001118
xiaohu.huang58292b32024-01-03 14:09:51 +08001119 taskENTER_CRITICAL();
1120 {
1121 /* If null is passed in here then it is the calling task that is
1122 * being deleted. */
1123 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001124
xiaohu.huang58292b32024-01-03 14:09:51 +08001125 /* Remove task from the ready/delayed list. */
1126 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1127 {
1128 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1129 }
1130 else
1131 {
1132 mtCOVERAGE_TEST_MARKER();
1133 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001134
xiaohu.huang58292b32024-01-03 14:09:51 +08001135 /* Is the task waiting on an event also? */
1136 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1137 {
1138 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1139 }
1140 else
1141 {
1142 mtCOVERAGE_TEST_MARKER();
1143 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001144
xiaohu.huang58292b32024-01-03 14:09:51 +08001145 /* Increment the uxTaskNumber also so kernel aware debuggers can
1146 * detect that the task lists need re-generating. This is done before
1147 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1148 * not return. */
1149 uxTaskNumber++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001150
xiaohu.huang58292b32024-01-03 14:09:51 +08001151 if( pxTCB == pxCurrentTCB )
1152 {
1153 /* A task is deleting itself. This cannot complete within the
1154 * task itself, as a context switch to another task is required.
1155 * Place the task in the termination list. The idle task will
1156 * check the termination list and free up any memory allocated by
1157 * the scheduler for the TCB and stack of the deleted task. */
1158 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001159
xiaohu.huang58292b32024-01-03 14:09:51 +08001160 /* Increment the ucTasksDeleted variable so the idle task knows
1161 * there is a task that has been deleted and that it should therefore
1162 * check the xTasksWaitingTermination list. */
1163 ++uxDeletedTasksWaitingCleanUp;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001164
xiaohu.huang58292b32024-01-03 14:09:51 +08001165 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1166 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1167 traceTASK_DELETE( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001168
xiaohu.huang58292b32024-01-03 14:09:51 +08001169 /* The pre-delete hook is primarily for the Windows simulator,
1170 * in which Windows specific clean up operations are performed,
1171 * after which it is not possible to yield away from this task -
1172 * hence xYieldPending is used to latch that a context switch is
1173 * required. */
1174 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPending );
1175 }
1176 else
1177 {
1178 --uxCurrentNumberOfTasks;
1179 traceTASK_DELETE( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001180
xiaohu.huang58292b32024-01-03 14:09:51 +08001181 /* Reset the next expected unblock time in case it referred to
1182 * the task that has just been deleted. */
1183 prvResetNextTaskUnblockTime();
1184 }
1185 }
1186 taskEXIT_CRITICAL();
shijie.xiongf9b5e162022-07-14 15:12:48 +08001187
xiaohu.huang58292b32024-01-03 14:09:51 +08001188 /* If the task is not deleting itself, call prvDeleteTCB from outside of
1189 * critical section. If a task deletes itself, prvDeleteTCB is called
1190 * from prvCheckTasksWaitingTermination which is called from Idle task. */
1191 if( pxTCB != pxCurrentTCB )
1192 {
1193 prvDeleteTCB( pxTCB );
1194 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001195
xiaohu.huang58292b32024-01-03 14:09:51 +08001196 /* Force a reschedule if it is the currently running task that has just
1197 * been deleted. */
1198 if( xSchedulerRunning != pdFALSE )
1199 {
1200 if( pxTCB == pxCurrentTCB )
1201 {
1202 configASSERT( uxSchedulerSuspended == 0 );
1203 portYIELD_WITHIN_API();
1204 }
1205 else
1206 {
1207 mtCOVERAGE_TEST_MARKER();
1208 }
1209 }
1210 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001211
1212#endif /* INCLUDE_vTaskDelete */
1213/*-----------------------------------------------------------*/
1214
xiaohu.huang58292b32024-01-03 14:09:51 +08001215#if ( INCLUDE_xTaskDelayUntil == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001216
xiaohu.huang58292b32024-01-03 14:09:51 +08001217 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
1218 const TickType_t xTimeIncrement )
1219 {
1220 TickType_t xTimeToWake;
1221 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001222
xiaohu.huang58292b32024-01-03 14:09:51 +08001223 configASSERT( pxPreviousWakeTime );
1224 configASSERT( ( xTimeIncrement > 0U ) );
1225 configASSERT( uxSchedulerSuspended == 0 );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001226
xiaohu.huang58292b32024-01-03 14:09:51 +08001227 vTaskSuspendAll();
1228 {
1229 /* Minor optimisation. The tick count cannot change in this
1230 * block. */
1231 const TickType_t xConstTickCount = xTickCount;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001232
xiaohu.huang58292b32024-01-03 14:09:51 +08001233 /* Generate the tick time at which the task wants to wake. */
1234 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001235
xiaohu.huang58292b32024-01-03 14:09:51 +08001236 if( xConstTickCount < *pxPreviousWakeTime )
1237 {
1238 /* The tick count has overflowed since this function was
1239 * lasted called. In this case the only time we should ever
1240 * actually delay is if the wake time has also overflowed,
1241 * and the wake time is greater than the tick time. When this
1242 * is the case it is as if neither time had overflowed. */
1243 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1244 {
1245 xShouldDelay = pdTRUE;
1246 }
1247 else
1248 {
1249 mtCOVERAGE_TEST_MARKER();
1250 }
1251 }
1252 else
1253 {
1254 /* The tick time has not overflowed. In this case we will
1255 * delay if either the wake time has overflowed, and/or the
1256 * tick time is less than the wake time. */
1257 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1258 {
1259 xShouldDelay = pdTRUE;
1260 }
1261 else
1262 {
1263 mtCOVERAGE_TEST_MARKER();
1264 }
1265 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001266
xiaohu.huang58292b32024-01-03 14:09:51 +08001267 /* Update the wake time ready for the next call. */
1268 *pxPreviousWakeTime = xTimeToWake;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001269
xiaohu.huang58292b32024-01-03 14:09:51 +08001270 if( xShouldDelay != pdFALSE )
1271 {
1272 traceTASK_DELAY_UNTIL( xTimeToWake );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001273
xiaohu.huang58292b32024-01-03 14:09:51 +08001274 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1275 * the time to wake, so subtract the current tick count. */
1276 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1277 }
1278 else
1279 {
1280 mtCOVERAGE_TEST_MARKER();
1281 }
1282 }
1283 xAlreadyYielded = xTaskResumeAll();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001284
xiaohu.huang58292b32024-01-03 14:09:51 +08001285 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1286 * have put ourselves to sleep. */
1287 if( xAlreadyYielded == pdFALSE )
1288 {
1289 portYIELD_WITHIN_API();
1290 }
1291 else
1292 {
1293 mtCOVERAGE_TEST_MARKER();
1294 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001295
xiaohu.huang58292b32024-01-03 14:09:51 +08001296 return xShouldDelay;
1297 }
1298
1299#endif /* INCLUDE_xTaskDelayUntil */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001300/*-----------------------------------------------------------*/
1301
1302#if ( INCLUDE_vTaskDelay == 1 )
1303
xiaohu.huang58292b32024-01-03 14:09:51 +08001304 void vTaskDelay( const TickType_t xTicksToDelay )
1305 {
1306 BaseType_t xAlreadyYielded = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001307
xiaohu.huang58292b32024-01-03 14:09:51 +08001308 /* A delay time of zero just forces a reschedule. */
1309 if( xTicksToDelay > ( TickType_t ) 0U )
1310 {
1311 configASSERT( uxSchedulerSuspended == 0 );
1312 vTaskSuspendAll();
1313 {
1314 traceTASK_DELAY();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001315
xiaohu.huang58292b32024-01-03 14:09:51 +08001316 /* A task that is removed from the event list while the
1317 * scheduler is suspended will not get placed in the ready
1318 * list or removed from the blocked list until the scheduler
1319 * is resumed.
1320 *
1321 * This task cannot be in an event list as it is the currently
1322 * executing task. */
1323 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1324 }
1325 xAlreadyYielded = xTaskResumeAll();
1326 }
1327 else
1328 {
1329 mtCOVERAGE_TEST_MARKER();
1330 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001331
xiaohu.huang58292b32024-01-03 14:09:51 +08001332 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1333 * have put ourselves to sleep. */
1334 if( xAlreadyYielded == pdFALSE )
1335 {
1336 portYIELD_WITHIN_API();
1337 }
1338 else
1339 {
1340 mtCOVERAGE_TEST_MARKER();
1341 }
1342 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001343
1344#endif /* INCLUDE_vTaskDelay */
1345/*-----------------------------------------------------------*/
1346
xiaohu.huang58292b32024-01-03 14:09:51 +08001347#if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001348
xiaohu.huang58292b32024-01-03 14:09:51 +08001349 eTaskState eTaskGetState( TaskHandle_t xTask )
1350 {
1351 eTaskState eReturn;
1352 List_t const * pxStateList;
1353 List_t const * pxDelayedList;
1354 List_t const * pxOverflowedDelayedList;
1355 const TCB_t * const pxTCB = xTask;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001356
xiaohu.huang58292b32024-01-03 14:09:51 +08001357 configASSERT( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001358
xiaohu.huang58292b32024-01-03 14:09:51 +08001359 if( pxTCB == pxCurrentTCB )
1360 {
1361 /* The task calling this function is querying its own state. */
1362 eReturn = eRunning;
1363 }
1364 else
1365 {
1366 taskENTER_CRITICAL();
1367 {
1368 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1369 pxDelayedList = pxDelayedTaskList;
1370 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
1371 }
1372 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001373
xiaohu.huang58292b32024-01-03 14:09:51 +08001374 if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
1375 {
1376 /* The task being queried is referenced from one of the Blocked
1377 * lists. */
1378 eReturn = eBlocked;
1379 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001380
xiaohu.huang58292b32024-01-03 14:09:51 +08001381 #if ( INCLUDE_vTaskSuspend == 1 )
1382 else if( pxStateList == &xSuspendedTaskList )
1383 {
1384 /* The task being queried is referenced from the suspended
1385 * list. Is it genuinely suspended or is it blocked
1386 * indefinitely? */
1387 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
1388 {
1389 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1390 {
1391 BaseType_t x;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001392
xiaohu.huang58292b32024-01-03 14:09:51 +08001393 /* The task does not appear on the event list item of
1394 * and of the RTOS objects, but could still be in the
1395 * blocked state if it is waiting on its notification
1396 * rather than waiting on an object. If not, is
1397 * suspended. */
1398 eReturn = eSuspended;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001399
xiaohu.huang58292b32024-01-03 14:09:51 +08001400 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1401 {
1402 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1403 {
1404 eReturn = eBlocked;
1405 break;
1406 }
1407 }
1408 }
1409 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1410 {
1411 eReturn = eSuspended;
1412 }
1413 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1414 }
1415 else
1416 {
1417 eReturn = eBlocked;
1418 }
1419 }
1420 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001421
xiaohu.huang58292b32024-01-03 14:09:51 +08001422 #if ( INCLUDE_vTaskDelete == 1 )
1423 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
1424 {
1425 /* The task being queried is referenced from the deleted
1426 * tasks list, or it is not referenced from any lists at
1427 * all. */
1428 eReturn = eDeleted;
1429 }
1430 #endif
1431
1432 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
1433 {
1434 /* If the task is not in any other state, it must be in the
1435 * Ready (including pending ready) state. */
1436 eReturn = eReady;
1437 }
1438 }
1439
1440 return eReturn;
1441 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001442
1443#endif /* INCLUDE_eTaskGetState */
1444/*-----------------------------------------------------------*/
1445
1446#if ( INCLUDE_uxTaskPriorityGet == 1 )
1447
xiaohu.huang58292b32024-01-03 14:09:51 +08001448 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
1449 {
1450 TCB_t const * pxTCB;
1451 UBaseType_t uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001452
xiaohu.huang58292b32024-01-03 14:09:51 +08001453 taskENTER_CRITICAL();
1454 {
1455 /* If null is passed in here then it is the priority of the task
1456 * that called uxTaskPriorityGet() that is being queried. */
1457 pxTCB = prvGetTCBFromHandle( xTask );
1458 uxReturn = pxTCB->uxPriority;
1459 }
1460 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001461
xiaohu.huang58292b32024-01-03 14:09:51 +08001462 return uxReturn;
1463 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001464
1465#endif /* INCLUDE_uxTaskPriorityGet */
1466/*-----------------------------------------------------------*/
1467
1468#if ( INCLUDE_uxTaskPriorityGet == 1 )
1469
xiaohu.huang58292b32024-01-03 14:09:51 +08001470 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
1471 {
1472 TCB_t const * pxTCB;
1473 UBaseType_t uxReturn, uxSavedInterruptState;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001474
xiaohu.huang58292b32024-01-03 14:09:51 +08001475 /* RTOS ports that support interrupt nesting have the concept of a
1476 * maximum system call (or maximum API call) interrupt priority.
1477 * Interrupts that are above the maximum system call priority are keep
1478 * permanently enabled, even when the RTOS kernel is in a critical section,
1479 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
1480 * is defined in FreeRTOSConfig.h then
1481 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1482 * failure if a FreeRTOS API function is called from an interrupt that has
1483 * been assigned a priority above the configured maximum system call
1484 * priority. Only FreeRTOS functions that end in FromISR can be called
1485 * from interrupts that have been assigned a priority at or (logically)
1486 * below the maximum system call interrupt priority. FreeRTOS maintains a
1487 * separate interrupt safe API to ensure interrupt entry is as fast and as
1488 * simple as possible. More information (albeit Cortex-M specific) is
1489 * provided on the following link:
1490 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1491 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001492
xiaohu.huang58292b32024-01-03 14:09:51 +08001493 uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
1494 {
1495 /* If null is passed in here then it is the priority of the calling
1496 * task that is being queried. */
1497 pxTCB = prvGetTCBFromHandle( xTask );
1498 uxReturn = pxTCB->uxPriority;
1499 }
1500 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001501
xiaohu.huang58292b32024-01-03 14:09:51 +08001502 return uxReturn;
1503 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001504
1505#endif /* INCLUDE_uxTaskPriorityGet */
1506/*-----------------------------------------------------------*/
1507
1508#if ( INCLUDE_vTaskPrioritySet == 1 )
1509
xiaohu.huang58292b32024-01-03 14:09:51 +08001510 void vTaskPrioritySet( TaskHandle_t xTask,
1511 UBaseType_t uxNewPriority )
1512 {
1513 TCB_t * pxTCB;
1514 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
1515 BaseType_t xYieldRequired = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001516
xiaohu.huang58292b32024-01-03 14:09:51 +08001517 configASSERT( uxNewPriority < configMAX_PRIORITIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001518
xiaohu.huang58292b32024-01-03 14:09:51 +08001519 /* Ensure the new priority is valid. */
1520 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1521 {
1522 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1523 }
1524 else
1525 {
1526 mtCOVERAGE_TEST_MARKER();
1527 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001528
xiaohu.huang58292b32024-01-03 14:09:51 +08001529 taskENTER_CRITICAL();
1530 {
1531 /* If null is passed in here then it is the priority of the calling
1532 * task that is being changed. */
1533 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001534
xiaohu.huang58292b32024-01-03 14:09:51 +08001535 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001536
xiaohu.huang58292b32024-01-03 14:09:51 +08001537 #if ( configUSE_MUTEXES == 1 )
1538 {
1539 uxCurrentBasePriority = pxTCB->uxBasePriority;
1540 }
1541 #else
1542 {
1543 uxCurrentBasePriority = pxTCB->uxPriority;
1544 }
1545 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001546
xiaohu.huang58292b32024-01-03 14:09:51 +08001547 if( uxCurrentBasePriority != uxNewPriority )
1548 {
1549 /* The priority change may have readied a task of higher
1550 * priority than the calling task. */
1551 if( uxNewPriority > uxCurrentBasePriority )
1552 {
1553 if( pxTCB != pxCurrentTCB )
1554 {
1555 /* The priority of a task other than the currently
1556 * running task is being raised. Is the priority being
1557 * raised above that of the running task? */
1558 if( uxNewPriority >= pxCurrentTCB->uxPriority )
1559 {
1560 xYieldRequired = pdTRUE;
1561 }
1562 else
1563 {
1564 mtCOVERAGE_TEST_MARKER();
1565 }
1566 }
1567 else
1568 {
1569 /* The priority of the running task is being raised,
1570 * but the running task must already be the highest
1571 * priority task able to run so no yield is required. */
1572 }
1573 }
1574 else if( pxTCB == pxCurrentTCB )
1575 {
1576 /* Setting the priority of the running task down means
1577 * there may now be another task of higher priority that
1578 * is ready to execute. */
1579 xYieldRequired = pdTRUE;
1580 }
1581 else
1582 {
1583 /* Setting the priority of any other task down does not
1584 * require a yield as the running task must be above the
1585 * new priority of the task being modified. */
1586 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001587
xiaohu.huang58292b32024-01-03 14:09:51 +08001588 /* Remember the ready list the task might be referenced from
1589 * before its uxPriority member is changed so the
1590 * taskRESET_READY_PRIORITY() macro can function correctly. */
1591 uxPriorityUsedOnEntry = pxTCB->uxPriority;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001592
xiaohu.huang58292b32024-01-03 14:09:51 +08001593 #if ( configUSE_MUTEXES == 1 )
1594 {
1595 /* Only change the priority being used if the task is not
1596 * currently using an inherited priority. */
1597 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
1598 {
1599 pxTCB->uxPriority = uxNewPriority;
1600 }
1601 else
1602 {
1603 mtCOVERAGE_TEST_MARKER();
1604 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001605
xiaohu.huang58292b32024-01-03 14:09:51 +08001606 /* The base priority gets set whatever. */
1607 pxTCB->uxBasePriority = uxNewPriority;
1608 }
1609 #else /* if ( configUSE_MUTEXES == 1 ) */
1610 {
1611 pxTCB->uxPriority = uxNewPriority;
1612 }
1613 #endif /* if ( configUSE_MUTEXES == 1 ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001614
xiaohu.huang58292b32024-01-03 14:09:51 +08001615 /* Only reset the event list item value if the value is not
1616 * being used for anything else. */
1617 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
1618 {
1619 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. */
1620 }
1621 else
1622 {
1623 mtCOVERAGE_TEST_MARKER();
1624 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001625
xiaohu.huang58292b32024-01-03 14:09:51 +08001626 /* If the task is in the blocked or suspended list we need do
1627 * nothing more than change its priority variable. However, if
1628 * the task is in a ready list it needs to be removed and placed
1629 * in the list appropriate to its new priority. */
1630 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
1631 {
1632 /* The task is currently in its ready list - remove before
1633 * adding it to its new ready list. As we are in a critical
1634 * section we can do this even if the scheduler is suspended. */
1635 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1636 {
1637 /* It is known that the task is in its ready list so
1638 * there is no need to check again and the port level
1639 * reset macro can be called directly. */
1640 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
1641 }
1642 else
1643 {
1644 mtCOVERAGE_TEST_MARKER();
1645 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001646
xiaohu.huang58292b32024-01-03 14:09:51 +08001647 prvAddTaskToReadyList( pxTCB );
1648 }
1649 else
1650 {
1651 mtCOVERAGE_TEST_MARKER();
1652 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001653
xiaohu.huang58292b32024-01-03 14:09:51 +08001654 if( xYieldRequired != pdFALSE )
1655 {
1656 taskYIELD_IF_USING_PREEMPTION();
1657 }
1658 else
1659 {
1660 mtCOVERAGE_TEST_MARKER();
1661 }
1662
1663 /* Remove compiler warning about unused variables when the port
1664 * optimised task selection is not being used. */
1665 ( void ) uxPriorityUsedOnEntry;
1666 }
1667 }
1668 taskEXIT_CRITICAL();
1669 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001670
1671#endif /* INCLUDE_vTaskPrioritySet */
1672/*-----------------------------------------------------------*/
1673
1674#if ( INCLUDE_vTaskSuspend == 1 )
1675
xiaohu.huang58292b32024-01-03 14:09:51 +08001676 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
1677 {
1678 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001679
xiaohu.huang58292b32024-01-03 14:09:51 +08001680 taskENTER_CRITICAL();
1681 {
1682 /* If null is passed in here then it is the running task that is
1683 * being suspended. */
1684 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001685
xiaohu.huang58292b32024-01-03 14:09:51 +08001686 traceTASK_SUSPEND( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001687
xiaohu.huang58292b32024-01-03 14:09:51 +08001688 /* Remove task from the ready/delayed list and place in the
1689 * suspended list. */
1690 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1691 {
1692 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1693 }
1694 else
1695 {
1696 mtCOVERAGE_TEST_MARKER();
1697 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001698
xiaohu.huang58292b32024-01-03 14:09:51 +08001699 /* Is the task waiting on an event also? */
1700 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1701 {
1702 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1703 }
1704 else
1705 {
1706 mtCOVERAGE_TEST_MARKER();
1707 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001708
xiaohu.huang58292b32024-01-03 14:09:51 +08001709 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001710
xiaohu.huang58292b32024-01-03 14:09:51 +08001711 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1712 {
1713 BaseType_t x;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001714
xiaohu.huang58292b32024-01-03 14:09:51 +08001715 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1716 {
1717 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1718 {
1719 /* The task was blocked to wait for a notification, but is
1720 * now suspended, so no notification was received. */
1721 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
1722 }
1723 }
1724 }
1725 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1726 }
1727 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001728
xiaohu.huang58292b32024-01-03 14:09:51 +08001729 if( xSchedulerRunning != pdFALSE )
1730 {
1731 /* Reset the next expected unblock time in case it referred to the
1732 * task that is now in the Suspended state. */
1733 taskENTER_CRITICAL();
1734 {
1735 prvResetNextTaskUnblockTime();
1736 }
1737 taskEXIT_CRITICAL();
1738 }
1739 else
1740 {
1741 mtCOVERAGE_TEST_MARKER();
1742 }
1743
1744 if( pxTCB == pxCurrentTCB )
1745 {
1746 if( xSchedulerRunning != pdFALSE )
1747 {
1748 /* The current task has just been suspended. */
1749 configASSERT( uxSchedulerSuspended == 0 );
1750 portYIELD_WITHIN_API();
1751 }
1752 else
1753 {
1754 /* The scheduler is not running, but the task that was pointed
1755 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
1756 * must be adjusted to point to a different task. */
1757 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
1758 {
1759 /* No other tasks are ready, so set pxCurrentTCB back to
1760 * NULL so when the next task is created pxCurrentTCB will
1761 * be set to point to it no matter what its relative priority
1762 * is. */
1763 pxCurrentTCB = NULL;
1764 }
1765 else
1766 {
1767 vTaskSwitchContext();
1768 }
1769 }
1770 }
1771 else
1772 {
1773 mtCOVERAGE_TEST_MARKER();
1774 }
1775 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001776
1777#endif /* INCLUDE_vTaskSuspend */
1778/*-----------------------------------------------------------*/
1779
1780#if ( INCLUDE_vTaskSuspend == 1 )
1781
xiaohu.huang58292b32024-01-03 14:09:51 +08001782 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
1783 {
1784 BaseType_t xReturn = pdFALSE;
1785 const TCB_t * const pxTCB = xTask;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001786
xiaohu.huang58292b32024-01-03 14:09:51 +08001787 /* Accesses xPendingReadyList so must be called from a critical
1788 * section. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001789
xiaohu.huang58292b32024-01-03 14:09:51 +08001790 /* It does not make sense to check if the calling task is suspended. */
1791 configASSERT( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001792
xiaohu.huang58292b32024-01-03 14:09:51 +08001793 /* Is the task being resumed actually in the suspended list? */
1794 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
1795 {
1796 /* Has the task already been resumed from within an ISR? */
1797 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
1798 {
1799 /* Is it in the suspended list because it is in the Suspended
1800 * state, or because is is blocked with no timeout? */
1801 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
1802 {
1803 xReturn = pdTRUE;
1804 }
1805 else
1806 {
1807 mtCOVERAGE_TEST_MARKER();
1808 }
1809 }
1810 else
1811 {
1812 mtCOVERAGE_TEST_MARKER();
1813 }
1814 }
1815 else
1816 {
1817 mtCOVERAGE_TEST_MARKER();
1818 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001819
xiaohu.huang58292b32024-01-03 14:09:51 +08001820 return xReturn;
1821 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001822
1823#endif /* INCLUDE_vTaskSuspend */
1824/*-----------------------------------------------------------*/
1825
1826#if ( INCLUDE_vTaskSuspend == 1 )
1827
xiaohu.huang58292b32024-01-03 14:09:51 +08001828 void vTaskResume( TaskHandle_t xTaskToResume )
1829 {
1830 TCB_t * const pxTCB = xTaskToResume;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001831
xiaohu.huang58292b32024-01-03 14:09:51 +08001832 /* It does not make sense to resume the calling task. */
1833 configASSERT( xTaskToResume );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001834
xiaohu.huang58292b32024-01-03 14:09:51 +08001835 /* The parameter cannot be NULL as it is impossible to resume the
1836 * currently executing task. */
1837 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
1838 {
1839 taskENTER_CRITICAL();
1840 {
1841 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
1842 {
1843 traceTASK_RESUME( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001844
xiaohu.huang58292b32024-01-03 14:09:51 +08001845 /* The ready list can be accessed even if the scheduler is
1846 * suspended because this is inside a critical section. */
1847 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
1848 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001849
xiaohu.huang58292b32024-01-03 14:09:51 +08001850 /* A higher priority task may have just been resumed. */
1851 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
1852 {
1853 /* This yield may not cause the task just resumed to run,
1854 * but will leave the lists in the correct state for the
1855 * next yield. */
1856 taskYIELD_IF_USING_PREEMPTION();
1857 }
1858 else
1859 {
1860 mtCOVERAGE_TEST_MARKER();
1861 }
1862 }
1863 else
1864 {
1865 mtCOVERAGE_TEST_MARKER();
1866 }
1867 }
1868 taskEXIT_CRITICAL();
1869 }
1870 else
1871 {
1872 mtCOVERAGE_TEST_MARKER();
1873 }
1874 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001875
1876#endif /* INCLUDE_vTaskSuspend */
1877
1878/*-----------------------------------------------------------*/
1879
1880#if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
1881
xiaohu.huang58292b32024-01-03 14:09:51 +08001882 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
1883 {
1884 BaseType_t xYieldRequired = pdFALSE;
1885 TCB_t * const pxTCB = xTaskToResume;
1886 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001887
xiaohu.huang58292b32024-01-03 14:09:51 +08001888 configASSERT( xTaskToResume );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001889
xiaohu.huang58292b32024-01-03 14:09:51 +08001890 /* RTOS ports that support interrupt nesting have the concept of a
1891 * maximum system call (or maximum API call) interrupt priority.
1892 * Interrupts that are above the maximum system call priority are keep
1893 * permanently enabled, even when the RTOS kernel is in a critical section,
1894 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
1895 * is defined in FreeRTOSConfig.h then
1896 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1897 * failure if a FreeRTOS API function is called from an interrupt that has
1898 * been assigned a priority above the configured maximum system call
1899 * priority. Only FreeRTOS functions that end in FromISR can be called
1900 * from interrupts that have been assigned a priority at or (logically)
1901 * below the maximum system call interrupt priority. FreeRTOS maintains a
1902 * separate interrupt safe API to ensure interrupt entry is as fast and as
1903 * simple as possible. More information (albeit Cortex-M specific) is
1904 * provided on the following link:
1905 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1906 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001907
xiaohu.huang58292b32024-01-03 14:09:51 +08001908 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1909 {
1910 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
1911 {
1912 traceTASK_RESUME_FROM_ISR( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001913
xiaohu.huang58292b32024-01-03 14:09:51 +08001914 /* Check the ready lists can be accessed. */
1915 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
1916 {
1917 /* Ready lists can be accessed so move the task from the
1918 * suspended list to the ready list directly. */
1919 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
1920 {
1921 xYieldRequired = pdTRUE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001922
xiaohu.huang58292b32024-01-03 14:09:51 +08001923 /* Mark that a yield is pending in case the user is not
1924 * using the return value to initiate a context switch
1925 * from the ISR using portYIELD_FROM_ISR. */
1926 xYieldPending = pdTRUE;
1927 }
1928 else
1929 {
1930 mtCOVERAGE_TEST_MARKER();
1931 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001932
xiaohu.huang58292b32024-01-03 14:09:51 +08001933 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
1934 prvAddTaskToReadyList( pxTCB );
1935 }
1936 else
1937 {
1938 /* The delayed or ready lists cannot be accessed so the task
1939 * is held in the pending ready list until the scheduler is
1940 * unsuspended. */
1941 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
1942 }
1943 }
1944 else
1945 {
1946 mtCOVERAGE_TEST_MARKER();
1947 }
1948 }
1949 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1950
1951 return xYieldRequired;
1952 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001953
1954#endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
1955/*-----------------------------------------------------------*/
1956
1957void vTaskStartScheduler( void )
1958{
xiaohu.huang58292b32024-01-03 14:09:51 +08001959 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001960
xiaohu.huang58292b32024-01-03 14:09:51 +08001961 /* Add the idle task at the lowest priority. */
1962 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1963 {
1964 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
1965 StackType_t * pxIdleTaskStackBuffer = NULL;
xiaohu.huang79d92512024-02-21 14:33:03 +08001966 configSTACK_DEPTH_TYPE ulIdleTaskStackSize;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001967
xiaohu.huang58292b32024-01-03 14:09:51 +08001968 /* The Idle task is created using user provided RAM - obtain the
1969 * address of the RAM then create the idle task. */
1970 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
1971 xIdleTaskHandle = xTaskCreateStatic( prvIdleTask,
1972 configIDLE_TASK_NAME,
1973 ulIdleTaskStackSize,
1974 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
1975 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
1976 pxIdleTaskStackBuffer,
1977 pxIdleTaskTCBBuffer ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001978
xiaohu.huang58292b32024-01-03 14:09:51 +08001979 if( xIdleTaskHandle != NULL )
1980 {
1981 xReturn = pdPASS;
1982 }
1983 else
1984 {
1985 xReturn = pdFAIL;
1986 }
1987 }
1988 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1989 {
1990 /* The Idle task is being created using dynamically allocated RAM. */
1991 xReturn = xTaskCreate( prvIdleTask,
1992 configIDLE_TASK_NAME,
1993 configMINIMAL_STACK_SIZE,
1994 ( void * ) NULL,
1995 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
1996 &xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
1997 }
1998 #endif /* configSUPPORT_STATIC_ALLOCATION */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001999
xiaohu.huang58292b32024-01-03 14:09:51 +08002000 #if ( configUSE_TIMERS == 1 )
2001 {
2002 if( xReturn == pdPASS )
2003 {
2004 xReturn = xTimerCreateTimerTask();
2005 }
2006 else
2007 {
2008 mtCOVERAGE_TEST_MARKER();
2009 }
2010 }
2011 #endif /* configUSE_TIMERS */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002012
xiaohu.huang58292b32024-01-03 14:09:51 +08002013 if( xReturn == pdPASS )
2014 {
2015 /* freertos_tasks_c_additions_init() should only be called if the user
2016 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
2017 * the only macro called by the function. */
2018 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
2019 {
2020 freertos_tasks_c_additions_init();
2021 }
2022 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002023
xiaohu.huang58292b32024-01-03 14:09:51 +08002024 /* Interrupts are turned off here, to ensure a tick does not occur
2025 * before or during the call to xPortStartScheduler(). The stacks of
2026 * the created tasks contain a status word with interrupts switched on
2027 * so interrupts will automatically get re-enabled when the first task
2028 * starts to run. */
2029 portDISABLE_INTERRUPTS();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002030
xiaohu.huang58292b32024-01-03 14:09:51 +08002031 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
2032 {
2033 /* Switch C-Runtime's TLS Block to point to the TLS
2034 * block specific to the task that will run first. */
2035 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
2036 }
2037 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002038
xiaohu.huang58292b32024-01-03 14:09:51 +08002039 xNextTaskUnblockTime = portMAX_DELAY;
2040 xSchedulerRunning = pdTRUE;
2041 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002042
xiaohu.huang58292b32024-01-03 14:09:51 +08002043 /* If configGENERATE_RUN_TIME_STATS is defined then the following
2044 * macro must be defined to configure the timer/counter used to generate
2045 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
2046 * is set to 0 and the following line fails to build then ensure you do not
2047 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
2048 * FreeRTOSConfig.h file. */
2049 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002050
xiaohu.huang58292b32024-01-03 14:09:51 +08002051 traceTASK_SWITCHED_IN();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002052
xiaohu.huang58292b32024-01-03 14:09:51 +08002053 /* Setting up the timer tick is hardware specific and thus in the
2054 * portable interface. */
2055 xPortStartScheduler();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002056
xiaohu.huang58292b32024-01-03 14:09:51 +08002057 /* In most cases, xPortStartScheduler() will not return. If it
2058 * returns pdTRUE then there was not enough heap memory available
2059 * to create either the Idle or the Timer task. If it returned
2060 * pdFALSE, then the application called xTaskEndScheduler().
2061 * Most ports don't implement xTaskEndScheduler() as there is
2062 * nothing to return to. */
2063 }
2064 else
2065 {
2066 /* This line will only be reached if the kernel could not be started,
2067 * because there was not enough FreeRTOS heap to create the idle task
2068 * or the timer task. */
2069 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
2070 }
2071
2072 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
2073 * meaning xIdleTaskHandle is not used anywhere else. */
2074 ( void ) xIdleTaskHandle;
2075
2076 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
2077 * from getting optimized out as it is no longer used by the kernel. */
2078 ( void ) uxTopUsedPriority;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002079}
2080/*-----------------------------------------------------------*/
2081
2082void vTaskEndScheduler( void )
2083{
xiaohu.huang58292b32024-01-03 14:09:51 +08002084 /* Stop the scheduler interrupts and call the portable scheduler end
2085 * routine so the original ISRs can be restored if necessary. The port
2086 * layer must ensure interrupts enable bit is left in the correct state. */
2087 portDISABLE_INTERRUPTS();
2088 xSchedulerRunning = pdFALSE;
2089 vPortEndScheduler();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002090}
2091/*----------------------------------------------------------*/
2092
2093void vTaskSuspendAll( void )
2094{
xiaohu.huang58292b32024-01-03 14:09:51 +08002095 /* A critical section is not required as the variable is of type
2096 * BaseType_t. Please read Richard Barry's reply in the following link to a
2097 * post in the FreeRTOS support forum before reporting this as a bug! -
2098 * https://goo.gl/wu4acr */
2099
2100 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
2101 * do not otherwise exhibit real time behaviour. */
2102 portSOFTWARE_BARRIER();
2103
2104 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
2105 * is used to allow calls to vTaskSuspendAll() to nest. */
2106 ++uxSchedulerSuspended;
2107
2108 /* Enforces ordering for ports and optimised compilers that may otherwise place
2109 * the above increment elsewhere. */
2110 portMEMORY_BARRIER();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002111}
2112/*----------------------------------------------------------*/
2113
2114#if ( configUSE_TICKLESS_IDLE != 0 )
2115
xiaohu.huang58292b32024-01-03 14:09:51 +08002116 static TickType_t prvGetExpectedIdleTime( void )
2117 {
2118 TickType_t xReturn;
2119 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002120
xiaohu.huang58292b32024-01-03 14:09:51 +08002121 /* uxHigherPriorityReadyTasks takes care of the case where
2122 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
2123 * task that are in the Ready state, even though the idle task is
2124 * running. */
2125 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
2126 {
2127 if( uxTopReadyPriority > tskIDLE_PRIORITY )
2128 {
2129 uxHigherPriorityReadyTasks = pdTRUE;
2130 }
2131 }
2132 #else
2133 {
2134 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002135
xiaohu.huang58292b32024-01-03 14:09:51 +08002136 /* When port optimised task selection is used the uxTopReadyPriority
2137 * variable is used as a bit map. If bits other than the least
2138 * significant bit are set then there are tasks that have a priority
2139 * above the idle priority that are in the Ready state. This takes
2140 * care of the case where the co-operative scheduler is in use. */
2141 if( uxTopReadyPriority > uxLeastSignificantBit )
2142 {
2143 uxHigherPriorityReadyTasks = pdTRUE;
2144 }
2145 }
2146 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002147
xiaohu.huang58292b32024-01-03 14:09:51 +08002148 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
2149 {
2150 xReturn = 0;
2151 }
2152 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
2153 {
2154 /* There are other idle priority tasks in the ready state. If
2155 * time slicing is used then the very next tick interrupt must be
2156 * processed. */
2157 xReturn = 0;
2158 }
2159 else if( uxHigherPriorityReadyTasks != pdFALSE )
2160 {
2161 /* There are tasks in the Ready state that have a priority above the
2162 * idle priority. This path can only be reached if
2163 * configUSE_PREEMPTION is 0. */
2164 xReturn = 0;
2165 }
2166 else
2167 {
2168 xReturn = xNextTaskUnblockTime - xTickCount;
2169 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002170
xiaohu.huang58292b32024-01-03 14:09:51 +08002171 return xReturn;
2172 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002173
2174#endif /* configUSE_TICKLESS_IDLE */
2175/*----------------------------------------------------------*/
2176
2177BaseType_t xTaskResumeAll( void )
2178{
xiaohu.huang58292b32024-01-03 14:09:51 +08002179 TCB_t * pxTCB = NULL;
2180 BaseType_t xAlreadyYielded = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002181
xiaohu.huang58292b32024-01-03 14:09:51 +08002182 /* If uxSchedulerSuspended is zero then this function does not match a
2183 * previous call to vTaskSuspendAll(). */
2184 configASSERT( uxSchedulerSuspended );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002185
xiaohu.huang58292b32024-01-03 14:09:51 +08002186 /* It is possible that an ISR caused a task to be removed from an event
2187 * list while the scheduler was suspended. If this was the case then the
2188 * removed task will have been added to the xPendingReadyList. Once the
2189 * scheduler has been resumed it is safe to move all the pending ready
2190 * tasks from this list into their appropriate ready list. */
2191 taskENTER_CRITICAL();
2192 {
2193 --uxSchedulerSuspended;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002194
xiaohu.huang58292b32024-01-03 14:09:51 +08002195 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2196 {
2197 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
2198 {
2199 /* Move any readied tasks from the pending list into the
2200 * appropriate ready list. */
2201 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
2202 {
2203 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xPendingReadyList ) ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
2204 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
2205 portMEMORY_BARRIER();
2206 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
2207 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002208
xiaohu.huang58292b32024-01-03 14:09:51 +08002209 /* If the moved task has a priority higher than or equal to
2210 * the current task then a yield must be performed. */
2211 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
2212 {
2213 xYieldPending = pdTRUE;
2214 }
2215 else
2216 {
2217 mtCOVERAGE_TEST_MARKER();
2218 }
2219 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002220
xiaohu.huang58292b32024-01-03 14:09:51 +08002221 if( pxTCB != NULL )
2222 {
2223 /* A task was unblocked while the scheduler was suspended,
2224 * which may have prevented the next unblock time from being
2225 * re-calculated, in which case re-calculate it now. Mainly
2226 * important for low power tickless implementations, where
2227 * this can prevent an unnecessary exit from low power
2228 * state. */
2229 prvResetNextTaskUnblockTime();
2230 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002231
xiaohu.huang58292b32024-01-03 14:09:51 +08002232 /* If any ticks occurred while the scheduler was suspended then
2233 * they should be processed now. This ensures the tick count does
2234 * not slip, and that any delayed tasks are resumed at the correct
2235 * time. */
2236 {
2237 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002238
xiaohu.huang58292b32024-01-03 14:09:51 +08002239 if( xPendedCounts > ( TickType_t ) 0U )
2240 {
2241 do
2242 {
2243 if( xTaskIncrementTick() != pdFALSE )
2244 {
2245 xYieldPending = pdTRUE;
2246 }
2247 else
2248 {
2249 mtCOVERAGE_TEST_MARKER();
2250 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002251
xiaohu.huang58292b32024-01-03 14:09:51 +08002252 --xPendedCounts;
2253 } while( xPendedCounts > ( TickType_t ) 0U );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002254
xiaohu.huang58292b32024-01-03 14:09:51 +08002255 xPendedTicks = 0;
2256 }
2257 else
2258 {
2259 mtCOVERAGE_TEST_MARKER();
2260 }
2261 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002262
xiaohu.huang58292b32024-01-03 14:09:51 +08002263 if( xYieldPending != pdFALSE )
2264 {
2265 #if ( configUSE_PREEMPTION != 0 )
2266 {
2267 xAlreadyYielded = pdTRUE;
2268 }
2269 #endif
2270 taskYIELD_IF_USING_PREEMPTION();
2271 }
2272 else
2273 {
2274 mtCOVERAGE_TEST_MARKER();
2275 }
2276 }
2277 }
2278 else
2279 {
2280 mtCOVERAGE_TEST_MARKER();
2281 }
2282 }
2283 taskEXIT_CRITICAL();
2284
2285 return xAlreadyYielded;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002286}
2287/*-----------------------------------------------------------*/
2288
2289TickType_t xTaskGetTickCount( void )
2290{
xiaohu.huang58292b32024-01-03 14:09:51 +08002291 TickType_t xTicks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002292
xiaohu.huang58292b32024-01-03 14:09:51 +08002293 /* Critical section required if running on a 16 bit processor. */
2294 portTICK_TYPE_ENTER_CRITICAL();
2295 {
2296 xTicks = xTickCount;
2297 }
2298 portTICK_TYPE_EXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002299
xiaohu.huang58292b32024-01-03 14:09:51 +08002300 return xTicks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002301}
2302/*-----------------------------------------------------------*/
2303
2304TickType_t xTaskGetTickCountFromISR( void )
2305{
xiaohu.huang58292b32024-01-03 14:09:51 +08002306 TickType_t xReturn;
2307 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002308
xiaohu.huang58292b32024-01-03 14:09:51 +08002309 /* RTOS ports that support interrupt nesting have the concept of a maximum
2310 * system call (or maximum API call) interrupt priority. Interrupts that are
2311 * above the maximum system call priority are kept permanently enabled, even
2312 * when the RTOS kernel is in a critical section, but cannot make any calls to
2313 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
2314 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2315 * failure if a FreeRTOS API function is called from an interrupt that has been
2316 * assigned a priority above the configured maximum system call priority.
2317 * Only FreeRTOS functions that end in FromISR can be called from interrupts
2318 * that have been assigned a priority at or (logically) below the maximum
2319 * system call interrupt priority. FreeRTOS maintains a separate interrupt
2320 * safe API to ensure interrupt entry is as fast and as simple as possible.
2321 * More information (albeit Cortex-M specific) is provided on the following
2322 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2323 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002324
xiaohu.huang58292b32024-01-03 14:09:51 +08002325 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
2326 {
2327 xReturn = xTickCount;
2328 }
2329 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002330
xiaohu.huang58292b32024-01-03 14:09:51 +08002331 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002332}
2333/*-----------------------------------------------------------*/
2334
2335UBaseType_t uxTaskGetNumberOfTasks( void )
2336{
xiaohu.huang58292b32024-01-03 14:09:51 +08002337 /* A critical section is not required because the variables are of type
2338 * BaseType_t. */
2339 return uxCurrentNumberOfTasks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002340}
2341/*-----------------------------------------------------------*/
2342
xiaohu.huang58292b32024-01-03 14:09:51 +08002343char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002344{
xiaohu.huang58292b32024-01-03 14:09:51 +08002345 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002346
xiaohu.huang58292b32024-01-03 14:09:51 +08002347 /* If null is passed in here then the name of the calling task is being
2348 * queried. */
2349 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
2350 configASSERT( pxTCB );
2351 return &( pxTCB->pcTaskName[ 0 ] );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002352}
xiaohu.huang58292b32024-01-03 14:09:51 +08002353/*-----------------------------------------------------------*/
Kelvin Zhang7f929772021-12-31 17:58:17 +08002354
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002355#if ( INCLUDE_xTaskGetHandle == 1 )
2356
xiaohu.huang58292b32024-01-03 14:09:51 +08002357 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
2358 const char pcNameToQuery[] )
2359 {
2360 TCB_t * pxNextTCB;
2361 TCB_t * pxFirstTCB;
2362 TCB_t * pxReturn = NULL;
2363 UBaseType_t x;
2364 char cNextChar;
2365 BaseType_t xBreakLoop;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002366
xiaohu.huang58292b32024-01-03 14:09:51 +08002367 /* This function is called with the scheduler suspended. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002368
xiaohu.huang58292b32024-01-03 14:09:51 +08002369 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
2370 {
2371 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002372
xiaohu.huang58292b32024-01-03 14:09:51 +08002373 do
2374 {
2375 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002376
xiaohu.huang58292b32024-01-03 14:09:51 +08002377 /* Check each character in the name looking for a match or
2378 * mismatch. */
2379 xBreakLoop = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002380
xiaohu.huang58292b32024-01-03 14:09:51 +08002381 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2382 {
2383 cNextChar = pxNextTCB->pcTaskName[ x ];
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002384
xiaohu.huang58292b32024-01-03 14:09:51 +08002385 if( cNextChar != pcNameToQuery[ x ] )
2386 {
2387 /* Characters didn't match. */
2388 xBreakLoop = pdTRUE;
2389 }
2390 else if( cNextChar == ( char ) 0x00 )
2391 {
2392 /* Both strings terminated, a match must have been
2393 * found. */
2394 pxReturn = pxNextTCB;
2395 xBreakLoop = pdTRUE;
2396 }
2397 else
2398 {
2399 mtCOVERAGE_TEST_MARKER();
2400 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002401
xiaohu.huang58292b32024-01-03 14:09:51 +08002402 if( xBreakLoop != pdFALSE )
2403 {
2404 break;
2405 }
2406 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002407
xiaohu.huang58292b32024-01-03 14:09:51 +08002408 if( pxReturn != NULL )
2409 {
2410 /* The handle has been found. */
2411 break;
2412 }
2413 } while( pxNextTCB != pxFirstTCB );
2414 }
2415 else
2416 {
2417 mtCOVERAGE_TEST_MARKER();
2418 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002419
xiaohu.huang58292b32024-01-03 14:09:51 +08002420 return pxReturn;
2421 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002422
2423#endif /* INCLUDE_xTaskGetHandle */
2424/*-----------------------------------------------------------*/
2425
2426#if ( INCLUDE_xTaskGetHandle == 1 )
2427
xiaohu.huang58292b32024-01-03 14:09:51 +08002428 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2429 {
2430 UBaseType_t uxQueue = configMAX_PRIORITIES;
2431 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002432
xiaohu.huang58292b32024-01-03 14:09:51 +08002433 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
2434 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002435
xiaohu.huang58292b32024-01-03 14:09:51 +08002436 vTaskSuspendAll();
2437 {
2438 /* Search the ready lists. */
2439 do
2440 {
2441 uxQueue--;
2442 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002443
xiaohu.huang58292b32024-01-03 14:09:51 +08002444 if( pxTCB != NULL )
2445 {
2446 /* Found the handle. */
2447 break;
2448 }
2449 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002450
xiaohu.huang58292b32024-01-03 14:09:51 +08002451 /* Search the delayed lists. */
2452 if( pxTCB == NULL )
2453 {
2454 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
2455 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002456
xiaohu.huang58292b32024-01-03 14:09:51 +08002457 if( pxTCB == NULL )
2458 {
2459 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
2460 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002461
xiaohu.huang58292b32024-01-03 14:09:51 +08002462 #if ( INCLUDE_vTaskSuspend == 1 )
2463 {
2464 if( pxTCB == NULL )
2465 {
2466 /* Search the suspended list. */
2467 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
2468 }
2469 }
2470 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002471
xiaohu.huang58292b32024-01-03 14:09:51 +08002472 #if ( INCLUDE_vTaskDelete == 1 )
2473 {
2474 if( pxTCB == NULL )
2475 {
2476 /* Search the deleted list. */
2477 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
2478 }
2479 }
2480 #endif
2481 }
2482 ( void ) xTaskResumeAll();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002483
xiaohu.huang58292b32024-01-03 14:09:51 +08002484 return pxTCB;
2485 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002486
2487#endif /* INCLUDE_xTaskGetHandle */
2488/*-----------------------------------------------------------*/
2489
2490#if ( configUSE_TRACE_FACILITY == 1 )
2491
xiaohu.huang58292b32024-01-03 14:09:51 +08002492 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
2493 const UBaseType_t uxArraySize,
2494 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
2495 {
2496 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002497
xiaohu.huang58292b32024-01-03 14:09:51 +08002498 vTaskSuspendAll();
2499 {
2500 /* Is there a space in the array for each task in the system? */
2501 if( uxArraySize >= uxCurrentNumberOfTasks )
2502 {
2503 /* Fill in an TaskStatus_t structure with information on each
2504 * task in the Ready state. */
2505 do
2506 {
2507 uxQueue--;
2508 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
2509 } while( uxQueue > ( UBaseType_t ) tskIDLE_PRIORITY ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002510
xiaohu.huang58292b32024-01-03 14:09:51 +08002511 /* Fill in an TaskStatus_t structure with information on each
2512 * task in the Blocked state. */
2513 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
2514 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002515
xiaohu.huang58292b32024-01-03 14:09:51 +08002516 #if ( INCLUDE_vTaskDelete == 1 )
2517 {
2518 /* Fill in an TaskStatus_t structure with information on
2519 * each task that has been deleted but not yet cleaned up. */
2520 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
2521 }
2522 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002523
xiaohu.huang58292b32024-01-03 14:09:51 +08002524 #if ( INCLUDE_vTaskSuspend == 1 )
2525 {
2526 /* Fill in an TaskStatus_t structure with information on
2527 * each task in the Suspended state. */
2528 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
2529 }
2530 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002531
xiaohu.huang58292b32024-01-03 14:09:51 +08002532 #if ( configGENERATE_RUN_TIME_STATS == 1 )
2533 {
2534 if( pulTotalRunTime != NULL )
2535 {
2536 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
2537 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
2538 #else
2539 *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
2540 #endif
2541 }
2542 }
2543 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
2544 {
2545 if( pulTotalRunTime != NULL )
2546 {
2547 *pulTotalRunTime = 0;
2548 }
2549 }
2550 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
2551 }
2552 else
2553 {
2554 mtCOVERAGE_TEST_MARKER();
2555 }
2556 }
2557 ( void ) xTaskResumeAll();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002558
xiaohu.huang58292b32024-01-03 14:09:51 +08002559 return uxTask;
2560 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002561
2562#endif /* configUSE_TRACE_FACILITY */
2563/*----------------------------------------------------------*/
2564
2565#if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
2566
xiaohu.huang58292b32024-01-03 14:09:51 +08002567 TaskHandle_t xTaskGetIdleTaskHandle( void )
2568 {
2569 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
2570 * started, then xIdleTaskHandle will be NULL. */
2571 configASSERT( ( xIdleTaskHandle != NULL ) );
2572 return xIdleTaskHandle;
2573 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002574
2575#endif /* INCLUDE_xTaskGetIdleTaskHandle */
2576/*----------------------------------------------------------*/
2577
2578/* This conditional compilation should use inequality to 0, not equality to 1.
xiaohu.huang58292b32024-01-03 14:09:51 +08002579 * This is to ensure vTaskStepTick() is available when user defined low power mode
2580 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
2581 * 1. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002582#if ( configUSE_TICKLESS_IDLE != 0 )
2583
xiaohu.huang58292b32024-01-03 14:09:51 +08002584 void vTaskStepTick( TickType_t xTicksToJump )
2585 {
2586 /* Correct the tick count value after a period during which the tick
2587 * was suppressed. Note this does *not* call the tick hook function for
2588 * each stepped tick. */
2589 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
2590
2591 if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
2592 {
2593 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
2594 * xTaskIncrementTick() when the scheduler resumes. This ensures
2595 * that any delayed tasks are resumed at the correct time. */
2596 configASSERT( uxSchedulerSuspended );
2597 configASSERT( xTicksToJump != ( TickType_t ) 0 );
2598
2599 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
2600 taskENTER_CRITICAL();
2601 {
2602 xPendedTicks++;
2603 }
2604 taskEXIT_CRITICAL();
2605 xTicksToJump--;
2606 }
2607 else
2608 {
2609 mtCOVERAGE_TEST_MARKER();
2610 }
2611
2612 xTickCount += xTicksToJump;
2613 traceINCREASE_TICK_COUNT( xTicksToJump );
2614 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002615
2616#endif /* configUSE_TICKLESS_IDLE */
2617/*----------------------------------------------------------*/
2618
xiaohu.huang58292b32024-01-03 14:09:51 +08002619BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
2620{
2621 BaseType_t xYieldOccurred;
2622
2623 /* Must not be called with the scheduler suspended as the implementation
2624 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
2625 configASSERT( uxSchedulerSuspended == 0 );
2626
2627 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
2628 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
2629 vTaskSuspendAll();
2630
2631 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
2632 taskENTER_CRITICAL();
2633 {
2634 xPendedTicks += xTicksToCatchUp;
2635 }
2636 taskEXIT_CRITICAL();
2637 xYieldOccurred = xTaskResumeAll();
2638
2639 return xYieldOccurred;
2640}
2641/*----------------------------------------------------------*/
2642
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002643#if ( INCLUDE_xTaskAbortDelay == 1 )
2644
xiaohu.huang58292b32024-01-03 14:09:51 +08002645 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
2646 {
2647 TCB_t * pxTCB = xTask;
2648 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002649
xiaohu.huang58292b32024-01-03 14:09:51 +08002650 configASSERT( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002651
xiaohu.huang58292b32024-01-03 14:09:51 +08002652 vTaskSuspendAll();
2653 {
2654 /* A task can only be prematurely removed from the Blocked state if
2655 * it is actually in the Blocked state. */
2656 if( eTaskGetState( xTask ) == eBlocked )
2657 {
2658 xReturn = pdPASS;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002659
xiaohu.huang58292b32024-01-03 14:09:51 +08002660 /* Remove the reference to the task from the blocked list. An
2661 * interrupt won't touch the xStateListItem because the
2662 * scheduler is suspended. */
2663 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002664
xiaohu.huang58292b32024-01-03 14:09:51 +08002665 /* Is the task waiting on an event also? If so remove it from
2666 * the event list too. Interrupts can touch the event list item,
2667 * even though the scheduler is suspended, so a critical section
2668 * is used. */
2669 taskENTER_CRITICAL();
2670 {
2671 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2672 {
2673 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002674
xiaohu.huang58292b32024-01-03 14:09:51 +08002675 /* This lets the task know it was forcibly removed from the
2676 * blocked state so it should not re-evaluate its block time and
2677 * then block again. */
2678 pxTCB->ucDelayAborted = pdTRUE;
2679 }
2680 else
2681 {
2682 mtCOVERAGE_TEST_MARKER();
2683 }
2684 }
2685 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002686
xiaohu.huang58292b32024-01-03 14:09:51 +08002687 /* Place the unblocked task into the appropriate ready list. */
2688 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002689
xiaohu.huang58292b32024-01-03 14:09:51 +08002690 /* A task being unblocked cannot cause an immediate context
2691 * switch if preemption is turned off. */
2692 #if ( configUSE_PREEMPTION == 1 )
2693 {
2694 /* Preemption is on, but a context switch should only be
2695 * performed if the unblocked task has a priority that is
2696 * higher than the currently executing task. */
2697 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
2698 {
2699 /* Pend the yield to be performed when the scheduler
2700 * is unsuspended. */
2701 xYieldPending = pdTRUE;
2702 }
2703 else
2704 {
2705 mtCOVERAGE_TEST_MARKER();
2706 }
2707 }
2708 #endif /* configUSE_PREEMPTION */
2709 }
2710 else
2711 {
2712 xReturn = pdFAIL;
2713 }
2714 }
2715 ( void ) xTaskResumeAll();
2716
2717 return xReturn;
2718 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002719
2720#endif /* INCLUDE_xTaskAbortDelay */
2721/*----------------------------------------------------------*/
2722
2723BaseType_t xTaskIncrementTick( void )
2724{
xiaohu.huang58292b32024-01-03 14:09:51 +08002725 TCB_t * pxTCB;
2726 TickType_t xItemValue;
2727 BaseType_t xSwitchRequired = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002728
xiaohu.huang58292b32024-01-03 14:09:51 +08002729 /* Called by the portable layer each time a tick interrupt occurs.
2730 * Increments the tick then checks to see if the new tick value will cause any
2731 * tasks to be unblocked. */
2732 traceTASK_INCREMENT_TICK( xTickCount );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002733
xiaohu.huang58292b32024-01-03 14:09:51 +08002734 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2735 {
2736 /* Minor optimisation. The tick count cannot change in this
2737 * block. */
2738 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002739
xiaohu.huang58292b32024-01-03 14:09:51 +08002740 /* Increment the RTOS tick, switching the delayed and overflowed
2741 * delayed lists if it wraps to 0. */
2742 xTickCount = xConstTickCount;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002743
xiaohu.huang58292b32024-01-03 14:09:51 +08002744 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
2745 {
2746 taskSWITCH_DELAYED_LISTS();
2747 }
2748 else
2749 {
2750 mtCOVERAGE_TEST_MARKER();
2751 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002752
xiaohu.huang58292b32024-01-03 14:09:51 +08002753 /* See if this tick has made a timeout expire. Tasks are stored in
2754 * the queue in the order of their wake time - meaning once one task
2755 * has been found whose block time has not expired there is no need to
2756 * look any further down the list. */
2757 if( xConstTickCount >= xNextTaskUnblockTime )
2758 {
2759 for( ; ; )
2760 {
2761 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
2762 {
2763 /* The delayed list is empty. Set xNextTaskUnblockTime
2764 * to the maximum possible value so it is extremely
2765 * unlikely that the
2766 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
2767 * next time through. */
2768 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2769 break;
2770 }
2771 else
2772 {
2773 /* The delayed list is not empty, get the value of the
2774 * item at the head of the delayed list. This is the time
2775 * at which the task at the head of the delayed list must
2776 * be removed from the Blocked state. */
2777 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( pxDelayedTaskList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
2778 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002779
xiaohu.huang58292b32024-01-03 14:09:51 +08002780 if( xConstTickCount < xItemValue )
2781 {
2782 /* It is not time to unblock this item yet, but the
2783 * item value is the time at which the task at the head
2784 * of the blocked list must be removed from the Blocked
2785 * state - so record the item value in
2786 * xNextTaskUnblockTime. */
2787 xNextTaskUnblockTime = xItemValue;
2788 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
2789 }
2790 else
2791 {
2792 mtCOVERAGE_TEST_MARKER();
2793 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002794
xiaohu.huang58292b32024-01-03 14:09:51 +08002795 /* It is time to remove the item from the Blocked state. */
2796 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002797
xiaohu.huang58292b32024-01-03 14:09:51 +08002798 /* Is the task waiting on an event also? If so remove
2799 * it from the event list. */
2800 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2801 {
2802 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
2803 }
2804 else
2805 {
2806 mtCOVERAGE_TEST_MARKER();
2807 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002808
xiaohu.huang58292b32024-01-03 14:09:51 +08002809 /* Place the unblocked task into the appropriate ready
2810 * list. */
2811 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002812
xiaohu.huang58292b32024-01-03 14:09:51 +08002813 /* A task being unblocked cannot cause an immediate
2814 * context switch if preemption is turned off. */
2815 #if ( configUSE_PREEMPTION == 1 )
2816 {
2817 /* Preemption is on, but a context switch should
2818 * only be performed if the unblocked task's
2819 * priority is higher than the currently executing
2820 * task.
2821 * The case of equal priority tasks sharing
2822 * processing time (which happens when both
2823 * preemption and time slicing are on) is
2824 * handled below.*/
2825 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
2826 {
2827 xSwitchRequired = pdTRUE;
2828 }
2829 else
2830 {
2831 mtCOVERAGE_TEST_MARKER();
2832 }
2833 }
2834 #endif /* configUSE_PREEMPTION */
2835 }
2836 }
2837 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002838
xiaohu.huang58292b32024-01-03 14:09:51 +08002839 /* Tasks of equal priority to the currently running task will share
2840 * processing time (time slice) if preemption is on, and the application
2841 * writer has not explicitly turned time slicing off. */
2842 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
2843 {
2844 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
2845 {
2846 xSwitchRequired = pdTRUE;
2847 }
2848 else
2849 {
2850 mtCOVERAGE_TEST_MARKER();
2851 }
2852 }
2853 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002854
xiaohu.huang58292b32024-01-03 14:09:51 +08002855 #if ( configUSE_TICK_HOOK == 1 )
2856 {
2857 /* Guard against the tick hook being called when the pended tick
2858 * count is being unwound (when the scheduler is being unlocked). */
2859 if( xPendedTicks == ( TickType_t ) 0 )
2860 {
2861 vApplicationTickHook();
2862 }
2863 else
2864 {
2865 mtCOVERAGE_TEST_MARKER();
2866 }
2867 }
2868 #endif /* configUSE_TICK_HOOK */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002869
xiaohu.huang58292b32024-01-03 14:09:51 +08002870 #if ( configUSE_PREEMPTION == 1 )
2871 {
2872 if( xYieldPending != pdFALSE )
2873 {
2874 xSwitchRequired = pdTRUE;
2875 }
2876 else
2877 {
2878 mtCOVERAGE_TEST_MARKER();
2879 }
2880 }
2881 #endif /* configUSE_PREEMPTION */
2882 }
2883 else
2884 {
2885 ++xPendedTicks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002886
xiaohu.huang58292b32024-01-03 14:09:51 +08002887 /* The tick hook gets called at regular intervals, even if the
2888 * scheduler is locked. */
2889 #if ( configUSE_TICK_HOOK == 1 )
2890 {
2891 vApplicationTickHook();
2892 }
2893 #endif
2894 }
2895
2896 return xSwitchRequired;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002897}
2898/*-----------------------------------------------------------*/
2899
2900#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2901
xiaohu.huang58292b32024-01-03 14:09:51 +08002902 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
2903 TaskHookFunction_t pxHookFunction )
2904 {
2905 TCB_t * xTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002906
xiaohu.huang58292b32024-01-03 14:09:51 +08002907 /* If xTask is NULL then it is the task hook of the calling task that is
2908 * getting set. */
2909 if( xTask == NULL )
2910 {
2911 xTCB = ( TCB_t * ) pxCurrentTCB;
2912 }
2913 else
2914 {
2915 xTCB = xTask;
2916 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002917
xiaohu.huang58292b32024-01-03 14:09:51 +08002918 /* Save the hook function in the TCB. A critical section is required as
2919 * the value can be accessed from an interrupt. */
2920 taskENTER_CRITICAL();
2921 {
2922 xTCB->pxTaskTag = pxHookFunction;
2923 }
2924 taskEXIT_CRITICAL();
2925 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002926
2927#endif /* configUSE_APPLICATION_TASK_TAG */
2928/*-----------------------------------------------------------*/
2929
2930#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2931
xiaohu.huang58292b32024-01-03 14:09:51 +08002932 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
2933 {
2934 TCB_t * pxTCB;
2935 TaskHookFunction_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002936
xiaohu.huang58292b32024-01-03 14:09:51 +08002937 /* If xTask is NULL then set the calling task's hook. */
2938 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002939
xiaohu.huang58292b32024-01-03 14:09:51 +08002940 /* Save the hook function in the TCB. A critical section is required as
2941 * the value can be accessed from an interrupt. */
2942 taskENTER_CRITICAL();
2943 {
2944 xReturn = pxTCB->pxTaskTag;
2945 }
2946 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002947
xiaohu.huang58292b32024-01-03 14:09:51 +08002948 return xReturn;
2949 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002950
2951#endif /* configUSE_APPLICATION_TASK_TAG */
2952/*-----------------------------------------------------------*/
2953
2954#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2955
xiaohu.huang58292b32024-01-03 14:09:51 +08002956 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
2957 {
2958 TCB_t * pxTCB;
2959 TaskHookFunction_t xReturn;
2960 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002961
xiaohu.huang58292b32024-01-03 14:09:51 +08002962 /* If xTask is NULL then set the calling task's hook. */
2963 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002964
xiaohu.huang58292b32024-01-03 14:09:51 +08002965 /* Save the hook function in the TCB. A critical section is required as
2966 * the value can be accessed from an interrupt. */
2967 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2968 {
2969 xReturn = pxTCB->pxTaskTag;
2970 }
2971 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002972
xiaohu.huang58292b32024-01-03 14:09:51 +08002973 return xReturn;
2974 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002975
2976#endif /* configUSE_APPLICATION_TASK_TAG */
2977/*-----------------------------------------------------------*/
2978
2979#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2980
xiaohu.huang58292b32024-01-03 14:09:51 +08002981 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
2982 void * pvParameter )
2983 {
2984 TCB_t * xTCB;
2985 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002986
xiaohu.huang58292b32024-01-03 14:09:51 +08002987 /* If xTask is NULL then we are calling our own task hook. */
2988 if( xTask == NULL )
2989 {
2990 xTCB = pxCurrentTCB;
2991 }
2992 else
2993 {
2994 xTCB = xTask;
2995 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002996
xiaohu.huang58292b32024-01-03 14:09:51 +08002997 if( xTCB->pxTaskTag != NULL )
2998 {
2999 xReturn = xTCB->pxTaskTag( pvParameter );
3000 }
3001 else
3002 {
3003 xReturn = pdFAIL;
3004 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003005
xiaohu.huang58292b32024-01-03 14:09:51 +08003006 return xReturn;
3007 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003008
3009#endif /* configUSE_APPLICATION_TASK_TAG */
3010/*-----------------------------------------------------------*/
3011
3012void vTaskSwitchContext( void )
3013{
xiaohu.huang58292b32024-01-03 14:09:51 +08003014 if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
3015 {
3016 /* The scheduler is currently suspended - do not allow a context
3017 * switch. */
3018 xYieldPending = pdTRUE;
3019 }
3020 else
3021 {
3022 xYieldPending = pdFALSE;
3023 traceTASK_SWITCHED_OUT();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003024
xiaohu.huang58292b32024-01-03 14:09:51 +08003025 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3026 {
3027 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3028 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
3029 #else
3030 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3031 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003032
xiaohu.huang58292b32024-01-03 14:09:51 +08003033 /* Add the amount of time the task has been running to the
3034 * accumulated time so far. The time the task started running was
3035 * stored in ulTaskSwitchedInTime. Note that there is no overflow
3036 * protection here so count values are only valid until the timer
3037 * overflows. The guard against negative values is to protect
3038 * against suspect run time stat counter implementations - which
3039 * are provided by the application, not the kernel. */
3040 if( ulTotalRunTime > ulTaskSwitchedInTime )
3041 {
3042 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );
3043 }
3044 else
3045 {
3046 mtCOVERAGE_TEST_MARKER();
3047 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003048
xiaohu.huang58292b32024-01-03 14:09:51 +08003049 ulTaskSwitchedInTime = ulTotalRunTime;
3050 }
3051 #endif /* configGENERATE_RUN_TIME_STATS */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003052
xiaohu.huang58292b32024-01-03 14:09:51 +08003053 /* Check for stack overflow, if configured. */
3054 taskCHECK_FOR_STACK_OVERFLOW();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003055
xiaohu.huang58292b32024-01-03 14:09:51 +08003056 /* Before the currently running task is switched out, save its errno. */
3057 #if ( configUSE_POSIX_ERRNO == 1 )
3058 {
3059 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
3060 }
3061 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003062
xiaohu.huang58292b32024-01-03 14:09:51 +08003063 /* Select a new task to run using either the generic C or port
3064 * optimised asm code. */
3065 taskSELECT_HIGHEST_PRIORITY_TASK(); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3066 traceTASK_SWITCHED_IN();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003067
xiaohu.huang58292b32024-01-03 14:09:51 +08003068 /* After the new task is switched in, update the global errno. */
3069 #if ( configUSE_POSIX_ERRNO == 1 )
3070 {
3071 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
3072 }
3073 #endif
3074
3075 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
3076 {
3077 /* Switch C-Runtime's TLS Block to point to the TLS
3078 * Block specific to this task. */
3079 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3080 }
3081 #endif
3082 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003083}
3084/*-----------------------------------------------------------*/
3085
xiaohu.huang58292b32024-01-03 14:09:51 +08003086void vTaskPlaceOnEventList( List_t * const pxEventList,
3087 const TickType_t xTicksToWait )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003088{
xiaohu.huang58292b32024-01-03 14:09:51 +08003089 configASSERT( pxEventList );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003090
xiaohu.huang58292b32024-01-03 14:09:51 +08003091 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
3092 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003093
xiaohu.huang58292b32024-01-03 14:09:51 +08003094 /* Place the event list item of the TCB in the appropriate event list.
3095 * This is placed in the list in priority order so the highest priority task
3096 * is the first to be woken by the event.
3097 *
3098 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
3099 * Normally, the xItemValue of a TCB's ListItem_t members is:
3100 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
3101 * Therefore, the event list is sorted in descending priority order.
3102 *
3103 * The queue that contains the event list is locked, preventing
3104 * simultaneous access from interrupts. */
3105 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003106
xiaohu.huang58292b32024-01-03 14:09:51 +08003107 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003108}
3109/*-----------------------------------------------------------*/
3110
xiaohu.huang58292b32024-01-03 14:09:51 +08003111void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
3112 const TickType_t xItemValue,
3113 const TickType_t xTicksToWait )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003114{
xiaohu.huang58292b32024-01-03 14:09:51 +08003115 configASSERT( pxEventList );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003116
xiaohu.huang58292b32024-01-03 14:09:51 +08003117 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3118 * the event groups implementation. */
3119 configASSERT( uxSchedulerSuspended != 0 );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003120
xiaohu.huang58292b32024-01-03 14:09:51 +08003121 /* Store the item value in the event list item. It is safe to access the
3122 * event list item here as interrupts won't access the event list item of a
3123 * task that is not in the Blocked state. */
3124 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003125
xiaohu.huang58292b32024-01-03 14:09:51 +08003126 /* Place the event list item of the TCB at the end of the appropriate event
3127 * list. It is safe to access the event list here because it is part of an
3128 * event group implementation - and interrupts don't access event groups
3129 * directly (instead they access them indirectly by pending function calls to
3130 * the task level). */
3131 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003132
xiaohu.huang58292b32024-01-03 14:09:51 +08003133 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003134}
3135/*-----------------------------------------------------------*/
3136
xiaohu.huang58292b32024-01-03 14:09:51 +08003137#if ( configUSE_TIMERS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003138
xiaohu.huang58292b32024-01-03 14:09:51 +08003139 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
3140 TickType_t xTicksToWait,
3141 const BaseType_t xWaitIndefinitely )
3142 {
3143 configASSERT( pxEventList );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003144
xiaohu.huang58292b32024-01-03 14:09:51 +08003145 /* This function should not be called by application code hence the
3146 * 'Restricted' in its name. It is not part of the public API. It is
3147 * designed for use by kernel code, and has special calling requirements -
3148 * it should be called with the scheduler suspended. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003149
3150
xiaohu.huang58292b32024-01-03 14:09:51 +08003151 /* Place the event list item of the TCB in the appropriate event list.
3152 * In this case it is assume that this is the only task that is going to
3153 * be waiting on this event list, so the faster vListInsertEnd() function
3154 * can be used in place of vListInsert. */
3155 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003156
xiaohu.huang58292b32024-01-03 14:09:51 +08003157 /* If the task should block indefinitely then set the block time to a
3158 * value that will be recognised as an indefinite delay inside the
3159 * prvAddCurrentTaskToDelayedList() function. */
3160 if( xWaitIndefinitely != pdFALSE )
3161 {
3162 xTicksToWait = portMAX_DELAY;
3163 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003164
xiaohu.huang58292b32024-01-03 14:09:51 +08003165 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
3166 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
3167 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003168
3169#endif /* configUSE_TIMERS */
3170/*-----------------------------------------------------------*/
3171
3172BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
3173{
xiaohu.huang58292b32024-01-03 14:09:51 +08003174 TCB_t * pxUnblockedTCB;
3175 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003176
xiaohu.huang58292b32024-01-03 14:09:51 +08003177 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
3178 * called from a critical section within an ISR. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003179
xiaohu.huang58292b32024-01-03 14:09:51 +08003180 /* The event list is sorted in priority order, so the first in the list can
3181 * be removed as it is known to be the highest priority. Remove the TCB from
3182 * the delayed list, and add it to the ready list.
3183 *
3184 * If an event is for a queue that is locked then this function will never
3185 * get called - the lock count on the queue will get modified instead. This
3186 * means exclusive access to the event list is guaranteed here.
3187 *
3188 * This function assumes that a check has already been made to ensure that
3189 * pxEventList is not empty. */
3190 pxUnblockedTCB = listGET_OWNER_OF_HEAD_ENTRY( pxEventList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3191 configASSERT( pxUnblockedTCB );
3192 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003193
xiaohu.huang58292b32024-01-03 14:09:51 +08003194 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3195 {
3196 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
3197 prvAddTaskToReadyList( pxUnblockedTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003198
xiaohu.huang58292b32024-01-03 14:09:51 +08003199 #if ( configUSE_TICKLESS_IDLE != 0 )
3200 {
3201 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3202 * might be set to the blocked task's time out time. If the task is
3203 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
3204 * normally left unchanged, because it is automatically reset to a new
3205 * value when the tick count equals xNextTaskUnblockTime. However if
3206 * tickless idling is used it might be more important to enter sleep mode
3207 * at the earliest possible time - so reset xNextTaskUnblockTime here to
3208 * ensure it is updated at the earliest possible time. */
3209 prvResetNextTaskUnblockTime();
3210 }
3211 #endif
3212 }
3213 else
3214 {
3215 /* The delayed and ready lists cannot be accessed, so hold this task
3216 * pending until the scheduler is resumed. */
3217 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
3218 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003219
xiaohu.huang58292b32024-01-03 14:09:51 +08003220 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
3221 {
3222 /* Return true if the task removed from the event list has a higher
3223 * priority than the calling task. This allows the calling task to know if
3224 * it should force a context switch now. */
3225 xReturn = pdTRUE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003226
xiaohu.huang58292b32024-01-03 14:09:51 +08003227 /* Mark that a yield is pending in case the user is not using the
3228 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
3229 xYieldPending = pdTRUE;
3230 }
3231 else
3232 {
3233 xReturn = pdFALSE;
3234 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003235
xiaohu.huang58292b32024-01-03 14:09:51 +08003236 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003237}
3238/*-----------------------------------------------------------*/
3239
xiaohu.huang58292b32024-01-03 14:09:51 +08003240void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
3241 const TickType_t xItemValue )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003242{
xiaohu.huang58292b32024-01-03 14:09:51 +08003243 TCB_t * pxUnblockedTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003244
xiaohu.huang58292b32024-01-03 14:09:51 +08003245 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3246 * the event flags implementation. */
3247 configASSERT( uxSchedulerSuspended != pdFALSE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003248
xiaohu.huang58292b32024-01-03 14:09:51 +08003249 /* Store the new item value in the event list. */
3250 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003251
xiaohu.huang58292b32024-01-03 14:09:51 +08003252 /* Remove the event list form the event flag. Interrupts do not access
3253 * event flags. */
3254 pxUnblockedTCB = listGET_LIST_ITEM_OWNER( pxEventListItem ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3255 configASSERT( pxUnblockedTCB );
3256 listREMOVE_ITEM( pxEventListItem );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003257
xiaohu.huang58292b32024-01-03 14:09:51 +08003258 #if ( configUSE_TICKLESS_IDLE != 0 )
3259 {
3260 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3261 * might be set to the blocked task's time out time. If the task is
3262 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
3263 * normally left unchanged, because it is automatically reset to a new
3264 * value when the tick count equals xNextTaskUnblockTime. However if
3265 * tickless idling is used it might be more important to enter sleep mode
3266 * at the earliest possible time - so reset xNextTaskUnblockTime here to
3267 * ensure it is updated at the earliest possible time. */
3268 prvResetNextTaskUnblockTime();
3269 }
3270 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003271
xiaohu.huang58292b32024-01-03 14:09:51 +08003272 /* Remove the task from the delayed list and add it to the ready list. The
3273 * scheduler is suspended so interrupts will not be accessing the ready
3274 * lists. */
3275 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
3276 prvAddTaskToReadyList( pxUnblockedTCB );
3277
3278 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
3279 {
3280 /* The unblocked task has a priority above that of the calling task, so
3281 * a context switch is required. This function is called with the
3282 * scheduler suspended so xYieldPending is set so the context switch
3283 * occurs immediately that the scheduler is resumed (unsuspended). */
3284 xYieldPending = pdTRUE;
3285 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003286}
3287/*-----------------------------------------------------------*/
3288
3289void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
3290{
xiaohu.huang58292b32024-01-03 14:09:51 +08003291 configASSERT( pxTimeOut );
3292 taskENTER_CRITICAL();
3293 {
3294 pxTimeOut->xOverflowCount = xNumOfOverflows;
3295 pxTimeOut->xTimeOnEntering = xTickCount;
3296 }
3297 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003298}
3299/*-----------------------------------------------------------*/
3300
3301void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
3302{
xiaohu.huang58292b32024-01-03 14:09:51 +08003303 /* For internal use only as it does not use a critical section. */
3304 pxTimeOut->xOverflowCount = xNumOfOverflows;
3305 pxTimeOut->xTimeOnEntering = xTickCount;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003306}
3307/*-----------------------------------------------------------*/
3308
xiaohu.huang58292b32024-01-03 14:09:51 +08003309BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
3310 TickType_t * const pxTicksToWait )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003311{
xiaohu.huang58292b32024-01-03 14:09:51 +08003312 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003313
xiaohu.huang58292b32024-01-03 14:09:51 +08003314 configASSERT( pxTimeOut );
3315 configASSERT( pxTicksToWait );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003316
xiaohu.huang58292b32024-01-03 14:09:51 +08003317 taskENTER_CRITICAL();
3318 {
3319 /* Minor optimisation. The tick count cannot change in this block. */
3320 const TickType_t xConstTickCount = xTickCount;
3321 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003322
xiaohu.huang58292b32024-01-03 14:09:51 +08003323 #if ( INCLUDE_xTaskAbortDelay == 1 )
3324 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
3325 {
3326 /* The delay was aborted, which is not the same as a time out,
3327 * but has the same result. */
3328 pxCurrentTCB->ucDelayAborted = pdFALSE;
3329 xReturn = pdTRUE;
3330 }
3331 else
3332 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003333
xiaohu.huang58292b32024-01-03 14:09:51 +08003334 #if ( INCLUDE_vTaskSuspend == 1 )
3335 if( *pxTicksToWait == portMAX_DELAY )
3336 {
3337 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
3338 * specified is the maximum block time then the task should block
3339 * indefinitely, and therefore never time out. */
3340 xReturn = pdFALSE;
3341 }
3342 else
3343 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003344
xiaohu.huang58292b32024-01-03 14:09:51 +08003345 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
3346 {
3347 /* The tick count is greater than the time at which
3348 * vTaskSetTimeout() was called, but has also overflowed since
3349 * vTaskSetTimeOut() was called. It must have wrapped all the way
3350 * around and gone past again. This passed since vTaskSetTimeout()
3351 * was called. */
3352 xReturn = pdTRUE;
3353 *pxTicksToWait = ( TickType_t ) 0;
3354 }
3355 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
3356 {
3357 /* Not a genuine timeout. Adjust parameters for time remaining. */
3358 *pxTicksToWait -= xElapsedTime;
3359 vTaskInternalSetTimeOutState( pxTimeOut );
3360 xReturn = pdFALSE;
3361 }
3362 else
3363 {
3364 *pxTicksToWait = ( TickType_t ) 0;
3365 xReturn = pdTRUE;
3366 }
3367 }
3368 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003369
xiaohu.huang58292b32024-01-03 14:09:51 +08003370 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003371}
3372/*-----------------------------------------------------------*/
3373
3374void vTaskMissedYield( void )
3375{
xiaohu.huang58292b32024-01-03 14:09:51 +08003376 xYieldPending = pdTRUE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003377}
3378/*-----------------------------------------------------------*/
3379
3380#if ( configUSE_TRACE_FACILITY == 1 )
3381
xiaohu.huang58292b32024-01-03 14:09:51 +08003382 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
3383 {
3384 UBaseType_t uxReturn;
3385 TCB_t const * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003386
xiaohu.huang58292b32024-01-03 14:09:51 +08003387 if( xTask != NULL )
3388 {
3389 pxTCB = xTask;
3390 uxReturn = pxTCB->uxTaskNumber;
3391 }
3392 else
3393 {
3394 uxReturn = 0U;
3395 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003396
xiaohu.huang58292b32024-01-03 14:09:51 +08003397 return uxReturn;
3398 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003399
3400#endif /* configUSE_TRACE_FACILITY */
3401/*-----------------------------------------------------------*/
3402
3403#if ( configUSE_TRACE_FACILITY == 1 )
3404
xiaohu.huang58292b32024-01-03 14:09:51 +08003405 void vTaskSetTaskNumber( TaskHandle_t xTask,
3406 const UBaseType_t uxHandle )
3407 {
3408 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003409
xiaohu.huang58292b32024-01-03 14:09:51 +08003410 if( xTask != NULL )
3411 {
3412 pxTCB = xTask;
3413 pxTCB->uxTaskNumber = uxHandle;
3414 }
3415 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003416
3417#endif /* configUSE_TRACE_FACILITY */
3418
3419/*
3420 * -----------------------------------------------------------
3421 * The Idle task.
3422 * ----------------------------------------------------------
3423 *
3424 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
3425 * language extensions. The equivalent prototype for this function is:
3426 *
3427 * void prvIdleTask( void *pvParameters );
3428 *
3429 */
3430static portTASK_FUNCTION( prvIdleTask, pvParameters )
3431{
xiaohu.huang58292b32024-01-03 14:09:51 +08003432 /* Stop warnings. */
3433 ( void ) pvParameters;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003434
xiaohu.huang58292b32024-01-03 14:09:51 +08003435 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
3436 * SCHEDULER IS STARTED. **/
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003437
xiaohu.huang58292b32024-01-03 14:09:51 +08003438 /* In case a task that has a secure context deletes itself, in which case
3439 * the idle task is responsible for deleting the task's secure context, if
3440 * any. */
3441 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003442
xiaohu.huang58292b32024-01-03 14:09:51 +08003443 for( ; ; )
3444 {
3445 /* See if any tasks have deleted themselves - if so then the idle task
3446 * is responsible for freeing the deleted task's TCB and stack. */
3447 prvCheckTasksWaitingTermination();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003448
xiaohu.huang58292b32024-01-03 14:09:51 +08003449 #if ( configUSE_PREEMPTION == 0 )
3450 {
3451 /* If we are not using preemption we keep forcing a task switch to
3452 * see if any other task has become available. If we are using
3453 * preemption we don't need to do this as any task becoming available
3454 * will automatically get the processor anyway. */
3455 taskYIELD();
3456 }
3457 #endif /* configUSE_PREEMPTION */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003458
xiaohu.huang58292b32024-01-03 14:09:51 +08003459 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
3460 {
3461 /* When using preemption tasks of equal priority will be
3462 * timesliced. If a task that is sharing the idle priority is ready
3463 * to run then the idle task should yield before the end of the
3464 * timeslice.
3465 *
3466 * A critical region is not required here as we are just reading from
3467 * the list, and an occasional incorrect value will not matter. If
3468 * the ready list at the idle priority contains more than one task
3469 * then a task other than the idle task is ready to execute. */
3470 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) 1 )
3471 {
3472 taskYIELD();
3473 }
3474 else
3475 {
3476 mtCOVERAGE_TEST_MARKER();
3477 }
3478 }
3479 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003480
xiaohu.huang58292b32024-01-03 14:09:51 +08003481 #if ( configUSE_IDLE_HOOK == 1 )
3482 {
3483 extern void vApplicationIdleHook( void );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003484
xiaohu.huang58292b32024-01-03 14:09:51 +08003485 /* Call the user defined function from within the idle task. This
3486 * allows the application designer to add background functionality
3487 * without the overhead of a separate task.
3488 * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
3489 * CALL A FUNCTION THAT MIGHT BLOCK. */
3490 vApplicationIdleHook();
3491 }
3492 #endif /* configUSE_IDLE_HOOK */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003493
xiaohu.huang58292b32024-01-03 14:09:51 +08003494 /* This conditional compilation should use inequality to 0, not equality
3495 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
3496 * user defined low power mode implementations require
3497 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
3498 #if ( configUSE_TICKLESS_IDLE != 0 )
3499 {
3500 TickType_t xExpectedIdleTime;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003501
xiaohu.huang58292b32024-01-03 14:09:51 +08003502 /* It is not desirable to suspend then resume the scheduler on
3503 * each iteration of the idle task. Therefore, a preliminary
3504 * test of the expected idle time is performed without the
3505 * scheduler suspended. The result here is not necessarily
3506 * valid. */
3507 xExpectedIdleTime = prvGetExpectedIdleTime();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003508
xiaohu.huang58292b32024-01-03 14:09:51 +08003509 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
3510 {
3511 vTaskSuspendAll();
3512 {
3513 /* Now the scheduler is suspended, the expected idle
3514 * time can be sampled again, and this time its value can
3515 * be used. */
3516 configASSERT( xNextTaskUnblockTime >= xTickCount );
3517 xExpectedIdleTime = prvGetExpectedIdleTime();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003518
xiaohu.huang58292b32024-01-03 14:09:51 +08003519 /* Define the following macro to set xExpectedIdleTime to 0
3520 * if the application does not want
3521 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
3522 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003523
xiaohu.huang58292b32024-01-03 14:09:51 +08003524 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
3525 {
3526 traceLOW_POWER_IDLE_BEGIN();
3527 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
3528 traceLOW_POWER_IDLE_END();
3529 }
3530 else
3531 {
3532 mtCOVERAGE_TEST_MARKER();
3533 }
3534 }
3535 ( void ) xTaskResumeAll();
3536 }
3537 else
3538 {
3539 mtCOVERAGE_TEST_MARKER();
3540 }
3541 }
3542 #endif /* configUSE_TICKLESS_IDLE */
3543 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003544}
3545/*-----------------------------------------------------------*/
3546
xiaohu.huang58292b32024-01-03 14:09:51 +08003547#if ( configUSE_TICKLESS_IDLE != 0 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003548
xiaohu.huang58292b32024-01-03 14:09:51 +08003549 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
3550 {
3551 #if ( INCLUDE_vTaskSuspend == 1 )
3552 /* The idle task exists in addition to the application tasks. */
3553 const UBaseType_t uxNonApplicationTasks = 1;
3554 #endif /* INCLUDE_vTaskSuspend */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003555
xiaohu.huang58292b32024-01-03 14:09:51 +08003556 eSleepModeStatus eReturn = eStandardSleep;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003557
xiaohu.huang58292b32024-01-03 14:09:51 +08003558 /* This function must be called from a critical section. */
3559
3560 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
3561 {
3562 /* A task was made ready while the scheduler was suspended. */
3563 eReturn = eAbortSleep;
3564 }
3565 else if( xYieldPending != pdFALSE )
3566 {
3567 /* A yield was pended while the scheduler was suspended. */
3568 eReturn = eAbortSleep;
3569 }
3570 else if( xPendedTicks != 0 )
3571 {
3572 /* A tick interrupt has already occurred but was held pending
3573 * because the scheduler is suspended. */
3574 eReturn = eAbortSleep;
3575 }
3576
3577 #if ( INCLUDE_vTaskSuspend == 1 )
3578 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
3579 {
3580 /* If all the tasks are in the suspended list (which might mean they
3581 * have an infinite block time rather than actually being suspended)
3582 * then it is safe to turn all clocks off and just wait for external
3583 * interrupts. */
3584 eReturn = eNoTasksWaitingTimeout;
3585 }
3586 #endif /* INCLUDE_vTaskSuspend */
3587 else
3588 {
3589 mtCOVERAGE_TEST_MARKER();
3590 }
3591
3592 return eReturn;
3593 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003594
3595#endif /* configUSE_TICKLESS_IDLE */
3596/*-----------------------------------------------------------*/
3597
3598#if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
3599
xiaohu.huang58292b32024-01-03 14:09:51 +08003600 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
3601 BaseType_t xIndex,
3602 void * pvValue )
3603 {
3604 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003605
xiaohu.huang58292b32024-01-03 14:09:51 +08003606 if( ( xIndex >= 0 ) &&
3607 ( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
3608 {
3609 pxTCB = prvGetTCBFromHandle( xTaskToSet );
3610 configASSERT( pxTCB != NULL );
3611 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
3612 }
3613 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003614
3615#endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
3616/*-----------------------------------------------------------*/
3617
3618#if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
3619
xiaohu.huang58292b32024-01-03 14:09:51 +08003620 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
3621 BaseType_t xIndex )
3622 {
3623 void * pvReturn = NULL;
3624 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003625
xiaohu.huang58292b32024-01-03 14:09:51 +08003626 if( ( xIndex >= 0 ) &&
3627 ( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
3628 {
3629 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3630 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
3631 }
3632 else
3633 {
3634 pvReturn = NULL;
3635 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003636
xiaohu.huang58292b32024-01-03 14:09:51 +08003637 return pvReturn;
3638 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003639
3640#endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
3641/*-----------------------------------------------------------*/
3642
3643#if ( portUSING_MPU_WRAPPERS == 1 )
3644
xiaohu.huang58292b32024-01-03 14:09:51 +08003645 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
3646 const MemoryRegion_t * const xRegions )
3647 {
3648 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003649
xiaohu.huang58292b32024-01-03 14:09:51 +08003650 /* If null is passed in here then we are modifying the MPU settings of
3651 * the calling task. */
3652 pxTCB = prvGetTCBFromHandle( xTaskToModify );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003653
xiaohu.huang58292b32024-01-03 14:09:51 +08003654 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
3655 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003656
3657#endif /* portUSING_MPU_WRAPPERS */
3658/*-----------------------------------------------------------*/
3659
3660static void prvInitialiseTaskLists( void )
3661{
xiaohu.huang58292b32024-01-03 14:09:51 +08003662 UBaseType_t uxPriority;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003663
xiaohu.huang58292b32024-01-03 14:09:51 +08003664 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
3665 {
3666 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
3667 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003668
xiaohu.huang58292b32024-01-03 14:09:51 +08003669 vListInitialise( &xDelayedTaskList1 );
3670 vListInitialise( &xDelayedTaskList2 );
3671 vListInitialise( &xPendingReadyList );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003672
xiaohu.huang58292b32024-01-03 14:09:51 +08003673 #if ( INCLUDE_vTaskDelete == 1 )
3674 {
3675 vListInitialise( &xTasksWaitingTermination );
3676 }
3677 #endif /* INCLUDE_vTaskDelete */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003678
xiaohu.huang58292b32024-01-03 14:09:51 +08003679 #if ( INCLUDE_vTaskSuspend == 1 )
3680 {
3681 vListInitialise( &xSuspendedTaskList );
3682 }
3683 #endif /* INCLUDE_vTaskSuspend */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003684
xiaohu.huang58292b32024-01-03 14:09:51 +08003685 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
3686 * using list2. */
3687 pxDelayedTaskList = &xDelayedTaskList1;
3688 pxOverflowDelayedTaskList = &xDelayedTaskList2;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003689}
3690/*-----------------------------------------------------------*/
3691
3692static void prvCheckTasksWaitingTermination( void )
3693{
xiaohu.huang58292b32024-01-03 14:09:51 +08003694 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003695
xiaohu.huang58292b32024-01-03 14:09:51 +08003696 #if ( INCLUDE_vTaskDelete == 1 )
3697 {
3698 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003699
xiaohu.huang58292b32024-01-03 14:09:51 +08003700 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
3701 * being called too often in the idle task. */
3702 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
3703 {
3704 taskENTER_CRITICAL();
3705 {
3706 pxTCB = listGET_OWNER_OF_HEAD_ENTRY( ( &xTasksWaitingTermination ) ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3707 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3708 --uxCurrentNumberOfTasks;
3709 --uxDeletedTasksWaitingCleanUp;
3710 }
3711 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003712
xiaohu.huang58292b32024-01-03 14:09:51 +08003713 prvDeleteTCB( pxTCB );
3714 }
3715 }
3716 #endif /* INCLUDE_vTaskDelete */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003717}
3718/*-----------------------------------------------------------*/
3719
xiaohu.huang58292b32024-01-03 14:09:51 +08003720#if ( configUSE_TRACE_FACILITY == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003721
xiaohu.huang58292b32024-01-03 14:09:51 +08003722 void vTaskGetInfo( TaskHandle_t xTask,
3723 TaskStatus_t * pxTaskStatus,
3724 BaseType_t xGetFreeStackSpace,
3725 eTaskState eState )
3726 {
3727 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003728
xiaohu.huang58292b32024-01-03 14:09:51 +08003729 /* xTask is NULL then get the state of the calling task. */
3730 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003731
xiaohu.huang58292b32024-01-03 14:09:51 +08003732 pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
3733 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
3734 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
3735 pxTaskStatus->pxStackBase = pxTCB->pxStack;
3736 #if ( ( portSTACK_GROWTH > 0 ) && ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
3737 pxTaskStatus->pxTopOfStack = pxTCB->pxTopOfStack;
3738 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
3739 #endif
3740 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003741
xiaohu.huang58292b32024-01-03 14:09:51 +08003742 #if ( configUSE_MUTEXES == 1 )
3743 {
3744 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
3745 }
3746 #else
3747 {
3748 pxTaskStatus->uxBasePriority = 0;
3749 }
3750 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003751
xiaohu.huang58292b32024-01-03 14:09:51 +08003752 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3753 {
3754 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
3755 }
3756 #else
3757 {
3758 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
3759 }
3760 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003761
xiaohu.huang58292b32024-01-03 14:09:51 +08003762 /* Obtaining the task state is a little fiddly, so is only done if the
3763 * value of eState passed into this function is eInvalid - otherwise the
3764 * state is just set to whatever is passed in. */
3765 if( eState != eInvalid )
3766 {
3767 if( pxTCB == pxCurrentTCB )
3768 {
3769 pxTaskStatus->eCurrentState = eRunning;
3770 }
3771 else
3772 {
3773 pxTaskStatus->eCurrentState = eState;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003774
xiaohu.huang58292b32024-01-03 14:09:51 +08003775 #if ( INCLUDE_vTaskSuspend == 1 )
3776 {
3777 /* If the task is in the suspended list then there is a
3778 * chance it is actually just blocked indefinitely - so really
3779 * it should be reported as being in the Blocked state. */
3780 if( eState == eSuspended )
3781 {
3782 vTaskSuspendAll();
3783 {
3784 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3785 {
3786 pxTaskStatus->eCurrentState = eBlocked;
3787 }
3788 }
3789 ( void ) xTaskResumeAll();
3790 }
3791 }
3792 #endif /* INCLUDE_vTaskSuspend */
3793 }
3794 }
3795 else
3796 {
3797 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
3798 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003799
xiaohu.huang58292b32024-01-03 14:09:51 +08003800 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
3801 * parameter is provided to allow it to be skipped. */
3802 if( xGetFreeStackSpace != pdFALSE )
3803 {
3804 #if ( portSTACK_GROWTH > 0 )
3805 {
3806 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
3807 }
3808 #else
3809 {
3810 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
3811 }
3812 #endif
3813 }
3814 else
3815 {
3816 pxTaskStatus->usStackHighWaterMark = 0;
3817 }
3818 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003819
3820#endif /* configUSE_TRACE_FACILITY */
3821/*-----------------------------------------------------------*/
3822
3823#if ( configUSE_TRACE_FACILITY == 1 )
3824
xiaohu.huang58292b32024-01-03 14:09:51 +08003825 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
3826 List_t * pxList,
3827 eTaskState eState )
3828 {
3829 configLIST_VOLATILE TCB_t * pxNextTCB;
3830 configLIST_VOLATILE TCB_t * pxFirstTCB;
3831 UBaseType_t uxTask = 0;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003832
xiaohu.huang58292b32024-01-03 14:09:51 +08003833 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3834 {
3835 listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003836
xiaohu.huang58292b32024-01-03 14:09:51 +08003837 /* Populate an TaskStatus_t structure within the
3838 * pxTaskStatusArray array for each task that is referenced from
3839 * pxList. See the definition of TaskStatus_t in task.h for the
3840 * meaning of each TaskStatus_t structure member. */
3841 do
3842 {
3843 listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList ); /*lint !e9079 void * is used as this macro is used with timers and co-routines too. Alignment is known to be fine as the type of the pointer stored and retrieved is the same. */
3844 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
3845 uxTask++;
3846 } while( pxNextTCB != pxFirstTCB );
3847 }
3848 else
3849 {
3850 mtCOVERAGE_TEST_MARKER();
3851 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003852
xiaohu.huang58292b32024-01-03 14:09:51 +08003853 return uxTask;
3854 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003855
3856#endif /* configUSE_TRACE_FACILITY */
3857/*-----------------------------------------------------------*/
3858
3859#if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
3860
xiaohu.huang58292b32024-01-03 14:09:51 +08003861 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
3862 {
3863 uint32_t ulCount = 0U;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003864
xiaohu.huang58292b32024-01-03 14:09:51 +08003865 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
3866 {
3867 pucStackByte -= portSTACK_GROWTH;
3868 ulCount++;
3869 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003870
xiaohu.huang58292b32024-01-03 14:09:51 +08003871 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003872
xiaohu.huang58292b32024-01-03 14:09:51 +08003873 return ( configSTACK_DEPTH_TYPE ) ulCount;
3874 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003875
3876#endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
3877/*-----------------------------------------------------------*/
3878
3879#if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
3880
xiaohu.huang58292b32024-01-03 14:09:51 +08003881/* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
3882 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
3883 * user to determine the return type. It gets around the problem of the value
3884 * overflowing on 8-bit types without breaking backward compatibility for
3885 * applications that expect an 8-bit return type. */
3886 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
3887 {
3888 TCB_t * pxTCB;
3889 uint8_t * pucEndOfStack;
3890 configSTACK_DEPTH_TYPE uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003891
xiaohu.huang58292b32024-01-03 14:09:51 +08003892 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
3893 * the same except for their return type. Using configSTACK_DEPTH_TYPE
3894 * allows the user to determine the return type. It gets around the
3895 * problem of the value overflowing on 8-bit types without breaking
3896 * backward compatibility for applications that expect an 8-bit return
3897 * type. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003898
xiaohu.huang58292b32024-01-03 14:09:51 +08003899 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003900
xiaohu.huang58292b32024-01-03 14:09:51 +08003901 #if portSTACK_GROWTH < 0
3902 {
3903 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
3904 }
3905 #else
3906 {
3907 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
3908 }
3909 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003910
xiaohu.huang58292b32024-01-03 14:09:51 +08003911 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003912
xiaohu.huang58292b32024-01-03 14:09:51 +08003913 return uxReturn;
3914 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003915
3916#endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
3917/*-----------------------------------------------------------*/
3918
3919#if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
3920
xiaohu.huang58292b32024-01-03 14:09:51 +08003921 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
3922 {
3923 TCB_t * pxTCB;
3924 uint8_t * pucEndOfStack;
3925 UBaseType_t uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003926
xiaohu.huang58292b32024-01-03 14:09:51 +08003927 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003928
xiaohu.huang58292b32024-01-03 14:09:51 +08003929 #if portSTACK_GROWTH < 0
3930 {
3931 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
3932 }
3933 #else
3934 {
3935 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
3936 }
3937 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003938
xiaohu.huang58292b32024-01-03 14:09:51 +08003939 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003940
xiaohu.huang58292b32024-01-03 14:09:51 +08003941 return uxReturn;
3942 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003943
3944#endif /* INCLUDE_uxTaskGetStackHighWaterMark */
3945/*-----------------------------------------------------------*/
3946
3947#if ( INCLUDE_vTaskDelete == 1 )
3948
xiaohu.huang58292b32024-01-03 14:09:51 +08003949 static void prvDeleteTCB( TCB_t * pxTCB )
3950 {
3951 /* This call is required specifically for the TriCore port. It must be
3952 * above the vPortFree() calls. The call is also used by ports/demos that
3953 * want to allocate and clean RAM statically. */
3954 portCLEAN_UP_TCB( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003955
xiaohu.huang58292b32024-01-03 14:09:51 +08003956 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
3957 {
3958 /* Free up the memory allocated for the task's TLS Block. */
3959 configDEINIT_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3960 }
3961 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003962
xiaohu.huang58292b32024-01-03 14:09:51 +08003963 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
3964 {
3965 /* The task can only have been allocated dynamically - free both
3966 * the stack and TCB. */
3967 vPortFreeStack( pxTCB->pxStack );
3968 vPortFree( pxTCB );
3969 }
3970 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
3971 {
3972 /* The task could have been allocated statically or dynamically, so
3973 * check what was statically allocated before trying to free the
3974 * memory. */
3975 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
3976 {
3977 /* Both the stack and TCB were allocated dynamically, so both
3978 * must be freed. */
3979 vPortFreeStack( pxTCB->pxStack );
3980 vPortFree( pxTCB );
3981 }
3982 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
3983 {
3984 /* Only the stack was statically allocated, so the TCB is the
3985 * only memory that must be freed. */
3986 vPortFree( pxTCB );
3987 }
3988 else
3989 {
3990 /* Neither the stack nor the TCB were allocated dynamically, so
3991 * nothing needs to be freed. */
3992 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
3993 mtCOVERAGE_TEST_MARKER();
3994 }
3995 }
3996 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
3997 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003998
3999#endif /* INCLUDE_vTaskDelete */
4000/*-----------------------------------------------------------*/
4001
4002static void prvResetNextTaskUnblockTime( void )
4003{
xiaohu.huang58292b32024-01-03 14:09:51 +08004004 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4005 {
4006 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
4007 * the maximum possible value so it is extremely unlikely that the
4008 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
4009 * there is an item in the delayed list. */
4010 xNextTaskUnblockTime = portMAX_DELAY;
4011 }
4012 else
4013 {
4014 /* The new current delayed list is not empty, get the value of
4015 * the item at the head of the delayed list. This is the time at
4016 * which the task at the head of the delayed list should be removed
4017 * from the Blocked state. */
4018 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
4019 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004020}
4021/*-----------------------------------------------------------*/
4022
4023#if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4024
xiaohu.huang58292b32024-01-03 14:09:51 +08004025 TaskHandle_t xTaskGetCurrentTaskHandle( void )
4026 {
4027 TaskHandle_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004028
xiaohu.huang58292b32024-01-03 14:09:51 +08004029 /* A critical section is not required as this is not called from
4030 * an interrupt and the current TCB will always be the same for any
4031 * individual execution thread. */
4032 xReturn = pxCurrentTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004033
xiaohu.huang58292b32024-01-03 14:09:51 +08004034 return xReturn;
4035 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004036
4037#endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
4038/*-----------------------------------------------------------*/
4039
4040#if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
4041
xiaohu.huang58292b32024-01-03 14:09:51 +08004042 BaseType_t xTaskGetSchedulerState( void )
4043 {
4044 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004045
xiaohu.huang58292b32024-01-03 14:09:51 +08004046 if( xSchedulerRunning == pdFALSE )
4047 {
4048 xReturn = taskSCHEDULER_NOT_STARTED;
4049 }
4050 else
4051 {
4052 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4053 {
4054 xReturn = taskSCHEDULER_RUNNING;
4055 }
4056 else
4057 {
4058 xReturn = taskSCHEDULER_SUSPENDED;
4059 }
4060 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004061
xiaohu.huang58292b32024-01-03 14:09:51 +08004062 return xReturn;
4063 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004064
4065#endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
4066/*-----------------------------------------------------------*/
4067
4068#if ( configUSE_MUTEXES == 1 )
4069
xiaohu.huang58292b32024-01-03 14:09:51 +08004070 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
4071 {
4072 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
4073 BaseType_t xReturn = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004074
xiaohu.huang58292b32024-01-03 14:09:51 +08004075 /* If the mutex was given back by an interrupt while the queue was
4076 * locked then the mutex holder might now be NULL. _RB_ Is this still
4077 * needed as interrupts can no longer use mutexes? */
4078 if( pxMutexHolder != NULL )
4079 {
4080 /* If the holder of the mutex has a priority below the priority of
4081 * the task attempting to obtain the mutex then it will temporarily
4082 * inherit the priority of the task attempting to obtain the mutex. */
4083 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
4084 {
4085 /* Adjust the mutex holder state to account for its new
4086 * priority. Only reset the event list item value if the value is
4087 * not being used for anything else. */
4088 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4089 {
4090 listSET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4091 }
4092 else
4093 {
4094 mtCOVERAGE_TEST_MARKER();
4095 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004096
xiaohu.huang58292b32024-01-03 14:09:51 +08004097 /* If the task being modified is in the ready state it will need
4098 * to be moved into a new list. */
4099 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
4100 {
4101 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4102 {
4103 /* It is known that the task is in its ready list so
4104 * there is no need to check again and the port level
4105 * reset macro can be called directly. */
4106 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
4107 }
4108 else
4109 {
4110 mtCOVERAGE_TEST_MARKER();
4111 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004112
xiaohu.huang58292b32024-01-03 14:09:51 +08004113 /* Inherit the priority before being moved into the new list. */
4114 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4115 prvAddTaskToReadyList( pxMutexHolderTCB );
4116 }
4117 else
4118 {
4119 /* Just inherit the priority. */
4120 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4121 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004122
xiaohu.huang58292b32024-01-03 14:09:51 +08004123 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004124
xiaohu.huang58292b32024-01-03 14:09:51 +08004125 /* Inheritance occurred. */
4126 xReturn = pdTRUE;
4127 }
4128 else
4129 {
4130 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
4131 {
4132 /* The base priority of the mutex holder is lower than the
4133 * priority of the task attempting to take the mutex, but the
4134 * current priority of the mutex holder is not lower than the
4135 * priority of the task attempting to take the mutex.
4136 * Therefore the mutex holder must have already inherited a
4137 * priority, but inheritance would have occurred if that had
4138 * not been the case. */
4139 xReturn = pdTRUE;
4140 }
4141 else
4142 {
4143 mtCOVERAGE_TEST_MARKER();
4144 }
4145 }
4146 }
4147 else
4148 {
4149 mtCOVERAGE_TEST_MARKER();
4150 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004151
xiaohu.huang58292b32024-01-03 14:09:51 +08004152 return xReturn;
4153 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004154
4155#endif /* configUSE_MUTEXES */
4156/*-----------------------------------------------------------*/
4157
4158#if ( configUSE_MUTEXES == 1 )
4159
xiaohu.huang58292b32024-01-03 14:09:51 +08004160 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
4161 {
4162 TCB_t * const pxTCB = pxMutexHolder;
4163 BaseType_t xReturn = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004164
xiaohu.huang58292b32024-01-03 14:09:51 +08004165 if( pxMutexHolder != NULL )
4166 {
4167 /* A task can only have an inherited priority if it holds the mutex.
4168 * If the mutex is held by a task then it cannot be given from an
4169 * interrupt, and if a mutex is given by the holding task then it must
4170 * be the running state task. */
4171 configASSERT( pxTCB == pxCurrentTCB );
4172 configASSERT( pxTCB->uxMutexesHeld );
4173 ( pxTCB->uxMutexesHeld )--;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004174
xiaohu.huang58292b32024-01-03 14:09:51 +08004175 /* Has the holder of the mutex inherited the priority of another
4176 * task? */
4177 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
4178 {
4179 /* Only disinherit if no other mutexes are held. */
4180 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
4181 {
4182 /* A task can only have an inherited priority if it holds
4183 * the mutex. If the mutex is held by a task then it cannot be
4184 * given from an interrupt, and if a mutex is given by the
4185 * holding task then it must be the running state task. Remove
4186 * the holding task from the ready list. */
4187 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4188 {
4189 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
4190 }
4191 else
4192 {
4193 mtCOVERAGE_TEST_MARKER();
4194 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004195
xiaohu.huang58292b32024-01-03 14:09:51 +08004196 /* Disinherit the priority before adding the task into the
4197 * new ready list. */
4198 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
4199 pxTCB->uxPriority = pxTCB->uxBasePriority;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004200
xiaohu.huang58292b32024-01-03 14:09:51 +08004201 /* Reset the event list item value. It cannot be in use for
4202 * any other purpose if this task is running, and it must be
4203 * running to give back the mutex. */
4204 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. */
4205 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004206
xiaohu.huang58292b32024-01-03 14:09:51 +08004207 /* Return true to indicate that a context switch is required.
4208 * This is only actually required in the corner case whereby
4209 * multiple mutexes were held and the mutexes were given back
4210 * in an order different to that in which they were taken.
4211 * If a context switch did not occur when the first mutex was
4212 * returned, even if a task was waiting on it, then a context
4213 * switch should occur when the last mutex is returned whether
4214 * a task is waiting on it or not. */
4215 xReturn = pdTRUE;
4216 }
4217 else
4218 {
4219 mtCOVERAGE_TEST_MARKER();
4220 }
4221 }
4222 else
4223 {
4224 mtCOVERAGE_TEST_MARKER();
4225 }
4226 }
4227 else
4228 {
4229 mtCOVERAGE_TEST_MARKER();
4230 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004231
xiaohu.huang58292b32024-01-03 14:09:51 +08004232 return xReturn;
4233 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004234
4235#endif /* configUSE_MUTEXES */
4236/*-----------------------------------------------------------*/
4237
4238#if ( configUSE_MUTEXES == 1 )
4239
xiaohu.huang58292b32024-01-03 14:09:51 +08004240 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
4241 UBaseType_t uxHighestPriorityWaitingTask )
4242 {
4243 TCB_t * const pxTCB = pxMutexHolder;
4244 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
4245 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004246
xiaohu.huang58292b32024-01-03 14:09:51 +08004247 if( pxMutexHolder != NULL )
4248 {
4249 /* If pxMutexHolder is not NULL then the holder must hold at least
4250 * one mutex. */
4251 configASSERT( pxTCB->uxMutexesHeld );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004252
xiaohu.huang58292b32024-01-03 14:09:51 +08004253 /* Determine the priority to which the priority of the task that
4254 * holds the mutex should be set. This will be the greater of the
4255 * holding task's base priority and the priority of the highest
4256 * priority task that is waiting to obtain the mutex. */
4257 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
4258 {
4259 uxPriorityToUse = uxHighestPriorityWaitingTask;
4260 }
4261 else
4262 {
4263 uxPriorityToUse = pxTCB->uxBasePriority;
4264 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004265
xiaohu.huang58292b32024-01-03 14:09:51 +08004266 /* Does the priority need to change? */
4267 if( pxTCB->uxPriority != uxPriorityToUse )
4268 {
4269 /* Only disinherit if no other mutexes are held. This is a
4270 * simplification in the priority inheritance implementation. If
4271 * the task that holds the mutex is also holding other mutexes then
4272 * the other mutexes may have caused the priority inheritance. */
4273 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
4274 {
4275 /* If a task has timed out because it already holds the
4276 * mutex it was trying to obtain then it cannot of inherited
4277 * its own priority. */
4278 configASSERT( pxTCB != pxCurrentTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004279
xiaohu.huang58292b32024-01-03 14:09:51 +08004280 /* Disinherit the priority, remembering the previous
4281 * priority to facilitate determining the subject task's
4282 * state. */
4283 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
4284 uxPriorityUsedOnEntry = pxTCB->uxPriority;
4285 pxTCB->uxPriority = uxPriorityToUse;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004286
xiaohu.huang58292b32024-01-03 14:09:51 +08004287 /* Only reset the event list item value if the value is not
4288 * being used for anything else. */
4289 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4290 {
4291 listSET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ), ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) uxPriorityToUse ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
4292 }
4293 else
4294 {
4295 mtCOVERAGE_TEST_MARKER();
4296 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004297
xiaohu.huang58292b32024-01-03 14:09:51 +08004298 /* If the running task is not the task that holds the mutex
4299 * then the task that holds the mutex could be in either the
4300 * Ready, Blocked or Suspended states. Only remove the task
4301 * from its current state list if it is in the Ready state as
4302 * the task's priority is going to change and there is one
4303 * Ready list per priority. */
4304 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
4305 {
4306 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4307 {
4308 /* It is known that the task is in its ready list so
4309 * there is no need to check again and the port level
4310 * reset macro can be called directly. */
4311 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
4312 }
4313 else
4314 {
4315 mtCOVERAGE_TEST_MARKER();
4316 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004317
xiaohu.huang58292b32024-01-03 14:09:51 +08004318 prvAddTaskToReadyList( pxTCB );
4319 }
4320 else
4321 {
4322 mtCOVERAGE_TEST_MARKER();
4323 }
4324 }
4325 else
4326 {
4327 mtCOVERAGE_TEST_MARKER();
4328 }
4329 }
4330 else
4331 {
4332 mtCOVERAGE_TEST_MARKER();
4333 }
4334 }
4335 else
4336 {
4337 mtCOVERAGE_TEST_MARKER();
4338 }
4339 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004340
4341#endif /* configUSE_MUTEXES */
4342/*-----------------------------------------------------------*/
4343
4344#if ( portCRITICAL_NESTING_IN_TCB == 1 )
4345
xiaohu.huang58292b32024-01-03 14:09:51 +08004346 void vTaskEnterCritical( void )
4347 {
4348 portDISABLE_INTERRUPTS();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004349
xiaohu.huang58292b32024-01-03 14:09:51 +08004350 if( xSchedulerRunning != pdFALSE )
4351 {
4352 ( pxCurrentTCB->uxCriticalNesting )++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004353
xiaohu.huang58292b32024-01-03 14:09:51 +08004354 /* This is not the interrupt safe version of the enter critical
4355 * function so assert() if it is being called from an interrupt
4356 * context. Only API functions that end in "FromISR" can be used in an
4357 * interrupt. Only assert if the critical nesting count is 1 to
4358 * protect against recursive calls if the assert function also uses a
4359 * critical section. */
4360 if( pxCurrentTCB->uxCriticalNesting == 1 )
4361 {
4362 portASSERT_IF_IN_ISR();
4363 }
4364 }
4365 else
4366 {
4367 mtCOVERAGE_TEST_MARKER();
4368 }
4369 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004370
4371#endif /* portCRITICAL_NESTING_IN_TCB */
4372/*-----------------------------------------------------------*/
4373
4374#if ( portCRITICAL_NESTING_IN_TCB == 1 )
4375
xiaohu.huang58292b32024-01-03 14:09:51 +08004376 void vTaskExitCritical( void )
4377 {
4378 if( xSchedulerRunning != pdFALSE )
4379 {
4380 if( pxCurrentTCB->uxCriticalNesting > 0U )
4381 {
4382 ( pxCurrentTCB->uxCriticalNesting )--;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004383
xiaohu.huang58292b32024-01-03 14:09:51 +08004384 if( pxCurrentTCB->uxCriticalNesting == 0U )
4385 {
4386 portENABLE_INTERRUPTS();
4387 }
4388 else
4389 {
4390 mtCOVERAGE_TEST_MARKER();
4391 }
4392 }
4393 else
4394 {
4395 mtCOVERAGE_TEST_MARKER();
4396 }
4397 }
4398 else
4399 {
4400 mtCOVERAGE_TEST_MARKER();
4401 }
4402 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004403
4404#endif /* portCRITICAL_NESTING_IN_TCB */
4405/*-----------------------------------------------------------*/
4406
xiaohu.huang58292b32024-01-03 14:09:51 +08004407#if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
4408
4409 static char * prvWriteNameToBuffer( char * pcBuffer,
4410 const char * pcTaskName )
4411 {
4412 size_t x;
4413
4414 /* Start by copying the entire string. */
4415 strcpy( pcBuffer, pcTaskName );
4416
4417 /* Pad the end of the string with spaces to ensure columns line up when
4418 * printed out. */
4419 for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
4420 {
4421 pcBuffer[ x ] = ' ';
4422 }
4423
4424 /* Terminate. */
4425 pcBuffer[ x ] = ( char ) 0x00;
4426
4427 /* Return the new end of string. */
4428 return &( pcBuffer[ x ] );
4429 }
4430
4431#endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
4432/*-----------------------------------------------------------*/
4433
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004434#if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
4435
xiaohu.huang58292b32024-01-03 14:09:51 +08004436 void vTaskList( char * pcWriteBuffer )
4437 {
4438 TaskStatus_t * pxTaskStatusArray;
4439 UBaseType_t uxArraySize, x;
4440 char cStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004441
xiaohu.huang58292b32024-01-03 14:09:51 +08004442 /*
4443 * PLEASE NOTE:
4444 *
4445 * This function is provided for convenience only, and is used by many
4446 * of the demo applications. Do not consider it to be part of the
4447 * scheduler.
4448 *
4449 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
4450 * uxTaskGetSystemState() output into a human readable table that
4451 * displays task: names, states, priority, stack usage and task number.
4452 * Stack usage specified as the number of unused StackType_t words stack can hold
4453 * on top of stack - not the number of bytes.
4454 *
4455 * vTaskList() has a dependency on the sprintf() C library function that
4456 * might bloat the code size, use a lot of stack, and provide different
4457 * results on different platforms. An alternative, tiny, third party,
4458 * and limited functionality implementation of sprintf() is provided in
4459 * many of the FreeRTOS/Demo sub-directories in a file called
4460 * printf-stdarg.c (note printf-stdarg.c does not provide a full
4461 * snprintf() implementation!).
4462 *
4463 * It is recommended that production systems call uxTaskGetSystemState()
4464 * directly to get access to raw stats data, rather than indirectly
4465 * through a call to vTaskList().
4466 */
Xiaohu.Huangdbadd052022-01-26 17:30:36 +08004467
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004468
xiaohu.huang58292b32024-01-03 14:09:51 +08004469 /* Make sure the write buffer does not contain a string. */
4470 *pcWriteBuffer = ( char ) 0x00;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004471
xiaohu.huang58292b32024-01-03 14:09:51 +08004472 /* Take a snapshot of the number of tasks in case it changes while this
4473 * function is executing. */
4474 uxArraySize = uxCurrentNumberOfTasks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004475
xiaohu.huang58292b32024-01-03 14:09:51 +08004476 /* Allocate an array index for each task. NOTE! if
4477 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
4478 * equate to NULL. */
4479 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation allocates a struct that has the alignment requirements of a pointer. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004480
xiaohu.huang58292b32024-01-03 14:09:51 +08004481 if( pxTaskStatusArray != NULL )
4482 {
4483 /* Generate the (binary) data. */
4484 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004485
xiaohu.huang58292b32024-01-03 14:09:51 +08004486 /* Create a human readable table from the binary data. */
4487 for( x = 0; x < uxArraySize; x++ )
4488 {
4489 switch( pxTaskStatusArray[ x ].eCurrentState )
4490 {
4491 case eRunning:
4492 cStatus = tskRUNNING_CHAR;
4493 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004494
xiaohu.huang58292b32024-01-03 14:09:51 +08004495 case eReady:
4496 cStatus = tskREADY_CHAR;
4497 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004498
xiaohu.huang58292b32024-01-03 14:09:51 +08004499 case eBlocked:
4500 cStatus = tskBLOCKED_CHAR;
4501 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004502
xiaohu.huang58292b32024-01-03 14:09:51 +08004503 case eSuspended:
4504 cStatus = tskSUSPENDED_CHAR;
4505 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004506
xiaohu.huang58292b32024-01-03 14:09:51 +08004507 case eDeleted:
4508 cStatus = tskDELETED_CHAR;
4509 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004510
xiaohu.huang58292b32024-01-03 14:09:51 +08004511 case eInvalid: /* Fall through. */
4512 default: /* Should not get here, but it is included
4513 * to prevent static checking errors. */
4514 cStatus = ( char ) 0x00;
4515 break;
4516 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004517
xiaohu.huang58292b32024-01-03 14:09:51 +08004518 /* Write the task name to the string, padding with spaces so it
4519 * can be printed in tabular form more easily. */
4520 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004521
xiaohu.huang58292b32024-01-03 14:09:51 +08004522 /* Write the rest of the string. */
4523 sprintf( pcWriteBuffer, "\t%c\t%u\t%u\t%u\r\n", cStatus, ( unsigned int ) pxTaskStatusArray[ x ].uxCurrentPriority, ( unsigned int ) pxTaskStatusArray[ x ].usStackHighWaterMark, ( unsigned int ) pxTaskStatusArray[ x ].xTaskNumber ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
4524 pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
4525 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004526
xiaohu.huang58292b32024-01-03 14:09:51 +08004527 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
4528 * is 0 then vPortFree() will be #defined to nothing. */
4529 vPortFree( pxTaskStatusArray );
4530 }
4531 else
4532 {
4533 mtCOVERAGE_TEST_MARKER();
4534 }
4535 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004536
xiaohu.huang58292b32024-01-03 14:09:51 +08004537#endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004538/*----------------------------------------------------------*/
4539
xiaohu.huang58292b32024-01-03 14:09:51 +08004540#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004541
xiaohu.huang58292b32024-01-03 14:09:51 +08004542 void vTaskGetRunTimeStats( char * pcWriteBuffer )
4543 {
4544 TaskStatus_t * pxTaskStatusArray;
4545 UBaseType_t uxArraySize, x;
4546 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004547
xiaohu.huang58292b32024-01-03 14:09:51 +08004548 /*
4549 * PLEASE NOTE:
4550 *
4551 * This function is provided for convenience only, and is used by many
4552 * of the demo applications. Do not consider it to be part of the
4553 * scheduler.
4554 *
4555 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
4556 * of the uxTaskGetSystemState() output into a human readable table that
4557 * displays the amount of time each task has spent in the Running state
4558 * in both absolute and percentage terms.
4559 *
4560 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
4561 * function that might bloat the code size, use a lot of stack, and
4562 * provide different results on different platforms. An alternative,
4563 * tiny, third party, and limited functionality implementation of
4564 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
4565 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
4566 * a full snprintf() implementation!).
4567 *
4568 * It is recommended that production systems call uxTaskGetSystemState()
4569 * directly to get access to raw stats data, rather than indirectly
4570 * through a call to vTaskGetRunTimeStats().
4571 */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004572
xiaohu.huang58292b32024-01-03 14:09:51 +08004573 /* Make sure the write buffer does not contain a string. */
4574 *pcWriteBuffer = ( char ) 0x00;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004575
xiaohu.huang58292b32024-01-03 14:09:51 +08004576 /* Take a snapshot of the number of tasks in case it changes while this
4577 * function is executing. */
4578 uxArraySize = uxCurrentNumberOfTasks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004579
xiaohu.huang58292b32024-01-03 14:09:51 +08004580 /* Allocate an array index for each task. NOTE! If
4581 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
4582 * equate to NULL. */
4583 pxTaskStatusArray = pvPortMalloc( uxCurrentNumberOfTasks * sizeof( TaskStatus_t ) ); /*lint !e9079 All values returned by pvPortMalloc() have at least the alignment required by the MCU's stack and this allocation allocates a struct that has the alignment requirements of a pointer. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004584
xiaohu.huang58292b32024-01-03 14:09:51 +08004585 if( pxTaskStatusArray != NULL )
4586 {
4587 /* Generate the (binary) data. */
4588 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004589
xiaohu.huang58292b32024-01-03 14:09:51 +08004590 /* For percentage calculations. */
4591 ulTotalTime /= 100UL;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004592
xiaohu.huang58292b32024-01-03 14:09:51 +08004593 /* Avoid divide by zero errors. */
4594 if( ulTotalTime > 0UL )
4595 {
4596 /* Create a human readable table from the binary data. */
4597 for( x = 0; x < uxArraySize; x++ )
4598 {
4599 /* What percentage of the total run time has the task used?
4600 * This will always be rounded down to the nearest integer.
4601 * ulTotalRunTime has already been divided by 100. */
4602 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004603
xiaohu.huang58292b32024-01-03 14:09:51 +08004604 /* Write the task name to the string, padding with
4605 * spaces so it can be printed in tabular form more
4606 * easily. */
4607 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004608
xiaohu.huang58292b32024-01-03 14:09:51 +08004609 if( ulStatsAsPercentage > 0UL )
4610 {
4611 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
4612 {
4613 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
4614 }
4615 #else
4616 {
4617 /* sizeof( int ) == sizeof( long ) so a smaller
4618 * printf() library can be used. */
4619 sprintf( pcWriteBuffer, "\t%u\t\t%u%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter, ( unsigned int ) ulStatsAsPercentage ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
4620 }
4621 #endif
4622 }
4623 else
4624 {
4625 /* If the percentage is zero here then the task has
4626 * consumed less than 1% of the total run time. */
4627 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
4628 {
4629 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
4630 }
4631 #else
4632 {
4633 /* sizeof( int ) == sizeof( long ) so a smaller
4634 * printf() library can be used. */
4635 sprintf( pcWriteBuffer, "\t%u\t\t<1%%\r\n", ( unsigned int ) pxTaskStatusArray[ x ].ulRunTimeCounter ); /*lint !e586 sprintf() allowed as this is compiled with many compilers and this is a utility function only - not part of the core kernel implementation. */
4636 }
4637 #endif
4638 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004639
xiaohu.huang58292b32024-01-03 14:09:51 +08004640 pcWriteBuffer += strlen( pcWriteBuffer ); /*lint !e9016 Pointer arithmetic ok on char pointers especially as in this case where it best denotes the intent of the code. */
4641 }
4642 }
4643 else
4644 {
4645 mtCOVERAGE_TEST_MARKER();
4646 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004647
xiaohu.huang58292b32024-01-03 14:09:51 +08004648 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
4649 * is 0 then vPortFree() will be #defined to nothing. */
4650 vPortFree( pxTaskStatusArray );
4651 }
4652 else
4653 {
4654 mtCOVERAGE_TEST_MARKER();
4655 }
4656 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004657
xiaohu.huang58292b32024-01-03 14:09:51 +08004658#endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004659/*-----------------------------------------------------------*/
4660
4661TickType_t uxTaskResetEventItemValue( void )
4662{
xiaohu.huang58292b32024-01-03 14:09:51 +08004663 TickType_t uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004664
xiaohu.huang58292b32024-01-03 14:09:51 +08004665 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004666
xiaohu.huang58292b32024-01-03 14:09:51 +08004667 /* Reset the event list item to its normal value - so it can be used with
4668 * queues and semaphores. */
4669 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), ( ( TickType_t ) configMAX_PRIORITIES - ( TickType_t ) pxCurrentTCB->uxPriority ) ); /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004670
xiaohu.huang58292b32024-01-03 14:09:51 +08004671 return uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004672}
4673/*-----------------------------------------------------------*/
4674
4675#if ( configUSE_MUTEXES == 1 )
4676
xiaohu.huang58292b32024-01-03 14:09:51 +08004677 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
4678 {
4679 /* If xSemaphoreCreateMutex() is called before any tasks have been created
4680 * then pxCurrentTCB will be NULL. */
4681 if( pxCurrentTCB != NULL )
4682 {
4683 ( pxCurrentTCB->uxMutexesHeld )++;
4684 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004685
xiaohu.huang58292b32024-01-03 14:09:51 +08004686 return pxCurrentTCB;
4687 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004688
4689#endif /* configUSE_MUTEXES */
4690/*-----------------------------------------------------------*/
4691
xiaohu.huang58292b32024-01-03 14:09:51 +08004692#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004693
xiaohu.huang58292b32024-01-03 14:09:51 +08004694 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
4695 BaseType_t xClearCountOnExit,
4696 TickType_t xTicksToWait )
4697 {
4698 uint32_t ulReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004699
xiaohu.huang58292b32024-01-03 14:09:51 +08004700 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004701
xiaohu.huang58292b32024-01-03 14:09:51 +08004702 taskENTER_CRITICAL();
4703 {
4704 /* Only block if the notification count is not already non-zero. */
4705 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
4706 {
4707 /* Mark this task as waiting for a notification. */
4708 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004709
xiaohu.huang58292b32024-01-03 14:09:51 +08004710 if( xTicksToWait > ( TickType_t ) 0 )
4711 {
4712 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4713 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004714
xiaohu.huang58292b32024-01-03 14:09:51 +08004715 /* All ports are written to allow a yield in a critical
4716 * section (some will yield immediately, others wait until the
4717 * critical section exits) - but it is not something that
4718 * application code should ever do. */
4719 portYIELD_WITHIN_API();
4720 }
4721 else
4722 {
4723 mtCOVERAGE_TEST_MARKER();
4724 }
4725 }
4726 else
4727 {
4728 mtCOVERAGE_TEST_MARKER();
4729 }
4730 }
4731 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004732
xiaohu.huang58292b32024-01-03 14:09:51 +08004733 taskENTER_CRITICAL();
4734 {
4735 traceTASK_NOTIFY_TAKE( uxIndexToWait );
4736 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004737
xiaohu.huang58292b32024-01-03 14:09:51 +08004738 if( ulReturn != 0UL )
4739 {
4740 if( xClearCountOnExit != pdFALSE )
4741 {
4742 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
4743 }
4744 else
4745 {
4746 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
4747 }
4748 }
4749 else
4750 {
4751 mtCOVERAGE_TEST_MARKER();
4752 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004753
xiaohu.huang58292b32024-01-03 14:09:51 +08004754 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
4755 }
4756 taskEXIT_CRITICAL();
4757
4758 return ulReturn;
4759 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004760
4761#endif /* configUSE_TASK_NOTIFICATIONS */
4762/*-----------------------------------------------------------*/
4763
xiaohu.huang58292b32024-01-03 14:09:51 +08004764#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004765
xiaohu.huang58292b32024-01-03 14:09:51 +08004766 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
4767 uint32_t ulBitsToClearOnEntry,
4768 uint32_t ulBitsToClearOnExit,
4769 uint32_t * pulNotificationValue,
4770 TickType_t xTicksToWait )
4771 {
4772 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004773
xiaohu.huang58292b32024-01-03 14:09:51 +08004774 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004775
xiaohu.huang58292b32024-01-03 14:09:51 +08004776 taskENTER_CRITICAL();
4777 {
4778 /* Only block if a notification is not already pending. */
4779 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
4780 {
4781 /* Clear bits in the task's notification value as bits may get
4782 * set by the notifying task or interrupt. This can be used to
4783 * clear the value to zero. */
4784 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004785
xiaohu.huang58292b32024-01-03 14:09:51 +08004786 /* Mark this task as waiting for a notification. */
4787 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004788
xiaohu.huang58292b32024-01-03 14:09:51 +08004789 if( xTicksToWait > ( TickType_t ) 0 )
4790 {
4791 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4792 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004793
xiaohu.huang58292b32024-01-03 14:09:51 +08004794 /* All ports are written to allow a yield in a critical
4795 * section (some will yield immediately, others wait until the
4796 * critical section exits) - but it is not something that
4797 * application code should ever do. */
4798 portYIELD_WITHIN_API();
4799 }
4800 else
4801 {
4802 mtCOVERAGE_TEST_MARKER();
4803 }
4804 }
4805 else
4806 {
4807 mtCOVERAGE_TEST_MARKER();
4808 }
4809 }
4810 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004811
xiaohu.huang58292b32024-01-03 14:09:51 +08004812 taskENTER_CRITICAL();
4813 {
4814 traceTASK_NOTIFY_WAIT( uxIndexToWait );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004815
xiaohu.huang58292b32024-01-03 14:09:51 +08004816 if( pulNotificationValue != NULL )
4817 {
4818 /* Output the current notification value, which may or may not
4819 * have changed. */
4820 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
4821 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004822
xiaohu.huang58292b32024-01-03 14:09:51 +08004823 /* If ucNotifyValue is set then either the task never entered the
4824 * blocked state (because a notification was already pending) or the
4825 * task unblocked because of a notification. Otherwise the task
4826 * unblocked because of a timeout. */
4827 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
4828 {
4829 /* A notification was not received. */
4830 xReturn = pdFALSE;
4831 }
4832 else
4833 {
4834 /* A notification was already pending or a notification was
4835 * received while the task was waiting. */
4836 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
4837 xReturn = pdTRUE;
4838 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004839
xiaohu.huang58292b32024-01-03 14:09:51 +08004840 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
4841 }
4842 taskEXIT_CRITICAL();
4843
4844 return xReturn;
4845 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004846
4847#endif /* configUSE_TASK_NOTIFICATIONS */
4848/*-----------------------------------------------------------*/
4849
xiaohu.huang58292b32024-01-03 14:09:51 +08004850#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004851
xiaohu.huang58292b32024-01-03 14:09:51 +08004852 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
4853 UBaseType_t uxIndexToNotify,
4854 uint32_t ulValue,
4855 eNotifyAction eAction,
4856 uint32_t * pulPreviousNotificationValue )
4857 {
4858 TCB_t * pxTCB;
4859 BaseType_t xReturn = pdPASS;
4860 uint8_t ucOriginalNotifyState;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004861
xiaohu.huang58292b32024-01-03 14:09:51 +08004862 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
4863 configASSERT( xTaskToNotify );
4864 pxTCB = xTaskToNotify;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004865
xiaohu.huang58292b32024-01-03 14:09:51 +08004866 taskENTER_CRITICAL();
4867 {
4868 if( pulPreviousNotificationValue != NULL )
4869 {
4870 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
4871 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004872
xiaohu.huang58292b32024-01-03 14:09:51 +08004873 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004874
xiaohu.huang58292b32024-01-03 14:09:51 +08004875 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004876
xiaohu.huang58292b32024-01-03 14:09:51 +08004877 switch( eAction )
4878 {
4879 case eSetBits:
4880 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
4881 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004882
xiaohu.huang58292b32024-01-03 14:09:51 +08004883 case eIncrement:
4884 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
4885 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004886
xiaohu.huang58292b32024-01-03 14:09:51 +08004887 case eSetValueWithOverwrite:
4888 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
4889 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004890
xiaohu.huang58292b32024-01-03 14:09:51 +08004891 case eSetValueWithoutOverwrite:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004892
xiaohu.huang58292b32024-01-03 14:09:51 +08004893 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
4894 {
4895 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
4896 }
4897 else
4898 {
4899 /* The value could not be written to the task. */
4900 xReturn = pdFAIL;
4901 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004902
xiaohu.huang58292b32024-01-03 14:09:51 +08004903 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004904
xiaohu.huang58292b32024-01-03 14:09:51 +08004905 case eNoAction:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004906
xiaohu.huang58292b32024-01-03 14:09:51 +08004907 /* The task is being notified without its notify value being
4908 * updated. */
4909 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004910
xiaohu.huang58292b32024-01-03 14:09:51 +08004911 default:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004912
xiaohu.huang58292b32024-01-03 14:09:51 +08004913 /* Should not get here if all enums are handled.
4914 * Artificially force an assert by testing a value the
4915 * compiler can't assume is const. */
4916 configASSERT( xTickCount == ( TickType_t ) 0 );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004917
xiaohu.huang58292b32024-01-03 14:09:51 +08004918 break;
4919 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004920
xiaohu.huang58292b32024-01-03 14:09:51 +08004921 traceTASK_NOTIFY( uxIndexToNotify );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004922
xiaohu.huang58292b32024-01-03 14:09:51 +08004923 /* If the task is in the blocked state specifically to wait for a
4924 * notification then unblock it now. */
4925 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
4926 {
4927 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4928 prvAddTaskToReadyList( pxTCB );
4929
4930 /* The task should not have been on an event list. */
4931 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
4932
4933 #if ( configUSE_TICKLESS_IDLE != 0 )
4934 {
4935 /* If a task is blocked waiting for a notification then
4936 * xNextTaskUnblockTime might be set to the blocked task's time
4937 * out time. If the task is unblocked for a reason other than
4938 * a timeout xNextTaskUnblockTime is normally left unchanged,
4939 * because it will automatically get reset to a new value when
4940 * the tick count equals xNextTaskUnblockTime. However if
4941 * tickless idling is used it might be more important to enter
4942 * sleep mode at the earliest possible time - so reset
4943 * xNextTaskUnblockTime here to ensure it is updated at the
4944 * earliest possible time. */
4945 prvResetNextTaskUnblockTime();
4946 }
4947 #endif
4948
4949 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4950 {
4951 /* The notified task has a priority above the currently
4952 * executing task so a yield is required. */
4953 taskYIELD_IF_USING_PREEMPTION();
4954 }
4955 else
4956 {
4957 mtCOVERAGE_TEST_MARKER();
4958 }
4959 }
4960 else
4961 {
4962 mtCOVERAGE_TEST_MARKER();
4963 }
4964 }
4965 taskEXIT_CRITICAL();
4966
4967 return xReturn;
4968 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004969
4970#endif /* configUSE_TASK_NOTIFICATIONS */
4971/*-----------------------------------------------------------*/
4972
xiaohu.huang58292b32024-01-03 14:09:51 +08004973#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004974
xiaohu.huang58292b32024-01-03 14:09:51 +08004975 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
4976 UBaseType_t uxIndexToNotify,
4977 uint32_t ulValue,
4978 eNotifyAction eAction,
4979 uint32_t * pulPreviousNotificationValue,
4980 BaseType_t * pxHigherPriorityTaskWoken )
4981 {
4982 TCB_t * pxTCB;
4983 uint8_t ucOriginalNotifyState;
4984 BaseType_t xReturn = pdPASS;
4985 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004986
xiaohu.huang58292b32024-01-03 14:09:51 +08004987 configASSERT( xTaskToNotify );
4988 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004989
xiaohu.huang58292b32024-01-03 14:09:51 +08004990 /* RTOS ports that support interrupt nesting have the concept of a
4991 * maximum system call (or maximum API call) interrupt priority.
4992 * Interrupts that are above the maximum system call priority are keep
4993 * permanently enabled, even when the RTOS kernel is in a critical section,
4994 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
4995 * is defined in FreeRTOSConfig.h then
4996 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4997 * failure if a FreeRTOS API function is called from an interrupt that has
4998 * been assigned a priority above the configured maximum system call
4999 * priority. Only FreeRTOS functions that end in FromISR can be called
5000 * from interrupts that have been assigned a priority at or (logically)
5001 * below the maximum system call interrupt priority. FreeRTOS maintains a
5002 * separate interrupt safe API to ensure interrupt entry is as fast and as
5003 * simple as possible. More information (albeit Cortex-M specific) is
5004 * provided on the following link:
5005 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5006 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005007
xiaohu.huang58292b32024-01-03 14:09:51 +08005008 pxTCB = xTaskToNotify;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005009
xiaohu.huang58292b32024-01-03 14:09:51 +08005010 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5011 {
5012 if( pulPreviousNotificationValue != NULL )
5013 {
5014 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5015 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005016
xiaohu.huang58292b32024-01-03 14:09:51 +08005017 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5018 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005019
xiaohu.huang58292b32024-01-03 14:09:51 +08005020 switch( eAction )
5021 {
5022 case eSetBits:
5023 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5024 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005025
xiaohu.huang58292b32024-01-03 14:09:51 +08005026 case eIncrement:
5027 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5028 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005029
xiaohu.huang58292b32024-01-03 14:09:51 +08005030 case eSetValueWithOverwrite:
5031 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5032 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005033
xiaohu.huang58292b32024-01-03 14:09:51 +08005034 case eSetValueWithoutOverwrite:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005035
xiaohu.huang58292b32024-01-03 14:09:51 +08005036 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5037 {
5038 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5039 }
5040 else
5041 {
5042 /* The value could not be written to the task. */
5043 xReturn = pdFAIL;
5044 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005045
xiaohu.huang58292b32024-01-03 14:09:51 +08005046 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005047
xiaohu.huang58292b32024-01-03 14:09:51 +08005048 case eNoAction:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005049
xiaohu.huang58292b32024-01-03 14:09:51 +08005050 /* The task is being notified without its notify value being
5051 * updated. */
5052 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005053
xiaohu.huang58292b32024-01-03 14:09:51 +08005054 default:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005055
xiaohu.huang58292b32024-01-03 14:09:51 +08005056 /* Should not get here if all enums are handled.
5057 * Artificially force an assert by testing a value the
5058 * compiler can't assume is const. */
5059 configASSERT( xTickCount == ( TickType_t ) 0 );
5060 break;
5061 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005062
xiaohu.huang58292b32024-01-03 14:09:51 +08005063 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005064
xiaohu.huang58292b32024-01-03 14:09:51 +08005065 /* If the task is in the blocked state specifically to wait for a
5066 * notification then unblock it now. */
5067 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5068 {
5069 /* The task should not have been on an event list. */
5070 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5071
5072 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5073 {
5074 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
5075 prvAddTaskToReadyList( pxTCB );
5076 }
5077 else
5078 {
5079 /* The delayed and ready lists cannot be accessed, so hold
5080 * this task pending until the scheduler is resumed. */
5081 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
5082 }
5083
5084 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
5085 {
5086 /* The notified task has a priority above the currently
5087 * executing task so a yield is required. */
5088 if( pxHigherPriorityTaskWoken != NULL )
5089 {
5090 *pxHigherPriorityTaskWoken = pdTRUE;
5091 }
5092
5093 /* Mark that a yield is pending in case the user is not
5094 * using the "xHigherPriorityTaskWoken" parameter to an ISR
5095 * safe FreeRTOS function. */
5096 xYieldPending = pdTRUE;
5097 }
5098 else
5099 {
5100 mtCOVERAGE_TEST_MARKER();
5101 }
5102 }
5103 }
5104 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
5105
5106 return xReturn;
5107 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005108
5109#endif /* configUSE_TASK_NOTIFICATIONS */
5110/*-----------------------------------------------------------*/
5111
xiaohu.huang58292b32024-01-03 14:09:51 +08005112#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005113
xiaohu.huang58292b32024-01-03 14:09:51 +08005114 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
5115 UBaseType_t uxIndexToNotify,
5116 BaseType_t * pxHigherPriorityTaskWoken )
5117 {
5118 TCB_t * pxTCB;
5119 uint8_t ucOriginalNotifyState;
5120 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005121
xiaohu.huang58292b32024-01-03 14:09:51 +08005122 configASSERT( xTaskToNotify );
5123 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005124
xiaohu.huang58292b32024-01-03 14:09:51 +08005125 /* RTOS ports that support interrupt nesting have the concept of a
5126 * maximum system call (or maximum API call) interrupt priority.
5127 * Interrupts that are above the maximum system call priority are keep
5128 * permanently enabled, even when the RTOS kernel is in a critical section,
5129 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
5130 * is defined in FreeRTOSConfig.h then
5131 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5132 * failure if a FreeRTOS API function is called from an interrupt that has
5133 * been assigned a priority above the configured maximum system call
5134 * priority. Only FreeRTOS functions that end in FromISR can be called
5135 * from interrupts that have been assigned a priority at or (logically)
5136 * below the maximum system call interrupt priority. FreeRTOS maintains a
5137 * separate interrupt safe API to ensure interrupt entry is as fast and as
5138 * simple as possible. More information (albeit Cortex-M specific) is
5139 * provided on the following link:
5140 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5141 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005142
xiaohu.huang58292b32024-01-03 14:09:51 +08005143 pxTCB = xTaskToNotify;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005144
xiaohu.huang58292b32024-01-03 14:09:51 +08005145 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5146 {
5147 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5148 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005149
xiaohu.huang58292b32024-01-03 14:09:51 +08005150 /* 'Giving' is equivalent to incrementing a count in a counting
5151 * semaphore. */
5152 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005153
xiaohu.huang58292b32024-01-03 14:09:51 +08005154 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005155
xiaohu.huang58292b32024-01-03 14:09:51 +08005156 /* If the task is in the blocked state specifically to wait for a
5157 * notification then unblock it now. */
5158 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5159 {
5160 /* The task should not have been on an event list. */
5161 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005162
xiaohu.huang58292b32024-01-03 14:09:51 +08005163 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5164 {
5165 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
5166 prvAddTaskToReadyList( pxTCB );
5167 }
5168 else
5169 {
5170 /* The delayed and ready lists cannot be accessed, so hold
5171 * this task pending until the scheduler is resumed. */
5172 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
5173 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005174
xiaohu.huang58292b32024-01-03 14:09:51 +08005175 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
5176 {
5177 /* The notified task has a priority above the currently
5178 * executing task so a yield is required. */
5179 if( pxHigherPriorityTaskWoken != NULL )
5180 {
5181 *pxHigherPriorityTaskWoken = pdTRUE;
5182 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005183
xiaohu.huang58292b32024-01-03 14:09:51 +08005184 /* Mark that a yield is pending in case the user is not
5185 * using the "xHigherPriorityTaskWoken" parameter in an ISR
5186 * safe FreeRTOS function. */
5187 xYieldPending = pdTRUE;
5188 }
5189 else
5190 {
5191 mtCOVERAGE_TEST_MARKER();
5192 }
5193 }
5194 }
5195 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
5196 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005197
5198#endif /* configUSE_TASK_NOTIFICATIONS */
5199/*-----------------------------------------------------------*/
5200
xiaohu.huang58292b32024-01-03 14:09:51 +08005201#if ( configUSE_TASK_NOTIFICATIONS == 1 )
5202
5203 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
5204 UBaseType_t uxIndexToClear )
5205 {
5206 TCB_t * pxTCB;
5207 BaseType_t xReturn;
5208
5209 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5210
5211 /* If null is passed in here then it is the calling task that is having
5212 * its notification state cleared. */
5213 pxTCB = prvGetTCBFromHandle( xTask );
5214
5215 taskENTER_CRITICAL();
5216 {
5217 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
5218 {
5219 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
5220 xReturn = pdPASS;
5221 }
5222 else
5223 {
5224 xReturn = pdFAIL;
5225 }
5226 }
5227 taskEXIT_CRITICAL();
5228
5229 return xReturn;
5230 }
5231
5232#endif /* configUSE_TASK_NOTIFICATIONS */
5233/*-----------------------------------------------------------*/
5234
5235#if ( configUSE_TASK_NOTIFICATIONS == 1 )
5236
5237 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
5238 UBaseType_t uxIndexToClear,
5239 uint32_t ulBitsToClear )
5240 {
5241 TCB_t * pxTCB;
5242 uint32_t ulReturn;
5243
5244 /* If null is passed in here then it is the calling task that is having
5245 * its notification state cleared. */
5246 pxTCB = prvGetTCBFromHandle( xTask );
5247
5248 taskENTER_CRITICAL();
5249 {
5250 /* Return the notification as it was before the bits were cleared,
5251 * then clear the bit mask. */
5252 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
5253 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
5254 }
5255 taskEXIT_CRITICAL();
5256
5257 return ulReturn;
5258 }
5259
5260#endif /* configUSE_TASK_NOTIFICATIONS */
5261/*-----------------------------------------------------------*/
5262
5263#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
5264
5265 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
5266 {
5267 return xIdleTaskHandle->ulRunTimeCounter;
5268 }
5269
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005270#endif
5271/*-----------------------------------------------------------*/
5272
xiaohu.huang58292b32024-01-03 14:09:51 +08005273#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
5274
5275 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
5276 {
5277 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
5278
5279 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE();
5280
5281 /* For percentage calculations. */
5282 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
5283
5284 /* Avoid divide by zero errors. */
5285 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
5286 {
5287 ulReturn = xIdleTaskHandle->ulRunTimeCounter / ulTotalTime;
5288 }
5289 else
5290 {
5291 ulReturn = 0;
5292 }
5293
5294 return ulReturn;
5295 }
5296
5297#endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
5298/*-----------------------------------------------------------*/
5299
5300static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
5301 const BaseType_t xCanBlockIndefinitely )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005302{
xiaohu.huang58292b32024-01-03 14:09:51 +08005303 TickType_t xTimeToWake;
5304 const TickType_t xConstTickCount = xTickCount;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005305
xiaohu.huang58292b32024-01-03 14:09:51 +08005306 #if ( INCLUDE_xTaskAbortDelay == 1 )
5307 {
5308 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
5309 * reset to pdFALSE so it can be detected as having been set to pdTRUE
5310 * when the task leaves the Blocked state. */
5311 pxCurrentTCB->ucDelayAborted = pdFALSE;
5312 }
5313 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005314
xiaohu.huang58292b32024-01-03 14:09:51 +08005315 /* Remove the task from the ready list before adding it to the blocked list
5316 * as the same list item is used for both lists. */
5317 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5318 {
5319 /* The current task must be in a ready list, so there is no need to
5320 * check, and the port reset macro can be called directly. */
5321 portRESET_READY_PRIORITY( pxCurrentTCB->uxPriority, uxTopReadyPriority ); /*lint !e931 pxCurrentTCB cannot change as it is the calling task. pxCurrentTCB->uxPriority and uxTopReadyPriority cannot change as called with scheduler suspended or in a critical section. */
5322 }
5323 else
5324 {
5325 mtCOVERAGE_TEST_MARKER();
5326 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005327
xiaohu.huang58292b32024-01-03 14:09:51 +08005328 #if ( INCLUDE_vTaskSuspend == 1 )
5329 {
5330 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
5331 {
5332 /* Add the task to the suspended task list instead of a delayed task
5333 * list to ensure it is not woken by a timing event. It will block
5334 * indefinitely. */
5335 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
5336 }
5337 else
5338 {
5339 /* Calculate the time at which the task should be woken if the event
5340 * does not occur. This may overflow but this doesn't matter, the
5341 * kernel will manage it correctly. */
5342 xTimeToWake = xConstTickCount + xTicksToWait;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005343
xiaohu.huang58292b32024-01-03 14:09:51 +08005344 /* The list item will be inserted in wake time order. */
5345 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005346
xiaohu.huang58292b32024-01-03 14:09:51 +08005347 if( xTimeToWake < xConstTickCount )
5348 {
5349 /* Wake time has overflowed. Place this item in the overflow
5350 * list. */
5351 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5352 }
5353 else
5354 {
5355 /* The wake time has not overflowed, so the current block list
5356 * is used. */
5357 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005358
xiaohu.huang58292b32024-01-03 14:09:51 +08005359 /* If the task entering the blocked state was placed at the
5360 * head of the list of blocked tasks then xNextTaskUnblockTime
5361 * needs to be updated too. */
5362 if( xTimeToWake < xNextTaskUnblockTime )
5363 {
5364 xNextTaskUnblockTime = xTimeToWake;
5365 }
5366 else
5367 {
5368 mtCOVERAGE_TEST_MARKER();
5369 }
5370 }
5371 }
5372 }
5373 #else /* INCLUDE_vTaskSuspend */
5374 {
5375 /* Calculate the time at which the task should be woken if the event
5376 * does not occur. This may overflow but this doesn't matter, the kernel
5377 * will manage it correctly. */
5378 xTimeToWake = xConstTickCount + xTicksToWait;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005379
xiaohu.huang58292b32024-01-03 14:09:51 +08005380 /* The list item will be inserted in wake time order. */
5381 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005382
xiaohu.huang58292b32024-01-03 14:09:51 +08005383 if( xTimeToWake < xConstTickCount )
5384 {
5385 /* Wake time has overflowed. Place this item in the overflow list. */
5386 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5387 }
5388 else
5389 {
5390 /* The wake time has not overflowed, so the current block list is used. */
5391 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005392
xiaohu.huang58292b32024-01-03 14:09:51 +08005393 /* If the task entering the blocked state was placed at the head of the
5394 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
5395 * too. */
5396 if( xTimeToWake < xNextTaskUnblockTime )
5397 {
5398 xNextTaskUnblockTime = xTimeToWake;
5399 }
5400 else
5401 {
5402 mtCOVERAGE_TEST_MARKER();
5403 }
5404 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005405
xiaohu.huang58292b32024-01-03 14:09:51 +08005406 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
5407 ( void ) xCanBlockIndefinitely;
5408 }
5409 #endif /* INCLUDE_vTaskSuspend */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005410}
5411
5412/* Code below here allows additional code to be inserted into this source file,
xiaohu.huang58292b32024-01-03 14:09:51 +08005413 * especially where access to file scope functions and data is needed (for example
5414 * when performing module tests). */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005415
5416#ifdef FREERTOS_MODULE_TEST
xiaohu.huang58292b32024-01-03 14:09:51 +08005417 #include "tasks_test_access_functions.h"
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005418#endif
5419
5420
xiaohu.huang58292b32024-01-03 14:09:51 +08005421#if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005422
xiaohu.huang58292b32024-01-03 14:09:51 +08005423 #include "freertos_tasks_c_additions.h"
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005424
xiaohu.huang58292b32024-01-03 14:09:51 +08005425 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
5426 static void freertos_tasks_c_additions_init( void )
5427 {
5428 FREERTOS_TASKS_C_ADDITIONS_INIT();
5429 }
5430 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005431
xiaohu.huang58292b32024-01-03 14:09:51 +08005432#endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
Xiaohu.Huangc9a7d4f2022-03-15 13:48:38 +08005433
5434/* Add include implement source code which depend on the inner elements */
5435#include "aml_tasks_ext.c"