2 * FreeRTOS Kernel <DEVELOPMENT BRANCH>
3 * Copyright (C) 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
5 * SPDX-License-Identifier: MIT
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:
14 * The above copyright notice and this permission notice shall be included in all
15 * copies or substantial portions of the Software.
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.
24 * https://www.FreeRTOS.org
25 * https://github.com/FreeRTOS
32 #ifndef INC_FREERTOS_H
33 #error "include FreeRTOS.h must appear in source files before include croutine.h"
44 /* Used to hide the implementation of the co-routine control block. The
45 * control block structure however has to be included in the header due to
46 * the macro implementation of the co-routine functionality. */
47 typedef void * CoRoutineHandle_t;
49 /* Defines the prototype to which co-routine functions must conform. */
50 typedef void (* crCOROUTINE_CODE)( CoRoutineHandle_t xHandle,
51 UBaseType_t uxIndex );
53 typedef struct corCoRoutineControlBlock
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. */
66 * BaseType_t xCoRoutineCreate(
67 * crCOROUTINE_CODE pxCoRoutineCode,
68 * UBaseType_t uxPriority,
73 * Create a new co-routine and add it to the list of co-routines that are
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.
80 * @param uxPriority The priority with respect to other co-routines at which
81 * the co-routine will run.
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.
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.
92 * // Co-routine to be created.
93 * void vFlashCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
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 };
100 * // Must start every co-routine with a call to crSTART();
101 * crSTART( xHandle );
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 ] );
114 * // Must end every co-routine with a call to crEND();
118 * // Function that creates two co-routines.
119 * void vOtherFunction( void )
121 * uint8_t ucParameterToPass;
122 * TaskHandle_t xHandle;
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++ )
129 * xCoRoutineCreate( vFlashCoRoutine, 0, uxIndex );
133 * \defgroup xCoRoutineCreate xCoRoutineCreate
136 BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode,
137 UBaseType_t uxPriority,
138 UBaseType_t uxIndex );
144 * void vCoRoutineSchedule( void );
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.
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
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 )
164 * vCoRoutineSchedule();
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
170 * void vApplicationIdleHook( void )
174 * vCoRoutineSchedule();
178 * \defgroup vCoRoutineSchedule vCoRoutineSchedule
181 void vCoRoutineSchedule( void );
186 * crSTART( CoRoutineHandle_t xHandle );
189 * This macro MUST always be called at the start of a co-routine function.
193 * // Co-routine to be created.
194 * void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
196 * // Variables in co-routines must be declared static if they must maintain value across a blocking call.
197 * static int32_t ulAVariable;
199 * // Must start every co-routine with a call to crSTART();
200 * crSTART( xHandle );
204 * // Co-routine functionality goes here.
207 * // Must end every co-routine with a call to crEND();
211 * \defgroup crSTART crSTART
214 #define crSTART( pxCRCB ) \
215 switch( ( ( CRCB_t * ) ( pxCRCB ) )->uxState ) { \
224 * This macro MUST always be called at the end of a co-routine function.
228 * // Co-routine to be created.
229 * void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
231 * // Variables in co-routines must be declared static if they must maintain value across a blocking call.
232 * static int32_t ulAVariable;
234 * // Must start every co-routine with a call to crSTART();
235 * crSTART( xHandle );
239 * // Co-routine functionality goes here.
242 * // Must end every co-routine with a call to crEND();
246 * \defgroup crSTART crSTART
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.
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 ):
265 * crDELAY( CoRoutineHandle_t xHandle, TickType_t xTicksToDelay );
268 * Delay a co-routine for a fixed period of time.
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.
274 * @param xHandle The handle of the co-routine to delay. This is the xHandle
275 * parameter of the co-routine function.
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.
284 * // Co-routine to be created.
285 * void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
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;
292 * // Must start every co-routine with a call to crSTART();
293 * crSTART( xHandle );
297 * // Delay for 200ms.
298 * crDELAY( xHandle, xDelayTime );
300 * // Do something here.
303 * // Must end every co-routine with a call to crEND();
307 * \defgroup crDELAY crDELAY
310 #define crDELAY( xHandle, xTicksToDelay ) \
312 if( ( xTicksToDelay ) > 0 ) \
314 vCoRoutineAddToDelayedList( ( xTicksToDelay ), NULL ); \
316 crSET_STATE0( ( xHandle ) ); \
322 * CoRoutineHandle_t xHandle,
323 * QueueHandle_t pxQueue,
324 * void *pvItemToQueue,
325 * TickType_t xTicksToWait,
326 * BaseType_t *pxResult
330 * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
331 * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
333 * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
334 * xQueueSend() and xQueueReceive() can only be used from tasks.
336 * crQUEUE_SEND can only be called from the co-routine function itself - not
337 * from within a function called by the co-routine function. This is because
338 * co-routines do not maintain their own stack.
340 * See the co-routine section of the WEB documentation for information on
341 * passing data between tasks and co-routines and between ISR's and
344 * @param xHandle The handle of the calling co-routine. This is the xHandle
345 * parameter of the co-routine function.
347 * @param pxQueue The handle of the queue on which the data will be posted.
348 * The handle is obtained as the return value when the queue is created using
349 * the xQueueCreate() API function.
351 * @param pvItemToQueue A pointer to the data being posted onto the queue.
352 * The number of bytes of each queued item is specified when the queue is
353 * created. This number of bytes is copied from pvItemToQueue into the queue
356 * @param xTickToDelay The number of ticks that the co-routine should block
357 * to wait for space to become available on the queue, should space not be
358 * available immediately. The actual amount of time this equates to is defined
359 * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
360 * portTICK_PERIOD_MS can be used to convert ticks to milliseconds (see example
363 * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
364 * data was successfully posted onto the queue, otherwise it will be set to an
365 * error defined within ProjDefs.h.
369 * // Co-routine function that blocks for a fixed period then posts a number onto
371 * static void prvCoRoutineFlashTask( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
373 * // Variables in co-routines must be declared static if they must maintain value across a blocking call.
374 * static BaseType_t xNumberToPost = 0;
375 * static BaseType_t xResult;
377 * // Co-routines must begin with a call to crSTART().
378 * crSTART( xHandle );
382 * // This assumes the queue has already been created.
383 * crQUEUE_SEND( xHandle, xCoRoutineQueue, &xNumberToPost, NO_DELAY, &xResult );
385 * if( xResult != pdPASS )
387 * // The message was not posted!
390 * // Increment the number to be posted onto the queue.
393 * // Delay for 100 ticks.
394 * crDELAY( xHandle, 100 );
397 * // Co-routines must end with a call to crEND().
401 * \defgroup crQUEUE_SEND crQUEUE_SEND
404 #define crQUEUE_SEND( xHandle, pxQueue, pvItemToQueue, xTicksToWait, pxResult ) \
406 *( pxResult ) = xQueueCRSend( ( pxQueue ), ( pvItemToQueue ), ( xTicksToWait ) ); \
407 if( *( pxResult ) == errQUEUE_BLOCKED ) \
409 crSET_STATE0( ( xHandle ) ); \
410 *pxResult = xQueueCRSend( ( pxQueue ), ( pvItemToQueue ), 0 ); \
412 if( *pxResult == errQUEUE_YIELD ) \
414 crSET_STATE1( ( xHandle ) ); \
415 *pxResult = pdPASS; \
423 * CoRoutineHandle_t xHandle,
424 * QueueHandle_t pxQueue,
426 * TickType_t xTicksToWait,
427 * BaseType_t *pxResult
431 * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
432 * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
434 * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
435 * xQueueSend() and xQueueReceive() can only be used from tasks.
437 * crQUEUE_RECEIVE can only be called from the co-routine function itself - not
438 * from within a function called by the co-routine function. This is because
439 * co-routines do not maintain their own stack.
441 * See the co-routine section of the WEB documentation for information on
442 * passing data between tasks and co-routines and between ISR's and
445 * @param xHandle The handle of the calling co-routine. This is the xHandle
446 * parameter of the co-routine function.
448 * @param pxQueue The handle of the queue from which the data will be received.
449 * The handle is obtained as the return value when the queue is created using
450 * the xQueueCreate() API function.
452 * @param pvBuffer The buffer into which the received item is to be copied.
453 * The number of bytes of each queued item is specified when the queue is
454 * created. This number of bytes is copied into pvBuffer.
456 * @param xTickToDelay The number of ticks that the co-routine should block
457 * to wait for data to become available from the queue, should data not be
458 * available immediately. The actual amount of time this equates to is defined
459 * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
460 * portTICK_PERIOD_MS can be used to convert ticks to milliseconds (see the
461 * crQUEUE_SEND example).
463 * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
464 * data was successfully retrieved from the queue, otherwise it will be set to
465 * an error code as defined within ProjDefs.h.
469 * // A co-routine receives the number of an LED to flash from a queue. It
470 * // blocks on the queue until the number is received.
471 * static void prvCoRoutineFlashWorkTask( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
473 * // Variables in co-routines must be declared static if they must maintain value across a blocking call.
474 * static BaseType_t xResult;
475 * static UBaseType_t uxLEDToFlash;
477 * // All co-routines must start with a call to crSTART().
478 * crSTART( xHandle );
482 * // Wait for data to become available on the queue.
483 * crQUEUE_RECEIVE( xHandle, xCoRoutineQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
485 * if( xResult == pdPASS )
487 * // We received the LED to flash - flash it!
488 * vParTestToggleLED( uxLEDToFlash );
495 * \defgroup crQUEUE_RECEIVE crQUEUE_RECEIVE
498 #define crQUEUE_RECEIVE( xHandle, pxQueue, pvBuffer, xTicksToWait, pxResult ) \
500 *( pxResult ) = xQueueCRReceive( ( pxQueue ), ( pvBuffer ), ( xTicksToWait ) ); \
501 if( *( pxResult ) == errQUEUE_BLOCKED ) \
503 crSET_STATE0( ( xHandle ) ); \
504 *( pxResult ) = xQueueCRReceive( ( pxQueue ), ( pvBuffer ), 0 ); \
506 if( *( pxResult ) == errQUEUE_YIELD ) \
508 crSET_STATE1( ( xHandle ) ); \
509 *( pxResult ) = pdPASS; \
516 * crQUEUE_SEND_FROM_ISR(
517 * QueueHandle_t pxQueue,
518 * void *pvItemToQueue,
519 * BaseType_t xCoRoutinePreviouslyWoken
523 * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
524 * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
525 * functions used by tasks.
527 * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
528 * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
529 * xQueueReceiveFromISR() can only be used to pass data between a task and and
532 * crQUEUE_SEND_FROM_ISR can only be called from an ISR to send data to a queue
533 * that is being used from within a co-routine.
535 * See the co-routine section of the WEB documentation for information on
536 * passing data between tasks and co-routines and between ISR's and
539 * @param xQueue The handle to the queue on which the item is to be posted.
541 * @param pvItemToQueue A pointer to the item that is to be placed on the
542 * queue. The size of the items the queue will hold was defined when the
543 * queue was created, so this many bytes will be copied from pvItemToQueue
544 * into the queue storage area.
546 * @param xCoRoutinePreviouslyWoken This is included so an ISR can post onto
547 * the same queue multiple times from a single interrupt. The first call
548 * should always pass in pdFALSE. Subsequent calls should pass in
549 * the value returned from the previous call.
551 * @return pdTRUE if a co-routine was woken by posting onto the queue. This is
552 * used by the ISR to determine if a context switch may be required following
557 * // A co-routine that blocks on a queue waiting for characters to be received.
558 * static void vReceivingCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
561 * BaseType_t xResult;
563 * // All co-routines must start with a call to crSTART().
564 * crSTART( xHandle );
568 * // Wait for data to become available on the queue. This assumes the
569 * // queue xCommsRxQueue has already been created!
570 * crQUEUE_RECEIVE( xHandle, xCommsRxQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
572 * // Was a character received?
573 * if( xResult == pdPASS )
575 * // Process the character here.
579 * // All co-routines must end with a call to crEND().
583 * // An ISR that uses a queue to send characters received on a serial port to
585 * void vUART_ISR( void )
588 * BaseType_t xCRWokenByPost = pdFALSE;
590 * // We loop around reading characters until there are none left in the UART.
591 * while( UART_RX_REG_NOT_EMPTY() )
593 * // Obtain the character from the UART.
594 * cRxedChar = UART_RX_REG;
596 * // Post the character onto a queue. xCRWokenByPost will be pdFALSE
597 * // the first time around the loop. If the post causes a co-routine
598 * // to be woken (unblocked) then xCRWokenByPost will be set to pdTRUE.
599 * // In this manner we can ensure that if more than one co-routine is
600 * // blocked on the queue only one is woken by this ISR no matter how
601 * // many characters are posted to the queue.
602 * xCRWokenByPost = crQUEUE_SEND_FROM_ISR( xCommsRxQueue, &cRxedChar, xCRWokenByPost );
606 * \defgroup crQUEUE_SEND_FROM_ISR crQUEUE_SEND_FROM_ISR
609 #define crQUEUE_SEND_FROM_ISR( pxQueue, pvItemToQueue, xCoRoutinePreviouslyWoken ) \
610 xQueueCRSendFromISR( ( pxQueue ), ( pvItemToQueue ), ( xCoRoutinePreviouslyWoken ) )
616 * crQUEUE_SEND_FROM_ISR(
617 * QueueHandle_t pxQueue,
619 * BaseType_t * pxCoRoutineWoken
623 * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
624 * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
625 * functions used by tasks.
627 * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
628 * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
629 * xQueueReceiveFromISR() can only be used to pass data between a task and and
632 * crQUEUE_RECEIVE_FROM_ISR can only be called from an ISR to receive data
633 * from a queue that is being used from within a co-routine (a co-routine
634 * posted to the queue).
636 * See the co-routine section of the WEB documentation for information on
637 * passing data between tasks and co-routines and between ISR's and
640 * @param xQueue The handle to the queue on which the item is to be posted.
642 * @param pvBuffer A pointer to a buffer into which the received item will be
643 * placed. The size of the items the queue will hold was defined when the
644 * queue was created, so this many bytes will be copied from the queue into
647 * @param pxCoRoutineWoken A co-routine may be blocked waiting for space to become
648 * available on the queue. If crQUEUE_RECEIVE_FROM_ISR causes such a
649 * co-routine to unblock *pxCoRoutineWoken will get set to pdTRUE, otherwise
650 * *pxCoRoutineWoken will remain unchanged.
652 * @return pdTRUE an item was successfully received from the queue, otherwise
657 * // A co-routine that posts a character to a queue then blocks for a fixed
658 * // period. The character is incremented each time.
659 * static void vSendingCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
661 * // cChar holds its value while this co-routine is blocked and must therefore
662 * // be declared static.
663 * static char cCharToTx = 'a';
664 * BaseType_t xResult;
666 * // All co-routines must start with a call to crSTART().
667 * crSTART( xHandle );
671 * // Send the next character to the queue.
672 * crQUEUE_SEND( xHandle, xCoRoutineQueue, &cCharToTx, NO_DELAY, &xResult );
674 * if( xResult == pdPASS )
676 * // The character was successfully posted to the queue.
680 * // Could not post the character to the queue.
683 * // Enable the UART Tx interrupt to cause an interrupt in this
684 * // hypothetical UART. The interrupt will obtain the character
685 * // from the queue and send it.
686 * ENABLE_RX_INTERRUPT();
688 * // Increment to the next character then block for a fixed period.
689 * // cCharToTx will maintain its value across the delay as it is
690 * // declared static.
692 * if( cCharToTx > 'x' )
699 * // All co-routines must end with a call to crEND().
703 * // An ISR that uses a queue to receive characters to send on a UART.
704 * void vUART_ISR( void )
707 * BaseType_t xCRWokenByPost = pdFALSE;
709 * while( UART_TX_REG_EMPTY() )
711 * // Are there any characters in the queue waiting to be sent?
712 * // xCRWokenByPost will automatically be set to pdTRUE if a co-routine
713 * // is woken by the post - ensuring that only a single co-routine is
714 * // woken no matter how many times we go around this loop.
715 * if( crQUEUE_RECEIVE_FROM_ISR( pxQueue, &cCharToTx, &xCRWokenByPost ) )
717 * SEND_CHARACTER( cCharToTx );
722 * \defgroup crQUEUE_RECEIVE_FROM_ISR crQUEUE_RECEIVE_FROM_ISR
725 #define crQUEUE_RECEIVE_FROM_ISR( pxQueue, pvBuffer, pxCoRoutineWoken ) \
726 xQueueCRReceiveFromISR( ( pxQueue ), ( pvBuffer ), ( pxCoRoutineWoken ) )
729 * This function is intended for internal use by the co-routine macros only.
730 * The macro nature of the co-routine implementation requires that the
731 * prototype appears here. The function should not be used by application
734 * Removes the current co-routine from its ready list and places it in the
735 * appropriate delayed list.
737 void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay,
738 List_t * pxEventList );
741 * This function is intended for internal use by the queue implementation only.
742 * The function should not be used by application writers.
744 * Removes the highest priority co-routine from the event list and places it in
745 * the pending ready list.
747 BaseType_t xCoRoutineRemoveFromEventList( const List_t * pxEventList );
755 #endif /* CO_ROUTINE_H */