Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +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 |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +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 |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 26 | * |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 27 | */ |
| 28 | |
| 29 | #ifndef CO_ROUTINE_H |
| 30 | #define CO_ROUTINE_H |
| 31 | |
| 32 | #ifndef INC_FREERTOS_H |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 33 | #error "include FreeRTOS.h must appear in source files before include croutine.h" |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 34 | #endif |
| 35 | |
| 36 | #include "list.h" |
| 37 | |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 38 | /* *INDENT-OFF* */ |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 39 | #ifdef __cplusplus |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 40 | extern "C" { |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 41 | #endif |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 42 | /* *INDENT-ON* */ |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 43 | |
| 44 | /* Used to hide the implementation of the co-routine control block. The |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 45 | * control block structure however has to be included in the header due to |
| 46 | * the macro implementation of the co-routine functionality. */ |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 47 | typedef void * CoRoutineHandle_t; |
| 48 | |
| 49 | /* Defines the prototype to which co-routine functions must conform. */ |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 50 | typedef void (* crCOROUTINE_CODE)( CoRoutineHandle_t, |
| 51 | UBaseType_t ); |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 52 | |
| 53 | typedef struct corCoRoutineControlBlock |
| 54 | { |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 55 | crCOROUTINE_CODE pxCoRoutineFunction; |
| 56 | ListItem_t xGenericListItem; /*< List item used to place the CRCB in ready and blocked queues. */ |
| 57 | ListItem_t xEventListItem; /*< List item used to place the CRCB in event lists. */ |
| 58 | UBaseType_t uxPriority; /*< The priority of the co-routine in relation to other co-routines. */ |
| 59 | UBaseType_t uxIndex; /*< Used to distinguish between co-routines when multiple co-routines use the same co-routine function. */ |
| 60 | uint16_t uxState; /*< Used internally by the co-routine implementation. */ |
| 61 | } CRCB_t; /* Co-routine control block. Note must be identical in size down to uxPriority with TCB_t. */ |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 62 | |
| 63 | /** |
| 64 | * croutine. h |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 65 | * @code{c} |
| 66 | * BaseType_t xCoRoutineCreate( |
| 67 | * crCOROUTINE_CODE pxCoRoutineCode, |
| 68 | * UBaseType_t uxPriority, |
| 69 | * UBaseType_t uxIndex |
| 70 | * ); |
| 71 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 72 | * |
| 73 | * Create a new co-routine and add it to the list of co-routines that are |
| 74 | * ready to run. |
| 75 | * |
| 76 | * @param pxCoRoutineCode Pointer to the co-routine function. Co-routine |
| 77 | * functions require special syntax - see the co-routine section of the WEB |
| 78 | * documentation for more information. |
| 79 | * |
| 80 | * @param uxPriority The priority with respect to other co-routines at which |
| 81 | * the co-routine will run. |
| 82 | * |
| 83 | * @param uxIndex Used to distinguish between different co-routines that |
| 84 | * execute the same function. See the example below and the co-routine section |
| 85 | * of the WEB documentation for further information. |
| 86 | * |
| 87 | * @return pdPASS if the co-routine was successfully created and added to a ready |
| 88 | * list, otherwise an error code defined with ProjDefs.h. |
| 89 | * |
| 90 | * Example usage: |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 91 | * @code{c} |
| 92 | * // Co-routine to be created. |
| 93 | * void vFlashCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex ) |
| 94 | * { |
| 95 | * // Variables in co-routines must be declared static if they must maintain value across a blocking call. |
| 96 | * // This may not be necessary for const variables. |
| 97 | * static const char cLedToFlash[ 2 ] = { 5, 6 }; |
| 98 | * static const TickType_t uxFlashRates[ 2 ] = { 200, 400 }; |
| 99 | * |
| 100 | * // Must start every co-routine with a call to crSTART(); |
| 101 | * crSTART( xHandle ); |
| 102 | * |
| 103 | * for( ;; ) |
| 104 | * { |
| 105 | * // This co-routine just delays for a fixed period, then toggles |
| 106 | * // an LED. Two co-routines are created using this function, so |
| 107 | * // the uxIndex parameter is used to tell the co-routine which |
| 108 | * // LED to flash and how int32_t to delay. This assumes xQueue has |
| 109 | * // already been created. |
| 110 | * vParTestToggleLED( cLedToFlash[ uxIndex ] ); |
| 111 | * crDELAY( xHandle, uxFlashRates[ uxIndex ] ); |
| 112 | * } |
| 113 | * |
| 114 | * // Must end every co-routine with a call to crEND(); |
| 115 | * crEND(); |
| 116 | * } |
| 117 | * |
| 118 | * // Function that creates two co-routines. |
| 119 | * void vOtherFunction( void ) |
| 120 | * { |
| 121 | * uint8_t ucParameterToPass; |
| 122 | * TaskHandle_t xHandle; |
| 123 | * |
| 124 | * // Create two co-routines at priority 0. The first is given index 0 |
| 125 | * // so (from the code above) toggles LED 5 every 200 ticks. The second |
| 126 | * // is given index 1 so toggles LED 6 every 400 ticks. |
| 127 | * for( uxIndex = 0; uxIndex < 2; uxIndex++ ) |
| 128 | * { |
| 129 | * xCoRoutineCreate( vFlashCoRoutine, 0, uxIndex ); |
| 130 | * } |
| 131 | * } |
| 132 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 133 | * \defgroup xCoRoutineCreate xCoRoutineCreate |
| 134 | * \ingroup Tasks |
| 135 | */ |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 136 | BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, |
| 137 | UBaseType_t uxPriority, |
| 138 | UBaseType_t uxIndex ); |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 139 | |
| 140 | |
| 141 | /** |
| 142 | * croutine. h |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 143 | * @code{c} |
| 144 | * void vCoRoutineSchedule( void ); |
| 145 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 146 | * |
| 147 | * Run a co-routine. |
| 148 | * |
| 149 | * vCoRoutineSchedule() executes the highest priority co-routine that is able |
| 150 | * to run. The co-routine will execute until it either blocks, yields or is |
| 151 | * preempted by a task. Co-routines execute cooperatively so one |
| 152 | * co-routine cannot be preempted by another, but can be preempted by a task. |
| 153 | * |
| 154 | * If an application comprises of both tasks and co-routines then |
| 155 | * vCoRoutineSchedule should be called from the idle task (in an idle task |
| 156 | * hook). |
| 157 | * |
| 158 | * Example usage: |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 159 | * @code{c} |
| 160 | * // This idle task hook will schedule a co-routine each time it is called. |
| 161 | * // The rest of the idle task will execute between co-routine calls. |
| 162 | * void vApplicationIdleHook( void ) |
| 163 | * { |
| 164 | * vCoRoutineSchedule(); |
| 165 | * } |
| 166 | * |
| 167 | * // Alternatively, if you do not require any other part of the idle task to |
| 168 | * // execute, the idle task hook can call vCoRoutineSchedule() within an |
| 169 | * // infinite loop. |
| 170 | * void vApplicationIdleHook( void ) |
| 171 | * { |
| 172 | * for( ;; ) |
| 173 | * { |
| 174 | * vCoRoutineSchedule(); |
| 175 | * } |
| 176 | * } |
| 177 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 178 | * \defgroup vCoRoutineSchedule vCoRoutineSchedule |
| 179 | * \ingroup Tasks |
| 180 | */ |
| 181 | void vCoRoutineSchedule( void ); |
| 182 | |
| 183 | /** |
| 184 | * croutine. h |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 185 | * @code{c} |
| 186 | * crSTART( CoRoutineHandle_t xHandle ); |
| 187 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 188 | * |
| 189 | * This macro MUST always be called at the start of a co-routine function. |
| 190 | * |
| 191 | * Example usage: |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 192 | * @code{c} |
| 193 | * // Co-routine to be created. |
| 194 | * void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex ) |
| 195 | * { |
| 196 | * // Variables in co-routines must be declared static if they must maintain value across a blocking call. |
| 197 | * static int32_t ulAVariable; |
| 198 | * |
| 199 | * // Must start every co-routine with a call to crSTART(); |
| 200 | * crSTART( xHandle ); |
| 201 | * |
| 202 | * for( ;; ) |
| 203 | * { |
| 204 | * // Co-routine functionality goes here. |
| 205 | * } |
| 206 | * |
| 207 | * // Must end every co-routine with a call to crEND(); |
| 208 | * crEND(); |
| 209 | * } |
| 210 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 211 | * \defgroup crSTART crSTART |
| 212 | * \ingroup Tasks |
| 213 | */ |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 214 | #define crSTART( pxCRCB ) \ |
| 215 | switch( ( ( CRCB_t * ) ( pxCRCB ) )->uxState ) { \ |
| 216 | case 0: |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 217 | |
| 218 | /** |
| 219 | * croutine. h |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 220 | * @code{c} |
| 221 | * crEND(); |
| 222 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 223 | * |
| 224 | * This macro MUST always be called at the end of a co-routine function. |
| 225 | * |
| 226 | * Example usage: |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 227 | * @code{c} |
| 228 | * // Co-routine to be created. |
| 229 | * void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex ) |
| 230 | * { |
| 231 | * // Variables in co-routines must be declared static if they must maintain value across a blocking call. |
| 232 | * static int32_t ulAVariable; |
| 233 | * |
| 234 | * // Must start every co-routine with a call to crSTART(); |
| 235 | * crSTART( xHandle ); |
| 236 | * |
| 237 | * for( ;; ) |
| 238 | * { |
| 239 | * // Co-routine functionality goes here. |
| 240 | * } |
| 241 | * |
| 242 | * // Must end every co-routine with a call to crEND(); |
| 243 | * crEND(); |
| 244 | * } |
| 245 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 246 | * \defgroup crSTART crSTART |
| 247 | * \ingroup Tasks |
| 248 | */ |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 249 | #define crEND() } |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 250 | |
| 251 | /* |
| 252 | * These macros are intended for internal use by the co-routine implementation |
| 253 | * only. The macros should not be used directly by application writers. |
| 254 | */ |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 255 | #define crSET_STATE0( xHandle ) \ |
| 256 | ( ( CRCB_t * ) ( xHandle ) )->uxState = ( __LINE__ * 2 ); return; \ |
| 257 | case ( __LINE__ * 2 ): |
| 258 | #define crSET_STATE1( xHandle ) \ |
| 259 | ( ( CRCB_t * ) ( xHandle ) )->uxState = ( ( __LINE__ * 2 ) + 1 ); return; \ |
| 260 | case ( ( __LINE__ * 2 ) + 1 ): |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 261 | |
| 262 | /** |
| 263 | * croutine. h |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 264 | * @code{c} |
| 265 | * crDELAY( CoRoutineHandle_t xHandle, TickType_t xTicksToDelay ); |
| 266 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 267 | * |
| 268 | * Delay a co-routine for a fixed period of time. |
| 269 | * |
| 270 | * crDELAY can only be called from the co-routine function itself - not |
| 271 | * from within a function called by the co-routine function. This is because |
| 272 | * co-routines do not maintain their own stack. |
| 273 | * |
| 274 | * @param xHandle The handle of the co-routine to delay. This is the xHandle |
| 275 | * parameter of the co-routine function. |
| 276 | * |
| 277 | * @param xTickToDelay The number of ticks that the co-routine should delay |
| 278 | * for. The actual amount of time this equates to is defined by |
| 279 | * configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant portTICK_PERIOD_MS |
| 280 | * can be used to convert ticks to milliseconds. |
| 281 | * |
| 282 | * Example usage: |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 283 | * @code{c} |
| 284 | * // Co-routine to be created. |
| 285 | * void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex ) |
| 286 | * { |
| 287 | * // Variables in co-routines must be declared static if they must maintain value across a blocking call. |
| 288 | * // This may not be necessary for const variables. |
| 289 | * // We are to delay for 200ms. |
| 290 | * static const xTickType xDelayTime = 200 / portTICK_PERIOD_MS; |
| 291 | * |
| 292 | * // Must start every co-routine with a call to crSTART(); |
| 293 | * crSTART( xHandle ); |
| 294 | * |
| 295 | * for( ;; ) |
| 296 | * { |
| 297 | * // Delay for 200ms. |
| 298 | * crDELAY( xHandle, xDelayTime ); |
| 299 | * |
| 300 | * // Do something here. |
| 301 | * } |
| 302 | * |
| 303 | * // Must end every co-routine with a call to crEND(); |
| 304 | * crEND(); |
| 305 | * } |
| 306 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 307 | * \defgroup crDELAY crDELAY |
| 308 | * \ingroup Tasks |
| 309 | */ |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 310 | #define crDELAY( xHandle, xTicksToDelay ) \ |
| 311 | if( ( xTicksToDelay ) > 0 ) \ |
| 312 | { \ |
| 313 | vCoRoutineAddToDelayedList( ( xTicksToDelay ), NULL ); \ |
| 314 | } \ |
| 315 | crSET_STATE0( ( xHandle ) ); |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 316 | |
| 317 | /** |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 318 | * @code{c} |
| 319 | * crQUEUE_SEND( |
| 320 | * CoRoutineHandle_t xHandle, |
| 321 | * QueueHandle_t pxQueue, |
| 322 | * void *pvItemToQueue, |
| 323 | * TickType_t xTicksToWait, |
| 324 | * BaseType_t *pxResult |
| 325 | * ) |
| 326 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 327 | * |
| 328 | * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine |
| 329 | * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks. |
| 330 | * |
| 331 | * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas |
| 332 | * xQueueSend() and xQueueReceive() can only be used from tasks. |
| 333 | * |
| 334 | * crQUEUE_SEND can only be called from the co-routine function itself - not |
| 335 | * from within a function called by the co-routine function. This is because |
| 336 | * co-routines do not maintain their own stack. |
| 337 | * |
| 338 | * See the co-routine section of the WEB documentation for information on |
| 339 | * passing data between tasks and co-routines and between ISR's and |
| 340 | * co-routines. |
| 341 | * |
| 342 | * @param xHandle The handle of the calling co-routine. This is the xHandle |
| 343 | * parameter of the co-routine function. |
| 344 | * |
| 345 | * @param pxQueue The handle of the queue on which the data will be posted. |
| 346 | * The handle is obtained as the return value when the queue is created using |
| 347 | * the xQueueCreate() API function. |
| 348 | * |
| 349 | * @param pvItemToQueue A pointer to the data being posted onto the queue. |
| 350 | * The number of bytes of each queued item is specified when the queue is |
| 351 | * created. This number of bytes is copied from pvItemToQueue into the queue |
| 352 | * itself. |
| 353 | * |
| 354 | * @param xTickToDelay The number of ticks that the co-routine should block |
| 355 | * to wait for space to become available on the queue, should space not be |
| 356 | * available immediately. The actual amount of time this equates to is defined |
| 357 | * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant |
| 358 | * portTICK_PERIOD_MS can be used to convert ticks to milliseconds (see example |
| 359 | * below). |
| 360 | * |
| 361 | * @param pxResult The variable pointed to by pxResult will be set to pdPASS if |
| 362 | * data was successfully posted onto the queue, otherwise it will be set to an |
| 363 | * error defined within ProjDefs.h. |
| 364 | * |
| 365 | * Example usage: |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 366 | * @code{c} |
| 367 | * // Co-routine function that blocks for a fixed period then posts a number onto |
| 368 | * // a queue. |
| 369 | * static void prvCoRoutineFlashTask( CoRoutineHandle_t xHandle, UBaseType_t uxIndex ) |
| 370 | * { |
| 371 | * // Variables in co-routines must be declared static if they must maintain value across a blocking call. |
| 372 | * static BaseType_t xNumberToPost = 0; |
| 373 | * static BaseType_t xResult; |
| 374 | * |
| 375 | * // Co-routines must begin with a call to crSTART(). |
| 376 | * crSTART( xHandle ); |
| 377 | * |
| 378 | * for( ;; ) |
| 379 | * { |
| 380 | * // This assumes the queue has already been created. |
| 381 | * crQUEUE_SEND( xHandle, xCoRoutineQueue, &xNumberToPost, NO_DELAY, &xResult ); |
| 382 | * |
| 383 | * if( xResult != pdPASS ) |
| 384 | * { |
| 385 | * // The message was not posted! |
| 386 | * } |
| 387 | * |
| 388 | * // Increment the number to be posted onto the queue. |
| 389 | * xNumberToPost++; |
| 390 | * |
| 391 | * // Delay for 100 ticks. |
| 392 | * crDELAY( xHandle, 100 ); |
| 393 | * } |
| 394 | * |
| 395 | * // Co-routines must end with a call to crEND(). |
| 396 | * crEND(); |
| 397 | * } |
| 398 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 399 | * \defgroup crQUEUE_SEND crQUEUE_SEND |
| 400 | * \ingroup Tasks |
| 401 | */ |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 402 | #define crQUEUE_SEND( xHandle, pxQueue, pvItemToQueue, xTicksToWait, pxResult ) \ |
| 403 | { \ |
| 404 | *( pxResult ) = xQueueCRSend( ( pxQueue ), ( pvItemToQueue ), ( xTicksToWait ) ); \ |
| 405 | if( *( pxResult ) == errQUEUE_BLOCKED ) \ |
| 406 | { \ |
| 407 | crSET_STATE0( ( xHandle ) ); \ |
| 408 | *pxResult = xQueueCRSend( ( pxQueue ), ( pvItemToQueue ), 0 ); \ |
| 409 | } \ |
| 410 | if( *pxResult == errQUEUE_YIELD ) \ |
| 411 | { \ |
| 412 | crSET_STATE1( ( xHandle ) ); \ |
| 413 | *pxResult = pdPASS; \ |
| 414 | } \ |
| 415 | } |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 416 | |
| 417 | /** |
| 418 | * croutine. h |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 419 | * @code{c} |
| 420 | * crQUEUE_RECEIVE( |
| 421 | * CoRoutineHandle_t xHandle, |
| 422 | * QueueHandle_t pxQueue, |
| 423 | * void *pvBuffer, |
| 424 | * TickType_t xTicksToWait, |
| 425 | * BaseType_t *pxResult |
| 426 | * ) |
| 427 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 428 | * |
| 429 | * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine |
| 430 | * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks. |
| 431 | * |
| 432 | * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas |
| 433 | * xQueueSend() and xQueueReceive() can only be used from tasks. |
| 434 | * |
| 435 | * crQUEUE_RECEIVE can only be called from the co-routine function itself - not |
| 436 | * from within a function called by the co-routine function. This is because |
| 437 | * co-routines do not maintain their own stack. |
| 438 | * |
| 439 | * See the co-routine section of the WEB documentation for information on |
| 440 | * passing data between tasks and co-routines and between ISR's and |
| 441 | * co-routines. |
| 442 | * |
| 443 | * @param xHandle The handle of the calling co-routine. This is the xHandle |
| 444 | * parameter of the co-routine function. |
| 445 | * |
| 446 | * @param pxQueue The handle of the queue from which the data will be received. |
| 447 | * The handle is obtained as the return value when the queue is created using |
| 448 | * the xQueueCreate() API function. |
| 449 | * |
| 450 | * @param pvBuffer The buffer into which the received item is to be copied. |
| 451 | * The number of bytes of each queued item is specified when the queue is |
| 452 | * created. This number of bytes is copied into pvBuffer. |
| 453 | * |
| 454 | * @param xTickToDelay The number of ticks that the co-routine should block |
| 455 | * to wait for data to become available from the queue, should data not be |
| 456 | * available immediately. The actual amount of time this equates to is defined |
| 457 | * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant |
| 458 | * portTICK_PERIOD_MS can be used to convert ticks to milliseconds (see the |
| 459 | * crQUEUE_SEND example). |
| 460 | * |
| 461 | * @param pxResult The variable pointed to by pxResult will be set to pdPASS if |
| 462 | * data was successfully retrieved from the queue, otherwise it will be set to |
| 463 | * an error code as defined within ProjDefs.h. |
| 464 | * |
| 465 | * Example usage: |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 466 | * @code{c} |
| 467 | * // A co-routine receives the number of an LED to flash from a queue. It |
| 468 | * // blocks on the queue until the number is received. |
| 469 | * static void prvCoRoutineFlashWorkTask( CoRoutineHandle_t xHandle, UBaseType_t uxIndex ) |
| 470 | * { |
| 471 | * // Variables in co-routines must be declared static if they must maintain value across a blocking call. |
| 472 | * static BaseType_t xResult; |
| 473 | * static UBaseType_t uxLEDToFlash; |
| 474 | * |
| 475 | * // All co-routines must start with a call to crSTART(). |
| 476 | * crSTART( xHandle ); |
| 477 | * |
| 478 | * for( ;; ) |
| 479 | * { |
| 480 | * // Wait for data to become available on the queue. |
| 481 | * crQUEUE_RECEIVE( xHandle, xCoRoutineQueue, &uxLEDToFlash, portMAX_DELAY, &xResult ); |
| 482 | * |
| 483 | * if( xResult == pdPASS ) |
| 484 | * { |
| 485 | * // We received the LED to flash - flash it! |
| 486 | * vParTestToggleLED( uxLEDToFlash ); |
| 487 | * } |
| 488 | * } |
| 489 | * |
| 490 | * crEND(); |
| 491 | * } |
| 492 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 493 | * \defgroup crQUEUE_RECEIVE crQUEUE_RECEIVE |
| 494 | * \ingroup Tasks |
| 495 | */ |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 496 | #define crQUEUE_RECEIVE( xHandle, pxQueue, pvBuffer, xTicksToWait, pxResult ) \ |
| 497 | { \ |
| 498 | *( pxResult ) = xQueueCRReceive( ( pxQueue ), ( pvBuffer ), ( xTicksToWait ) ); \ |
| 499 | if( *( pxResult ) == errQUEUE_BLOCKED ) \ |
| 500 | { \ |
| 501 | crSET_STATE0( ( xHandle ) ); \ |
| 502 | *( pxResult ) = xQueueCRReceive( ( pxQueue ), ( pvBuffer ), 0 ); \ |
| 503 | } \ |
| 504 | if( *( pxResult ) == errQUEUE_YIELD ) \ |
| 505 | { \ |
| 506 | crSET_STATE1( ( xHandle ) ); \ |
| 507 | *( pxResult ) = pdPASS; \ |
| 508 | } \ |
| 509 | } |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 510 | |
| 511 | /** |
| 512 | * croutine. h |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 513 | * @code{c} |
| 514 | * crQUEUE_SEND_FROM_ISR( |
| 515 | * QueueHandle_t pxQueue, |
| 516 | * void *pvItemToQueue, |
| 517 | * BaseType_t xCoRoutinePreviouslyWoken |
| 518 | * ) |
| 519 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 520 | * |
| 521 | * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the |
| 522 | * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR() |
| 523 | * functions used by tasks. |
| 524 | * |
| 525 | * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to |
| 526 | * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and |
| 527 | * xQueueReceiveFromISR() can only be used to pass data between a task and and |
| 528 | * ISR. |
| 529 | * |
| 530 | * crQUEUE_SEND_FROM_ISR can only be called from an ISR to send data to a queue |
| 531 | * that is being used from within a co-routine. |
| 532 | * |
| 533 | * See the co-routine section of the WEB documentation for information on |
| 534 | * passing data between tasks and co-routines and between ISR's and |
| 535 | * co-routines. |
| 536 | * |
| 537 | * @param xQueue The handle to the queue on which the item is to be posted. |
| 538 | * |
| 539 | * @param pvItemToQueue A pointer to the item that is to be placed on the |
| 540 | * queue. The size of the items the queue will hold was defined when the |
| 541 | * queue was created, so this many bytes will be copied from pvItemToQueue |
| 542 | * into the queue storage area. |
| 543 | * |
| 544 | * @param xCoRoutinePreviouslyWoken This is included so an ISR can post onto |
| 545 | * the same queue multiple times from a single interrupt. The first call |
| 546 | * should always pass in pdFALSE. Subsequent calls should pass in |
| 547 | * the value returned from the previous call. |
| 548 | * |
| 549 | * @return pdTRUE if a co-routine was woken by posting onto the queue. This is |
| 550 | * used by the ISR to determine if a context switch may be required following |
| 551 | * the ISR. |
| 552 | * |
| 553 | * Example usage: |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 554 | * @code{c} |
| 555 | * // A co-routine that blocks on a queue waiting for characters to be received. |
| 556 | * static void vReceivingCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex ) |
| 557 | * { |
| 558 | * char cRxedChar; |
| 559 | * BaseType_t xResult; |
| 560 | * |
| 561 | * // All co-routines must start with a call to crSTART(). |
| 562 | * crSTART( xHandle ); |
| 563 | * |
| 564 | * for( ;; ) |
| 565 | * { |
| 566 | * // Wait for data to become available on the queue. This assumes the |
| 567 | * // queue xCommsRxQueue has already been created! |
| 568 | * crQUEUE_RECEIVE( xHandle, xCommsRxQueue, &uxLEDToFlash, portMAX_DELAY, &xResult ); |
| 569 | * |
| 570 | * // Was a character received? |
| 571 | * if( xResult == pdPASS ) |
| 572 | * { |
| 573 | * // Process the character here. |
| 574 | * } |
| 575 | * } |
| 576 | * |
| 577 | * // All co-routines must end with a call to crEND(). |
| 578 | * crEND(); |
| 579 | * } |
| 580 | * |
| 581 | * // An ISR that uses a queue to send characters received on a serial port to |
| 582 | * // a co-routine. |
| 583 | * void vUART_ISR( void ) |
| 584 | * { |
| 585 | * char cRxedChar; |
| 586 | * BaseType_t xCRWokenByPost = pdFALSE; |
| 587 | * |
| 588 | * // We loop around reading characters until there are none left in the UART. |
| 589 | * while( UART_RX_REG_NOT_EMPTY() ) |
| 590 | * { |
| 591 | * // Obtain the character from the UART. |
| 592 | * cRxedChar = UART_RX_REG; |
| 593 | * |
| 594 | * // Post the character onto a queue. xCRWokenByPost will be pdFALSE |
| 595 | * // the first time around the loop. If the post causes a co-routine |
| 596 | * // to be woken (unblocked) then xCRWokenByPost will be set to pdTRUE. |
| 597 | * // In this manner we can ensure that if more than one co-routine is |
| 598 | * // blocked on the queue only one is woken by this ISR no matter how |
| 599 | * // many characters are posted to the queue. |
| 600 | * xCRWokenByPost = crQUEUE_SEND_FROM_ISR( xCommsRxQueue, &cRxedChar, xCRWokenByPost ); |
| 601 | * } |
| 602 | * } |
| 603 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 604 | * \defgroup crQUEUE_SEND_FROM_ISR crQUEUE_SEND_FROM_ISR |
| 605 | * \ingroup Tasks |
| 606 | */ |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 607 | #define crQUEUE_SEND_FROM_ISR( pxQueue, pvItemToQueue, xCoRoutinePreviouslyWoken ) \ |
| 608 | xQueueCRSendFromISR( ( pxQueue ), ( pvItemToQueue ), ( xCoRoutinePreviouslyWoken ) ) |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 609 | |
| 610 | |
| 611 | /** |
| 612 | * croutine. h |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 613 | * @code{c} |
| 614 | * crQUEUE_SEND_FROM_ISR( |
| 615 | * QueueHandle_t pxQueue, |
| 616 | * void *pvBuffer, |
| 617 | * BaseType_t * pxCoRoutineWoken |
| 618 | * ) |
| 619 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 620 | * |
| 621 | * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the |
| 622 | * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR() |
| 623 | * functions used by tasks. |
| 624 | * |
| 625 | * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to |
| 626 | * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and |
| 627 | * xQueueReceiveFromISR() can only be used to pass data between a task and and |
| 628 | * ISR. |
| 629 | * |
| 630 | * crQUEUE_RECEIVE_FROM_ISR can only be called from an ISR to receive data |
| 631 | * from a queue that is being used from within a co-routine (a co-routine |
| 632 | * posted to the queue). |
| 633 | * |
| 634 | * See the co-routine section of the WEB documentation for information on |
| 635 | * passing data between tasks and co-routines and between ISR's and |
| 636 | * co-routines. |
| 637 | * |
| 638 | * @param xQueue The handle to the queue on which the item is to be posted. |
| 639 | * |
| 640 | * @param pvBuffer A pointer to a buffer into which the received item will be |
| 641 | * placed. The size of the items the queue will hold was defined when the |
| 642 | * queue was created, so this many bytes will be copied from the queue into |
| 643 | * pvBuffer. |
| 644 | * |
| 645 | * @param pxCoRoutineWoken A co-routine may be blocked waiting for space to become |
| 646 | * available on the queue. If crQUEUE_RECEIVE_FROM_ISR causes such a |
| 647 | * co-routine to unblock *pxCoRoutineWoken will get set to pdTRUE, otherwise |
| 648 | * *pxCoRoutineWoken will remain unchanged. |
| 649 | * |
| 650 | * @return pdTRUE an item was successfully received from the queue, otherwise |
| 651 | * pdFALSE. |
| 652 | * |
| 653 | * Example usage: |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 654 | * @code{c} |
| 655 | * // A co-routine that posts a character to a queue then blocks for a fixed |
| 656 | * // period. The character is incremented each time. |
| 657 | * static void vSendingCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex ) |
| 658 | * { |
| 659 | * // cChar holds its value while this co-routine is blocked and must therefore |
| 660 | * // be declared static. |
| 661 | * static char cCharToTx = 'a'; |
| 662 | * BaseType_t xResult; |
| 663 | * |
| 664 | * // All co-routines must start with a call to crSTART(). |
| 665 | * crSTART( xHandle ); |
| 666 | * |
| 667 | * for( ;; ) |
| 668 | * { |
| 669 | * // Send the next character to the queue. |
| 670 | * crQUEUE_SEND( xHandle, xCoRoutineQueue, &cCharToTx, NO_DELAY, &xResult ); |
| 671 | * |
| 672 | * if( xResult == pdPASS ) |
| 673 | * { |
| 674 | * // The character was successfully posted to the queue. |
| 675 | * } |
| 676 | * else |
| 677 | * { |
| 678 | * // Could not post the character to the queue. |
| 679 | * } |
| 680 | * |
| 681 | * // Enable the UART Tx interrupt to cause an interrupt in this |
| 682 | * // hypothetical UART. The interrupt will obtain the character |
| 683 | * // from the queue and send it. |
| 684 | * ENABLE_RX_INTERRUPT(); |
| 685 | * |
| 686 | * // Increment to the next character then block for a fixed period. |
| 687 | * // cCharToTx will maintain its value across the delay as it is |
| 688 | * // declared static. |
| 689 | * cCharToTx++; |
| 690 | * if( cCharToTx > 'x' ) |
| 691 | * { |
| 692 | * cCharToTx = 'a'; |
| 693 | * } |
| 694 | * crDELAY( 100 ); |
| 695 | * } |
| 696 | * |
| 697 | * // All co-routines must end with a call to crEND(). |
| 698 | * crEND(); |
| 699 | * } |
| 700 | * |
| 701 | * // An ISR that uses a queue to receive characters to send on a UART. |
| 702 | * void vUART_ISR( void ) |
| 703 | * { |
| 704 | * char cCharToTx; |
| 705 | * BaseType_t xCRWokenByPost = pdFALSE; |
| 706 | * |
| 707 | * while( UART_TX_REG_EMPTY() ) |
| 708 | * { |
| 709 | * // Are there any characters in the queue waiting to be sent? |
| 710 | * // xCRWokenByPost will automatically be set to pdTRUE if a co-routine |
| 711 | * // is woken by the post - ensuring that only a single co-routine is |
| 712 | * // woken no matter how many times we go around this loop. |
| 713 | * if( crQUEUE_RECEIVE_FROM_ISR( pxQueue, &cCharToTx, &xCRWokenByPost ) ) |
| 714 | * { |
| 715 | * SEND_CHARACTER( cCharToTx ); |
| 716 | * } |
| 717 | * } |
| 718 | * } |
| 719 | * @endcode |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 720 | * \defgroup crQUEUE_RECEIVE_FROM_ISR crQUEUE_RECEIVE_FROM_ISR |
| 721 | * \ingroup Tasks |
| 722 | */ |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 723 | #define crQUEUE_RECEIVE_FROM_ISR( pxQueue, pvBuffer, pxCoRoutineWoken ) \ |
| 724 | xQueueCRReceiveFromISR( ( pxQueue ), ( pvBuffer ), ( pxCoRoutineWoken ) ) |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 725 | |
| 726 | /* |
| 727 | * This function is intended for internal use by the co-routine macros only. |
| 728 | * The macro nature of the co-routine implementation requires that the |
| 729 | * prototype appears here. The function should not be used by application |
| 730 | * writers. |
| 731 | * |
| 732 | * Removes the current co-routine from its ready list and places it in the |
| 733 | * appropriate delayed list. |
| 734 | */ |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 735 | void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay, |
| 736 | List_t * pxEventList ); |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 737 | |
| 738 | /* |
| 739 | * This function is intended for internal use by the queue implementation only. |
| 740 | * The function should not be used by application writers. |
| 741 | * |
| 742 | * Removes the highest priority co-routine from the event list and places it in |
| 743 | * the pending ready list. |
| 744 | */ |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 745 | BaseType_t xCoRoutineRemoveFromEventList( const List_t * pxEventList ); |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 746 | |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 747 | /* *INDENT-OFF* */ |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 748 | #ifdef __cplusplus |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 749 | } |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 750 | #endif |
xiaohu.huang | 58292b3 | 2024-01-03 14:09:51 +0800 | [diff] [blame] | 751 | /* *INDENT-ON* */ |
Xiaohu.Huang | 2c96ef4 | 2021-10-15 16:12:27 +0800 | [diff] [blame] | 752 | |
| 753 | #endif /* CO_ROUTINE_H */ |