2 FreeRTOS V9.0.0rc1 - Copyright (C) 2016 Real Time Engineers Ltd.
\r
5 VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.
\r
7 This file is part of the FreeRTOS distribution.
\r
9 FreeRTOS is free software; you can redistribute it and/or modify it under
\r
10 the terms of the GNU General Public License (version 2) as published by the
\r
11 Free Software Foundation >>>> AND MODIFIED BY <<<< the FreeRTOS exception.
\r
13 ***************************************************************************
\r
14 >>! NOTE: The modification to the GPL is included to allow you to !<<
\r
15 >>! distribute a combined work that includes FreeRTOS without being !<<
\r
16 >>! obliged to provide the source code for proprietary components !<<
\r
17 >>! outside of the FreeRTOS kernel. !<<
\r
18 ***************************************************************************
\r
20 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
21 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
22 FOR A PARTICULAR PURPOSE. Full license text is available on the following
\r
23 link: http://www.freertos.org/a00114.html
\r
25 ***************************************************************************
\r
27 * FreeRTOS provides completely free yet professionally developed, *
\r
28 * robust, strictly quality controlled, supported, and cross *
\r
29 * platform software that is more than just the market leader, it *
\r
30 * is the industry's de facto standard. *
\r
32 * Help yourself get started quickly while simultaneously helping *
\r
33 * to support the FreeRTOS project by purchasing a FreeRTOS *
\r
34 * tutorial book, reference manual, or both: *
\r
35 * http://www.FreeRTOS.org/Documentation *
\r
37 ***************************************************************************
\r
39 http://www.FreeRTOS.org/FAQHelp.html - Having a problem? Start by reading
\r
40 the FAQ page "My application does not run, what could be wrong?". Have you
\r
41 defined configASSERT()?
\r
43 http://www.FreeRTOS.org/support - In return for receiving this top quality
\r
44 embedded software for free we request you assist our global community by
\r
45 participating in the support forum.
\r
47 http://www.FreeRTOS.org/training - Investing in training allows your team to
\r
48 be as productive as possible as early as possible. Now you can receive
\r
49 FreeRTOS training directly from Richard Barry, CEO of Real Time Engineers
\r
50 Ltd, and the world's leading authority on the world's leading RTOS.
\r
52 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
53 including FreeRTOS+Trace - an indispensable productivity tool, a DOS
\r
54 compatible FAT file system, and our tiny thread aware UDP/IP stack.
\r
56 http://www.FreeRTOS.org/labs - Where new FreeRTOS products go to incubate.
\r
57 Come and try FreeRTOS+TCP, our new open source TCP/IP stack for FreeRTOS.
\r
59 http://www.OpenRTOS.com - Real Time Engineers ltd. license FreeRTOS to High
\r
60 Integrity Systems ltd. to sell under the OpenRTOS brand. Low cost OpenRTOS
\r
61 licenses offer ticketed support, indemnification and commercial middleware.
\r
63 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
64 engineered and independently SIL3 certified version for use in safety and
\r
65 mission critical applications that require provable dependability.
\r
70 #ifndef CO_ROUTINE_H
\r
71 #define CO_ROUTINE_H
\r
73 #ifndef INC_FREERTOS_H
\r
74 #error "include FreeRTOS.h must appear in source files before include croutine.h"
\r
83 /* Used to hide the implementation of the co-routine control block. The
\r
84 control block structure however has to be included in the header due to
\r
85 the macro implementation of the co-routine functionality. */
\r
86 typedef void * CoRoutineHandle_t;
\r
88 /* Defines the prototype to which co-routine functions must conform. */
\r
89 typedef void (*crCOROUTINE_CODE)( CoRoutineHandle_t, UBaseType_t );
\r
91 typedef struct corCoRoutineControlBlock
\r
93 crCOROUTINE_CODE pxCoRoutineFunction;
\r
94 ListItem_t xGenericListItem; /*< List item used to place the CRCB in ready and blocked queues. */
\r
95 ListItem_t xEventListItem; /*< List item used to place the CRCB in event lists. */
\r
96 UBaseType_t uxPriority; /*< The priority of the co-routine in relation to other co-routines. */
\r
97 UBaseType_t uxIndex; /*< Used to distinguish between co-routines when multiple co-routines use the same co-routine function. */
\r
98 uint16_t uxState; /*< Used internally by the co-routine implementation. */
\r
99 } CRCB_t; /* Co-routine control block. Note must be identical in size down to uxPriority with TCB_t. */
\r
104 BaseType_t xCoRoutineCreate(
\r
105 crCOROUTINE_CODE pxCoRoutineCode,
\r
106 UBaseType_t uxPriority,
\r
107 UBaseType_t uxIndex
\r
110 * Create a new co-routine and add it to the list of co-routines that are
\r
113 * @param pxCoRoutineCode Pointer to the co-routine function. Co-routine
\r
114 * functions require special syntax - see the co-routine section of the WEB
\r
115 * documentation for more information.
\r
117 * @param uxPriority The priority with respect to other co-routines at which
\r
118 * the co-routine will run.
\r
120 * @param uxIndex Used to distinguish between different co-routines that
\r
121 * execute the same function. See the example below and the co-routine section
\r
122 * of the WEB documentation for further information.
\r
124 * @return pdPASS if the co-routine was successfully created and added to a ready
\r
125 * list, otherwise an error code defined with ProjDefs.h.
\r
129 // Co-routine to be created.
\r
130 void vFlashCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
132 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
133 // This may not be necessary for const variables.
\r
134 static const char cLedToFlash[ 2 ] = { 5, 6 };
\r
135 static const TickType_t uxFlashRates[ 2 ] = { 200, 400 };
\r
137 // Must start every co-routine with a call to crSTART();
\r
138 crSTART( xHandle );
\r
142 // This co-routine just delays for a fixed period, then toggles
\r
143 // an LED. Two co-routines are created using this function, so
\r
144 // the uxIndex parameter is used to tell the co-routine which
\r
145 // LED to flash and how int32_t to delay. This assumes xQueue has
\r
146 // already been created.
\r
147 vParTestToggleLED( cLedToFlash[ uxIndex ] );
\r
148 crDELAY( xHandle, uxFlashRates[ uxIndex ] );
\r
151 // Must end every co-routine with a call to crEND();
\r
155 // Function that creates two co-routines.
\r
156 void vOtherFunction( void )
\r
158 uint8_t ucParameterToPass;
\r
159 TaskHandle_t xHandle;
\r
161 // Create two co-routines at priority 0. The first is given index 0
\r
162 // so (from the code above) toggles LED 5 every 200 ticks. The second
\r
163 // is given index 1 so toggles LED 6 every 400 ticks.
\r
164 for( uxIndex = 0; uxIndex < 2; uxIndex++ )
\r
166 xCoRoutineCreate( vFlashCoRoutine, 0, uxIndex );
\r
170 * \defgroup xCoRoutineCreate xCoRoutineCreate
\r
173 BaseType_t xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, UBaseType_t uxPriority, UBaseType_t uxIndex );
\r
179 void vCoRoutineSchedule( void );</pre>
\r
181 * Run a co-routine.
\r
183 * vCoRoutineSchedule() executes the highest priority co-routine that is able
\r
184 * to run. The co-routine will execute until it either blocks, yields or is
\r
185 * preempted by a task. Co-routines execute cooperatively so one
\r
186 * co-routine cannot be preempted by another, but can be preempted by a task.
\r
188 * If an application comprises of both tasks and co-routines then
\r
189 * vCoRoutineSchedule should be called from the idle task (in an idle task
\r
194 // This idle task hook will schedule a co-routine each time it is called.
\r
195 // The rest of the idle task will execute between co-routine calls.
\r
196 void vApplicationIdleHook( void )
\r
198 vCoRoutineSchedule();
\r
201 // Alternatively, if you do not require any other part of the idle task to
\r
202 // execute, the idle task hook can call vCoRoutineScheduler() within an
\r
204 void vApplicationIdleHook( void )
\r
208 vCoRoutineSchedule();
\r
212 * \defgroup vCoRoutineSchedule vCoRoutineSchedule
\r
215 void vCoRoutineSchedule( void );
\r
220 crSTART( CoRoutineHandle_t xHandle );</pre>
\r
222 * This macro MUST always be called at the start of a co-routine function.
\r
226 // Co-routine to be created.
\r
227 void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
229 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
230 static int32_t ulAVariable;
\r
232 // Must start every co-routine with a call to crSTART();
\r
233 crSTART( xHandle );
\r
237 // Co-routine functionality goes here.
\r
240 // Must end every co-routine with a call to crEND();
\r
243 * \defgroup crSTART crSTART
\r
246 #define crSTART( pxCRCB ) switch( ( ( CRCB_t * )( pxCRCB ) )->uxState ) { case 0:
\r
253 * This macro MUST always be called at the end of a co-routine function.
\r
257 // Co-routine to be created.
\r
258 void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
260 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
261 static int32_t ulAVariable;
\r
263 // Must start every co-routine with a call to crSTART();
\r
264 crSTART( xHandle );
\r
268 // Co-routine functionality goes here.
\r
271 // Must end every co-routine with a call to crEND();
\r
274 * \defgroup crSTART crSTART
\r
280 * These macros are intended for internal use by the co-routine implementation
\r
281 * only. The macros should not be used directly by application writers.
\r
283 #define crSET_STATE0( xHandle ) ( ( CRCB_t * )( xHandle ) )->uxState = (__LINE__ * 2); return; case (__LINE__ * 2):
\r
284 #define crSET_STATE1( xHandle ) ( ( CRCB_t * )( xHandle ) )->uxState = ((__LINE__ * 2)+1); return; case ((__LINE__ * 2)+1):
\r
289 crDELAY( CoRoutineHandle_t xHandle, TickType_t xTicksToDelay );</pre>
\r
291 * Delay a co-routine for a fixed period of time.
\r
293 * crDELAY can only be called from the co-routine function itself - not
\r
294 * from within a function called by the co-routine function. This is because
\r
295 * co-routines do not maintain their own stack.
\r
297 * @param xHandle The handle of the co-routine to delay. This is the xHandle
\r
298 * parameter of the co-routine function.
\r
300 * @param xTickToDelay The number of ticks that the co-routine should delay
\r
301 * for. The actual amount of time this equates to is defined by
\r
302 * configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant portTICK_PERIOD_MS
\r
303 * can be used to convert ticks to milliseconds.
\r
307 // Co-routine to be created.
\r
308 void vACoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
310 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
311 // This may not be necessary for const variables.
\r
312 // We are to delay for 200ms.
\r
313 static const xTickType xDelayTime = 200 / portTICK_PERIOD_MS;
\r
315 // Must start every co-routine with a call to crSTART();
\r
316 crSTART( xHandle );
\r
320 // Delay for 200ms.
\r
321 crDELAY( xHandle, xDelayTime );
\r
323 // Do something here.
\r
326 // Must end every co-routine with a call to crEND();
\r
329 * \defgroup crDELAY crDELAY
\r
332 #define crDELAY( xHandle, xTicksToDelay ) \
\r
333 if( ( xTicksToDelay ) > 0 ) \
\r
335 vCoRoutineAddToDelayedList( ( xTicksToDelay ), NULL ); \
\r
337 crSET_STATE0( ( xHandle ) );
\r
342 CoRoutineHandle_t xHandle,
\r
343 QueueHandle_t pxQueue,
\r
344 void *pvItemToQueue,
\r
345 TickType_t xTicksToWait,
\r
346 BaseType_t *pxResult
\r
349 * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
\r
350 * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
\r
352 * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
\r
353 * xQueueSend() and xQueueReceive() can only be used from tasks.
\r
355 * crQUEUE_SEND can only be called from the co-routine function itself - not
\r
356 * from within a function called by the co-routine function. This is because
\r
357 * co-routines do not maintain their own stack.
\r
359 * See the co-routine section of the WEB documentation for information on
\r
360 * passing data between tasks and co-routines and between ISR's and
\r
363 * @param xHandle The handle of the calling co-routine. This is the xHandle
\r
364 * parameter of the co-routine function.
\r
366 * @param pxQueue The handle of the queue on which the data will be posted.
\r
367 * The handle is obtained as the return value when the queue is created using
\r
368 * the xQueueCreate() API function.
\r
370 * @param pvItemToQueue A pointer to the data being posted onto the queue.
\r
371 * The number of bytes of each queued item is specified when the queue is
\r
372 * created. This number of bytes is copied from pvItemToQueue into the queue
\r
375 * @param xTickToDelay The number of ticks that the co-routine should block
\r
376 * to wait for space to become available on the queue, should space not be
\r
377 * available immediately. The actual amount of time this equates to is defined
\r
378 * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
\r
379 * portTICK_PERIOD_MS can be used to convert ticks to milliseconds (see example
\r
382 * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
\r
383 * data was successfully posted onto the queue, otherwise it will be set to an
\r
384 * error defined within ProjDefs.h.
\r
388 // Co-routine function that blocks for a fixed period then posts a number onto
\r
390 static void prvCoRoutineFlashTask( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
392 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
393 static BaseType_t xNumberToPost = 0;
\r
394 static BaseType_t xResult;
\r
396 // Co-routines must begin with a call to crSTART().
\r
397 crSTART( xHandle );
\r
401 // This assumes the queue has already been created.
\r
402 crQUEUE_SEND( xHandle, xCoRoutineQueue, &xNumberToPost, NO_DELAY, &xResult );
\r
404 if( xResult != pdPASS )
\r
406 // The message was not posted!
\r
409 // Increment the number to be posted onto the queue.
\r
412 // Delay for 100 ticks.
\r
413 crDELAY( xHandle, 100 );
\r
416 // Co-routines must end with a call to crEND().
\r
419 * \defgroup crQUEUE_SEND crQUEUE_SEND
\r
422 #define crQUEUE_SEND( xHandle, pxQueue, pvItemToQueue, xTicksToWait, pxResult ) \
\r
424 *( pxResult ) = xQueueCRSend( ( pxQueue) , ( pvItemToQueue) , ( xTicksToWait ) ); \
\r
425 if( *( pxResult ) == errQUEUE_BLOCKED ) \
\r
427 crSET_STATE0( ( xHandle ) ); \
\r
428 *pxResult = xQueueCRSend( ( pxQueue ), ( pvItemToQueue ), 0 ); \
\r
430 if( *pxResult == errQUEUE_YIELD ) \
\r
432 crSET_STATE1( ( xHandle ) ); \
\r
433 *pxResult = pdPASS; \
\r
441 CoRoutineHandle_t xHandle,
\r
442 QueueHandle_t pxQueue,
\r
444 TickType_t xTicksToWait,
\r
445 BaseType_t *pxResult
\r
448 * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
\r
449 * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
\r
451 * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
\r
452 * xQueueSend() and xQueueReceive() can only be used from tasks.
\r
454 * crQUEUE_RECEIVE can only be called from the co-routine function itself - not
\r
455 * from within a function called by the co-routine function. This is because
\r
456 * co-routines do not maintain their own stack.
\r
458 * See the co-routine section of the WEB documentation for information on
\r
459 * passing data between tasks and co-routines and between ISR's and
\r
462 * @param xHandle The handle of the calling co-routine. This is the xHandle
\r
463 * parameter of the co-routine function.
\r
465 * @param pxQueue The handle of the queue from which the data will be received.
\r
466 * The handle is obtained as the return value when the queue is created using
\r
467 * the xQueueCreate() API function.
\r
469 * @param pvBuffer The buffer into which the received item is to be copied.
\r
470 * The number of bytes of each queued item is specified when the queue is
\r
471 * created. This number of bytes is copied into pvBuffer.
\r
473 * @param xTickToDelay The number of ticks that the co-routine should block
\r
474 * to wait for data to become available from the queue, should data not be
\r
475 * available immediately. The actual amount of time this equates to is defined
\r
476 * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
\r
477 * portTICK_PERIOD_MS can be used to convert ticks to milliseconds (see the
\r
478 * crQUEUE_SEND example).
\r
480 * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
\r
481 * data was successfully retrieved from the queue, otherwise it will be set to
\r
482 * an error code as defined within ProjDefs.h.
\r
486 // A co-routine receives the number of an LED to flash from a queue. It
\r
487 // blocks on the queue until the number is received.
\r
488 static void prvCoRoutineFlashWorkTask( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
490 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
491 static BaseType_t xResult;
\r
492 static UBaseType_t uxLEDToFlash;
\r
494 // All co-routines must start with a call to crSTART().
\r
495 crSTART( xHandle );
\r
499 // Wait for data to become available on the queue.
\r
500 crQUEUE_RECEIVE( xHandle, xCoRoutineQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
\r
502 if( xResult == pdPASS )
\r
504 // We received the LED to flash - flash it!
\r
505 vParTestToggleLED( uxLEDToFlash );
\r
511 * \defgroup crQUEUE_RECEIVE crQUEUE_RECEIVE
\r
514 #define crQUEUE_RECEIVE( xHandle, pxQueue, pvBuffer, xTicksToWait, pxResult ) \
\r
516 *( pxResult ) = xQueueCRReceive( ( pxQueue) , ( pvBuffer ), ( xTicksToWait ) ); \
\r
517 if( *( pxResult ) == errQUEUE_BLOCKED ) \
\r
519 crSET_STATE0( ( xHandle ) ); \
\r
520 *( pxResult ) = xQueueCRReceive( ( pxQueue) , ( pvBuffer ), 0 ); \
\r
522 if( *( pxResult ) == errQUEUE_YIELD ) \
\r
524 crSET_STATE1( ( xHandle ) ); \
\r
525 *( pxResult ) = pdPASS; \
\r
532 crQUEUE_SEND_FROM_ISR(
\r
533 QueueHandle_t pxQueue,
\r
534 void *pvItemToQueue,
\r
535 BaseType_t xCoRoutinePreviouslyWoken
\r
538 * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
\r
539 * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
\r
540 * functions used by tasks.
\r
542 * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
\r
543 * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
\r
544 * xQueueReceiveFromISR() can only be used to pass data between a task and and
\r
547 * crQUEUE_SEND_FROM_ISR can only be called from an ISR to send data to a queue
\r
548 * that is being used from within a co-routine.
\r
550 * See the co-routine section of the WEB documentation for information on
\r
551 * passing data between tasks and co-routines and between ISR's and
\r
554 * @param xQueue The handle to the queue on which the item is to be posted.
\r
556 * @param pvItemToQueue A pointer to the item that is to be placed on the
\r
557 * queue. The size of the items the queue will hold was defined when the
\r
558 * queue was created, so this many bytes will be copied from pvItemToQueue
\r
559 * into the queue storage area.
\r
561 * @param xCoRoutinePreviouslyWoken This is included so an ISR can post onto
\r
562 * the same queue multiple times from a single interrupt. The first call
\r
563 * should always pass in pdFALSE. Subsequent calls should pass in
\r
564 * the value returned from the previous call.
\r
566 * @return pdTRUE if a co-routine was woken by posting onto the queue. This is
\r
567 * used by the ISR to determine if a context switch may be required following
\r
572 // A co-routine that blocks on a queue waiting for characters to be received.
\r
573 static void vReceivingCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
576 BaseType_t xResult;
\r
578 // All co-routines must start with a call to crSTART().
\r
579 crSTART( xHandle );
\r
583 // Wait for data to become available on the queue. This assumes the
\r
584 // queue xCommsRxQueue has already been created!
\r
585 crQUEUE_RECEIVE( xHandle, xCommsRxQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
\r
587 // Was a character received?
\r
588 if( xResult == pdPASS )
\r
590 // Process the character here.
\r
594 // All co-routines must end with a call to crEND().
\r
598 // An ISR that uses a queue to send characters received on a serial port to
\r
600 void vUART_ISR( void )
\r
603 BaseType_t xCRWokenByPost = pdFALSE;
\r
605 // We loop around reading characters until there are none left in the UART.
\r
606 while( UART_RX_REG_NOT_EMPTY() )
\r
608 // Obtain the character from the UART.
\r
609 cRxedChar = UART_RX_REG;
\r
611 // Post the character onto a queue. xCRWokenByPost will be pdFALSE
\r
612 // the first time around the loop. If the post causes a co-routine
\r
613 // to be woken (unblocked) then xCRWokenByPost will be set to pdTRUE.
\r
614 // In this manner we can ensure that if more than one co-routine is
\r
615 // blocked on the queue only one is woken by this ISR no matter how
\r
616 // many characters are posted to the queue.
\r
617 xCRWokenByPost = crQUEUE_SEND_FROM_ISR( xCommsRxQueue, &cRxedChar, xCRWokenByPost );
\r
620 * \defgroup crQUEUE_SEND_FROM_ISR crQUEUE_SEND_FROM_ISR
\r
623 #define crQUEUE_SEND_FROM_ISR( pxQueue, pvItemToQueue, xCoRoutinePreviouslyWoken ) xQueueCRSendFromISR( ( pxQueue ), ( pvItemToQueue ), ( xCoRoutinePreviouslyWoken ) )
\r
629 crQUEUE_SEND_FROM_ISR(
\r
630 QueueHandle_t pxQueue,
\r
632 BaseType_t * pxCoRoutineWoken
\r
635 * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
\r
636 * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
\r
637 * functions used by tasks.
\r
639 * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
\r
640 * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
\r
641 * xQueueReceiveFromISR() can only be used to pass data between a task and and
\r
644 * crQUEUE_RECEIVE_FROM_ISR can only be called from an ISR to receive data
\r
645 * from a queue that is being used from within a co-routine (a co-routine
\r
646 * posted to the queue).
\r
648 * See the co-routine section of the WEB documentation for information on
\r
649 * passing data between tasks and co-routines and between ISR's and
\r
652 * @param xQueue The handle to the queue on which the item is to be posted.
\r
654 * @param pvBuffer A pointer to a buffer into which the received item will be
\r
655 * placed. The size of the items the queue will hold was defined when the
\r
656 * queue was created, so this many bytes will be copied from the queue into
\r
659 * @param pxCoRoutineWoken A co-routine may be blocked waiting for space to become
\r
660 * available on the queue. If crQUEUE_RECEIVE_FROM_ISR causes such a
\r
661 * co-routine to unblock *pxCoRoutineWoken will get set to pdTRUE, otherwise
\r
662 * *pxCoRoutineWoken will remain unchanged.
\r
664 * @return pdTRUE an item was successfully received from the queue, otherwise
\r
669 // A co-routine that posts a character to a queue then blocks for a fixed
\r
670 // period. The character is incremented each time.
\r
671 static void vSendingCoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
\r
673 // cChar holds its value while this co-routine is blocked and must therefore
\r
674 // be declared static.
\r
675 static char cCharToTx = 'a';
\r
676 BaseType_t xResult;
\r
678 // All co-routines must start with a call to crSTART().
\r
679 crSTART( xHandle );
\r
683 // Send the next character to the queue.
\r
684 crQUEUE_SEND( xHandle, xCoRoutineQueue, &cCharToTx, NO_DELAY, &xResult );
\r
686 if( xResult == pdPASS )
\r
688 // The character was successfully posted to the queue.
\r
692 // Could not post the character to the queue.
\r
695 // Enable the UART Tx interrupt to cause an interrupt in this
\r
696 // hypothetical UART. The interrupt will obtain the character
\r
697 // from the queue and send it.
\r
698 ENABLE_RX_INTERRUPT();
\r
700 // Increment to the next character then block for a fixed period.
\r
701 // cCharToTx will maintain its value across the delay as it is
\r
702 // declared static.
\r
704 if( cCharToTx > 'x' )
\r
711 // All co-routines must end with a call to crEND().
\r
715 // An ISR that uses a queue to receive characters to send on a UART.
\r
716 void vUART_ISR( void )
\r
719 BaseType_t xCRWokenByPost = pdFALSE;
\r
721 while( UART_TX_REG_EMPTY() )
\r
723 // Are there any characters in the queue waiting to be sent?
\r
724 // xCRWokenByPost will automatically be set to pdTRUE if a co-routine
\r
725 // is woken by the post - ensuring that only a single co-routine is
\r
726 // woken no matter how many times we go around this loop.
\r
727 if( crQUEUE_RECEIVE_FROM_ISR( pxQueue, &cCharToTx, &xCRWokenByPost ) )
\r
729 SEND_CHARACTER( cCharToTx );
\r
733 * \defgroup crQUEUE_RECEIVE_FROM_ISR crQUEUE_RECEIVE_FROM_ISR
\r
736 #define crQUEUE_RECEIVE_FROM_ISR( pxQueue, pvBuffer, pxCoRoutineWoken ) xQueueCRReceiveFromISR( ( pxQueue ), ( pvBuffer ), ( pxCoRoutineWoken ) )
\r
739 * This function is intended for internal use by the co-routine macros only.
\r
740 * The macro nature of the co-routine implementation requires that the
\r
741 * prototype appears here. The function should not be used by application
\r
744 * Removes the current co-routine from its ready list and places it in the
\r
745 * appropriate delayed list.
\r
747 void vCoRoutineAddToDelayedList( TickType_t xTicksToDelay, List_t *pxEventList );
\r
750 * This function is intended for internal use by the queue implementation only.
\r
751 * The function should not be used by application writers.
\r
753 * Removes the highest priority co-routine from the event list and places it in
\r
754 * the pending ready list.
\r
756 BaseType_t xCoRoutineRemoveFromEventList( const List_t *pxEventList );
\r
762 #endif /* CO_ROUTINE_H */
\r