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