blob: 3ef0ec5d4bf93a7eecdf8adabcda05b0bcec5e90 [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"
shijie.xionga23e8242024-02-27 14:57:02 +080043#if CONFIG_FTRACE
44#include "ftrace.h"
45#endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080046
47/* Lint e9021, e961 and e750 are suppressed as a MISRA exception justified
xiaohu.huang58292b32024-01-03 14:09:51 +080048 * because the MPU ports require MPU_WRAPPERS_INCLUDED_FROM_API_FILE to be defined
49 * for the header files above, but not in this file, in order to generate the
50 * correct privileged Vs unprivileged linkage and placement. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080051#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE /*lint !e961 !e750 !e9021. */
52
53/* Set configUSE_STATS_FORMATTING_FUNCTIONS to 2 to include the stats formatting
xiaohu.huang58292b32024-01-03 14:09:51 +080054 * functions but without including stdio.h here. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080055#if ( configUSE_STATS_FORMATTING_FUNCTIONS == 1 )
xiaohu.huang58292b32024-01-03 14:09:51 +080056
57/* At the bottom of this file are two optional functions that can be used
58 * to generate human readable text from the raw data generated by the
59 * uxTaskGetSystemState() function. Note the formatting functions are provided
60 * for convenience only, and are NOT considered part of the kernel. */
61 #include <stdio.h>
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080062#endif /* configUSE_STATS_FORMATTING_FUNCTIONS == 1 ) */
63
xiaohu.huang58292b32024-01-03 14:09:51 +080064#if ( configUSE_PREEMPTION == 0 )
65
66/* If the cooperative scheduler is being used then a yield should not be
67 * performed just because a higher priority task has been woken. */
68 #define taskYIELD_IF_USING_PREEMPTION()
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080069#else
xiaohu.huang58292b32024-01-03 14:09:51 +080070 #define taskYIELD_IF_USING_PREEMPTION() portYIELD_WITHIN_API()
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080071#endif
72
73/* Values that can be assigned to the ucNotifyState member of the TCB. */
xiaohu.huang58292b32024-01-03 14:09:51 +080074#define taskNOT_WAITING_NOTIFICATION ( ( uint8_t ) 0 ) /* Must be zero as it is the initialised value. */
75#define taskWAITING_NOTIFICATION ( ( uint8_t ) 1 )
76#define taskNOTIFICATION_RECEIVED ( ( uint8_t ) 2 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080077
78/*
79 * The value used to fill the stack of a task when the task is created. This
80 * is used purely for checking the high water mark for tasks.
81 */
xiaohu.huang58292b32024-01-03 14:09:51 +080082#define tskSTACK_FILL_BYTE ( 0xa5U )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080083
xiaohu.huang58292b32024-01-03 14:09:51 +080084/* Bits used to record how a task's stack and TCB were allocated. */
85#define tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 0 )
86#define tskSTATICALLY_ALLOCATED_STACK_ONLY ( ( uint8_t ) 1 )
87#define tskSTATICALLY_ALLOCATED_STACK_AND_TCB ( ( uint8_t ) 2 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080088
89/* If any of the following are set then task stacks are filled with a known
xiaohu.huang58292b32024-01-03 14:09:51 +080090 * value so the high water mark can be determined. If none of the following are
91 * set then don't fill the stack so there is no unnecessary dependency on memset. */
92#if ( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
93 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 1
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080094#else
xiaohu.huang58292b32024-01-03 14:09:51 +080095 #define tskSET_NEW_STACKS_TO_KNOWN_VALUE 0
kelvin.zhang57fb6ae2021-10-15 10:19:42 +080096#endif
97
98/*
99 * Macros used by vListTask to indicate which state a task is in.
100 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800101#define tskRUNNING_CHAR ( 'X' )
102#define tskBLOCKED_CHAR ( 'B' )
103#define tskREADY_CHAR ( 'R' )
104#define tskDELETED_CHAR ( 'D' )
105#define tskSUSPENDED_CHAR ( 'S' )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800106
107/*
xiaohu.huang58292b32024-01-03 14:09:51 +0800108 * Some kernel aware debuggers require the data the debugger needs access to to
109 * be global, rather than file scope.
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800110 */
111#ifdef portREMOVE_STATIC_QUALIFIER
xiaohu.huang58292b32024-01-03 14:09:51 +0800112 #define static
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800113#endif
114
115/* The name allocated to the Idle task. This can be overridden by defining
xiaohu.huang58292b32024-01-03 14:09:51 +0800116 * configIDLE_TASK_NAME in FreeRTOSConfig.h. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800117#ifndef configIDLE_TASK_NAME
xiaohu.huang58292b32024-01-03 14:09:51 +0800118 #define configIDLE_TASK_NAME "IDLE"
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800119#endif
120
121#if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
122
xiaohu.huang58292b32024-01-03 14:09:51 +0800123/* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 0 then task selection is
124 * performed in a generic way that is not optimised to any particular
125 * microcontroller architecture. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800126
xiaohu.huang58292b32024-01-03 14:09:51 +0800127/* uxTopReadyPriority holds the priority of the highest priority ready
128 * state task. */
129 #define taskRECORD_READY_PRIORITY( uxPriority ) \
130 { \
131 if( ( uxPriority ) > uxTopReadyPriority ) \
132 { \
133 uxTopReadyPriority = ( uxPriority ); \
134 } \
135 } /* taskRECORD_READY_PRIORITY */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800136
xiaohu.huang58292b32024-01-03 14:09:51 +0800137/*-----------------------------------------------------------*/
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800138
xiaohu.huang58292b32024-01-03 14:09:51 +0800139 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
140 { \
141 UBaseType_t uxTopPriority = uxTopReadyPriority; \
142 \
143 /* Find the highest priority queue that contains ready tasks. */ \
144 while( listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxTopPriority ] ) ) ) \
145 { \
146 configASSERT( uxTopPriority ); \
147 --uxTopPriority; \
148 } \
149 \
150 /* listGET_OWNER_OF_NEXT_ENTRY indexes through the list, so the tasks of \
151 * the same priority get an equal share of the processor time. */ \
152 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
153 uxTopReadyPriority = uxTopPriority; \
154 } /* taskSELECT_HIGHEST_PRIORITY_TASK */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800155
xiaohu.huang58292b32024-01-03 14:09:51 +0800156/*-----------------------------------------------------------*/
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800157
xiaohu.huang58292b32024-01-03 14:09:51 +0800158/* Define away taskRESET_READY_PRIORITY() and portRESET_READY_PRIORITY() as
159 * they are only required when a port optimised method of task selection is
160 * being used. */
161 #define taskRESET_READY_PRIORITY( uxPriority )
162 #define portRESET_READY_PRIORITY( uxPriority, uxTopReadyPriority )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800163
164#else /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
165
xiaohu.huang58292b32024-01-03 14:09:51 +0800166/* If configUSE_PORT_OPTIMISED_TASK_SELECTION is 1 then task selection is
167 * performed in a way that is tailored to the particular microcontroller
168 * architecture being used. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800169
xiaohu.huang58292b32024-01-03 14:09:51 +0800170/* A port optimised version is provided. Call the port defined macros. */
171 #define taskRECORD_READY_PRIORITY( uxPriority ) portRECORD_READY_PRIORITY( ( uxPriority ), uxTopReadyPriority )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800172
xiaohu.huang58292b32024-01-03 14:09:51 +0800173/*-----------------------------------------------------------*/
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800174
xiaohu.huang58292b32024-01-03 14:09:51 +0800175 #define taskSELECT_HIGHEST_PRIORITY_TASK() \
176 { \
177 UBaseType_t uxTopPriority; \
178 \
179 /* Find the highest priority list that contains ready tasks. */ \
180 portGET_HIGHEST_PRIORITY( uxTopPriority, uxTopReadyPriority ); \
181 configASSERT( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ uxTopPriority ] ) ) > 0 ); \
182 listGET_OWNER_OF_NEXT_ENTRY( pxCurrentTCB, &( pxReadyTasksLists[ uxTopPriority ] ) ); \
183 } /* taskSELECT_HIGHEST_PRIORITY_TASK() */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800184
xiaohu.huang58292b32024-01-03 14:09:51 +0800185/*-----------------------------------------------------------*/
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800186
xiaohu.huang58292b32024-01-03 14:09:51 +0800187/* A port optimised version is provided, call it only if the TCB being reset
188 * is being referenced from a ready list. If it is referenced from a delayed
189 * or suspended list then it won't be in a ready list. */
190 #define taskRESET_READY_PRIORITY( uxPriority ) \
191 { \
192 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ ( uxPriority ) ] ) ) == ( UBaseType_t ) 0 ) \
193 { \
194 portRESET_READY_PRIORITY( ( uxPriority ), ( uxTopReadyPriority ) ); \
195 } \
196 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800197
198#endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */
199
200/*-----------------------------------------------------------*/
201
202/* pxDelayedTaskList and pxOverflowDelayedTaskList are switched when the tick
xiaohu.huang58292b32024-01-03 14:09:51 +0800203 * count overflows. */
204#define taskSWITCH_DELAYED_LISTS() \
205 { \
206 List_t * pxTemp; \
207 \
208 /* The delayed tasks list should be empty when the lists are switched. */ \
209 configASSERT( ( listLIST_IS_EMPTY( pxDelayedTaskList ) ) ); \
210 \
211 pxTemp = pxDelayedTaskList; \
212 pxDelayedTaskList = pxOverflowDelayedTaskList; \
213 pxOverflowDelayedTaskList = pxTemp; \
214 xNumOfOverflows++; \
215 prvResetNextTaskUnblockTime(); \
216 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800217
218/*-----------------------------------------------------------*/
219
220/*
221 * Place the task represented by pxTCB into the appropriate ready list for
222 * the task. It is inserted at the end of the list.
223 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800224#define prvAddTaskToReadyList( pxTCB ) \
225 traceMOVED_TASK_TO_READY_STATE( pxTCB ); \
226 taskRECORD_READY_PRIORITY( ( pxTCB )->uxPriority ); \
227 listINSERT_END( &( pxReadyTasksLists[ ( pxTCB )->uxPriority ] ), &( ( pxTCB )->xStateListItem ) ); \
228 tracePOST_MOVED_TASK_TO_READY_STATE( pxTCB )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800229/*-----------------------------------------------------------*/
230
231/*
xiaohu.huang58292b32024-01-03 14:09:51 +0800232 * Several functions take a TaskHandle_t parameter that can optionally be NULL,
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800233 * where NULL is used to indicate that the handle of the currently executing
234 * task should be used in place of the parameter. This macro simply checks to
235 * see if the parameter is NULL and returns a pointer to the appropriate TCB.
236 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800237#define prvGetTCBFromHandle( pxHandle ) ( ( ( pxHandle ) == NULL ) ? pxCurrentTCB : ( pxHandle ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800238
239/* The item value of the event list item is normally used to hold the priority
xiaohu.huang58292b32024-01-03 14:09:51 +0800240 * of the task to which it belongs (coded to allow it to be held in reverse
241 * priority order). However, it is occasionally borrowed for other purposes. It
242 * is important its value is not updated due to a task priority change while it is
243 * being used for another purpose. The following bit definition is used to inform
244 * the scheduler that the value should not be changed - in which case it is the
245 * responsibility of whichever module is using the value to ensure it gets set back
246 * to its original value when it is released. */
247#if ( configUSE_16_BIT_TICKS == 1 )
248 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x8000U
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800249#else
xiaohu.huang58292b32024-01-03 14:09:51 +0800250 #define taskEVENT_LIST_ITEM_VALUE_IN_USE 0x80000000UL
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800251#endif
252
253/*
254 * Task control block. A task control block (TCB) is allocated for each task,
255 * and stores task state information, including a pointer to the task's context
256 * (the task's run time environment, including register values)
257 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800258typedef struct tskTaskControlBlock /* The old naming convention is used to prevent breaking kernel aware debuggers. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800259{
xiaohu.huang58292b32024-01-03 14:09:51 +0800260 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 +0800261
xiaohu.huang58292b32024-01-03 14:09:51 +0800262 #if ( portUSING_MPU_WRAPPERS == 1 )
263 xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */
264 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800265
xiaohu.huang58292b32024-01-03 14:09:51 +0800266 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 ). */
267 ListItem_t xEventListItem; /*< Used to reference a task from an event list. */
268 UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */
269 StackType_t * pxStack; /*< Points to the start of the stack. */
xiaohu.huang79d92512024-02-21 14:33:03 +0800270 configSTACK_DEPTH_TYPE xStackDepth; /* AML add the member and add dummy member to xSTATIC_TCB in FreeRTOS.h */
xiaohu.huang58292b32024-01-03 14:09:51 +0800271 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 +0800272
xiaohu.huang58292b32024-01-03 14:09:51 +0800273 #if ( ( portSTACK_GROWTH > 0 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
274 StackType_t * pxEndOfStack; /*< Points to the highest valid address for the stack. */
275 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800276
xiaohu.huang58292b32024-01-03 14:09:51 +0800277 #if ( portCRITICAL_NESTING_IN_TCB == 1 )
278 UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */
279 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800280
xiaohu.huang58292b32024-01-03 14:09:51 +0800281 #if ( configUSE_TRACE_FACILITY == 1 )
282 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. */
283 UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */
284 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800285
xiaohu.huang58292b32024-01-03 14:09:51 +0800286 #if ( configUSE_MUTEXES == 1 )
287 UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */
288 UBaseType_t uxMutexesHeld;
289 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800290
xiaohu.huang58292b32024-01-03 14:09:51 +0800291 #if ( configUSE_APPLICATION_TASK_TAG == 1 )
292 TaskHookFunction_t pxTaskTag;
293 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800294
xiaohu.huang58292b32024-01-03 14:09:51 +0800295 #if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )
296 void * pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];
297 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800298
xiaohu.huang58292b32024-01-03 14:09:51 +0800299 #if ( configGENERATE_RUN_TIME_STATS == 1 )
300 configRUN_TIME_COUNTER_TYPE ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */
301 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800302
xiaohu.huang58292b32024-01-03 14:09:51 +0800303 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
304 configTLS_BLOCK_TYPE xTLSBlock; /*< Memory block used as Thread Local Storage (TLS) Block for the task. */
305 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800306
xiaohu.huang58292b32024-01-03 14:09:51 +0800307 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
308 volatile uint32_t ulNotifiedValue[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
309 volatile uint8_t ucNotifyState[ configTASK_NOTIFICATION_ARRAY_ENTRIES ];
310 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800311
xiaohu.huang58292b32024-01-03 14:09:51 +0800312 /* See the comments in FreeRTOS.h with the definition of
313 * tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
314 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
315 uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
316 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800317
xiaohu.huang58292b32024-01-03 14:09:51 +0800318 #if ( INCLUDE_xTaskAbortDelay == 1 )
319 uint8_t ucDelayAborted;
320 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800321
xiaohu.huang58292b32024-01-03 14:09:51 +0800322 #if ( configUSE_POSIX_ERRNO == 1 )
323 int iTaskErrno;
324 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800325} tskTCB;
326
327/* The old tskTCB name is maintained above then typedefed to the new TCB_t name
xiaohu.huang58292b32024-01-03 14:09:51 +0800328 * below to enable the use of older kernel aware debuggers. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800329typedef tskTCB TCB_t;
330
331/*lint -save -e956 A manual analysis and inspection has been used to determine
xiaohu.huang58292b32024-01-03 14:09:51 +0800332 * which static variables must be declared volatile. */
333portDONT_DISCARD PRIVILEGED_DATA TCB_t * volatile pxCurrentTCB = NULL;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800334
335/* Lists for ready and blocked tasks. --------------------
xiaohu.huang58292b32024-01-03 14:09:51 +0800336 * xDelayedTaskList1 and xDelayedTaskList2 could be moved to function scope but
337 * doing so breaks some kernel aware debuggers and debuggers that rely on removing
338 * the static qualifier. */
339PRIVILEGED_DATA static List_t pxReadyTasksLists[ configMAX_PRIORITIES ]; /*< Prioritised ready tasks. */
340PRIVILEGED_DATA static List_t xDelayedTaskList1; /*< Delayed tasks. */
341PRIVILEGED_DATA static List_t xDelayedTaskList2; /*< Delayed tasks (two lists are used - one for delays that have overflowed the current tick count. */
342PRIVILEGED_DATA static List_t * volatile pxDelayedTaskList; /*< Points to the delayed task list currently being used. */
343PRIVILEGED_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. */
344PRIVILEGED_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 +0800345
xiaohu.huang58292b32024-01-03 14:09:51 +0800346#if ( INCLUDE_vTaskDelete == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800347
xiaohu.huang58292b32024-01-03 14:09:51 +0800348 PRIVILEGED_DATA static List_t xTasksWaitingTermination; /*< Tasks that have been deleted - but their memory not yet freed. */
349 PRIVILEGED_DATA static volatile UBaseType_t uxDeletedTasksWaitingCleanUp = ( UBaseType_t ) 0U;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800350
351#endif
352
353#if ( INCLUDE_vTaskSuspend == 1 )
354
xiaohu.huang58292b32024-01-03 14:09:51 +0800355 PRIVILEGED_DATA static List_t xSuspendedTaskList; /*< Tasks that are currently suspended. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800356
357#endif
358
359/* Global POSIX errno. Its value is changed upon context switching to match
xiaohu.huang58292b32024-01-03 14:09:51 +0800360 * the errno of the currently running task. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800361#if ( configUSE_POSIX_ERRNO == 1 )
xiaohu.huang58292b32024-01-03 14:09:51 +0800362 int FreeRTOS_errno = 0;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800363#endif
364
365/* Other file private variables. --------------------------------*/
xiaohu.huang58292b32024-01-03 14:09:51 +0800366PRIVILEGED_DATA static volatile UBaseType_t uxCurrentNumberOfTasks = ( UBaseType_t ) 0U;
367PRIVILEGED_DATA static volatile TickType_t xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
368PRIVILEGED_DATA static volatile UBaseType_t uxTopReadyPriority = tskIDLE_PRIORITY;
369PRIVILEGED_DATA static volatile BaseType_t xSchedulerRunning = pdFALSE;
370PRIVILEGED_DATA static volatile TickType_t xPendedTicks = ( TickType_t ) 0U;
371PRIVILEGED_DATA static volatile BaseType_t xYieldPending = pdFALSE;
372PRIVILEGED_DATA static volatile BaseType_t xNumOfOverflows = ( BaseType_t ) 0;
373PRIVILEGED_DATA static UBaseType_t uxTaskNumber = ( UBaseType_t ) 0U;
374PRIVILEGED_DATA static volatile TickType_t xNextTaskUnblockTime = ( TickType_t ) 0U; /* Initialised to portMAX_DELAY before the scheduler starts. */
375PRIVILEGED_DATA static TaskHandle_t xIdleTaskHandle = NULL; /*< Holds the handle of the idle task. The idle task is created automatically when the scheduler is started. */
376
377/* Improve support for OpenOCD. The kernel tracks Ready tasks via priority lists.
378 * For tracking the state of remote threads, OpenOCD uses uxTopUsedPriority
379 * to determine the number of priority lists to read back from the remote target. */
380const volatile UBaseType_t uxTopUsedPriority = configMAX_PRIORITIES - 1U;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800381
382/* Context switches are held pending while the scheduler is suspended. Also,
xiaohu.huang58292b32024-01-03 14:09:51 +0800383 * interrupts must not manipulate the xStateListItem of a TCB, or any of the
384 * lists the xStateListItem can be referenced from, if the scheduler is suspended.
385 * If an interrupt needs to unblock a task while the scheduler is suspended then it
386 * moves the task's event list item into the xPendingReadyList, ready for the
387 * kernel to move the task from the pending ready list into the real ready list
388 * when the scheduler is unsuspended. The pending ready list itself can only be
389 * accessed from a critical section. */
390PRIVILEGED_DATA static volatile UBaseType_t uxSchedulerSuspended = ( UBaseType_t ) pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800391
392#if ( configGENERATE_RUN_TIME_STATS == 1 )
393
xiaohu.huang58292b32024-01-03 14:09:51 +0800394/* Do not move these variables to function scope as doing so prevents the
395 * code working with debuggers that need to remove the static qualifier. */
396 PRIVILEGED_DATA static configRUN_TIME_COUNTER_TYPE ulTaskSwitchedInTime = 0UL; /*< Holds the value of a timer/counter the last time a task was switched in. */
397 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 +0800398
399#endif
400
401/*lint -restore */
402
403/*-----------------------------------------------------------*/
404
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800405/* File private functions. --------------------------------*/
406
407/**
408 * Utility task that simply returns pdTRUE if the task referenced by xTask is
409 * currently in the Suspended state, or pdFALSE if the task referenced by xTask
410 * is in any other state.
411 */
412#if ( INCLUDE_vTaskSuspend == 1 )
413
xiaohu.huang58292b32024-01-03 14:09:51 +0800414 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800415
416#endif /* INCLUDE_vTaskSuspend */
417
418/*
419 * Utility to ready all the lists used by the scheduler. This is called
420 * automatically upon the creation of the first task.
421 */
422static void prvInitialiseTaskLists( void ) PRIVILEGED_FUNCTION;
423
424/*
425 * The idle task, which as all tasks is implemented as a never ending loop.
426 * The idle task is automatically created and added to the ready lists upon
427 * creation of the first user task.
428 *
429 * The portTASK_FUNCTION_PROTO() macro is used to allow port/compiler specific
430 * language extensions. The equivalent prototype for this function is:
431 *
432 * void prvIdleTask( void *pvParameters );
433 *
434 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800435static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800436
437/*
438 * Utility to free all memory allocated by the scheduler to hold a TCB,
439 * including the stack pointed to by the TCB.
440 *
441 * This does not free memory allocated by the task itself (i.e. memory
442 * allocated by calls to pvPortMalloc from within the tasks application code).
443 */
444#if ( INCLUDE_vTaskDelete == 1 )
445
xiaohu.huang58292b32024-01-03 14:09:51 +0800446 static void prvDeleteTCB( TCB_t * pxTCB ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800447
448#endif
449
450/*
451 * Used only by the idle task. This checks to see if anything has been placed
452 * in the list of tasks waiting to be deleted. If so the task is cleaned up
453 * and its TCB deleted.
454 */
455static void prvCheckTasksWaitingTermination( void ) PRIVILEGED_FUNCTION;
456
457/*
458 * The currently executing task is entering the Blocked state. Add the task to
459 * either the current or the overflow delayed task list.
460 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800461static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
462 const BaseType_t xCanBlockIndefinitely ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800463
464/*
465 * Fills an TaskStatus_t structure with information on each task that is
466 * referenced from the pxList list (which may be a ready list, a delayed list,
467 * a suspended list, etc.).
468 *
469 * THIS FUNCTION IS INTENDED FOR DEBUGGING ONLY, AND SHOULD NOT BE CALLED FROM
470 * NORMAL APPLICATION CODE.
471 */
472#if ( configUSE_TRACE_FACILITY == 1 )
473
xiaohu.huang58292b32024-01-03 14:09:51 +0800474 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
475 List_t * pxList,
476 eTaskState eState ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800477
478#endif
479
480/*
481 * Searches pxList for a task with name pcNameToQuery - returning a handle to
482 * the task if it is found, or NULL if the task is not found.
483 */
484#if ( INCLUDE_xTaskGetHandle == 1 )
485
xiaohu.huang58292b32024-01-03 14:09:51 +0800486 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
487 const char pcNameToQuery[] ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800488
489#endif
490
491/*
492 * When a task is created, the stack of the task is filled with a known value.
493 * This function determines the 'high water mark' of the task stack by
494 * determining how much of the stack remains at the original preset value.
495 */
496#if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
497
xiaohu.huang58292b32024-01-03 14:09:51 +0800498 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800499
500#endif
501
502/*
503 * Return the amount of time, in ticks, that will pass before the kernel will
504 * next move a task from the Blocked state to the Running state.
505 *
506 * This conditional compilation should use inequality to 0, not equality to 1.
507 * This is to ensure portSUPPRESS_TICKS_AND_SLEEP() can be called when user
508 * defined low power mode implementations require configUSE_TICKLESS_IDLE to be
509 * set to a value other than 1.
510 */
511#if ( configUSE_TICKLESS_IDLE != 0 )
512
xiaohu.huang58292b32024-01-03 14:09:51 +0800513 static TickType_t prvGetExpectedIdleTime( void ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800514
515#endif
516
517/*
518 * Set xNextTaskUnblockTime to the time at which the next Blocked state task
519 * will exit the Blocked state.
520 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800521static void prvResetNextTaskUnblockTime( void ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800522
xiaohu.huang58292b32024-01-03 14:09:51 +0800523#if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800524
xiaohu.huang58292b32024-01-03 14:09:51 +0800525/*
526 * Helper function used to pad task names with spaces when printing out
527 * human readable tables of task information.
528 */
529 static char * prvWriteNameToBuffer( char * pcBuffer,
530 const char * pcTaskName ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800531
532#endif
533
534/*
535 * Called after a Task_t structure has been allocated either statically or
536 * dynamically to fill in the structure's members.
537 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800538static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
539 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
xiaohu.huang79d92512024-02-21 14:33:03 +0800540 const configSTACK_DEPTH_TYPE ulStackDepth,
xiaohu.huang58292b32024-01-03 14:09:51 +0800541 void * const pvParameters,
542 UBaseType_t uxPriority,
543 TaskHandle_t * const pxCreatedTask,
544 TCB_t * pxNewTCB,
545 const MemoryRegion_t * const xRegions ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800546
547/*
548 * Called after a new task has been created and initialised to place the task
549 * under the control of the scheduler.
550 */
xiaohu.huang58292b32024-01-03 14:09:51 +0800551static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800552
553/*
554 * freertos_tasks_c_additions_init() should only be called if the user definable
555 * macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is the only macro
556 * called by the function.
557 */
558#ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
559
xiaohu.huang58292b32024-01-03 14:09:51 +0800560 static void freertos_tasks_c_additions_init( void ) PRIVILEGED_FUNCTION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800561
562#endif
563
564/*-----------------------------------------------------------*/
565
xiaohu.huang58292b32024-01-03 14:09:51 +0800566#if ( configSUPPORT_STATIC_ALLOCATION == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800567
xiaohu.huang58292b32024-01-03 14:09:51 +0800568 TaskHandle_t xTaskCreateStatic( TaskFunction_t pxTaskCode,
569 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
xiaohu.huang79d92512024-02-21 14:33:03 +0800570 const configSTACK_DEPTH_TYPE ulStackDepth,
xiaohu.huang58292b32024-01-03 14:09:51 +0800571 void * const pvParameters,
572 UBaseType_t uxPriority,
573 StackType_t * const puxStackBuffer,
574 StaticTask_t * const pxTaskBuffer )
575 {
576 TCB_t * pxNewTCB;
577 TaskHandle_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800578
xiaohu.huang58292b32024-01-03 14:09:51 +0800579 configASSERT( puxStackBuffer != NULL );
580 configASSERT( pxTaskBuffer != NULL );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800581
xiaohu.huang58292b32024-01-03 14:09:51 +0800582 #if ( configASSERT_DEFINED == 1 )
583 {
584 /* Sanity check that the size of the structure used to declare a
585 * variable of type StaticTask_t equals the size of the real task
586 * structure. */
587 volatile size_t xSize = sizeof( StaticTask_t );
588 configASSERT( xSize == sizeof( TCB_t ) );
589 ( void ) xSize; /* Prevent lint warning when configASSERT() is not used. */
590 }
591 #endif /* configASSERT_DEFINED */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800592
xiaohu.huang58292b32024-01-03 14:09:51 +0800593 if( ( pxTaskBuffer != NULL ) && ( puxStackBuffer != NULL ) )
594 {
595 /* The memory used for the task's TCB and stack are passed into this
596 * function - use them. */
597 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. */
598 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
599 pxNewTCB->pxStack = ( StackType_t * ) puxStackBuffer;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800600
xiaohu.huang58292b32024-01-03 14:09:51 +0800601 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
602 {
603 /* Tasks can be created statically or dynamically, so note this
604 * task was created statically in case the task is later deleted. */
605 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
606 }
607 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800608
xiaohu.huang58292b32024-01-03 14:09:51 +0800609 prvInitialiseNewTask( pxTaskCode, pcName, ulStackDepth, pvParameters, uxPriority, &xReturn, pxNewTCB, NULL );
610 prvAddNewTaskToReadyList( pxNewTCB );
611 }
612 else
613 {
614 xReturn = NULL;
615 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800616
xiaohu.huang58292b32024-01-03 14:09:51 +0800617 return xReturn;
618 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800619
620#endif /* SUPPORT_STATIC_ALLOCATION */
621/*-----------------------------------------------------------*/
622
xiaohu.huang58292b32024-01-03 14:09:51 +0800623#if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800624
xiaohu.huang58292b32024-01-03 14:09:51 +0800625 BaseType_t xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition,
626 TaskHandle_t * pxCreatedTask )
627 {
628 TCB_t * pxNewTCB;
629 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800630
xiaohu.huang58292b32024-01-03 14:09:51 +0800631 configASSERT( pxTaskDefinition->puxStackBuffer != NULL );
632 configASSERT( pxTaskDefinition->pxTaskBuffer != NULL );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800633
xiaohu.huang58292b32024-01-03 14:09:51 +0800634 if( ( pxTaskDefinition->puxStackBuffer != NULL ) && ( pxTaskDefinition->pxTaskBuffer != NULL ) )
635 {
636 /* Allocate space for the TCB. Where the memory comes from depends
637 * on the implementation of the port malloc function and whether or
638 * not static allocation is being used. */
639 pxNewTCB = ( TCB_t * ) pxTaskDefinition->pxTaskBuffer;
640 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800641
xiaohu.huang58292b32024-01-03 14:09:51 +0800642 /* Store the stack location in the TCB. */
643 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800644
xiaohu.huang58292b32024-01-03 14:09:51 +0800645 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
646 {
647 /* Tasks can be created statically or dynamically, so note this
648 * task was created statically in case the task is later deleted. */
649 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_AND_TCB;
650 }
651 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800652
xiaohu.huang58292b32024-01-03 14:09:51 +0800653 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
654 pxTaskDefinition->pcName,
xiaohu.huang79d92512024-02-21 14:33:03 +0800655 ( configSTACK_DEPTH_TYPE ) pxTaskDefinition->usStackDepth,
xiaohu.huang58292b32024-01-03 14:09:51 +0800656 pxTaskDefinition->pvParameters,
657 pxTaskDefinition->uxPriority,
658 pxCreatedTask, pxNewTCB,
659 pxTaskDefinition->xRegions );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800660
xiaohu.huang58292b32024-01-03 14:09:51 +0800661 prvAddNewTaskToReadyList( pxNewTCB );
662 xReturn = pdPASS;
663 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800664
xiaohu.huang58292b32024-01-03 14:09:51 +0800665 return xReturn;
666 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800667
668#endif /* ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
669/*-----------------------------------------------------------*/
670
xiaohu.huang58292b32024-01-03 14:09:51 +0800671#if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800672
xiaohu.huang58292b32024-01-03 14:09:51 +0800673 BaseType_t xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition,
674 TaskHandle_t * pxCreatedTask )
675 {
676 TCB_t * pxNewTCB;
677 BaseType_t xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800678
xiaohu.huang58292b32024-01-03 14:09:51 +0800679 configASSERT( pxTaskDefinition->puxStackBuffer );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800680
xiaohu.huang58292b32024-01-03 14:09:51 +0800681 if( pxTaskDefinition->puxStackBuffer != NULL )
682 {
683 /* Allocate space for the TCB. Where the memory comes from depends
684 * on the implementation of the port malloc function and whether or
685 * not static allocation is being used. */
686 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800687
xiaohu.huang58292b32024-01-03 14:09:51 +0800688 if( pxNewTCB != NULL )
689 {
690 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800691
xiaohu.huang58292b32024-01-03 14:09:51 +0800692 /* Store the stack location in the TCB. */
693 pxNewTCB->pxStack = pxTaskDefinition->puxStackBuffer;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800694
xiaohu.huang58292b32024-01-03 14:09:51 +0800695 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )
696 {
697 /* Tasks can be created statically or dynamically, so note
698 * this task had a statically allocated stack in case it is
699 * later deleted. The TCB was allocated dynamically. */
700 pxNewTCB->ucStaticallyAllocated = tskSTATICALLY_ALLOCATED_STACK_ONLY;
701 }
702 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800703
xiaohu.huang58292b32024-01-03 14:09:51 +0800704 prvInitialiseNewTask( pxTaskDefinition->pvTaskCode,
705 pxTaskDefinition->pcName,
xiaohu.huang79d92512024-02-21 14:33:03 +0800706 ( configSTACK_DEPTH_TYPE ) pxTaskDefinition->usStackDepth,
xiaohu.huang58292b32024-01-03 14:09:51 +0800707 pxTaskDefinition->pvParameters,
708 pxTaskDefinition->uxPriority,
709 pxCreatedTask, pxNewTCB,
710 pxTaskDefinition->xRegions );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800711
xiaohu.huang58292b32024-01-03 14:09:51 +0800712 prvAddNewTaskToReadyList( pxNewTCB );
713 xReturn = pdPASS;
714 }
715 }
716
717 return xReturn;
718 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800719
720#endif /* portUSING_MPU_WRAPPERS */
721/*-----------------------------------------------------------*/
722
xiaohu.huang58292b32024-01-03 14:09:51 +0800723#if ( configSUPPORT_DYNAMIC_ALLOCATION == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800724
xiaohu.huang58292b32024-01-03 14:09:51 +0800725 BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
726 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
727 const configSTACK_DEPTH_TYPE usStackDepth,
728 void * const pvParameters,
729 UBaseType_t uxPriority,
730 TaskHandle_t * const pxCreatedTask )
731 {
732 TCB_t * pxNewTCB;
733 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800734
xiaohu.huang58292b32024-01-03 14:09:51 +0800735 /* If the stack grows down then allocate the stack then the TCB so the stack
736 * does not grow into the TCB. Likewise if the stack grows up then allocate
737 * the TCB then the stack. */
738 #if ( portSTACK_GROWTH > 0 )
739 {
740 /* Allocate space for the TCB. Where the memory comes from depends on
741 * the implementation of the port malloc function and whether or not static
742 * allocation is being used. */
743 pxNewTCB = ( TCB_t * ) pvPortMalloc( sizeof( TCB_t ) );
Xiaohu.Huang2c96ef42021-10-15 16:12:27 +0800744
xiaohu.huang58292b32024-01-03 14:09:51 +0800745 if( pxNewTCB != NULL )
746 {
747 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800748
xiaohu.huang58292b32024-01-03 14:09:51 +0800749 /* Allocate space for the stack used by the task being created.
750 * The base of the stack memory stored in the TCB so the task can
751 * be deleted later if required. */
752 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 +0800753
xiaohu.huang58292b32024-01-03 14:09:51 +0800754 if( pxNewTCB->pxStack == NULL )
755 {
756 /* Could not allocate the stack. Delete the allocated TCB. */
757 vPortFree( pxNewTCB );
758 pxNewTCB = NULL;
759 }
760 }
761 }
762 #else /* portSTACK_GROWTH */
763 {
764 StackType_t * pxStack;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800765
xiaohu.huang58292b32024-01-03 14:09:51 +0800766 /* Allocate space for the stack used by the task being created. */
767 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 +0800768
xiaohu.huang58292b32024-01-03 14:09:51 +0800769 if( pxStack != NULL )
770 {
771 /* Allocate space for the TCB. */
772 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 +0800773
xiaohu.huang58292b32024-01-03 14:09:51 +0800774 if( pxNewTCB != NULL )
775 {
776 memset( ( void * ) pxNewTCB, 0x00, sizeof( TCB_t ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800777
xiaohu.huang58292b32024-01-03 14:09:51 +0800778 /* Store the stack location in the TCB. */
779 pxNewTCB->pxStack = pxStack;
780 }
781 else
782 {
783 /* The stack cannot be used as the TCB was not created. Free
784 * it again. */
785 vPortFreeStack( pxStack );
786 }
787 }
788 else
789 {
790 pxNewTCB = NULL;
791 }
792 }
793 #endif /* portSTACK_GROWTH */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800794
xiaohu.huang58292b32024-01-03 14:09:51 +0800795 if( pxNewTCB != NULL )
796 {
797 #if ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e9029 !e731 Macro has been consolidated for readability reasons. */
798 {
799 /* Tasks can be created statically or dynamically, so note this
800 * task was created dynamically in case it is later deleted. */
801 pxNewTCB->ucStaticallyAllocated = tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB;
802 }
803 #endif /* tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800804
xiaohu.huang79d92512024-02-21 14:33:03 +0800805 prvInitialiseNewTask( pxTaskCode, pcName, ( configSTACK_DEPTH_TYPE ) usStackDepth, pvParameters, uxPriority, pxCreatedTask, pxNewTCB, NULL );
xiaohu.huang58292b32024-01-03 14:09:51 +0800806 prvAddNewTaskToReadyList( pxNewTCB );
807 xReturn = pdPASS;
808 }
809 else
810 {
811 xReturn = errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY;
812 }
813
814 return xReturn;
815 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800816
817#endif /* configSUPPORT_DYNAMIC_ALLOCATION */
818/*-----------------------------------------------------------*/
xiaohu.huang58292b32024-01-03 14:09:51 +0800819
820static void prvInitialiseNewTask( TaskFunction_t pxTaskCode,
821 const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
xiaohu.huang79d92512024-02-21 14:33:03 +0800822 const configSTACK_DEPTH_TYPE ulStackDepth,
xiaohu.huang58292b32024-01-03 14:09:51 +0800823 void * const pvParameters,
824 UBaseType_t uxPriority,
825 TaskHandle_t * const pxCreatedTask,
826 TCB_t * pxNewTCB,
827 const MemoryRegion_t * const xRegions )
Xiaohu.Huang2c96ef42021-10-15 16:12:27 +0800828{
xiaohu.huang58292b32024-01-03 14:09:51 +0800829 StackType_t * pxTopOfStack;
830 UBaseType_t x;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800831
xiaohu.huang58292b32024-01-03 14:09:51 +0800832 #if ( portUSING_MPU_WRAPPERS == 1 )
833 /* Should the task be created in privileged mode? */
834 BaseType_t xRunPrivileged;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800835
xiaohu.huang58292b32024-01-03 14:09:51 +0800836 if( ( uxPriority & portPRIVILEGE_BIT ) != 0U )
837 {
838 xRunPrivileged = pdTRUE;
839 }
840 else
841 {
842 xRunPrivileged = pdFALSE;
843 }
844 uxPriority &= ~portPRIVILEGE_BIT;
845 #endif /* portUSING_MPU_WRAPPERS == 1 */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800846
shijie.xiong02c0d642024-02-20 10:37:19 +0800847 pxNewTCB->xStackDepth = ulStackDepth;
848
xiaohu.huang58292b32024-01-03 14:09:51 +0800849 /* Avoid dependency on memset() if it is not required. */
850 #if ( tskSET_NEW_STACKS_TO_KNOWN_VALUE == 1 )
851 {
852 /* Fill the stack with a known value to assist debugging. */
853 ( void ) memset( pxNewTCB->pxStack, ( int ) tskSTACK_FILL_BYTE, ( size_t ) ulStackDepth * sizeof( StackType_t ) );
854 }
855 #endif /* tskSET_NEW_STACKS_TO_KNOWN_VALUE */
Xiaohu.Huang2c96ef42021-10-15 16:12:27 +0800856
xiaohu.huang58292b32024-01-03 14:09:51 +0800857 /* Calculate the top of stack address. This depends on whether the stack
858 * grows from high memory to low (as per the 80x86) or vice versa.
859 * portSTACK_GROWTH is used to make the result positive or negative as required
860 * by the port. */
861 #if ( portSTACK_GROWTH < 0 )
862 {
xiaohu.huang79d92512024-02-21 14:33:03 +0800863 pxTopOfStack = &( pxNewTCB->pxStack[ ulStackDepth - ( configSTACK_DEPTH_TYPE ) 1 ] );
xiaohu.huang58292b32024-01-03 14:09:51 +0800864 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 +0800865
xiaohu.huang58292b32024-01-03 14:09:51 +0800866 /* Check the alignment of the calculated top of stack is correct. */
867 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxTopOfStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800868
xiaohu.huang58292b32024-01-03 14:09:51 +0800869 #if ( configRECORD_STACK_HIGH_ADDRESS == 1 )
870 {
871 /* Also record the stack's high address, which may assist
872 * debugging. */
873 pxNewTCB->pxEndOfStack = pxTopOfStack;
874 }
875 #endif /* configRECORD_STACK_HIGH_ADDRESS */
876 }
877 #else /* portSTACK_GROWTH */
878 {
879 pxTopOfStack = pxNewTCB->pxStack;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800880
xiaohu.huang58292b32024-01-03 14:09:51 +0800881 /* Check the alignment of the stack buffer is correct. */
882 configASSERT( ( ( ( portPOINTER_SIZE_TYPE ) pxNewTCB->pxStack & ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) == 0UL ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800883
xiaohu.huang58292b32024-01-03 14:09:51 +0800884 /* The other extreme of the stack space is required if stack checking is
885 * performed. */
xiaohu.huang79d92512024-02-21 14:33:03 +0800886 pxNewTCB->pxEndOfStack = pxNewTCB->pxStack + ( ulStackDepth - ( configSTACK_DEPTH_TYPE ) 1 );
xiaohu.huang58292b32024-01-03 14:09:51 +0800887 }
888 #endif /* portSTACK_GROWTH */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800889
xiaohu.huang58292b32024-01-03 14:09:51 +0800890 /* Store the task name in the TCB. */
891 if( pcName != NULL )
892 {
893 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
894 {
895 pxNewTCB->pcTaskName[ x ] = pcName[ x ];
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800896
xiaohu.huang58292b32024-01-03 14:09:51 +0800897 /* Don't copy all configMAX_TASK_NAME_LEN if the string is shorter than
898 * configMAX_TASK_NAME_LEN characters just in case the memory after the
899 * string is not accessible (extremely unlikely). */
900 if( pcName[ x ] == ( char ) 0x00 )
901 {
902 break;
903 }
904 else
905 {
906 mtCOVERAGE_TEST_MARKER();
907 }
908 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800909
xiaohu.huang58292b32024-01-03 14:09:51 +0800910 /* Ensure the name string is terminated in the case that the string length
911 * was greater or equal to configMAX_TASK_NAME_LEN. */
912 pxNewTCB->pcTaskName[ configMAX_TASK_NAME_LEN - 1 ] = '\0';
913 }
914 else
915 {
916 mtCOVERAGE_TEST_MARKER();
917 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800918
xiaohu.huang58292b32024-01-03 14:09:51 +0800919 /* This is used as an array index so must ensure it's not too large. */
920 configASSERT( uxPriority < configMAX_PRIORITIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800921
xiaohu.huang58292b32024-01-03 14:09:51 +0800922 if( uxPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
923 {
924 uxPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
925 }
926 else
927 {
928 mtCOVERAGE_TEST_MARKER();
929 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800930
xiaohu.huang58292b32024-01-03 14:09:51 +0800931 pxNewTCB->uxPriority = uxPriority;
932 #if ( configUSE_MUTEXES == 1 )
933 {
934 pxNewTCB->uxBasePriority = uxPriority;
935 }
936 #endif /* configUSE_MUTEXES */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800937
xiaohu.huang58292b32024-01-03 14:09:51 +0800938 vListInitialiseItem( &( pxNewTCB->xStateListItem ) );
939 vListInitialiseItem( &( pxNewTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800940
xiaohu.huang58292b32024-01-03 14:09:51 +0800941 /* Set the pxNewTCB as a link back from the ListItem_t. This is so we can get
942 * back to the containing TCB from a generic item in a list. */
943 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xStateListItem ), pxNewTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800944
xiaohu.huang58292b32024-01-03 14:09:51 +0800945 /* Event lists are always in priority order. */
946 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. */
947 listSET_LIST_ITEM_OWNER( &( pxNewTCB->xEventListItem ), pxNewTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800948
xiaohu.huang58292b32024-01-03 14:09:51 +0800949 #if ( portUSING_MPU_WRAPPERS == 1 )
950 {
951 vPortStoreTaskMPUSettings( &( pxNewTCB->xMPUSettings ), xRegions, pxNewTCB->pxStack, ulStackDepth );
952 }
953 #else
954 {
955 /* Avoid compiler warning about unreferenced parameter. */
956 ( void ) xRegions;
957 }
958 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800959
xiaohu.huang58292b32024-01-03 14:09:51 +0800960 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
961 {
962 /* Allocate and initialize memory for the task's TLS Block. */
963 configINIT_TLS_BLOCK( pxNewTCB->xTLSBlock );
964 }
965 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +0800966
xiaohu.huang58292b32024-01-03 14:09:51 +0800967 /* Initialize the TCB stack to look as if the task was already running,
968 * but had been interrupted by the scheduler. The return address is set
969 * to the start of the task function. Once the stack has been initialised
970 * the top of stack variable is updated. */
971 #if ( portUSING_MPU_WRAPPERS == 1 )
972 {
973 /* If the port has capability to detect stack overflow,
974 * pass the stack end address to the stack initialization
975 * function as well. */
976 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
977 {
978 #if ( portSTACK_GROWTH < 0 )
979 {
980 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters, xRunPrivileged );
981 }
982 #else /* portSTACK_GROWTH */
983 {
984 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters, xRunPrivileged );
985 }
986 #endif /* portSTACK_GROWTH */
987 }
988 #else /* portHAS_STACK_OVERFLOW_CHECKING */
989 {
990 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters, xRunPrivileged );
991 }
992 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
993 }
994 #else /* portUSING_MPU_WRAPPERS */
995 {
996 /* If the port has capability to detect stack overflow,
997 * pass the stack end address to the stack initialization
998 * function as well. */
999 #if ( portHAS_STACK_OVERFLOW_CHECKING == 1 )
1000 {
1001 #if ( portSTACK_GROWTH < 0 )
1002 {
1003 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxStack, pxTaskCode, pvParameters );
1004 }
1005 #else /* portSTACK_GROWTH */
1006 {
1007 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxNewTCB->pxEndOfStack, pxTaskCode, pvParameters );
1008 }
1009 #endif /* portSTACK_GROWTH */
1010 }
1011 #else /* portHAS_STACK_OVERFLOW_CHECKING */
1012 {
1013 pxNewTCB->pxTopOfStack = pxPortInitialiseStack( pxTopOfStack, pxTaskCode, pvParameters );
1014 }
1015 #endif /* portHAS_STACK_OVERFLOW_CHECKING */
1016 }
1017 #endif /* portUSING_MPU_WRAPPERS */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001018
xiaohu.huang58292b32024-01-03 14:09:51 +08001019 if( pxCreatedTask != NULL )
1020 {
1021 /* Pass the handle out in an anonymous way. The handle can be used to
1022 * change the created task's priority, delete the created task, etc.*/
1023 *pxCreatedTask = ( TaskHandle_t ) pxNewTCB;
1024 }
1025 else
1026 {
1027 mtCOVERAGE_TEST_MARKER();
1028 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001029}
1030/*-----------------------------------------------------------*/
1031
xiaohu.huang58292b32024-01-03 14:09:51 +08001032static void prvAddNewTaskToReadyList( TCB_t * pxNewTCB )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001033{
xiaohu.huang58292b32024-01-03 14:09:51 +08001034 /* Ensure interrupts don't access the task lists while the lists are being
1035 * updated. */
1036 taskENTER_CRITICAL();
1037 {
1038 uxCurrentNumberOfTasks++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001039
xiaohu.huang58292b32024-01-03 14:09:51 +08001040 if( pxCurrentTCB == NULL )
1041 {
1042 /* There are no other tasks, or all the other tasks are in
1043 * the suspended state - make this the current task. */
1044 pxCurrentTCB = pxNewTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001045
xiaohu.huang58292b32024-01-03 14:09:51 +08001046 if( uxCurrentNumberOfTasks == ( UBaseType_t ) 1 )
1047 {
1048 /* This is the first task to be created so do the preliminary
1049 * initialisation required. We will not recover if this call
1050 * fails, but we will report the failure. */
1051 prvInitialiseTaskLists();
1052 }
1053 else
1054 {
1055 mtCOVERAGE_TEST_MARKER();
1056 }
1057 }
1058 else
1059 {
1060 /* If the scheduler is not already running, make this task the
1061 * current task if it is the highest priority task to be created
1062 * so far. */
1063 if( xSchedulerRunning == pdFALSE )
1064 {
1065 if( pxCurrentTCB->uxPriority <= pxNewTCB->uxPriority )
1066 {
1067 pxCurrentTCB = pxNewTCB;
1068 }
1069 else
1070 {
1071 mtCOVERAGE_TEST_MARKER();
1072 }
1073 }
1074 else
1075 {
1076 mtCOVERAGE_TEST_MARKER();
1077 }
1078 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001079
xiaohu.huang58292b32024-01-03 14:09:51 +08001080 uxTaskNumber++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001081
xiaohu.huang58292b32024-01-03 14:09:51 +08001082 #if ( configUSE_TRACE_FACILITY == 1 )
1083 {
1084 /* Add a counter into the TCB for tracing only. */
1085 pxNewTCB->uxTCBNumber = uxTaskNumber;
1086 }
1087 #endif /* configUSE_TRACE_FACILITY */
1088 traceTASK_CREATE( pxNewTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001089
xiaohu.huang58292b32024-01-03 14:09:51 +08001090 prvAddTaskToReadyList( pxNewTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001091
xiaohu.huang58292b32024-01-03 14:09:51 +08001092 portSETUP_TCB( pxNewTCB );
1093 }
1094 taskEXIT_CRITICAL();
1095
1096 if( xSchedulerRunning != pdFALSE )
1097 {
1098 /* If the created task is of a higher priority than the current task
1099 * then it should run now. */
1100 if( pxCurrentTCB->uxPriority < pxNewTCB->uxPriority )
1101 {
1102 taskYIELD_IF_USING_PREEMPTION();
1103 }
1104 else
1105 {
1106 mtCOVERAGE_TEST_MARKER();
1107 }
1108 }
1109 else
1110 {
1111 mtCOVERAGE_TEST_MARKER();
1112 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001113}
1114/*-----------------------------------------------------------*/
1115
1116#if ( INCLUDE_vTaskDelete == 1 )
1117
xiaohu.huang58292b32024-01-03 14:09:51 +08001118 void vTaskDelete( TaskHandle_t xTaskToDelete )
1119 {
1120 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001121
xiaohu.huang58292b32024-01-03 14:09:51 +08001122 taskENTER_CRITICAL();
1123 {
1124 /* If null is passed in here then it is the calling task that is
1125 * being deleted. */
1126 pxTCB = prvGetTCBFromHandle( xTaskToDelete );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001127
xiaohu.huang58292b32024-01-03 14:09:51 +08001128 /* Remove task from the ready/delayed list. */
1129 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1130 {
1131 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1132 }
1133 else
1134 {
1135 mtCOVERAGE_TEST_MARKER();
1136 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001137
xiaohu.huang58292b32024-01-03 14:09:51 +08001138 /* Is the task waiting on an event also? */
1139 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1140 {
1141 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1142 }
1143 else
1144 {
1145 mtCOVERAGE_TEST_MARKER();
1146 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001147
xiaohu.huang58292b32024-01-03 14:09:51 +08001148 /* Increment the uxTaskNumber also so kernel aware debuggers can
1149 * detect that the task lists need re-generating. This is done before
1150 * portPRE_TASK_DELETE_HOOK() as in the Windows port that macro will
1151 * not return. */
1152 uxTaskNumber++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001153
xiaohu.huang58292b32024-01-03 14:09:51 +08001154 if( pxTCB == pxCurrentTCB )
1155 {
1156 /* A task is deleting itself. This cannot complete within the
1157 * task itself, as a context switch to another task is required.
1158 * Place the task in the termination list. The idle task will
1159 * check the termination list and free up any memory allocated by
1160 * the scheduler for the TCB and stack of the deleted task. */
1161 vListInsertEnd( &xTasksWaitingTermination, &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001162
xiaohu.huang58292b32024-01-03 14:09:51 +08001163 /* Increment the ucTasksDeleted variable so the idle task knows
1164 * there is a task that has been deleted and that it should therefore
1165 * check the xTasksWaitingTermination list. */
1166 ++uxDeletedTasksWaitingCleanUp;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001167
xiaohu.huang58292b32024-01-03 14:09:51 +08001168 /* Call the delete hook before portPRE_TASK_DELETE_HOOK() as
1169 * portPRE_TASK_DELETE_HOOK() does not return in the Win32 port. */
1170 traceTASK_DELETE( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001171
xiaohu.huang58292b32024-01-03 14:09:51 +08001172 /* The pre-delete hook is primarily for the Windows simulator,
1173 * in which Windows specific clean up operations are performed,
1174 * after which it is not possible to yield away from this task -
1175 * hence xYieldPending is used to latch that a context switch is
1176 * required. */
1177 portPRE_TASK_DELETE_HOOK( pxTCB, &xYieldPending );
1178 }
1179 else
1180 {
1181 --uxCurrentNumberOfTasks;
1182 traceTASK_DELETE( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001183
xiaohu.huang58292b32024-01-03 14:09:51 +08001184 /* Reset the next expected unblock time in case it referred to
1185 * the task that has just been deleted. */
1186 prvResetNextTaskUnblockTime();
1187 }
1188 }
1189 taskEXIT_CRITICAL();
shijie.xiongf9b5e162022-07-14 15:12:48 +08001190
xiaohu.huang58292b32024-01-03 14:09:51 +08001191 /* If the task is not deleting itself, call prvDeleteTCB from outside of
1192 * critical section. If a task deletes itself, prvDeleteTCB is called
1193 * from prvCheckTasksWaitingTermination which is called from Idle task. */
1194 if( pxTCB != pxCurrentTCB )
1195 {
1196 prvDeleteTCB( pxTCB );
1197 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001198
xiaohu.huang58292b32024-01-03 14:09:51 +08001199 /* Force a reschedule if it is the currently running task that has just
1200 * been deleted. */
1201 if( xSchedulerRunning != pdFALSE )
1202 {
1203 if( pxTCB == pxCurrentTCB )
1204 {
1205 configASSERT( uxSchedulerSuspended == 0 );
1206 portYIELD_WITHIN_API();
1207 }
1208 else
1209 {
1210 mtCOVERAGE_TEST_MARKER();
1211 }
1212 }
1213 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001214
1215#endif /* INCLUDE_vTaskDelete */
1216/*-----------------------------------------------------------*/
1217
xiaohu.huang58292b32024-01-03 14:09:51 +08001218#if ( INCLUDE_xTaskDelayUntil == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001219
xiaohu.huang58292b32024-01-03 14:09:51 +08001220 BaseType_t xTaskDelayUntil( TickType_t * const pxPreviousWakeTime,
1221 const TickType_t xTimeIncrement )
1222 {
1223 TickType_t xTimeToWake;
1224 BaseType_t xAlreadyYielded, xShouldDelay = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001225
xiaohu.huang58292b32024-01-03 14:09:51 +08001226 configASSERT( pxPreviousWakeTime );
1227 configASSERT( ( xTimeIncrement > 0U ) );
1228 configASSERT( uxSchedulerSuspended == 0 );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001229
xiaohu.huang58292b32024-01-03 14:09:51 +08001230 vTaskSuspendAll();
1231 {
1232 /* Minor optimisation. The tick count cannot change in this
1233 * block. */
1234 const TickType_t xConstTickCount = xTickCount;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001235
xiaohu.huang58292b32024-01-03 14:09:51 +08001236 /* Generate the tick time at which the task wants to wake. */
1237 xTimeToWake = *pxPreviousWakeTime + xTimeIncrement;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001238
xiaohu.huang58292b32024-01-03 14:09:51 +08001239 if( xConstTickCount < *pxPreviousWakeTime )
1240 {
1241 /* The tick count has overflowed since this function was
1242 * lasted called. In this case the only time we should ever
1243 * actually delay is if the wake time has also overflowed,
1244 * and the wake time is greater than the tick time. When this
1245 * is the case it is as if neither time had overflowed. */
1246 if( ( xTimeToWake < *pxPreviousWakeTime ) && ( xTimeToWake > xConstTickCount ) )
1247 {
1248 xShouldDelay = pdTRUE;
1249 }
1250 else
1251 {
1252 mtCOVERAGE_TEST_MARKER();
1253 }
1254 }
1255 else
1256 {
1257 /* The tick time has not overflowed. In this case we will
1258 * delay if either the wake time has overflowed, and/or the
1259 * tick time is less than the wake time. */
1260 if( ( xTimeToWake < *pxPreviousWakeTime ) || ( xTimeToWake > xConstTickCount ) )
1261 {
1262 xShouldDelay = pdTRUE;
1263 }
1264 else
1265 {
1266 mtCOVERAGE_TEST_MARKER();
1267 }
1268 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001269
xiaohu.huang58292b32024-01-03 14:09:51 +08001270 /* Update the wake time ready for the next call. */
1271 *pxPreviousWakeTime = xTimeToWake;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001272
xiaohu.huang58292b32024-01-03 14:09:51 +08001273 if( xShouldDelay != pdFALSE )
1274 {
1275 traceTASK_DELAY_UNTIL( xTimeToWake );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001276
xiaohu.huang58292b32024-01-03 14:09:51 +08001277 /* prvAddCurrentTaskToDelayedList() needs the block time, not
1278 * the time to wake, so subtract the current tick count. */
1279 prvAddCurrentTaskToDelayedList( xTimeToWake - xConstTickCount, pdFALSE );
1280 }
1281 else
1282 {
1283 mtCOVERAGE_TEST_MARKER();
1284 }
1285 }
1286 xAlreadyYielded = xTaskResumeAll();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001287
xiaohu.huang58292b32024-01-03 14:09:51 +08001288 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1289 * have put ourselves to sleep. */
1290 if( xAlreadyYielded == pdFALSE )
1291 {
1292 portYIELD_WITHIN_API();
1293 }
1294 else
1295 {
1296 mtCOVERAGE_TEST_MARKER();
1297 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001298
xiaohu.huang58292b32024-01-03 14:09:51 +08001299 return xShouldDelay;
1300 }
1301
1302#endif /* INCLUDE_xTaskDelayUntil */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001303/*-----------------------------------------------------------*/
1304
1305#if ( INCLUDE_vTaskDelay == 1 )
1306
xiaohu.huang58292b32024-01-03 14:09:51 +08001307 void vTaskDelay( const TickType_t xTicksToDelay )
1308 {
1309 BaseType_t xAlreadyYielded = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001310
xiaohu.huang58292b32024-01-03 14:09:51 +08001311 /* A delay time of zero just forces a reschedule. */
1312 if( xTicksToDelay > ( TickType_t ) 0U )
1313 {
1314 configASSERT( uxSchedulerSuspended == 0 );
1315 vTaskSuspendAll();
1316 {
1317 traceTASK_DELAY();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001318
xiaohu.huang58292b32024-01-03 14:09:51 +08001319 /* A task that is removed from the event list while the
1320 * scheduler is suspended will not get placed in the ready
1321 * list or removed from the blocked list until the scheduler
1322 * is resumed.
1323 *
1324 * This task cannot be in an event list as it is the currently
1325 * executing task. */
1326 prvAddCurrentTaskToDelayedList( xTicksToDelay, pdFALSE );
1327 }
1328 xAlreadyYielded = xTaskResumeAll();
1329 }
1330 else
1331 {
1332 mtCOVERAGE_TEST_MARKER();
1333 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001334
xiaohu.huang58292b32024-01-03 14:09:51 +08001335 /* Force a reschedule if xTaskResumeAll has not already done so, we may
1336 * have put ourselves to sleep. */
1337 if( xAlreadyYielded == pdFALSE )
1338 {
1339 portYIELD_WITHIN_API();
1340 }
1341 else
1342 {
1343 mtCOVERAGE_TEST_MARKER();
1344 }
1345 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001346
1347#endif /* INCLUDE_vTaskDelay */
1348/*-----------------------------------------------------------*/
1349
xiaohu.huang58292b32024-01-03 14:09:51 +08001350#if ( ( INCLUDE_eTaskGetState == 1 ) || ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_xTaskAbortDelay == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001351
xiaohu.huang58292b32024-01-03 14:09:51 +08001352 eTaskState eTaskGetState( TaskHandle_t xTask )
1353 {
1354 eTaskState eReturn;
1355 List_t const * pxStateList;
1356 List_t const * pxDelayedList;
1357 List_t const * pxOverflowedDelayedList;
1358 const TCB_t * const pxTCB = xTask;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001359
xiaohu.huang58292b32024-01-03 14:09:51 +08001360 configASSERT( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001361
xiaohu.huang58292b32024-01-03 14:09:51 +08001362 if( pxTCB == pxCurrentTCB )
1363 {
1364 /* The task calling this function is querying its own state. */
1365 eReturn = eRunning;
1366 }
1367 else
1368 {
1369 taskENTER_CRITICAL();
1370 {
1371 pxStateList = listLIST_ITEM_CONTAINER( &( pxTCB->xStateListItem ) );
1372 pxDelayedList = pxDelayedTaskList;
1373 pxOverflowedDelayedList = pxOverflowDelayedTaskList;
1374 }
1375 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001376
xiaohu.huang58292b32024-01-03 14:09:51 +08001377 if( ( pxStateList == pxDelayedList ) || ( pxStateList == pxOverflowedDelayedList ) )
1378 {
1379 /* The task being queried is referenced from one of the Blocked
1380 * lists. */
1381 eReturn = eBlocked;
1382 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001383
xiaohu.huang58292b32024-01-03 14:09:51 +08001384 #if ( INCLUDE_vTaskSuspend == 1 )
1385 else if( pxStateList == &xSuspendedTaskList )
1386 {
1387 /* The task being queried is referenced from the suspended
1388 * list. Is it genuinely suspended or is it blocked
1389 * indefinitely? */
1390 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL )
1391 {
1392 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1393 {
1394 BaseType_t x;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001395
xiaohu.huang58292b32024-01-03 14:09:51 +08001396 /* The task does not appear on the event list item of
1397 * and of the RTOS objects, but could still be in the
1398 * blocked state if it is waiting on its notification
1399 * rather than waiting on an object. If not, is
1400 * suspended. */
1401 eReturn = eSuspended;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001402
xiaohu.huang58292b32024-01-03 14:09:51 +08001403 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1404 {
1405 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1406 {
1407 eReturn = eBlocked;
1408 break;
1409 }
1410 }
1411 }
1412 #else /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1413 {
1414 eReturn = eSuspended;
1415 }
1416 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1417 }
1418 else
1419 {
1420 eReturn = eBlocked;
1421 }
1422 }
1423 #endif /* if ( INCLUDE_vTaskSuspend == 1 ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001424
xiaohu.huang58292b32024-01-03 14:09:51 +08001425 #if ( INCLUDE_vTaskDelete == 1 )
1426 else if( ( pxStateList == &xTasksWaitingTermination ) || ( pxStateList == NULL ) )
1427 {
1428 /* The task being queried is referenced from the deleted
1429 * tasks list, or it is not referenced from any lists at
1430 * all. */
1431 eReturn = eDeleted;
1432 }
1433 #endif
1434
1435 else /*lint !e525 Negative indentation is intended to make use of pre-processor clearer. */
1436 {
1437 /* If the task is not in any other state, it must be in the
1438 * Ready (including pending ready) state. */
1439 eReturn = eReady;
1440 }
1441 }
1442
1443 return eReturn;
1444 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001445
1446#endif /* INCLUDE_eTaskGetState */
1447/*-----------------------------------------------------------*/
1448
1449#if ( INCLUDE_uxTaskPriorityGet == 1 )
1450
xiaohu.huang58292b32024-01-03 14:09:51 +08001451 UBaseType_t uxTaskPriorityGet( const TaskHandle_t xTask )
1452 {
1453 TCB_t const * pxTCB;
1454 UBaseType_t uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001455
xiaohu.huang58292b32024-01-03 14:09:51 +08001456 taskENTER_CRITICAL();
1457 {
1458 /* If null is passed in here then it is the priority of the task
1459 * that called uxTaskPriorityGet() that is being queried. */
1460 pxTCB = prvGetTCBFromHandle( xTask );
1461 uxReturn = pxTCB->uxPriority;
1462 }
1463 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001464
xiaohu.huang58292b32024-01-03 14:09:51 +08001465 return uxReturn;
1466 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001467
1468#endif /* INCLUDE_uxTaskPriorityGet */
1469/*-----------------------------------------------------------*/
1470
1471#if ( INCLUDE_uxTaskPriorityGet == 1 )
1472
xiaohu.huang58292b32024-01-03 14:09:51 +08001473 UBaseType_t uxTaskPriorityGetFromISR( const TaskHandle_t xTask )
1474 {
1475 TCB_t const * pxTCB;
1476 UBaseType_t uxReturn, uxSavedInterruptState;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001477
xiaohu.huang58292b32024-01-03 14:09:51 +08001478 /* RTOS ports that support interrupt nesting have the concept of a
1479 * maximum system call (or maximum API call) interrupt priority.
1480 * Interrupts that are above the maximum system call priority are keep
1481 * permanently enabled, even when the RTOS kernel is in a critical section,
1482 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
1483 * is defined in FreeRTOSConfig.h then
1484 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1485 * failure if a FreeRTOS API function is called from an interrupt that has
1486 * been assigned a priority above the configured maximum system call
1487 * priority. Only FreeRTOS functions that end in FromISR can be called
1488 * from interrupts that have been assigned a priority at or (logically)
1489 * below the maximum system call interrupt priority. FreeRTOS maintains a
1490 * separate interrupt safe API to ensure interrupt entry is as fast and as
1491 * simple as possible. More information (albeit Cortex-M specific) is
1492 * provided on the following link:
1493 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1494 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001495
xiaohu.huang58292b32024-01-03 14:09:51 +08001496 uxSavedInterruptState = portSET_INTERRUPT_MASK_FROM_ISR();
1497 {
1498 /* If null is passed in here then it is the priority of the calling
1499 * task that is being queried. */
1500 pxTCB = prvGetTCBFromHandle( xTask );
1501 uxReturn = pxTCB->uxPriority;
1502 }
1503 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptState );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001504
xiaohu.huang58292b32024-01-03 14:09:51 +08001505 return uxReturn;
1506 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001507
1508#endif /* INCLUDE_uxTaskPriorityGet */
1509/*-----------------------------------------------------------*/
1510
1511#if ( INCLUDE_vTaskPrioritySet == 1 )
1512
xiaohu.huang58292b32024-01-03 14:09:51 +08001513 void vTaskPrioritySet( TaskHandle_t xTask,
1514 UBaseType_t uxNewPriority )
1515 {
1516 TCB_t * pxTCB;
1517 UBaseType_t uxCurrentBasePriority, uxPriorityUsedOnEntry;
1518 BaseType_t xYieldRequired = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001519
xiaohu.huang58292b32024-01-03 14:09:51 +08001520 configASSERT( uxNewPriority < configMAX_PRIORITIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001521
xiaohu.huang58292b32024-01-03 14:09:51 +08001522 /* Ensure the new priority is valid. */
1523 if( uxNewPriority >= ( UBaseType_t ) configMAX_PRIORITIES )
1524 {
1525 uxNewPriority = ( UBaseType_t ) configMAX_PRIORITIES - ( UBaseType_t ) 1U;
1526 }
1527 else
1528 {
1529 mtCOVERAGE_TEST_MARKER();
1530 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001531
xiaohu.huang58292b32024-01-03 14:09:51 +08001532 taskENTER_CRITICAL();
1533 {
1534 /* If null is passed in here then it is the priority of the calling
1535 * task that is being changed. */
1536 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001537
xiaohu.huang58292b32024-01-03 14:09:51 +08001538 traceTASK_PRIORITY_SET( pxTCB, uxNewPriority );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001539
xiaohu.huang58292b32024-01-03 14:09:51 +08001540 #if ( configUSE_MUTEXES == 1 )
1541 {
1542 uxCurrentBasePriority = pxTCB->uxBasePriority;
1543 }
1544 #else
1545 {
1546 uxCurrentBasePriority = pxTCB->uxPriority;
1547 }
1548 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001549
xiaohu.huang58292b32024-01-03 14:09:51 +08001550 if( uxCurrentBasePriority != uxNewPriority )
1551 {
1552 /* The priority change may have readied a task of higher
1553 * priority than the calling task. */
1554 if( uxNewPriority > uxCurrentBasePriority )
1555 {
1556 if( pxTCB != pxCurrentTCB )
1557 {
1558 /* The priority of a task other than the currently
1559 * running task is being raised. Is the priority being
1560 * raised above that of the running task? */
1561 if( uxNewPriority >= pxCurrentTCB->uxPriority )
1562 {
1563 xYieldRequired = pdTRUE;
1564 }
1565 else
1566 {
1567 mtCOVERAGE_TEST_MARKER();
1568 }
1569 }
1570 else
1571 {
1572 /* The priority of the running task is being raised,
1573 * but the running task must already be the highest
1574 * priority task able to run so no yield is required. */
1575 }
1576 }
1577 else if( pxTCB == pxCurrentTCB )
1578 {
1579 /* Setting the priority of the running task down means
1580 * there may now be another task of higher priority that
1581 * is ready to execute. */
1582 xYieldRequired = pdTRUE;
1583 }
1584 else
1585 {
1586 /* Setting the priority of any other task down does not
1587 * require a yield as the running task must be above the
1588 * new priority of the task being modified. */
1589 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001590
xiaohu.huang58292b32024-01-03 14:09:51 +08001591 /* Remember the ready list the task might be referenced from
1592 * before its uxPriority member is changed so the
1593 * taskRESET_READY_PRIORITY() macro can function correctly. */
1594 uxPriorityUsedOnEntry = pxTCB->uxPriority;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001595
xiaohu.huang58292b32024-01-03 14:09:51 +08001596 #if ( configUSE_MUTEXES == 1 )
1597 {
1598 /* Only change the priority being used if the task is not
1599 * currently using an inherited priority. */
1600 if( pxTCB->uxBasePriority == pxTCB->uxPriority )
1601 {
1602 pxTCB->uxPriority = uxNewPriority;
1603 }
1604 else
1605 {
1606 mtCOVERAGE_TEST_MARKER();
1607 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001608
xiaohu.huang58292b32024-01-03 14:09:51 +08001609 /* The base priority gets set whatever. */
1610 pxTCB->uxBasePriority = uxNewPriority;
1611 }
1612 #else /* if ( configUSE_MUTEXES == 1 ) */
1613 {
1614 pxTCB->uxPriority = uxNewPriority;
1615 }
1616 #endif /* if ( configUSE_MUTEXES == 1 ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001617
xiaohu.huang58292b32024-01-03 14:09:51 +08001618 /* Only reset the event list item value if the value is not
1619 * being used for anything else. */
1620 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
1621 {
1622 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. */
1623 }
1624 else
1625 {
1626 mtCOVERAGE_TEST_MARKER();
1627 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001628
xiaohu.huang58292b32024-01-03 14:09:51 +08001629 /* If the task is in the blocked or suspended list we need do
1630 * nothing more than change its priority variable. However, if
1631 * the task is in a ready list it needs to be removed and placed
1632 * in the list appropriate to its new priority. */
1633 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
1634 {
1635 /* The task is currently in its ready list - remove before
1636 * adding it to its new ready list. As we are in a critical
1637 * section we can do this even if the scheduler is suspended. */
1638 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1639 {
1640 /* It is known that the task is in its ready list so
1641 * there is no need to check again and the port level
1642 * reset macro can be called directly. */
1643 portRESET_READY_PRIORITY( uxPriorityUsedOnEntry, uxTopReadyPriority );
1644 }
1645 else
1646 {
1647 mtCOVERAGE_TEST_MARKER();
1648 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001649
xiaohu.huang58292b32024-01-03 14:09:51 +08001650 prvAddTaskToReadyList( pxTCB );
1651 }
1652 else
1653 {
1654 mtCOVERAGE_TEST_MARKER();
1655 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001656
xiaohu.huang58292b32024-01-03 14:09:51 +08001657 if( xYieldRequired != pdFALSE )
1658 {
1659 taskYIELD_IF_USING_PREEMPTION();
1660 }
1661 else
1662 {
1663 mtCOVERAGE_TEST_MARKER();
1664 }
1665
1666 /* Remove compiler warning about unused variables when the port
1667 * optimised task selection is not being used. */
1668 ( void ) uxPriorityUsedOnEntry;
1669 }
1670 }
1671 taskEXIT_CRITICAL();
1672 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001673
1674#endif /* INCLUDE_vTaskPrioritySet */
1675/*-----------------------------------------------------------*/
1676
1677#if ( INCLUDE_vTaskSuspend == 1 )
1678
xiaohu.huang58292b32024-01-03 14:09:51 +08001679 void vTaskSuspend( TaskHandle_t xTaskToSuspend )
1680 {
1681 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001682
xiaohu.huang58292b32024-01-03 14:09:51 +08001683 taskENTER_CRITICAL();
1684 {
1685 /* If null is passed in here then it is the running task that is
1686 * being suspended. */
1687 pxTCB = prvGetTCBFromHandle( xTaskToSuspend );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001688
xiaohu.huang58292b32024-01-03 14:09:51 +08001689 traceTASK_SUSPEND( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001690
xiaohu.huang58292b32024-01-03 14:09:51 +08001691 /* Remove task from the ready/delayed list and place in the
1692 * suspended list. */
1693 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
1694 {
1695 taskRESET_READY_PRIORITY( pxTCB->uxPriority );
1696 }
1697 else
1698 {
1699 mtCOVERAGE_TEST_MARKER();
1700 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001701
xiaohu.huang58292b32024-01-03 14:09:51 +08001702 /* Is the task waiting on an event also? */
1703 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
1704 {
1705 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
1706 }
1707 else
1708 {
1709 mtCOVERAGE_TEST_MARKER();
1710 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001711
xiaohu.huang58292b32024-01-03 14:09:51 +08001712 vListInsertEnd( &xSuspendedTaskList, &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001713
xiaohu.huang58292b32024-01-03 14:09:51 +08001714 #if ( configUSE_TASK_NOTIFICATIONS == 1 )
1715 {
1716 BaseType_t x;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001717
xiaohu.huang58292b32024-01-03 14:09:51 +08001718 for( x = 0; x < configTASK_NOTIFICATION_ARRAY_ENTRIES; x++ )
1719 {
1720 if( pxTCB->ucNotifyState[ x ] == taskWAITING_NOTIFICATION )
1721 {
1722 /* The task was blocked to wait for a notification, but is
1723 * now suspended, so no notification was received. */
1724 pxTCB->ucNotifyState[ x ] = taskNOT_WAITING_NOTIFICATION;
1725 }
1726 }
1727 }
1728 #endif /* if ( configUSE_TASK_NOTIFICATIONS == 1 ) */
1729 }
1730 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001731
xiaohu.huang58292b32024-01-03 14:09:51 +08001732 if( xSchedulerRunning != pdFALSE )
1733 {
1734 /* Reset the next expected unblock time in case it referred to the
1735 * task that is now in the Suspended state. */
1736 taskENTER_CRITICAL();
1737 {
1738 prvResetNextTaskUnblockTime();
1739 }
1740 taskEXIT_CRITICAL();
1741 }
1742 else
1743 {
1744 mtCOVERAGE_TEST_MARKER();
1745 }
1746
1747 if( pxTCB == pxCurrentTCB )
1748 {
1749 if( xSchedulerRunning != pdFALSE )
1750 {
1751 /* The current task has just been suspended. */
1752 configASSERT( uxSchedulerSuspended == 0 );
1753 portYIELD_WITHIN_API();
1754 }
1755 else
1756 {
1757 /* The scheduler is not running, but the task that was pointed
1758 * to by pxCurrentTCB has just been suspended and pxCurrentTCB
1759 * must be adjusted to point to a different task. */
1760 if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == uxCurrentNumberOfTasks ) /*lint !e931 Right has no side effect, just volatile. */
1761 {
1762 /* No other tasks are ready, so set pxCurrentTCB back to
1763 * NULL so when the next task is created pxCurrentTCB will
1764 * be set to point to it no matter what its relative priority
1765 * is. */
1766 pxCurrentTCB = NULL;
1767 }
1768 else
1769 {
1770 vTaskSwitchContext();
1771 }
1772 }
1773 }
1774 else
1775 {
1776 mtCOVERAGE_TEST_MARKER();
1777 }
1778 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001779
1780#endif /* INCLUDE_vTaskSuspend */
1781/*-----------------------------------------------------------*/
1782
1783#if ( INCLUDE_vTaskSuspend == 1 )
1784
xiaohu.huang58292b32024-01-03 14:09:51 +08001785 static BaseType_t prvTaskIsTaskSuspended( const TaskHandle_t xTask )
1786 {
1787 BaseType_t xReturn = pdFALSE;
1788 const TCB_t * const pxTCB = xTask;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001789
xiaohu.huang58292b32024-01-03 14:09:51 +08001790 /* Accesses xPendingReadyList so must be called from a critical
1791 * section. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001792
xiaohu.huang58292b32024-01-03 14:09:51 +08001793 /* It does not make sense to check if the calling task is suspended. */
1794 configASSERT( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001795
xiaohu.huang58292b32024-01-03 14:09:51 +08001796 /* Is the task being resumed actually in the suspended list? */
1797 if( listIS_CONTAINED_WITHIN( &xSuspendedTaskList, &( pxTCB->xStateListItem ) ) != pdFALSE )
1798 {
1799 /* Has the task already been resumed from within an ISR? */
1800 if( listIS_CONTAINED_WITHIN( &xPendingReadyList, &( pxTCB->xEventListItem ) ) == pdFALSE )
1801 {
1802 /* Is it in the suspended list because it is in the Suspended
1803 * state, or because is is blocked with no timeout? */
1804 if( listIS_CONTAINED_WITHIN( NULL, &( pxTCB->xEventListItem ) ) != pdFALSE ) /*lint !e961. The cast is only redundant when NULL is used. */
1805 {
1806 xReturn = pdTRUE;
1807 }
1808 else
1809 {
1810 mtCOVERAGE_TEST_MARKER();
1811 }
1812 }
1813 else
1814 {
1815 mtCOVERAGE_TEST_MARKER();
1816 }
1817 }
1818 else
1819 {
1820 mtCOVERAGE_TEST_MARKER();
1821 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001822
xiaohu.huang58292b32024-01-03 14:09:51 +08001823 return xReturn;
1824 } /*lint !e818 xTask cannot be a pointer to const because it is a typedef. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001825
1826#endif /* INCLUDE_vTaskSuspend */
1827/*-----------------------------------------------------------*/
1828
1829#if ( INCLUDE_vTaskSuspend == 1 )
1830
xiaohu.huang58292b32024-01-03 14:09:51 +08001831 void vTaskResume( TaskHandle_t xTaskToResume )
1832 {
1833 TCB_t * const pxTCB = xTaskToResume;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001834
xiaohu.huang58292b32024-01-03 14:09:51 +08001835 /* It does not make sense to resume the calling task. */
1836 configASSERT( xTaskToResume );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001837
xiaohu.huang58292b32024-01-03 14:09:51 +08001838 /* The parameter cannot be NULL as it is impossible to resume the
1839 * currently executing task. */
1840 if( ( pxTCB != pxCurrentTCB ) && ( pxTCB != NULL ) )
1841 {
1842 taskENTER_CRITICAL();
1843 {
1844 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
1845 {
1846 traceTASK_RESUME( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001847
xiaohu.huang58292b32024-01-03 14:09:51 +08001848 /* The ready list can be accessed even if the scheduler is
1849 * suspended because this is inside a critical section. */
1850 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
1851 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001852
xiaohu.huang58292b32024-01-03 14:09:51 +08001853 /* A higher priority task may have just been resumed. */
1854 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
1855 {
1856 /* This yield may not cause the task just resumed to run,
1857 * but will leave the lists in the correct state for the
1858 * next yield. */
1859 taskYIELD_IF_USING_PREEMPTION();
1860 }
1861 else
1862 {
1863 mtCOVERAGE_TEST_MARKER();
1864 }
1865 }
1866 else
1867 {
1868 mtCOVERAGE_TEST_MARKER();
1869 }
1870 }
1871 taskEXIT_CRITICAL();
1872 }
1873 else
1874 {
1875 mtCOVERAGE_TEST_MARKER();
1876 }
1877 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001878
1879#endif /* INCLUDE_vTaskSuspend */
1880
1881/*-----------------------------------------------------------*/
1882
1883#if ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )
1884
xiaohu.huang58292b32024-01-03 14:09:51 +08001885 BaseType_t xTaskResumeFromISR( TaskHandle_t xTaskToResume )
1886 {
1887 BaseType_t xYieldRequired = pdFALSE;
1888 TCB_t * const pxTCB = xTaskToResume;
1889 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001890
xiaohu.huang58292b32024-01-03 14:09:51 +08001891 configASSERT( xTaskToResume );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001892
xiaohu.huang58292b32024-01-03 14:09:51 +08001893 /* RTOS ports that support interrupt nesting have the concept of a
1894 * maximum system call (or maximum API call) interrupt priority.
1895 * Interrupts that are above the maximum system call priority are keep
1896 * permanently enabled, even when the RTOS kernel is in a critical section,
1897 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
1898 * is defined in FreeRTOSConfig.h then
1899 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
1900 * failure if a FreeRTOS API function is called from an interrupt that has
1901 * been assigned a priority above the configured maximum system call
1902 * priority. Only FreeRTOS functions that end in FromISR can be called
1903 * from interrupts that have been assigned a priority at or (logically)
1904 * below the maximum system call interrupt priority. FreeRTOS maintains a
1905 * separate interrupt safe API to ensure interrupt entry is as fast and as
1906 * simple as possible. More information (albeit Cortex-M specific) is
1907 * provided on the following link:
1908 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
1909 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001910
xiaohu.huang58292b32024-01-03 14:09:51 +08001911 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
1912 {
1913 if( prvTaskIsTaskSuspended( pxTCB ) != pdFALSE )
1914 {
1915 traceTASK_RESUME_FROM_ISR( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001916
xiaohu.huang58292b32024-01-03 14:09:51 +08001917 /* Check the ready lists can be accessed. */
1918 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
1919 {
1920 /* Ready lists can be accessed so move the task from the
1921 * suspended list to the ready list directly. */
1922 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
1923 {
1924 xYieldRequired = pdTRUE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001925
xiaohu.huang58292b32024-01-03 14:09:51 +08001926 /* Mark that a yield is pending in case the user is not
1927 * using the return value to initiate a context switch
1928 * from the ISR using portYIELD_FROM_ISR. */
1929 xYieldPending = pdTRUE;
1930 }
1931 else
1932 {
1933 mtCOVERAGE_TEST_MARKER();
1934 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001935
xiaohu.huang58292b32024-01-03 14:09:51 +08001936 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
1937 prvAddTaskToReadyList( pxTCB );
1938 }
1939 else
1940 {
1941 /* The delayed or ready lists cannot be accessed so the task
1942 * is held in the pending ready list until the scheduler is
1943 * unsuspended. */
1944 vListInsertEnd( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
1945 }
1946 }
1947 else
1948 {
1949 mtCOVERAGE_TEST_MARKER();
1950 }
1951 }
1952 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
1953
1954 return xYieldRequired;
1955 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001956
1957#endif /* ( ( INCLUDE_xTaskResumeFromISR == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) ) */
1958/*-----------------------------------------------------------*/
1959
1960void vTaskStartScheduler( void )
1961{
xiaohu.huang58292b32024-01-03 14:09:51 +08001962 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001963
xiaohu.huang58292b32024-01-03 14:09:51 +08001964 /* Add the idle task at the lowest priority. */
1965 #if ( configSUPPORT_STATIC_ALLOCATION == 1 )
1966 {
1967 StaticTask_t * pxIdleTaskTCBBuffer = NULL;
1968 StackType_t * pxIdleTaskStackBuffer = NULL;
xiaohu.huang79d92512024-02-21 14:33:03 +08001969 configSTACK_DEPTH_TYPE ulIdleTaskStackSize;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08001970
xiaohu.huang58292b32024-01-03 14:09:51 +08001971 /* The Idle task is created using user provided RAM - obtain the
1972 * address of the RAM then create the idle task. */
1973 vApplicationGetIdleTaskMemory( &pxIdleTaskTCBBuffer, &pxIdleTaskStackBuffer, &ulIdleTaskStackSize );
1974 xIdleTaskHandle = xTaskCreateStatic( prvIdleTask,
1975 configIDLE_TASK_NAME,
1976 ulIdleTaskStackSize,
1977 ( void * ) NULL, /*lint !e961. The cast is not redundant for all compilers. */
1978 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
1979 pxIdleTaskStackBuffer,
1980 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 +08001981
xiaohu.huang58292b32024-01-03 14:09:51 +08001982 if( xIdleTaskHandle != NULL )
1983 {
1984 xReturn = pdPASS;
1985 }
1986 else
1987 {
1988 xReturn = pdFAIL;
1989 }
1990 }
1991 #else /* if ( configSUPPORT_STATIC_ALLOCATION == 1 ) */
1992 {
1993 /* The Idle task is being created using dynamically allocated RAM. */
1994 xReturn = xTaskCreate( prvIdleTask,
1995 configIDLE_TASK_NAME,
1996 configMINIMAL_STACK_SIZE,
1997 ( void * ) NULL,
1998 portPRIVILEGE_BIT, /* In effect ( tskIDLE_PRIORITY | portPRIVILEGE_BIT ), but tskIDLE_PRIORITY is zero. */
1999 &xIdleTaskHandle ); /*lint !e961 MISRA exception, justified as it is not a redundant explicit cast to all supported compilers. */
2000 }
2001 #endif /* configSUPPORT_STATIC_ALLOCATION */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002002
xiaohu.huang58292b32024-01-03 14:09:51 +08002003 #if ( configUSE_TIMERS == 1 )
2004 {
2005 if( xReturn == pdPASS )
2006 {
2007 xReturn = xTimerCreateTimerTask();
2008 }
2009 else
2010 {
2011 mtCOVERAGE_TEST_MARKER();
2012 }
2013 }
2014 #endif /* configUSE_TIMERS */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002015
xiaohu.huang58292b32024-01-03 14:09:51 +08002016 if( xReturn == pdPASS )
2017 {
2018 /* freertos_tasks_c_additions_init() should only be called if the user
2019 * definable macro FREERTOS_TASKS_C_ADDITIONS_INIT() is defined, as that is
2020 * the only macro called by the function. */
2021 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
2022 {
2023 freertos_tasks_c_additions_init();
2024 }
2025 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002026
xiaohu.huang58292b32024-01-03 14:09:51 +08002027 /* Interrupts are turned off here, to ensure a tick does not occur
2028 * before or during the call to xPortStartScheduler(). The stacks of
2029 * the created tasks contain a status word with interrupts switched on
2030 * so interrupts will automatically get re-enabled when the first task
2031 * starts to run. */
2032 portDISABLE_INTERRUPTS();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002033
xiaohu.huang58292b32024-01-03 14:09:51 +08002034 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
2035 {
2036 /* Switch C-Runtime's TLS Block to point to the TLS
2037 * block specific to the task that will run first. */
2038 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
2039 }
2040 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002041
xiaohu.huang58292b32024-01-03 14:09:51 +08002042 xNextTaskUnblockTime = portMAX_DELAY;
2043 xSchedulerRunning = pdTRUE;
2044 xTickCount = ( TickType_t ) configINITIAL_TICK_COUNT;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002045
xiaohu.huang58292b32024-01-03 14:09:51 +08002046 /* If configGENERATE_RUN_TIME_STATS is defined then the following
2047 * macro must be defined to configure the timer/counter used to generate
2048 * the run time counter time base. NOTE: If configGENERATE_RUN_TIME_STATS
2049 * is set to 0 and the following line fails to build then ensure you do not
2050 * have portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() defined in your
2051 * FreeRTOSConfig.h file. */
2052 portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002053
xiaohu.huang58292b32024-01-03 14:09:51 +08002054 traceTASK_SWITCHED_IN();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002055
xiaohu.huang58292b32024-01-03 14:09:51 +08002056 /* Setting up the timer tick is hardware specific and thus in the
2057 * portable interface. */
2058 xPortStartScheduler();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002059
xiaohu.huang58292b32024-01-03 14:09:51 +08002060 /* In most cases, xPortStartScheduler() will not return. If it
2061 * returns pdTRUE then there was not enough heap memory available
2062 * to create either the Idle or the Timer task. If it returned
2063 * pdFALSE, then the application called xTaskEndScheduler().
2064 * Most ports don't implement xTaskEndScheduler() as there is
2065 * nothing to return to. */
2066 }
2067 else
2068 {
2069 /* This line will only be reached if the kernel could not be started,
2070 * because there was not enough FreeRTOS heap to create the idle task
2071 * or the timer task. */
2072 configASSERT( xReturn != errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY );
2073 }
2074
2075 /* Prevent compiler warnings if INCLUDE_xTaskGetIdleTaskHandle is set to 0,
2076 * meaning xIdleTaskHandle is not used anywhere else. */
2077 ( void ) xIdleTaskHandle;
2078
2079 /* OpenOCD makes use of uxTopUsedPriority for thread debugging. Prevent uxTopUsedPriority
2080 * from getting optimized out as it is no longer used by the kernel. */
2081 ( void ) uxTopUsedPriority;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002082}
2083/*-----------------------------------------------------------*/
2084
2085void vTaskEndScheduler( void )
2086{
xiaohu.huang58292b32024-01-03 14:09:51 +08002087 /* Stop the scheduler interrupts and call the portable scheduler end
2088 * routine so the original ISRs can be restored if necessary. The port
2089 * layer must ensure interrupts enable bit is left in the correct state. */
2090 portDISABLE_INTERRUPTS();
2091 xSchedulerRunning = pdFALSE;
2092 vPortEndScheduler();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002093}
2094/*----------------------------------------------------------*/
2095
2096void vTaskSuspendAll( void )
2097{
xiaohu.huang58292b32024-01-03 14:09:51 +08002098 /* A critical section is not required as the variable is of type
2099 * BaseType_t. Please read Richard Barry's reply in the following link to a
2100 * post in the FreeRTOS support forum before reporting this as a bug! -
2101 * https://goo.gl/wu4acr */
2102
2103 /* portSOFTWARE_BARRIER() is only implemented for emulated/simulated ports that
2104 * do not otherwise exhibit real time behaviour. */
2105 portSOFTWARE_BARRIER();
2106
2107 /* The scheduler is suspended if uxSchedulerSuspended is non-zero. An increment
2108 * is used to allow calls to vTaskSuspendAll() to nest. */
2109 ++uxSchedulerSuspended;
2110
2111 /* Enforces ordering for ports and optimised compilers that may otherwise place
2112 * the above increment elsewhere. */
2113 portMEMORY_BARRIER();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002114}
2115/*----------------------------------------------------------*/
2116
2117#if ( configUSE_TICKLESS_IDLE != 0 )
2118
xiaohu.huang58292b32024-01-03 14:09:51 +08002119 static TickType_t prvGetExpectedIdleTime( void )
2120 {
2121 TickType_t xReturn;
2122 UBaseType_t uxHigherPriorityReadyTasks = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002123
xiaohu.huang58292b32024-01-03 14:09:51 +08002124 /* uxHigherPriorityReadyTasks takes care of the case where
2125 * configUSE_PREEMPTION is 0, so there may be tasks above the idle priority
2126 * task that are in the Ready state, even though the idle task is
2127 * running. */
2128 #if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 )
2129 {
2130 if( uxTopReadyPriority > tskIDLE_PRIORITY )
2131 {
2132 uxHigherPriorityReadyTasks = pdTRUE;
2133 }
2134 }
2135 #else
2136 {
2137 const UBaseType_t uxLeastSignificantBit = ( UBaseType_t ) 0x01;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002138
xiaohu.huang58292b32024-01-03 14:09:51 +08002139 /* When port optimised task selection is used the uxTopReadyPriority
2140 * variable is used as a bit map. If bits other than the least
2141 * significant bit are set then there are tasks that have a priority
2142 * above the idle priority that are in the Ready state. This takes
2143 * care of the case where the co-operative scheduler is in use. */
2144 if( uxTopReadyPriority > uxLeastSignificantBit )
2145 {
2146 uxHigherPriorityReadyTasks = pdTRUE;
2147 }
2148 }
2149 #endif /* if ( configUSE_PORT_OPTIMISED_TASK_SELECTION == 0 ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002150
xiaohu.huang58292b32024-01-03 14:09:51 +08002151 if( pxCurrentTCB->uxPriority > tskIDLE_PRIORITY )
2152 {
2153 xReturn = 0;
2154 }
2155 else if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > 1 )
2156 {
2157 /* There are other idle priority tasks in the ready state. If
2158 * time slicing is used then the very next tick interrupt must be
2159 * processed. */
2160 xReturn = 0;
2161 }
2162 else if( uxHigherPriorityReadyTasks != pdFALSE )
2163 {
2164 /* There are tasks in the Ready state that have a priority above the
2165 * idle priority. This path can only be reached if
2166 * configUSE_PREEMPTION is 0. */
2167 xReturn = 0;
2168 }
2169 else
2170 {
2171 xReturn = xNextTaskUnblockTime - xTickCount;
2172 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002173
xiaohu.huang58292b32024-01-03 14:09:51 +08002174 return xReturn;
2175 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002176
2177#endif /* configUSE_TICKLESS_IDLE */
2178/*----------------------------------------------------------*/
2179
2180BaseType_t xTaskResumeAll( void )
2181{
xiaohu.huang58292b32024-01-03 14:09:51 +08002182 TCB_t * pxTCB = NULL;
2183 BaseType_t xAlreadyYielded = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002184
xiaohu.huang58292b32024-01-03 14:09:51 +08002185 /* If uxSchedulerSuspended is zero then this function does not match a
2186 * previous call to vTaskSuspendAll(). */
2187 configASSERT( uxSchedulerSuspended );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002188
xiaohu.huang58292b32024-01-03 14:09:51 +08002189 /* It is possible that an ISR caused a task to be removed from an event
2190 * list while the scheduler was suspended. If this was the case then the
2191 * removed task will have been added to the xPendingReadyList. Once the
2192 * scheduler has been resumed it is safe to move all the pending ready
2193 * tasks from this list into their appropriate ready list. */
2194 taskENTER_CRITICAL();
2195 {
2196 --uxSchedulerSuspended;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002197
xiaohu.huang58292b32024-01-03 14:09:51 +08002198 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2199 {
2200 if( uxCurrentNumberOfTasks > ( UBaseType_t ) 0U )
2201 {
2202 /* Move any readied tasks from the pending list into the
2203 * appropriate ready list. */
2204 while( listLIST_IS_EMPTY( &xPendingReadyList ) == pdFALSE )
2205 {
2206 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. */
2207 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
2208 portMEMORY_BARRIER();
2209 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
2210 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002211
xiaohu.huang58292b32024-01-03 14:09:51 +08002212 /* If the moved task has a priority higher than or equal to
2213 * the current task then a yield must be performed. */
2214 if( pxTCB->uxPriority >= pxCurrentTCB->uxPriority )
2215 {
2216 xYieldPending = pdTRUE;
2217 }
2218 else
2219 {
2220 mtCOVERAGE_TEST_MARKER();
2221 }
2222 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002223
xiaohu.huang58292b32024-01-03 14:09:51 +08002224 if( pxTCB != NULL )
2225 {
2226 /* A task was unblocked while the scheduler was suspended,
2227 * which may have prevented the next unblock time from being
2228 * re-calculated, in which case re-calculate it now. Mainly
2229 * important for low power tickless implementations, where
2230 * this can prevent an unnecessary exit from low power
2231 * state. */
2232 prvResetNextTaskUnblockTime();
2233 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002234
xiaohu.huang58292b32024-01-03 14:09:51 +08002235 /* If any ticks occurred while the scheduler was suspended then
2236 * they should be processed now. This ensures the tick count does
2237 * not slip, and that any delayed tasks are resumed at the correct
2238 * time. */
2239 {
2240 TickType_t xPendedCounts = xPendedTicks; /* Non-volatile copy. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002241
xiaohu.huang58292b32024-01-03 14:09:51 +08002242 if( xPendedCounts > ( TickType_t ) 0U )
2243 {
2244 do
2245 {
2246 if( xTaskIncrementTick() != pdFALSE )
2247 {
2248 xYieldPending = pdTRUE;
2249 }
2250 else
2251 {
2252 mtCOVERAGE_TEST_MARKER();
2253 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002254
xiaohu.huang58292b32024-01-03 14:09:51 +08002255 --xPendedCounts;
2256 } while( xPendedCounts > ( TickType_t ) 0U );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002257
xiaohu.huang58292b32024-01-03 14:09:51 +08002258 xPendedTicks = 0;
2259 }
2260 else
2261 {
2262 mtCOVERAGE_TEST_MARKER();
2263 }
2264 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002265
xiaohu.huang58292b32024-01-03 14:09:51 +08002266 if( xYieldPending != pdFALSE )
2267 {
2268 #if ( configUSE_PREEMPTION != 0 )
2269 {
2270 xAlreadyYielded = pdTRUE;
2271 }
2272 #endif
2273 taskYIELD_IF_USING_PREEMPTION();
2274 }
2275 else
2276 {
2277 mtCOVERAGE_TEST_MARKER();
2278 }
2279 }
2280 }
2281 else
2282 {
2283 mtCOVERAGE_TEST_MARKER();
2284 }
2285 }
2286 taskEXIT_CRITICAL();
2287
2288 return xAlreadyYielded;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002289}
2290/*-----------------------------------------------------------*/
2291
2292TickType_t xTaskGetTickCount( void )
2293{
xiaohu.huang58292b32024-01-03 14:09:51 +08002294 TickType_t xTicks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002295
xiaohu.huang58292b32024-01-03 14:09:51 +08002296 /* Critical section required if running on a 16 bit processor. */
2297 portTICK_TYPE_ENTER_CRITICAL();
2298 {
2299 xTicks = xTickCount;
2300 }
2301 portTICK_TYPE_EXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002302
xiaohu.huang58292b32024-01-03 14:09:51 +08002303 return xTicks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002304}
2305/*-----------------------------------------------------------*/
2306
2307TickType_t xTaskGetTickCountFromISR( void )
2308{
xiaohu.huang58292b32024-01-03 14:09:51 +08002309 TickType_t xReturn;
2310 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002311
xiaohu.huang58292b32024-01-03 14:09:51 +08002312 /* RTOS ports that support interrupt nesting have the concept of a maximum
2313 * system call (or maximum API call) interrupt priority. Interrupts that are
2314 * above the maximum system call priority are kept permanently enabled, even
2315 * when the RTOS kernel is in a critical section, but cannot make any calls to
2316 * FreeRTOS API functions. If configASSERT() is defined in FreeRTOSConfig.h
2317 * then portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
2318 * failure if a FreeRTOS API function is called from an interrupt that has been
2319 * assigned a priority above the configured maximum system call priority.
2320 * Only FreeRTOS functions that end in FromISR can be called from interrupts
2321 * that have been assigned a priority at or (logically) below the maximum
2322 * system call interrupt priority. FreeRTOS maintains a separate interrupt
2323 * safe API to ensure interrupt entry is as fast and as simple as possible.
2324 * More information (albeit Cortex-M specific) is provided on the following
2325 * link: https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
2326 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002327
xiaohu.huang58292b32024-01-03 14:09:51 +08002328 uxSavedInterruptStatus = portTICK_TYPE_SET_INTERRUPT_MASK_FROM_ISR();
2329 {
2330 xReturn = xTickCount;
2331 }
2332 portTICK_TYPE_CLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002333
xiaohu.huang58292b32024-01-03 14:09:51 +08002334 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002335}
2336/*-----------------------------------------------------------*/
2337
2338UBaseType_t uxTaskGetNumberOfTasks( void )
2339{
xiaohu.huang58292b32024-01-03 14:09:51 +08002340 /* A critical section is not required because the variables are of type
2341 * BaseType_t. */
2342 return uxCurrentNumberOfTasks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002343}
2344/*-----------------------------------------------------------*/
2345
xiaohu.huang58292b32024-01-03 14:09:51 +08002346char * pcTaskGetName( TaskHandle_t xTaskToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002347{
xiaohu.huang58292b32024-01-03 14:09:51 +08002348 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002349
xiaohu.huang58292b32024-01-03 14:09:51 +08002350 /* If null is passed in here then the name of the calling task is being
2351 * queried. */
2352 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
2353 configASSERT( pxTCB );
2354 return &( pxTCB->pcTaskName[ 0 ] );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002355}
xiaohu.huang58292b32024-01-03 14:09:51 +08002356/*-----------------------------------------------------------*/
Kelvin Zhang7f929772021-12-31 17:58:17 +08002357
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002358#if ( INCLUDE_xTaskGetHandle == 1 )
2359
xiaohu.huang58292b32024-01-03 14:09:51 +08002360 static TCB_t * prvSearchForNameWithinSingleList( List_t * pxList,
2361 const char pcNameToQuery[] )
2362 {
2363 TCB_t * pxNextTCB;
2364 TCB_t * pxFirstTCB;
2365 TCB_t * pxReturn = NULL;
2366 UBaseType_t x;
2367 char cNextChar;
2368 BaseType_t xBreakLoop;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002369
xiaohu.huang58292b32024-01-03 14:09:51 +08002370 /* This function is called with the scheduler suspended. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002371
xiaohu.huang58292b32024-01-03 14:09:51 +08002372 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
2373 {
2374 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 +08002375
xiaohu.huang58292b32024-01-03 14:09:51 +08002376 do
2377 {
2378 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 +08002379
xiaohu.huang58292b32024-01-03 14:09:51 +08002380 /* Check each character in the name looking for a match or
2381 * mismatch. */
2382 xBreakLoop = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002383
xiaohu.huang58292b32024-01-03 14:09:51 +08002384 for( x = ( UBaseType_t ) 0; x < ( UBaseType_t ) configMAX_TASK_NAME_LEN; x++ )
2385 {
2386 cNextChar = pxNextTCB->pcTaskName[ x ];
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002387
xiaohu.huang58292b32024-01-03 14:09:51 +08002388 if( cNextChar != pcNameToQuery[ x ] )
2389 {
2390 /* Characters didn't match. */
2391 xBreakLoop = pdTRUE;
2392 }
2393 else if( cNextChar == ( char ) 0x00 )
2394 {
2395 /* Both strings terminated, a match must have been
2396 * found. */
2397 pxReturn = pxNextTCB;
2398 xBreakLoop = pdTRUE;
2399 }
2400 else
2401 {
2402 mtCOVERAGE_TEST_MARKER();
2403 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002404
xiaohu.huang58292b32024-01-03 14:09:51 +08002405 if( xBreakLoop != pdFALSE )
2406 {
2407 break;
2408 }
2409 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002410
xiaohu.huang58292b32024-01-03 14:09:51 +08002411 if( pxReturn != NULL )
2412 {
2413 /* The handle has been found. */
2414 break;
2415 }
2416 } while( pxNextTCB != pxFirstTCB );
2417 }
2418 else
2419 {
2420 mtCOVERAGE_TEST_MARKER();
2421 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002422
xiaohu.huang58292b32024-01-03 14:09:51 +08002423 return pxReturn;
2424 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002425
2426#endif /* INCLUDE_xTaskGetHandle */
2427/*-----------------------------------------------------------*/
2428
2429#if ( INCLUDE_xTaskGetHandle == 1 )
2430
xiaohu.huang58292b32024-01-03 14:09:51 +08002431 TaskHandle_t xTaskGetHandle( const char * pcNameToQuery ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
2432 {
2433 UBaseType_t uxQueue = configMAX_PRIORITIES;
2434 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002435
xiaohu.huang58292b32024-01-03 14:09:51 +08002436 /* Task names will be truncated to configMAX_TASK_NAME_LEN - 1 bytes. */
2437 configASSERT( strlen( pcNameToQuery ) < configMAX_TASK_NAME_LEN );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002438
xiaohu.huang58292b32024-01-03 14:09:51 +08002439 vTaskSuspendAll();
2440 {
2441 /* Search the ready lists. */
2442 do
2443 {
2444 uxQueue--;
2445 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) &( pxReadyTasksLists[ uxQueue ] ), pcNameToQuery );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002446
xiaohu.huang58292b32024-01-03 14:09:51 +08002447 if( pxTCB != NULL )
2448 {
2449 /* Found the handle. */
2450 break;
2451 }
2452 } 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 +08002453
xiaohu.huang58292b32024-01-03 14:09:51 +08002454 /* Search the delayed lists. */
2455 if( pxTCB == NULL )
2456 {
2457 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxDelayedTaskList, pcNameToQuery );
2458 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002459
xiaohu.huang58292b32024-01-03 14:09:51 +08002460 if( pxTCB == NULL )
2461 {
2462 pxTCB = prvSearchForNameWithinSingleList( ( List_t * ) pxOverflowDelayedTaskList, pcNameToQuery );
2463 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002464
xiaohu.huang58292b32024-01-03 14:09:51 +08002465 #if ( INCLUDE_vTaskSuspend == 1 )
2466 {
2467 if( pxTCB == NULL )
2468 {
2469 /* Search the suspended list. */
2470 pxTCB = prvSearchForNameWithinSingleList( &xSuspendedTaskList, pcNameToQuery );
2471 }
2472 }
2473 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002474
xiaohu.huang58292b32024-01-03 14:09:51 +08002475 #if ( INCLUDE_vTaskDelete == 1 )
2476 {
2477 if( pxTCB == NULL )
2478 {
2479 /* Search the deleted list. */
2480 pxTCB = prvSearchForNameWithinSingleList( &xTasksWaitingTermination, pcNameToQuery );
2481 }
2482 }
2483 #endif
2484 }
2485 ( void ) xTaskResumeAll();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002486
xiaohu.huang58292b32024-01-03 14:09:51 +08002487 return pxTCB;
2488 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002489
2490#endif /* INCLUDE_xTaskGetHandle */
2491/*-----------------------------------------------------------*/
2492
2493#if ( configUSE_TRACE_FACILITY == 1 )
2494
xiaohu.huang58292b32024-01-03 14:09:51 +08002495 UBaseType_t uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray,
2496 const UBaseType_t uxArraySize,
2497 configRUN_TIME_COUNTER_TYPE * const pulTotalRunTime )
2498 {
2499 UBaseType_t uxTask = 0, uxQueue = configMAX_PRIORITIES;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002500
xiaohu.huang58292b32024-01-03 14:09:51 +08002501 vTaskSuspendAll();
2502 {
2503 /* Is there a space in the array for each task in the system? */
2504 if( uxArraySize >= uxCurrentNumberOfTasks )
2505 {
2506 /* Fill in an TaskStatus_t structure with information on each
2507 * task in the Ready state. */
2508 do
2509 {
2510 uxQueue--;
2511 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &( pxReadyTasksLists[ uxQueue ] ), eReady );
2512 } 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 +08002513
xiaohu.huang58292b32024-01-03 14:09:51 +08002514 /* Fill in an TaskStatus_t structure with information on each
2515 * task in the Blocked state. */
2516 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxDelayedTaskList, eBlocked );
2517 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), ( List_t * ) pxOverflowDelayedTaskList, eBlocked );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002518
xiaohu.huang58292b32024-01-03 14:09:51 +08002519 #if ( INCLUDE_vTaskDelete == 1 )
2520 {
2521 /* Fill in an TaskStatus_t structure with information on
2522 * each task that has been deleted but not yet cleaned up. */
2523 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xTasksWaitingTermination, eDeleted );
2524 }
2525 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002526
xiaohu.huang58292b32024-01-03 14:09:51 +08002527 #if ( INCLUDE_vTaskSuspend == 1 )
2528 {
2529 /* Fill in an TaskStatus_t structure with information on
2530 * each task in the Suspended state. */
2531 uxTask += prvListTasksWithinSingleList( &( pxTaskStatusArray[ uxTask ] ), &xSuspendedTaskList, eSuspended );
2532 }
2533 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002534
xiaohu.huang58292b32024-01-03 14:09:51 +08002535 #if ( configGENERATE_RUN_TIME_STATS == 1 )
2536 {
2537 if( pulTotalRunTime != NULL )
2538 {
2539 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
2540 portALT_GET_RUN_TIME_COUNTER_VALUE( ( *pulTotalRunTime ) );
2541 #else
2542 *pulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
2543 #endif
2544 }
2545 }
2546 #else /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
2547 {
2548 if( pulTotalRunTime != NULL )
2549 {
2550 *pulTotalRunTime = 0;
2551 }
2552 }
2553 #endif /* if ( configGENERATE_RUN_TIME_STATS == 1 ) */
2554 }
2555 else
2556 {
2557 mtCOVERAGE_TEST_MARKER();
2558 }
2559 }
2560 ( void ) xTaskResumeAll();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002561
xiaohu.huang58292b32024-01-03 14:09:51 +08002562 return uxTask;
2563 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002564
2565#endif /* configUSE_TRACE_FACILITY */
2566/*----------------------------------------------------------*/
2567
2568#if ( INCLUDE_xTaskGetIdleTaskHandle == 1 )
2569
xiaohu.huang58292b32024-01-03 14:09:51 +08002570 TaskHandle_t xTaskGetIdleTaskHandle( void )
2571 {
2572 /* If xTaskGetIdleTaskHandle() is called before the scheduler has been
2573 * started, then xIdleTaskHandle will be NULL. */
2574 configASSERT( ( xIdleTaskHandle != NULL ) );
2575 return xIdleTaskHandle;
2576 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002577
2578#endif /* INCLUDE_xTaskGetIdleTaskHandle */
2579/*----------------------------------------------------------*/
2580
2581/* This conditional compilation should use inequality to 0, not equality to 1.
xiaohu.huang58292b32024-01-03 14:09:51 +08002582 * This is to ensure vTaskStepTick() is available when user defined low power mode
2583 * implementations require configUSE_TICKLESS_IDLE to be set to a value other than
2584 * 1. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002585#if ( configUSE_TICKLESS_IDLE != 0 )
2586
xiaohu.huang58292b32024-01-03 14:09:51 +08002587 void vTaskStepTick( TickType_t xTicksToJump )
2588 {
2589 /* Correct the tick count value after a period during which the tick
2590 * was suppressed. Note this does *not* call the tick hook function for
2591 * each stepped tick. */
2592 configASSERT( ( xTickCount + xTicksToJump ) <= xNextTaskUnblockTime );
2593
2594 if( ( xTickCount + xTicksToJump ) == xNextTaskUnblockTime )
2595 {
2596 /* Arrange for xTickCount to reach xNextTaskUnblockTime in
2597 * xTaskIncrementTick() when the scheduler resumes. This ensures
2598 * that any delayed tasks are resumed at the correct time. */
2599 configASSERT( uxSchedulerSuspended );
2600 configASSERT( xTicksToJump != ( TickType_t ) 0 );
2601
2602 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
2603 taskENTER_CRITICAL();
2604 {
2605 xPendedTicks++;
2606 }
2607 taskEXIT_CRITICAL();
2608 xTicksToJump--;
2609 }
2610 else
2611 {
2612 mtCOVERAGE_TEST_MARKER();
2613 }
2614
2615 xTickCount += xTicksToJump;
2616 traceINCREASE_TICK_COUNT( xTicksToJump );
2617 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002618
2619#endif /* configUSE_TICKLESS_IDLE */
2620/*----------------------------------------------------------*/
2621
xiaohu.huang58292b32024-01-03 14:09:51 +08002622BaseType_t xTaskCatchUpTicks( TickType_t xTicksToCatchUp )
2623{
2624 BaseType_t xYieldOccurred;
2625
2626 /* Must not be called with the scheduler suspended as the implementation
2627 * relies on xPendedTicks being wound down to 0 in xTaskResumeAll(). */
2628 configASSERT( uxSchedulerSuspended == 0 );
2629
2630 /* Use xPendedTicks to mimic xTicksToCatchUp number of ticks occurring when
2631 * the scheduler is suspended so the ticks are executed in xTaskResumeAll(). */
2632 vTaskSuspendAll();
2633
2634 /* Prevent the tick interrupt modifying xPendedTicks simultaneously. */
2635 taskENTER_CRITICAL();
2636 {
2637 xPendedTicks += xTicksToCatchUp;
2638 }
2639 taskEXIT_CRITICAL();
2640 xYieldOccurred = xTaskResumeAll();
2641
2642 return xYieldOccurred;
2643}
2644/*----------------------------------------------------------*/
2645
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002646#if ( INCLUDE_xTaskAbortDelay == 1 )
2647
xiaohu.huang58292b32024-01-03 14:09:51 +08002648 BaseType_t xTaskAbortDelay( TaskHandle_t xTask )
2649 {
2650 TCB_t * pxTCB = xTask;
2651 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002652
xiaohu.huang58292b32024-01-03 14:09:51 +08002653 configASSERT( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002654
xiaohu.huang58292b32024-01-03 14:09:51 +08002655 vTaskSuspendAll();
2656 {
2657 /* A task can only be prematurely removed from the Blocked state if
2658 * it is actually in the Blocked state. */
2659 if( eTaskGetState( xTask ) == eBlocked )
2660 {
2661 xReturn = pdPASS;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002662
xiaohu.huang58292b32024-01-03 14:09:51 +08002663 /* Remove the reference to the task from the blocked list. An
2664 * interrupt won't touch the xStateListItem because the
2665 * scheduler is suspended. */
2666 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002667
xiaohu.huang58292b32024-01-03 14:09:51 +08002668 /* Is the task waiting on an event also? If so remove it from
2669 * the event list too. Interrupts can touch the event list item,
2670 * even though the scheduler is suspended, so a critical section
2671 * is used. */
2672 taskENTER_CRITICAL();
2673 {
2674 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2675 {
2676 ( void ) uxListRemove( &( pxTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002677
xiaohu.huang58292b32024-01-03 14:09:51 +08002678 /* This lets the task know it was forcibly removed from the
2679 * blocked state so it should not re-evaluate its block time and
2680 * then block again. */
2681 pxTCB->ucDelayAborted = pdTRUE;
2682 }
2683 else
2684 {
2685 mtCOVERAGE_TEST_MARKER();
2686 }
2687 }
2688 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002689
xiaohu.huang58292b32024-01-03 14:09:51 +08002690 /* Place the unblocked task into the appropriate ready list. */
2691 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002692
xiaohu.huang58292b32024-01-03 14:09:51 +08002693 /* A task being unblocked cannot cause an immediate context
2694 * switch if preemption is turned off. */
2695 #if ( configUSE_PREEMPTION == 1 )
2696 {
2697 /* Preemption is on, but a context switch should only be
2698 * performed if the unblocked task has a priority that is
2699 * higher than the currently executing task. */
2700 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
2701 {
2702 /* Pend the yield to be performed when the scheduler
2703 * is unsuspended. */
2704 xYieldPending = pdTRUE;
2705 }
2706 else
2707 {
2708 mtCOVERAGE_TEST_MARKER();
2709 }
2710 }
2711 #endif /* configUSE_PREEMPTION */
2712 }
2713 else
2714 {
2715 xReturn = pdFAIL;
2716 }
2717 }
2718 ( void ) xTaskResumeAll();
2719
2720 return xReturn;
2721 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002722
2723#endif /* INCLUDE_xTaskAbortDelay */
2724/*----------------------------------------------------------*/
2725
2726BaseType_t xTaskIncrementTick( void )
2727{
xiaohu.huang58292b32024-01-03 14:09:51 +08002728 TCB_t * pxTCB;
2729 TickType_t xItemValue;
2730 BaseType_t xSwitchRequired = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002731
xiaohu.huang58292b32024-01-03 14:09:51 +08002732 /* Called by the portable layer each time a tick interrupt occurs.
2733 * Increments the tick then checks to see if the new tick value will cause any
2734 * tasks to be unblocked. */
2735 traceTASK_INCREMENT_TICK( xTickCount );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002736
xiaohu.huang58292b32024-01-03 14:09:51 +08002737 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
2738 {
2739 /* Minor optimisation. The tick count cannot change in this
2740 * block. */
2741 const TickType_t xConstTickCount = xTickCount + ( TickType_t ) 1;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002742
xiaohu.huang58292b32024-01-03 14:09:51 +08002743 /* Increment the RTOS tick, switching the delayed and overflowed
2744 * delayed lists if it wraps to 0. */
2745 xTickCount = xConstTickCount;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002746
xiaohu.huang58292b32024-01-03 14:09:51 +08002747 if( xConstTickCount == ( TickType_t ) 0U ) /*lint !e774 'if' does not always evaluate to false as it is looking for an overflow. */
2748 {
2749 taskSWITCH_DELAYED_LISTS();
2750 }
2751 else
2752 {
2753 mtCOVERAGE_TEST_MARKER();
2754 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002755
xiaohu.huang58292b32024-01-03 14:09:51 +08002756 /* See if this tick has made a timeout expire. Tasks are stored in
2757 * the queue in the order of their wake time - meaning once one task
2758 * has been found whose block time has not expired there is no need to
2759 * look any further down the list. */
2760 if( xConstTickCount >= xNextTaskUnblockTime )
2761 {
2762 for( ; ; )
2763 {
2764 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
2765 {
2766 /* The delayed list is empty. Set xNextTaskUnblockTime
2767 * to the maximum possible value so it is extremely
2768 * unlikely that the
2769 * if( xTickCount >= xNextTaskUnblockTime ) test will pass
2770 * next time through. */
2771 xNextTaskUnblockTime = portMAX_DELAY; /*lint !e961 MISRA exception as the casts are only redundant for some ports. */
2772 break;
2773 }
2774 else
2775 {
2776 /* The delayed list is not empty, get the value of the
2777 * item at the head of the delayed list. This is the time
2778 * at which the task at the head of the delayed list must
2779 * be removed from the Blocked state. */
2780 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. */
2781 xItemValue = listGET_LIST_ITEM_VALUE( &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002782
xiaohu.huang58292b32024-01-03 14:09:51 +08002783 if( xConstTickCount < xItemValue )
2784 {
2785 /* It is not time to unblock this item yet, but the
2786 * item value is the time at which the task at the head
2787 * of the blocked list must be removed from the Blocked
2788 * state - so record the item value in
2789 * xNextTaskUnblockTime. */
2790 xNextTaskUnblockTime = xItemValue;
2791 break; /*lint !e9011 Code structure here is deemed easier to understand with multiple breaks. */
2792 }
2793 else
2794 {
2795 mtCOVERAGE_TEST_MARKER();
2796 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002797
xiaohu.huang58292b32024-01-03 14:09:51 +08002798 /* It is time to remove the item from the Blocked state. */
2799 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002800
xiaohu.huang58292b32024-01-03 14:09:51 +08002801 /* Is the task waiting on an event also? If so remove
2802 * it from the event list. */
2803 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
2804 {
2805 listREMOVE_ITEM( &( pxTCB->xEventListItem ) );
2806 }
2807 else
2808 {
2809 mtCOVERAGE_TEST_MARKER();
2810 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002811
xiaohu.huang58292b32024-01-03 14:09:51 +08002812 /* Place the unblocked task into the appropriate ready
2813 * list. */
2814 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002815
xiaohu.huang58292b32024-01-03 14:09:51 +08002816 /* A task being unblocked cannot cause an immediate
2817 * context switch if preemption is turned off. */
2818 #if ( configUSE_PREEMPTION == 1 )
2819 {
2820 /* Preemption is on, but a context switch should
2821 * only be performed if the unblocked task's
2822 * priority is higher than the currently executing
2823 * task.
2824 * The case of equal priority tasks sharing
2825 * processing time (which happens when both
2826 * preemption and time slicing are on) is
2827 * handled below.*/
2828 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
2829 {
2830 xSwitchRequired = pdTRUE;
2831 }
2832 else
2833 {
2834 mtCOVERAGE_TEST_MARKER();
2835 }
2836 }
2837 #endif /* configUSE_PREEMPTION */
2838 }
2839 }
2840 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002841
xiaohu.huang58292b32024-01-03 14:09:51 +08002842 /* Tasks of equal priority to the currently running task will share
2843 * processing time (time slice) if preemption is on, and the application
2844 * writer has not explicitly turned time slicing off. */
2845 #if ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) )
2846 {
2847 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ pxCurrentTCB->uxPriority ] ) ) > ( UBaseType_t ) 1 )
2848 {
2849 xSwitchRequired = pdTRUE;
2850 }
2851 else
2852 {
2853 mtCOVERAGE_TEST_MARKER();
2854 }
2855 }
2856 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configUSE_TIME_SLICING == 1 ) ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002857
xiaohu.huang58292b32024-01-03 14:09:51 +08002858 #if ( configUSE_TICK_HOOK == 1 )
2859 {
2860 /* Guard against the tick hook being called when the pended tick
2861 * count is being unwound (when the scheduler is being unlocked). */
2862 if( xPendedTicks == ( TickType_t ) 0 )
2863 {
2864 vApplicationTickHook();
2865 }
2866 else
2867 {
2868 mtCOVERAGE_TEST_MARKER();
2869 }
2870 }
2871 #endif /* configUSE_TICK_HOOK */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002872
xiaohu.huang58292b32024-01-03 14:09:51 +08002873 #if ( configUSE_PREEMPTION == 1 )
2874 {
2875 if( xYieldPending != pdFALSE )
2876 {
2877 xSwitchRequired = pdTRUE;
2878 }
2879 else
2880 {
2881 mtCOVERAGE_TEST_MARKER();
2882 }
2883 }
2884 #endif /* configUSE_PREEMPTION */
2885 }
2886 else
2887 {
2888 ++xPendedTicks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002889
xiaohu.huang58292b32024-01-03 14:09:51 +08002890 /* The tick hook gets called at regular intervals, even if the
2891 * scheduler is locked. */
2892 #if ( configUSE_TICK_HOOK == 1 )
2893 {
2894 vApplicationTickHook();
2895 }
2896 #endif
2897 }
2898
2899 return xSwitchRequired;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002900}
2901/*-----------------------------------------------------------*/
2902
2903#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2904
xiaohu.huang58292b32024-01-03 14:09:51 +08002905 void vTaskSetApplicationTaskTag( TaskHandle_t xTask,
2906 TaskHookFunction_t pxHookFunction )
2907 {
2908 TCB_t * xTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002909
xiaohu.huang58292b32024-01-03 14:09:51 +08002910 /* If xTask is NULL then it is the task hook of the calling task that is
2911 * getting set. */
2912 if( xTask == NULL )
2913 {
2914 xTCB = ( TCB_t * ) pxCurrentTCB;
2915 }
2916 else
2917 {
2918 xTCB = xTask;
2919 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002920
xiaohu.huang58292b32024-01-03 14:09:51 +08002921 /* Save the hook function in the TCB. A critical section is required as
2922 * the value can be accessed from an interrupt. */
2923 taskENTER_CRITICAL();
2924 {
2925 xTCB->pxTaskTag = pxHookFunction;
2926 }
2927 taskEXIT_CRITICAL();
2928 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002929
2930#endif /* configUSE_APPLICATION_TASK_TAG */
2931/*-----------------------------------------------------------*/
2932
2933#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2934
xiaohu.huang58292b32024-01-03 14:09:51 +08002935 TaskHookFunction_t xTaskGetApplicationTaskTag( TaskHandle_t xTask )
2936 {
2937 TCB_t * pxTCB;
2938 TaskHookFunction_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002939
xiaohu.huang58292b32024-01-03 14:09:51 +08002940 /* If xTask is NULL then set the calling task's hook. */
2941 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002942
xiaohu.huang58292b32024-01-03 14:09:51 +08002943 /* Save the hook function in the TCB. A critical section is required as
2944 * the value can be accessed from an interrupt. */
2945 taskENTER_CRITICAL();
2946 {
2947 xReturn = pxTCB->pxTaskTag;
2948 }
2949 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002950
xiaohu.huang58292b32024-01-03 14:09:51 +08002951 return xReturn;
2952 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002953
2954#endif /* configUSE_APPLICATION_TASK_TAG */
2955/*-----------------------------------------------------------*/
2956
2957#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2958
xiaohu.huang58292b32024-01-03 14:09:51 +08002959 TaskHookFunction_t xTaskGetApplicationTaskTagFromISR( TaskHandle_t xTask )
2960 {
2961 TCB_t * pxTCB;
2962 TaskHookFunction_t xReturn;
2963 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002964
xiaohu.huang58292b32024-01-03 14:09:51 +08002965 /* If xTask is NULL then set the calling task's hook. */
2966 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002967
xiaohu.huang58292b32024-01-03 14:09:51 +08002968 /* Save the hook function in the TCB. A critical section is required as
2969 * the value can be accessed from an interrupt. */
2970 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
2971 {
2972 xReturn = pxTCB->pxTaskTag;
2973 }
2974 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002975
xiaohu.huang58292b32024-01-03 14:09:51 +08002976 return xReturn;
2977 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002978
2979#endif /* configUSE_APPLICATION_TASK_TAG */
2980/*-----------------------------------------------------------*/
2981
2982#if ( configUSE_APPLICATION_TASK_TAG == 1 )
2983
xiaohu.huang58292b32024-01-03 14:09:51 +08002984 BaseType_t xTaskCallApplicationTaskHook( TaskHandle_t xTask,
2985 void * pvParameter )
2986 {
2987 TCB_t * xTCB;
2988 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002989
xiaohu.huang58292b32024-01-03 14:09:51 +08002990 /* If xTask is NULL then we are calling our own task hook. */
2991 if( xTask == NULL )
2992 {
2993 xTCB = pxCurrentTCB;
2994 }
2995 else
2996 {
2997 xTCB = xTask;
2998 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08002999
xiaohu.huang58292b32024-01-03 14:09:51 +08003000 if( xTCB->pxTaskTag != NULL )
3001 {
3002 xReturn = xTCB->pxTaskTag( pvParameter );
3003 }
3004 else
3005 {
3006 xReturn = pdFAIL;
3007 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003008
xiaohu.huang58292b32024-01-03 14:09:51 +08003009 return xReturn;
3010 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003011
3012#endif /* configUSE_APPLICATION_TASK_TAG */
3013/*-----------------------------------------------------------*/
3014
3015void vTaskSwitchContext( void )
3016{
xiaohu.huang58292b32024-01-03 14:09:51 +08003017 if( uxSchedulerSuspended != ( UBaseType_t ) pdFALSE )
3018 {
3019 /* The scheduler is currently suspended - do not allow a context
3020 * switch. */
3021 xYieldPending = pdTRUE;
3022 }
3023 else
3024 {
3025 xYieldPending = pdFALSE;
3026 traceTASK_SWITCHED_OUT();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003027
xiaohu.huang58292b32024-01-03 14:09:51 +08003028 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3029 {
3030 #ifdef portALT_GET_RUN_TIME_COUNTER_VALUE
3031 portALT_GET_RUN_TIME_COUNTER_VALUE( ulTotalRunTime );
3032 #else
3033 ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();
3034 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003035
xiaohu.huang58292b32024-01-03 14:09:51 +08003036 /* Add the amount of time the task has been running to the
3037 * accumulated time so far. The time the task started running was
3038 * stored in ulTaskSwitchedInTime. Note that there is no overflow
3039 * protection here so count values are only valid until the timer
3040 * overflows. The guard against negative values is to protect
3041 * against suspect run time stat counter implementations - which
3042 * are provided by the application, not the kernel. */
3043 if( ulTotalRunTime > ulTaskSwitchedInTime )
3044 {
3045 pxCurrentTCB->ulRunTimeCounter += ( ulTotalRunTime - ulTaskSwitchedInTime );
shijie.xionga23e8242024-02-27 14:57:02 +08003046#if CONFIG_FTRACE
3047 vTraceSwitchContext((uint32_t)pxCurrentTCB->uxTCBNumber);
3048#endif
xiaohu.huang58292b32024-01-03 14:09:51 +08003049 }
3050 else
3051 {
3052 mtCOVERAGE_TEST_MARKER();
3053 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003054
xiaohu.huang58292b32024-01-03 14:09:51 +08003055 ulTaskSwitchedInTime = ulTotalRunTime;
3056 }
3057 #endif /* configGENERATE_RUN_TIME_STATS */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003058
xiaohu.huang58292b32024-01-03 14:09:51 +08003059 /* Check for stack overflow, if configured. */
3060 taskCHECK_FOR_STACK_OVERFLOW();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003061
xiaohu.huang58292b32024-01-03 14:09:51 +08003062 /* Before the currently running task is switched out, save its errno. */
3063 #if ( configUSE_POSIX_ERRNO == 1 )
3064 {
3065 pxCurrentTCB->iTaskErrno = FreeRTOS_errno;
3066 }
3067 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003068
xiaohu.huang58292b32024-01-03 14:09:51 +08003069 /* Select a new task to run using either the generic C or port
3070 * optimised asm code. */
3071 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. */
3072 traceTASK_SWITCHED_IN();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003073
xiaohu.huang58292b32024-01-03 14:09:51 +08003074 /* After the new task is switched in, update the global errno. */
3075 #if ( configUSE_POSIX_ERRNO == 1 )
3076 {
3077 FreeRTOS_errno = pxCurrentTCB->iTaskErrno;
3078 }
3079 #endif
3080
3081 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
3082 {
3083 /* Switch C-Runtime's TLS Block to point to the TLS
3084 * Block specific to this task. */
3085 configSET_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3086 }
3087 #endif
3088 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003089}
3090/*-----------------------------------------------------------*/
3091
xiaohu.huang58292b32024-01-03 14:09:51 +08003092void vTaskPlaceOnEventList( List_t * const pxEventList,
3093 const TickType_t xTicksToWait )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003094{
xiaohu.huang58292b32024-01-03 14:09:51 +08003095 configASSERT( pxEventList );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003096
xiaohu.huang58292b32024-01-03 14:09:51 +08003097 /* THIS FUNCTION MUST BE CALLED WITH EITHER INTERRUPTS DISABLED OR THE
3098 * SCHEDULER SUSPENDED AND THE QUEUE BEING ACCESSED LOCKED. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003099
xiaohu.huang58292b32024-01-03 14:09:51 +08003100 /* Place the event list item of the TCB in the appropriate event list.
3101 * This is placed in the list in priority order so the highest priority task
3102 * is the first to be woken by the event.
3103 *
3104 * Note: Lists are sorted in ascending order by ListItem_t.xItemValue.
3105 * Normally, the xItemValue of a TCB's ListItem_t members is:
3106 * xItemValue = ( configMAX_PRIORITIES - uxPriority )
3107 * Therefore, the event list is sorted in descending priority order.
3108 *
3109 * The queue that contains the event list is locked, preventing
3110 * simultaneous access from interrupts. */
3111 vListInsert( pxEventList, &( pxCurrentTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003112
xiaohu.huang58292b32024-01-03 14:09:51 +08003113 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003114}
3115/*-----------------------------------------------------------*/
3116
xiaohu.huang58292b32024-01-03 14:09:51 +08003117void vTaskPlaceOnUnorderedEventList( List_t * pxEventList,
3118 const TickType_t xItemValue,
3119 const TickType_t xTicksToWait )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003120{
xiaohu.huang58292b32024-01-03 14:09:51 +08003121 configASSERT( pxEventList );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003122
xiaohu.huang58292b32024-01-03 14:09:51 +08003123 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3124 * the event groups implementation. */
3125 configASSERT( uxSchedulerSuspended != 0 );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003126
xiaohu.huang58292b32024-01-03 14:09:51 +08003127 /* Store the item value in the event list item. It is safe to access the
3128 * event list item here as interrupts won't access the event list item of a
3129 * task that is not in the Blocked state. */
3130 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ), xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003131
xiaohu.huang58292b32024-01-03 14:09:51 +08003132 /* Place the event list item of the TCB at the end of the appropriate event
3133 * list. It is safe to access the event list here because it is part of an
3134 * event group implementation - and interrupts don't access event groups
3135 * directly (instead they access them indirectly by pending function calls to
3136 * the task level). */
3137 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003138
xiaohu.huang58292b32024-01-03 14:09:51 +08003139 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003140}
3141/*-----------------------------------------------------------*/
3142
xiaohu.huang58292b32024-01-03 14:09:51 +08003143#if ( configUSE_TIMERS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003144
xiaohu.huang58292b32024-01-03 14:09:51 +08003145 void vTaskPlaceOnEventListRestricted( List_t * const pxEventList,
3146 TickType_t xTicksToWait,
3147 const BaseType_t xWaitIndefinitely )
3148 {
3149 configASSERT( pxEventList );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003150
xiaohu.huang58292b32024-01-03 14:09:51 +08003151 /* This function should not be called by application code hence the
3152 * 'Restricted' in its name. It is not part of the public API. It is
3153 * designed for use by kernel code, and has special calling requirements -
3154 * it should be called with the scheduler suspended. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003155
3156
xiaohu.huang58292b32024-01-03 14:09:51 +08003157 /* Place the event list item of the TCB in the appropriate event list.
3158 * In this case it is assume that this is the only task that is going to
3159 * be waiting on this event list, so the faster vListInsertEnd() function
3160 * can be used in place of vListInsert. */
3161 listINSERT_END( pxEventList, &( pxCurrentTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003162
xiaohu.huang58292b32024-01-03 14:09:51 +08003163 /* If the task should block indefinitely then set the block time to a
3164 * value that will be recognised as an indefinite delay inside the
3165 * prvAddCurrentTaskToDelayedList() function. */
3166 if( xWaitIndefinitely != pdFALSE )
3167 {
3168 xTicksToWait = portMAX_DELAY;
3169 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003170
xiaohu.huang58292b32024-01-03 14:09:51 +08003171 traceTASK_DELAY_UNTIL( ( xTickCount + xTicksToWait ) );
3172 prvAddCurrentTaskToDelayedList( xTicksToWait, xWaitIndefinitely );
3173 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003174
3175#endif /* configUSE_TIMERS */
3176/*-----------------------------------------------------------*/
3177
3178BaseType_t xTaskRemoveFromEventList( const List_t * const pxEventList )
3179{
xiaohu.huang58292b32024-01-03 14:09:51 +08003180 TCB_t * pxUnblockedTCB;
3181 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003182
xiaohu.huang58292b32024-01-03 14:09:51 +08003183 /* THIS FUNCTION MUST BE CALLED FROM A CRITICAL SECTION. It can also be
3184 * called from a critical section within an ISR. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003185
xiaohu.huang58292b32024-01-03 14:09:51 +08003186 /* The event list is sorted in priority order, so the first in the list can
3187 * be removed as it is known to be the highest priority. Remove the TCB from
3188 * the delayed list, and add it to the ready list.
3189 *
3190 * If an event is for a queue that is locked then this function will never
3191 * get called - the lock count on the queue will get modified instead. This
3192 * means exclusive access to the event list is guaranteed here.
3193 *
3194 * This function assumes that a check has already been made to ensure that
3195 * pxEventList is not empty. */
3196 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. */
3197 configASSERT( pxUnblockedTCB );
3198 listREMOVE_ITEM( &( pxUnblockedTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003199
xiaohu.huang58292b32024-01-03 14:09:51 +08003200 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
3201 {
3202 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
3203 prvAddTaskToReadyList( pxUnblockedTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003204
xiaohu.huang58292b32024-01-03 14:09:51 +08003205 #if ( configUSE_TICKLESS_IDLE != 0 )
3206 {
3207 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3208 * might be set to the blocked task's time out time. If the task is
3209 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
3210 * normally left unchanged, because it is automatically reset to a new
3211 * value when the tick count equals xNextTaskUnblockTime. However if
3212 * tickless idling is used it might be more important to enter sleep mode
3213 * at the earliest possible time - so reset xNextTaskUnblockTime here to
3214 * ensure it is updated at the earliest possible time. */
3215 prvResetNextTaskUnblockTime();
3216 }
3217 #endif
3218 }
3219 else
3220 {
3221 /* The delayed and ready lists cannot be accessed, so hold this task
3222 * pending until the scheduler is resumed. */
3223 listINSERT_END( &( xPendingReadyList ), &( pxUnblockedTCB->xEventListItem ) );
3224 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003225
xiaohu.huang58292b32024-01-03 14:09:51 +08003226 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
3227 {
3228 /* Return true if the task removed from the event list has a higher
3229 * priority than the calling task. This allows the calling task to know if
3230 * it should force a context switch now. */
3231 xReturn = pdTRUE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003232
xiaohu.huang58292b32024-01-03 14:09:51 +08003233 /* Mark that a yield is pending in case the user is not using the
3234 * "xHigherPriorityTaskWoken" parameter to an ISR safe FreeRTOS function. */
3235 xYieldPending = pdTRUE;
3236 }
3237 else
3238 {
3239 xReturn = pdFALSE;
3240 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003241
xiaohu.huang58292b32024-01-03 14:09:51 +08003242 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003243}
3244/*-----------------------------------------------------------*/
3245
xiaohu.huang58292b32024-01-03 14:09:51 +08003246void vTaskRemoveFromUnorderedEventList( ListItem_t * pxEventListItem,
3247 const TickType_t xItemValue )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003248{
xiaohu.huang58292b32024-01-03 14:09:51 +08003249 TCB_t * pxUnblockedTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003250
xiaohu.huang58292b32024-01-03 14:09:51 +08003251 /* THIS FUNCTION MUST BE CALLED WITH THE SCHEDULER SUSPENDED. It is used by
3252 * the event flags implementation. */
3253 configASSERT( uxSchedulerSuspended != pdFALSE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003254
xiaohu.huang58292b32024-01-03 14:09:51 +08003255 /* Store the new item value in the event list. */
3256 listSET_LIST_ITEM_VALUE( pxEventListItem, xItemValue | taskEVENT_LIST_ITEM_VALUE_IN_USE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003257
xiaohu.huang58292b32024-01-03 14:09:51 +08003258 /* Remove the event list form the event flag. Interrupts do not access
3259 * event flags. */
3260 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. */
3261 configASSERT( pxUnblockedTCB );
3262 listREMOVE_ITEM( pxEventListItem );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003263
xiaohu.huang58292b32024-01-03 14:09:51 +08003264 #if ( configUSE_TICKLESS_IDLE != 0 )
3265 {
3266 /* If a task is blocked on a kernel object then xNextTaskUnblockTime
3267 * might be set to the blocked task's time out time. If the task is
3268 * unblocked for a reason other than a timeout xNextTaskUnblockTime is
3269 * normally left unchanged, because it is automatically reset to a new
3270 * value when the tick count equals xNextTaskUnblockTime. However if
3271 * tickless idling is used it might be more important to enter sleep mode
3272 * at the earliest possible time - so reset xNextTaskUnblockTime here to
3273 * ensure it is updated at the earliest possible time. */
3274 prvResetNextTaskUnblockTime();
3275 }
3276 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003277
xiaohu.huang58292b32024-01-03 14:09:51 +08003278 /* Remove the task from the delayed list and add it to the ready list. The
3279 * scheduler is suspended so interrupts will not be accessing the ready
3280 * lists. */
3281 listREMOVE_ITEM( &( pxUnblockedTCB->xStateListItem ) );
3282 prvAddTaskToReadyList( pxUnblockedTCB );
3283
3284 if( pxUnblockedTCB->uxPriority > pxCurrentTCB->uxPriority )
3285 {
3286 /* The unblocked task has a priority above that of the calling task, so
3287 * a context switch is required. This function is called with the
3288 * scheduler suspended so xYieldPending is set so the context switch
3289 * occurs immediately that the scheduler is resumed (unsuspended). */
3290 xYieldPending = pdTRUE;
3291 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003292}
3293/*-----------------------------------------------------------*/
3294
3295void vTaskSetTimeOutState( TimeOut_t * const pxTimeOut )
3296{
xiaohu.huang58292b32024-01-03 14:09:51 +08003297 configASSERT( pxTimeOut );
3298 taskENTER_CRITICAL();
3299 {
3300 pxTimeOut->xOverflowCount = xNumOfOverflows;
3301 pxTimeOut->xTimeOnEntering = xTickCount;
3302 }
3303 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003304}
3305/*-----------------------------------------------------------*/
3306
3307void vTaskInternalSetTimeOutState( TimeOut_t * const pxTimeOut )
3308{
xiaohu.huang58292b32024-01-03 14:09:51 +08003309 /* For internal use only as it does not use a critical section. */
3310 pxTimeOut->xOverflowCount = xNumOfOverflows;
3311 pxTimeOut->xTimeOnEntering = xTickCount;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003312}
3313/*-----------------------------------------------------------*/
3314
xiaohu.huang58292b32024-01-03 14:09:51 +08003315BaseType_t xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut,
3316 TickType_t * const pxTicksToWait )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003317{
xiaohu.huang58292b32024-01-03 14:09:51 +08003318 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003319
xiaohu.huang58292b32024-01-03 14:09:51 +08003320 configASSERT( pxTimeOut );
3321 configASSERT( pxTicksToWait );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003322
xiaohu.huang58292b32024-01-03 14:09:51 +08003323 taskENTER_CRITICAL();
3324 {
3325 /* Minor optimisation. The tick count cannot change in this block. */
3326 const TickType_t xConstTickCount = xTickCount;
3327 const TickType_t xElapsedTime = xConstTickCount - pxTimeOut->xTimeOnEntering;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003328
xiaohu.huang58292b32024-01-03 14:09:51 +08003329 #if ( INCLUDE_xTaskAbortDelay == 1 )
3330 if( pxCurrentTCB->ucDelayAborted != ( uint8_t ) pdFALSE )
3331 {
3332 /* The delay was aborted, which is not the same as a time out,
3333 * but has the same result. */
3334 pxCurrentTCB->ucDelayAborted = pdFALSE;
3335 xReturn = pdTRUE;
3336 }
3337 else
3338 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003339
xiaohu.huang58292b32024-01-03 14:09:51 +08003340 #if ( INCLUDE_vTaskSuspend == 1 )
3341 if( *pxTicksToWait == portMAX_DELAY )
3342 {
3343 /* If INCLUDE_vTaskSuspend is set to 1 and the block time
3344 * specified is the maximum block time then the task should block
3345 * indefinitely, and therefore never time out. */
3346 xReturn = pdFALSE;
3347 }
3348 else
3349 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003350
xiaohu.huang58292b32024-01-03 14:09:51 +08003351 if( ( xNumOfOverflows != pxTimeOut->xOverflowCount ) && ( xConstTickCount >= pxTimeOut->xTimeOnEntering ) ) /*lint !e525 Indentation preferred as is to make code within pre-processor directives clearer. */
3352 {
3353 /* The tick count is greater than the time at which
3354 * vTaskSetTimeout() was called, but has also overflowed since
3355 * vTaskSetTimeOut() was called. It must have wrapped all the way
3356 * around and gone past again. This passed since vTaskSetTimeout()
3357 * was called. */
3358 xReturn = pdTRUE;
3359 *pxTicksToWait = ( TickType_t ) 0;
3360 }
3361 else if( xElapsedTime < *pxTicksToWait ) /*lint !e961 Explicit casting is only redundant with some compilers, whereas others require it to prevent integer conversion errors. */
3362 {
3363 /* Not a genuine timeout. Adjust parameters for time remaining. */
3364 *pxTicksToWait -= xElapsedTime;
3365 vTaskInternalSetTimeOutState( pxTimeOut );
3366 xReturn = pdFALSE;
3367 }
3368 else
3369 {
3370 *pxTicksToWait = ( TickType_t ) 0;
3371 xReturn = pdTRUE;
3372 }
3373 }
3374 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003375
xiaohu.huang58292b32024-01-03 14:09:51 +08003376 return xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003377}
3378/*-----------------------------------------------------------*/
3379
3380void vTaskMissedYield( void )
3381{
xiaohu.huang58292b32024-01-03 14:09:51 +08003382 xYieldPending = pdTRUE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003383}
3384/*-----------------------------------------------------------*/
3385
3386#if ( configUSE_TRACE_FACILITY == 1 )
3387
xiaohu.huang58292b32024-01-03 14:09:51 +08003388 UBaseType_t uxTaskGetTaskNumber( TaskHandle_t xTask )
3389 {
3390 UBaseType_t uxReturn;
3391 TCB_t const * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003392
xiaohu.huang58292b32024-01-03 14:09:51 +08003393 if( xTask != NULL )
3394 {
3395 pxTCB = xTask;
3396 uxReturn = pxTCB->uxTaskNumber;
3397 }
3398 else
3399 {
3400 uxReturn = 0U;
3401 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003402
xiaohu.huang58292b32024-01-03 14:09:51 +08003403 return uxReturn;
3404 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003405
3406#endif /* configUSE_TRACE_FACILITY */
3407/*-----------------------------------------------------------*/
3408
3409#if ( configUSE_TRACE_FACILITY == 1 )
3410
xiaohu.huang58292b32024-01-03 14:09:51 +08003411 void vTaskSetTaskNumber( TaskHandle_t xTask,
3412 const UBaseType_t uxHandle )
3413 {
3414 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003415
xiaohu.huang58292b32024-01-03 14:09:51 +08003416 if( xTask != NULL )
3417 {
3418 pxTCB = xTask;
3419 pxTCB->uxTaskNumber = uxHandle;
3420 }
3421 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003422
3423#endif /* configUSE_TRACE_FACILITY */
3424
3425/*
3426 * -----------------------------------------------------------
3427 * The Idle task.
3428 * ----------------------------------------------------------
3429 *
3430 * The portTASK_FUNCTION() macro is used to allow port/compiler specific
3431 * language extensions. The equivalent prototype for this function is:
3432 *
3433 * void prvIdleTask( void *pvParameters );
3434 *
3435 */
3436static portTASK_FUNCTION( prvIdleTask, pvParameters )
3437{
xiaohu.huang58292b32024-01-03 14:09:51 +08003438 /* Stop warnings. */
3439 ( void ) pvParameters;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003440
xiaohu.huang58292b32024-01-03 14:09:51 +08003441 /** THIS IS THE RTOS IDLE TASK - WHICH IS CREATED AUTOMATICALLY WHEN THE
3442 * SCHEDULER IS STARTED. **/
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003443
xiaohu.huang58292b32024-01-03 14:09:51 +08003444 /* In case a task that has a secure context deletes itself, in which case
3445 * the idle task is responsible for deleting the task's secure context, if
3446 * any. */
3447 portALLOCATE_SECURE_CONTEXT( configMINIMAL_SECURE_STACK_SIZE );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003448
xiaohu.huang58292b32024-01-03 14:09:51 +08003449 for( ; ; )
3450 {
3451 /* See if any tasks have deleted themselves - if so then the idle task
3452 * is responsible for freeing the deleted task's TCB and stack. */
3453 prvCheckTasksWaitingTermination();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003454
xiaohu.huang58292b32024-01-03 14:09:51 +08003455 #if ( configUSE_PREEMPTION == 0 )
3456 {
3457 /* If we are not using preemption we keep forcing a task switch to
3458 * see if any other task has become available. If we are using
3459 * preemption we don't need to do this as any task becoming available
3460 * will automatically get the processor anyway. */
3461 taskYIELD();
3462 }
3463 #endif /* configUSE_PREEMPTION */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003464
xiaohu.huang58292b32024-01-03 14:09:51 +08003465 #if ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) )
3466 {
3467 /* When using preemption tasks of equal priority will be
3468 * timesliced. If a task that is sharing the idle priority is ready
3469 * to run then the idle task should yield before the end of the
3470 * timeslice.
3471 *
3472 * A critical region is not required here as we are just reading from
3473 * the list, and an occasional incorrect value will not matter. If
3474 * the ready list at the idle priority contains more than one task
3475 * then a task other than the idle task is ready to execute. */
3476 if( listCURRENT_LIST_LENGTH( &( pxReadyTasksLists[ tskIDLE_PRIORITY ] ) ) > ( UBaseType_t ) 1 )
3477 {
3478 taskYIELD();
3479 }
3480 else
3481 {
3482 mtCOVERAGE_TEST_MARKER();
3483 }
3484 }
3485 #endif /* ( ( configUSE_PREEMPTION == 1 ) && ( configIDLE_SHOULD_YIELD == 1 ) ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003486
xiaohu.huang58292b32024-01-03 14:09:51 +08003487 #if ( configUSE_IDLE_HOOK == 1 )
3488 {
3489 extern void vApplicationIdleHook( void );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003490
xiaohu.huang58292b32024-01-03 14:09:51 +08003491 /* Call the user defined function from within the idle task. This
3492 * allows the application designer to add background functionality
3493 * without the overhead of a separate task.
3494 * NOTE: vApplicationIdleHook() MUST NOT, UNDER ANY CIRCUMSTANCES,
3495 * CALL A FUNCTION THAT MIGHT BLOCK. */
3496 vApplicationIdleHook();
3497 }
3498 #endif /* configUSE_IDLE_HOOK */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003499
xiaohu.huang58292b32024-01-03 14:09:51 +08003500 /* This conditional compilation should use inequality to 0, not equality
3501 * to 1. This is to ensure portSUPPRESS_TICKS_AND_SLEEP() is called when
3502 * user defined low power mode implementations require
3503 * configUSE_TICKLESS_IDLE to be set to a value other than 1. */
3504 #if ( configUSE_TICKLESS_IDLE != 0 )
3505 {
3506 TickType_t xExpectedIdleTime;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003507
xiaohu.huang58292b32024-01-03 14:09:51 +08003508 /* It is not desirable to suspend then resume the scheduler on
3509 * each iteration of the idle task. Therefore, a preliminary
3510 * test of the expected idle time is performed without the
3511 * scheduler suspended. The result here is not necessarily
3512 * valid. */
3513 xExpectedIdleTime = prvGetExpectedIdleTime();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003514
xiaohu.huang58292b32024-01-03 14:09:51 +08003515 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
3516 {
3517 vTaskSuspendAll();
3518 {
3519 /* Now the scheduler is suspended, the expected idle
3520 * time can be sampled again, and this time its value can
3521 * be used. */
3522 configASSERT( xNextTaskUnblockTime >= xTickCount );
3523 xExpectedIdleTime = prvGetExpectedIdleTime();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003524
xiaohu.huang58292b32024-01-03 14:09:51 +08003525 /* Define the following macro to set xExpectedIdleTime to 0
3526 * if the application does not want
3527 * portSUPPRESS_TICKS_AND_SLEEP() to be called. */
3528 configPRE_SUPPRESS_TICKS_AND_SLEEP_PROCESSING( xExpectedIdleTime );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003529
xiaohu.huang58292b32024-01-03 14:09:51 +08003530 if( xExpectedIdleTime >= configEXPECTED_IDLE_TIME_BEFORE_SLEEP )
3531 {
3532 traceLOW_POWER_IDLE_BEGIN();
3533 portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime );
3534 traceLOW_POWER_IDLE_END();
3535 }
3536 else
3537 {
3538 mtCOVERAGE_TEST_MARKER();
3539 }
3540 }
3541 ( void ) xTaskResumeAll();
3542 }
3543 else
3544 {
3545 mtCOVERAGE_TEST_MARKER();
3546 }
3547 }
3548 #endif /* configUSE_TICKLESS_IDLE */
3549 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003550}
3551/*-----------------------------------------------------------*/
3552
xiaohu.huang58292b32024-01-03 14:09:51 +08003553#if ( configUSE_TICKLESS_IDLE != 0 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003554
xiaohu.huang58292b32024-01-03 14:09:51 +08003555 eSleepModeStatus eTaskConfirmSleepModeStatus( void )
3556 {
3557 #if ( INCLUDE_vTaskSuspend == 1 )
3558 /* The idle task exists in addition to the application tasks. */
3559 const UBaseType_t uxNonApplicationTasks = 1;
3560 #endif /* INCLUDE_vTaskSuspend */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003561
xiaohu.huang58292b32024-01-03 14:09:51 +08003562 eSleepModeStatus eReturn = eStandardSleep;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003563
xiaohu.huang58292b32024-01-03 14:09:51 +08003564 /* This function must be called from a critical section. */
3565
3566 if( listCURRENT_LIST_LENGTH( &xPendingReadyList ) != 0 )
3567 {
3568 /* A task was made ready while the scheduler was suspended. */
3569 eReturn = eAbortSleep;
3570 }
3571 else if( xYieldPending != pdFALSE )
3572 {
3573 /* A yield was pended while the scheduler was suspended. */
3574 eReturn = eAbortSleep;
3575 }
3576 else if( xPendedTicks != 0 )
3577 {
3578 /* A tick interrupt has already occurred but was held pending
3579 * because the scheduler is suspended. */
3580 eReturn = eAbortSleep;
3581 }
3582
3583 #if ( INCLUDE_vTaskSuspend == 1 )
3584 else if( listCURRENT_LIST_LENGTH( &xSuspendedTaskList ) == ( uxCurrentNumberOfTasks - uxNonApplicationTasks ) )
3585 {
3586 /* If all the tasks are in the suspended list (which might mean they
3587 * have an infinite block time rather than actually being suspended)
3588 * then it is safe to turn all clocks off and just wait for external
3589 * interrupts. */
3590 eReturn = eNoTasksWaitingTimeout;
3591 }
3592 #endif /* INCLUDE_vTaskSuspend */
3593 else
3594 {
3595 mtCOVERAGE_TEST_MARKER();
3596 }
3597
3598 return eReturn;
3599 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003600
3601#endif /* configUSE_TICKLESS_IDLE */
3602/*-----------------------------------------------------------*/
3603
3604#if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
3605
xiaohu.huang58292b32024-01-03 14:09:51 +08003606 void vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet,
3607 BaseType_t xIndex,
3608 void * pvValue )
3609 {
3610 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003611
xiaohu.huang58292b32024-01-03 14:09:51 +08003612 if( ( xIndex >= 0 ) &&
3613 ( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
3614 {
3615 pxTCB = prvGetTCBFromHandle( xTaskToSet );
3616 configASSERT( pxTCB != NULL );
3617 pxTCB->pvThreadLocalStoragePointers[ xIndex ] = pvValue;
3618 }
3619 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003620
3621#endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
3622/*-----------------------------------------------------------*/
3623
3624#if ( configNUM_THREAD_LOCAL_STORAGE_POINTERS != 0 )
3625
xiaohu.huang58292b32024-01-03 14:09:51 +08003626 void * pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery,
3627 BaseType_t xIndex )
3628 {
3629 void * pvReturn = NULL;
3630 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003631
xiaohu.huang58292b32024-01-03 14:09:51 +08003632 if( ( xIndex >= 0 ) &&
3633 ( xIndex < configNUM_THREAD_LOCAL_STORAGE_POINTERS ) )
3634 {
3635 pxTCB = prvGetTCBFromHandle( xTaskToQuery );
3636 pvReturn = pxTCB->pvThreadLocalStoragePointers[ xIndex ];
3637 }
3638 else
3639 {
3640 pvReturn = NULL;
3641 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003642
xiaohu.huang58292b32024-01-03 14:09:51 +08003643 return pvReturn;
3644 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003645
3646#endif /* configNUM_THREAD_LOCAL_STORAGE_POINTERS */
3647/*-----------------------------------------------------------*/
3648
3649#if ( portUSING_MPU_WRAPPERS == 1 )
3650
xiaohu.huang58292b32024-01-03 14:09:51 +08003651 void vTaskAllocateMPURegions( TaskHandle_t xTaskToModify,
3652 const MemoryRegion_t * const xRegions )
3653 {
3654 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003655
xiaohu.huang58292b32024-01-03 14:09:51 +08003656 /* If null is passed in here then we are modifying the MPU settings of
3657 * the calling task. */
3658 pxTCB = prvGetTCBFromHandle( xTaskToModify );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003659
xiaohu.huang58292b32024-01-03 14:09:51 +08003660 vPortStoreTaskMPUSettings( &( pxTCB->xMPUSettings ), xRegions, NULL, 0 );
3661 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003662
3663#endif /* portUSING_MPU_WRAPPERS */
3664/*-----------------------------------------------------------*/
3665
3666static void prvInitialiseTaskLists( void )
3667{
xiaohu.huang58292b32024-01-03 14:09:51 +08003668 UBaseType_t uxPriority;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003669
xiaohu.huang58292b32024-01-03 14:09:51 +08003670 for( uxPriority = ( UBaseType_t ) 0U; uxPriority < ( UBaseType_t ) configMAX_PRIORITIES; uxPriority++ )
3671 {
3672 vListInitialise( &( pxReadyTasksLists[ uxPriority ] ) );
3673 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003674
xiaohu.huang58292b32024-01-03 14:09:51 +08003675 vListInitialise( &xDelayedTaskList1 );
3676 vListInitialise( &xDelayedTaskList2 );
3677 vListInitialise( &xPendingReadyList );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003678
xiaohu.huang58292b32024-01-03 14:09:51 +08003679 #if ( INCLUDE_vTaskDelete == 1 )
3680 {
3681 vListInitialise( &xTasksWaitingTermination );
3682 }
3683 #endif /* INCLUDE_vTaskDelete */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003684
xiaohu.huang58292b32024-01-03 14:09:51 +08003685 #if ( INCLUDE_vTaskSuspend == 1 )
3686 {
3687 vListInitialise( &xSuspendedTaskList );
3688 }
3689 #endif /* INCLUDE_vTaskSuspend */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003690
xiaohu.huang58292b32024-01-03 14:09:51 +08003691 /* Start with pxDelayedTaskList using list1 and the pxOverflowDelayedTaskList
3692 * using list2. */
3693 pxDelayedTaskList = &xDelayedTaskList1;
3694 pxOverflowDelayedTaskList = &xDelayedTaskList2;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003695}
3696/*-----------------------------------------------------------*/
3697
3698static void prvCheckTasksWaitingTermination( void )
3699{
xiaohu.huang58292b32024-01-03 14:09:51 +08003700 /** THIS FUNCTION IS CALLED FROM THE RTOS IDLE TASK **/
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003701
xiaohu.huang58292b32024-01-03 14:09:51 +08003702 #if ( INCLUDE_vTaskDelete == 1 )
3703 {
3704 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003705
xiaohu.huang58292b32024-01-03 14:09:51 +08003706 /* uxDeletedTasksWaitingCleanUp is used to prevent taskENTER_CRITICAL()
3707 * being called too often in the idle task. */
3708 while( uxDeletedTasksWaitingCleanUp > ( UBaseType_t ) 0U )
3709 {
3710 taskENTER_CRITICAL();
3711 {
3712 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. */
3713 ( void ) uxListRemove( &( pxTCB->xStateListItem ) );
3714 --uxCurrentNumberOfTasks;
3715 --uxDeletedTasksWaitingCleanUp;
3716 }
3717 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003718
xiaohu.huang58292b32024-01-03 14:09:51 +08003719 prvDeleteTCB( pxTCB );
3720 }
3721 }
3722 #endif /* INCLUDE_vTaskDelete */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003723}
3724/*-----------------------------------------------------------*/
3725
xiaohu.huang58292b32024-01-03 14:09:51 +08003726#if ( configUSE_TRACE_FACILITY == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003727
xiaohu.huang58292b32024-01-03 14:09:51 +08003728 void vTaskGetInfo( TaskHandle_t xTask,
3729 TaskStatus_t * pxTaskStatus,
3730 BaseType_t xGetFreeStackSpace,
3731 eTaskState eState )
3732 {
3733 TCB_t * pxTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003734
xiaohu.huang58292b32024-01-03 14:09:51 +08003735 /* xTask is NULL then get the state of the calling task. */
3736 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003737
xiaohu.huang58292b32024-01-03 14:09:51 +08003738 pxTaskStatus->xHandle = ( TaskHandle_t ) pxTCB;
3739 pxTaskStatus->pcTaskName = ( const char * ) &( pxTCB->pcTaskName[ 0 ] );
3740 pxTaskStatus->uxCurrentPriority = pxTCB->uxPriority;
3741 pxTaskStatus->pxStackBase = pxTCB->pxStack;
3742 #if ( ( portSTACK_GROWTH > 0 ) && ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
3743 pxTaskStatus->pxTopOfStack = pxTCB->pxTopOfStack;
3744 pxTaskStatus->pxEndOfStack = pxTCB->pxEndOfStack;
3745 #endif
3746 pxTaskStatus->xTaskNumber = pxTCB->uxTCBNumber;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003747
xiaohu.huang58292b32024-01-03 14:09:51 +08003748 #if ( configUSE_MUTEXES == 1 )
3749 {
3750 pxTaskStatus->uxBasePriority = pxTCB->uxBasePriority;
3751 }
3752 #else
3753 {
3754 pxTaskStatus->uxBasePriority = 0;
3755 }
3756 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003757
xiaohu.huang58292b32024-01-03 14:09:51 +08003758 #if ( configGENERATE_RUN_TIME_STATS == 1 )
3759 {
3760 pxTaskStatus->ulRunTimeCounter = pxTCB->ulRunTimeCounter;
3761 }
3762 #else
3763 {
3764 pxTaskStatus->ulRunTimeCounter = ( configRUN_TIME_COUNTER_TYPE ) 0;
3765 }
3766 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003767
xiaohu.huang58292b32024-01-03 14:09:51 +08003768 /* Obtaining the task state is a little fiddly, so is only done if the
3769 * value of eState passed into this function is eInvalid - otherwise the
3770 * state is just set to whatever is passed in. */
3771 if( eState != eInvalid )
3772 {
3773 if( pxTCB == pxCurrentTCB )
3774 {
3775 pxTaskStatus->eCurrentState = eRunning;
3776 }
3777 else
3778 {
3779 pxTaskStatus->eCurrentState = eState;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003780
xiaohu.huang58292b32024-01-03 14:09:51 +08003781 #if ( INCLUDE_vTaskSuspend == 1 )
3782 {
3783 /* If the task is in the suspended list then there is a
3784 * chance it is actually just blocked indefinitely - so really
3785 * it should be reported as being in the Blocked state. */
3786 if( eState == eSuspended )
3787 {
3788 vTaskSuspendAll();
3789 {
3790 if( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) != NULL )
3791 {
3792 pxTaskStatus->eCurrentState = eBlocked;
3793 }
3794 }
3795 ( void ) xTaskResumeAll();
3796 }
3797 }
3798 #endif /* INCLUDE_vTaskSuspend */
3799 }
3800 }
3801 else
3802 {
3803 pxTaskStatus->eCurrentState = eTaskGetState( pxTCB );
3804 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003805
xiaohu.huang58292b32024-01-03 14:09:51 +08003806 /* Obtaining the stack space takes some time, so the xGetFreeStackSpace
3807 * parameter is provided to allow it to be skipped. */
3808 if( xGetFreeStackSpace != pdFALSE )
3809 {
3810 #if ( portSTACK_GROWTH > 0 )
3811 {
3812 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxEndOfStack );
3813 }
3814 #else
3815 {
3816 pxTaskStatus->usStackHighWaterMark = prvTaskCheckFreeStackSpace( ( uint8_t * ) pxTCB->pxStack );
3817 }
3818 #endif
3819 }
3820 else
3821 {
3822 pxTaskStatus->usStackHighWaterMark = 0;
3823 }
3824 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003825
3826#endif /* configUSE_TRACE_FACILITY */
3827/*-----------------------------------------------------------*/
3828
3829#if ( configUSE_TRACE_FACILITY == 1 )
3830
xiaohu.huang58292b32024-01-03 14:09:51 +08003831 static UBaseType_t prvListTasksWithinSingleList( TaskStatus_t * pxTaskStatusArray,
3832 List_t * pxList,
3833 eTaskState eState )
3834 {
3835 configLIST_VOLATILE TCB_t * pxNextTCB;
3836 configLIST_VOLATILE TCB_t * pxFirstTCB;
3837 UBaseType_t uxTask = 0;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003838
xiaohu.huang58292b32024-01-03 14:09:51 +08003839 if( listCURRENT_LIST_LENGTH( pxList ) > ( UBaseType_t ) 0 )
3840 {
3841 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 +08003842
xiaohu.huang58292b32024-01-03 14:09:51 +08003843 /* Populate an TaskStatus_t structure within the
3844 * pxTaskStatusArray array for each task that is referenced from
3845 * pxList. See the definition of TaskStatus_t in task.h for the
3846 * meaning of each TaskStatus_t structure member. */
3847 do
3848 {
3849 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. */
3850 vTaskGetInfo( ( TaskHandle_t ) pxNextTCB, &( pxTaskStatusArray[ uxTask ] ), pdTRUE, eState );
3851 uxTask++;
3852 } while( pxNextTCB != pxFirstTCB );
3853 }
3854 else
3855 {
3856 mtCOVERAGE_TEST_MARKER();
3857 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003858
xiaohu.huang58292b32024-01-03 14:09:51 +08003859 return uxTask;
3860 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003861
3862#endif /* configUSE_TRACE_FACILITY */
3863/*-----------------------------------------------------------*/
3864
3865#if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) )
3866
xiaohu.huang58292b32024-01-03 14:09:51 +08003867 static configSTACK_DEPTH_TYPE prvTaskCheckFreeStackSpace( const uint8_t * pucStackByte )
3868 {
3869 uint32_t ulCount = 0U;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003870
xiaohu.huang58292b32024-01-03 14:09:51 +08003871 while( *pucStackByte == ( uint8_t ) tskSTACK_FILL_BYTE )
3872 {
3873 pucStackByte -= portSTACK_GROWTH;
3874 ulCount++;
3875 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003876
xiaohu.huang58292b32024-01-03 14:09:51 +08003877 ulCount /= ( uint32_t ) sizeof( StackType_t ); /*lint !e961 Casting is not redundant on smaller architectures. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003878
xiaohu.huang58292b32024-01-03 14:09:51 +08003879 return ( configSTACK_DEPTH_TYPE ) ulCount;
3880 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003881
3882#endif /* ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 ) ) */
3883/*-----------------------------------------------------------*/
3884
3885#if ( INCLUDE_uxTaskGetStackHighWaterMark2 == 1 )
3886
xiaohu.huang58292b32024-01-03 14:09:51 +08003887/* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are the
3888 * same except for their return type. Using configSTACK_DEPTH_TYPE allows the
3889 * user to determine the return type. It gets around the problem of the value
3890 * overflowing on 8-bit types without breaking backward compatibility for
3891 * applications that expect an 8-bit return type. */
3892 configSTACK_DEPTH_TYPE uxTaskGetStackHighWaterMark2( TaskHandle_t xTask )
3893 {
3894 TCB_t * pxTCB;
3895 uint8_t * pucEndOfStack;
3896 configSTACK_DEPTH_TYPE uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003897
xiaohu.huang58292b32024-01-03 14:09:51 +08003898 /* uxTaskGetStackHighWaterMark() and uxTaskGetStackHighWaterMark2() are
3899 * the same except for their return type. Using configSTACK_DEPTH_TYPE
3900 * allows the user to determine the return type. It gets around the
3901 * problem of the value overflowing on 8-bit types without breaking
3902 * backward compatibility for applications that expect an 8-bit return
3903 * type. */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003904
xiaohu.huang58292b32024-01-03 14:09:51 +08003905 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003906
xiaohu.huang58292b32024-01-03 14:09:51 +08003907 #if portSTACK_GROWTH < 0
3908 {
3909 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
3910 }
3911 #else
3912 {
3913 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
3914 }
3915 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003916
xiaohu.huang58292b32024-01-03 14:09:51 +08003917 uxReturn = prvTaskCheckFreeStackSpace( pucEndOfStack );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003918
xiaohu.huang58292b32024-01-03 14:09:51 +08003919 return uxReturn;
3920 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003921
3922#endif /* INCLUDE_uxTaskGetStackHighWaterMark2 */
3923/*-----------------------------------------------------------*/
3924
3925#if ( INCLUDE_uxTaskGetStackHighWaterMark == 1 )
3926
xiaohu.huang58292b32024-01-03 14:09:51 +08003927 UBaseType_t uxTaskGetStackHighWaterMark( TaskHandle_t xTask )
3928 {
3929 TCB_t * pxTCB;
3930 uint8_t * pucEndOfStack;
3931 UBaseType_t uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003932
xiaohu.huang58292b32024-01-03 14:09:51 +08003933 pxTCB = prvGetTCBFromHandle( xTask );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003934
xiaohu.huang58292b32024-01-03 14:09:51 +08003935 #if portSTACK_GROWTH < 0
3936 {
3937 pucEndOfStack = ( uint8_t * ) pxTCB->pxStack;
3938 }
3939 #else
3940 {
3941 pucEndOfStack = ( uint8_t * ) pxTCB->pxEndOfStack;
3942 }
3943 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003944
xiaohu.huang58292b32024-01-03 14:09:51 +08003945 uxReturn = ( UBaseType_t ) prvTaskCheckFreeStackSpace( pucEndOfStack );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003946
xiaohu.huang58292b32024-01-03 14:09:51 +08003947 return uxReturn;
3948 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003949
3950#endif /* INCLUDE_uxTaskGetStackHighWaterMark */
3951/*-----------------------------------------------------------*/
3952
3953#if ( INCLUDE_vTaskDelete == 1 )
3954
xiaohu.huang58292b32024-01-03 14:09:51 +08003955 static void prvDeleteTCB( TCB_t * pxTCB )
3956 {
3957 /* This call is required specifically for the TriCore port. It must be
3958 * above the vPortFree() calls. The call is also used by ports/demos that
3959 * want to allocate and clean RAM statically. */
3960 portCLEAN_UP_TCB( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003961
xiaohu.huang58292b32024-01-03 14:09:51 +08003962 #if ( ( configUSE_NEWLIB_REENTRANT == 1 ) || ( configUSE_C_RUNTIME_TLS_SUPPORT == 1 ) )
3963 {
3964 /* Free up the memory allocated for the task's TLS Block. */
3965 configDEINIT_TLS_BLOCK( pxCurrentTCB->xTLSBlock );
3966 }
3967 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08003968
xiaohu.huang58292b32024-01-03 14:09:51 +08003969 #if ( ( configSUPPORT_DYNAMIC_ALLOCATION == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 0 ) && ( portUSING_MPU_WRAPPERS == 0 ) )
3970 {
3971 /* The task can only have been allocated dynamically - free both
3972 * the stack and TCB. */
3973 vPortFreeStack( pxTCB->pxStack );
3974 vPortFree( pxTCB );
3975 }
3976 #elif ( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 !e9029 Macro has been consolidated for readability reasons. */
3977 {
3978 /* The task could have been allocated statically or dynamically, so
3979 * check what was statically allocated before trying to free the
3980 * memory. */
3981 if( pxTCB->ucStaticallyAllocated == tskDYNAMICALLY_ALLOCATED_STACK_AND_TCB )
3982 {
3983 /* Both the stack and TCB were allocated dynamically, so both
3984 * must be freed. */
3985 vPortFreeStack( pxTCB->pxStack );
3986 vPortFree( pxTCB );
3987 }
3988 else if( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_ONLY )
3989 {
3990 /* Only the stack was statically allocated, so the TCB is the
3991 * only memory that must be freed. */
3992 vPortFree( pxTCB );
3993 }
3994 else
3995 {
3996 /* Neither the stack nor the TCB were allocated dynamically, so
3997 * nothing needs to be freed. */
3998 configASSERT( pxTCB->ucStaticallyAllocated == tskSTATICALLY_ALLOCATED_STACK_AND_TCB );
3999 mtCOVERAGE_TEST_MARKER();
4000 }
4001 }
4002 #endif /* configSUPPORT_DYNAMIC_ALLOCATION */
4003 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004004
4005#endif /* INCLUDE_vTaskDelete */
4006/*-----------------------------------------------------------*/
4007
4008static void prvResetNextTaskUnblockTime( void )
4009{
xiaohu.huang58292b32024-01-03 14:09:51 +08004010 if( listLIST_IS_EMPTY( pxDelayedTaskList ) != pdFALSE )
4011 {
4012 /* The new current delayed list is empty. Set xNextTaskUnblockTime to
4013 * the maximum possible value so it is extremely unlikely that the
4014 * if( xTickCount >= xNextTaskUnblockTime ) test will pass until
4015 * there is an item in the delayed list. */
4016 xNextTaskUnblockTime = portMAX_DELAY;
4017 }
4018 else
4019 {
4020 /* The new current delayed list is not empty, get the value of
4021 * the item at the head of the delayed list. This is the time at
4022 * which the task at the head of the delayed list should be removed
4023 * from the Blocked state. */
4024 xNextTaskUnblockTime = listGET_ITEM_VALUE_OF_HEAD_ENTRY( pxDelayedTaskList );
4025 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004026}
4027/*-----------------------------------------------------------*/
4028
4029#if ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) )
4030
xiaohu.huang58292b32024-01-03 14:09:51 +08004031 TaskHandle_t xTaskGetCurrentTaskHandle( void )
4032 {
4033 TaskHandle_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004034
xiaohu.huang58292b32024-01-03 14:09:51 +08004035 /* A critical section is not required as this is not called from
4036 * an interrupt and the current TCB will always be the same for any
4037 * individual execution thread. */
4038 xReturn = pxCurrentTCB;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004039
xiaohu.huang58292b32024-01-03 14:09:51 +08004040 return xReturn;
4041 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004042
4043#endif /* ( ( INCLUDE_xTaskGetCurrentTaskHandle == 1 ) || ( configUSE_MUTEXES == 1 ) ) */
4044/*-----------------------------------------------------------*/
4045
4046#if ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) )
4047
xiaohu.huang58292b32024-01-03 14:09:51 +08004048 BaseType_t xTaskGetSchedulerState( void )
4049 {
4050 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004051
xiaohu.huang58292b32024-01-03 14:09:51 +08004052 if( xSchedulerRunning == pdFALSE )
4053 {
4054 xReturn = taskSCHEDULER_NOT_STARTED;
4055 }
4056 else
4057 {
4058 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
4059 {
4060 xReturn = taskSCHEDULER_RUNNING;
4061 }
4062 else
4063 {
4064 xReturn = taskSCHEDULER_SUSPENDED;
4065 }
4066 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004067
xiaohu.huang58292b32024-01-03 14:09:51 +08004068 return xReturn;
4069 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004070
4071#endif /* ( ( INCLUDE_xTaskGetSchedulerState == 1 ) || ( configUSE_TIMERS == 1 ) ) */
4072/*-----------------------------------------------------------*/
4073
4074#if ( configUSE_MUTEXES == 1 )
4075
xiaohu.huang58292b32024-01-03 14:09:51 +08004076 BaseType_t xTaskPriorityInherit( TaskHandle_t const pxMutexHolder )
4077 {
4078 TCB_t * const pxMutexHolderTCB = pxMutexHolder;
4079 BaseType_t xReturn = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004080
xiaohu.huang58292b32024-01-03 14:09:51 +08004081 /* If the mutex was given back by an interrupt while the queue was
4082 * locked then the mutex holder might now be NULL. _RB_ Is this still
4083 * needed as interrupts can no longer use mutexes? */
4084 if( pxMutexHolder != NULL )
4085 {
4086 /* If the holder of the mutex has a priority below the priority of
4087 * the task attempting to obtain the mutex then it will temporarily
4088 * inherit the priority of the task attempting to obtain the mutex. */
4089 if( pxMutexHolderTCB->uxPriority < pxCurrentTCB->uxPriority )
4090 {
4091 /* Adjust the mutex holder state to account for its new
4092 * priority. Only reset the event list item value if the value is
4093 * not being used for anything else. */
4094 if( ( listGET_LIST_ITEM_VALUE( &( pxMutexHolderTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4095 {
4096 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. */
4097 }
4098 else
4099 {
4100 mtCOVERAGE_TEST_MARKER();
4101 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004102
xiaohu.huang58292b32024-01-03 14:09:51 +08004103 /* If the task being modified is in the ready state it will need
4104 * to be moved into a new list. */
4105 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ pxMutexHolderTCB->uxPriority ] ), &( pxMutexHolderTCB->xStateListItem ) ) != pdFALSE )
4106 {
4107 if( uxListRemove( &( pxMutexHolderTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4108 {
4109 /* It is known that the task is in its ready list so
4110 * there is no need to check again and the port level
4111 * reset macro can be called directly. */
4112 portRESET_READY_PRIORITY( pxMutexHolderTCB->uxPriority, uxTopReadyPriority );
4113 }
4114 else
4115 {
4116 mtCOVERAGE_TEST_MARKER();
4117 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004118
xiaohu.huang58292b32024-01-03 14:09:51 +08004119 /* Inherit the priority before being moved into the new list. */
4120 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4121 prvAddTaskToReadyList( pxMutexHolderTCB );
4122 }
4123 else
4124 {
4125 /* Just inherit the priority. */
4126 pxMutexHolderTCB->uxPriority = pxCurrentTCB->uxPriority;
4127 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004128
xiaohu.huang58292b32024-01-03 14:09:51 +08004129 traceTASK_PRIORITY_INHERIT( pxMutexHolderTCB, pxCurrentTCB->uxPriority );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004130
xiaohu.huang58292b32024-01-03 14:09:51 +08004131 /* Inheritance occurred. */
4132 xReturn = pdTRUE;
4133 }
4134 else
4135 {
4136 if( pxMutexHolderTCB->uxBasePriority < pxCurrentTCB->uxPriority )
4137 {
4138 /* The base priority of the mutex holder is lower than the
4139 * priority of the task attempting to take the mutex, but the
4140 * current priority of the mutex holder is not lower than the
4141 * priority of the task attempting to take the mutex.
4142 * Therefore the mutex holder must have already inherited a
4143 * priority, but inheritance would have occurred if that had
4144 * not been the case. */
4145 xReturn = pdTRUE;
4146 }
4147 else
4148 {
4149 mtCOVERAGE_TEST_MARKER();
4150 }
4151 }
4152 }
4153 else
4154 {
4155 mtCOVERAGE_TEST_MARKER();
4156 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004157
xiaohu.huang58292b32024-01-03 14:09:51 +08004158 return xReturn;
4159 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004160
4161#endif /* configUSE_MUTEXES */
4162/*-----------------------------------------------------------*/
4163
4164#if ( configUSE_MUTEXES == 1 )
4165
xiaohu.huang58292b32024-01-03 14:09:51 +08004166 BaseType_t xTaskPriorityDisinherit( TaskHandle_t const pxMutexHolder )
4167 {
4168 TCB_t * const pxTCB = pxMutexHolder;
4169 BaseType_t xReturn = pdFALSE;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004170
xiaohu.huang58292b32024-01-03 14:09:51 +08004171 if( pxMutexHolder != NULL )
4172 {
4173 /* A task can only have an inherited priority if it holds the mutex.
4174 * If the mutex is held by a task then it cannot be given from an
4175 * interrupt, and if a mutex is given by the holding task then it must
4176 * be the running state task. */
4177 configASSERT( pxTCB == pxCurrentTCB );
4178 configASSERT( pxTCB->uxMutexesHeld );
4179 ( pxTCB->uxMutexesHeld )--;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004180
xiaohu.huang58292b32024-01-03 14:09:51 +08004181 /* Has the holder of the mutex inherited the priority of another
4182 * task? */
4183 if( pxTCB->uxPriority != pxTCB->uxBasePriority )
4184 {
4185 /* Only disinherit if no other mutexes are held. */
4186 if( pxTCB->uxMutexesHeld == ( UBaseType_t ) 0 )
4187 {
4188 /* A task can only have an inherited priority if it holds
4189 * the mutex. If the mutex is held by a task then it cannot be
4190 * given from an interrupt, and if a mutex is given by the
4191 * holding task then it must be the running state task. Remove
4192 * the holding task from the ready list. */
4193 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4194 {
4195 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
4196 }
4197 else
4198 {
4199 mtCOVERAGE_TEST_MARKER();
4200 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004201
xiaohu.huang58292b32024-01-03 14:09:51 +08004202 /* Disinherit the priority before adding the task into the
4203 * new ready list. */
4204 traceTASK_PRIORITY_DISINHERIT( pxTCB, pxTCB->uxBasePriority );
4205 pxTCB->uxPriority = pxTCB->uxBasePriority;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004206
xiaohu.huang58292b32024-01-03 14:09:51 +08004207 /* Reset the event list item value. It cannot be in use for
4208 * any other purpose if this task is running, and it must be
4209 * running to give back the mutex. */
4210 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. */
4211 prvAddTaskToReadyList( pxTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004212
xiaohu.huang58292b32024-01-03 14:09:51 +08004213 /* Return true to indicate that a context switch is required.
4214 * This is only actually required in the corner case whereby
4215 * multiple mutexes were held and the mutexes were given back
4216 * in an order different to that in which they were taken.
4217 * If a context switch did not occur when the first mutex was
4218 * returned, even if a task was waiting on it, then a context
4219 * switch should occur when the last mutex is returned whether
4220 * a task is waiting on it or not. */
4221 xReturn = pdTRUE;
4222 }
4223 else
4224 {
4225 mtCOVERAGE_TEST_MARKER();
4226 }
4227 }
4228 else
4229 {
4230 mtCOVERAGE_TEST_MARKER();
4231 }
4232 }
4233 else
4234 {
4235 mtCOVERAGE_TEST_MARKER();
4236 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004237
xiaohu.huang58292b32024-01-03 14:09:51 +08004238 return xReturn;
4239 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004240
4241#endif /* configUSE_MUTEXES */
4242/*-----------------------------------------------------------*/
4243
4244#if ( configUSE_MUTEXES == 1 )
4245
xiaohu.huang58292b32024-01-03 14:09:51 +08004246 void vTaskPriorityDisinheritAfterTimeout( TaskHandle_t const pxMutexHolder,
4247 UBaseType_t uxHighestPriorityWaitingTask )
4248 {
4249 TCB_t * const pxTCB = pxMutexHolder;
4250 UBaseType_t uxPriorityUsedOnEntry, uxPriorityToUse;
4251 const UBaseType_t uxOnlyOneMutexHeld = ( UBaseType_t ) 1;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004252
xiaohu.huang58292b32024-01-03 14:09:51 +08004253 if( pxMutexHolder != NULL )
4254 {
4255 /* If pxMutexHolder is not NULL then the holder must hold at least
4256 * one mutex. */
4257 configASSERT( pxTCB->uxMutexesHeld );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004258
xiaohu.huang58292b32024-01-03 14:09:51 +08004259 /* Determine the priority to which the priority of the task that
4260 * holds the mutex should be set. This will be the greater of the
4261 * holding task's base priority and the priority of the highest
4262 * priority task that is waiting to obtain the mutex. */
4263 if( pxTCB->uxBasePriority < uxHighestPriorityWaitingTask )
4264 {
4265 uxPriorityToUse = uxHighestPriorityWaitingTask;
4266 }
4267 else
4268 {
4269 uxPriorityToUse = pxTCB->uxBasePriority;
4270 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004271
xiaohu.huang58292b32024-01-03 14:09:51 +08004272 /* Does the priority need to change? */
4273 if( pxTCB->uxPriority != uxPriorityToUse )
4274 {
4275 /* Only disinherit if no other mutexes are held. This is a
4276 * simplification in the priority inheritance implementation. If
4277 * the task that holds the mutex is also holding other mutexes then
4278 * the other mutexes may have caused the priority inheritance. */
4279 if( pxTCB->uxMutexesHeld == uxOnlyOneMutexHeld )
4280 {
4281 /* If a task has timed out because it already holds the
4282 * mutex it was trying to obtain then it cannot of inherited
4283 * its own priority. */
4284 configASSERT( pxTCB != pxCurrentTCB );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004285
xiaohu.huang58292b32024-01-03 14:09:51 +08004286 /* Disinherit the priority, remembering the previous
4287 * priority to facilitate determining the subject task's
4288 * state. */
4289 traceTASK_PRIORITY_DISINHERIT( pxTCB, uxPriorityToUse );
4290 uxPriorityUsedOnEntry = pxTCB->uxPriority;
4291 pxTCB->uxPriority = uxPriorityToUse;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004292
xiaohu.huang58292b32024-01-03 14:09:51 +08004293 /* Only reset the event list item value if the value is not
4294 * being used for anything else. */
4295 if( ( listGET_LIST_ITEM_VALUE( &( pxTCB->xEventListItem ) ) & taskEVENT_LIST_ITEM_VALUE_IN_USE ) == 0UL )
4296 {
4297 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. */
4298 }
4299 else
4300 {
4301 mtCOVERAGE_TEST_MARKER();
4302 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004303
xiaohu.huang58292b32024-01-03 14:09:51 +08004304 /* If the running task is not the task that holds the mutex
4305 * then the task that holds the mutex could be in either the
4306 * Ready, Blocked or Suspended states. Only remove the task
4307 * from its current state list if it is in the Ready state as
4308 * the task's priority is going to change and there is one
4309 * Ready list per priority. */
4310 if( listIS_CONTAINED_WITHIN( &( pxReadyTasksLists[ uxPriorityUsedOnEntry ] ), &( pxTCB->xStateListItem ) ) != pdFALSE )
4311 {
4312 if( uxListRemove( &( pxTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
4313 {
4314 /* It is known that the task is in its ready list so
4315 * there is no need to check again and the port level
4316 * reset macro can be called directly. */
4317 portRESET_READY_PRIORITY( pxTCB->uxPriority, uxTopReadyPriority );
4318 }
4319 else
4320 {
4321 mtCOVERAGE_TEST_MARKER();
4322 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004323
xiaohu.huang58292b32024-01-03 14:09:51 +08004324 prvAddTaskToReadyList( pxTCB );
4325 }
4326 else
4327 {
4328 mtCOVERAGE_TEST_MARKER();
4329 }
4330 }
4331 else
4332 {
4333 mtCOVERAGE_TEST_MARKER();
4334 }
4335 }
4336 else
4337 {
4338 mtCOVERAGE_TEST_MARKER();
4339 }
4340 }
4341 else
4342 {
4343 mtCOVERAGE_TEST_MARKER();
4344 }
4345 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004346
4347#endif /* configUSE_MUTEXES */
4348/*-----------------------------------------------------------*/
4349
4350#if ( portCRITICAL_NESTING_IN_TCB == 1 )
4351
xiaohu.huang58292b32024-01-03 14:09:51 +08004352 void vTaskEnterCritical( void )
4353 {
4354 portDISABLE_INTERRUPTS();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004355
xiaohu.huang58292b32024-01-03 14:09:51 +08004356 if( xSchedulerRunning != pdFALSE )
4357 {
4358 ( pxCurrentTCB->uxCriticalNesting )++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004359
xiaohu.huang58292b32024-01-03 14:09:51 +08004360 /* This is not the interrupt safe version of the enter critical
4361 * function so assert() if it is being called from an interrupt
4362 * context. Only API functions that end in "FromISR" can be used in an
4363 * interrupt. Only assert if the critical nesting count is 1 to
4364 * protect against recursive calls if the assert function also uses a
4365 * critical section. */
4366 if( pxCurrentTCB->uxCriticalNesting == 1 )
4367 {
4368 portASSERT_IF_IN_ISR();
4369 }
4370 }
4371 else
4372 {
4373 mtCOVERAGE_TEST_MARKER();
4374 }
4375 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004376
4377#endif /* portCRITICAL_NESTING_IN_TCB */
4378/*-----------------------------------------------------------*/
4379
4380#if ( portCRITICAL_NESTING_IN_TCB == 1 )
4381
xiaohu.huang58292b32024-01-03 14:09:51 +08004382 void vTaskExitCritical( void )
4383 {
4384 if( xSchedulerRunning != pdFALSE )
4385 {
4386 if( pxCurrentTCB->uxCriticalNesting > 0U )
4387 {
4388 ( pxCurrentTCB->uxCriticalNesting )--;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004389
xiaohu.huang58292b32024-01-03 14:09:51 +08004390 if( pxCurrentTCB->uxCriticalNesting == 0U )
4391 {
4392 portENABLE_INTERRUPTS();
4393 }
4394 else
4395 {
4396 mtCOVERAGE_TEST_MARKER();
4397 }
4398 }
4399 else
4400 {
4401 mtCOVERAGE_TEST_MARKER();
4402 }
4403 }
4404 else
4405 {
4406 mtCOVERAGE_TEST_MARKER();
4407 }
4408 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004409
4410#endif /* portCRITICAL_NESTING_IN_TCB */
4411/*-----------------------------------------------------------*/
4412
xiaohu.huang58292b32024-01-03 14:09:51 +08004413#if ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 )
4414
4415 static char * prvWriteNameToBuffer( char * pcBuffer,
4416 const char * pcTaskName )
4417 {
4418 size_t x;
4419
4420 /* Start by copying the entire string. */
4421 strcpy( pcBuffer, pcTaskName );
4422
4423 /* Pad the end of the string with spaces to ensure columns line up when
4424 * printed out. */
4425 for( x = strlen( pcBuffer ); x < ( size_t ) ( configMAX_TASK_NAME_LEN - 1 ); x++ )
4426 {
4427 pcBuffer[ x ] = ' ';
4428 }
4429
4430 /* Terminate. */
4431 pcBuffer[ x ] = ( char ) 0x00;
4432
4433 /* Return the new end of string. */
4434 return &( pcBuffer[ x ] );
4435 }
4436
4437#endif /* ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) */
4438/*-----------------------------------------------------------*/
4439
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004440#if ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) )
4441
xiaohu.huang58292b32024-01-03 14:09:51 +08004442 void vTaskList( char * pcWriteBuffer )
4443 {
4444 TaskStatus_t * pxTaskStatusArray;
4445 UBaseType_t uxArraySize, x;
4446 char cStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004447
xiaohu.huang58292b32024-01-03 14:09:51 +08004448 /*
4449 * PLEASE NOTE:
4450 *
4451 * This function is provided for convenience only, and is used by many
4452 * of the demo applications. Do not consider it to be part of the
4453 * scheduler.
4454 *
4455 * vTaskList() calls uxTaskGetSystemState(), then formats part of the
4456 * uxTaskGetSystemState() output into a human readable table that
4457 * displays task: names, states, priority, stack usage and task number.
4458 * Stack usage specified as the number of unused StackType_t words stack can hold
4459 * on top of stack - not the number of bytes.
4460 *
4461 * vTaskList() has a dependency on the sprintf() C library function that
4462 * might bloat the code size, use a lot of stack, and provide different
4463 * results on different platforms. An alternative, tiny, third party,
4464 * and limited functionality implementation of sprintf() is provided in
4465 * many of the FreeRTOS/Demo sub-directories in a file called
4466 * printf-stdarg.c (note printf-stdarg.c does not provide a full
4467 * snprintf() implementation!).
4468 *
4469 * It is recommended that production systems call uxTaskGetSystemState()
4470 * directly to get access to raw stats data, rather than indirectly
4471 * through a call to vTaskList().
4472 */
Xiaohu.Huangdbadd052022-01-26 17:30:36 +08004473
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004474
xiaohu.huang58292b32024-01-03 14:09:51 +08004475 /* Make sure the write buffer does not contain a string. */
4476 *pcWriteBuffer = ( char ) 0x00;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004477
xiaohu.huang58292b32024-01-03 14:09:51 +08004478 /* Take a snapshot of the number of tasks in case it changes while this
4479 * function is executing. */
4480 uxArraySize = uxCurrentNumberOfTasks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004481
xiaohu.huang58292b32024-01-03 14:09:51 +08004482 /* Allocate an array index for each task. NOTE! if
4483 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
4484 * equate to NULL. */
4485 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 +08004486
xiaohu.huang58292b32024-01-03 14:09:51 +08004487 if( pxTaskStatusArray != NULL )
4488 {
4489 /* Generate the (binary) data. */
4490 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, NULL );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004491
xiaohu.huang58292b32024-01-03 14:09:51 +08004492 /* Create a human readable table from the binary data. */
4493 for( x = 0; x < uxArraySize; x++ )
4494 {
4495 switch( pxTaskStatusArray[ x ].eCurrentState )
4496 {
4497 case eRunning:
4498 cStatus = tskRUNNING_CHAR;
4499 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004500
xiaohu.huang58292b32024-01-03 14:09:51 +08004501 case eReady:
4502 cStatus = tskREADY_CHAR;
4503 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004504
xiaohu.huang58292b32024-01-03 14:09:51 +08004505 case eBlocked:
4506 cStatus = tskBLOCKED_CHAR;
4507 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004508
xiaohu.huang58292b32024-01-03 14:09:51 +08004509 case eSuspended:
4510 cStatus = tskSUSPENDED_CHAR;
4511 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004512
xiaohu.huang58292b32024-01-03 14:09:51 +08004513 case eDeleted:
4514 cStatus = tskDELETED_CHAR;
4515 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004516
xiaohu.huang58292b32024-01-03 14:09:51 +08004517 case eInvalid: /* Fall through. */
4518 default: /* Should not get here, but it is included
4519 * to prevent static checking errors. */
4520 cStatus = ( char ) 0x00;
4521 break;
4522 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004523
xiaohu.huang58292b32024-01-03 14:09:51 +08004524 /* Write the task name to the string, padding with spaces so it
4525 * can be printed in tabular form more easily. */
4526 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004527
xiaohu.huang58292b32024-01-03 14:09:51 +08004528 /* Write the rest of the string. */
4529 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. */
4530 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. */
4531 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004532
xiaohu.huang58292b32024-01-03 14:09:51 +08004533 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
4534 * is 0 then vPortFree() will be #defined to nothing. */
4535 vPortFree( pxTaskStatusArray );
4536 }
4537 else
4538 {
4539 mtCOVERAGE_TEST_MARKER();
4540 }
4541 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004542
xiaohu.huang58292b32024-01-03 14:09:51 +08004543#endif /* ( ( configUSE_TRACE_FACILITY == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004544/*----------------------------------------------------------*/
4545
xiaohu.huang58292b32024-01-03 14:09:51 +08004546#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) && ( configUSE_TRACE_FACILITY == 1 ) )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004547
xiaohu.huang58292b32024-01-03 14:09:51 +08004548 void vTaskGetRunTimeStats( char * pcWriteBuffer )
4549 {
4550 TaskStatus_t * pxTaskStatusArray;
4551 UBaseType_t uxArraySize, x;
4552 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulStatsAsPercentage;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004553
xiaohu.huang58292b32024-01-03 14:09:51 +08004554 /*
4555 * PLEASE NOTE:
4556 *
4557 * This function is provided for convenience only, and is used by many
4558 * of the demo applications. Do not consider it to be part of the
4559 * scheduler.
4560 *
4561 * vTaskGetRunTimeStats() calls uxTaskGetSystemState(), then formats part
4562 * of the uxTaskGetSystemState() output into a human readable table that
4563 * displays the amount of time each task has spent in the Running state
4564 * in both absolute and percentage terms.
4565 *
4566 * vTaskGetRunTimeStats() has a dependency on the sprintf() C library
4567 * function that might bloat the code size, use a lot of stack, and
4568 * provide different results on different platforms. An alternative,
4569 * tiny, third party, and limited functionality implementation of
4570 * sprintf() is provided in many of the FreeRTOS/Demo sub-directories in
4571 * a file called printf-stdarg.c (note printf-stdarg.c does not provide
4572 * a full snprintf() implementation!).
4573 *
4574 * It is recommended that production systems call uxTaskGetSystemState()
4575 * directly to get access to raw stats data, rather than indirectly
4576 * through a call to vTaskGetRunTimeStats().
4577 */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004578
xiaohu.huang58292b32024-01-03 14:09:51 +08004579 /* Make sure the write buffer does not contain a string. */
4580 *pcWriteBuffer = ( char ) 0x00;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004581
xiaohu.huang58292b32024-01-03 14:09:51 +08004582 /* Take a snapshot of the number of tasks in case it changes while this
4583 * function is executing. */
4584 uxArraySize = uxCurrentNumberOfTasks;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004585
xiaohu.huang58292b32024-01-03 14:09:51 +08004586 /* Allocate an array index for each task. NOTE! If
4587 * configSUPPORT_DYNAMIC_ALLOCATION is set to 0 then pvPortMalloc() will
4588 * equate to NULL. */
4589 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 +08004590
xiaohu.huang58292b32024-01-03 14:09:51 +08004591 if( pxTaskStatusArray != NULL )
4592 {
4593 /* Generate the (binary) data. */
4594 uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalTime );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004595
xiaohu.huang58292b32024-01-03 14:09:51 +08004596 /* For percentage calculations. */
4597 ulTotalTime /= 100UL;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004598
xiaohu.huang58292b32024-01-03 14:09:51 +08004599 /* Avoid divide by zero errors. */
4600 if( ulTotalTime > 0UL )
4601 {
4602 /* Create a human readable table from the binary data. */
4603 for( x = 0; x < uxArraySize; x++ )
4604 {
4605 /* What percentage of the total run time has the task used?
4606 * This will always be rounded down to the nearest integer.
4607 * ulTotalRunTime has already been divided by 100. */
4608 ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalTime;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004609
xiaohu.huang58292b32024-01-03 14:09:51 +08004610 /* Write the task name to the string, padding with
4611 * spaces so it can be printed in tabular form more
4612 * easily. */
4613 pcWriteBuffer = prvWriteNameToBuffer( pcWriteBuffer, pxTaskStatusArray[ x ].pcTaskName );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004614
xiaohu.huang58292b32024-01-03 14:09:51 +08004615 if( ulStatsAsPercentage > 0UL )
4616 {
4617 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
4618 {
4619 sprintf( pcWriteBuffer, "\t%lu\t\t%lu%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
4620 }
4621 #else
4622 {
4623 /* sizeof( int ) == sizeof( long ) so a smaller
4624 * printf() library can be used. */
4625 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. */
4626 }
4627 #endif
4628 }
4629 else
4630 {
4631 /* If the percentage is zero here then the task has
4632 * consumed less than 1% of the total run time. */
4633 #ifdef portLU_PRINTF_SPECIFIER_REQUIRED
4634 {
4635 sprintf( pcWriteBuffer, "\t%lu\t\t<1%%\r\n", pxTaskStatusArray[ x ].ulRunTimeCounter );
4636 }
4637 #else
4638 {
4639 /* sizeof( int ) == sizeof( long ) so a smaller
4640 * printf() library can be used. */
4641 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. */
4642 }
4643 #endif
4644 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004645
xiaohu.huang58292b32024-01-03 14:09:51 +08004646 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. */
4647 }
4648 }
4649 else
4650 {
4651 mtCOVERAGE_TEST_MARKER();
4652 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004653
xiaohu.huang58292b32024-01-03 14:09:51 +08004654 /* Free the array again. NOTE! If configSUPPORT_DYNAMIC_ALLOCATION
4655 * is 0 then vPortFree() will be #defined to nothing. */
4656 vPortFree( pxTaskStatusArray );
4657 }
4658 else
4659 {
4660 mtCOVERAGE_TEST_MARKER();
4661 }
4662 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004663
xiaohu.huang58292b32024-01-03 14:09:51 +08004664#endif /* ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( configUSE_STATS_FORMATTING_FUNCTIONS > 0 ) ) */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004665/*-----------------------------------------------------------*/
4666
4667TickType_t uxTaskResetEventItemValue( void )
4668{
xiaohu.huang58292b32024-01-03 14:09:51 +08004669 TickType_t uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004670
xiaohu.huang58292b32024-01-03 14:09:51 +08004671 uxReturn = listGET_LIST_ITEM_VALUE( &( pxCurrentTCB->xEventListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004672
xiaohu.huang58292b32024-01-03 14:09:51 +08004673 /* Reset the event list item to its normal value - so it can be used with
4674 * queues and semaphores. */
4675 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 +08004676
xiaohu.huang58292b32024-01-03 14:09:51 +08004677 return uxReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004678}
4679/*-----------------------------------------------------------*/
4680
4681#if ( configUSE_MUTEXES == 1 )
4682
xiaohu.huang58292b32024-01-03 14:09:51 +08004683 TaskHandle_t pvTaskIncrementMutexHeldCount( void )
4684 {
4685 /* If xSemaphoreCreateMutex() is called before any tasks have been created
4686 * then pxCurrentTCB will be NULL. */
4687 if( pxCurrentTCB != NULL )
4688 {
4689 ( pxCurrentTCB->uxMutexesHeld )++;
4690 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004691
xiaohu.huang58292b32024-01-03 14:09:51 +08004692 return pxCurrentTCB;
4693 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004694
4695#endif /* configUSE_MUTEXES */
4696/*-----------------------------------------------------------*/
4697
xiaohu.huang58292b32024-01-03 14:09:51 +08004698#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004699
xiaohu.huang58292b32024-01-03 14:09:51 +08004700 uint32_t ulTaskGenericNotifyTake( UBaseType_t uxIndexToWait,
4701 BaseType_t xClearCountOnExit,
4702 TickType_t xTicksToWait )
4703 {
4704 uint32_t ulReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004705
xiaohu.huang58292b32024-01-03 14:09:51 +08004706 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004707
xiaohu.huang58292b32024-01-03 14:09:51 +08004708 taskENTER_CRITICAL();
4709 {
4710 /* Only block if the notification count is not already non-zero. */
4711 if( pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] == 0UL )
4712 {
4713 /* Mark this task as waiting for a notification. */
4714 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004715
xiaohu.huang58292b32024-01-03 14:09:51 +08004716 if( xTicksToWait > ( TickType_t ) 0 )
4717 {
4718 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4719 traceTASK_NOTIFY_TAKE_BLOCK( uxIndexToWait );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004720
xiaohu.huang58292b32024-01-03 14:09:51 +08004721 /* All ports are written to allow a yield in a critical
4722 * section (some will yield immediately, others wait until the
4723 * critical section exits) - but it is not something that
4724 * application code should ever do. */
4725 portYIELD_WITHIN_API();
4726 }
4727 else
4728 {
4729 mtCOVERAGE_TEST_MARKER();
4730 }
4731 }
4732 else
4733 {
4734 mtCOVERAGE_TEST_MARKER();
4735 }
4736 }
4737 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004738
xiaohu.huang58292b32024-01-03 14:09:51 +08004739 taskENTER_CRITICAL();
4740 {
4741 traceTASK_NOTIFY_TAKE( uxIndexToWait );
4742 ulReturn = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004743
xiaohu.huang58292b32024-01-03 14:09:51 +08004744 if( ulReturn != 0UL )
4745 {
4746 if( xClearCountOnExit != pdFALSE )
4747 {
4748 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = 0UL;
4749 }
4750 else
4751 {
4752 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] = ulReturn - ( uint32_t ) 1;
4753 }
4754 }
4755 else
4756 {
4757 mtCOVERAGE_TEST_MARKER();
4758 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004759
xiaohu.huang58292b32024-01-03 14:09:51 +08004760 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
4761 }
4762 taskEXIT_CRITICAL();
4763
4764 return ulReturn;
4765 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004766
4767#endif /* configUSE_TASK_NOTIFICATIONS */
4768/*-----------------------------------------------------------*/
4769
xiaohu.huang58292b32024-01-03 14:09:51 +08004770#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004771
xiaohu.huang58292b32024-01-03 14:09:51 +08004772 BaseType_t xTaskGenericNotifyWait( UBaseType_t uxIndexToWait,
4773 uint32_t ulBitsToClearOnEntry,
4774 uint32_t ulBitsToClearOnExit,
4775 uint32_t * pulNotificationValue,
4776 TickType_t xTicksToWait )
4777 {
4778 BaseType_t xReturn;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004779
xiaohu.huang58292b32024-01-03 14:09:51 +08004780 configASSERT( uxIndexToWait < configTASK_NOTIFICATION_ARRAY_ENTRIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004781
xiaohu.huang58292b32024-01-03 14:09:51 +08004782 taskENTER_CRITICAL();
4783 {
4784 /* Only block if a notification is not already pending. */
4785 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
4786 {
4787 /* Clear bits in the task's notification value as bits may get
4788 * set by the notifying task or interrupt. This can be used to
4789 * clear the value to zero. */
4790 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnEntry;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004791
xiaohu.huang58292b32024-01-03 14:09:51 +08004792 /* Mark this task as waiting for a notification. */
4793 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskWAITING_NOTIFICATION;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004794
xiaohu.huang58292b32024-01-03 14:09:51 +08004795 if( xTicksToWait > ( TickType_t ) 0 )
4796 {
4797 prvAddCurrentTaskToDelayedList( xTicksToWait, pdTRUE );
4798 traceTASK_NOTIFY_WAIT_BLOCK( uxIndexToWait );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004799
xiaohu.huang58292b32024-01-03 14:09:51 +08004800 /* All ports are written to allow a yield in a critical
4801 * section (some will yield immediately, others wait until the
4802 * critical section exits) - but it is not something that
4803 * application code should ever do. */
4804 portYIELD_WITHIN_API();
4805 }
4806 else
4807 {
4808 mtCOVERAGE_TEST_MARKER();
4809 }
4810 }
4811 else
4812 {
4813 mtCOVERAGE_TEST_MARKER();
4814 }
4815 }
4816 taskEXIT_CRITICAL();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004817
xiaohu.huang58292b32024-01-03 14:09:51 +08004818 taskENTER_CRITICAL();
4819 {
4820 traceTASK_NOTIFY_WAIT( uxIndexToWait );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004821
xiaohu.huang58292b32024-01-03 14:09:51 +08004822 if( pulNotificationValue != NULL )
4823 {
4824 /* Output the current notification value, which may or may not
4825 * have changed. */
4826 *pulNotificationValue = pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ];
4827 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004828
xiaohu.huang58292b32024-01-03 14:09:51 +08004829 /* If ucNotifyValue is set then either the task never entered the
4830 * blocked state (because a notification was already pending) or the
4831 * task unblocked because of a notification. Otherwise the task
4832 * unblocked because of a timeout. */
4833 if( pxCurrentTCB->ucNotifyState[ uxIndexToWait ] != taskNOTIFICATION_RECEIVED )
4834 {
4835 /* A notification was not received. */
4836 xReturn = pdFALSE;
4837 }
4838 else
4839 {
4840 /* A notification was already pending or a notification was
4841 * received while the task was waiting. */
4842 pxCurrentTCB->ulNotifiedValue[ uxIndexToWait ] &= ~ulBitsToClearOnExit;
4843 xReturn = pdTRUE;
4844 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004845
xiaohu.huang58292b32024-01-03 14:09:51 +08004846 pxCurrentTCB->ucNotifyState[ uxIndexToWait ] = taskNOT_WAITING_NOTIFICATION;
4847 }
4848 taskEXIT_CRITICAL();
4849
4850 return xReturn;
4851 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004852
4853#endif /* configUSE_TASK_NOTIFICATIONS */
4854/*-----------------------------------------------------------*/
4855
xiaohu.huang58292b32024-01-03 14:09:51 +08004856#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004857
xiaohu.huang58292b32024-01-03 14:09:51 +08004858 BaseType_t xTaskGenericNotify( TaskHandle_t xTaskToNotify,
4859 UBaseType_t uxIndexToNotify,
4860 uint32_t ulValue,
4861 eNotifyAction eAction,
4862 uint32_t * pulPreviousNotificationValue )
4863 {
4864 TCB_t * pxTCB;
4865 BaseType_t xReturn = pdPASS;
4866 uint8_t ucOriginalNotifyState;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004867
xiaohu.huang58292b32024-01-03 14:09:51 +08004868 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
4869 configASSERT( xTaskToNotify );
4870 pxTCB = xTaskToNotify;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004871
xiaohu.huang58292b32024-01-03 14:09:51 +08004872 taskENTER_CRITICAL();
4873 {
4874 if( pulPreviousNotificationValue != NULL )
4875 {
4876 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
4877 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004878
xiaohu.huang58292b32024-01-03 14:09:51 +08004879 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004880
xiaohu.huang58292b32024-01-03 14:09:51 +08004881 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004882
xiaohu.huang58292b32024-01-03 14:09:51 +08004883 switch( eAction )
4884 {
4885 case eSetBits:
4886 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
4887 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004888
xiaohu.huang58292b32024-01-03 14:09:51 +08004889 case eIncrement:
4890 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
4891 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004892
xiaohu.huang58292b32024-01-03 14:09:51 +08004893 case eSetValueWithOverwrite:
4894 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
4895 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004896
xiaohu.huang58292b32024-01-03 14:09:51 +08004897 case eSetValueWithoutOverwrite:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004898
xiaohu.huang58292b32024-01-03 14:09:51 +08004899 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
4900 {
4901 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
4902 }
4903 else
4904 {
4905 /* The value could not be written to the task. */
4906 xReturn = pdFAIL;
4907 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004908
xiaohu.huang58292b32024-01-03 14:09:51 +08004909 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004910
xiaohu.huang58292b32024-01-03 14:09:51 +08004911 case eNoAction:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004912
xiaohu.huang58292b32024-01-03 14:09:51 +08004913 /* The task is being notified without its notify value being
4914 * updated. */
4915 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004916
xiaohu.huang58292b32024-01-03 14:09:51 +08004917 default:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004918
xiaohu.huang58292b32024-01-03 14:09:51 +08004919 /* Should not get here if all enums are handled.
4920 * Artificially force an assert by testing a value the
4921 * compiler can't assume is const. */
4922 configASSERT( xTickCount == ( TickType_t ) 0 );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004923
xiaohu.huang58292b32024-01-03 14:09:51 +08004924 break;
4925 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004926
xiaohu.huang58292b32024-01-03 14:09:51 +08004927 traceTASK_NOTIFY( uxIndexToNotify );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004928
xiaohu.huang58292b32024-01-03 14:09:51 +08004929 /* If the task is in the blocked state specifically to wait for a
4930 * notification then unblock it now. */
4931 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
4932 {
4933 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
4934 prvAddTaskToReadyList( pxTCB );
4935
4936 /* The task should not have been on an event list. */
4937 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
4938
4939 #if ( configUSE_TICKLESS_IDLE != 0 )
4940 {
4941 /* If a task is blocked waiting for a notification then
4942 * xNextTaskUnblockTime might be set to the blocked task's time
4943 * out time. If the task is unblocked for a reason other than
4944 * a timeout xNextTaskUnblockTime is normally left unchanged,
4945 * because it will automatically get reset to a new value when
4946 * the tick count equals xNextTaskUnblockTime. However if
4947 * tickless idling is used it might be more important to enter
4948 * sleep mode at the earliest possible time - so reset
4949 * xNextTaskUnblockTime here to ensure it is updated at the
4950 * earliest possible time. */
4951 prvResetNextTaskUnblockTime();
4952 }
4953 #endif
4954
4955 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
4956 {
4957 /* The notified task has a priority above the currently
4958 * executing task so a yield is required. */
4959 taskYIELD_IF_USING_PREEMPTION();
4960 }
4961 else
4962 {
4963 mtCOVERAGE_TEST_MARKER();
4964 }
4965 }
4966 else
4967 {
4968 mtCOVERAGE_TEST_MARKER();
4969 }
4970 }
4971 taskEXIT_CRITICAL();
4972
4973 return xReturn;
4974 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004975
4976#endif /* configUSE_TASK_NOTIFICATIONS */
4977/*-----------------------------------------------------------*/
4978
xiaohu.huang58292b32024-01-03 14:09:51 +08004979#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004980
xiaohu.huang58292b32024-01-03 14:09:51 +08004981 BaseType_t xTaskGenericNotifyFromISR( TaskHandle_t xTaskToNotify,
4982 UBaseType_t uxIndexToNotify,
4983 uint32_t ulValue,
4984 eNotifyAction eAction,
4985 uint32_t * pulPreviousNotificationValue,
4986 BaseType_t * pxHigherPriorityTaskWoken )
4987 {
4988 TCB_t * pxTCB;
4989 uint8_t ucOriginalNotifyState;
4990 BaseType_t xReturn = pdPASS;
4991 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004992
xiaohu.huang58292b32024-01-03 14:09:51 +08004993 configASSERT( xTaskToNotify );
4994 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08004995
xiaohu.huang58292b32024-01-03 14:09:51 +08004996 /* RTOS ports that support interrupt nesting have the concept of a
4997 * maximum system call (or maximum API call) interrupt priority.
4998 * Interrupts that are above the maximum system call priority are keep
4999 * permanently enabled, even when the RTOS kernel is in a critical section,
5000 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
5001 * is defined in FreeRTOSConfig.h then
5002 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5003 * failure if a FreeRTOS API function is called from an interrupt that has
5004 * been assigned a priority above the configured maximum system call
5005 * priority. Only FreeRTOS functions that end in FromISR can be called
5006 * from interrupts that have been assigned a priority at or (logically)
5007 * below the maximum system call interrupt priority. FreeRTOS maintains a
5008 * separate interrupt safe API to ensure interrupt entry is as fast and as
5009 * simple as possible. More information (albeit Cortex-M specific) is
5010 * provided on the following link:
5011 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5012 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005013
xiaohu.huang58292b32024-01-03 14:09:51 +08005014 pxTCB = xTaskToNotify;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005015
xiaohu.huang58292b32024-01-03 14:09:51 +08005016 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5017 {
5018 if( pulPreviousNotificationValue != NULL )
5019 {
5020 *pulPreviousNotificationValue = pxTCB->ulNotifiedValue[ uxIndexToNotify ];
5021 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005022
xiaohu.huang58292b32024-01-03 14:09:51 +08005023 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5024 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005025
xiaohu.huang58292b32024-01-03 14:09:51 +08005026 switch( eAction )
5027 {
5028 case eSetBits:
5029 pxTCB->ulNotifiedValue[ uxIndexToNotify ] |= ulValue;
5030 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005031
xiaohu.huang58292b32024-01-03 14:09:51 +08005032 case eIncrement:
5033 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
5034 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005035
xiaohu.huang58292b32024-01-03 14:09:51 +08005036 case eSetValueWithOverwrite:
5037 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5038 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005039
xiaohu.huang58292b32024-01-03 14:09:51 +08005040 case eSetValueWithoutOverwrite:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005041
xiaohu.huang58292b32024-01-03 14:09:51 +08005042 if( ucOriginalNotifyState != taskNOTIFICATION_RECEIVED )
5043 {
5044 pxTCB->ulNotifiedValue[ uxIndexToNotify ] = ulValue;
5045 }
5046 else
5047 {
5048 /* The value could not be written to the task. */
5049 xReturn = pdFAIL;
5050 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005051
xiaohu.huang58292b32024-01-03 14:09:51 +08005052 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005053
xiaohu.huang58292b32024-01-03 14:09:51 +08005054 case eNoAction:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005055
xiaohu.huang58292b32024-01-03 14:09:51 +08005056 /* The task is being notified without its notify value being
5057 * updated. */
5058 break;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005059
xiaohu.huang58292b32024-01-03 14:09:51 +08005060 default:
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005061
xiaohu.huang58292b32024-01-03 14:09:51 +08005062 /* Should not get here if all enums are handled.
5063 * Artificially force an assert by testing a value the
5064 * compiler can't assume is const. */
5065 configASSERT( xTickCount == ( TickType_t ) 0 );
5066 break;
5067 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005068
xiaohu.huang58292b32024-01-03 14:09:51 +08005069 traceTASK_NOTIFY_FROM_ISR( uxIndexToNotify );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005070
xiaohu.huang58292b32024-01-03 14:09:51 +08005071 /* If the task is in the blocked state specifically to wait for a
5072 * notification then unblock it now. */
5073 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5074 {
5075 /* The task should not have been on an event list. */
5076 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
5077
5078 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5079 {
5080 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
5081 prvAddTaskToReadyList( pxTCB );
5082 }
5083 else
5084 {
5085 /* The delayed and ready lists cannot be accessed, so hold
5086 * this task pending until the scheduler is resumed. */
5087 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
5088 }
5089
5090 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
5091 {
5092 /* The notified task has a priority above the currently
5093 * executing task so a yield is required. */
5094 if( pxHigherPriorityTaskWoken != NULL )
5095 {
5096 *pxHigherPriorityTaskWoken = pdTRUE;
5097 }
5098
5099 /* Mark that a yield is pending in case the user is not
5100 * using the "xHigherPriorityTaskWoken" parameter to an ISR
5101 * safe FreeRTOS function. */
5102 xYieldPending = pdTRUE;
5103 }
5104 else
5105 {
5106 mtCOVERAGE_TEST_MARKER();
5107 }
5108 }
5109 }
5110 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
5111
5112 return xReturn;
5113 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005114
5115#endif /* configUSE_TASK_NOTIFICATIONS */
5116/*-----------------------------------------------------------*/
5117
xiaohu.huang58292b32024-01-03 14:09:51 +08005118#if ( configUSE_TASK_NOTIFICATIONS == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005119
xiaohu.huang58292b32024-01-03 14:09:51 +08005120 void vTaskGenericNotifyGiveFromISR( TaskHandle_t xTaskToNotify,
5121 UBaseType_t uxIndexToNotify,
5122 BaseType_t * pxHigherPriorityTaskWoken )
5123 {
5124 TCB_t * pxTCB;
5125 uint8_t ucOriginalNotifyState;
5126 UBaseType_t uxSavedInterruptStatus;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005127
xiaohu.huang58292b32024-01-03 14:09:51 +08005128 configASSERT( xTaskToNotify );
5129 configASSERT( uxIndexToNotify < configTASK_NOTIFICATION_ARRAY_ENTRIES );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005130
xiaohu.huang58292b32024-01-03 14:09:51 +08005131 /* RTOS ports that support interrupt nesting have the concept of a
5132 * maximum system call (or maximum API call) interrupt priority.
5133 * Interrupts that are above the maximum system call priority are keep
5134 * permanently enabled, even when the RTOS kernel is in a critical section,
5135 * but cannot make any calls to FreeRTOS API functions. If configASSERT()
5136 * is defined in FreeRTOSConfig.h then
5137 * portASSERT_IF_INTERRUPT_PRIORITY_INVALID() will result in an assertion
5138 * failure if a FreeRTOS API function is called from an interrupt that has
5139 * been assigned a priority above the configured maximum system call
5140 * priority. Only FreeRTOS functions that end in FromISR can be called
5141 * from interrupts that have been assigned a priority at or (logically)
5142 * below the maximum system call interrupt priority. FreeRTOS maintains a
5143 * separate interrupt safe API to ensure interrupt entry is as fast and as
5144 * simple as possible. More information (albeit Cortex-M specific) is
5145 * provided on the following link:
5146 * https://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html */
5147 portASSERT_IF_INTERRUPT_PRIORITY_INVALID();
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005148
xiaohu.huang58292b32024-01-03 14:09:51 +08005149 pxTCB = xTaskToNotify;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005150
xiaohu.huang58292b32024-01-03 14:09:51 +08005151 uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
5152 {
5153 ucOriginalNotifyState = pxTCB->ucNotifyState[ uxIndexToNotify ];
5154 pxTCB->ucNotifyState[ uxIndexToNotify ] = taskNOTIFICATION_RECEIVED;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005155
xiaohu.huang58292b32024-01-03 14:09:51 +08005156 /* 'Giving' is equivalent to incrementing a count in a counting
5157 * semaphore. */
5158 ( pxTCB->ulNotifiedValue[ uxIndexToNotify ] )++;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005159
xiaohu.huang58292b32024-01-03 14:09:51 +08005160 traceTASK_NOTIFY_GIVE_FROM_ISR( uxIndexToNotify );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005161
xiaohu.huang58292b32024-01-03 14:09:51 +08005162 /* If the task is in the blocked state specifically to wait for a
5163 * notification then unblock it now. */
5164 if( ucOriginalNotifyState == taskWAITING_NOTIFICATION )
5165 {
5166 /* The task should not have been on an event list. */
5167 configASSERT( listLIST_ITEM_CONTAINER( &( pxTCB->xEventListItem ) ) == NULL );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005168
xiaohu.huang58292b32024-01-03 14:09:51 +08005169 if( uxSchedulerSuspended == ( UBaseType_t ) pdFALSE )
5170 {
5171 listREMOVE_ITEM( &( pxTCB->xStateListItem ) );
5172 prvAddTaskToReadyList( pxTCB );
5173 }
5174 else
5175 {
5176 /* The delayed and ready lists cannot be accessed, so hold
5177 * this task pending until the scheduler is resumed. */
5178 listINSERT_END( &( xPendingReadyList ), &( pxTCB->xEventListItem ) );
5179 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005180
xiaohu.huang58292b32024-01-03 14:09:51 +08005181 if( pxTCB->uxPriority > pxCurrentTCB->uxPriority )
5182 {
5183 /* The notified task has a priority above the currently
5184 * executing task so a yield is required. */
5185 if( pxHigherPriorityTaskWoken != NULL )
5186 {
5187 *pxHigherPriorityTaskWoken = pdTRUE;
5188 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005189
xiaohu.huang58292b32024-01-03 14:09:51 +08005190 /* Mark that a yield is pending in case the user is not
5191 * using the "xHigherPriorityTaskWoken" parameter in an ISR
5192 * safe FreeRTOS function. */
5193 xYieldPending = pdTRUE;
5194 }
5195 else
5196 {
5197 mtCOVERAGE_TEST_MARKER();
5198 }
5199 }
5200 }
5201 portCLEAR_INTERRUPT_MASK_FROM_ISR( uxSavedInterruptStatus );
5202 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005203
5204#endif /* configUSE_TASK_NOTIFICATIONS */
5205/*-----------------------------------------------------------*/
5206
xiaohu.huang58292b32024-01-03 14:09:51 +08005207#if ( configUSE_TASK_NOTIFICATIONS == 1 )
5208
5209 BaseType_t xTaskGenericNotifyStateClear( TaskHandle_t xTask,
5210 UBaseType_t uxIndexToClear )
5211 {
5212 TCB_t * pxTCB;
5213 BaseType_t xReturn;
5214
5215 configASSERT( uxIndexToClear < configTASK_NOTIFICATION_ARRAY_ENTRIES );
5216
5217 /* If null is passed in here then it is the calling task that is having
5218 * its notification state cleared. */
5219 pxTCB = prvGetTCBFromHandle( xTask );
5220
5221 taskENTER_CRITICAL();
5222 {
5223 if( pxTCB->ucNotifyState[ uxIndexToClear ] == taskNOTIFICATION_RECEIVED )
5224 {
5225 pxTCB->ucNotifyState[ uxIndexToClear ] = taskNOT_WAITING_NOTIFICATION;
5226 xReturn = pdPASS;
5227 }
5228 else
5229 {
5230 xReturn = pdFAIL;
5231 }
5232 }
5233 taskEXIT_CRITICAL();
5234
5235 return xReturn;
5236 }
5237
5238#endif /* configUSE_TASK_NOTIFICATIONS */
5239/*-----------------------------------------------------------*/
5240
5241#if ( configUSE_TASK_NOTIFICATIONS == 1 )
5242
5243 uint32_t ulTaskGenericNotifyValueClear( TaskHandle_t xTask,
5244 UBaseType_t uxIndexToClear,
5245 uint32_t ulBitsToClear )
5246 {
5247 TCB_t * pxTCB;
5248 uint32_t ulReturn;
5249
5250 /* If null is passed in here then it is the calling task that is having
5251 * its notification state cleared. */
5252 pxTCB = prvGetTCBFromHandle( xTask );
5253
5254 taskENTER_CRITICAL();
5255 {
5256 /* Return the notification as it was before the bits were cleared,
5257 * then clear the bit mask. */
5258 ulReturn = pxTCB->ulNotifiedValue[ uxIndexToClear ];
5259 pxTCB->ulNotifiedValue[ uxIndexToClear ] &= ~ulBitsToClear;
5260 }
5261 taskEXIT_CRITICAL();
5262
5263 return ulReturn;
5264 }
5265
5266#endif /* configUSE_TASK_NOTIFICATIONS */
5267/*-----------------------------------------------------------*/
5268
5269#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
5270
5271 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimeCounter( void )
5272 {
5273 return xIdleTaskHandle->ulRunTimeCounter;
5274 }
5275
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005276#endif
5277/*-----------------------------------------------------------*/
5278
xiaohu.huang58292b32024-01-03 14:09:51 +08005279#if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) )
5280
5281 configRUN_TIME_COUNTER_TYPE ulTaskGetIdleRunTimePercent( void )
5282 {
5283 configRUN_TIME_COUNTER_TYPE ulTotalTime, ulReturn;
5284
5285 ulTotalTime = portGET_RUN_TIME_COUNTER_VALUE();
5286
5287 /* For percentage calculations. */
5288 ulTotalTime /= ( configRUN_TIME_COUNTER_TYPE ) 100;
5289
5290 /* Avoid divide by zero errors. */
5291 if( ulTotalTime > ( configRUN_TIME_COUNTER_TYPE ) 0 )
5292 {
5293 ulReturn = xIdleTaskHandle->ulRunTimeCounter / ulTotalTime;
5294 }
5295 else
5296 {
5297 ulReturn = 0;
5298 }
5299
5300 return ulReturn;
5301 }
5302
5303#endif /* if ( ( configGENERATE_RUN_TIME_STATS == 1 ) && ( INCLUDE_xTaskGetIdleTaskHandle == 1 ) ) */
5304/*-----------------------------------------------------------*/
5305
5306static void prvAddCurrentTaskToDelayedList( TickType_t xTicksToWait,
5307 const BaseType_t xCanBlockIndefinitely )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005308{
xiaohu.huang58292b32024-01-03 14:09:51 +08005309 TickType_t xTimeToWake;
5310 const TickType_t xConstTickCount = xTickCount;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005311
xiaohu.huang58292b32024-01-03 14:09:51 +08005312 #if ( INCLUDE_xTaskAbortDelay == 1 )
5313 {
5314 /* About to enter a delayed list, so ensure the ucDelayAborted flag is
5315 * reset to pdFALSE so it can be detected as having been set to pdTRUE
5316 * when the task leaves the Blocked state. */
5317 pxCurrentTCB->ucDelayAborted = pdFALSE;
5318 }
5319 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005320
xiaohu.huang58292b32024-01-03 14:09:51 +08005321 /* Remove the task from the ready list before adding it to the blocked list
5322 * as the same list item is used for both lists. */
5323 if( uxListRemove( &( pxCurrentTCB->xStateListItem ) ) == ( UBaseType_t ) 0 )
5324 {
5325 /* The current task must be in a ready list, so there is no need to
5326 * check, and the port reset macro can be called directly. */
5327 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. */
5328 }
5329 else
5330 {
5331 mtCOVERAGE_TEST_MARKER();
5332 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005333
xiaohu.huang58292b32024-01-03 14:09:51 +08005334 #if ( INCLUDE_vTaskSuspend == 1 )
5335 {
5336 if( ( xTicksToWait == portMAX_DELAY ) && ( xCanBlockIndefinitely != pdFALSE ) )
5337 {
5338 /* Add the task to the suspended task list instead of a delayed task
5339 * list to ensure it is not woken by a timing event. It will block
5340 * indefinitely. */
5341 listINSERT_END( &xSuspendedTaskList, &( pxCurrentTCB->xStateListItem ) );
5342 }
5343 else
5344 {
5345 /* Calculate the time at which the task should be woken if the event
5346 * does not occur. This may overflow but this doesn't matter, the
5347 * kernel will manage it correctly. */
5348 xTimeToWake = xConstTickCount + xTicksToWait;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005349
xiaohu.huang58292b32024-01-03 14:09:51 +08005350 /* The list item will be inserted in wake time order. */
5351 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005352
xiaohu.huang58292b32024-01-03 14:09:51 +08005353 if( xTimeToWake < xConstTickCount )
5354 {
5355 /* Wake time has overflowed. Place this item in the overflow
5356 * list. */
5357 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5358 }
5359 else
5360 {
5361 /* The wake time has not overflowed, so the current block list
5362 * is used. */
5363 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005364
xiaohu.huang58292b32024-01-03 14:09:51 +08005365 /* If the task entering the blocked state was placed at the
5366 * head of the list of blocked tasks then xNextTaskUnblockTime
5367 * needs to be updated too. */
5368 if( xTimeToWake < xNextTaskUnblockTime )
5369 {
5370 xNextTaskUnblockTime = xTimeToWake;
5371 }
5372 else
5373 {
5374 mtCOVERAGE_TEST_MARKER();
5375 }
5376 }
5377 }
5378 }
5379 #else /* INCLUDE_vTaskSuspend */
5380 {
5381 /* Calculate the time at which the task should be woken if the event
5382 * does not occur. This may overflow but this doesn't matter, the kernel
5383 * will manage it correctly. */
5384 xTimeToWake = xConstTickCount + xTicksToWait;
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005385
xiaohu.huang58292b32024-01-03 14:09:51 +08005386 /* The list item will be inserted in wake time order. */
5387 listSET_LIST_ITEM_VALUE( &( pxCurrentTCB->xStateListItem ), xTimeToWake );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005388
xiaohu.huang58292b32024-01-03 14:09:51 +08005389 if( xTimeToWake < xConstTickCount )
5390 {
5391 /* Wake time has overflowed. Place this item in the overflow list. */
5392 vListInsert( pxOverflowDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
5393 }
5394 else
5395 {
5396 /* The wake time has not overflowed, so the current block list is used. */
5397 vListInsert( pxDelayedTaskList, &( pxCurrentTCB->xStateListItem ) );
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005398
xiaohu.huang58292b32024-01-03 14:09:51 +08005399 /* If the task entering the blocked state was placed at the head of the
5400 * list of blocked tasks then xNextTaskUnblockTime needs to be updated
5401 * too. */
5402 if( xTimeToWake < xNextTaskUnblockTime )
5403 {
5404 xNextTaskUnblockTime = xTimeToWake;
5405 }
5406 else
5407 {
5408 mtCOVERAGE_TEST_MARKER();
5409 }
5410 }
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005411
xiaohu.huang58292b32024-01-03 14:09:51 +08005412 /* Avoid compiler warning when INCLUDE_vTaskSuspend is not 1. */
5413 ( void ) xCanBlockIndefinitely;
5414 }
5415 #endif /* INCLUDE_vTaskSuspend */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005416}
5417
5418/* Code below here allows additional code to be inserted into this source file,
xiaohu.huang58292b32024-01-03 14:09:51 +08005419 * especially where access to file scope functions and data is needed (for example
5420 * when performing module tests). */
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005421
5422#ifdef FREERTOS_MODULE_TEST
xiaohu.huang58292b32024-01-03 14:09:51 +08005423 #include "tasks_test_access_functions.h"
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005424#endif
5425
5426
xiaohu.huang58292b32024-01-03 14:09:51 +08005427#if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 )
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005428
xiaohu.huang58292b32024-01-03 14:09:51 +08005429 #include "freertos_tasks_c_additions.h"
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005430
xiaohu.huang58292b32024-01-03 14:09:51 +08005431 #ifdef FREERTOS_TASKS_C_ADDITIONS_INIT
5432 static void freertos_tasks_c_additions_init( void )
5433 {
5434 FREERTOS_TASKS_C_ADDITIONS_INIT();
5435 }
5436 #endif
kelvin.zhang57fb6ae2021-10-15 10:19:42 +08005437
xiaohu.huang58292b32024-01-03 14:09:51 +08005438#endif /* if ( configINCLUDE_FREERTOS_TASK_C_ADDITIONS_H == 1 ) */
Xiaohu.Huangc9a7d4f2022-03-15 13:48:38 +08005439
5440/* Add include implement source code which depend on the inner elements */
5441#include "aml_tasks_ext.c"