2 FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.
\r
4 FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME. PLEASE VISIT
\r
5 http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS tutorial books are available in pdf and paperback. *
\r
10 * Complete, revised, and edited pdf reference manuals are also *
\r
13 * Purchasing FreeRTOS documentation will not only help you, by *
\r
14 * ensuring you get running as quickly as possible and with an *
\r
15 * in-depth knowledge of how to use FreeRTOS, it will also help *
\r
16 * the FreeRTOS project to continue with its mission of providing *
\r
17 * professional grade, cross platform, de facto standard solutions *
\r
18 * for microcontrollers - completely free of charge! *
\r
20 * >>> See http://www.FreeRTOS.org/Documentation for details. <<< *
\r
22 * Thank you for using FreeRTOS, and thank you for your support! *
\r
24 ***************************************************************************
\r
27 This file is part of the FreeRTOS distribution.
\r
29 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
30 the terms of the GNU General Public License (version 2) as published by the
\r
31 Free Software Foundation AND MODIFIED BY the FreeRTOS exception.
\r
32 >>>NOTE<<< The modification to the GPL is included to allow you to
\r
33 distribute a combined work that includes FreeRTOS without being obliged to
\r
34 provide the source code for proprietary components outside of the FreeRTOS
\r
35 kernel. FreeRTOS is distributed in the hope that it will be useful, but
\r
36 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
\r
37 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
\r
38 more details. You should have received a copy of the GNU General Public
\r
39 License and the FreeRTOS license exception along with FreeRTOS; if not it
\r
40 can be viewed here: http://www.freertos.org/a00114.html and also obtained
\r
41 by writing to Richard Barry, contact details for whom are available on the
\r
46 ***************************************************************************
\r
48 * Having a problem? Start by reading the FAQ "My application does *
\r
49 * not run, what could be wrong?" *
\r
51 * http://www.FreeRTOS.org/FAQHelp.html *
\r
53 ***************************************************************************
\r
56 http://www.FreeRTOS.org - Documentation, training, latest versions, license
\r
57 and contact details.
\r
59 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
60 including FreeRTOS+Trace - an indispensable productivity tool.
\r
62 Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell
\r
63 the code with commercial support, indemnification, and middleware, under
\r
64 the OpenRTOS brand: http://www.OpenRTOS.com. High Integrity Systems also
\r
65 provide a safety engineered and independently SIL3 certified version under
\r
66 the SafeRTOS brand: http://www.SafeRTOS.com.
\r
69 #ifndef CO_ROUTINE_H
\r
70 #define CO_ROUTINE_H
\r
72 #ifndef INC_FREERTOS_H
\r
73 #error "include FreeRTOS.h must appear in source files before include croutine.h"
\r
82 /* Used to hide the implementation of the co-routine control block. The
\r
83 control block structure however has to be included in the header due to
\r
84 the macro implementation of the co-routine functionality. */
\r
85 typedef void * xCoRoutineHandle;
\r
87 /* Defines the prototype to which co-routine functions must conform. */
\r
88 typedef void (*crCOROUTINE_CODE)( xCoRoutineHandle, unsigned portBASE_TYPE );
\r
90 typedef struct corCoRoutineControlBlock
\r
92 crCOROUTINE_CODE pxCoRoutineFunction;
\r
93 xListItem xGenericListItem; /*< List item used to place the CRCB in ready and blocked queues. */
\r
94 xListItem xEventListItem; /*< List item used to place the CRCB in event lists. */
\r
95 unsigned portBASE_TYPE uxPriority; /*< The priority of the co-routine in relation to other co-routines. */
\r
96 unsigned portBASE_TYPE uxIndex; /*< Used to distinguish between co-routines when multiple co-routines use the same co-routine function. */
\r
97 unsigned short uxState; /*< Used internally by the co-routine implementation. */
\r
98 } corCRCB; /* Co-routine control block. Note must be identical in size down to uxPriority with tskTCB. */
\r
103 portBASE_TYPE xCoRoutineCreate(
\r
104 crCOROUTINE_CODE pxCoRoutineCode,
\r
105 unsigned portBASE_TYPE uxPriority,
\r
106 unsigned portBASE_TYPE uxIndex
\r
109 * Create a new co-routine and add it to the list of co-routines that are
\r
112 * @param pxCoRoutineCode Pointer to the co-routine function. Co-routine
\r
113 * functions require special syntax - see the co-routine section of the WEB
\r
114 * documentation for more information.
\r
116 * @param uxPriority The priority with respect to other co-routines at which
\r
117 * the co-routine will run.
\r
119 * @param uxIndex Used to distinguish between different co-routines that
\r
120 * execute the same function. See the example below and the co-routine section
\r
121 * of the WEB documentation for further information.
\r
123 * @return pdPASS if the co-routine was successfully created and added to a ready
\r
124 * list, otherwise an error code defined with ProjDefs.h.
\r
128 // Co-routine to be created.
\r
129 void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
131 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
132 // This may not be necessary for const variables.
\r
133 static const char cLedToFlash[ 2 ] = { 5, 6 };
\r
134 static const portTickType uxFlashRates[ 2 ] = { 200, 400 };
\r
136 // Must start every co-routine with a call to crSTART();
\r
137 crSTART( xHandle );
\r
141 // This co-routine just delays for a fixed period, then toggles
\r
142 // an LED. Two co-routines are created using this function, so
\r
143 // the uxIndex parameter is used to tell the co-routine which
\r
144 // LED to flash and how long to delay. This assumes xQueue has
\r
145 // already been created.
\r
146 vParTestToggleLED( cLedToFlash[ uxIndex ] );
\r
147 crDELAY( xHandle, uxFlashRates[ uxIndex ] );
\r
150 // Must end every co-routine with a call to crEND();
\r
154 // Function that creates two co-routines.
\r
155 void vOtherFunction( void )
\r
157 unsigned char ucParameterToPass;
\r
158 xTaskHandle xHandle;
\r
160 // Create two co-routines at priority 0. The first is given index 0
\r
161 // so (from the code above) toggles LED 5 every 200 ticks. The second
\r
162 // is given index 1 so toggles LED 6 every 400 ticks.
\r
163 for( uxIndex = 0; uxIndex < 2; uxIndex++ )
\r
165 xCoRoutineCreate( vFlashCoRoutine, 0, uxIndex );
\r
169 * \defgroup xCoRoutineCreate xCoRoutineCreate
\r
172 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex );
\r
178 void vCoRoutineSchedule( void );</pre>
\r
180 * Run a co-routine.
\r
182 * vCoRoutineSchedule() executes the highest priority co-routine that is able
\r
183 * to run. The co-routine will execute until it either blocks, yields or is
\r
184 * preempted by a task. Co-routines execute cooperatively so one
\r
185 * co-routine cannot be preempted by another, but can be preempted by a task.
\r
187 * If an application comprises of both tasks and co-routines then
\r
188 * vCoRoutineSchedule should be called from the idle task (in an idle task
\r
193 // This idle task hook will schedule a co-routine each time it is called.
\r
194 // The rest of the idle task will execute between co-routine calls.
\r
195 void vApplicationIdleHook( void )
\r
197 vCoRoutineSchedule();
\r
200 // Alternatively, if you do not require any other part of the idle task to
\r
201 // execute, the idle task hook can call vCoRoutineScheduler() within an
\r
203 void vApplicationIdleHook( void )
\r
207 vCoRoutineSchedule();
\r
211 * \defgroup vCoRoutineSchedule vCoRoutineSchedule
\r
214 void vCoRoutineSchedule( void );
\r
219 crSTART( xCoRoutineHandle xHandle );</pre>
\r
221 * This macro MUST always be called at the start of a co-routine function.
\r
225 // Co-routine to be created.
\r
226 void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
228 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
229 static long ulAVariable;
\r
231 // Must start every co-routine with a call to crSTART();
\r
232 crSTART( xHandle );
\r
236 // Co-routine functionality goes here.
\r
239 // Must end every co-routine with a call to crEND();
\r
242 * \defgroup crSTART crSTART
\r
245 #define crSTART( pxCRCB ) switch( ( ( corCRCB * )( pxCRCB ) )->uxState ) { case 0:
\r
252 * This macro MUST always be called at the end of a co-routine function.
\r
256 // Co-routine to be created.
\r
257 void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
259 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
260 static long ulAVariable;
\r
262 // Must start every co-routine with a call to crSTART();
\r
263 crSTART( xHandle );
\r
267 // Co-routine functionality goes here.
\r
270 // Must end every co-routine with a call to crEND();
\r
273 * \defgroup crSTART crSTART
\r
279 * These macros are intended for internal use by the co-routine implementation
\r
280 * only. The macros should not be used directly by application writers.
\r
282 #define crSET_STATE0( xHandle ) ( ( corCRCB * )( xHandle ) )->uxState = (__LINE__ * 2); return; case (__LINE__ * 2):
\r
283 #define crSET_STATE1( xHandle ) ( ( corCRCB * )( xHandle ) )->uxState = ((__LINE__ * 2)+1); return; case ((__LINE__ * 2)+1):
\r
288 crDELAY( xCoRoutineHandle xHandle, portTickType xTicksToDelay );</pre>
\r
290 * Delay a co-routine for a fixed period of time.
\r
292 * crDELAY can only be called from the co-routine function itself - not
\r
293 * from within a function called by the co-routine function. This is because
\r
294 * co-routines do not maintain their own stack.
\r
296 * @param xHandle The handle of the co-routine to delay. This is the xHandle
\r
297 * parameter of the co-routine function.
\r
299 * @param xTickToDelay The number of ticks that the co-routine should delay
\r
300 * for. The actual amount of time this equates to is defined by
\r
301 * configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant portTICK_RATE_MS
\r
302 * can be used to convert ticks to milliseconds.
\r
306 // Co-routine to be created.
\r
307 void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
309 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
310 // This may not be necessary for const variables.
\r
311 // We are to delay for 200ms.
\r
312 static const xTickType xDelayTime = 200 / portTICK_RATE_MS;
\r
314 // Must start every co-routine with a call to crSTART();
\r
315 crSTART( xHandle );
\r
319 // Delay for 200ms.
\r
320 crDELAY( xHandle, xDelayTime );
\r
322 // Do something here.
\r
325 // Must end every co-routine with a call to crEND();
\r
328 * \defgroup crDELAY crDELAY
\r
331 #define crDELAY( xHandle, xTicksToDelay ) \
\r
332 if( ( xTicksToDelay ) > 0 ) \
\r
334 vCoRoutineAddToDelayedList( ( xTicksToDelay ), NULL ); \
\r
336 crSET_STATE0( ( xHandle ) );
\r
341 xCoRoutineHandle xHandle,
\r
342 xQueueHandle pxQueue,
\r
343 void *pvItemToQueue,
\r
344 portTickType xTicksToWait,
\r
345 portBASE_TYPE *pxResult
\r
348 * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
\r
349 * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
\r
351 * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
\r
352 * xQueueSend() and xQueueReceive() can only be used from tasks.
\r
354 * crQUEUE_SEND can only be called from the co-routine function itself - not
\r
355 * from within a function called by the co-routine function. This is because
\r
356 * co-routines do not maintain their own stack.
\r
358 * See the co-routine section of the WEB documentation for information on
\r
359 * passing data between tasks and co-routines and between ISR's and
\r
362 * @param xHandle The handle of the calling co-routine. This is the xHandle
\r
363 * parameter of the co-routine function.
\r
365 * @param pxQueue The handle of the queue on which the data will be posted.
\r
366 * The handle is obtained as the return value when the queue is created using
\r
367 * the xQueueCreate() API function.
\r
369 * @param pvItemToQueue A pointer to the data being posted onto the queue.
\r
370 * The number of bytes of each queued item is specified when the queue is
\r
371 * created. This number of bytes is copied from pvItemToQueue into the queue
\r
374 * @param xTickToDelay The number of ticks that the co-routine should block
\r
375 * to wait for space to become available on the queue, should space not be
\r
376 * available immediately. The actual amount of time this equates to is defined
\r
377 * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
\r
378 * portTICK_RATE_MS can be used to convert ticks to milliseconds (see example
\r
381 * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
\r
382 * data was successfully posted onto the queue, otherwise it will be set to an
\r
383 * error defined within ProjDefs.h.
\r
387 // Co-routine function that blocks for a fixed period then posts a number onto
\r
389 static void prvCoRoutineFlashTask( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
391 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
392 static portBASE_TYPE xNumberToPost = 0;
\r
393 static portBASE_TYPE xResult;
\r
395 // Co-routines must begin with a call to crSTART().
\r
396 crSTART( xHandle );
\r
400 // This assumes the queue has already been created.
\r
401 crQUEUE_SEND( xHandle, xCoRoutineQueue, &xNumberToPost, NO_DELAY, &xResult );
\r
403 if( xResult != pdPASS )
\r
405 // The message was not posted!
\r
408 // Increment the number to be posted onto the queue.
\r
411 // Delay for 100 ticks.
\r
412 crDELAY( xHandle, 100 );
\r
415 // Co-routines must end with a call to crEND().
\r
418 * \defgroup crQUEUE_SEND crQUEUE_SEND
\r
421 #define crQUEUE_SEND( xHandle, pxQueue, pvItemToQueue, xTicksToWait, pxResult ) \
\r
423 *( pxResult ) = xQueueCRSend( ( pxQueue) , ( pvItemToQueue) , ( xTicksToWait ) ); \
\r
424 if( *( pxResult ) == errQUEUE_BLOCKED ) \
\r
426 crSET_STATE0( ( xHandle ) ); \
\r
427 *pxResult = xQueueCRSend( ( pxQueue ), ( pvItemToQueue ), 0 ); \
\r
429 if( *pxResult == errQUEUE_YIELD ) \
\r
431 crSET_STATE1( ( xHandle ) ); \
\r
432 *pxResult = pdPASS; \
\r
440 xCoRoutineHandle xHandle,
\r
441 xQueueHandle pxQueue,
\r
443 portTickType xTicksToWait,
\r
444 portBASE_TYPE *pxResult
\r
447 * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
\r
448 * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
\r
450 * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
\r
451 * xQueueSend() and xQueueReceive() can only be used from tasks.
\r
453 * crQUEUE_RECEIVE can only be called from the co-routine function itself - not
\r
454 * from within a function called by the co-routine function. This is because
\r
455 * co-routines do not maintain their own stack.
\r
457 * See the co-routine section of the WEB documentation for information on
\r
458 * passing data between tasks and co-routines and between ISR's and
\r
461 * @param xHandle The handle of the calling co-routine. This is the xHandle
\r
462 * parameter of the co-routine function.
\r
464 * @param pxQueue The handle of the queue from which the data will be received.
\r
465 * The handle is obtained as the return value when the queue is created using
\r
466 * the xQueueCreate() API function.
\r
468 * @param pvBuffer The buffer into which the received item is to be copied.
\r
469 * The number of bytes of each queued item is specified when the queue is
\r
470 * created. This number of bytes is copied into pvBuffer.
\r
472 * @param xTickToDelay The number of ticks that the co-routine should block
\r
473 * to wait for data to become available from the queue, should data not be
\r
474 * available immediately. The actual amount of time this equates to is defined
\r
475 * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
\r
476 * portTICK_RATE_MS can be used to convert ticks to milliseconds (see the
\r
477 * crQUEUE_SEND example).
\r
479 * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
\r
480 * data was successfully retrieved from the queue, otherwise it will be set to
\r
481 * an error code as defined within ProjDefs.h.
\r
485 // A co-routine receives the number of an LED to flash from a queue. It
\r
486 // blocks on the queue until the number is received.
\r
487 static void prvCoRoutineFlashWorkTask( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
489 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
490 static portBASE_TYPE xResult;
\r
491 static unsigned portBASE_TYPE uxLEDToFlash;
\r
493 // All co-routines must start with a call to crSTART().
\r
494 crSTART( xHandle );
\r
498 // Wait for data to become available on the queue.
\r
499 crQUEUE_RECEIVE( xHandle, xCoRoutineQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
\r
501 if( xResult == pdPASS )
\r
503 // We received the LED to flash - flash it!
\r
504 vParTestToggleLED( uxLEDToFlash );
\r
510 * \defgroup crQUEUE_RECEIVE crQUEUE_RECEIVE
\r
513 #define crQUEUE_RECEIVE( xHandle, pxQueue, pvBuffer, xTicksToWait, pxResult ) \
\r
515 *( pxResult ) = xQueueCRReceive( ( pxQueue) , ( pvBuffer ), ( xTicksToWait ) ); \
\r
516 if( *( pxResult ) == errQUEUE_BLOCKED ) \
\r
518 crSET_STATE0( ( xHandle ) ); \
\r
519 *( pxResult ) = xQueueCRReceive( ( pxQueue) , ( pvBuffer ), 0 ); \
\r
521 if( *( pxResult ) == errQUEUE_YIELD ) \
\r
523 crSET_STATE1( ( xHandle ) ); \
\r
524 *( pxResult ) = pdPASS; \
\r
531 crQUEUE_SEND_FROM_ISR(
\r
532 xQueueHandle pxQueue,
\r
533 void *pvItemToQueue,
\r
534 portBASE_TYPE xCoRoutinePreviouslyWoken
\r
537 * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
\r
538 * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
\r
539 * functions used by tasks.
\r
541 * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
\r
542 * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
\r
543 * xQueueReceiveFromISR() can only be used to pass data between a task and and
\r
546 * crQUEUE_SEND_FROM_ISR can only be called from an ISR to send data to a queue
\r
547 * that is being used from within a co-routine.
\r
549 * See the co-routine section of the WEB documentation for information on
\r
550 * passing data between tasks and co-routines and between ISR's and
\r
553 * @param xQueue The handle to the queue on which the item is to be posted.
\r
555 * @param pvItemToQueue A pointer to the item that is to be placed on the
\r
556 * queue. The size of the items the queue will hold was defined when the
\r
557 * queue was created, so this many bytes will be copied from pvItemToQueue
\r
558 * into the queue storage area.
\r
560 * @param xCoRoutinePreviouslyWoken This is included so an ISR can post onto
\r
561 * the same queue multiple times from a single interrupt. The first call
\r
562 * should always pass in pdFALSE. Subsequent calls should pass in
\r
563 * the value returned from the previous call.
\r
565 * @return pdTRUE if a co-routine was woken by posting onto the queue. This is
\r
566 * used by the ISR to determine if a context switch may be required following
\r
571 // A co-routine that blocks on a queue waiting for characters to be received.
\r
572 static void vReceivingCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
575 portBASE_TYPE xResult;
\r
577 // All co-routines must start with a call to crSTART().
\r
578 crSTART( xHandle );
\r
582 // Wait for data to become available on the queue. This assumes the
\r
583 // queue xCommsRxQueue has already been created!
\r
584 crQUEUE_RECEIVE( xHandle, xCommsRxQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
\r
586 // Was a character received?
\r
587 if( xResult == pdPASS )
\r
589 // Process the character here.
\r
593 // All co-routines must end with a call to crEND().
\r
597 // An ISR that uses a queue to send characters received on a serial port to
\r
599 void vUART_ISR( void )
\r
602 portBASE_TYPE xCRWokenByPost = pdFALSE;
\r
604 // We loop around reading characters until there are none left in the UART.
\r
605 while( UART_RX_REG_NOT_EMPTY() )
\r
607 // Obtain the character from the UART.
\r
608 cRxedChar = UART_RX_REG;
\r
610 // Post the character onto a queue. xCRWokenByPost will be pdFALSE
\r
611 // the first time around the loop. If the post causes a co-routine
\r
612 // to be woken (unblocked) then xCRWokenByPost will be set to pdTRUE.
\r
613 // In this manner we can ensure that if more than one co-routine is
\r
614 // blocked on the queue only one is woken by this ISR no matter how
\r
615 // many characters are posted to the queue.
\r
616 xCRWokenByPost = crQUEUE_SEND_FROM_ISR( xCommsRxQueue, &cRxedChar, xCRWokenByPost );
\r
619 * \defgroup crQUEUE_SEND_FROM_ISR crQUEUE_SEND_FROM_ISR
\r
622 #define crQUEUE_SEND_FROM_ISR( pxQueue, pvItemToQueue, xCoRoutinePreviouslyWoken ) xQueueCRSendFromISR( ( pxQueue ), ( pvItemToQueue ), ( xCoRoutinePreviouslyWoken ) )
\r
628 crQUEUE_SEND_FROM_ISR(
\r
629 xQueueHandle pxQueue,
\r
631 portBASE_TYPE * pxCoRoutineWoken
\r
634 * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
\r
635 * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
\r
636 * functions used by tasks.
\r
638 * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
\r
639 * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
\r
640 * xQueueReceiveFromISR() can only be used to pass data between a task and and
\r
643 * crQUEUE_RECEIVE_FROM_ISR can only be called from an ISR to receive data
\r
644 * from a queue that is being used from within a co-routine (a co-routine
\r
645 * posted to the queue).
\r
647 * See the co-routine section of the WEB documentation for information on
\r
648 * passing data between tasks and co-routines and between ISR's and
\r
651 * @param xQueue The handle to the queue on which the item is to be posted.
\r
653 * @param pvBuffer A pointer to a buffer into which the received item will be
\r
654 * placed. The size of the items the queue will hold was defined when the
\r
655 * queue was created, so this many bytes will be copied from the queue into
\r
658 * @param pxCoRoutineWoken A co-routine may be blocked waiting for space to become
\r
659 * available on the queue. If crQUEUE_RECEIVE_FROM_ISR causes such a
\r
660 * co-routine to unblock *pxCoRoutineWoken will get set to pdTRUE, otherwise
\r
661 * *pxCoRoutineWoken will remain unchanged.
\r
663 * @return pdTRUE an item was successfully received from the queue, otherwise
\r
668 // A co-routine that posts a character to a queue then blocks for a fixed
\r
669 // period. The character is incremented each time.
\r
670 static void vSendingCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
672 // cChar holds its value while this co-routine is blocked and must therefore
\r
673 // be declared static.
\r
674 static char cCharToTx = 'a';
\r
675 portBASE_TYPE xResult;
\r
677 // All co-routines must start with a call to crSTART().
\r
678 crSTART( xHandle );
\r
682 // Send the next character to the queue.
\r
683 crQUEUE_SEND( xHandle, xCoRoutineQueue, &cCharToTx, NO_DELAY, &xResult );
\r
685 if( xResult == pdPASS )
\r
687 // The character was successfully posted to the queue.
\r
691 // Could not post the character to the queue.
\r
694 // Enable the UART Tx interrupt to cause an interrupt in this
\r
695 // hypothetical UART. The interrupt will obtain the character
\r
696 // from the queue and send it.
\r
697 ENABLE_RX_INTERRUPT();
\r
699 // Increment to the next character then block for a fixed period.
\r
700 // cCharToTx will maintain its value across the delay as it is
\r
701 // declared static.
\r
703 if( cCharToTx > 'x' )
\r
710 // All co-routines must end with a call to crEND().
\r
714 // An ISR that uses a queue to receive characters to send on a UART.
\r
715 void vUART_ISR( void )
\r
718 portBASE_TYPE xCRWokenByPost = pdFALSE;
\r
720 while( UART_TX_REG_EMPTY() )
\r
722 // Are there any characters in the queue waiting to be sent?
\r
723 // xCRWokenByPost will automatically be set to pdTRUE if a co-routine
\r
724 // is woken by the post - ensuring that only a single co-routine is
\r
725 // woken no matter how many times we go around this loop.
\r
726 if( crQUEUE_RECEIVE_FROM_ISR( pxQueue, &cCharToTx, &xCRWokenByPost ) )
\r
728 SEND_CHARACTER( cCharToTx );
\r
732 * \defgroup crQUEUE_RECEIVE_FROM_ISR crQUEUE_RECEIVE_FROM_ISR
\r
735 #define crQUEUE_RECEIVE_FROM_ISR( pxQueue, pvBuffer, pxCoRoutineWoken ) xQueueCRReceiveFromISR( ( pxQueue ), ( pvBuffer ), ( pxCoRoutineWoken ) )
\r
738 * This function is intended for internal use by the co-routine macros only.
\r
739 * The macro nature of the co-routine implementation requires that the
\r
740 * prototype appears here. The function should not be used by application
\r
743 * Removes the current co-routine from its ready list and places it in the
\r
744 * appropriate delayed list.
\r
746 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList );
\r
749 * This function is intended for internal use by the queue implementation only.
\r
750 * The function should not be used by application writers.
\r
752 * Removes the highest priority co-routine from the event list and places it in
\r
753 * the pending ready list.
\r
755 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList );
\r
761 #endif /* CO_ROUTINE_H */
\r