blob: 8331f4e2ccb4cbb3828b5bf14f28873085804604 [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. */
267 StackType_t xStackDepth; /* AML add the member and add dummy member to xSTATIC_TCB in FreeRTOS.h */
268 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. */
537 const uint32_t ulStackDepth,
538 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. */
567 const uint32_t ulStackDepth,
568 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,
652 ( uint32_t ) pxTaskDefinition->usStackDepth,
653 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,
703 ( uint32_t ) pxTaskDefinition->usStackDepth,
704 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.huang58292b32024-01-03 14:09:51 +0800802 prvInitialiseNewTask( pxTaskCode, pcName, ( uint32_t ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
803 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. */
819 const uint32_t ulStackDepth,
820 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
xiaohu.huang58292b32024-01-03 14:09:51 +0800844 /* Avoid dependency on memset() if it is not required. */
845 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
846 {
847 /* Fill the stack with a known value to assist debugging. */
848 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
849 }
850 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
Xiaohu.Huang2c96ef42021-10-15 16:12:27 +0800851
xiaohu.huang58292b32024-01-03 14:09:51 +0800852 /* Calculate the top of stack address. This depends on whether the stack
853 * grows from high memory to low (as per the 80x86) or vice versa.
854 * portSTACK_GROWTH is used to make the result positive or negative as required
855 * by the port. */
856 #if ( portSTACK_GROWTH < 0 )
857 {
858 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( uint32_t ) 1 ] );
859 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 +0800860
xiaohu.huang58292b32024-01-03 14:09:51 +0800861 /* Check the alignment of the calculated top of stack is correct. */
862 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800863
xiaohu.huang58292b32024-01-03 14:09:51 +0800864 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
865 {
866 /* Also record the stack's high address, which may assist
867 * debugging. */
868 pxNewTCB->pxEndOfStack = pxTopOfStack;
869 }
870 #endif /* configRECORD_STACK_HIGH_ADDRESS */
871 }
872 #else /* portSTACK_GROWTH */
873 {
874 pxTopOfStack = pxNewTCB->pxStack;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800875
xiaohu.huang58292b32024-01-03 14:09:51 +0800876 /* Check the alignment of the stack buffer is correct. */
877 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800878
xiaohu.huang58292b32024-01-03 14:09:51 +0800879 /* The other extreme of the stack space is required if stack checking is
880 * performed. */
881 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( uint32_t ) 1 );
882 }
883 #endif /* portSTACK_GROWTH */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800884
xiaohu.huang58292b32024-01-03 14:09:51 +0800885 /* Store the task name in the TCB. */
886 if( pcName != NULL )
887 {
888 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
889 {
890 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800891
xiaohu.huang58292b32024-01-03 14:09:51 +0800892 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
893 * configMAX_TASK_NAME_LEN characters just in case the memory after the
894 * string is not accessible (extremely unlikely). */
895 if( pcName[ x ] == ( char ) 0x00 )
896 {
897 break;
898 }
899 else
900 {
901 mtCOVERAGE_TEST_MARKER();
902 }
903 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800904
xiaohu.huang58292b32024-01-03 14:09:51 +0800905 /* Ensure the name string is terminated in the case that the string length
906 * was greater or equal to configMAX_TASK_NAME_LEN. */
907 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
908 }
909 else
910 {
911 mtCOVERAGE_TEST_MARKER();
912 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800913
xiaohu.huang58292b32024-01-03 14:09:51 +0800914 /* This is used as an array index so must ensure it's not too large. */
915 configASSERT( uxPriority < configMAX_PRIORITIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800916
xiaohu.huang58292b32024-01-03 14:09:51 +0800917 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
918 {
919 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
920 }
921 else
922 {
923 mtCOVERAGE_TEST_MARKER();
924 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800925
xiaohu.huang58292b32024-01-03 14:09:51 +0800926 pxNewTCB->uxPriority = uxPriority;
927 #if ( configUSE_MUTEXES == 1 )
928 {
929 pxNewTCB->uxBasePriority = uxPriority;
930 }
931 #endif /* configUSE_MUTEXES */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800932
xiaohu.huang58292b32024-01-03 14:09:51 +0800933 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
934 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800935
xiaohu.huang58292b32024-01-03 14:09:51 +0800936 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
937 * back to the containing TCB from a generic item in a list. */
938 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800939
xiaohu.huang58292b32024-01-03 14:09:51 +0800940 /* Event lists are always in priority order. */
941 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. */
942 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800943
xiaohu.huang58292b32024-01-03 14:09:51 +0800944 #if ( portUSING_MPU_WRAPPERS == 1 )
945 {
946 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
947 }
948 #else
949 {
950 /* Avoid compiler warning about unreferenced parameter. */
951 ( void ) xRegions;
952 }
953 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800954
xiaohu.huang58292b32024-01-03 14:09:51 +0800955 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
956 {
957 /* Allocate and initialize memory for the task's TLS Block. */
958 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock );
959 }
960 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800961
xiaohu.huang58292b32024-01-03 14:09:51 +0800962 /* Initialize the TCB stack to look as if the task was already running,
963 * but had been interrupted by the scheduler. The return address is set
964 * to the start of the task function. Once the stack has been initialised
965 * the top of stack variable is updated. */
966 #if ( portUSING_MPU_WRAPPERS == 1 )
967 {
968 /* If the port has capability to detect stack overflow,
969 * pass the stack end address to the stack initialization
970 * function as well. */
971 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
972 {
973 #if ( portSTACK_GROWTH < 0 )
974 {
975 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
976 }
977 #else /* portSTACK_GROWTH */
978 {
979 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
980 }
981 #endif /* portSTACK_GROWTH */
982 }
983 #else /* portHAS_STACK_OVERFLOW_CHECKING */
984 {
985 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
986 }
987 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
988 }
989 #else /* portUSING_MPU_WRAPPERS */
990 {
991 /* If the port has capability to detect stack overflow,
992 * pass the stack end address to the stack initialization
993 * function as well. */
994 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
995 {
996 #if ( portSTACK_GROWTH < 0 )
997 {
998 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
999 }
1000 #else /* portSTACK_GROWTH */
1001 {
1002 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1003 }
1004 #endif /* portSTACK_GROWTH */
1005 }
1006 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1007 {
1008 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1009 }
1010 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1011 }
1012 #endif /* portUSING_MPU_WRAPPERS */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001013
xiaohu.huang58292b32024-01-03 14:09:51 +08001014 if( pxCreatedTask != NULL )
1015 {
1016 /* Pass the handle out in an anonymous way. The handle can be used to
1017 * change the created task's priority, delete the created task, etc.*/
1018 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1019 }
1020 else
1021 {
1022 mtCOVERAGE_TEST_MARKER();
1023 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001024}
1025/*-----------------------------------------------------------*/
1026
xiaohu.huang58292b32024-01-03 14:09:51 +08001027static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001028{
xiaohu.huang58292b32024-01-03 14:09:51 +08001029 /* Ensure interrupts don't access the task lists while the lists are being
1030 * updated. */
1031 taskENTER_CRITICAL();
1032 {
1033 uxCurrentNumberOfTasks++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001034
xiaohu.huang58292b32024-01-03 14:09:51 +08001035 if( pxCurrentTCB == NULL )
1036 {
1037 /* There are no other tasks, or all the other tasks are in
1038 * the suspended state - make this the current task. */
1039 pxCurrentTCB = pxNewTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001040
xiaohu.huang58292b32024-01-03 14:09:51 +08001041 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1042 {
1043 /* This is the first task to be created so do the preliminary
1044 * initialisation required. We will not recover if this call
1045 * fails, but we will report the failure. */
1046 prvInitialiseTaskLists();
1047 }
1048 else
1049 {
1050 mtCOVERAGE_TEST_MARKER();
1051 }
1052 }
1053 else
1054 {
1055 /* If the scheduler is not already running, make this task the
1056 * current task if it is the highest priority task to be created
1057 * so far. */
1058 if( xSchedulerRunning == pdFALSE )
1059 {
1060 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
1061 {
1062 pxCurrentTCB = pxNewTCB;
1063 }
1064 else
1065 {
1066 mtCOVERAGE_TEST_MARKER();
1067 }
1068 }
1069 else
1070 {
1071 mtCOVERAGE_TEST_MARKER();
1072 }
1073 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001074
xiaohu.huang58292b32024-01-03 14:09:51 +08001075 uxTaskNumber++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001076
xiaohu.huang58292b32024-01-03 14:09:51 +08001077 #if ( configUSE_TRACE_FACILITY == 1 )
1078 {
1079 /* Add a counter into the TCB for tracing only. */
1080 pxNewTCB->uxTCBNumber = uxTaskNumber;
1081 }
1082 #endif /* configUSE_TRACE_FACILITY */
1083 traceTASK_CREATE( pxNewTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001084
xiaohu.huang58292b32024-01-03 14:09:51 +08001085 prvAddTaskToReadyList( pxNewTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001086
xiaohu.huang58292b32024-01-03 14:09:51 +08001087 portSETUP_TCB( pxNewTCB );
1088 }
1089 taskEXIT_CRITICAL();
1090
1091 if( xSchedulerRunning != pdFALSE )
1092 {
1093 /* If the created task is of a higher priority than the current task
1094 * then it should run now. */
1095 if( pxCurrentTCB->uxPriority < pxNewTCB->uxPriority )
1096 {
1097 taskYIELD_IF_USING_PREEMPTION();
1098 }
1099 else
1100 {
1101 mtCOVERAGE_TEST_MARKER();
1102 }
1103 }
1104 else
1105 {
1106 mtCOVERAGE_TEST_MARKER();
1107 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001108}
1109/*-----------------------------------------------------------*/
1110
1111#if ( INCLUDE_vTaskDelete == 1 )
1112
xiaohu.huang58292b32024-01-03 14:09:51 +08001113 void vTaskDelete( TaskHandle_t xTaskToDelete )
1114 {
1115 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001116
xiaohu.huang58292b32024-01-03 14:09:51 +08001117 taskENTER_CRITICAL();
1118 {
1119 /* If null is passed in here then it is the calling task that is
1120 * being deleted. */
1121 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001122
xiaohu.huang58292b32024-01-03 14:09:51 +08001123 /* Remove task from the ready/delayed list. */
1124 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1125 {
1126 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1127 }
1128 else
1129 {
1130 mtCOVERAGE_TEST_MARKER();
1131 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001132
xiaohu.huang58292b32024-01-03 14:09:51 +08001133 /* Is the task waiting on an event also? */
1134 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1135 {
1136 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1137 }
1138 else
1139 {
1140 mtCOVERAGE_TEST_MARKER();
1141 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001142
xiaohu.huang58292b32024-01-03 14:09:51 +08001143 /* Increment the uxTaskNumber also so kernel aware debuggers can
1144 * detect that the task lists need re-generating. This is done before
1145 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1146 * not return. */
1147 uxTaskNumber++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001148
xiaohu.huang58292b32024-01-03 14:09:51 +08001149 if( pxTCB == pxCurrentTCB )
1150 {
1151 /* A task is deleting itself. This cannot complete within the
1152 * task itself, as a context switch to another task is required.
1153 * Place the task in the termination list. The idle task will
1154 * check the termination list and free up any memory allocated by
1155 * the scheduler for the TCB and stack of the deleted task. */
1156 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001157
xiaohu.huang58292b32024-01-03 14:09:51 +08001158 /* Increment the ucTasksDeleted variable so the idle task knows
1159 * there is a task that has been deleted and that it should therefore
1160 * check the xTasksWaitingTermination list. */
1161 ++uxDeletedTasksWaitingCleanUp;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001162
xiaohu.huang58292b32024-01-03 14:09:51 +08001163 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1164 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1165 traceTASK_DELETE( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001166
xiaohu.huang58292b32024-01-03 14:09:51 +08001167 /* The pre-delete hook is primarily for the Windows simulator,
1168 * in which Windows specific clean up operations are performed,
1169 * after which it is not possible to yield away from this task -
1170 * hence xYieldPending is used to latch that a context switch is
1171 * required. */
1172 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPending );
1173 }
1174 else
1175 {
1176 --uxCurrentNumberOfTasks;
1177 traceTASK_DELETE( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001178
xiaohu.huang58292b32024-01-03 14:09:51 +08001179 /* Reset the next expected unblock time in case it referred to
1180 * the task that has just been deleted. */
1181 prvResetNextTaskUnblockTime();
1182 }
1183 }
1184 taskEXIT_CRITICAL();
shijie.xiongf9b5e162022-07-14 15:12:48 +08001185
xiaohu.huang58292b32024-01-03 14:09:51 +08001186 /* If the task is not deleting itself, call prvDeleteTCB from outside of
1187 * critical section. If a task deletes itself, prvDeleteTCB is called
1188 * from prvCheckTasksWaitingTermination which is called from Idle task. */
1189 if( pxTCB != pxCurrentTCB )
1190 {
1191 prvDeleteTCB( pxTCB );
1192 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001193
xiaohu.huang58292b32024-01-03 14:09:51 +08001194 /* Force a reschedule if it is the currently running task that has just
1195 * been deleted. */
1196 if( xSchedulerRunning != pdFALSE )
1197 {
1198 if( pxTCB == pxCurrentTCB )
1199 {
1200 configASSERT( uxSchedulerSuspended == 0 );
1201 portYIELD_WITHIN_API();
1202 }
1203 else
1204 {
1205 mtCOVERAGE_TEST_MARKER();
1206 }
1207 }
1208 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001209
1210#endif /* INCLUDE_vTaskDelete */
1211/*-----------------------------------------------------------*/
1212
xiaohu.huang58292b32024-01-03 14:09:51 +08001213#if ( INCLUDE_xTaskDelayUntil == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001214
xiaohu.huang58292b32024-01-03 14:09:51 +08001215 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
1216 const TickType_t xTimeIncrement )
1217 {
1218 TickType_t xTimeToWake;
1219 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001220
xiaohu.huang58292b32024-01-03 14:09:51 +08001221 configASSERT( pxPreviousWakeTime );
1222 configASSERT( ( xTimeIncrement > 0U ) );
1223 configASSERT( uxSchedulerSuspended == 0 );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001224
xiaohu.huang58292b32024-01-03 14:09:51 +08001225 vTaskSuspendAll();
1226 {
1227 /* Minor optimisation. The tick count cannot change in this
1228 * block. */
1229 const TickType_t xConstTickCount = xTickCount;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001230
xiaohu.huang58292b32024-01-03 14:09:51 +08001231 /* Generate the tick time at which the task wants to wake. */
1232 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001233
xiaohu.huang58292b32024-01-03 14:09:51 +08001234 if( xConstTickCount < *pxPreviousWakeTime )
1235 {
1236 /* The tick count has overflowed since this function was
1237 * lasted called. In this case the only time we should ever
1238 * actually delay is if the wake time has also overflowed,
1239 * and the wake time is greater than the tick time. When this
1240 * is the case it is as if neither time had overflowed. */
1241 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1242 {
1243 xShouldDelay = pdTRUE;
1244 }
1245 else
1246 {
1247 mtCOVERAGE_TEST_MARKER();
1248 }
1249 }
1250 else
1251 {
1252 /* The tick time has not overflowed. In this case we will
1253 * delay if either the wake time has overflowed, and/or the
1254 * tick time is less than the wake time. */
1255 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1256 {
1257 xShouldDelay = pdTRUE;
1258 }
1259 else
1260 {
1261 mtCOVERAGE_TEST_MARKER();
1262 }
1263 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001264
xiaohu.huang58292b32024-01-03 14:09:51 +08001265 /* Update the wake time ready for the next call. */
1266 *pxPreviousWakeTime = xTimeToWake;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001267
xiaohu.huang58292b32024-01-03 14:09:51 +08001268 if( xShouldDelay != pdFALSE )
1269 {
1270 traceTASK_DELAY_UNTIL( xTimeToWake );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001271
xiaohu.huang58292b32024-01-03 14:09:51 +08001272 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1273 * the time to wake, so subtract the current tick count. */
1274 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1275 }
1276 else
1277 {
1278 mtCOVERAGE_TEST_MARKER();
1279 }
1280 }
1281 xAlreadyYielded = xTaskResumeAll();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001282
xiaohu.huang58292b32024-01-03 14:09:51 +08001283 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1284 * have put ourselves to sleep. */
1285 if( xAlreadyYielded == pdFALSE )
1286 {
1287 portYIELD_WITHIN_API();
1288 }
1289 else
1290 {
1291 mtCOVERAGE_TEST_MARKER();
1292 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001293
xiaohu.huang58292b32024-01-03 14:09:51 +08001294 return xShouldDelay;
1295 }
1296
1297#endif /* INCLUDE_xTaskDelayUntil */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001298/*-----------------------------------------------------------*/
1299
1300#if ( INCLUDE_vTaskDelay == 1 )
1301
xiaohu.huang58292b32024-01-03 14:09:51 +08001302 void vTaskDelay( const TickType_t xTicksToDelay )
1303 {
1304 BaseType_t xAlreadyYielded = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001305
xiaohu.huang58292b32024-01-03 14:09:51 +08001306 /* A delay time of zero just forces a reschedule. */
1307 if( xTicksToDelay > ( TickType_t ) 0U )
1308 {
1309 configASSERT( uxSchedulerSuspended == 0 );
1310 vTaskSuspendAll();
1311 {
1312 traceTASK_DELAY();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001313
xiaohu.huang58292b32024-01-03 14:09:51 +08001314 /* A task that is removed from the event list while the
1315 * scheduler is suspended will not get placed in the ready
1316 * list or removed from the blocked list until the scheduler
1317 * is resumed.
1318 *
1319 * This task cannot be in an event list as it is the currently
1320 * executing task. */
1321 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1322 }
1323 xAlreadyYielded = xTaskResumeAll();
1324 }
1325 else
1326 {
1327 mtCOVERAGE_TEST_MARKER();
1328 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001329
xiaohu.huang58292b32024-01-03 14:09:51 +08001330 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1331 * have put ourselves to sleep. */
1332 if( xAlreadyYielded == pdFALSE )
1333 {
1334 portYIELD_WITHIN_API();
1335 }
1336 else
1337 {
1338 mtCOVERAGE_TEST_MARKER();
1339 }
1340 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001341
1342#endif /* INCLUDE_vTaskDelay */
1343/*-----------------------------------------------------------*/
1344
xiaohu.huang58292b32024-01-03 14:09:51 +08001345#if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001346
xiaohu.huang58292b32024-01-03 14:09:51 +08001347 eTaskState eTaskGetState( TaskHandle_t xTask )
1348 {
1349 eTaskState eReturn;
1350 List_t const * pxStateList;
1351 List_t const * pxDelayedList;
1352 List_t const * pxOverflowedDelayedList;
1353 const TCB_t * const pxTCB = xTask;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001354
xiaohu.huang58292b32024-01-03 14:09:51 +08001355 configASSERT( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001356
xiaohu.huang58292b32024-01-03 14:09:51 +08001357 if( pxTCB == pxCurrentTCB )
1358 {
1359 /* The task calling this function is querying its own state. */
1360 eReturn = eRunning;
1361 }
1362 else
1363 {
1364 taskENTER_CRITICAL();
1365 {
1366 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1367 pxDelayedList = pxDelayedTaskList;
1368 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
1369 }
1370 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001371
xiaohu.huang58292b32024-01-03 14:09:51 +08001372 if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
1373 {
1374 /* The task being queried is referenced from one of the Blocked
1375 * lists. */
1376 eReturn = eBlocked;
1377 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001378
xiaohu.huang58292b32024-01-03 14:09:51 +08001379 #if ( INCLUDE_vTaskSuspend == 1 )
1380 else if( pxStateList == &xSuspendedTaskList )
1381 {
1382 /* The task being queried is referenced from the suspended
1383 * list. Is it genuinely suspended or is it blocked
1384 * indefinitely? */
1385 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
1386 {
1387 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1388 {
1389 BaseType_t x;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001390
xiaohu.huang58292b32024-01-03 14:09:51 +08001391 /* The task does not appear on the event list item of
1392 * and of the RTOS objects, but could still be in the
1393 * blocked state if it is waiting on its notification
1394 * rather than waiting on an object. If not, is
1395 * suspended. */
1396 eReturn = eSuspended;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001397
xiaohu.huang58292b32024-01-03 14:09:51 +08001398 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1399 {
1400 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1401 {
1402 eReturn = eBlocked;
1403 break;
1404 }
1405 }
1406 }
1407 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1408 {
1409 eReturn = eSuspended;
1410 }
1411 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1412 }
1413 else
1414 {
1415 eReturn = eBlocked;
1416 }
1417 }
1418 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001419
xiaohu.huang58292b32024-01-03 14:09:51 +08001420 #if ( INCLUDE_vTaskDelete == 1 )
1421 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
1422 {
1423 /* The task being queried is referenced from the deleted
1424 * tasks list, or it is not referenced from any lists at
1425 * all. */
1426 eReturn = eDeleted;
1427 }
1428 #endif
1429
1430 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
1431 {
1432 /* If the task is not in any other state, it must be in the
1433 * Ready (including pending ready) state. */
1434 eReturn = eReady;
1435 }
1436 }
1437
1438 return eReturn;
1439 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001440
1441#endif /* INCLUDE_eTaskGetState */
1442/*-----------------------------------------------------------*/
1443
1444#if ( INCLUDE_uxTaskPriorityGet == 1 )
1445
xiaohu.huang58292b32024-01-03 14:09:51 +08001446 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
1447 {
1448 TCB_t const * pxTCB;
1449 UBaseType_t uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001450
xiaohu.huang58292b32024-01-03 14:09:51 +08001451 taskENTER_CRITICAL();
1452 {
1453 /* If null is passed in here then it is the priority of the task
1454 * that called uxTaskPriorityGet() that is being queried. */
1455 pxTCB = prvGetTCBFromHandle( xTask );
1456 uxReturn = pxTCB->uxPriority;
1457 }
1458 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001459
xiaohu.huang58292b32024-01-03 14:09:51 +08001460 return uxReturn;
1461 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001462
1463#endif /* INCLUDE_uxTaskPriorityGet */
1464/*-----------------------------------------------------------*/
1465
1466#if ( INCLUDE_uxTaskPriorityGet == 1 )
1467
xiaohu.huang58292b32024-01-03 14:09:51 +08001468 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
1469 {
1470 TCB_t const * pxTCB;
1471 UBaseType_t uxReturn, uxSavedInterruptState;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001472
xiaohu.huang58292b32024-01-03 14:09:51 +08001473 /* RTOS ports that support interrupt nesting have the concept of a
1474 * maximum system call (or maximum API call) interrupt priority.
1475 * Interrupts that are above the maximum system call priority are keep
1476 * permanently enabled, even when the RTOS kernel is in a critical section,
1477 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
1478 * is defined in FreeRTOSConfig.h then
1479 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1480 * failure if a FreeRTOS API function is called from an interrupt that has
1481 * been assigned a priority above the configured maximum system call
1482 * priority. Only FreeRTOS functions that end in FromISR can be called
1483 * from interrupts that have been assigned a priority at or (logically)
1484 * below the maximum system call interrupt priority. FreeRTOS maintains a
1485 * separate interrupt safe API to ensure interrupt entry is as fast and as
1486 * simple as possible. More information (albeit Cortex-M specific) is
1487 * provided on the following link:
1488 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1489 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001490
xiaohu.huang58292b32024-01-03 14:09:51 +08001491 uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
1492 {
1493 /* If null is passed in here then it is the priority of the calling
1494 * task that is being queried. */
1495 pxTCB = prvGetTCBFromHandle( xTask );
1496 uxReturn = pxTCB->uxPriority;
1497 }
1498 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001499
xiaohu.huang58292b32024-01-03 14:09:51 +08001500 return uxReturn;
1501 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001502
1503#endif /* INCLUDE_uxTaskPriorityGet */
1504/*-----------------------------------------------------------*/
1505
1506#if ( INCLUDE_vTaskPrioritySet == 1 )
1507
xiaohu.huang58292b32024-01-03 14:09:51 +08001508 void vTaskPrioritySet( TaskHandle_t xTask,
1509 UBaseType_t uxNewPriority )
1510 {
1511 TCB_t * pxTCB;
1512 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
1513 BaseType_t xYieldRequired = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001514
xiaohu.huang58292b32024-01-03 14:09:51 +08001515 configASSERT( uxNewPriority < configMAX_PRIORITIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001516
xiaohu.huang58292b32024-01-03 14:09:51 +08001517 /* Ensure the new priority is valid. */
1518 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1519 {
1520 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1521 }
1522 else
1523 {
1524 mtCOVERAGE_TEST_MARKER();
1525 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001526
xiaohu.huang58292b32024-01-03 14:09:51 +08001527 taskENTER_CRITICAL();
1528 {
1529 /* If null is passed in here then it is the priority of the calling
1530 * task that is being changed. */
1531 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001532
xiaohu.huang58292b32024-01-03 14:09:51 +08001533 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001534
xiaohu.huang58292b32024-01-03 14:09:51 +08001535 #if ( configUSE_MUTEXES == 1 )
1536 {
1537 uxCurrentBasePriority = pxTCB->uxBasePriority;
1538 }
1539 #else
1540 {
1541 uxCurrentBasePriority = pxTCB->uxPriority;
1542 }
1543 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001544
xiaohu.huang58292b32024-01-03 14:09:51 +08001545 if( uxCurrentBasePriority != uxNewPriority )
1546 {
1547 /* The priority change may have readied a task of higher
1548 * priority than the calling task. */
1549 if( uxNewPriority > uxCurrentBasePriority )
1550 {
1551 if( pxTCB != pxCurrentTCB )
1552 {
1553 /* The priority of a task other than the currently
1554 * running task is being raised. Is the priority being
1555 * raised above that of the running task? */
1556 if( uxNewPriority >= pxCurrentTCB->uxPriority )
1557 {
1558 xYieldRequired = pdTRUE;
1559 }
1560 else
1561 {
1562 mtCOVERAGE_TEST_MARKER();
1563 }
1564 }
1565 else
1566 {
1567 /* The priority of the running task is being raised,
1568 * but the running task must already be the highest
1569 * priority task able to run so no yield is required. */
1570 }
1571 }
1572 else if( pxTCB == pxCurrentTCB )
1573 {
1574 /* Setting the priority of the running task down means
1575 * there may now be another task of higher priority that
1576 * is ready to execute. */
1577 xYieldRequired = pdTRUE;
1578 }
1579 else
1580 {
1581 /* Setting the priority of any other task down does not
1582 * require a yield as the running task must be above the
1583 * new priority of the task being modified. */
1584 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001585
xiaohu.huang58292b32024-01-03 14:09:51 +08001586 /* Remember the ready list the task might be referenced from
1587 * before its uxPriority member is changed so the
1588 * taskRESET_READY_PRIORITY() macro can function correctly. */
1589 uxPriorityUsedOnEntry = pxTCB->uxPriority;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001590
xiaohu.huang58292b32024-01-03 14:09:51 +08001591 #if ( configUSE_MUTEXES == 1 )
1592 {
1593 /* Only change the priority being used if the task is not
1594 * currently using an inherited priority. */
1595 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
1596 {
1597 pxTCB->uxPriority = uxNewPriority;
1598 }
1599 else
1600 {
1601 mtCOVERAGE_TEST_MARKER();
1602 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001603
xiaohu.huang58292b32024-01-03 14:09:51 +08001604 /* The base priority gets set whatever. */
1605 pxTCB->uxBasePriority = uxNewPriority;
1606 }
1607 #else /* if ( configUSE_MUTEXES == 1 ) */
1608 {
1609 pxTCB->uxPriority = uxNewPriority;
1610 }
1611 #endif /* if ( configUSE_MUTEXES == 1 ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001612
xiaohu.huang58292b32024-01-03 14:09:51 +08001613 /* Only reset the event list item value if the value is not
1614 * being used for anything else. */
1615 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
1616 {
1617 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. */
1618 }
1619 else
1620 {
1621 mtCOVERAGE_TEST_MARKER();
1622 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001623
xiaohu.huang58292b32024-01-03 14:09:51 +08001624 /* If the task is in the blocked or suspended list we need do
1625 * nothing more than change its priority variable. However, if
1626 * the task is in a ready list it needs to be removed and placed
1627 * in the list appropriate to its new priority. */
1628 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
1629 {
1630 /* The task is currently in its ready list - remove before
1631 * adding it to its new ready list. As we are in a critical
1632 * section we can do this even if the scheduler is suspended. */
1633 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1634 {
1635 /* It is known that the task is in its ready list so
1636 * there is no need to check again and the port level
1637 * reset macro can be called directly. */
1638 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
1639 }
1640 else
1641 {
1642 mtCOVERAGE_TEST_MARKER();
1643 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001644
xiaohu.huang58292b32024-01-03 14:09:51 +08001645 prvAddTaskToReadyList( pxTCB );
1646 }
1647 else
1648 {
1649 mtCOVERAGE_TEST_MARKER();
1650 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001651
xiaohu.huang58292b32024-01-03 14:09:51 +08001652 if( xYieldRequired != pdFALSE )
1653 {
1654 taskYIELD_IF_USING_PREEMPTION();
1655 }
1656 else
1657 {
1658 mtCOVERAGE_TEST_MARKER();
1659 }
1660
1661 /* Remove compiler warning about unused variables when the port
1662 * optimised task selection is not being used. */
1663 ( void ) uxPriorityUsedOnEntry;
1664 }
1665 }
1666 taskEXIT_CRITICAL();
1667 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001668
1669#endif /* INCLUDE_vTaskPrioritySet */
1670/*-----------------------------------------------------------*/
1671
1672#if ( INCLUDE_vTaskSuspend == 1 )
1673
xiaohu.huang58292b32024-01-03 14:09:51 +08001674 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
1675 {
1676 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001677
xiaohu.huang58292b32024-01-03 14:09:51 +08001678 taskENTER_CRITICAL();
1679 {
1680 /* If null is passed in here then it is the running task that is
1681 * being suspended. */
1682 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001683
xiaohu.huang58292b32024-01-03 14:09:51 +08001684 traceTASK_SUSPEND( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001685
xiaohu.huang58292b32024-01-03 14:09:51 +08001686 /* Remove task from the ready/delayed list and place in the
1687 * suspended list. */
1688 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1689 {
1690 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1691 }
1692 else
1693 {
1694 mtCOVERAGE_TEST_MARKER();
1695 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001696
xiaohu.huang58292b32024-01-03 14:09:51 +08001697 /* Is the task waiting on an event also? */
1698 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1699 {
1700 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1701 }
1702 else
1703 {
1704 mtCOVERAGE_TEST_MARKER();
1705 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001706
xiaohu.huang58292b32024-01-03 14:09:51 +08001707 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001708
xiaohu.huang58292b32024-01-03 14:09:51 +08001709 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1710 {
1711 BaseType_t x;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001712
xiaohu.huang58292b32024-01-03 14:09:51 +08001713 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1714 {
1715 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1716 {
1717 /* The task was blocked to wait for a notification, but is
1718 * now suspended, so no notification was received. */
1719 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
1720 }
1721 }
1722 }
1723 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1724 }
1725 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001726
xiaohu.huang58292b32024-01-03 14:09:51 +08001727 if( xSchedulerRunning != pdFALSE )
1728 {
1729 /* Reset the next expected unblock time in case it referred to the
1730 * task that is now in the Suspended state. */
1731 taskENTER_CRITICAL();
1732 {
1733 prvResetNextTaskUnblockTime();
1734 }
1735 taskEXIT_CRITICAL();
1736 }
1737 else
1738 {
1739 mtCOVERAGE_TEST_MARKER();
1740 }
1741
1742 if( pxTCB == pxCurrentTCB )
1743 {
1744 if( xSchedulerRunning != pdFALSE )
1745 {
1746 /* The current task has just been suspended. */
1747 configASSERT( uxSchedulerSuspended == 0 );
1748 portYIELD_WITHIN_API();
1749 }
1750 else
1751 {
1752 /* The scheduler is not running, but the task that was pointed
1753 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
1754 * must be adjusted to point to a different task. */
1755 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
1756 {
1757 /* No other tasks are ready, so set pxCurrentTCB back to
1758 * NULL so when the next task is created pxCurrentTCB will
1759 * be set to point to it no matter what its relative priority
1760 * is. */
1761 pxCurrentTCB = NULL;
1762 }
1763 else
1764 {
1765 vTaskSwitchContext();
1766 }
1767 }
1768 }
1769 else
1770 {
1771 mtCOVERAGE_TEST_MARKER();
1772 }
1773 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001774
1775#endif /* INCLUDE_vTaskSuspend */
1776/*-----------------------------------------------------------*/
1777
1778#if ( INCLUDE_vTaskSuspend == 1 )
1779
xiaohu.huang58292b32024-01-03 14:09:51 +08001780 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
1781 {
1782 BaseType_t xReturn = pdFALSE;
1783 const TCB_t * const pxTCB = xTask;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001784
xiaohu.huang58292b32024-01-03 14:09:51 +08001785 /* Accesses xPendingReadyList so must be called from a critical
1786 * section. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001787
xiaohu.huang58292b32024-01-03 14:09:51 +08001788 /* It does not make sense to check if the calling task is suspended. */
1789 configASSERT( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001790
xiaohu.huang58292b32024-01-03 14:09:51 +08001791 /* Is the task being resumed actually in the suspended list? */
1792 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
1793 {
1794 /* Has the task already been resumed from within an ISR? */
1795 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
1796 {
1797 /* Is it in the suspended list because it is in the Suspended
1798 * state, or because is is blocked with no timeout? */
1799 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
1800 {
1801 xReturn = pdTRUE;
1802 }
1803 else
1804 {
1805 mtCOVERAGE_TEST_MARKER();
1806 }
1807 }
1808 else
1809 {
1810 mtCOVERAGE_TEST_MARKER();
1811 }
1812 }
1813 else
1814 {
1815 mtCOVERAGE_TEST_MARKER();
1816 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001817
xiaohu.huang58292b32024-01-03 14:09:51 +08001818 return xReturn;
1819 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001820
1821#endif /* INCLUDE_vTaskSuspend */
1822/*-----------------------------------------------------------*/
1823
1824#if ( INCLUDE_vTaskSuspend == 1 )
1825
xiaohu.huang58292b32024-01-03 14:09:51 +08001826 void vTaskResume( TaskHandle_t xTaskToResume )
1827 {
1828 TCB_t * const pxTCB = xTaskToResume;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001829
xiaohu.huang58292b32024-01-03 14:09:51 +08001830 /* It does not make sense to resume the calling task. */
1831 configASSERT( xTaskToResume );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001832
xiaohu.huang58292b32024-01-03 14:09:51 +08001833 /* The parameter cannot be NULL as it is impossible to resume the
1834 * currently executing task. */
1835 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
1836 {
1837 taskENTER_CRITICAL();
1838 {
1839 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
1840 {
1841 traceTASK_RESUME( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001842
xiaohu.huang58292b32024-01-03 14:09:51 +08001843 /* The ready list can be accessed even if the scheduler is
1844 * suspended because this is inside a critical section. */
1845 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
1846 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001847
xiaohu.huang58292b32024-01-03 14:09:51 +08001848 /* A higher priority task may have just been resumed. */
1849 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
1850 {
1851 /* This yield may not cause the task just resumed to run,
1852 * but will leave the lists in the correct state for the
1853 * next yield. */
1854 taskYIELD_IF_USING_PREEMPTION();
1855 }
1856 else
1857 {
1858 mtCOVERAGE_TEST_MARKER();
1859 }
1860 }
1861 else
1862 {
1863 mtCOVERAGE_TEST_MARKER();
1864 }
1865 }
1866 taskEXIT_CRITICAL();
1867 }
1868 else
1869 {
1870 mtCOVERAGE_TEST_MARKER();
1871 }
1872 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001873
1874#endif /* INCLUDE_vTaskSuspend */
1875
1876/*-----------------------------------------------------------*/
1877
1878#if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
1879
xiaohu.huang58292b32024-01-03 14:09:51 +08001880 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
1881 {
1882 BaseType_t xYieldRequired = pdFALSE;
1883 TCB_t * const pxTCB = xTaskToResume;
1884 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001885
xiaohu.huang58292b32024-01-03 14:09:51 +08001886 configASSERT( xTaskToResume );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001887
xiaohu.huang58292b32024-01-03 14:09:51 +08001888 /* RTOS ports that support interrupt nesting have the concept of a
1889 * maximum system call (or maximum API call) interrupt priority.
1890 * Interrupts that are above the maximum system call priority are keep
1891 * permanently enabled, even when the RTOS kernel is in a critical section,
1892 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
1893 * is defined in FreeRTOSConfig.h then
1894 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1895 * failure if a FreeRTOS API function is called from an interrupt that has
1896 * been assigned a priority above the configured maximum system call
1897 * priority. Only FreeRTOS functions that end in FromISR can be called
1898 * from interrupts that have been assigned a priority at or (logically)
1899 * below the maximum system call interrupt priority. FreeRTOS maintains a
1900 * separate interrupt safe API to ensure interrupt entry is as fast and as
1901 * simple as possible. More information (albeit Cortex-M specific) is
1902 * provided on the following link:
1903 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1904 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001905
xiaohu.huang58292b32024-01-03 14:09:51 +08001906 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1907 {
1908 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
1909 {
1910 traceTASK_RESUME_FROM_ISR( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001911
xiaohu.huang58292b32024-01-03 14:09:51 +08001912 /* Check the ready lists can be accessed. */
1913 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
1914 {
1915 /* Ready lists can be accessed so move the task from the
1916 * suspended list to the ready list directly. */
1917 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
1918 {
1919 xYieldRequired = pdTRUE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001920
xiaohu.huang58292b32024-01-03 14:09:51 +08001921 /* Mark that a yield is pending in case the user is not
1922 * using the return value to initiate a context switch
1923 * from the ISR using portYIELD_FROM_ISR. */
1924 xYieldPending = pdTRUE;
1925 }
1926 else
1927 {
1928 mtCOVERAGE_TEST_MARKER();
1929 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001930
xiaohu.huang58292b32024-01-03 14:09:51 +08001931 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
1932 prvAddTaskToReadyList( pxTCB );
1933 }
1934 else
1935 {
1936 /* The delayed or ready lists cannot be accessed so the task
1937 * is held in the pending ready list until the scheduler is
1938 * unsuspended. */
1939 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
1940 }
1941 }
1942 else
1943 {
1944 mtCOVERAGE_TEST_MARKER();
1945 }
1946 }
1947 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1948
1949 return xYieldRequired;
1950 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001951
1952#endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
1953/*-----------------------------------------------------------*/
1954
1955void vTaskStartScheduler( void )
1956{
xiaohu.huang58292b32024-01-03 14:09:51 +08001957 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001958
xiaohu.huang58292b32024-01-03 14:09:51 +08001959 /* Add the idle task at the lowest priority. */
1960 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1961 {
1962 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
1963 StackType_t * pxIdleTaskStackBuffer = NULL;
1964 uint32_t ulIdleTaskStackSize;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001965
xiaohu.huang58292b32024-01-03 14:09:51 +08001966 /* The Idle task is created using user provided RAM - obtain the
1967 * address of the RAM then create the idle task. */
1968 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
1969 xIdleTaskHandle = xTaskCreateStatic( prvIdleTask,
1970 configIDLE_TASK_NAME,
1971 ulIdleTaskStackSize,
1972 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
1973 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
1974 pxIdleTaskStackBuffer,
1975 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 +08001976
xiaohu.huang58292b32024-01-03 14:09:51 +08001977 if( xIdleTaskHandle != NULL )
1978 {
1979 xReturn = pdPASS;
1980 }
1981 else
1982 {
1983 xReturn = pdFAIL;
1984 }
1985 }
1986 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1987 {
1988 /* The Idle task is being created using dynamically allocated RAM. */
1989 xReturn = xTaskCreate( prvIdleTask,
1990 configIDLE_TASK_NAME,
1991 configMINIMAL_STACK_SIZE,
1992 ( void * ) NULL,
1993 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
1994 &xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
1995 }
1996 #endif /* configSUPPORT_STATIC_ALLOCATION */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001997
xiaohu.huang58292b32024-01-03 14:09:51 +08001998 #if ( configUSE_TIMERS == 1 )
1999 {
2000 if( xReturn == pdPASS )
2001 {
2002 xReturn = xTimerCreateTimerTask();
2003 }
2004 else
2005 {
2006 mtCOVERAGE_TEST_MARKER();
2007 }
2008 }
2009 #endif /* configUSE_TIMERS */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002010
xiaohu.huang58292b32024-01-03 14:09:51 +08002011 if( xReturn == pdPASS )
2012 {
2013 /* freertos_tasks_c_additions_init() should only be called if the user
2014 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
2015 * the only macro called by the function. */
2016 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
2017 {
2018 freertos_tasks_c_additions_init();
2019 }
2020 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002021
xiaohu.huang58292b32024-01-03 14:09:51 +08002022 /* Interrupts are turned off here, to ensure a tick does not occur
2023 * before or during the call to xPortStartScheduler(). The stacks of
2024 * the created tasks contain a status word with interrupts switched on
2025 * so interrupts will automatically get re-enabled when the first task
2026 * starts to run. */
2027 portDISABLE_INTERRUPTS();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002028
xiaohu.huang58292b32024-01-03 14:09:51 +08002029 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
2030 {
2031 /* Switch C-Runtime's TLS Block to point to the TLS
2032 * block specific to the task that will run first. */
2033 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
2034 }
2035 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002036
xiaohu.huang58292b32024-01-03 14:09:51 +08002037 xNextTaskUnblockTime = portMAX_DELAY;
2038 xSchedulerRunning = pdTRUE;
2039 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002040
xiaohu.huang58292b32024-01-03 14:09:51 +08002041 /* If configGENERATE_RUN_TIME_STATS is defined then the following
2042 * macro must be defined to configure the timer/counter used to generate
2043 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
2044 * is set to 0 and the following line fails to build then ensure you do not
2045 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
2046 * FreeRTOSConfig.h file. */
2047 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002048
xiaohu.huang58292b32024-01-03 14:09:51 +08002049 traceTASK_SWITCHED_IN();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002050
xiaohu.huang58292b32024-01-03 14:09:51 +08002051 /* Setting up the timer tick is hardware specific and thus in the
2052 * portable interface. */
2053 xPortStartScheduler();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002054
xiaohu.huang58292b32024-01-03 14:09:51 +08002055 /* In most cases, xPortStartScheduler() will not return. If it
2056 * returns pdTRUE then there was not enough heap memory available
2057 * to create either the Idle or the Timer task. If it returned
2058 * pdFALSE, then the application called xTaskEndScheduler().
2059 * Most ports don't implement xTaskEndScheduler() as there is
2060 * nothing to return to. */
2061 }
2062 else
2063 {
2064 /* This line will only be reached if the kernel could not be started,
2065 * because there was not enough FreeRTOS heap to create the idle task
2066 * or the timer task. */
2067 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
2068 }
2069
2070 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
2071 * meaning xIdleTaskHandle is not used anywhere else. */
2072 ( void ) xIdleTaskHandle;
2073
2074 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
2075 * from getting optimized out as it is no longer used by the kernel. */
2076 ( void ) uxTopUsedPriority;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002077}
2078/*-----------------------------------------------------------*/
2079
2080void vTaskEndScheduler( void )
2081{
xiaohu.huang58292b32024-01-03 14:09:51 +08002082 /* Stop the scheduler interrupts and call the portable scheduler end
2083 * routine so the original ISRs can be restored if necessary. The port
2084 * layer must ensure interrupts enable bit is left in the correct state. */
2085 portDISABLE_INTERRUPTS();
2086 xSchedulerRunning = pdFALSE;
2087 vPortEndScheduler();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002088}
2089/*----------------------------------------------------------*/
2090
2091void vTaskSuspendAll( void )
2092{
xiaohu.huang58292b32024-01-03 14:09:51 +08002093 /* A critical section is not required as the variable is of type
2094 * BaseType_t. Please read Richard Barry's reply in the following link to a
2095 * post in the FreeRTOS support forum before reporting this as a bug! -
2096 * https://goo.gl/wu4acr */
2097
2098 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
2099 * do not otherwise exhibit real time behaviour. */
2100 portSOFTWARE_BARRIER();
2101
2102 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
2103 * is used to allow calls to vTaskSuspendAll() to nest. */
2104 ++uxSchedulerSuspended;
2105
2106 /* Enforces ordering for ports and optimised compilers that may otherwise place
2107 * the above increment elsewhere. */
2108 portMEMORY_BARRIER();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002109}
2110/*----------------------------------------------------------*/
2111
2112#if ( configUSE_TICKLESS_IDLE != 0 )
2113
xiaohu.huang58292b32024-01-03 14:09:51 +08002114 static TickType_t prvGetExpectedIdleTime( void )
2115 {
2116 TickType_t xReturn;
2117 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002118
xiaohu.huang58292b32024-01-03 14:09:51 +08002119 /* uxHigherPriorityReadyTasks takes care of the case where
2120 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
2121 * task that are in the Ready state, even though the idle task is
2122 * running. */
2123 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
2124 {
2125 if( uxTopReadyPriority > tskIDLE_PRIORITY )
2126 {
2127 uxHigherPriorityReadyTasks = pdTRUE;
2128 }
2129 }
2130 #else
2131 {
2132 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002133
xiaohu.huang58292b32024-01-03 14:09:51 +08002134 /* When port optimised task selection is used the uxTopReadyPriority
2135 * variable is used as a bit map. If bits other than the least
2136 * significant bit are set then there are tasks that have a priority
2137 * above the idle priority that are in the Ready state. This takes
2138 * care of the case where the co-operative scheduler is in use. */
2139 if( uxTopReadyPriority > uxLeastSignificantBit )
2140 {
2141 uxHigherPriorityReadyTasks = pdTRUE;
2142 }
2143 }
2144 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002145
xiaohu.huang58292b32024-01-03 14:09:51 +08002146 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
2147 {
2148 xReturn = 0;
2149 }
2150 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
2151 {
2152 /* There are other idle priority tasks in the ready state. If
2153 * time slicing is used then the very next tick interrupt must be
2154 * processed. */
2155 xReturn = 0;
2156 }
2157 else if( uxHigherPriorityReadyTasks != pdFALSE )
2158 {
2159 /* There are tasks in the Ready state that have a priority above the
2160 * idle priority. This path can only be reached if
2161 * configUSE_PREEMPTION is 0. */
2162 xReturn = 0;
2163 }
2164 else
2165 {
2166 xReturn = xNextTaskUnblockTime - xTickCount;
2167 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002168
xiaohu.huang58292b32024-01-03 14:09:51 +08002169 return xReturn;
2170 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002171
2172#endif /* configUSE_TICKLESS_IDLE */
2173/*----------------------------------------------------------*/
2174
2175BaseType_t xTaskResumeAll( void )
2176{
xiaohu.huang58292b32024-01-03 14:09:51 +08002177 TCB_t * pxTCB = NULL;
2178 BaseType_t xAlreadyYielded = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002179
xiaohu.huang58292b32024-01-03 14:09:51 +08002180 /* If uxSchedulerSuspended is zero then this function does not match a
2181 * previous call to vTaskSuspendAll(). */
2182 configASSERT( uxSchedulerSuspended );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002183
xiaohu.huang58292b32024-01-03 14:09:51 +08002184 /* It is possible that an ISR caused a task to be removed from an event
2185 * list while the scheduler was suspended. If this was the case then the
2186 * removed task will have been added to the xPendingReadyList. Once the
2187 * scheduler has been resumed it is safe to move all the pending ready
2188 * tasks from this list into their appropriate ready list. */
2189 taskENTER_CRITICAL();
2190 {
2191 --uxSchedulerSuspended;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002192
xiaohu.huang58292b32024-01-03 14:09:51 +08002193 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2194 {
2195 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
2196 {
2197 /* Move any readied tasks from the pending list into the
2198 * appropriate ready list. */
2199 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
2200 {
2201 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. */
2202 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
2203 portMEMORY_BARRIER();
2204 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
2205 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002206
xiaohu.huang58292b32024-01-03 14:09:51 +08002207 /* If the moved task has a priority higher than or equal to
2208 * the current task then a yield must be performed. */
2209 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
2210 {
2211 xYieldPending = pdTRUE;
2212 }
2213 else
2214 {
2215 mtCOVERAGE_TEST_MARKER();
2216 }
2217 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002218
xiaohu.huang58292b32024-01-03 14:09:51 +08002219 if( pxTCB != NULL )
2220 {
2221 /* A task was unblocked while the scheduler was suspended,
2222 * which may have prevented the next unblock time from being
2223 * re-calculated, in which case re-calculate it now. Mainly
2224 * important for low power tickless implementations, where
2225 * this can prevent an unnecessary exit from low power
2226 * state. */
2227 prvResetNextTaskUnblockTime();
2228 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002229
xiaohu.huang58292b32024-01-03 14:09:51 +08002230 /* If any ticks occurred while the scheduler was suspended then
2231 * they should be processed now. This ensures the tick count does
2232 * not slip, and that any delayed tasks are resumed at the correct
2233 * time. */
2234 {
2235 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002236
xiaohu.huang58292b32024-01-03 14:09:51 +08002237 if( xPendedCounts > ( TickType_t ) 0U )
2238 {
2239 do
2240 {
2241 if( xTaskIncrementTick() != pdFALSE )
2242 {
2243 xYieldPending = pdTRUE;
2244 }
2245 else
2246 {
2247 mtCOVERAGE_TEST_MARKER();
2248 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002249
xiaohu.huang58292b32024-01-03 14:09:51 +08002250 --xPendedCounts;
2251 } while( xPendedCounts > ( TickType_t ) 0U );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002252
xiaohu.huang58292b32024-01-03 14:09:51 +08002253 xPendedTicks = 0;
2254 }
2255 else
2256 {
2257 mtCOVERAGE_TEST_MARKER();
2258 }
2259 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002260
xiaohu.huang58292b32024-01-03 14:09:51 +08002261 if( xYieldPending != pdFALSE )
2262 {
2263 #if ( configUSE_PREEMPTION != 0 )
2264 {
2265 xAlreadyYielded = pdTRUE;
2266 }
2267 #endif
2268 taskYIELD_IF_USING_PREEMPTION();
2269 }
2270 else
2271 {
2272 mtCOVERAGE_TEST_MARKER();
2273 }
2274 }
2275 }
2276 else
2277 {
2278 mtCOVERAGE_TEST_MARKER();
2279 }
2280 }
2281 taskEXIT_CRITICAL();
2282
2283 return xAlreadyYielded;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002284}
2285/*-----------------------------------------------------------*/
2286
2287TickType_t xTaskGetTickCount( void )
2288{
xiaohu.huang58292b32024-01-03 14:09:51 +08002289 TickType_t xTicks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002290
xiaohu.huang58292b32024-01-03 14:09:51 +08002291 /* Critical section required if running on a 16 bit processor. */
2292 portTICK_TYPE_ENTER_CRITICAL();
2293 {
2294 xTicks = xTickCount;
2295 }
2296 portTICK_TYPE_EXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002297
xiaohu.huang58292b32024-01-03 14:09:51 +08002298 return xTicks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002299}
2300/*-----------------------------------------------------------*/
2301
2302TickType_t xTaskGetTickCountFromISR( void )
2303{
xiaohu.huang58292b32024-01-03 14:09:51 +08002304 TickType_t xReturn;
2305 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002306
xiaohu.huang58292b32024-01-03 14:09:51 +08002307 /* RTOS ports that support interrupt nesting have the concept of a maximum
2308 * system call (or maximum API call) interrupt priority. Interrupts that are
2309 * above the maximum system call priority are kept permanently enabled, even
2310 * when the RTOS kernel is in a critical section, but cannot make any calls to
2311 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
2312 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2313 * failure if a FreeRTOS API function is called from an interrupt that has been
2314 * assigned a priority above the configured maximum system call priority.
2315 * Only FreeRTOS functions that end in FromISR can be called from interrupts
2316 * that have been assigned a priority at or (logically) below the maximum
2317 * system call interrupt priority. FreeRTOS maintains a separate interrupt
2318 * safe API to ensure interrupt entry is as fast and as simple as possible.
2319 * More information (albeit Cortex-M specific) is provided on the following
2320 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2321 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002322
xiaohu.huang58292b32024-01-03 14:09:51 +08002323 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
2324 {
2325 xReturn = xTickCount;
2326 }
2327 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002328
xiaohu.huang58292b32024-01-03 14:09:51 +08002329 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002330}
2331/*-----------------------------------------------------------*/
2332
2333UBaseType_t uxTaskGetNumberOfTasks( void )
2334{
xiaohu.huang58292b32024-01-03 14:09:51 +08002335 /* A critical section is not required because the variables are of type
2336 * BaseType_t. */
2337 return uxCurrentNumberOfTasks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002338}
2339/*-----------------------------------------------------------*/
2340
xiaohu.huang58292b32024-01-03 14:09:51 +08002341char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002342{
xiaohu.huang58292b32024-01-03 14:09:51 +08002343 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002344
xiaohu.huang58292b32024-01-03 14:09:51 +08002345 /* If null is passed in here then the name of the calling task is being
2346 * queried. */
2347 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
2348 configASSERT( pxTCB );
2349 return &( pxTCB->pcTaskName[ 0 ] );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002350}
xiaohu.huang58292b32024-01-03 14:09:51 +08002351/*-----------------------------------------------------------*/
Kelvin Zhang7f929772021-12-31 17:58:17 +08002352
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002353#if ( INCLUDE_xTaskGetHandle == 1 )
2354
xiaohu.huang58292b32024-01-03 14:09:51 +08002355 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
2356 const char pcNameToQuery[] )
2357 {
2358 TCB_t * pxNextTCB;
2359 TCB_t * pxFirstTCB;
2360 TCB_t * pxReturn = NULL;
2361 UBaseType_t x;
2362 char cNextChar;
2363 BaseType_t xBreakLoop;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002364
xiaohu.huang58292b32024-01-03 14:09:51 +08002365 /* This function is called with the scheduler suspended. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002366
xiaohu.huang58292b32024-01-03 14:09:51 +08002367 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
2368 {
2369 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 +08002370
xiaohu.huang58292b32024-01-03 14:09:51 +08002371 do
2372 {
2373 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 +08002374
xiaohu.huang58292b32024-01-03 14:09:51 +08002375 /* Check each character in the name looking for a match or
2376 * mismatch. */
2377 xBreakLoop = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002378
xiaohu.huang58292b32024-01-03 14:09:51 +08002379 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2380 {
2381 cNextChar = pxNextTCB->pcTaskName[ x ];
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002382
xiaohu.huang58292b32024-01-03 14:09:51 +08002383 if( cNextChar != pcNameToQuery[ x ] )
2384 {
2385 /* Characters didn't match. */
2386 xBreakLoop = pdTRUE;
2387 }
2388 else if( cNextChar == ( char ) 0x00 )
2389 {
2390 /* Both strings terminated, a match must have been
2391 * found. */
2392 pxReturn = pxNextTCB;
2393 xBreakLoop = pdTRUE;
2394 }
2395 else
2396 {
2397 mtCOVERAGE_TEST_MARKER();
2398 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002399
xiaohu.huang58292b32024-01-03 14:09:51 +08002400 if( xBreakLoop != pdFALSE )
2401 {
2402 break;
2403 }
2404 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002405
xiaohu.huang58292b32024-01-03 14:09:51 +08002406 if( pxReturn != NULL )
2407 {
2408 /* The handle has been found. */
2409 break;
2410 }
2411 } while( pxNextTCB != pxFirstTCB );
2412 }
2413 else
2414 {
2415 mtCOVERAGE_TEST_MARKER();
2416 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002417
xiaohu.huang58292b32024-01-03 14:09:51 +08002418 return pxReturn;
2419 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002420
2421#endif /* INCLUDE_xTaskGetHandle */
2422/*-----------------------------------------------------------*/
2423
2424#if ( INCLUDE_xTaskGetHandle == 1 )
2425
xiaohu.huang58292b32024-01-03 14:09:51 +08002426 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2427 {
2428 UBaseType_t uxQueue = configMAX_PRIORITIES;
2429 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002430
xiaohu.huang58292b32024-01-03 14:09:51 +08002431 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
2432 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002433
xiaohu.huang58292b32024-01-03 14:09:51 +08002434 vTaskSuspendAll();
2435 {
2436 /* Search the ready lists. */
2437 do
2438 {
2439 uxQueue--;
2440 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002441
xiaohu.huang58292b32024-01-03 14:09:51 +08002442 if( pxTCB != NULL )
2443 {
2444 /* Found the handle. */
2445 break;
2446 }
2447 } 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 +08002448
xiaohu.huang58292b32024-01-03 14:09:51 +08002449 /* Search the delayed lists. */
2450 if( pxTCB == NULL )
2451 {
2452 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
2453 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002454
xiaohu.huang58292b32024-01-03 14:09:51 +08002455 if( pxTCB == NULL )
2456 {
2457 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
2458 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002459
xiaohu.huang58292b32024-01-03 14:09:51 +08002460 #if ( INCLUDE_vTaskSuspend == 1 )
2461 {
2462 if( pxTCB == NULL )
2463 {
2464 /* Search the suspended list. */
2465 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
2466 }
2467 }
2468 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002469
xiaohu.huang58292b32024-01-03 14:09:51 +08002470 #if ( INCLUDE_vTaskDelete == 1 )
2471 {
2472 if( pxTCB == NULL )
2473 {
2474 /* Search the deleted list. */
2475 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
2476 }
2477 }
2478 #endif
2479 }
2480 ( void ) xTaskResumeAll();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002481
xiaohu.huang58292b32024-01-03 14:09:51 +08002482 return pxTCB;
2483 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002484
2485#endif /* INCLUDE_xTaskGetHandle */
2486/*-----------------------------------------------------------*/
2487
2488#if ( configUSE_TRACE_FACILITY == 1 )
2489
xiaohu.huang58292b32024-01-03 14:09:51 +08002490 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
2491 const UBaseType_t uxArraySize,
2492 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
2493 {
2494 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002495
xiaohu.huang58292b32024-01-03 14:09:51 +08002496 vTaskSuspendAll();
2497 {
2498 /* Is there a space in the array for each task in the system? */
2499 if( uxArraySize >= uxCurrentNumberOfTasks )
2500 {
2501 /* Fill in an TaskStatus_t structure with information on each
2502 * task in the Ready state. */
2503 do
2504 {
2505 uxQueue--;
2506 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
2507 } 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 +08002508
xiaohu.huang58292b32024-01-03 14:09:51 +08002509 /* Fill in an TaskStatus_t structure with information on each
2510 * task in the Blocked state. */
2511 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
2512 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002513
xiaohu.huang58292b32024-01-03 14:09:51 +08002514 #if ( INCLUDE_vTaskDelete == 1 )
2515 {
2516 /* Fill in an TaskStatus_t structure with information on
2517 * each task that has been deleted but not yet cleaned up. */
2518 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
2519 }
2520 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002521
xiaohu.huang58292b32024-01-03 14:09:51 +08002522 #if ( INCLUDE_vTaskSuspend == 1 )
2523 {
2524 /* Fill in an TaskStatus_t structure with information on
2525 * each task in the Suspended state. */
2526 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
2527 }
2528 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002529
xiaohu.huang58292b32024-01-03 14:09:51 +08002530 #if ( configGENERATE_RUN_TIME_STATS == 1 )
2531 {
2532 if( pulTotalRunTime != NULL )
2533 {
2534 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
2535 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
2536 #else
2537 *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
2538 #endif
2539 }
2540 }
2541 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
2542 {
2543 if( pulTotalRunTime != NULL )
2544 {
2545 *pulTotalRunTime = 0;
2546 }
2547 }
2548 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
2549 }
2550 else
2551 {
2552 mtCOVERAGE_TEST_MARKER();
2553 }
2554 }
2555 ( void ) xTaskResumeAll();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002556
xiaohu.huang58292b32024-01-03 14:09:51 +08002557 return uxTask;
2558 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002559
2560#endif /* configUSE_TRACE_FACILITY */
2561/*----------------------------------------------------------*/
2562
2563#if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
2564
xiaohu.huang58292b32024-01-03 14:09:51 +08002565 TaskHandle_t xTaskGetIdleTaskHandle( void )
2566 {
2567 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
2568 * started, then xIdleTaskHandle will be NULL. */
2569 configASSERT( ( xIdleTaskHandle != NULL ) );
2570 return xIdleTaskHandle;
2571 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002572
2573#endif /* INCLUDE_xTaskGetIdleTaskHandle */
2574/*----------------------------------------------------------*/
2575
2576/* This conditional compilation should use inequality to 0, not equality to 1.
xiaohu.huang58292b32024-01-03 14:09:51 +08002577 * This is to ensure vTaskStepTick() is available when user defined low power mode
2578 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
2579 * 1. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002580#if ( configUSE_TICKLESS_IDLE != 0 )
2581
xiaohu.huang58292b32024-01-03 14:09:51 +08002582 void vTaskStepTick( TickType_t xTicksToJump )
2583 {
2584 /* Correct the tick count value after a period during which the tick
2585 * was suppressed. Note this does *not* call the tick hook function for
2586 * each stepped tick. */
2587 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
2588
2589 if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
2590 {
2591 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
2592 * xTaskIncrementTick() when the scheduler resumes. This ensures
2593 * that any delayed tasks are resumed at the correct time. */
2594 configASSERT( uxSchedulerSuspended );
2595 configASSERT( xTicksToJump != ( TickType_t ) 0 );
2596
2597 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
2598 taskENTER_CRITICAL();
2599 {
2600 xPendedTicks++;
2601 }
2602 taskEXIT_CRITICAL();
2603 xTicksToJump--;
2604 }
2605 else
2606 {
2607 mtCOVERAGE_TEST_MARKER();
2608 }
2609
2610 xTickCount += xTicksToJump;
2611 traceINCREASE_TICK_COUNT( xTicksToJump );
2612 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002613
2614#endif /* configUSE_TICKLESS_IDLE */
2615/*----------------------------------------------------------*/
2616
xiaohu.huang58292b32024-01-03 14:09:51 +08002617BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
2618{
2619 BaseType_t xYieldOccurred;
2620
2621 /* Must not be called with the scheduler suspended as the implementation
2622 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
2623 configASSERT( uxSchedulerSuspended == 0 );
2624
2625 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
2626 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
2627 vTaskSuspendAll();
2628
2629 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
2630 taskENTER_CRITICAL();
2631 {
2632 xPendedTicks += xTicksToCatchUp;
2633 }
2634 taskEXIT_CRITICAL();
2635 xYieldOccurred = xTaskResumeAll();
2636
2637 return xYieldOccurred;
2638}
2639/*----------------------------------------------------------*/
2640
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002641#if ( INCLUDE_xTaskAbortDelay == 1 )
2642
xiaohu.huang58292b32024-01-03 14:09:51 +08002643 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
2644 {
2645 TCB_t * pxTCB = xTask;
2646 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002647
xiaohu.huang58292b32024-01-03 14:09:51 +08002648 configASSERT( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002649
xiaohu.huang58292b32024-01-03 14:09:51 +08002650 vTaskSuspendAll();
2651 {
2652 /* A task can only be prematurely removed from the Blocked state if
2653 * it is actually in the Blocked state. */
2654 if( eTaskGetState( xTask ) == eBlocked )
2655 {
2656 xReturn = pdPASS;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002657
xiaohu.huang58292b32024-01-03 14:09:51 +08002658 /* Remove the reference to the task from the blocked list. An
2659 * interrupt won't touch the xStateListItem because the
2660 * scheduler is suspended. */
2661 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002662
xiaohu.huang58292b32024-01-03 14:09:51 +08002663 /* Is the task waiting on an event also? If so remove it from
2664 * the event list too. Interrupts can touch the event list item,
2665 * even though the scheduler is suspended, so a critical section
2666 * is used. */
2667 taskENTER_CRITICAL();
2668 {
2669 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2670 {
2671 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002672
xiaohu.huang58292b32024-01-03 14:09:51 +08002673 /* This lets the task know it was forcibly removed from the
2674 * blocked state so it should not re-evaluate its block time and
2675 * then block again. */
2676 pxTCB->ucDelayAborted = pdTRUE;
2677 }
2678 else
2679 {
2680 mtCOVERAGE_TEST_MARKER();
2681 }
2682 }
2683 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002684
xiaohu.huang58292b32024-01-03 14:09:51 +08002685 /* Place the unblocked task into the appropriate ready list. */
2686 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002687
xiaohu.huang58292b32024-01-03 14:09:51 +08002688 /* A task being unblocked cannot cause an immediate context
2689 * switch if preemption is turned off. */
2690 #if ( configUSE_PREEMPTION == 1 )
2691 {
2692 /* Preemption is on, but a context switch should only be
2693 * performed if the unblocked task has a priority that is
2694 * higher than the currently executing task. */
2695 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
2696 {
2697 /* Pend the yield to be performed when the scheduler
2698 * is unsuspended. */
2699 xYieldPending = pdTRUE;
2700 }
2701 else
2702 {
2703 mtCOVERAGE_TEST_MARKER();
2704 }
2705 }
2706 #endif /* configUSE_PREEMPTION */
2707 }
2708 else
2709 {
2710 xReturn = pdFAIL;
2711 }
2712 }
2713 ( void ) xTaskResumeAll();
2714
2715 return xReturn;
2716 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002717
2718#endif /* INCLUDE_xTaskAbortDelay */
2719/*----------------------------------------------------------*/
2720
2721BaseType_t xTaskIncrementTick( void )
2722{
xiaohu.huang58292b32024-01-03 14:09:51 +08002723 TCB_t * pxTCB;
2724 TickType_t xItemValue;
2725 BaseType_t xSwitchRequired = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002726
xiaohu.huang58292b32024-01-03 14:09:51 +08002727 /* Called by the portable layer each time a tick interrupt occurs.
2728 * Increments the tick then checks to see if the new tick value will cause any
2729 * tasks to be unblocked. */
2730 traceTASK_INCREMENT_TICK( xTickCount );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002731
xiaohu.huang58292b32024-01-03 14:09:51 +08002732 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2733 {
2734 /* Minor optimisation. The tick count cannot change in this
2735 * block. */
2736 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002737
xiaohu.huang58292b32024-01-03 14:09:51 +08002738 /* Increment the RTOS tick, switching the delayed and overflowed
2739 * delayed lists if it wraps to 0. */
2740 xTickCount = xConstTickCount;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002741
xiaohu.huang58292b32024-01-03 14:09:51 +08002742 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
2743 {
2744 taskSWITCH_DELAYED_LISTS();
2745 }
2746 else
2747 {
2748 mtCOVERAGE_TEST_MARKER();
2749 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002750
xiaohu.huang58292b32024-01-03 14:09:51 +08002751 /* See if this tick has made a timeout expire. Tasks are stored in
2752 * the queue in the order of their wake time - meaning once one task
2753 * has been found whose block time has not expired there is no need to
2754 * look any further down the list. */
2755 if( xConstTickCount >= xNextTaskUnblockTime )
2756 {
2757 for( ; ; )
2758 {
2759 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
2760 {
2761 /* The delayed list is empty. Set xNextTaskUnblockTime
2762 * to the maximum possible value so it is extremely
2763 * unlikely that the
2764 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
2765 * next time through. */
2766 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2767 break;
2768 }
2769 else
2770 {
2771 /* The delayed list is not empty, get the value of the
2772 * item at the head of the delayed list. This is the time
2773 * at which the task at the head of the delayed list must
2774 * be removed from the Blocked state. */
2775 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. */
2776 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002777
xiaohu.huang58292b32024-01-03 14:09:51 +08002778 if( xConstTickCount < xItemValue )
2779 {
2780 /* It is not time to unblock this item yet, but the
2781 * item value is the time at which the task at the head
2782 * of the blocked list must be removed from the Blocked
2783 * state - so record the item value in
2784 * xNextTaskUnblockTime. */
2785 xNextTaskUnblockTime = xItemValue;
2786 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
2787 }
2788 else
2789 {
2790 mtCOVERAGE_TEST_MARKER();
2791 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002792
xiaohu.huang58292b32024-01-03 14:09:51 +08002793 /* It is time to remove the item from the Blocked state. */
2794 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002795
xiaohu.huang58292b32024-01-03 14:09:51 +08002796 /* Is the task waiting on an event also? If so remove
2797 * it from the event list. */
2798 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2799 {
2800 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
2801 }
2802 else
2803 {
2804 mtCOVERAGE_TEST_MARKER();
2805 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002806
xiaohu.huang58292b32024-01-03 14:09:51 +08002807 /* Place the unblocked task into the appropriate ready
2808 * list. */
2809 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002810
xiaohu.huang58292b32024-01-03 14:09:51 +08002811 /* A task being unblocked cannot cause an immediate
2812 * context switch if preemption is turned off. */
2813 #if ( configUSE_PREEMPTION == 1 )
2814 {
2815 /* Preemption is on, but a context switch should
2816 * only be performed if the unblocked task's
2817 * priority is higher than the currently executing
2818 * task.
2819 * The case of equal priority tasks sharing
2820 * processing time (which happens when both
2821 * preemption and time slicing are on) is
2822 * handled below.*/
2823 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
2824 {
2825 xSwitchRequired = pdTRUE;
2826 }
2827 else
2828 {
2829 mtCOVERAGE_TEST_MARKER();
2830 }
2831 }
2832 #endif /* configUSE_PREEMPTION */
2833 }
2834 }
2835 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002836
xiaohu.huang58292b32024-01-03 14:09:51 +08002837 /* Tasks of equal priority to the currently running task will share
2838 * processing time (time slice) if preemption is on, and the application
2839 * writer has not explicitly turned time slicing off. */
2840 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
2841 {
2842 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
2843 {
2844 xSwitchRequired = pdTRUE;
2845 }
2846 else
2847 {
2848 mtCOVERAGE_TEST_MARKER();
2849 }
2850 }
2851 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002852
xiaohu.huang58292b32024-01-03 14:09:51 +08002853 #if ( configUSE_TICK_HOOK == 1 )
2854 {
2855 /* Guard against the tick hook being called when the pended tick
2856 * count is being unwound (when the scheduler is being unlocked). */
2857 if( xPendedTicks == ( TickType_t ) 0 )
2858 {
2859 vApplicationTickHook();
2860 }
2861 else
2862 {
2863 mtCOVERAGE_TEST_MARKER();
2864 }
2865 }
2866 #endif /* configUSE_TICK_HOOK */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002867
xiaohu.huang58292b32024-01-03 14:09:51 +08002868 #if ( configUSE_PREEMPTION == 1 )
2869 {
2870 if( xYieldPending != pdFALSE )
2871 {
2872 xSwitchRequired = pdTRUE;
2873 }
2874 else
2875 {
2876 mtCOVERAGE_TEST_MARKER();
2877 }
2878 }
2879 #endif /* configUSE_PREEMPTION */
2880 }
2881 else
2882 {
2883 ++xPendedTicks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002884
xiaohu.huang58292b32024-01-03 14:09:51 +08002885 /* The tick hook gets called at regular intervals, even if the
2886 * scheduler is locked. */
2887 #if ( configUSE_TICK_HOOK == 1 )
2888 {
2889 vApplicationTickHook();
2890 }
2891 #endif
2892 }
2893
2894 return xSwitchRequired;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002895}
2896/*-----------------------------------------------------------*/
2897
2898#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2899
xiaohu.huang58292b32024-01-03 14:09:51 +08002900 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
2901 TaskHookFunction_t pxHookFunction )
2902 {
2903 TCB_t * xTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002904
xiaohu.huang58292b32024-01-03 14:09:51 +08002905 /* If xTask is NULL then it is the task hook of the calling task that is
2906 * getting set. */
2907 if( xTask == NULL )
2908 {
2909 xTCB = ( TCB_t * ) pxCurrentTCB;
2910 }
2911 else
2912 {
2913 xTCB = xTask;
2914 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002915
xiaohu.huang58292b32024-01-03 14:09:51 +08002916 /* Save the hook function in the TCB. A critical section is required as
2917 * the value can be accessed from an interrupt. */
2918 taskENTER_CRITICAL();
2919 {
2920 xTCB->pxTaskTag = pxHookFunction;
2921 }
2922 taskEXIT_CRITICAL();
2923 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002924
2925#endif /* configUSE_APPLICATION_TASK_TAG */
2926/*-----------------------------------------------------------*/
2927
2928#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2929
xiaohu.huang58292b32024-01-03 14:09:51 +08002930 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
2931 {
2932 TCB_t * pxTCB;
2933 TaskHookFunction_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002934
xiaohu.huang58292b32024-01-03 14:09:51 +08002935 /* If xTask is NULL then set the calling task's hook. */
2936 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002937
xiaohu.huang58292b32024-01-03 14:09:51 +08002938 /* Save the hook function in the TCB. A critical section is required as
2939 * the value can be accessed from an interrupt. */
2940 taskENTER_CRITICAL();
2941 {
2942 xReturn = pxTCB->pxTaskTag;
2943 }
2944 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002945
xiaohu.huang58292b32024-01-03 14:09:51 +08002946 return xReturn;
2947 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002948
2949#endif /* configUSE_APPLICATION_TASK_TAG */
2950/*-----------------------------------------------------------*/
2951
2952#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2953
xiaohu.huang58292b32024-01-03 14:09:51 +08002954 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
2955 {
2956 TCB_t * pxTCB;
2957 TaskHookFunction_t xReturn;
2958 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002959
xiaohu.huang58292b32024-01-03 14:09:51 +08002960 /* If xTask is NULL then set the calling task's hook. */
2961 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002962
xiaohu.huang58292b32024-01-03 14:09:51 +08002963 /* Save the hook function in the TCB. A critical section is required as
2964 * the value can be accessed from an interrupt. */
2965 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2966 {
2967 xReturn = pxTCB->pxTaskTag;
2968 }
2969 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002970
xiaohu.huang58292b32024-01-03 14:09:51 +08002971 return xReturn;
2972 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002973
2974#endif /* configUSE_APPLICATION_TASK_TAG */
2975/*-----------------------------------------------------------*/
2976
2977#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2978
xiaohu.huang58292b32024-01-03 14:09:51 +08002979 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
2980 void * pvParameter )
2981 {
2982 TCB_t * xTCB;
2983 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002984
xiaohu.huang58292b32024-01-03 14:09:51 +08002985 /* If xTask is NULL then we are calling our own task hook. */
2986 if( xTask == NULL )
2987 {
2988 xTCB = pxCurrentTCB;
2989 }
2990 else
2991 {
2992 xTCB = xTask;
2993 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002994
xiaohu.huang58292b32024-01-03 14:09:51 +08002995 if( xTCB->pxTaskTag != NULL )
2996 {
2997 xReturn = xTCB->pxTaskTag( pvParameter );
2998 }
2999 else
3000 {
3001 xReturn = pdFAIL;
3002 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003003
xiaohu.huang58292b32024-01-03 14:09:51 +08003004 return xReturn;
3005 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003006
3007#endif /* configUSE_APPLICATION_TASK_TAG */
3008/*-----------------------------------------------------------*/
3009
3010void vTaskSwitchContext( void )
3011{
xiaohu.huang58292b32024-01-03 14:09:51 +08003012 if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
3013 {
3014 /* The scheduler is currently suspended - do not allow a context
3015 * switch. */
3016 xYieldPending = pdTRUE;
3017 }
3018 else
3019 {
3020 xYieldPending = pdFALSE;
3021 traceTASK_SWITCHED_OUT();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003022
xiaohu.huang58292b32024-01-03 14:09:51 +08003023 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3024 {
3025 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3026 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
3027 #else
3028 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3029 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003030
xiaohu.huang58292b32024-01-03 14:09:51 +08003031 /* Add the amount of time the task has been running to the
3032 * accumulated time so far. The time the task started running was
3033 * stored in ulTaskSwitchedInTime. Note that there is no overflow
3034 * protection here so count values are only valid until the timer
3035 * overflows. The guard against negative values is to protect
3036 * against suspect run time stat counter implementations - which
3037 * are provided by the application, not the kernel. */
3038 if( ulTotalRunTime > ulTaskSwitchedInTime )
3039 {
3040 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );
3041 }
3042 else
3043 {
3044 mtCOVERAGE_TEST_MARKER();
3045 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003046
xiaohu.huang58292b32024-01-03 14:09:51 +08003047 ulTaskSwitchedInTime = ulTotalRunTime;
3048 }
3049 #endif /* configGENERATE_RUN_TIME_STATS */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003050
xiaohu.huang58292b32024-01-03 14:09:51 +08003051 /* Check for stack overflow, if configured. */
3052 taskCHECK_FOR_STACK_OVERFLOW();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003053
xiaohu.huang58292b32024-01-03 14:09:51 +08003054 /* Before the currently running task is switched out, save its errno. */
3055 #if ( configUSE_POSIX_ERRNO == 1 )
3056 {
3057 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
3058 }
3059 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003060
xiaohu.huang58292b32024-01-03 14:09:51 +08003061 /* Select a new task to run using either the generic C or port
3062 * optimised asm code. */
3063 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. */
3064 traceTASK_SWITCHED_IN();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003065
xiaohu.huang58292b32024-01-03 14:09:51 +08003066 /* After the new task is switched in, update the global errno. */
3067 #if ( configUSE_POSIX_ERRNO == 1 )
3068 {
3069 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
3070 }
3071 #endif
3072
3073 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
3074 {
3075 /* Switch C-Runtime's TLS Block to point to the TLS
3076 * Block specific to this task. */
3077 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3078 }
3079 #endif
3080 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003081}
3082/*-----------------------------------------------------------*/
3083
xiaohu.huang58292b32024-01-03 14:09:51 +08003084void vTaskPlaceOnEventList( List_t * const pxEventList,
3085 const TickType_t xTicksToWait )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003086{
xiaohu.huang58292b32024-01-03 14:09:51 +08003087 configASSERT( pxEventList );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003088
xiaohu.huang58292b32024-01-03 14:09:51 +08003089 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
3090 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003091
xiaohu.huang58292b32024-01-03 14:09:51 +08003092 /* Place the event list item of the TCB in the appropriate event list.
3093 * This is placed in the list in priority order so the highest priority task
3094 * is the first to be woken by the event.
3095 *
3096 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
3097 * Normally, the xItemValue of a TCB's ListItem_t members is:
3098 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
3099 * Therefore, the event list is sorted in descending priority order.
3100 *
3101 * The queue that contains the event list is locked, preventing
3102 * simultaneous access from interrupts. */
3103 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003104
xiaohu.huang58292b32024-01-03 14:09:51 +08003105 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003106}
3107/*-----------------------------------------------------------*/
3108
xiaohu.huang58292b32024-01-03 14:09:51 +08003109void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
3110 const TickType_t xItemValue,
3111 const TickType_t xTicksToWait )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003112{
xiaohu.huang58292b32024-01-03 14:09:51 +08003113 configASSERT( pxEventList );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003114
xiaohu.huang58292b32024-01-03 14:09:51 +08003115 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3116 * the event groups implementation. */
3117 configASSERT( uxSchedulerSuspended != 0 );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003118
xiaohu.huang58292b32024-01-03 14:09:51 +08003119 /* Store the item value in the event list item. It is safe to access the
3120 * event list item here as interrupts won't access the event list item of a
3121 * task that is not in the Blocked state. */
3122 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003123
xiaohu.huang58292b32024-01-03 14:09:51 +08003124 /* Place the event list item of the TCB at the end of the appropriate event
3125 * list. It is safe to access the event list here because it is part of an
3126 * event group implementation - and interrupts don't access event groups
3127 * directly (instead they access them indirectly by pending function calls to
3128 * the task level). */
3129 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003130
xiaohu.huang58292b32024-01-03 14:09:51 +08003131 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003132}
3133/*-----------------------------------------------------------*/
3134
xiaohu.huang58292b32024-01-03 14:09:51 +08003135#if ( configUSE_TIMERS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003136
xiaohu.huang58292b32024-01-03 14:09:51 +08003137 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
3138 TickType_t xTicksToWait,
3139 const BaseType_t xWaitIndefinitely )
3140 {
3141 configASSERT( pxEventList );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003142
xiaohu.huang58292b32024-01-03 14:09:51 +08003143 /* This function should not be called by application code hence the
3144 * 'Restricted' in its name. It is not part of the public API. It is
3145 * designed for use by kernel code, and has special calling requirements -
3146 * it should be called with the scheduler suspended. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003147
3148
xiaohu.huang58292b32024-01-03 14:09:51 +08003149 /* Place the event list item of the TCB in the appropriate event list.
3150 * In this case it is assume that this is the only task that is going to
3151 * be waiting on this event list, so the faster vListInsertEnd() function
3152 * can be used in place of vListInsert. */
3153 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003154
xiaohu.huang58292b32024-01-03 14:09:51 +08003155 /* If the task should block indefinitely then set the block time to a
3156 * value that will be recognised as an indefinite delay inside the
3157 * prvAddCurrentTaskToDelayedList() function. */
3158 if( xWaitIndefinitely != pdFALSE )
3159 {
3160 xTicksToWait = portMAX_DELAY;
3161 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003162
xiaohu.huang58292b32024-01-03 14:09:51 +08003163 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
3164 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
3165 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003166
3167#endif /* configUSE_TIMERS */
3168/*-----------------------------------------------------------*/
3169
3170BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
3171{
xiaohu.huang58292b32024-01-03 14:09:51 +08003172 TCB_t * pxUnblockedTCB;
3173 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003174
xiaohu.huang58292b32024-01-03 14:09:51 +08003175 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
3176 * called from a critical section within an ISR. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003177
xiaohu.huang58292b32024-01-03 14:09:51 +08003178 /* The event list is sorted in priority order, so the first in the list can
3179 * be removed as it is known to be the highest priority. Remove the TCB from
3180 * the delayed list, and add it to the ready list.
3181 *
3182 * If an event is for a queue that is locked then this function will never
3183 * get called - the lock count on the queue will get modified instead. This
3184 * means exclusive access to the event list is guaranteed here.
3185 *
3186 * This function assumes that a check has already been made to ensure that
3187 * pxEventList is not empty. */
3188 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. */
3189 configASSERT( pxUnblockedTCB );
3190 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003191
xiaohu.huang58292b32024-01-03 14:09:51 +08003192 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3193 {
3194 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
3195 prvAddTaskToReadyList( pxUnblockedTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003196
xiaohu.huang58292b32024-01-03 14:09:51 +08003197 #if ( configUSE_TICKLESS_IDLE != 0 )
3198 {
3199 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3200 * might be set to the blocked task's time out time. If the task is
3201 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
3202 * normally left unchanged, because it is automatically reset to a new
3203 * value when the tick count equals xNextTaskUnblockTime. However if
3204 * tickless idling is used it might be more important to enter sleep mode
3205 * at the earliest possible time - so reset xNextTaskUnblockTime here to
3206 * ensure it is updated at the earliest possible time. */
3207 prvResetNextTaskUnblockTime();
3208 }
3209 #endif
3210 }
3211 else
3212 {
3213 /* The delayed and ready lists cannot be accessed, so hold this task
3214 * pending until the scheduler is resumed. */
3215 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
3216 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003217
xiaohu.huang58292b32024-01-03 14:09:51 +08003218 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
3219 {
3220 /* Return true if the task removed from the event list has a higher
3221 * priority than the calling task. This allows the calling task to know if
3222 * it should force a context switch now. */
3223 xReturn = pdTRUE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003224
xiaohu.huang58292b32024-01-03 14:09:51 +08003225 /* Mark that a yield is pending in case the user is not using the
3226 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
3227 xYieldPending = pdTRUE;
3228 }
3229 else
3230 {
3231 xReturn = pdFALSE;
3232 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003233
xiaohu.huang58292b32024-01-03 14:09:51 +08003234 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003235}
3236/*-----------------------------------------------------------*/
3237
xiaohu.huang58292b32024-01-03 14:09:51 +08003238void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
3239 const TickType_t xItemValue )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003240{
xiaohu.huang58292b32024-01-03 14:09:51 +08003241 TCB_t * pxUnblockedTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003242
xiaohu.huang58292b32024-01-03 14:09:51 +08003243 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3244 * the event flags implementation. */
3245 configASSERT( uxSchedulerSuspended != pdFALSE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003246
xiaohu.huang58292b32024-01-03 14:09:51 +08003247 /* Store the new item value in the event list. */
3248 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003249
xiaohu.huang58292b32024-01-03 14:09:51 +08003250 /* Remove the event list form the event flag. Interrupts do not access
3251 * event flags. */
3252 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. */
3253 configASSERT( pxUnblockedTCB );
3254 listREMOVE_ITEM( pxEventListItem );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003255
xiaohu.huang58292b32024-01-03 14:09:51 +08003256 #if ( configUSE_TICKLESS_IDLE != 0 )
3257 {
3258 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3259 * might be set to the blocked task's time out time. If the task is
3260 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
3261 * normally left unchanged, because it is automatically reset to a new
3262 * value when the tick count equals xNextTaskUnblockTime. However if
3263 * tickless idling is used it might be more important to enter sleep mode
3264 * at the earliest possible time - so reset xNextTaskUnblockTime here to
3265 * ensure it is updated at the earliest possible time. */
3266 prvResetNextTaskUnblockTime();
3267 }
3268 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003269
xiaohu.huang58292b32024-01-03 14:09:51 +08003270 /* Remove the task from the delayed list and add it to the ready list. The
3271 * scheduler is suspended so interrupts will not be accessing the ready
3272 * lists. */
3273 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
3274 prvAddTaskToReadyList( pxUnblockedTCB );
3275
3276 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
3277 {
3278 /* The unblocked task has a priority above that of the calling task, so
3279 * a context switch is required. This function is called with the
3280 * scheduler suspended so xYieldPending is set so the context switch
3281 * occurs immediately that the scheduler is resumed (unsuspended). */
3282 xYieldPending = pdTRUE;
3283 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003284}
3285/*-----------------------------------------------------------*/
3286
3287void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
3288{
xiaohu.huang58292b32024-01-03 14:09:51 +08003289 configASSERT( pxTimeOut );
3290 taskENTER_CRITICAL();
3291 {
3292 pxTimeOut->xOverflowCount = xNumOfOverflows;
3293 pxTimeOut->xTimeOnEntering = xTickCount;
3294 }
3295 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003296}
3297/*-----------------------------------------------------------*/
3298
3299void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
3300{
xiaohu.huang58292b32024-01-03 14:09:51 +08003301 /* For internal use only as it does not use a critical section. */
3302 pxTimeOut->xOverflowCount = xNumOfOverflows;
3303 pxTimeOut->xTimeOnEntering = xTickCount;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003304}
3305/*-----------------------------------------------------------*/
3306
xiaohu.huang58292b32024-01-03 14:09:51 +08003307BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
3308 TickType_t * const pxTicksToWait )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003309{
xiaohu.huang58292b32024-01-03 14:09:51 +08003310 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003311
xiaohu.huang58292b32024-01-03 14:09:51 +08003312 configASSERT( pxTimeOut );
3313 configASSERT( pxTicksToWait );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003314
xiaohu.huang58292b32024-01-03 14:09:51 +08003315 taskENTER_CRITICAL();
3316 {
3317 /* Minor optimisation. The tick count cannot change in this block. */
3318 const TickType_t xConstTickCount = xTickCount;
3319 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003320
xiaohu.huang58292b32024-01-03 14:09:51 +08003321 #if ( INCLUDE_xTaskAbortDelay == 1 )
3322 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
3323 {
3324 /* The delay was aborted, which is not the same as a time out,
3325 * but has the same result. */
3326 pxCurrentTCB->ucDelayAborted = pdFALSE;
3327 xReturn = pdTRUE;
3328 }
3329 else
3330 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003331
xiaohu.huang58292b32024-01-03 14:09:51 +08003332 #if ( INCLUDE_vTaskSuspend == 1 )
3333 if( *pxTicksToWait == portMAX_DELAY )
3334 {
3335 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
3336 * specified is the maximum block time then the task should block
3337 * indefinitely, and therefore never time out. */
3338 xReturn = pdFALSE;
3339 }
3340 else
3341 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003342
xiaohu.huang58292b32024-01-03 14:09:51 +08003343 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
3344 {
3345 /* The tick count is greater than the time at which
3346 * vTaskSetTimeout() was called, but has also overflowed since
3347 * vTaskSetTimeOut() was called. It must have wrapped all the way
3348 * around and gone past again. This passed since vTaskSetTimeout()
3349 * was called. */
3350 xReturn = pdTRUE;
3351 *pxTicksToWait = ( TickType_t ) 0;
3352 }
3353 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
3354 {
3355 /* Not a genuine timeout. Adjust parameters for time remaining. */
3356 *pxTicksToWait -= xElapsedTime;
3357 vTaskInternalSetTimeOutState( pxTimeOut );
3358 xReturn = pdFALSE;
3359 }
3360 else
3361 {
3362 *pxTicksToWait = ( TickType_t ) 0;
3363 xReturn = pdTRUE;
3364 }
3365 }
3366 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003367
xiaohu.huang58292b32024-01-03 14:09:51 +08003368 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003369}
3370/*-----------------------------------------------------------*/
3371
3372void vTaskMissedYield( void )
3373{
xiaohu.huang58292b32024-01-03 14:09:51 +08003374 xYieldPending = pdTRUE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003375}
3376/*-----------------------------------------------------------*/
3377
3378#if ( configUSE_TRACE_FACILITY == 1 )
3379
xiaohu.huang58292b32024-01-03 14:09:51 +08003380 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
3381 {
3382 UBaseType_t uxReturn;
3383 TCB_t const * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003384
xiaohu.huang58292b32024-01-03 14:09:51 +08003385 if( xTask != NULL )
3386 {
3387 pxTCB = xTask;
3388 uxReturn = pxTCB->uxTaskNumber;
3389 }
3390 else
3391 {
3392 uxReturn = 0U;
3393 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003394
xiaohu.huang58292b32024-01-03 14:09:51 +08003395 return uxReturn;
3396 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003397
3398#endif /* configUSE_TRACE_FACILITY */
3399/*-----------------------------------------------------------*/
3400
3401#if ( configUSE_TRACE_FACILITY == 1 )
3402
xiaohu.huang58292b32024-01-03 14:09:51 +08003403 void vTaskSetTaskNumber( TaskHandle_t xTask,
3404 const UBaseType_t uxHandle )
3405 {
3406 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003407
xiaohu.huang58292b32024-01-03 14:09:51 +08003408 if( xTask != NULL )
3409 {
3410 pxTCB = xTask;
3411 pxTCB->uxTaskNumber = uxHandle;
3412 }
3413 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003414
3415#endif /* configUSE_TRACE_FACILITY */
3416
3417/*
3418 * -----------------------------------------------------------
3419 * The Idle task.
3420 * ----------------------------------------------------------
3421 *
3422 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
3423 * language extensions. The equivalent prototype for this function is:
3424 *
3425 * void prvIdleTask( void *pvParameters );
3426 *
3427 */
3428static portTASK_FUNCTION( prvIdleTask, pvParameters )
3429{
xiaohu.huang58292b32024-01-03 14:09:51 +08003430 /* Stop warnings. */
3431 ( void ) pvParameters;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003432
xiaohu.huang58292b32024-01-03 14:09:51 +08003433 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
3434 * SCHEDULER IS STARTED. **/
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003435
xiaohu.huang58292b32024-01-03 14:09:51 +08003436 /* In case a task that has a secure context deletes itself, in which case
3437 * the idle task is responsible for deleting the task's secure context, if
3438 * any. */
3439 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003440
xiaohu.huang58292b32024-01-03 14:09:51 +08003441 for( ; ; )
3442 {
3443 /* See if any tasks have deleted themselves - if so then the idle task
3444 * is responsible for freeing the deleted task's TCB and stack. */
3445 prvCheckTasksWaitingTermination();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003446
xiaohu.huang58292b32024-01-03 14:09:51 +08003447 #if ( configUSE_PREEMPTION == 0 )
3448 {
3449 /* If we are not using preemption we keep forcing a task switch to
3450 * see if any other task has become available. If we are using
3451 * preemption we don't need to do this as any task becoming available
3452 * will automatically get the processor anyway. */
3453 taskYIELD();
3454 }
3455 #endif /* configUSE_PREEMPTION */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003456
xiaohu.huang58292b32024-01-03 14:09:51 +08003457 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
3458 {
3459 /* When using preemption tasks of equal priority will be
3460 * timesliced. If a task that is sharing the idle priority is ready
3461 * to run then the idle task should yield before the end of the
3462 * timeslice.
3463 *
3464 * A critical region is not required here as we are just reading from
3465 * the list, and an occasional incorrect value will not matter. If
3466 * the ready list at the idle priority contains more than one task
3467 * then a task other than the idle task is ready to execute. */
3468 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) 1 )
3469 {
3470 taskYIELD();
3471 }
3472 else
3473 {
3474 mtCOVERAGE_TEST_MARKER();
3475 }
3476 }
3477 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003478
xiaohu.huang58292b32024-01-03 14:09:51 +08003479 #if ( configUSE_IDLE_HOOK == 1 )
3480 {
3481 extern void vApplicationIdleHook( void );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003482
xiaohu.huang58292b32024-01-03 14:09:51 +08003483 /* Call the user defined function from within the idle task. This
3484 * allows the application designer to add background functionality
3485 * without the overhead of a separate task.
3486 * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
3487 * CALL A FUNCTION THAT MIGHT BLOCK. */
3488 vApplicationIdleHook();
3489 }
3490 #endif /* configUSE_IDLE_HOOK */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003491
xiaohu.huang58292b32024-01-03 14:09:51 +08003492 /* This conditional compilation should use inequality to 0, not equality
3493 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
3494 * user defined low power mode implementations require
3495 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
3496 #if ( configUSE_TICKLESS_IDLE != 0 )
3497 {
3498 TickType_t xExpectedIdleTime;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003499
xiaohu.huang58292b32024-01-03 14:09:51 +08003500 /* It is not desirable to suspend then resume the scheduler on
3501 * each iteration of the idle task. Therefore, a preliminary
3502 * test of the expected idle time is performed without the
3503 * scheduler suspended. The result here is not necessarily
3504 * valid. */
3505 xExpectedIdleTime = prvGetExpectedIdleTime();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003506
xiaohu.huang58292b32024-01-03 14:09:51 +08003507 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
3508 {
3509 vTaskSuspendAll();
3510 {
3511 /* Now the scheduler is suspended, the expected idle
3512 * time can be sampled again, and this time its value can
3513 * be used. */
3514 configASSERT( xNextTaskUnblockTime >= xTickCount );
3515 xExpectedIdleTime = prvGetExpectedIdleTime();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003516
xiaohu.huang58292b32024-01-03 14:09:51 +08003517 /* Define the following macro to set xExpectedIdleTime to 0
3518 * if the application does not want
3519 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
3520 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003521
xiaohu.huang58292b32024-01-03 14:09:51 +08003522 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
3523 {
3524 traceLOW_POWER_IDLE_BEGIN();
3525 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
3526 traceLOW_POWER_IDLE_END();
3527 }
3528 else
3529 {
3530 mtCOVERAGE_TEST_MARKER();
3531 }
3532 }
3533 ( void ) xTaskResumeAll();
3534 }
3535 else
3536 {
3537 mtCOVERAGE_TEST_MARKER();
3538 }
3539 }
3540 #endif /* configUSE_TICKLESS_IDLE */
3541 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003542}
3543/*-----------------------------------------------------------*/
3544
xiaohu.huang58292b32024-01-03 14:09:51 +08003545#if ( configUSE_TICKLESS_IDLE != 0 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003546
xiaohu.huang58292b32024-01-03 14:09:51 +08003547 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
3548 {
3549 #if ( INCLUDE_vTaskSuspend == 1 )
3550 /* The idle task exists in addition to the application tasks. */
3551 const UBaseType_t uxNonApplicationTasks = 1;
3552 #endif /* INCLUDE_vTaskSuspend */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003553
xiaohu.huang58292b32024-01-03 14:09:51 +08003554 eSleepModeStatus eReturn = eStandardSleep;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003555
xiaohu.huang58292b32024-01-03 14:09:51 +08003556 /* This function must be called from a critical section. */
3557
3558 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
3559 {
3560 /* A task was made ready while the scheduler was suspended. */
3561 eReturn = eAbortSleep;
3562 }
3563 else if( xYieldPending != pdFALSE )
3564 {
3565 /* A yield was pended while the scheduler was suspended. */
3566 eReturn = eAbortSleep;
3567 }
3568 else if( xPendedTicks != 0 )
3569 {
3570 /* A tick interrupt has already occurred but was held pending
3571 * because the scheduler is suspended. */
3572 eReturn = eAbortSleep;
3573 }
3574
3575 #if ( INCLUDE_vTaskSuspend == 1 )
3576 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
3577 {
3578 /* If all the tasks are in the suspended list (which might mean they
3579 * have an infinite block time rather than actually being suspended)
3580 * then it is safe to turn all clocks off and just wait for external
3581 * interrupts. */
3582 eReturn = eNoTasksWaitingTimeout;
3583 }
3584 #endif /* INCLUDE_vTaskSuspend */
3585 else
3586 {
3587 mtCOVERAGE_TEST_MARKER();
3588 }
3589
3590 return eReturn;
3591 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003592
3593#endif /* configUSE_TICKLESS_IDLE */
3594/*-----------------------------------------------------------*/
3595
3596#if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
3597
xiaohu.huang58292b32024-01-03 14:09:51 +08003598 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
3599 BaseType_t xIndex,
3600 void * pvValue )
3601 {
3602 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003603
xiaohu.huang58292b32024-01-03 14:09:51 +08003604 if( ( xIndex >= 0 ) &&
3605 ( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
3606 {
3607 pxTCB = prvGetTCBFromHandle( xTaskToSet );
3608 configASSERT( pxTCB != NULL );
3609 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
3610 }
3611 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003612
3613#endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
3614/*-----------------------------------------------------------*/
3615
3616#if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
3617
xiaohu.huang58292b32024-01-03 14:09:51 +08003618 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
3619 BaseType_t xIndex )
3620 {
3621 void * pvReturn = NULL;
3622 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003623
xiaohu.huang58292b32024-01-03 14:09:51 +08003624 if( ( xIndex >= 0 ) &&
3625 ( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
3626 {
3627 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3628 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
3629 }
3630 else
3631 {
3632 pvReturn = NULL;
3633 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003634
xiaohu.huang58292b32024-01-03 14:09:51 +08003635 return pvReturn;
3636 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003637
3638#endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
3639/*-----------------------------------------------------------*/
3640
3641#if ( portUSING_MPU_WRAPPERS == 1 )
3642
xiaohu.huang58292b32024-01-03 14:09:51 +08003643 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
3644 const MemoryRegion_t * const xRegions )
3645 {
3646 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003647
xiaohu.huang58292b32024-01-03 14:09:51 +08003648 /* If null is passed in here then we are modifying the MPU settings of
3649 * the calling task. */
3650 pxTCB = prvGetTCBFromHandle( xTaskToModify );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003651
xiaohu.huang58292b32024-01-03 14:09:51 +08003652 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
3653 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003654
3655#endif /* portUSING_MPU_WRAPPERS */
3656/*-----------------------------------------------------------*/
3657
3658static void prvInitialiseTaskLists( void )
3659{
xiaohu.huang58292b32024-01-03 14:09:51 +08003660 UBaseType_t uxPriority;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003661
xiaohu.huang58292b32024-01-03 14:09:51 +08003662 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
3663 {
3664 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
3665 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003666
xiaohu.huang58292b32024-01-03 14:09:51 +08003667 vListInitialise( &xDelayedTaskList1 );
3668 vListInitialise( &xDelayedTaskList2 );
3669 vListInitialise( &xPendingReadyList );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003670
xiaohu.huang58292b32024-01-03 14:09:51 +08003671 #if ( INCLUDE_vTaskDelete == 1 )
3672 {
3673 vListInitialise( &xTasksWaitingTermination );
3674 }
3675 #endif /* INCLUDE_vTaskDelete */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003676
xiaohu.huang58292b32024-01-03 14:09:51 +08003677 #if ( INCLUDE_vTaskSuspend == 1 )
3678 {
3679 vListInitialise( &xSuspendedTaskList );
3680 }
3681 #endif /* INCLUDE_vTaskSuspend */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003682
xiaohu.huang58292b32024-01-03 14:09:51 +08003683 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
3684 * using list2. */
3685 pxDelayedTaskList = &xDelayedTaskList1;
3686 pxOverflowDelayedTaskList = &xDelayedTaskList2;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003687}
3688/*-----------------------------------------------------------*/
3689
3690static void prvCheckTasksWaitingTermination( void )
3691{
xiaohu.huang58292b32024-01-03 14:09:51 +08003692 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003693
xiaohu.huang58292b32024-01-03 14:09:51 +08003694 #if ( INCLUDE_vTaskDelete == 1 )
3695 {
3696 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003697
xiaohu.huang58292b32024-01-03 14:09:51 +08003698 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
3699 * being called too often in the idle task. */
3700 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
3701 {
3702 taskENTER_CRITICAL();
3703 {
3704 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. */
3705 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3706 --uxCurrentNumberOfTasks;
3707 --uxDeletedTasksWaitingCleanUp;
3708 }
3709 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003710
xiaohu.huang58292b32024-01-03 14:09:51 +08003711 prvDeleteTCB( pxTCB );
3712 }
3713 }
3714 #endif /* INCLUDE_vTaskDelete */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003715}
3716/*-----------------------------------------------------------*/
3717
xiaohu.huang58292b32024-01-03 14:09:51 +08003718#if ( configUSE_TRACE_FACILITY == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003719
xiaohu.huang58292b32024-01-03 14:09:51 +08003720 void vTaskGetInfo( TaskHandle_t xTask,
3721 TaskStatus_t * pxTaskStatus,
3722 BaseType_t xGetFreeStackSpace,
3723 eTaskState eState )
3724 {
3725 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003726
xiaohu.huang58292b32024-01-03 14:09:51 +08003727 /* xTask is NULL then get the state of the calling task. */
3728 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003729
xiaohu.huang58292b32024-01-03 14:09:51 +08003730 pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
3731 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
3732 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
3733 pxTaskStatus->pxStackBase = pxTCB->pxStack;
3734 #if ( ( portSTACK_GROWTH > 0 ) && ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
3735 pxTaskStatus->pxTopOfStack = pxTCB->pxTopOfStack;
3736 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
3737 #endif
3738 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003739
xiaohu.huang58292b32024-01-03 14:09:51 +08003740 #if ( configUSE_MUTEXES == 1 )
3741 {
3742 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
3743 }
3744 #else
3745 {
3746 pxTaskStatus->uxBasePriority = 0;
3747 }
3748 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003749
xiaohu.huang58292b32024-01-03 14:09:51 +08003750 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3751 {
3752 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
3753 }
3754 #else
3755 {
3756 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
3757 }
3758 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003759
xiaohu.huang58292b32024-01-03 14:09:51 +08003760 /* Obtaining the task state is a little fiddly, so is only done if the
3761 * value of eState passed into this function is eInvalid - otherwise the
3762 * state is just set to whatever is passed in. */
3763 if( eState != eInvalid )
3764 {
3765 if( pxTCB == pxCurrentTCB )
3766 {
3767 pxTaskStatus->eCurrentState = eRunning;
3768 }
3769 else
3770 {
3771 pxTaskStatus->eCurrentState = eState;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003772
xiaohu.huang58292b32024-01-03 14:09:51 +08003773 #if ( INCLUDE_vTaskSuspend == 1 )
3774 {
3775 /* If the task is in the suspended list then there is a
3776 * chance it is actually just blocked indefinitely - so really
3777 * it should be reported as being in the Blocked state. */
3778 if( eState == eSuspended )
3779 {
3780 vTaskSuspendAll();
3781 {
3782 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3783 {
3784 pxTaskStatus->eCurrentState = eBlocked;
3785 }
3786 }
3787 ( void ) xTaskResumeAll();
3788 }
3789 }
3790 #endif /* INCLUDE_vTaskSuspend */
3791 }
3792 }
3793 else
3794 {
3795 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
3796 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003797
xiaohu.huang58292b32024-01-03 14:09:51 +08003798 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
3799 * parameter is provided to allow it to be skipped. */
3800 if( xGetFreeStackSpace != pdFALSE )
3801 {
3802 #if ( portSTACK_GROWTH > 0 )
3803 {
3804 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
3805 }
3806 #else
3807 {
3808 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
3809 }
3810 #endif
3811 }
3812 else
3813 {
3814 pxTaskStatus->usStackHighWaterMark = 0;
3815 }
3816 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003817
3818#endif /* configUSE_TRACE_FACILITY */
3819/*-----------------------------------------------------------*/
3820
3821#if ( configUSE_TRACE_FACILITY == 1 )
3822
xiaohu.huang58292b32024-01-03 14:09:51 +08003823 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
3824 List_t * pxList,
3825 eTaskState eState )
3826 {
3827 configLIST_VOLATILE TCB_t * pxNextTCB;
3828 configLIST_VOLATILE TCB_t * pxFirstTCB;
3829 UBaseType_t uxTask = 0;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003830
xiaohu.huang58292b32024-01-03 14:09:51 +08003831 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3832 {
3833 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 +08003834
xiaohu.huang58292b32024-01-03 14:09:51 +08003835 /* Populate an TaskStatus_t structure within the
3836 * pxTaskStatusArray array for each task that is referenced from
3837 * pxList. See the definition of TaskStatus_t in task.h for the
3838 * meaning of each TaskStatus_t structure member. */
3839 do
3840 {
3841 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. */
3842 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
3843 uxTask++;
3844 } while( pxNextTCB != pxFirstTCB );
3845 }
3846 else
3847 {
3848 mtCOVERAGE_TEST_MARKER();
3849 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003850
xiaohu.huang58292b32024-01-03 14:09:51 +08003851 return uxTask;
3852 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003853
3854#endif /* configUSE_TRACE_FACILITY */
3855/*-----------------------------------------------------------*/
3856
3857#if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
3858
xiaohu.huang58292b32024-01-03 14:09:51 +08003859 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
3860 {
3861 uint32_t ulCount = 0U;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003862
xiaohu.huang58292b32024-01-03 14:09:51 +08003863 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
3864 {
3865 pucStackByte -= portSTACK_GROWTH;
3866 ulCount++;
3867 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003868
xiaohu.huang58292b32024-01-03 14:09:51 +08003869 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003870
xiaohu.huang58292b32024-01-03 14:09:51 +08003871 return ( configSTACK_DEPTH_TYPE ) ulCount;
3872 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003873
3874#endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
3875/*-----------------------------------------------------------*/
3876
3877#if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
3878
xiaohu.huang58292b32024-01-03 14:09:51 +08003879/* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
3880 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
3881 * user to determine the return type. It gets around the problem of the value
3882 * overflowing on 8-bit types without breaking backward compatibility for
3883 * applications that expect an 8-bit return type. */
3884 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
3885 {
3886 TCB_t * pxTCB;
3887 uint8_t * pucEndOfStack;
3888 configSTACK_DEPTH_TYPE uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003889
xiaohu.huang58292b32024-01-03 14:09:51 +08003890 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
3891 * the same except for their return type. Using configSTACK_DEPTH_TYPE
3892 * allows the user to determine the return type. It gets around the
3893 * problem of the value overflowing on 8-bit types without breaking
3894 * backward compatibility for applications that expect an 8-bit return
3895 * type. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003896
xiaohu.huang58292b32024-01-03 14:09:51 +08003897 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003898
xiaohu.huang58292b32024-01-03 14:09:51 +08003899 #if portSTACK_GROWTH < 0
3900 {
3901 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
3902 }
3903 #else
3904 {
3905 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
3906 }
3907 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003908
xiaohu.huang58292b32024-01-03 14:09:51 +08003909 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003910
xiaohu.huang58292b32024-01-03 14:09:51 +08003911 return uxReturn;
3912 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003913
3914#endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
3915/*-----------------------------------------------------------*/
3916
3917#if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
3918
xiaohu.huang58292b32024-01-03 14:09:51 +08003919 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
3920 {
3921 TCB_t * pxTCB;
3922 uint8_t * pucEndOfStack;
3923 UBaseType_t uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003924
xiaohu.huang58292b32024-01-03 14:09:51 +08003925 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003926
xiaohu.huang58292b32024-01-03 14:09:51 +08003927 #if portSTACK_GROWTH < 0
3928 {
3929 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
3930 }
3931 #else
3932 {
3933 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
3934 }
3935 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003936
xiaohu.huang58292b32024-01-03 14:09:51 +08003937 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003938
xiaohu.huang58292b32024-01-03 14:09:51 +08003939 return uxReturn;
3940 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003941
3942#endif /* INCLUDE_uxTaskGetStackHighWaterMark */
3943/*-----------------------------------------------------------*/
3944
3945#if ( INCLUDE_vTaskDelete == 1 )
3946
xiaohu.huang58292b32024-01-03 14:09:51 +08003947 static void prvDeleteTCB( TCB_t * pxTCB )
3948 {
3949 /* This call is required specifically for the TriCore port. It must be
3950 * above the vPortFree() calls. The call is also used by ports/demos that
3951 * want to allocate and clean RAM statically. */
3952 portCLEAN_UP_TCB( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003953
xiaohu.huang58292b32024-01-03 14:09:51 +08003954 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
3955 {
3956 /* Free up the memory allocated for the task's TLS Block. */
3957 configDEINIT_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3958 }
3959 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003960
xiaohu.huang58292b32024-01-03 14:09:51 +08003961 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
3962 {
3963 /* The task can only have been allocated dynamically - free both
3964 * the stack and TCB. */
3965 vPortFreeStack( pxTCB->pxStack );
3966 vPortFree( pxTCB );
3967 }
3968 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
3969 {
3970 /* The task could have been allocated statically or dynamically, so
3971 * check what was statically allocated before trying to free the
3972 * memory. */
3973 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
3974 {
3975 /* Both the stack and TCB were allocated dynamically, so both
3976 * must be freed. */
3977 vPortFreeStack( pxTCB->pxStack );
3978 vPortFree( pxTCB );
3979 }
3980 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
3981 {
3982 /* Only the stack was statically allocated, so the TCB is the
3983 * only memory that must be freed. */
3984 vPortFree( pxTCB );
3985 }
3986 else
3987 {
3988 /* Neither the stack nor the TCB were allocated dynamically, so
3989 * nothing needs to be freed. */
3990 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
3991 mtCOVERAGE_TEST_MARKER();
3992 }
3993 }
3994 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
3995 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003996
3997#endif /* INCLUDE_vTaskDelete */
3998/*-----------------------------------------------------------*/
3999
4000static void prvResetNextTaskUnblockTime( void )
4001{
xiaohu.huang58292b32024-01-03 14:09:51 +08004002 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4003 {
4004 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
4005 * the maximum possible value so it is extremely unlikely that the
4006 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
4007 * there is an item in the delayed list. */
4008 xNextTaskUnblockTime = portMAX_DELAY;
4009 }
4010 else
4011 {
4012 /* The new current delayed list is not empty, get the value of
4013 * the item at the head of the delayed list. This is the time at
4014 * which the task at the head of the delayed list should be removed
4015 * from the Blocked state. */
4016 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
4017 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004018}
4019/*-----------------------------------------------------------*/
4020
4021#if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4022
xiaohu.huang58292b32024-01-03 14:09:51 +08004023 TaskHandle_t xTaskGetCurrentTaskHandle( void )
4024 {
4025 TaskHandle_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004026
xiaohu.huang58292b32024-01-03 14:09:51 +08004027 /* A critical section is not required as this is not called from
4028 * an interrupt and the current TCB will always be the same for any
4029 * individual execution thread. */
4030 xReturn = pxCurrentTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004031
xiaohu.huang58292b32024-01-03 14:09:51 +08004032 return xReturn;
4033 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004034
4035#endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
4036/*-----------------------------------------------------------*/
4037
4038#if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
4039
xiaohu.huang58292b32024-01-03 14:09:51 +08004040 BaseType_t xTaskGetSchedulerState( void )
4041 {
4042 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004043
xiaohu.huang58292b32024-01-03 14:09:51 +08004044 if( xSchedulerRunning == pdFALSE )
4045 {
4046 xReturn = taskSCHEDULER_NOT_STARTED;
4047 }
4048 else
4049 {
4050 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4051 {
4052 xReturn = taskSCHEDULER_RUNNING;
4053 }
4054 else
4055 {
4056 xReturn = taskSCHEDULER_SUSPENDED;
4057 }
4058 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004059
xiaohu.huang58292b32024-01-03 14:09:51 +08004060 return xReturn;
4061 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004062
4063#endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
4064/*-----------------------------------------------------------*/
4065
4066#if ( configUSE_MUTEXES == 1 )
4067
xiaohu.huang58292b32024-01-03 14:09:51 +08004068 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
4069 {
4070 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
4071 BaseType_t xReturn = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004072
xiaohu.huang58292b32024-01-03 14:09:51 +08004073 /* If the mutex was given back by an interrupt while the queue was
4074 * locked then the mutex holder might now be NULL. _RB_ Is this still
4075 * needed as interrupts can no longer use mutexes? */
4076 if( pxMutexHolder != NULL )
4077 {
4078 /* If the holder of the mutex has a priority below the priority of
4079 * the task attempting to obtain the mutex then it will temporarily
4080 * inherit the priority of the task attempting to obtain the mutex. */
4081 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
4082 {
4083 /* Adjust the mutex holder state to account for its new
4084 * priority. Only reset the event list item value if the value is
4085 * not being used for anything else. */
4086 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4087 {
4088 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. */
4089 }
4090 else
4091 {
4092 mtCOVERAGE_TEST_MARKER();
4093 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004094
xiaohu.huang58292b32024-01-03 14:09:51 +08004095 /* If the task being modified is in the ready state it will need
4096 * to be moved into a new list. */
4097 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
4098 {
4099 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4100 {
4101 /* It is known that the task is in its ready list so
4102 * there is no need to check again and the port level
4103 * reset macro can be called directly. */
4104 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
4105 }
4106 else
4107 {
4108 mtCOVERAGE_TEST_MARKER();
4109 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004110
xiaohu.huang58292b32024-01-03 14:09:51 +08004111 /* Inherit the priority before being moved into the new list. */
4112 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4113 prvAddTaskToReadyList( pxMutexHolderTCB );
4114 }
4115 else
4116 {
4117 /* Just inherit the priority. */
4118 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4119 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004120
xiaohu.huang58292b32024-01-03 14:09:51 +08004121 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004122
xiaohu.huang58292b32024-01-03 14:09:51 +08004123 /* Inheritance occurred. */
4124 xReturn = pdTRUE;
4125 }
4126 else
4127 {
4128 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
4129 {
4130 /* The base priority of the mutex holder is lower than the
4131 * priority of the task attempting to take the mutex, but the
4132 * current priority of the mutex holder is not lower than the
4133 * priority of the task attempting to take the mutex.
4134 * Therefore the mutex holder must have already inherited a
4135 * priority, but inheritance would have occurred if that had
4136 * not been the case. */
4137 xReturn = pdTRUE;
4138 }
4139 else
4140 {
4141 mtCOVERAGE_TEST_MARKER();
4142 }
4143 }
4144 }
4145 else
4146 {
4147 mtCOVERAGE_TEST_MARKER();
4148 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004149
xiaohu.huang58292b32024-01-03 14:09:51 +08004150 return xReturn;
4151 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004152
4153#endif /* configUSE_MUTEXES */
4154/*-----------------------------------------------------------*/
4155
4156#if ( configUSE_MUTEXES == 1 )
4157
xiaohu.huang58292b32024-01-03 14:09:51 +08004158 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
4159 {
4160 TCB_t * const pxTCB = pxMutexHolder;
4161 BaseType_t xReturn = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004162
xiaohu.huang58292b32024-01-03 14:09:51 +08004163 if( pxMutexHolder != NULL )
4164 {
4165 /* A task can only have an inherited priority if it holds the mutex.
4166 * If the mutex is held by a task then it cannot be given from an
4167 * interrupt, and if a mutex is given by the holding task then it must
4168 * be the running state task. */
4169 configASSERT( pxTCB == pxCurrentTCB );
4170 configASSERT( pxTCB->uxMutexesHeld );
4171 ( pxTCB->uxMutexesHeld )--;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004172
xiaohu.huang58292b32024-01-03 14:09:51 +08004173 /* Has the holder of the mutex inherited the priority of another
4174 * task? */
4175 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
4176 {
4177 /* Only disinherit if no other mutexes are held. */
4178 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
4179 {
4180 /* A task can only have an inherited priority if it holds
4181 * the mutex. If the mutex is held by a task then it cannot be
4182 * given from an interrupt, and if a mutex is given by the
4183 * holding task then it must be the running state task. Remove
4184 * the holding task from the ready list. */
4185 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4186 {
4187 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
4188 }
4189 else
4190 {
4191 mtCOVERAGE_TEST_MARKER();
4192 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004193
xiaohu.huang58292b32024-01-03 14:09:51 +08004194 /* Disinherit the priority before adding the task into the
4195 * new ready list. */
4196 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
4197 pxTCB->uxPriority = pxTCB->uxBasePriority;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004198
xiaohu.huang58292b32024-01-03 14:09:51 +08004199 /* Reset the event list item value. It cannot be in use for
4200 * any other purpose if this task is running, and it must be
4201 * running to give back the mutex. */
4202 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. */
4203 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004204
xiaohu.huang58292b32024-01-03 14:09:51 +08004205 /* Return true to indicate that a context switch is required.
4206 * This is only actually required in the corner case whereby
4207 * multiple mutexes were held and the mutexes were given back
4208 * in an order different to that in which they were taken.
4209 * If a context switch did not occur when the first mutex was
4210 * returned, even if a task was waiting on it, then a context
4211 * switch should occur when the last mutex is returned whether
4212 * a task is waiting on it or not. */
4213 xReturn = pdTRUE;
4214 }
4215 else
4216 {
4217 mtCOVERAGE_TEST_MARKER();
4218 }
4219 }
4220 else
4221 {
4222 mtCOVERAGE_TEST_MARKER();
4223 }
4224 }
4225 else
4226 {
4227 mtCOVERAGE_TEST_MARKER();
4228 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004229
xiaohu.huang58292b32024-01-03 14:09:51 +08004230 return xReturn;
4231 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004232
4233#endif /* configUSE_MUTEXES */
4234/*-----------------------------------------------------------*/
4235
4236#if ( configUSE_MUTEXES == 1 )
4237
xiaohu.huang58292b32024-01-03 14:09:51 +08004238 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
4239 UBaseType_t uxHighestPriorityWaitingTask )
4240 {
4241 TCB_t * const pxTCB = pxMutexHolder;
4242 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
4243 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004244
xiaohu.huang58292b32024-01-03 14:09:51 +08004245 if( pxMutexHolder != NULL )
4246 {
4247 /* If pxMutexHolder is not NULL then the holder must hold at least
4248 * one mutex. */
4249 configASSERT( pxTCB->uxMutexesHeld );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004250
xiaohu.huang58292b32024-01-03 14:09:51 +08004251 /* Determine the priority to which the priority of the task that
4252 * holds the mutex should be set. This will be the greater of the
4253 * holding task's base priority and the priority of the highest
4254 * priority task that is waiting to obtain the mutex. */
4255 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
4256 {
4257 uxPriorityToUse = uxHighestPriorityWaitingTask;
4258 }
4259 else
4260 {
4261 uxPriorityToUse = pxTCB->uxBasePriority;
4262 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004263
xiaohu.huang58292b32024-01-03 14:09:51 +08004264 /* Does the priority need to change? */
4265 if( pxTCB->uxPriority != uxPriorityToUse )
4266 {
4267 /* Only disinherit if no other mutexes are held. This is a
4268 * simplification in the priority inheritance implementation. If
4269 * the task that holds the mutex is also holding other mutexes then
4270 * the other mutexes may have caused the priority inheritance. */
4271 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
4272 {
4273 /* If a task has timed out because it already holds the
4274 * mutex it was trying to obtain then it cannot of inherited
4275 * its own priority. */
4276 configASSERT( pxTCB != pxCurrentTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004277
xiaohu.huang58292b32024-01-03 14:09:51 +08004278 /* Disinherit the priority, remembering the previous
4279 * priority to facilitate determining the subject task's
4280 * state. */
4281 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
4282 uxPriorityUsedOnEntry = pxTCB->uxPriority;
4283 pxTCB->uxPriority = uxPriorityToUse;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004284
xiaohu.huang58292b32024-01-03 14:09:51 +08004285 /* Only reset the event list item value if the value is not
4286 * being used for anything else. */
4287 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4288 {
4289 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. */
4290 }
4291 else
4292 {
4293 mtCOVERAGE_TEST_MARKER();
4294 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004295
xiaohu.huang58292b32024-01-03 14:09:51 +08004296 /* If the running task is not the task that holds the mutex
4297 * then the task that holds the mutex could be in either the
4298 * Ready, Blocked or Suspended states. Only remove the task
4299 * from its current state list if it is in the Ready state as
4300 * the task's priority is going to change and there is one
4301 * Ready list per priority. */
4302 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
4303 {
4304 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4305 {
4306 /* It is known that the task is in its ready list so
4307 * there is no need to check again and the port level
4308 * reset macro can be called directly. */
4309 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
4310 }
4311 else
4312 {
4313 mtCOVERAGE_TEST_MARKER();
4314 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004315
xiaohu.huang58292b32024-01-03 14:09:51 +08004316 prvAddTaskToReadyList( pxTCB );
4317 }
4318 else
4319 {
4320 mtCOVERAGE_TEST_MARKER();
4321 }
4322 }
4323 else
4324 {
4325 mtCOVERAGE_TEST_MARKER();
4326 }
4327 }
4328 else
4329 {
4330 mtCOVERAGE_TEST_MARKER();
4331 }
4332 }
4333 else
4334 {
4335 mtCOVERAGE_TEST_MARKER();
4336 }
4337 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004338
4339#endif /* configUSE_MUTEXES */
4340/*-----------------------------------------------------------*/
4341
4342#if ( portCRITICAL_NESTING_IN_TCB == 1 )
4343
xiaohu.huang58292b32024-01-03 14:09:51 +08004344 void vTaskEnterCritical( void )
4345 {
4346 portDISABLE_INTERRUPTS();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004347
xiaohu.huang58292b32024-01-03 14:09:51 +08004348 if( xSchedulerRunning != pdFALSE )
4349 {
4350 ( pxCurrentTCB->uxCriticalNesting )++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004351
xiaohu.huang58292b32024-01-03 14:09:51 +08004352 /* This is not the interrupt safe version of the enter critical
4353 * function so assert() if it is being called from an interrupt
4354 * context. Only API functions that end in "FromISR" can be used in an
4355 * interrupt. Only assert if the critical nesting count is 1 to
4356 * protect against recursive calls if the assert function also uses a
4357 * critical section. */
4358 if( pxCurrentTCB->uxCriticalNesting == 1 )
4359 {
4360 portASSERT_IF_IN_ISR();
4361 }
4362 }
4363 else
4364 {
4365 mtCOVERAGE_TEST_MARKER();
4366 }
4367 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004368
4369#endif /* portCRITICAL_NESTING_IN_TCB */
4370/*-----------------------------------------------------------*/
4371
4372#if ( portCRITICAL_NESTING_IN_TCB == 1 )
4373
xiaohu.huang58292b32024-01-03 14:09:51 +08004374 void vTaskExitCritical( void )
4375 {
4376 if( xSchedulerRunning != pdFALSE )
4377 {
4378 if( pxCurrentTCB->uxCriticalNesting > 0U )
4379 {
4380 ( pxCurrentTCB->uxCriticalNesting )--;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004381
xiaohu.huang58292b32024-01-03 14:09:51 +08004382 if( pxCurrentTCB->uxCriticalNesting == 0U )
4383 {
4384 portENABLE_INTERRUPTS();
4385 }
4386 else
4387 {
4388 mtCOVERAGE_TEST_MARKER();
4389 }
4390 }
4391 else
4392 {
4393 mtCOVERAGE_TEST_MARKER();
4394 }
4395 }
4396 else
4397 {
4398 mtCOVERAGE_TEST_MARKER();
4399 }
4400 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004401
4402#endif /* portCRITICAL_NESTING_IN_TCB */
4403/*-----------------------------------------------------------*/
4404
xiaohu.huang58292b32024-01-03 14:09:51 +08004405#if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
4406
4407 static char * prvWriteNameToBuffer( char * pcBuffer,
4408 const char * pcTaskName )
4409 {
4410 size_t x;
4411
4412 /* Start by copying the entire string. */
4413 strcpy( pcBuffer, pcTaskName );
4414
4415 /* Pad the end of the string with spaces to ensure columns line up when
4416 * printed out. */
4417 for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
4418 {
4419 pcBuffer[ x ] = ' ';
4420 }
4421
4422 /* Terminate. */
4423 pcBuffer[ x ] = ( char ) 0x00;
4424
4425 /* Return the new end of string. */
4426 return &( pcBuffer[ x ] );
4427 }
4428
4429#endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
4430/*-----------------------------------------------------------*/
4431
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004432#if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
4433
xiaohu.huang58292b32024-01-03 14:09:51 +08004434 void vTaskList( char * pcWriteBuffer )
4435 {
4436 TaskStatus_t * pxTaskStatusArray;
4437 UBaseType_t uxArraySize, x;
4438 char cStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004439
xiaohu.huang58292b32024-01-03 14:09:51 +08004440 /*
4441 * PLEASE NOTE:
4442 *
4443 * This function is provided for convenience only, and is used by many
4444 * of the demo applications. Do not consider it to be part of the
4445 * scheduler.
4446 *
4447 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
4448 * uxTaskGetSystemState() output into a human readable table that
4449 * displays task: names, states, priority, stack usage and task number.
4450 * Stack usage specified as the number of unused StackType_t words stack can hold
4451 * on top of stack - not the number of bytes.
4452 *
4453 * vTaskList() has a dependency on the sprintf() C library function that
4454 * might bloat the code size, use a lot of stack, and provide different
4455 * results on different platforms. An alternative, tiny, third party,
4456 * and limited functionality implementation of sprintf() is provided in
4457 * many of the FreeRTOS/Demo sub-directories in a file called
4458 * printf-stdarg.c (note printf-stdarg.c does not provide a full
4459 * snprintf() implementation!).
4460 *
4461 * It is recommended that production systems call uxTaskGetSystemState()
4462 * directly to get access to raw stats data, rather than indirectly
4463 * through a call to vTaskList().
4464 */
Xiaohu.Huangdbadd052022-01-26 17:30:36 +08004465
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004466
xiaohu.huang58292b32024-01-03 14:09:51 +08004467 /* Make sure the write buffer does not contain a string. */
4468 *pcWriteBuffer = ( char ) 0x00;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004469
xiaohu.huang58292b32024-01-03 14:09:51 +08004470 /* Take a snapshot of the number of tasks in case it changes while this
4471 * function is executing. */
4472 uxArraySize = uxCurrentNumberOfTasks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004473
xiaohu.huang58292b32024-01-03 14:09:51 +08004474 /* Allocate an array index for each task. NOTE! if
4475 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
4476 * equate to NULL. */
4477 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 +08004478
xiaohu.huang58292b32024-01-03 14:09:51 +08004479 if( pxTaskStatusArray != NULL )
4480 {
4481 /* Generate the (binary) data. */
4482 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004483
xiaohu.huang58292b32024-01-03 14:09:51 +08004484 /* Create a human readable table from the binary data. */
4485 for( x = 0; x < uxArraySize; x++ )
4486 {
4487 switch( pxTaskStatusArray[ x ].eCurrentState )
4488 {
4489 case eRunning:
4490 cStatus = tskRUNNING_CHAR;
4491 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004492
xiaohu.huang58292b32024-01-03 14:09:51 +08004493 case eReady:
4494 cStatus = tskREADY_CHAR;
4495 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004496
xiaohu.huang58292b32024-01-03 14:09:51 +08004497 case eBlocked:
4498 cStatus = tskBLOCKED_CHAR;
4499 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004500
xiaohu.huang58292b32024-01-03 14:09:51 +08004501 case eSuspended:
4502 cStatus = tskSUSPENDED_CHAR;
4503 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004504
xiaohu.huang58292b32024-01-03 14:09:51 +08004505 case eDeleted:
4506 cStatus = tskDELETED_CHAR;
4507 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004508
xiaohu.huang58292b32024-01-03 14:09:51 +08004509 case eInvalid: /* Fall through. */
4510 default: /* Should not get here, but it is included
4511 * to prevent static checking errors. */
4512 cStatus = ( char ) 0x00;
4513 break;
4514 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004515
xiaohu.huang58292b32024-01-03 14:09:51 +08004516 /* Write the task name to the string, padding with spaces so it
4517 * can be printed in tabular form more easily. */
4518 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004519
xiaohu.huang58292b32024-01-03 14:09:51 +08004520 /* Write the rest of the string. */
4521 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. */
4522 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. */
4523 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004524
xiaohu.huang58292b32024-01-03 14:09:51 +08004525 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
4526 * is 0 then vPortFree() will be #defined to nothing. */
4527 vPortFree( pxTaskStatusArray );
4528 }
4529 else
4530 {
4531 mtCOVERAGE_TEST_MARKER();
4532 }
4533 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004534
xiaohu.huang58292b32024-01-03 14:09:51 +08004535#endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004536/*----------------------------------------------------------*/
4537
xiaohu.huang58292b32024-01-03 14:09:51 +08004538#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004539
xiaohu.huang58292b32024-01-03 14:09:51 +08004540 void vTaskGetRunTimeStats( char * pcWriteBuffer )
4541 {
4542 TaskStatus_t * pxTaskStatusArray;
4543 UBaseType_t uxArraySize, x;
4544 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004545
xiaohu.huang58292b32024-01-03 14:09:51 +08004546 /*
4547 * PLEASE NOTE:
4548 *
4549 * This function is provided for convenience only, and is used by many
4550 * of the demo applications. Do not consider it to be part of the
4551 * scheduler.
4552 *
4553 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
4554 * of the uxTaskGetSystemState() output into a human readable table that
4555 * displays the amount of time each task has spent in the Running state
4556 * in both absolute and percentage terms.
4557 *
4558 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
4559 * function that might bloat the code size, use a lot of stack, and
4560 * provide different results on different platforms. An alternative,
4561 * tiny, third party, and limited functionality implementation of
4562 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
4563 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
4564 * a full snprintf() implementation!).
4565 *
4566 * It is recommended that production systems call uxTaskGetSystemState()
4567 * directly to get access to raw stats data, rather than indirectly
4568 * through a call to vTaskGetRunTimeStats().
4569 */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004570
xiaohu.huang58292b32024-01-03 14:09:51 +08004571 /* Make sure the write buffer does not contain a string. */
4572 *pcWriteBuffer = ( char ) 0x00;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004573
xiaohu.huang58292b32024-01-03 14:09:51 +08004574 /* Take a snapshot of the number of tasks in case it changes while this
4575 * function is executing. */
4576 uxArraySize = uxCurrentNumberOfTasks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004577
xiaohu.huang58292b32024-01-03 14:09:51 +08004578 /* Allocate an array index for each task. NOTE! If
4579 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
4580 * equate to NULL. */
4581 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 +08004582
xiaohu.huang58292b32024-01-03 14:09:51 +08004583 if( pxTaskStatusArray != NULL )
4584 {
4585 /* Generate the (binary) data. */
4586 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004587
xiaohu.huang58292b32024-01-03 14:09:51 +08004588 /* For percentage calculations. */
4589 ulTotalTime /= 100UL;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004590
xiaohu.huang58292b32024-01-03 14:09:51 +08004591 /* Avoid divide by zero errors. */
4592 if( ulTotalTime > 0UL )
4593 {
4594 /* Create a human readable table from the binary data. */
4595 for( x = 0; x < uxArraySize; x++ )
4596 {
4597 /* What percentage of the total run time has the task used?
4598 * This will always be rounded down to the nearest integer.
4599 * ulTotalRunTime has already been divided by 100. */
4600 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004601
xiaohu.huang58292b32024-01-03 14:09:51 +08004602 /* Write the task name to the string, padding with
4603 * spaces so it can be printed in tabular form more
4604 * easily. */
4605 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004606
xiaohu.huang58292b32024-01-03 14:09:51 +08004607 if( ulStatsAsPercentage > 0UL )
4608 {
4609 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
4610 {
4611 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
4612 }
4613 #else
4614 {
4615 /* sizeof( int ) == sizeof( long ) so a smaller
4616 * printf() library can be used. */
4617 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. */
4618 }
4619 #endif
4620 }
4621 else
4622 {
4623 /* If the percentage is zero here then the task has
4624 * consumed less than 1% of the total run time. */
4625 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
4626 {
4627 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
4628 }
4629 #else
4630 {
4631 /* sizeof( int ) == sizeof( long ) so a smaller
4632 * printf() library can be used. */
4633 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. */
4634 }
4635 #endif
4636 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004637
xiaohu.huang58292b32024-01-03 14:09:51 +08004638 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. */
4639 }
4640 }
4641 else
4642 {
4643 mtCOVERAGE_TEST_MARKER();
4644 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004645
xiaohu.huang58292b32024-01-03 14:09:51 +08004646 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
4647 * is 0 then vPortFree() will be #defined to nothing. */
4648 vPortFree( pxTaskStatusArray );
4649 }
4650 else
4651 {
4652 mtCOVERAGE_TEST_MARKER();
4653 }
4654 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004655
xiaohu.huang58292b32024-01-03 14:09:51 +08004656#endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004657/*-----------------------------------------------------------*/
4658
4659TickType_t uxTaskResetEventItemValue( void )
4660{
xiaohu.huang58292b32024-01-03 14:09:51 +08004661 TickType_t uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004662
xiaohu.huang58292b32024-01-03 14:09:51 +08004663 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004664
xiaohu.huang58292b32024-01-03 14:09:51 +08004665 /* Reset the event list item to its normal value - so it can be used with
4666 * queues and semaphores. */
4667 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 +08004668
xiaohu.huang58292b32024-01-03 14:09:51 +08004669 return uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004670}
4671/*-----------------------------------------------------------*/
4672
4673#if ( configUSE_MUTEXES == 1 )
4674
xiaohu.huang58292b32024-01-03 14:09:51 +08004675 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
4676 {
4677 /* If xSemaphoreCreateMutex() is called before any tasks have been created
4678 * then pxCurrentTCB will be NULL. */
4679 if( pxCurrentTCB != NULL )
4680 {
4681 ( pxCurrentTCB->uxMutexesHeld )++;
4682 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004683
xiaohu.huang58292b32024-01-03 14:09:51 +08004684 return pxCurrentTCB;
4685 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004686
4687#endif /* configUSE_MUTEXES */
4688/*-----------------------------------------------------------*/
4689
xiaohu.huang58292b32024-01-03 14:09:51 +08004690#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004691
xiaohu.huang58292b32024-01-03 14:09:51 +08004692 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
4693 BaseType_t xClearCountOnExit,
4694 TickType_t xTicksToWait )
4695 {
4696 uint32_t ulReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004697
xiaohu.huang58292b32024-01-03 14:09:51 +08004698 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004699
xiaohu.huang58292b32024-01-03 14:09:51 +08004700 taskENTER_CRITICAL();
4701 {
4702 /* Only block if the notification count is not already non-zero. */
4703 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
4704 {
4705 /* Mark this task as waiting for a notification. */
4706 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004707
xiaohu.huang58292b32024-01-03 14:09:51 +08004708 if( xTicksToWait > ( TickType_t ) 0 )
4709 {
4710 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4711 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004712
xiaohu.huang58292b32024-01-03 14:09:51 +08004713 /* All ports are written to allow a yield in a critical
4714 * section (some will yield immediately, others wait until the
4715 * critical section exits) - but it is not something that
4716 * application code should ever do. */
4717 portYIELD_WITHIN_API();
4718 }
4719 else
4720 {
4721 mtCOVERAGE_TEST_MARKER();
4722 }
4723 }
4724 else
4725 {
4726 mtCOVERAGE_TEST_MARKER();
4727 }
4728 }
4729 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004730
xiaohu.huang58292b32024-01-03 14:09:51 +08004731 taskENTER_CRITICAL();
4732 {
4733 traceTASK_NOTIFY_TAKE( uxIndexToWait );
4734 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004735
xiaohu.huang58292b32024-01-03 14:09:51 +08004736 if( ulReturn != 0UL )
4737 {
4738 if( xClearCountOnExit != pdFALSE )
4739 {
4740 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
4741 }
4742 else
4743 {
4744 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
4745 }
4746 }
4747 else
4748 {
4749 mtCOVERAGE_TEST_MARKER();
4750 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004751
xiaohu.huang58292b32024-01-03 14:09:51 +08004752 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
4753 }
4754 taskEXIT_CRITICAL();
4755
4756 return ulReturn;
4757 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004758
4759#endif /* configUSE_TASK_NOTIFICATIONS */
4760/*-----------------------------------------------------------*/
4761
xiaohu.huang58292b32024-01-03 14:09:51 +08004762#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004763
xiaohu.huang58292b32024-01-03 14:09:51 +08004764 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
4765 uint32_t ulBitsToClearOnEntry,
4766 uint32_t ulBitsToClearOnExit,
4767 uint32_t * pulNotificationValue,
4768 TickType_t xTicksToWait )
4769 {
4770 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004771
xiaohu.huang58292b32024-01-03 14:09:51 +08004772 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004773
xiaohu.huang58292b32024-01-03 14:09:51 +08004774 taskENTER_CRITICAL();
4775 {
4776 /* Only block if a notification is not already pending. */
4777 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
4778 {
4779 /* Clear bits in the task's notification value as bits may get
4780 * set by the notifying task or interrupt. This can be used to
4781 * clear the value to zero. */
4782 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004783
xiaohu.huang58292b32024-01-03 14:09:51 +08004784 /* Mark this task as waiting for a notification. */
4785 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004786
xiaohu.huang58292b32024-01-03 14:09:51 +08004787 if( xTicksToWait > ( TickType_t ) 0 )
4788 {
4789 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4790 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004791
xiaohu.huang58292b32024-01-03 14:09:51 +08004792 /* All ports are written to allow a yield in a critical
4793 * section (some will yield immediately, others wait until the
4794 * critical section exits) - but it is not something that
4795 * application code should ever do. */
4796 portYIELD_WITHIN_API();
4797 }
4798 else
4799 {
4800 mtCOVERAGE_TEST_MARKER();
4801 }
4802 }
4803 else
4804 {
4805 mtCOVERAGE_TEST_MARKER();
4806 }
4807 }
4808 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004809
xiaohu.huang58292b32024-01-03 14:09:51 +08004810 taskENTER_CRITICAL();
4811 {
4812 traceTASK_NOTIFY_WAIT( uxIndexToWait );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004813
xiaohu.huang58292b32024-01-03 14:09:51 +08004814 if( pulNotificationValue != NULL )
4815 {
4816 /* Output the current notification value, which may or may not
4817 * have changed. */
4818 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
4819 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004820
xiaohu.huang58292b32024-01-03 14:09:51 +08004821 /* If ucNotifyValue is set then either the task never entered the
4822 * blocked state (because a notification was already pending) or the
4823 * task unblocked because of a notification. Otherwise the task
4824 * unblocked because of a timeout. */
4825 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
4826 {
4827 /* A notification was not received. */
4828 xReturn = pdFALSE;
4829 }
4830 else
4831 {
4832 /* A notification was already pending or a notification was
4833 * received while the task was waiting. */
4834 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
4835 xReturn = pdTRUE;
4836 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004837
xiaohu.huang58292b32024-01-03 14:09:51 +08004838 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
4839 }
4840 taskEXIT_CRITICAL();
4841
4842 return xReturn;
4843 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004844
4845#endif /* configUSE_TASK_NOTIFICATIONS */
4846/*-----------------------------------------------------------*/
4847
xiaohu.huang58292b32024-01-03 14:09:51 +08004848#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004849
xiaohu.huang58292b32024-01-03 14:09:51 +08004850 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
4851 UBaseType_t uxIndexToNotify,
4852 uint32_t ulValue,
4853 eNotifyAction eAction,
4854 uint32_t * pulPreviousNotificationValue )
4855 {
4856 TCB_t * pxTCB;
4857 BaseType_t xReturn = pdPASS;
4858 uint8_t ucOriginalNotifyState;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004859
xiaohu.huang58292b32024-01-03 14:09:51 +08004860 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
4861 configASSERT( xTaskToNotify );
4862 pxTCB = xTaskToNotify;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004863
xiaohu.huang58292b32024-01-03 14:09:51 +08004864 taskENTER_CRITICAL();
4865 {
4866 if( pulPreviousNotificationValue != NULL )
4867 {
4868 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
4869 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004870
xiaohu.huang58292b32024-01-03 14:09:51 +08004871 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004872
xiaohu.huang58292b32024-01-03 14:09:51 +08004873 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004874
xiaohu.huang58292b32024-01-03 14:09:51 +08004875 switch( eAction )
4876 {
4877 case eSetBits:
4878 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
4879 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004880
xiaohu.huang58292b32024-01-03 14:09:51 +08004881 case eIncrement:
4882 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
4883 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004884
xiaohu.huang58292b32024-01-03 14:09:51 +08004885 case eSetValueWithOverwrite:
4886 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
4887 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004888
xiaohu.huang58292b32024-01-03 14:09:51 +08004889 case eSetValueWithoutOverwrite:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004890
xiaohu.huang58292b32024-01-03 14:09:51 +08004891 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
4892 {
4893 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
4894 }
4895 else
4896 {
4897 /* The value could not be written to the task. */
4898 xReturn = pdFAIL;
4899 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004900
xiaohu.huang58292b32024-01-03 14:09:51 +08004901 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004902
xiaohu.huang58292b32024-01-03 14:09:51 +08004903 case eNoAction:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004904
xiaohu.huang58292b32024-01-03 14:09:51 +08004905 /* The task is being notified without its notify value being
4906 * updated. */
4907 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004908
xiaohu.huang58292b32024-01-03 14:09:51 +08004909 default:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004910
xiaohu.huang58292b32024-01-03 14:09:51 +08004911 /* Should not get here if all enums are handled.
4912 * Artificially force an assert by testing a value the
4913 * compiler can't assume is const. */
4914 configASSERT( xTickCount == ( TickType_t ) 0 );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004915
xiaohu.huang58292b32024-01-03 14:09:51 +08004916 break;
4917 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004918
xiaohu.huang58292b32024-01-03 14:09:51 +08004919 traceTASK_NOTIFY( uxIndexToNotify );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004920
xiaohu.huang58292b32024-01-03 14:09:51 +08004921 /* If the task is in the blocked state specifically to wait for a
4922 * notification then unblock it now. */
4923 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
4924 {
4925 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4926 prvAddTaskToReadyList( pxTCB );
4927
4928 /* The task should not have been on an event list. */
4929 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
4930
4931 #if ( configUSE_TICKLESS_IDLE != 0 )
4932 {
4933 /* If a task is blocked waiting for a notification then
4934 * xNextTaskUnblockTime might be set to the blocked task's time
4935 * out time. If the task is unblocked for a reason other than
4936 * a timeout xNextTaskUnblockTime is normally left unchanged,
4937 * because it will automatically get reset to a new value when
4938 * the tick count equals xNextTaskUnblockTime. However if
4939 * tickless idling is used it might be more important to enter
4940 * sleep mode at the earliest possible time - so reset
4941 * xNextTaskUnblockTime here to ensure it is updated at the
4942 * earliest possible time. */
4943 prvResetNextTaskUnblockTime();
4944 }
4945 #endif
4946
4947 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4948 {
4949 /* The notified task has a priority above the currently
4950 * executing task so a yield is required. */
4951 taskYIELD_IF_USING_PREEMPTION();
4952 }
4953 else
4954 {
4955 mtCOVERAGE_TEST_MARKER();
4956 }
4957 }
4958 else
4959 {
4960 mtCOVERAGE_TEST_MARKER();
4961 }
4962 }
4963 taskEXIT_CRITICAL();
4964
4965 return xReturn;
4966 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004967
4968#endif /* configUSE_TASK_NOTIFICATIONS */
4969/*-----------------------------------------------------------*/
4970
xiaohu.huang58292b32024-01-03 14:09:51 +08004971#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004972
xiaohu.huang58292b32024-01-03 14:09:51 +08004973 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
4974 UBaseType_t uxIndexToNotify,
4975 uint32_t ulValue,
4976 eNotifyAction eAction,
4977 uint32_t * pulPreviousNotificationValue,
4978 BaseType_t * pxHigherPriorityTaskWoken )
4979 {
4980 TCB_t * pxTCB;
4981 uint8_t ucOriginalNotifyState;
4982 BaseType_t xReturn = pdPASS;
4983 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004984
xiaohu.huang58292b32024-01-03 14:09:51 +08004985 configASSERT( xTaskToNotify );
4986 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004987
xiaohu.huang58292b32024-01-03 14:09:51 +08004988 /* RTOS ports that support interrupt nesting have the concept of a
4989 * maximum system call (or maximum API call) interrupt priority.
4990 * Interrupts that are above the maximum system call priority are keep
4991 * permanently enabled, even when the RTOS kernel is in a critical section,
4992 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
4993 * is defined in FreeRTOSConfig.h then
4994 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
4995 * failure if a FreeRTOS API function is called from an interrupt that has
4996 * been assigned a priority above the configured maximum system call
4997 * priority. Only FreeRTOS functions that end in FromISR can be called
4998 * from interrupts that have been assigned a priority at or (logically)
4999 * below the maximum system call interrupt priority. FreeRTOS maintains a
5000 * separate interrupt safe API to ensure interrupt entry is as fast and as
5001 * simple as possible. More information (albeit Cortex-M specific) is
5002 * provided on the following link:
5003 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5004 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005005
xiaohu.huang58292b32024-01-03 14:09:51 +08005006 pxTCB = xTaskToNotify;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005007
xiaohu.huang58292b32024-01-03 14:09:51 +08005008 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5009 {
5010 if( pulPreviousNotificationValue != NULL )
5011 {
5012 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5013 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005014
xiaohu.huang58292b32024-01-03 14:09:51 +08005015 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5016 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005017
xiaohu.huang58292b32024-01-03 14:09:51 +08005018 switch( eAction )
5019 {
5020 case eSetBits:
5021 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5022 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005023
xiaohu.huang58292b32024-01-03 14:09:51 +08005024 case eIncrement:
5025 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5026 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005027
xiaohu.huang58292b32024-01-03 14:09:51 +08005028 case eSetValueWithOverwrite:
5029 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5030 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005031
xiaohu.huang58292b32024-01-03 14:09:51 +08005032 case eSetValueWithoutOverwrite:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005033
xiaohu.huang58292b32024-01-03 14:09:51 +08005034 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5035 {
5036 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5037 }
5038 else
5039 {
5040 /* The value could not be written to the task. */
5041 xReturn = pdFAIL;
5042 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005043
xiaohu.huang58292b32024-01-03 14:09:51 +08005044 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005045
xiaohu.huang58292b32024-01-03 14:09:51 +08005046 case eNoAction:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005047
xiaohu.huang58292b32024-01-03 14:09:51 +08005048 /* The task is being notified without its notify value being
5049 * updated. */
5050 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005051
xiaohu.huang58292b32024-01-03 14:09:51 +08005052 default:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005053
xiaohu.huang58292b32024-01-03 14:09:51 +08005054 /* Should not get here if all enums are handled.
5055 * Artificially force an assert by testing a value the
5056 * compiler can't assume is const. */
5057 configASSERT( xTickCount == ( TickType_t ) 0 );
5058 break;
5059 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005060
xiaohu.huang58292b32024-01-03 14:09:51 +08005061 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005062
xiaohu.huang58292b32024-01-03 14:09:51 +08005063 /* If the task is in the blocked state specifically to wait for a
5064 * notification then unblock it now. */
5065 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5066 {
5067 /* The task should not have been on an event list. */
5068 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5069
5070 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5071 {
5072 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
5073 prvAddTaskToReadyList( pxTCB );
5074 }
5075 else
5076 {
5077 /* The delayed and ready lists cannot be accessed, so hold
5078 * this task pending until the scheduler is resumed. */
5079 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
5080 }
5081
5082 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
5083 {
5084 /* The notified task has a priority above the currently
5085 * executing task so a yield is required. */
5086 if( pxHigherPriorityTaskWoken != NULL )
5087 {
5088 *pxHigherPriorityTaskWoken = pdTRUE;
5089 }
5090
5091 /* Mark that a yield is pending in case the user is not
5092 * using the "xHigherPriorityTaskWoken" parameter to an ISR
5093 * safe FreeRTOS function. */
5094 xYieldPending = pdTRUE;
5095 }
5096 else
5097 {
5098 mtCOVERAGE_TEST_MARKER();
5099 }
5100 }
5101 }
5102 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
5103
5104 return xReturn;
5105 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005106
5107#endif /* configUSE_TASK_NOTIFICATIONS */
5108/*-----------------------------------------------------------*/
5109
xiaohu.huang58292b32024-01-03 14:09:51 +08005110#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005111
xiaohu.huang58292b32024-01-03 14:09:51 +08005112 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
5113 UBaseType_t uxIndexToNotify,
5114 BaseType_t * pxHigherPriorityTaskWoken )
5115 {
5116 TCB_t * pxTCB;
5117 uint8_t ucOriginalNotifyState;
5118 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005119
xiaohu.huang58292b32024-01-03 14:09:51 +08005120 configASSERT( xTaskToNotify );
5121 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005122
xiaohu.huang58292b32024-01-03 14:09:51 +08005123 /* RTOS ports that support interrupt nesting have the concept of a
5124 * maximum system call (or maximum API call) interrupt priority.
5125 * Interrupts that are above the maximum system call priority are keep
5126 * permanently enabled, even when the RTOS kernel is in a critical section,
5127 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
5128 * is defined in FreeRTOSConfig.h then
5129 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5130 * failure if a FreeRTOS API function is called from an interrupt that has
5131 * been assigned a priority above the configured maximum system call
5132 * priority. Only FreeRTOS functions that end in FromISR can be called
5133 * from interrupts that have been assigned a priority at or (logically)
5134 * below the maximum system call interrupt priority. FreeRTOS maintains a
5135 * separate interrupt safe API to ensure interrupt entry is as fast and as
5136 * simple as possible. More information (albeit Cortex-M specific) is
5137 * provided on the following link:
5138 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5139 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005140
xiaohu.huang58292b32024-01-03 14:09:51 +08005141 pxTCB = xTaskToNotify;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005142
xiaohu.huang58292b32024-01-03 14:09:51 +08005143 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5144 {
5145 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5146 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005147
xiaohu.huang58292b32024-01-03 14:09:51 +08005148 /* 'Giving' is equivalent to incrementing a count in a counting
5149 * semaphore. */
5150 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005151
xiaohu.huang58292b32024-01-03 14:09:51 +08005152 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005153
xiaohu.huang58292b32024-01-03 14:09:51 +08005154 /* If the task is in the blocked state specifically to wait for a
5155 * notification then unblock it now. */
5156 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5157 {
5158 /* The task should not have been on an event list. */
5159 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005160
xiaohu.huang58292b32024-01-03 14:09:51 +08005161 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5162 {
5163 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
5164 prvAddTaskToReadyList( pxTCB );
5165 }
5166 else
5167 {
5168 /* The delayed and ready lists cannot be accessed, so hold
5169 * this task pending until the scheduler is resumed. */
5170 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
5171 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005172
xiaohu.huang58292b32024-01-03 14:09:51 +08005173 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
5174 {
5175 /* The notified task has a priority above the currently
5176 * executing task so a yield is required. */
5177 if( pxHigherPriorityTaskWoken != NULL )
5178 {
5179 *pxHigherPriorityTaskWoken = pdTRUE;
5180 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005181
xiaohu.huang58292b32024-01-03 14:09:51 +08005182 /* Mark that a yield is pending in case the user is not
5183 * using the "xHigherPriorityTaskWoken" parameter in an ISR
5184 * safe FreeRTOS function. */
5185 xYieldPending = pdTRUE;
5186 }
5187 else
5188 {
5189 mtCOVERAGE_TEST_MARKER();
5190 }
5191 }
5192 }
5193 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
5194 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005195
5196#endif /* configUSE_TASK_NOTIFICATIONS */
5197/*-----------------------------------------------------------*/
5198
xiaohu.huang58292b32024-01-03 14:09:51 +08005199#if ( configUSE_TASK_NOTIFICATIONS == 1 )
5200
5201 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
5202 UBaseType_t uxIndexToClear )
5203 {
5204 TCB_t * pxTCB;
5205 BaseType_t xReturn;
5206
5207 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5208
5209 /* If null is passed in here then it is the calling task that is having
5210 * its notification state cleared. */
5211 pxTCB = prvGetTCBFromHandle( xTask );
5212
5213 taskENTER_CRITICAL();
5214 {
5215 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
5216 {
5217 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
5218 xReturn = pdPASS;
5219 }
5220 else
5221 {
5222 xReturn = pdFAIL;
5223 }
5224 }
5225 taskEXIT_CRITICAL();
5226
5227 return xReturn;
5228 }
5229
5230#endif /* configUSE_TASK_NOTIFICATIONS */
5231/*-----------------------------------------------------------*/
5232
5233#if ( configUSE_TASK_NOTIFICATIONS == 1 )
5234
5235 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
5236 UBaseType_t uxIndexToClear,
5237 uint32_t ulBitsToClear )
5238 {
5239 TCB_t * pxTCB;
5240 uint32_t ulReturn;
5241
5242 /* If null is passed in here then it is the calling task that is having
5243 * its notification state cleared. */
5244 pxTCB = prvGetTCBFromHandle( xTask );
5245
5246 taskENTER_CRITICAL();
5247 {
5248 /* Return the notification as it was before the bits were cleared,
5249 * then clear the bit mask. */
5250 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
5251 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
5252 }
5253 taskEXIT_CRITICAL();
5254
5255 return ulReturn;
5256 }
5257
5258#endif /* configUSE_TASK_NOTIFICATIONS */
5259/*-----------------------------------------------------------*/
5260
5261#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
5262
5263 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
5264 {
5265 return xIdleTaskHandle->ulRunTimeCounter;
5266 }
5267
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005268#endif
5269/*-----------------------------------------------------------*/
5270
xiaohu.huang58292b32024-01-03 14:09:51 +08005271#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
5272
5273 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
5274 {
5275 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
5276
5277 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE();
5278
5279 /* For percentage calculations. */
5280 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
5281
5282 /* Avoid divide by zero errors. */
5283 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
5284 {
5285 ulReturn = xIdleTaskHandle->ulRunTimeCounter / ulTotalTime;
5286 }
5287 else
5288 {
5289 ulReturn = 0;
5290 }
5291
5292 return ulReturn;
5293 }
5294
5295#endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
5296/*-----------------------------------------------------------*/
5297
5298static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
5299 const BaseType_t xCanBlockIndefinitely )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005300{
xiaohu.huang58292b32024-01-03 14:09:51 +08005301 TickType_t xTimeToWake;
5302 const TickType_t xConstTickCount = xTickCount;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005303
xiaohu.huang58292b32024-01-03 14:09:51 +08005304 #if ( INCLUDE_xTaskAbortDelay == 1 )
5305 {
5306 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
5307 * reset to pdFALSE so it can be detected as having been set to pdTRUE
5308 * when the task leaves the Blocked state. */
5309 pxCurrentTCB->ucDelayAborted = pdFALSE;
5310 }
5311 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005312
xiaohu.huang58292b32024-01-03 14:09:51 +08005313 /* Remove the task from the ready list before adding it to the blocked list
5314 * as the same list item is used for both lists. */
5315 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5316 {
5317 /* The current task must be in a ready list, so there is no need to
5318 * check, and the port reset macro can be called directly. */
5319 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. */
5320 }
5321 else
5322 {
5323 mtCOVERAGE_TEST_MARKER();
5324 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005325
xiaohu.huang58292b32024-01-03 14:09:51 +08005326 #if ( INCLUDE_vTaskSuspend == 1 )
5327 {
5328 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
5329 {
5330 /* Add the task to the suspended task list instead of a delayed task
5331 * list to ensure it is not woken by a timing event. It will block
5332 * indefinitely. */
5333 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
5334 }
5335 else
5336 {
5337 /* Calculate the time at which the task should be woken if the event
5338 * does not occur. This may overflow but this doesn't matter, the
5339 * kernel will manage it correctly. */
5340 xTimeToWake = xConstTickCount + xTicksToWait;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005341
xiaohu.huang58292b32024-01-03 14:09:51 +08005342 /* The list item will be inserted in wake time order. */
5343 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005344
xiaohu.huang58292b32024-01-03 14:09:51 +08005345 if( xTimeToWake < xConstTickCount )
5346 {
5347 /* Wake time has overflowed. Place this item in the overflow
5348 * list. */
5349 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5350 }
5351 else
5352 {
5353 /* The wake time has not overflowed, so the current block list
5354 * is used. */
5355 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005356
xiaohu.huang58292b32024-01-03 14:09:51 +08005357 /* If the task entering the blocked state was placed at the
5358 * head of the list of blocked tasks then xNextTaskUnblockTime
5359 * needs to be updated too. */
5360 if( xTimeToWake < xNextTaskUnblockTime )
5361 {
5362 xNextTaskUnblockTime = xTimeToWake;
5363 }
5364 else
5365 {
5366 mtCOVERAGE_TEST_MARKER();
5367 }
5368 }
5369 }
5370 }
5371 #else /* INCLUDE_vTaskSuspend */
5372 {
5373 /* Calculate the time at which the task should be woken if the event
5374 * does not occur. This may overflow but this doesn't matter, the kernel
5375 * will manage it correctly. */
5376 xTimeToWake = xConstTickCount + xTicksToWait;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005377
xiaohu.huang58292b32024-01-03 14:09:51 +08005378 /* The list item will be inserted in wake time order. */
5379 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005380
xiaohu.huang58292b32024-01-03 14:09:51 +08005381 if( xTimeToWake < xConstTickCount )
5382 {
5383 /* Wake time has overflowed. Place this item in the overflow list. */
5384 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5385 }
5386 else
5387 {
5388 /* The wake time has not overflowed, so the current block list is used. */
5389 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005390
xiaohu.huang58292b32024-01-03 14:09:51 +08005391 /* If the task entering the blocked state was placed at the head of the
5392 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
5393 * too. */
5394 if( xTimeToWake < xNextTaskUnblockTime )
5395 {
5396 xNextTaskUnblockTime = xTimeToWake;
5397 }
5398 else
5399 {
5400 mtCOVERAGE_TEST_MARKER();
5401 }
5402 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005403
xiaohu.huang58292b32024-01-03 14:09:51 +08005404 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
5405 ( void ) xCanBlockIndefinitely;
5406 }
5407 #endif /* INCLUDE_vTaskSuspend */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005408}
5409
5410/* Code below here allows additional code to be inserted into this source file,
xiaohu.huang58292b32024-01-03 14:09:51 +08005411 * especially where access to file scope functions and data is needed (for example
5412 * when performing module tests). */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005413
5414#ifdef FREERTOS_MODULE_TEST
xiaohu.huang58292b32024-01-03 14:09:51 +08005415 #include "tasks_test_access_functions.h"
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005416#endif
5417
5418
xiaohu.huang58292b32024-01-03 14:09:51 +08005419#if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005420
xiaohu.huang58292b32024-01-03 14:09:51 +08005421 #include "freertos_tasks_c_additions.h"
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005422
xiaohu.huang58292b32024-01-03 14:09:51 +08005423 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
5424 static void freertos_tasks_c_additions_init( void )
5425 {
5426 FREERTOS_TASKS_C_ADDITIONS_INIT();
5427 }
5428 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005429
xiaohu.huang58292b32024-01-03 14:09:51 +08005430#endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
Xiaohu.Huangc9a7d4f2022-03-15 13:48:38 +08005431
5432/* Add include implement source code which depend on the inner elements */
5433#include "aml_tasks_ext.c"