FreeRTOS API Reference 7.0.0 r0
|
croutine. h
portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex );
Create a new co-routine and add it to the list of co-routines that are ready to run.
pxCoRoutineCode | Pointer to the co-routine function. Co-routine functions require special syntax - see the co-routine section of the WEB documentation for more information. |
uxPriority | The priority with respect to other co-routines at which the co-routine will run. |
uxIndex | Used to distinguish between different co-routines that execute the same function. See the example below and the co-routine section of the WEB documentation for further information. |
Example usage:
// Co-routine to be created. void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex ) { // Variables in co-routines must be declared static if they must maintain value across a blocking call. // This may not be necessary for const variables. static const char cLedToFlash[ 2 ] = { 5, 6 }; static const portTickType uxFlashRates[ 2 ] = { 200, 400 }; // Must start every co-routine with a call to crSTART(); crSTART( xHandle ); for( ;; ) { // This co-routine just delays for a fixed period, then toggles // an LED. Two co-routines are created using this function, so // the uxIndex parameter is used to tell the co-routine which // LED to flash and how long to delay. This assumes xQueue has // already been created. vParTestToggleLED( cLedToFlash[ uxIndex ] ); crDELAY( xHandle, uxFlashRates[ uxIndex ] ); } // Must end every co-routine with a call to crEND(); crEND(); } // Function that creates two co-routines. void vOtherFunction( void ) { unsigned char ucParameterToPass; xTaskHandle xHandle; // Create two co-routines at priority 0. The first is given index 0 // so (from the code above) toggles LED 5 every 200 ticks. The second // is given index 1 so toggles LED 6 every 400 ticks. for( uxIndex = 0; uxIndex < 2; uxIndex++ ) { xCoRoutineCreate( vFlashCoRoutine, 0, uxIndex ); } }
croutine. h
void vCoRoutineSchedule( void );
Run a co-routine.
vCoRoutineSchedule() executes the highest priority co-routine that is able to run. The co-routine will execute until it either blocks, yields or is preempted by a task. Co-routines execute cooperatively so one co-routine cannot be preempted by another, but can be preempted by a task.
If an application comprises of both tasks and co-routines then vCoRoutineSchedule should be called from the idle task (in an idle task hook).
Example usage:
// This idle task hook will schedule a co-routine each time it is called. // The rest of the idle task will execute between co-routine calls. void vApplicationIdleHook( void ) { vCoRoutineSchedule(); } // Alternatively, if you do not require any other part of the idle task to // execute, the idle task hook can call vCoRoutineScheduler() within an // infinite loop. void vApplicationIdleHook( void ) { for( ;; ) { vCoRoutineSchedule(); } }
croutine. h
crSTART( xCoRoutineHandle xHandle );
This macro MUST always be called at the start of a co-routine function.
Example usage:
// Co-routine to be created. void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex ) { // Variables in co-routines must be declared static if they must maintain value across a blocking call. static long ulAVariable; // Must start every co-routine with a call to crSTART(); crSTART( xHandle ); for( ;; ) { // Co-routine functionality goes here. } // Must end every co-routine with a call to crEND(); crEND(); }
croutine. h
crEND();
This macro MUST always be called at the end of a co-routine function.
Example usage:
// Co-routine to be created. void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex ) { // Variables in co-routines must be declared static if they must maintain value across a blocking call. static long ulAVariable; // Must start every co-routine with a call to crSTART(); crSTART( xHandle ); for( ;; ) { // Co-routine functionality goes here. } // Must end every co-routine with a call to crEND(); crEND(); }
croutine. h
crDELAY( xCoRoutineHandle xHandle, portTickType xTicksToDelay );
Delay a co-routine for a fixed period of time.
crDELAY can only be called from the co-routine function itself - not from within a function called by the co-routine function. This is because co-routines do not maintain their own stack.
xHandle | The handle of the co-routine to delay. This is the xHandle parameter of the co-routine function. |
xTickToDelay | The number of ticks that the co-routine should delay for. The actual amount of time this equates to is defined by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant portTICK_RATE_MS can be used to convert ticks to milliseconds. |
Example usage:
// Co-routine to be created. void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex ) { // Variables in co-routines must be declared static if they must maintain value across a blocking call. // This may not be necessary for const variables. // We are to delay for 200ms. static const xTickType xDelayTime = 200 / portTICK_RATE_MS; // Must start every co-routine with a call to crSTART(); crSTART( xHandle ); for( ;; ) { // Delay for 200ms. crDELAY( xHandle, xDelayTime ); // Do something here. } // Must end every co-routine with a call to crEND(); crEND(); }
crQUEUE_SEND( xCoRoutineHandle xHandle, xQueueHandle pxQueue, void *pvItemToQueue, portTickType xTicksToWait, portBASE_TYPE *pxResult )
The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas xQueueSend() and xQueueReceive() can only be used from tasks.
crQUEUE_SEND can only be called from the co-routine function itself - not from within a function called by the co-routine function. This is because co-routines do not maintain their own stack.
See the co-routine section of the WEB documentation for information on passing data between tasks and co-routines and between ISR's and co-routines.
xHandle | The handle of the calling co-routine. This is the xHandle parameter of the co-routine function. |
pxQueue | The handle of the queue on which the data will be posted. The handle is obtained as the return value when the queue is created using the xQueueCreate() API function. |
pvItemToQueue | A pointer to the data being posted onto the queue. The number of bytes of each queued item is specified when the queue is created. This number of bytes is copied from pvItemToQueue into the queue itself. |
xTickToDelay | The number of ticks that the co-routine should block to wait for space to become available on the queue, should space not be available immediately. The actual amount of time this equates to is defined by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant portTICK_RATE_MS can be used to convert ticks to milliseconds (see example below). |
pxResult | The variable pointed to by pxResult will be set to pdPASS if data was successfully posted onto the queue, otherwise it will be set to an error defined within ProjDefs.h. |
Example usage:
// Co-routine function that blocks for a fixed period then posts a number onto // a queue. static void prvCoRoutineFlashTask( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex ) { // Variables in co-routines must be declared static if they must maintain value across a blocking call. static portBASE_TYPE xNumberToPost = 0; static portBASE_TYPE xResult; // Co-routines must begin with a call to crSTART(). crSTART( xHandle ); for( ;; ) { // This assumes the queue has already been created. crQUEUE_SEND( xHandle, xCoRoutineQueue, &xNumberToPost, NO_DELAY, &xResult ); if( xResult != pdPASS ) { // The message was not posted! } // Increment the number to be posted onto the queue. xNumberToPost++; // Delay for 100 ticks. crDELAY( xHandle, 100 ); } // Co-routines must end with a call to crEND(). crEND(); }
croutine. h
crQUEUE_RECEIVE( xCoRoutineHandle xHandle, xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait, portBASE_TYPE *pxResult )
The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas xQueueSend() and xQueueReceive() can only be used from tasks.
crQUEUE_RECEIVE can only be called from the co-routine function itself - not from within a function called by the co-routine function. This is because co-routines do not maintain their own stack.
See the co-routine section of the WEB documentation for information on passing data between tasks and co-routines and between ISR's and co-routines.
xHandle | The handle of the calling co-routine. This is the xHandle parameter of the co-routine function. |
pxQueue | The handle of the queue from which the data will be received. The handle is obtained as the return value when the queue is created using the xQueueCreate() API function. |
pvBuffer | The buffer into which the received item is to be copied. The number of bytes of each queued item is specified when the queue is created. This number of bytes is copied into pvBuffer. |
xTickToDelay | The number of ticks that the co-routine should block to wait for data to become available from the queue, should data not be available immediately. The actual amount of time this equates to is defined by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant portTICK_RATE_MS can be used to convert ticks to milliseconds (see the crQUEUE_SEND example). |
pxResult | The variable pointed to by pxResult will be set to pdPASS if data was successfully retrieved from the queue, otherwise it will be set to an error code as defined within ProjDefs.h. |
Example usage:
// A co-routine receives the number of an LED to flash from a queue. It // blocks on the queue until the number is received. static void prvCoRoutineFlashWorkTask( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex ) { // Variables in co-routines must be declared static if they must maintain value across a blocking call. static portBASE_TYPE xResult; static unsigned portBASE_TYPE uxLEDToFlash; // All co-routines must start with a call to crSTART(). crSTART( xHandle ); for( ;; ) { // Wait for data to become available on the queue. crQUEUE_RECEIVE( xHandle, xCoRoutineQueue, &uxLEDToFlash, portMAX_DELAY, &xResult ); if( xResult == pdPASS ) { // We received the LED to flash - flash it! vParTestToggleLED( uxLEDToFlash ); } } crEND(); }
croutine. h
crQUEUE_SEND_FROM_ISR( xQueueHandle pxQueue, void *pvItemToQueue, portBASE_TYPE xCoRoutinePreviouslyWoken )
The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR() functions used by tasks.
crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and xQueueReceiveFromISR() can only be used to pass data between a task and and ISR.
crQUEUE_SEND_FROM_ISR can only be called from an ISR to send data to a queue that is being used from within a co-routine.
See the co-routine section of the WEB documentation for information on passing data between tasks and co-routines and between ISR's and co-routines.
xQueue | The handle to the queue on which the item is to be posted. |
pvItemToQueue | A pointer to the item that is to be placed on the queue. The size of the items the queue will hold was defined when the queue was created, so this many bytes will be copied from pvItemToQueue into the queue storage area. |
xCoRoutinePreviouslyWoken | This is included so an ISR can post onto the same queue multiple times from a single interrupt. The first call should always pass in pdFALSE. Subsequent calls should pass in the value returned from the previous call. |
Example usage:
// A co-routine that blocks on a queue waiting for characters to be received. static void vReceivingCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex ) { char cRxedChar; portBASE_TYPE xResult; // All co-routines must start with a call to crSTART(). crSTART( xHandle ); for( ;; ) { // Wait for data to become available on the queue. This assumes the // queue xCommsRxQueue has already been created! crQUEUE_RECEIVE( xHandle, xCommsRxQueue, &uxLEDToFlash, portMAX_DELAY, &xResult ); // Was a character received? if( xResult == pdPASS ) { // Process the character here. } } // All co-routines must end with a call to crEND(). crEND(); } // An ISR that uses a queue to send characters received on a serial port to // a co-routine. void vUART_ISR( void ) { char cRxedChar; portBASE_TYPE xCRWokenByPost = pdFALSE; // We loop around reading characters until there are none left in the UART. while( UART_RX_REG_NOT_EMPTY() ) { // Obtain the character from the UART. cRxedChar = UART_RX_REG; // Post the character onto a queue. xCRWokenByPost will be pdFALSE // the first time around the loop. If the post causes a co-routine // to be woken (unblocked) then xCRWokenByPost will be set to pdTRUE. // In this manner we can ensure that if more than one co-routine is // blocked on the queue only one is woken by this ISR no matter how // many characters are posted to the queue. xCRWokenByPost = crQUEUE_SEND_FROM_ISR( xCommsRxQueue, &cRxedChar, xCRWokenByPost ); } }
croutine. h
crQUEUE_RECEIVE_FROM_ISR( xQueueHandle pxQueue, void *pvBuffer, portBASE_TYPE * pxCoRoutineWoken )
The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR() functions used by tasks.
crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and xQueueReceiveFromISR() can only be used to pass data between a task and and ISR.
crQUEUE_RECEIVE_FROM_ISR can only be called from an ISR to receive data from a queue that is being used from within a co-routine (a co-routine posted to the queue).
See the co-routine section of the WEB documentation for information on passing data between tasks and co-routines and between ISR's and co-routines.
xQueue | The handle to the queue on which the item is to be posted. |
pvBuffer | A pointer to a buffer into which the received item will be placed. The size of the items the queue will hold was defined when the queue was created, so this many bytes will be copied from the queue into pvBuffer. |
pxCoRoutineWoken | A co-routine may be blocked waiting for space to become available on the queue. If crQUEUE_RECEIVE_FROM_ISR causes such a co-routine to unblock *pxCoRoutineWoken will get set to pdTRUE, otherwise *pxCoRoutineWoken will remain unchanged. |
Example usage:
// A co-routine that posts a character to a queue then blocks for a fixed // period. The character is incremented each time. static void vSendingCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex ) { // cChar holds its value while this co-routine is blocked and must therefore // be declared static. static char cCharToTx = 'a'; portBASE_TYPE xResult; // All co-routines must start with a call to crSTART(). crSTART( xHandle ); for( ;; ) { // Send the next character to the queue. crQUEUE_SEND( xHandle, xCoRoutineQueue, &cCharToTx, NO_DELAY, &xResult ); if( xResult == pdPASS ) { // The character was successfully posted to the queue. } else { // Could not post the character to the queue. } // Enable the UART Tx interrupt to cause an interrupt in this // hypothetical UART. The interrupt will obtain the character // from the queue and send it. ENABLE_RX_INTERRUPT(); // Increment to the next character then block for a fixed period. // cCharToTx will maintain its value across the delay as it is // declared static. cCharToTx++; if( cCharToTx > 'x' ) { cCharToTx = 'a'; } crDELAY( 100 ); } // All co-routines must end with a call to crEND(). crEND(); } // An ISR that uses a queue to receive characters to send on a UART. void vUART_ISR( void ) { char cCharToTx; portBASE_TYPE xCRWokenByPost = pdFALSE; while( UART_TX_REG_EMPTY() ) { // Are there any characters in the queue waiting to be sent? // xCRWokenByPost will automatically be set to pdTRUE if a co-routine // is woken by the post - ensuring that only a single co-routine is // woken no matter how many times we go around this loop. if( crQUEUE_RECEIVE_FROM_ISR( pxQueue, &cCharToTx, &xCRWokenByPost ) ) { SEND_CHARACTER( cCharToTx ); } } }