2 FreeRTOS V7.5.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
4 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
6 ***************************************************************************
\r
8 * FreeRTOS provides completely free yet professionally developed, *
\r
9 * robust, strictly quality controlled, supported, and cross *
\r
10 * platform software that has become a de facto standard. *
\r
12 * Help yourself get started quickly and support the FreeRTOS *
\r
13 * project by purchasing a FreeRTOS tutorial book, reference *
\r
14 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
18 ***************************************************************************
\r
20 This file is part of the FreeRTOS distribution.
\r
22 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
23 the terms of the GNU General Public License (version 2) as published by the
\r
24 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
26 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
27 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
28 >>! the source code for proprietary components outside of the FreeRTOS
\r
31 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
32 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
33 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
34 link: http://www.freertos.org/a00114.html
\r
38 ***************************************************************************
\r
40 * Having a problem? Start by reading the FAQ "My application does *
\r
41 * not run, what could be wrong?" *
\r
43 * http://www.FreeRTOS.org/FAQHelp.html *
\r
45 ***************************************************************************
\r
47 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
48 license and Real Time Engineers Ltd. contact details.
\r
50 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
51 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
52 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
54 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
55 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
56 licenses offer ticketed support, indemnification and middleware.
\r
58 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
59 engineered and independently SIL3 certified version for use in safety and
\r
60 mission critical applications that require provable dependability.
\r
65 #ifndef CO_ROUTINE_H
\r
66 #define CO_ROUTINE_H
\r
68 #ifndef INC_FREERTOS_H
\r
69 #error "include FreeRTOS.h must appear in source files before include croutine.h"
\r
78 /* Used to hide the implementation of the co-routine control block. The
\r
79 control block structure however has to be included in the header due to
\r
80 the macro implementation of the co-routine functionality. */
\r
81 typedef void * xCoRoutineHandle;
\r
83 /* Defines the prototype to which co-routine functions must conform. */
\r
84 typedef void (*crCOROUTINE_CODE)( xCoRoutineHandle, unsigned portBASE_TYPE );
\r
86 typedef struct corCoRoutineControlBlock
\r
88 crCOROUTINE_CODE pxCoRoutineFunction;
\r
89 xListItem xGenericListItem; /*< List item used to place the CRCB in ready and blocked queues. */
\r
90 xListItem xEventListItem; /*< List item used to place the CRCB in event lists. */
\r
91 unsigned portBASE_TYPE uxPriority; /*< The priority of the co-routine in relation to other co-routines. */
\r
92 unsigned portBASE_TYPE uxIndex; /*< Used to distinguish between co-routines when multiple co-routines use the same co-routine function. */
\r
93 unsigned short uxState; /*< Used internally by the co-routine implementation. */
\r
94 } corCRCB; /* Co-routine control block. Note must be identical in size down to uxPriority with tskTCB. */
\r
99 portBASE_TYPE xCoRoutineCreate(
\r
100 crCOROUTINE_CODE pxCoRoutineCode,
\r
101 unsigned portBASE_TYPE uxPriority,
\r
102 unsigned portBASE_TYPE uxIndex
\r
105 * Create a new co-routine and add it to the list of co-routines that are
\r
108 * @param pxCoRoutineCode Pointer to the co-routine function. Co-routine
\r
109 * functions require special syntax - see the co-routine section of the WEB
\r
110 * documentation for more information.
\r
112 * @param uxPriority The priority with respect to other co-routines at which
\r
113 * the co-routine will run.
\r
115 * @param uxIndex Used to distinguish between different co-routines that
\r
116 * execute the same function. See the example below and the co-routine section
\r
117 * of the WEB documentation for further information.
\r
119 * @return pdPASS if the co-routine was successfully created and added to a ready
\r
120 * list, otherwise an error code defined with ProjDefs.h.
\r
124 // Co-routine to be created.
\r
125 void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
127 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
128 // This may not be necessary for const variables.
\r
129 static const char cLedToFlash[ 2 ] = { 5, 6 };
\r
130 static const portTickType uxFlashRates[ 2 ] = { 200, 400 };
\r
132 // Must start every co-routine with a call to crSTART();
\r
133 crSTART( xHandle );
\r
137 // This co-routine just delays for a fixed period, then toggles
\r
138 // an LED. Two co-routines are created using this function, so
\r
139 // the uxIndex parameter is used to tell the co-routine which
\r
140 // LED to flash and how long to delay. This assumes xQueue has
\r
141 // already been created.
\r
142 vParTestToggleLED( cLedToFlash[ uxIndex ] );
\r
143 crDELAY( xHandle, uxFlashRates[ uxIndex ] );
\r
146 // Must end every co-routine with a call to crEND();
\r
150 // Function that creates two co-routines.
\r
151 void vOtherFunction( void )
\r
153 unsigned char ucParameterToPass;
\r
154 xTaskHandle xHandle;
\r
156 // Create two co-routines at priority 0. The first is given index 0
\r
157 // so (from the code above) toggles LED 5 every 200 ticks. The second
\r
158 // is given index 1 so toggles LED 6 every 400 ticks.
\r
159 for( uxIndex = 0; uxIndex < 2; uxIndex++ )
\r
161 xCoRoutineCreate( vFlashCoRoutine, 0, uxIndex );
\r
165 * \defgroup xCoRoutineCreate xCoRoutineCreate
\r
168 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex );
\r
174 void vCoRoutineSchedule( void );</pre>
\r
176 * Run a co-routine.
\r
178 * vCoRoutineSchedule() executes the highest priority co-routine that is able
\r
179 * to run. The co-routine will execute until it either blocks, yields or is
\r
180 * preempted by a task. Co-routines execute cooperatively so one
\r
181 * co-routine cannot be preempted by another, but can be preempted by a task.
\r
183 * If an application comprises of both tasks and co-routines then
\r
184 * vCoRoutineSchedule should be called from the idle task (in an idle task
\r
189 // This idle task hook will schedule a co-routine each time it is called.
\r
190 // The rest of the idle task will execute between co-routine calls.
\r
191 void vApplicationIdleHook( void )
\r
193 vCoRoutineSchedule();
\r
196 // Alternatively, if you do not require any other part of the idle task to
\r
197 // execute, the idle task hook can call vCoRoutineScheduler() within an
\r
199 void vApplicationIdleHook( void )
\r
203 vCoRoutineSchedule();
\r
207 * \defgroup vCoRoutineSchedule vCoRoutineSchedule
\r
210 void vCoRoutineSchedule( void );
\r
215 crSTART( xCoRoutineHandle xHandle );</pre>
\r
217 * This macro MUST always be called at the start of a co-routine function.
\r
221 // Co-routine to be created.
\r
222 void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
224 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
225 static long ulAVariable;
\r
227 // Must start every co-routine with a call to crSTART();
\r
228 crSTART( xHandle );
\r
232 // Co-routine functionality goes here.
\r
235 // Must end every co-routine with a call to crEND();
\r
238 * \defgroup crSTART crSTART
\r
241 #define crSTART( pxCRCB ) switch( ( ( corCRCB * )( pxCRCB ) )->uxState ) { case 0:
\r
248 * This macro MUST always be called at the end of a co-routine function.
\r
252 // Co-routine to be created.
\r
253 void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
255 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
256 static long ulAVariable;
\r
258 // Must start every co-routine with a call to crSTART();
\r
259 crSTART( xHandle );
\r
263 // Co-routine functionality goes here.
\r
266 // Must end every co-routine with a call to crEND();
\r
269 * \defgroup crSTART crSTART
\r
275 * These macros are intended for internal use by the co-routine implementation
\r
276 * only. The macros should not be used directly by application writers.
\r
278 #define crSET_STATE0( xHandle ) ( ( corCRCB * )( xHandle ) )->uxState = (__LINE__ * 2); return; case (__LINE__ * 2):
\r
279 #define crSET_STATE1( xHandle ) ( ( corCRCB * )( xHandle ) )->uxState = ((__LINE__ * 2)+1); return; case ((__LINE__ * 2)+1):
\r
284 crDELAY( xCoRoutineHandle xHandle, portTickType xTicksToDelay );</pre>
\r
286 * Delay a co-routine for a fixed period of time.
\r
288 * crDELAY can only be called from the co-routine function itself - not
\r
289 * from within a function called by the co-routine function. This is because
\r
290 * co-routines do not maintain their own stack.
\r
292 * @param xHandle The handle of the co-routine to delay. This is the xHandle
\r
293 * parameter of the co-routine function.
\r
295 * @param xTickToDelay The number of ticks that the co-routine should delay
\r
296 * for. The actual amount of time this equates to is defined by
\r
297 * configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant portTICK_RATE_MS
\r
298 * can be used to convert ticks to milliseconds.
\r
302 // Co-routine to be created.
\r
303 void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
305 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
306 // This may not be necessary for const variables.
\r
307 // We are to delay for 200ms.
\r
308 static const xTickType xDelayTime = 200 / portTICK_RATE_MS;
\r
310 // Must start every co-routine with a call to crSTART();
\r
311 crSTART( xHandle );
\r
315 // Delay for 200ms.
\r
316 crDELAY( xHandle, xDelayTime );
\r
318 // Do something here.
\r
321 // Must end every co-routine with a call to crEND();
\r
324 * \defgroup crDELAY crDELAY
\r
327 #define crDELAY( xHandle, xTicksToDelay ) \
\r
328 if( ( xTicksToDelay ) > 0 ) \
\r
330 vCoRoutineAddToDelayedList( ( xTicksToDelay ), NULL ); \
\r
332 crSET_STATE0( ( xHandle ) );
\r
337 xCoRoutineHandle xHandle,
\r
338 xQueueHandle pxQueue,
\r
339 void *pvItemToQueue,
\r
340 portTickType xTicksToWait,
\r
341 portBASE_TYPE *pxResult
\r
344 * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
\r
345 * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
\r
347 * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
\r
348 * xQueueSend() and xQueueReceive() can only be used from tasks.
\r
350 * crQUEUE_SEND can only be called from the co-routine function itself - not
\r
351 * from within a function called by the co-routine function. This is because
\r
352 * co-routines do not maintain their own stack.
\r
354 * See the co-routine section of the WEB documentation for information on
\r
355 * passing data between tasks and co-routines and between ISR's and
\r
358 * @param xHandle The handle of the calling co-routine. This is the xHandle
\r
359 * parameter of the co-routine function.
\r
361 * @param pxQueue The handle of the queue on which the data will be posted.
\r
362 * The handle is obtained as the return value when the queue is created using
\r
363 * the xQueueCreate() API function.
\r
365 * @param pvItemToQueue A pointer to the data being posted onto the queue.
\r
366 * The number of bytes of each queued item is specified when the queue is
\r
367 * created. This number of bytes is copied from pvItemToQueue into the queue
\r
370 * @param xTickToDelay The number of ticks that the co-routine should block
\r
371 * to wait for space to become available on the queue, should space not be
\r
372 * available immediately. The actual amount of time this equates to is defined
\r
373 * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
\r
374 * portTICK_RATE_MS can be used to convert ticks to milliseconds (see example
\r
377 * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
\r
378 * data was successfully posted onto the queue, otherwise it will be set to an
\r
379 * error defined within ProjDefs.h.
\r
383 // Co-routine function that blocks for a fixed period then posts a number onto
\r
385 static void prvCoRoutineFlashTask( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
387 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
388 static portBASE_TYPE xNumberToPost = 0;
\r
389 static portBASE_TYPE xResult;
\r
391 // Co-routines must begin with a call to crSTART().
\r
392 crSTART( xHandle );
\r
396 // This assumes the queue has already been created.
\r
397 crQUEUE_SEND( xHandle, xCoRoutineQueue, &xNumberToPost, NO_DELAY, &xResult );
\r
399 if( xResult != pdPASS )
\r
401 // The message was not posted!
\r
404 // Increment the number to be posted onto the queue.
\r
407 // Delay for 100 ticks.
\r
408 crDELAY( xHandle, 100 );
\r
411 // Co-routines must end with a call to crEND().
\r
414 * \defgroup crQUEUE_SEND crQUEUE_SEND
\r
417 #define crQUEUE_SEND( xHandle, pxQueue, pvItemToQueue, xTicksToWait, pxResult ) \
\r
419 *( pxResult ) = xQueueCRSend( ( pxQueue) , ( pvItemToQueue) , ( xTicksToWait ) ); \
\r
420 if( *( pxResult ) == errQUEUE_BLOCKED ) \
\r
422 crSET_STATE0( ( xHandle ) ); \
\r
423 *pxResult = xQueueCRSend( ( pxQueue ), ( pvItemToQueue ), 0 ); \
\r
425 if( *pxResult == errQUEUE_YIELD ) \
\r
427 crSET_STATE1( ( xHandle ) ); \
\r
428 *pxResult = pdPASS; \
\r
436 xCoRoutineHandle xHandle,
\r
437 xQueueHandle pxQueue,
\r
439 portTickType xTicksToWait,
\r
440 portBASE_TYPE *pxResult
\r
443 * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
\r
444 * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
\r
446 * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
\r
447 * xQueueSend() and xQueueReceive() can only be used from tasks.
\r
449 * crQUEUE_RECEIVE can only be called from the co-routine function itself - not
\r
450 * from within a function called by the co-routine function. This is because
\r
451 * co-routines do not maintain their own stack.
\r
453 * See the co-routine section of the WEB documentation for information on
\r
454 * passing data between tasks and co-routines and between ISR's and
\r
457 * @param xHandle The handle of the calling co-routine. This is the xHandle
\r
458 * parameter of the co-routine function.
\r
460 * @param pxQueue The handle of the queue from which the data will be received.
\r
461 * The handle is obtained as the return value when the queue is created using
\r
462 * the xQueueCreate() API function.
\r
464 * @param pvBuffer The buffer into which the received item is to be copied.
\r
465 * The number of bytes of each queued item is specified when the queue is
\r
466 * created. This number of bytes is copied into pvBuffer.
\r
468 * @param xTickToDelay The number of ticks that the co-routine should block
\r
469 * to wait for data to become available from the queue, should data not be
\r
470 * available immediately. The actual amount of time this equates to is defined
\r
471 * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
\r
472 * portTICK_RATE_MS can be used to convert ticks to milliseconds (see the
\r
473 * crQUEUE_SEND example).
\r
475 * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
\r
476 * data was successfully retrieved from the queue, otherwise it will be set to
\r
477 * an error code as defined within ProjDefs.h.
\r
481 // A co-routine receives the number of an LED to flash from a queue. It
\r
482 // blocks on the queue until the number is received.
\r
483 static void prvCoRoutineFlashWorkTask( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
485 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
486 static portBASE_TYPE xResult;
\r
487 static unsigned portBASE_TYPE uxLEDToFlash;
\r
489 // All co-routines must start with a call to crSTART().
\r
490 crSTART( xHandle );
\r
494 // Wait for data to become available on the queue.
\r
495 crQUEUE_RECEIVE( xHandle, xCoRoutineQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
\r
497 if( xResult == pdPASS )
\r
499 // We received the LED to flash - flash it!
\r
500 vParTestToggleLED( uxLEDToFlash );
\r
506 * \defgroup crQUEUE_RECEIVE crQUEUE_RECEIVE
\r
509 #define crQUEUE_RECEIVE( xHandle, pxQueue, pvBuffer, xTicksToWait, pxResult ) \
\r
511 *( pxResult ) = xQueueCRReceive( ( pxQueue) , ( pvBuffer ), ( xTicksToWait ) ); \
\r
512 if( *( pxResult ) == errQUEUE_BLOCKED ) \
\r
514 crSET_STATE0( ( xHandle ) ); \
\r
515 *( pxResult ) = xQueueCRReceive( ( pxQueue) , ( pvBuffer ), 0 ); \
\r
517 if( *( pxResult ) == errQUEUE_YIELD ) \
\r
519 crSET_STATE1( ( xHandle ) ); \
\r
520 *( pxResult ) = pdPASS; \
\r
527 crQUEUE_SEND_FROM_ISR(
\r
528 xQueueHandle pxQueue,
\r
529 void *pvItemToQueue,
\r
530 portBASE_TYPE xCoRoutinePreviouslyWoken
\r
533 * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
\r
534 * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
\r
535 * functions used by tasks.
\r
537 * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
\r
538 * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
\r
539 * xQueueReceiveFromISR() can only be used to pass data between a task and and
\r
542 * crQUEUE_SEND_FROM_ISR can only be called from an ISR to send data to a queue
\r
543 * that is being used from within a co-routine.
\r
545 * See the co-routine section of the WEB documentation for information on
\r
546 * passing data between tasks and co-routines and between ISR's and
\r
549 * @param xQueue The handle to the queue on which the item is to be posted.
\r
551 * @param pvItemToQueue A pointer to the item that is to be placed on the
\r
552 * queue. The size of the items the queue will hold was defined when the
\r
553 * queue was created, so this many bytes will be copied from pvItemToQueue
\r
554 * into the queue storage area.
\r
556 * @param xCoRoutinePreviouslyWoken This is included so an ISR can post onto
\r
557 * the same queue multiple times from a single interrupt. The first call
\r
558 * should always pass in pdFALSE. Subsequent calls should pass in
\r
559 * the value returned from the previous call.
\r
561 * @return pdTRUE if a co-routine was woken by posting onto the queue. This is
\r
562 * used by the ISR to determine if a context switch may be required following
\r
567 // A co-routine that blocks on a queue waiting for characters to be received.
\r
568 static void vReceivingCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
571 portBASE_TYPE xResult;
\r
573 // All co-routines must start with a call to crSTART().
\r
574 crSTART( xHandle );
\r
578 // Wait for data to become available on the queue. This assumes the
\r
579 // queue xCommsRxQueue has already been created!
\r
580 crQUEUE_RECEIVE( xHandle, xCommsRxQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
\r
582 // Was a character received?
\r
583 if( xResult == pdPASS )
\r
585 // Process the character here.
\r
589 // All co-routines must end with a call to crEND().
\r
593 // An ISR that uses a queue to send characters received on a serial port to
\r
595 void vUART_ISR( void )
\r
598 portBASE_TYPE xCRWokenByPost = pdFALSE;
\r
600 // We loop around reading characters until there are none left in the UART.
\r
601 while( UART_RX_REG_NOT_EMPTY() )
\r
603 // Obtain the character from the UART.
\r
604 cRxedChar = UART_RX_REG;
\r
606 // Post the character onto a queue. xCRWokenByPost will be pdFALSE
\r
607 // the first time around the loop. If the post causes a co-routine
\r
608 // to be woken (unblocked) then xCRWokenByPost will be set to pdTRUE.
\r
609 // In this manner we can ensure that if more than one co-routine is
\r
610 // blocked on the queue only one is woken by this ISR no matter how
\r
611 // many characters are posted to the queue.
\r
612 xCRWokenByPost = crQUEUE_SEND_FROM_ISR( xCommsRxQueue, &cRxedChar, xCRWokenByPost );
\r
615 * \defgroup crQUEUE_SEND_FROM_ISR crQUEUE_SEND_FROM_ISR
\r
618 #define crQUEUE_SEND_FROM_ISR( pxQueue, pvItemToQueue, xCoRoutinePreviouslyWoken ) xQueueCRSendFromISR( ( pxQueue ), ( pvItemToQueue ), ( xCoRoutinePreviouslyWoken ) )
\r
624 crQUEUE_SEND_FROM_ISR(
\r
625 xQueueHandle pxQueue,
\r
627 portBASE_TYPE * pxCoRoutineWoken
\r
630 * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
\r
631 * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
\r
632 * functions used by tasks.
\r
634 * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
\r
635 * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
\r
636 * xQueueReceiveFromISR() can only be used to pass data between a task and and
\r
639 * crQUEUE_RECEIVE_FROM_ISR can only be called from an ISR to receive data
\r
640 * from a queue that is being used from within a co-routine (a co-routine
\r
641 * posted to the queue).
\r
643 * See the co-routine section of the WEB documentation for information on
\r
644 * passing data between tasks and co-routines and between ISR's and
\r
647 * @param xQueue The handle to the queue on which the item is to be posted.
\r
649 * @param pvBuffer A pointer to a buffer into which the received item will be
\r
650 * placed. The size of the items the queue will hold was defined when the
\r
651 * queue was created, so this many bytes will be copied from the queue into
\r
654 * @param pxCoRoutineWoken A co-routine may be blocked waiting for space to become
\r
655 * available on the queue. If crQUEUE_RECEIVE_FROM_ISR causes such a
\r
656 * co-routine to unblock *pxCoRoutineWoken will get set to pdTRUE, otherwise
\r
657 * *pxCoRoutineWoken will remain unchanged.
\r
659 * @return pdTRUE an item was successfully received from the queue, otherwise
\r
664 // A co-routine that posts a character to a queue then blocks for a fixed
\r
665 // period. The character is incremented each time.
\r
666 static void vSendingCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
668 // cChar holds its value while this co-routine is blocked and must therefore
\r
669 // be declared static.
\r
670 static char cCharToTx = 'a';
\r
671 portBASE_TYPE xResult;
\r
673 // All co-routines must start with a call to crSTART().
\r
674 crSTART( xHandle );
\r
678 // Send the next character to the queue.
\r
679 crQUEUE_SEND( xHandle, xCoRoutineQueue, &cCharToTx, NO_DELAY, &xResult );
\r
681 if( xResult == pdPASS )
\r
683 // The character was successfully posted to the queue.
\r
687 // Could not post the character to the queue.
\r
690 // Enable the UART Tx interrupt to cause an interrupt in this
\r
691 // hypothetical UART. The interrupt will obtain the character
\r
692 // from the queue and send it.
\r
693 ENABLE_RX_INTERRUPT();
\r
695 // Increment to the next character then block for a fixed period.
\r
696 // cCharToTx will maintain its value across the delay as it is
\r
697 // declared static.
\r
699 if( cCharToTx > 'x' )
\r
706 // All co-routines must end with a call to crEND().
\r
710 // An ISR that uses a queue to receive characters to send on a UART.
\r
711 void vUART_ISR( void )
\r
714 portBASE_TYPE xCRWokenByPost = pdFALSE;
\r
716 while( UART_TX_REG_EMPTY() )
\r
718 // Are there any characters in the queue waiting to be sent?
\r
719 // xCRWokenByPost will automatically be set to pdTRUE if a co-routine
\r
720 // is woken by the post - ensuring that only a single co-routine is
\r
721 // woken no matter how many times we go around this loop.
\r
722 if( crQUEUE_RECEIVE_FROM_ISR( pxQueue, &cCharToTx, &xCRWokenByPost ) )
\r
724 SEND_CHARACTER( cCharToTx );
\r
728 * \defgroup crQUEUE_RECEIVE_FROM_ISR crQUEUE_RECEIVE_FROM_ISR
\r
731 #define crQUEUE_RECEIVE_FROM_ISR( pxQueue, pvBuffer, pxCoRoutineWoken ) xQueueCRReceiveFromISR( ( pxQueue ), ( pvBuffer ), ( pxCoRoutineWoken ) )
\r
734 * This function is intended for internal use by the co-routine macros only.
\r
735 * The macro nature of the co-routine implementation requires that the
\r
736 * prototype appears here. The function should not be used by application
\r
739 * Removes the current co-routine from its ready list and places it in the
\r
740 * appropriate delayed list.
\r
742 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList );
\r
745 * This function is intended for internal use by the queue implementation only.
\r
746 * The function should not be used by application writers.
\r
748 * Removes the highest priority co-routine from the event list and places it in
\r
749 * the pending ready list.
\r
751 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList );
\r
757 #endif /* CO_ROUTINE_H */
\r