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