2 FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 ***************************************************************************
\r
9 * FreeRTOS provides completely free yet professionally developed, *
\r
10 * robust, strictly quality controlled, supported, and cross *
\r
11 * platform software that has become a de facto standard. *
\r
13 * Help yourself get started quickly and support the FreeRTOS *
\r
14 * project by purchasing a FreeRTOS tutorial book, reference *
\r
15 * manual, or both from: http://www.FreeRTOS.org/Documentation *
\r
19 ***************************************************************************
\r
21 This file is part of the FreeRTOS distribution.
\r
23 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
24 the terms of the GNU General Public License (version 2) as published by the
\r
25 Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.
\r
27 >>! NOTE: The modification to the GPL is included to allow you to distribute
\r
28 >>! a combined work that includes FreeRTOS without being obliged to provide
\r
29 >>! the source code for proprietary components outside of the FreeRTOS
\r
32 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
33 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
34 FOR A PARTICULAR PURPOSE. Full license text is available from the following
\r
35 link: http://www.freertos.org/a00114.html
\r
39 ***************************************************************************
\r
41 * Having a problem? Start by reading the FAQ "My application does *
\r
42 * not run, what could be wrong?" *
\r
44 * http://www.FreeRTOS.org/FAQHelp.html *
\r
46 ***************************************************************************
\r
48 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
49 license and Real Time Engineers Ltd. contact details.
\r
51 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
52 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
53 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
55 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
56 Integrity Systems to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
57 licenses offer ticketed support, indemnification and middleware.
\r
59 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
60 engineered and independently SIL3 certified version for use in safety and
\r
61 mission critical applications that require provable dependability.
\r
66 #ifndef CO_ROUTINE_H
\r
67 #define CO_ROUTINE_H
\r
69 #ifndef INC_FREERTOS_H
\r
70 #error "include FreeRTOS.h must appear in source files before include croutine.h"
\r
79 /* Used to hide the implementation of the co-routine control block. The
\r
80 control block structure however has to be included in the header due to
\r
81 the macro implementation of the co-routine functionality. */
\r
82 typedef void * CoRoutineHandle_t;
\r
84 /* Defines the prototype to which co-routine functions must conform. */
\r
85 typedef void (*crCOROUTINE_CODE)( CoRoutineHandle_t, UBaseType_t );
\r
87 typedef struct corCoRoutineControlBlock
\r
89 crCOROUTINE_CODE pxCoRoutineFunction;
\r
90 ListItem_t xGenericListItem; /*< List item used to place the CRCB in ready and blocked queues. */
\r
91 ListItem_t xEventListItem; /*< List item used to place the CRCB in event lists. */
\r
92 UBaseType_t uxPriority; /*< The priority of the co-routine in relation to other co-routines. */
\r
93 UBaseType_t uxIndex; /*< Used to distinguish between co-routines when multiple co-routines use the same co-routine function. */
\r
94 uint16_t uxState; /*< Used internally by the co-routine implementation. */
\r
95 } CRCB_t; /* Co-routine control block. Note must be identical in size down to uxPriority with TCB_t. */
\r
100 BaseType_t xCoRoutineCreate(
\r
101 crCOROUTINE_CODE pxCoRoutineCode,
\r
102 UBaseType_t uxPriority,
\r
103 UBaseType_t uxIndex
\r
106 * Create a new co-routine and add it to the list of co-routines that are
\r
109 * @param pxCoRoutineCode Pointer to the co-routine function. Co-routine
\r
110 * functions require special syntax - see the co-routine section of the WEB
\r
111 * documentation for more information.
\r
113 * @param uxPriority The priority with respect to other co-routines at which
\r
114 * the co-routine will run.
\r
116 * @param uxIndex Used to distinguish between different co-routines that
\r
117 * execute the same function. See the example below and the co-routine section
\r
118 * of the WEB documentation for further information.
\r
120 * @return pdPASS if the co-routine was successfully created and added to a ready
\r
121 * list, otherwise an error code defined with ProjDefs.h.
\r
125 // Co-routine to be created.
\r
126 void vFlashCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
128 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
129 // This may not be necessary for const variables.
\r
130 static const char cLedToFlash[ 2 ] = { 5, 6 };
\r
131 static const TickType_t uxFlashRates[ 2 ] = { 200, 400 };
\r
133 // Must start every co-routine with a call to crSTART();
\r
134 crSTART( xHandle );
\r
138 // This co-routine just delays for a fixed period, then toggles
\r
139 // an LED. Two co-routines are created using this function, so
\r
140 // the uxIndex parameter is used to tell the co-routine which
\r
141 // LED to flash and how int32_t to delay. This assumes xQueue has
\r
142 // already been created.
\r
143 vParTestToggleLED( cLedToFlash[ uxIndex ] );
\r
144 crDELAY( xHandle, uxFlashRates[ uxIndex ] );
\r
147 // Must end every co-routine with a call to crEND();
\r
151 // Function that creates two co-routines.
\r
152 void vOtherFunction( void )
\r
154 uint8_t ucParameterToPass;
\r
155 TaskHandle_t xHandle;
\r
157 // Create two co-routines at priority 0. The first is given index 0
\r
158 // so (from the code above) toggles LED 5 every 200 ticks. The second
\r
159 // is given index 1 so toggles LED 6 every 400 ticks.
\r
160 for( uxIndex = 0; uxIndex < 2; uxIndex++ )
\r
162 xCoRoutineCreate( vFlashCoRoutine, 0, uxIndex );
\r
166 * \defgroup xCoRoutineCreate xCoRoutineCreate
\r
169 BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex );
\r
175 void vCoRoutineSchedule( void );</pre>
\r
177 * Run a co-routine.
\r
179 * vCoRoutineSchedule() executes the highest priority co-routine that is able
\r
180 * to run. The co-routine will execute until it either blocks, yields or is
\r
181 * preempted by a task. Co-routines execute cooperatively so one
\r
182 * co-routine cannot be preempted by another, but can be preempted by a task.
\r
184 * If an application comprises of both tasks and co-routines then
\r
185 * vCoRoutineSchedule should be called from the idle task (in an idle task
\r
190 // This idle task hook will schedule a co-routine each time it is called.
\r
191 // The rest of the idle task will execute between co-routine calls.
\r
192 void vApplicationIdleHook( void )
\r
194 vCoRoutineSchedule();
\r
197 // Alternatively, if you do not require any other part of the idle task to
\r
198 // execute, the idle task hook can call vCoRoutineScheduler() within an
\r
200 void vApplicationIdleHook( void )
\r
204 vCoRoutineSchedule();
\r
208 * \defgroup vCoRoutineSchedule vCoRoutineSchedule
\r
211 void vCoRoutineSchedule( void );
\r
216 crSTART( CoRoutineHandle_t xHandle );</pre>
\r
218 * This macro MUST always be called at the start of a co-routine function.
\r
222 // Co-routine to be created.
\r
223 void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
225 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
226 static int32_t ulAVariable;
\r
228 // Must start every co-routine with a call to crSTART();
\r
229 crSTART( xHandle );
\r
233 // Co-routine functionality goes here.
\r
236 // Must end every co-routine with a call to crEND();
\r
239 * \defgroup crSTART crSTART
\r
242 #define crSTART( pxCRCB ) switch( ( ( CRCB_t * )( pxCRCB ) )->uxState ) { case 0:
\r
249 * This macro MUST always be called at the end of a co-routine function.
\r
253 // Co-routine to be created.
\r
254 void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
256 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
257 static int32_t ulAVariable;
\r
259 // Must start every co-routine with a call to crSTART();
\r
260 crSTART( xHandle );
\r
264 // Co-routine functionality goes here.
\r
267 // Must end every co-routine with a call to crEND();
\r
270 * \defgroup crSTART crSTART
\r
276 * These macros are intended for internal use by the co-routine implementation
\r
277 * only. The macros should not be used directly by application writers.
\r
279 #define crSET_STATE0( xHandle ) ( ( CRCB_t * )( xHandle ) )->uxState = (__LINE__ * 2); return; case (__LINE__ * 2):
\r
280 #define crSET_STATE1( xHandle ) ( ( CRCB_t * )( xHandle ) )->uxState = ((__LINE__ * 2)+1); return; case ((__LINE__ * 2)+1):
\r
285 crDELAY( CoRoutineHandle_t xHandle, TickType_t xTicksToDelay );</pre>
\r
287 * Delay a co-routine for a fixed period of time.
\r
289 * crDELAY can only be called from the co-routine function itself - not
\r
290 * from within a function called by the co-routine function. This is because
\r
291 * co-routines do not maintain their own stack.
\r
293 * @param xHandle The handle of the co-routine to delay. This is the xHandle
\r
294 * parameter of the co-routine function.
\r
296 * @param xTickToDelay The number of ticks that the co-routine should delay
\r
297 * for. The actual amount of time this equates to is defined by
\r
298 * configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant portTICK_RATE_MS
\r
299 * can be used to convert ticks to milliseconds.
\r
303 // Co-routine to be created.
\r
304 void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
306 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
307 // This may not be necessary for const variables.
\r
308 // We are to delay for 200ms.
\r
309 static const xTickType xDelayTime = 200 / portTICK_RATE_MS;
\r
311 // Must start every co-routine with a call to crSTART();
\r
312 crSTART( xHandle );
\r
316 // Delay for 200ms.
\r
317 crDELAY( xHandle, xDelayTime );
\r
319 // Do something here.
\r
322 // Must end every co-routine with a call to crEND();
\r
325 * \defgroup crDELAY crDELAY
\r
328 #define crDELAY( xHandle, xTicksToDelay ) \
\r
329 if( ( xTicksToDelay ) > 0 ) \
\r
331 vCoRoutineAddToDelayedList( ( xTicksToDelay ), NULL ); \
\r
333 crSET_STATE0( ( xHandle ) );
\r
338 CoRoutineHandle_t xHandle,
\r
339 QueueHandle_t pxQueue,
\r
340 void *pvItemToQueue,
\r
341 TickType_t xTicksToWait,
\r
342 BaseType_t *pxResult
\r
345 * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
\r
346 * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
\r
348 * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
\r
349 * xQueueSend() and xQueueReceive() can only be used from tasks.
\r
351 * crQUEUE_SEND can only be called from the co-routine function itself - not
\r
352 * from within a function called by the co-routine function. This is because
\r
353 * co-routines do not maintain their own stack.
\r
355 * See the co-routine section of the WEB documentation for information on
\r
356 * passing data between tasks and co-routines and between ISR's and
\r
359 * @param xHandle The handle of the calling co-routine. This is the xHandle
\r
360 * parameter of the co-routine function.
\r
362 * @param pxQueue The handle of the queue on which the data will be posted.
\r
363 * The handle is obtained as the return value when the queue is created using
\r
364 * the xQueueCreate() API function.
\r
366 * @param pvItemToQueue A pointer to the data being posted onto the queue.
\r
367 * The number of bytes of each queued item is specified when the queue is
\r
368 * created. This number of bytes is copied from pvItemToQueue into the queue
\r
371 * @param xTickToDelay The number of ticks that the co-routine should block
\r
372 * to wait for space to become available on the queue, should space not be
\r
373 * available immediately. The actual amount of time this equates to is defined
\r
374 * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
\r
375 * portTICK_RATE_MS can be used to convert ticks to milliseconds (see example
\r
378 * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
\r
379 * data was successfully posted onto the queue, otherwise it will be set to an
\r
380 * error defined within ProjDefs.h.
\r
384 // Co-routine function that blocks for a fixed period then posts a number onto
\r
386 static void prvCoRoutineFlashTask( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
388 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
389 static BaseType_t xNumberToPost = 0;
\r
390 static BaseType_t xResult;
\r
392 // Co-routines must begin with a call to crSTART().
\r
393 crSTART( xHandle );
\r
397 // This assumes the queue has already been created.
\r
398 crQUEUE_SEND( xHandle, xCoRoutineQueue, &xNumberToPost, NO_DELAY, &xResult );
\r
400 if( xResult != pdPASS )
\r
402 // The message was not posted!
\r
405 // Increment the number to be posted onto the queue.
\r
408 // Delay for 100 ticks.
\r
409 crDELAY( xHandle, 100 );
\r
412 // Co-routines must end with a call to crEND().
\r
415 * \defgroup crQUEUE_SEND crQUEUE_SEND
\r
418 #define crQUEUE_SEND( xHandle, pxQueue, pvItemToQueue, xTicksToWait, pxResult ) \
\r
420 *( pxResult ) = xQueueCRSend( ( pxQueue) , ( pvItemToQueue) , ( xTicksToWait ) ); \
\r
421 if( *( pxResult ) == errQUEUE_BLOCKED ) \
\r
423 crSET_STATE0( ( xHandle ) ); \
\r
424 *pxResult = xQueueCRSend( ( pxQueue ), ( pvItemToQueue ), 0 ); \
\r
426 if( *pxResult == errQUEUE_YIELD ) \
\r
428 crSET_STATE1( ( xHandle ) ); \
\r
429 *pxResult = pdPASS; \
\r
437 CoRoutineHandle_t xHandle,
\r
438 QueueHandle_t pxQueue,
\r
440 TickType_t xTicksToWait,
\r
441 BaseType_t *pxResult
\r
444 * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
\r
445 * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
\r
447 * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
\r
448 * xQueueSend() and xQueueReceive() can only be used from tasks.
\r
450 * crQUEUE_RECEIVE can only be called from the co-routine function itself - not
\r
451 * from within a function called by the co-routine function. This is because
\r
452 * co-routines do not maintain their own stack.
\r
454 * See the co-routine section of the WEB documentation for information on
\r
455 * passing data between tasks and co-routines and between ISR's and
\r
458 * @param xHandle The handle of the calling co-routine. This is the xHandle
\r
459 * parameter of the co-routine function.
\r
461 * @param pxQueue The handle of the queue from which the data will be received.
\r
462 * The handle is obtained as the return value when the queue is created using
\r
463 * the xQueueCreate() API function.
\r
465 * @param pvBuffer The buffer into which the received item is to be copied.
\r
466 * The number of bytes of each queued item is specified when the queue is
\r
467 * created. This number of bytes is copied into pvBuffer.
\r
469 * @param xTickToDelay The number of ticks that the co-routine should block
\r
470 * to wait for data to become available from the queue, should data not be
\r
471 * available immediately. The actual amount of time this equates to is defined
\r
472 * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
\r
473 * portTICK_RATE_MS can be used to convert ticks to milliseconds (see the
\r
474 * crQUEUE_SEND example).
\r
476 * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
\r
477 * data was successfully retrieved from the queue, otherwise it will be set to
\r
478 * an error code as defined within ProjDefs.h.
\r
482 // A co-routine receives the number of an LED to flash from a queue. It
\r
483 // blocks on the queue until the number is received.
\r
484 static void prvCoRoutineFlashWorkTask( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
486 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
487 static BaseType_t xResult;
\r
488 static UBaseType_t uxLEDToFlash;
\r
490 // All co-routines must start with a call to crSTART().
\r
491 crSTART( xHandle );
\r
495 // Wait for data to become available on the queue.
\r
496 crQUEUE_RECEIVE( xHandle, xCoRoutineQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
\r
498 if( xResult == pdPASS )
\r
500 // We received the LED to flash - flash it!
\r
501 vParTestToggleLED( uxLEDToFlash );
\r
507 * \defgroup crQUEUE_RECEIVE crQUEUE_RECEIVE
\r
510 #define crQUEUE_RECEIVE( xHandle, pxQueue, pvBuffer, xTicksToWait, pxResult ) \
\r
512 *( pxResult ) = xQueueCRReceive( ( pxQueue) , ( pvBuffer ), ( xTicksToWait ) ); \
\r
513 if( *( pxResult ) == errQUEUE_BLOCKED ) \
\r
515 crSET_STATE0( ( xHandle ) ); \
\r
516 *( pxResult ) = xQueueCRReceive( ( pxQueue) , ( pvBuffer ), 0 ); \
\r
518 if( *( pxResult ) == errQUEUE_YIELD ) \
\r
520 crSET_STATE1( ( xHandle ) ); \
\r
521 *( pxResult ) = pdPASS; \
\r
528 crQUEUE_SEND_FROM_ISR(
\r
529 QueueHandle_t pxQueue,
\r
530 void *pvItemToQueue,
\r
531 BaseType_t xCoRoutinePreviouslyWoken
\r
534 * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
\r
535 * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
\r
536 * functions used by tasks.
\r
538 * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
\r
539 * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
\r
540 * xQueueReceiveFromISR() can only be used to pass data between a task and and
\r
543 * crQUEUE_SEND_FROM_ISR can only be called from an ISR to send data to a queue
\r
544 * that is being used from within a co-routine.
\r
546 * See the co-routine section of the WEB documentation for information on
\r
547 * passing data between tasks and co-routines and between ISR's and
\r
550 * @param xQueue The handle to the queue on which the item is to be posted.
\r
552 * @param pvItemToQueue A pointer to the item that is to be placed on the
\r
553 * queue. The size of the items the queue will hold was defined when the
\r
554 * queue was created, so this many bytes will be copied from pvItemToQueue
\r
555 * into the queue storage area.
\r
557 * @param xCoRoutinePreviouslyWoken This is included so an ISR can post onto
\r
558 * the same queue multiple times from a single interrupt. The first call
\r
559 * should always pass in pdFALSE. Subsequent calls should pass in
\r
560 * the value returned from the previous call.
\r
562 * @return pdTRUE if a co-routine was woken by posting onto the queue. This is
\r
563 * used by the ISR to determine if a context switch may be required following
\r
568 // A co-routine that blocks on a queue waiting for characters to be received.
\r
569 static void vReceivingCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
572 BaseType_t xResult;
\r
574 // All co-routines must start with a call to crSTART().
\r
575 crSTART( xHandle );
\r
579 // Wait for data to become available on the queue. This assumes the
\r
580 // queue xCommsRxQueue has already been created!
\r
581 crQUEUE_RECEIVE( xHandle, xCommsRxQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
\r
583 // Was a character received?
\r
584 if( xResult == pdPASS )
\r
586 // Process the character here.
\r
590 // All co-routines must end with a call to crEND().
\r
594 // An ISR that uses a queue to send characters received on a serial port to
\r
596 void vUART_ISR( void )
\r
599 BaseType_t xCRWokenByPost = pdFALSE;
\r
601 // We loop around reading characters until there are none left in the UART.
\r
602 while( UART_RX_REG_NOT_EMPTY() )
\r
604 // Obtain the character from the UART.
\r
605 cRxedChar = UART_RX_REG;
\r
607 // Post the character onto a queue. xCRWokenByPost will be pdFALSE
\r
608 // the first time around the loop. If the post causes a co-routine
\r
609 // to be woken (unblocked) then xCRWokenByPost will be set to pdTRUE.
\r
610 // In this manner we can ensure that if more than one co-routine is
\r
611 // blocked on the queue only one is woken by this ISR no matter how
\r
612 // many characters are posted to the queue.
\r
613 xCRWokenByPost = crQUEUE_SEND_FROM_ISR( xCommsRxQueue, &cRxedChar, xCRWokenByPost );
\r
616 * \defgroup crQUEUE_SEND_FROM_ISR crQUEUE_SEND_FROM_ISR
\r
619 #define crQUEUE_SEND_FROM_ISR( pxQueue, pvItemToQueue, xCoRoutinePreviouslyWoken ) xQueueCRSendFromISR( ( pxQueue ), ( pvItemToQueue ), ( xCoRoutinePreviouslyWoken ) )
\r
625 crQUEUE_SEND_FROM_ISR(
\r
626 QueueHandle_t pxQueue,
\r
628 BaseType_t * pxCoRoutineWoken
\r
631 * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
\r
632 * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
\r
633 * functions used by tasks.
\r
635 * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
\r
636 * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
\r
637 * xQueueReceiveFromISR() can only be used to pass data between a task and and
\r
640 * crQUEUE_RECEIVE_FROM_ISR can only be called from an ISR to receive data
\r
641 * from a queue that is being used from within a co-routine (a co-routine
\r
642 * posted to the queue).
\r
644 * See the co-routine section of the WEB documentation for information on
\r
645 * passing data between tasks and co-routines and between ISR's and
\r
648 * @param xQueue The handle to the queue on which the item is to be posted.
\r
650 * @param pvBuffer A pointer to a buffer into which the received item will be
\r
651 * placed. The size of the items the queue will hold was defined when the
\r
652 * queue was created, so this many bytes will be copied from the queue into
\r
655 * @param pxCoRoutineWoken A co-routine may be blocked waiting for space to become
\r
656 * available on the queue. If crQUEUE_RECEIVE_FROM_ISR causes such a
\r
657 * co-routine to unblock *pxCoRoutineWoken will get set to pdTRUE, otherwise
\r
658 * *pxCoRoutineWoken will remain unchanged.
\r
660 * @return pdTRUE an item was successfully received from the queue, otherwise
\r
665 // A co-routine that posts a character to a queue then blocks for a fixed
\r
666 // period. The character is incremented each time.
\r
667 static void vSendingCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
669 // cChar holds its value while this co-routine is blocked and must therefore
\r
670 // be declared static.
\r
671 static char cCharToTx = 'a';
\r
672 BaseType_t xResult;
\r
674 // All co-routines must start with a call to crSTART().
\r
675 crSTART( xHandle );
\r
679 // Send the next character to the queue.
\r
680 crQUEUE_SEND( xHandle, xCoRoutineQueue, &cCharToTx, NO_DELAY, &xResult );
\r
682 if( xResult == pdPASS )
\r
684 // The character was successfully posted to the queue.
\r
688 // Could not post the character to the queue.
\r
691 // Enable the UART Tx interrupt to cause an interrupt in this
\r
692 // hypothetical UART. The interrupt will obtain the character
\r
693 // from the queue and send it.
\r
694 ENABLE_RX_INTERRUPT();
\r
696 // Increment to the next character then block for a fixed period.
\r
697 // cCharToTx will maintain its value across the delay as it is
\r
698 // declared static.
\r
700 if( cCharToTx > 'x' )
\r
707 // All co-routines must end with a call to crEND().
\r
711 // An ISR that uses a queue to receive characters to send on a UART.
\r
712 void vUART_ISR( void )
\r
715 BaseType_t xCRWokenByPost = pdFALSE;
\r
717 while( UART_TX_REG_EMPTY() )
\r
719 // Are there any characters in the queue waiting to be sent?
\r
720 // xCRWokenByPost will automatically be set to pdTRUE if a co-routine
\r
721 // is woken by the post - ensuring that only a single co-routine is
\r
722 // woken no matter how many times we go around this loop.
\r
723 if( crQUEUE_RECEIVE_FROM_ISR( pxQueue, &cCharToTx, &xCRWokenByPost ) )
\r
725 SEND_CHARACTER( cCharToTx );
\r
729 * \defgroup crQUEUE_RECEIVE_FROM_ISR crQUEUE_RECEIVE_FROM_ISR
\r
732 #define crQUEUE_RECEIVE_FROM_ISR( pxQueue, pvBuffer, pxCoRoutineWoken ) xQueueCRReceiveFromISR( ( pxQueue ), ( pvBuffer ), ( pxCoRoutineWoken ) )
\r
735 * This function is intended for internal use by the co-routine macros only.
\r
736 * The macro nature of the co-routine implementation requires that the
\r
737 * prototype appears here. The function should not be used by application
\r
740 * Removes the current co-routine from its ready list and places it in the
\r
741 * appropriate delayed list.
\r
743 void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay, List_t *pxEventList );
\r
746 * This function is intended for internal use by the queue implementation only.
\r
747 * The function should not be used by application writers.
\r
749 * Removes the highest priority co-routine from the event list and places it in
\r
750 * the pending ready list.
\r
752 BaseType_t xCoRoutineRemoveFromEventList( const List_t *pxEventList );
\r
758 #endif /* CO_ROUTINE_H */
\r