2 FreeRTOS V7.0.2 - Copyright (C) 2011 Real Time Engineers Ltd.
5 ***************************************************************************
7 * FreeRTOS tutorial books are available in pdf and paperback. *
8 * Complete, revised, and edited pdf reference manuals are also *
11 * Purchasing FreeRTOS documentation will not only help you, by *
12 * ensuring you get running as quickly as possible and with an *
13 * in-depth knowledge of how to use FreeRTOS, it will also help *
14 * the FreeRTOS project to continue with its mission of providing *
15 * professional grade, cross platform, de facto standard solutions *
16 * for microcontrollers - completely free of charge! *
18 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
20 * Thank you for using FreeRTOS, and thank you for your support! *
22 ***************************************************************************
25 This file is part of the FreeRTOS distribution.
27 FreeRTOS is free software; you can redistribute it and/or modify it under
28 the terms of the GNU General Public License (version 2) as published by the
29 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
30 >>>NOTE<<< The modification to the GPL is included to allow you to
31 distribute a combined work that includes FreeRTOS without being obliged to
32 provide the source code for proprietary components outside of the FreeRTOS
33 kernel. FreeRTOS is distributed in the hope that it will be useful, but
34 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
35 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
36 more details. You should have received a copy of the GNU General Public
37 License and the FreeRTOS license exception along with FreeRTOS; if not it
38 can be viewed here: http://www.freertos.org/a00114.html and also obtained
39 by writing to Richard Barry, contact details for whom are available on the
44 http://www.FreeRTOS.org - Documentation, latest information, license and
47 http://www.SafeRTOS.com - A version that is certified for use in safety
50 http://www.OpenRTOS.com - Commercial support, development, porting,
51 licensing and training services.
57 #ifndef INC_FREERTOS_H
58 #error "include FreeRTOS.h must appear in source files before include croutine.h"
67 /* Used to hide the implementation of the co-routine control block. The
68 control block structure however has to be included in the header due to
69 the macro implementation of the co-routine functionality. */
70 typedef void * xCoRoutineHandle;
72 /* Defines the prototype to which co-routine functions must conform. */
73 typedef void (*crCOROUTINE_CODE)( xCoRoutineHandle, unsigned portBASE_TYPE );
75 typedef struct corCoRoutineControlBlock
77 crCOROUTINE_CODE pxCoRoutineFunction;
78 xListItem xGenericListItem; /*< List item used to place the CRCB in ready and blocked queues. */
79 xListItem xEventListItem; /*< List item used to place the CRCB in event lists. */
80 unsigned portBASE_TYPE uxPriority; /*< The priority of the co-routine in relation to other co-routines. */
81 unsigned portBASE_TYPE uxIndex; /*< Used to distinguish between co-routines when multiple co-routines use the same co-routine function. */
82 unsigned short uxState; /*< Used internally by the co-routine implementation. */
83 } corCRCB; /* Co-routine control block. Note must be identical in size down to uxPriority with tskTCB. */
88 portBASE_TYPE xCoRoutineCreate(
89 crCOROUTINE_CODE pxCoRoutineCode,
90 unsigned portBASE_TYPE uxPriority,
91 unsigned portBASE_TYPE uxIndex
94 * Create a new co-routine and add it to the list of co-routines that are
97 * @param pxCoRoutineCode Pointer to the co-routine function. Co-routine
98 * functions require special syntax - see the co-routine section of the WEB
99 * documentation for more information.
101 * @param uxPriority The priority with respect to other co-routines at which
102 * the co-routine will run.
104 * @param uxIndex Used to distinguish between different co-routines that
105 * execute the same function. See the example below and the co-routine section
106 * of the WEB documentation for further information.
108 * @return pdPASS if the co-routine was successfully created and added to a ready
109 * list, otherwise an error code defined with ProjDefs.h.
113 // Co-routine to be created.
114 void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
116 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
117 // This may not be necessary for const variables.
118 static const char cLedToFlash[ 2 ] = { 5, 6 };
119 static const portTickType uxFlashRates[ 2 ] = { 200, 400 };
121 // Must start every co-routine with a call to crSTART();
126 // This co-routine just delays for a fixed period, then toggles
127 // an LED. Two co-routines are created using this function, so
128 // the uxIndex parameter is used to tell the co-routine which
129 // LED to flash and how long to delay. This assumes xQueue has
130 // already been created.
131 vParTestToggleLED( cLedToFlash[ uxIndex ] );
132 crDELAY( xHandle, uxFlashRates[ uxIndex ] );
135 // Must end every co-routine with a call to crEND();
139 // Function that creates two co-routines.
140 void vOtherFunction( void )
142 unsigned char ucParameterToPass;
145 // Create two co-routines at priority 0. The first is given index 0
146 // so (from the code above) toggles LED 5 every 200 ticks. The second
147 // is given index 1 so toggles LED 6 every 400 ticks.
148 for( uxIndex = 0; uxIndex < 2; uxIndex++ )
150 xCoRoutineCreate( vFlashCoRoutine, 0, uxIndex );
154 * \defgroup xCoRoutineCreate xCoRoutineCreate
157 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex );
163 void vCoRoutineSchedule( void );</pre>
167 * vCoRoutineSchedule() executes the highest priority co-routine that is able
168 * to run. The co-routine will execute until it either blocks, yields or is
169 * preempted by a task. Co-routines execute cooperatively so one
170 * co-routine cannot be preempted by another, but can be preempted by a task.
172 * If an application comprises of both tasks and co-routines then
173 * vCoRoutineSchedule should be called from the idle task (in an idle task
178 // This idle task hook will schedule a co-routine each time it is called.
179 // The rest of the idle task will execute between co-routine calls.
180 void vApplicationIdleHook( void )
182 vCoRoutineSchedule();
185 // Alternatively, if you do not require any other part of the idle task to
186 // execute, the idle task hook can call vCoRoutineScheduler() within an
188 void vApplicationIdleHook( void )
192 vCoRoutineSchedule();
196 * \defgroup vCoRoutineSchedule vCoRoutineSchedule
199 void vCoRoutineSchedule( void );
204 crSTART( xCoRoutineHandle xHandle );</pre>
206 * This macro MUST always be called at the start of a co-routine function.
210 // Co-routine to be created.
211 void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
213 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
214 static long ulAVariable;
216 // Must start every co-routine with a call to crSTART();
221 // Co-routine functionality goes here.
224 // Must end every co-routine with a call to crEND();
227 * \defgroup crSTART crSTART
230 #define crSTART( pxCRCB ) switch( ( ( corCRCB * )( pxCRCB ) )->uxState ) { case 0:
237 * This macro MUST always be called at the end of a co-routine function.
241 // Co-routine to be created.
242 void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
244 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
245 static long ulAVariable;
247 // Must start every co-routine with a call to crSTART();
252 // Co-routine functionality goes here.
255 // Must end every co-routine with a call to crEND();
258 * \defgroup crSTART crSTART
264 * These macros are intended for internal use by the co-routine implementation
265 * only. The macros should not be used directly by application writers.
267 #define crSET_STATE0( xHandle ) ( ( corCRCB * )( xHandle ) )->uxState = (__LINE__ * 2); return; case (__LINE__ * 2):
268 #define crSET_STATE1( xHandle ) ( ( corCRCB * )( xHandle ) )->uxState = ((__LINE__ * 2)+1); return; case ((__LINE__ * 2)+1):
273 crDELAY( xCoRoutineHandle xHandle, portTickType xTicksToDelay );</pre>
275 * Delay a co-routine for a fixed period of time.
277 * crDELAY can only be called from the co-routine function itself - not
278 * from within a function called by the co-routine function. This is because
279 * co-routines do not maintain their own stack.
281 * @param xHandle The handle of the co-routine to delay. This is the xHandle
282 * parameter of the co-routine function.
284 * @param xTickToDelay The number of ticks that the co-routine should delay
285 * for. The actual amount of time this equates to is defined by
286 * configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant portTICK_RATE_MS
287 * can be used to convert ticks to milliseconds.
291 // Co-routine to be created.
292 void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
294 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
295 // This may not be necessary for const variables.
296 // We are to delay for 200ms.
297 static const xTickType xDelayTime = 200 / portTICK_RATE_MS;
299 // Must start every co-routine with a call to crSTART();
305 crDELAY( xHandle, xDelayTime );
307 // Do something here.
310 // Must end every co-routine with a call to crEND();
313 * \defgroup crDELAY crDELAY
316 #define crDELAY( xHandle, xTicksToDelay ) \
317 if( ( xTicksToDelay ) > 0 ) \
319 vCoRoutineAddToDelayedList( ( xTicksToDelay ), NULL ); \
321 crSET_STATE0( ( xHandle ) );
326 xCoRoutineHandle xHandle,
327 xQueueHandle pxQueue,
329 portTickType xTicksToWait,
330 portBASE_TYPE *pxResult
333 * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
334 * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
336 * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
337 * xQueueSend() and xQueueReceive() can only be used from tasks.
339 * crQUEUE_SEND can only be called from the co-routine function itself - not
340 * from within a function called by the co-routine function. This is because
341 * co-routines do not maintain their own stack.
343 * See the co-routine section of the WEB documentation for information on
344 * passing data between tasks and co-routines and between ISR's and
347 * @param xHandle The handle of the calling co-routine. This is the xHandle
348 * parameter of the co-routine function.
350 * @param pxQueue The handle of the queue on which the data will be posted.
351 * The handle is obtained as the return value when the queue is created using
352 * the xQueueCreate() API function.
354 * @param pvItemToQueue A pointer to the data being posted onto the queue.
355 * The number of bytes of each queued item is specified when the queue is
356 * created. This number of bytes is copied from pvItemToQueue into the queue
359 * @param xTickToDelay The number of ticks that the co-routine should block
360 * to wait for space to become available on the queue, should space not be
361 * available immediately. The actual amount of time this equates to is defined
362 * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
363 * portTICK_RATE_MS can be used to convert ticks to milliseconds (see example
366 * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
367 * data was successfully posted onto the queue, otherwise it will be set to an
368 * error defined within ProjDefs.h.
372 // Co-routine function that blocks for a fixed period then posts a number onto
374 static void prvCoRoutineFlashTask( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
376 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
377 static portBASE_TYPE xNumberToPost = 0;
378 static portBASE_TYPE xResult;
380 // Co-routines must begin with a call to crSTART().
385 // This assumes the queue has already been created.
386 crQUEUE_SEND( xHandle, xCoRoutineQueue, &xNumberToPost, NO_DELAY, &xResult );
388 if( xResult != pdPASS )
390 // The message was not posted!
393 // Increment the number to be posted onto the queue.
396 // Delay for 100 ticks.
397 crDELAY( xHandle, 100 );
400 // Co-routines must end with a call to crEND().
403 * \defgroup crQUEUE_SEND crQUEUE_SEND
406 #define crQUEUE_SEND( xHandle, pxQueue, pvItemToQueue, xTicksToWait, pxResult ) \
408 *( pxResult ) = xQueueCRSend( ( pxQueue) , ( pvItemToQueue) , ( xTicksToWait ) ); \
409 if( *( pxResult ) == errQUEUE_BLOCKED ) \
411 crSET_STATE0( ( xHandle ) ); \
412 *pxResult = xQueueCRSend( ( pxQueue ), ( pvItemToQueue ), 0 ); \
414 if( *pxResult == errQUEUE_YIELD ) \
416 crSET_STATE1( ( xHandle ) ); \
417 *pxResult = pdPASS; \
425 xCoRoutineHandle xHandle,
426 xQueueHandle pxQueue,
428 portTickType xTicksToWait,
429 portBASE_TYPE *pxResult
432 * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
433 * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
435 * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
436 * xQueueSend() and xQueueReceive() can only be used from tasks.
438 * crQUEUE_RECEIVE can only be called from the co-routine function itself - not
439 * from within a function called by the co-routine function. This is because
440 * co-routines do not maintain their own stack.
442 * See the co-routine section of the WEB documentation for information on
443 * passing data between tasks and co-routines and between ISR's and
446 * @param xHandle The handle of the calling co-routine. This is the xHandle
447 * parameter of the co-routine function.
449 * @param pxQueue The handle of the queue from which the data will be received.
450 * The handle is obtained as the return value when the queue is created using
451 * the xQueueCreate() API function.
453 * @param pvBuffer The buffer into which the received item is to be copied.
454 * The number of bytes of each queued item is specified when the queue is
455 * created. This number of bytes is copied into pvBuffer.
457 * @param xTickToDelay The number of ticks that the co-routine should block
458 * to wait for data to become available from the queue, should data not be
459 * available immediately. The actual amount of time this equates to is defined
460 * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
461 * portTICK_RATE_MS can be used to convert ticks to milliseconds (see the
462 * crQUEUE_SEND example).
464 * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
465 * data was successfully retrieved from the queue, otherwise it will be set to
466 * an error code as defined within ProjDefs.h.
470 // A co-routine receives the number of an LED to flash from a queue. It
471 // blocks on the queue until the number is received.
472 static void prvCoRoutineFlashWorkTask( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
474 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
475 static portBASE_TYPE xResult;
476 static unsigned portBASE_TYPE uxLEDToFlash;
478 // All co-routines must start with a call to crSTART().
483 // Wait for data to become available on the queue.
484 crQUEUE_RECEIVE( xHandle, xCoRoutineQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
486 if( xResult == pdPASS )
488 // We received the LED to flash - flash it!
489 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 xQueueHandle pxQueue,
519 portBASE_TYPE xCoRoutinePreviouslyWoken
522 * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
523 * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
524 * functions used by tasks.
526 * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
527 * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
528 * xQueueReceiveFromISR() can only be used to pass data between a task and and
531 * crQUEUE_SEND_FROM_ISR can only be called from an ISR to send data to a queue
532 * that is being used from within a co-routine.
534 * See the co-routine section of the WEB documentation for information on
535 * passing data between tasks and co-routines and between ISR's and
538 * @param xQueue The handle to the queue on which the item is to be posted.
540 * @param pvItemToQueue A pointer to the item that is to be placed on the
541 * queue. The size of the items the queue will hold was defined when the
542 * queue was created, so this many bytes will be copied from pvItemToQueue
543 * into the queue storage area.
545 * @param xCoRoutinePreviouslyWoken This is included so an ISR can post onto
546 * the same queue multiple times from a single interrupt. The first call
547 * should always pass in pdFALSE. Subsequent calls should pass in
548 * the value returned from the previous call.
550 * @return pdTRUE if a co-routine was woken by posting onto the queue. This is
551 * used by the ISR to determine if a context switch may be required following
556 // A co-routine that blocks on a queue waiting for characters to be received.
557 static void vReceivingCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
560 portBASE_TYPE xResult;
562 // All co-routines must start with a call to crSTART().
567 // Wait for data to become available on the queue. This assumes the
568 // queue xCommsRxQueue has already been created!
569 crQUEUE_RECEIVE( xHandle, xCommsRxQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
571 // Was a character received?
572 if( xResult == pdPASS )
574 // Process the character here.
578 // All co-routines must end with a call to crEND().
582 // An ISR that uses a queue to send characters received on a serial port to
584 void vUART_ISR( void )
587 portBASE_TYPE xCRWokenByPost = pdFALSE;
589 // We loop around reading characters until there are none left in the UART.
590 while( UART_RX_REG_NOT_EMPTY() )
592 // Obtain the character from the UART.
593 cRxedChar = UART_RX_REG;
595 // Post the character onto a queue. xCRWokenByPost will be pdFALSE
596 // the first time around the loop. If the post causes a co-routine
597 // to be woken (unblocked) then xCRWokenByPost will be set to pdTRUE.
598 // In this manner we can ensure that if more than one co-routine is
599 // blocked on the queue only one is woken by this ISR no matter how
600 // many characters are posted to the queue.
601 xCRWokenByPost = crQUEUE_SEND_FROM_ISR( xCommsRxQueue, &cRxedChar, xCRWokenByPost );
604 * \defgroup crQUEUE_SEND_FROM_ISR crQUEUE_SEND_FROM_ISR
607 #define crQUEUE_SEND_FROM_ISR( pxQueue, pvItemToQueue, xCoRoutinePreviouslyWoken ) xQueueCRSendFromISR( ( pxQueue ), ( pvItemToQueue ), ( xCoRoutinePreviouslyWoken ) )
613 crQUEUE_SEND_FROM_ISR(
614 xQueueHandle pxQueue,
616 portBASE_TYPE * pxCoRoutineWoken
619 * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
620 * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
621 * functions used by tasks.
623 * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
624 * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
625 * xQueueReceiveFromISR() can only be used to pass data between a task and and
628 * crQUEUE_RECEIVE_FROM_ISR can only be called from an ISR to receive data
629 * from a queue that is being used from within a co-routine (a co-routine
630 * posted to the queue).
632 * See the co-routine section of the WEB documentation for information on
633 * passing data between tasks and co-routines and between ISR's and
636 * @param xQueue The handle to the queue on which the item is to be posted.
638 * @param pvBuffer A pointer to a buffer into which the received item will be
639 * placed. The size of the items the queue will hold was defined when the
640 * queue was created, so this many bytes will be copied from the queue into
643 * @param pxCoRoutineWoken A co-routine may be blocked waiting for space to become
644 * available on the queue. If crQUEUE_RECEIVE_FROM_ISR causes such a
645 * co-routine to unblock *pxCoRoutineWoken will get set to pdTRUE, otherwise
646 * *pxCoRoutineWoken will remain unchanged.
648 * @return pdTRUE an item was successfully received from the queue, otherwise
653 // A co-routine that posts a character to a queue then blocks for a fixed
654 // period. The character is incremented each time.
655 static void vSendingCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
657 // cChar holds its value while this co-routine is blocked and must therefore
658 // be declared static.
659 static char cCharToTx = 'a';
660 portBASE_TYPE xResult;
662 // All co-routines must start with a call to crSTART().
667 // Send the next character to the queue.
668 crQUEUE_SEND( xHandle, xCoRoutineQueue, &cCharToTx, NO_DELAY, &xResult );
670 if( xResult == pdPASS )
672 // The character was successfully posted to the queue.
676 // Could not post the character to the queue.
679 // Enable the UART Tx interrupt to cause an interrupt in this
680 // hypothetical UART. The interrupt will obtain the character
681 // from the queue and send it.
682 ENABLE_RX_INTERRUPT();
684 // Increment to the next character then block for a fixed period.
685 // cCharToTx will maintain its value across the delay as it is
688 if( cCharToTx > 'x' )
695 // All co-routines must end with a call to crEND().
699 // An ISR that uses a queue to receive characters to send on a UART.
700 void vUART_ISR( void )
703 portBASE_TYPE xCRWokenByPost = pdFALSE;
705 while( UART_TX_REG_EMPTY() )
707 // Are there any characters in the queue waiting to be sent?
708 // xCRWokenByPost will automatically be set to pdTRUE if a co-routine
709 // is woken by the post - ensuring that only a single co-routine is
710 // woken no matter how many times we go around this loop.
711 if( crQUEUE_RECEIVE_FROM_ISR( pxQueue, &cCharToTx, &xCRWokenByPost ) )
713 SEND_CHARACTER( cCharToTx );
717 * \defgroup crQUEUE_RECEIVE_FROM_ISR crQUEUE_RECEIVE_FROM_ISR
720 #define crQUEUE_RECEIVE_FROM_ISR( pxQueue, pvBuffer, pxCoRoutineWoken ) xQueueCRReceiveFromISR( ( pxQueue ), ( pvBuffer ), ( pxCoRoutineWoken ) )
723 * This function is intended for internal use by the co-routine macros only.
724 * The macro nature of the co-routine implementation requires that the
725 * prototype appears here. The function should not be used by application
728 * Removes the current co-routine from its ready list and places it in the
729 * appropriate delayed list.
731 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList );
734 * This function is intended for internal use by the queue implementation only.
735 * The function should not be used by application writers.
737 * Removes the highest priority co-routine from the event list and places it in
738 * the pending ready list.
740 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList );
746 #endif /* CO_ROUTINE_H */