2 FreeRTOS V7.4.2 - Copyright (C) 2013 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
33 >>>>>>NOTE<<<<<< The modification to the GPL is included to allow you to
\r
34 distribute a combined work that includes FreeRTOS without being obliged to
\r
35 provide the source code for proprietary components outside of the FreeRTOS
\r
38 FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY
\r
39 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
\r
40 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
\r
41 details. You should have received a copy of the GNU General Public License
\r
42 and the FreeRTOS license exception along with FreeRTOS; if not it can be
\r
43 viewed here: http://www.freertos.org/a00114.html and also obtained by
\r
44 writing to Real Time Engineers Ltd., contact details for whom are available
\r
45 on the FreeRTOS WEB site.
\r
49 ***************************************************************************
\r
51 * Having a problem? Start by reading the FAQ "My application does *
\r
52 * not run, what could be wrong?" *
\r
54 * http://www.FreeRTOS.org/FAQHelp.html *
\r
56 ***************************************************************************
\r
59 http://www.FreeRTOS.org - Documentation, books, training, latest versions,
\r
60 license and Real Time Engineers Ltd. contact details.
\r
62 http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,
\r
63 including FreeRTOS+Trace - an indispensable productivity tool, and our new
\r
64 fully thread aware and reentrant UDP/IP stack.
\r
66 http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High
\r
67 Integrity Systems, who sell the code with commercial support,
\r
68 indemnification and middleware, under the OpenRTOS brand.
\r
70 http://www.SafeRTOS.com - High Integrity Systems also provide a safety
\r
71 engineered and independently SIL3 certified version for use in safety and
\r
72 mission critical applications that require provable dependability.
\r
75 #ifndef CO_ROUTINE_H
\r
76 #define CO_ROUTINE_H
\r
78 #ifndef INC_FREERTOS_H
\r
79 #error "include FreeRTOS.h must appear in source files before include croutine.h"
\r
88 /* Used to hide the implementation of the co-routine control block. The
\r
89 control block structure however has to be included in the header due to
\r
90 the macro implementation of the co-routine functionality. */
\r
91 typedef void * xCoRoutineHandle;
\r
93 /* Defines the prototype to which co-routine functions must conform. */
\r
94 typedef void (*crCOROUTINE_CODE)( xCoRoutineHandle, unsigned portBASE_TYPE );
\r
96 typedef struct corCoRoutineControlBlock
\r
98 crCOROUTINE_CODE pxCoRoutineFunction;
\r
99 xListItem xGenericListItem; /*< List item used to place the CRCB in ready and blocked queues. */
\r
100 xListItem xEventListItem; /*< List item used to place the CRCB in event lists. */
\r
101 unsigned portBASE_TYPE uxPriority; /*< The priority of the co-routine in relation to other co-routines. */
\r
102 unsigned portBASE_TYPE uxIndex; /*< Used to distinguish between co-routines when multiple co-routines use the same co-routine function. */
\r
103 unsigned short uxState; /*< Used internally by the co-routine implementation. */
\r
104 } corCRCB; /* Co-routine control block. Note must be identical in size down to uxPriority with tskTCB. */
\r
109 portBASE_TYPE xCoRoutineCreate(
\r
110 crCOROUTINE_CODE pxCoRoutineCode,
\r
111 unsigned portBASE_TYPE uxPriority,
\r
112 unsigned portBASE_TYPE uxIndex
\r
115 * Create a new co-routine and add it to the list of co-routines that are
\r
118 * @param pxCoRoutineCode Pointer to the co-routine function. Co-routine
\r
119 * functions require special syntax - see the co-routine section of the WEB
\r
120 * documentation for more information.
\r
122 * @param uxPriority The priority with respect to other co-routines at which
\r
123 * the co-routine will run.
\r
125 * @param uxIndex Used to distinguish between different co-routines that
\r
126 * execute the same function. See the example below and the co-routine section
\r
127 * of the WEB documentation for further information.
\r
129 * @return pdPASS if the co-routine was successfully created and added to a ready
\r
130 * list, otherwise an error code defined with ProjDefs.h.
\r
134 // Co-routine to be created.
\r
135 void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
137 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
138 // This may not be necessary for const variables.
\r
139 static const char cLedToFlash[ 2 ] = { 5, 6 };
\r
140 static const portTickType uxFlashRates[ 2 ] = { 200, 400 };
\r
142 // Must start every co-routine with a call to crSTART();
\r
143 crSTART( xHandle );
\r
147 // This co-routine just delays for a fixed period, then toggles
\r
148 // an LED. Two co-routines are created using this function, so
\r
149 // the uxIndex parameter is used to tell the co-routine which
\r
150 // LED to flash and how long to delay. This assumes xQueue has
\r
151 // already been created.
\r
152 vParTestToggleLED( cLedToFlash[ uxIndex ] );
\r
153 crDELAY( xHandle, uxFlashRates[ uxIndex ] );
\r
156 // Must end every co-routine with a call to crEND();
\r
160 // Function that creates two co-routines.
\r
161 void vOtherFunction( void )
\r
163 unsigned char ucParameterToPass;
\r
164 xTaskHandle xHandle;
\r
166 // Create two co-routines at priority 0. The first is given index 0
\r
167 // so (from the code above) toggles LED 5 every 200 ticks. The second
\r
168 // is given index 1 so toggles LED 6 every 400 ticks.
\r
169 for( uxIndex = 0; uxIndex < 2; uxIndex++ )
\r
171 xCoRoutineCreate( vFlashCoRoutine, 0, uxIndex );
\r
175 * \defgroup xCoRoutineCreate xCoRoutineCreate
\r
178 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex );
\r
184 void vCoRoutineSchedule( void );</pre>
\r
186 * Run a co-routine.
\r
188 * vCoRoutineSchedule() executes the highest priority co-routine that is able
\r
189 * to run. The co-routine will execute until it either blocks, yields or is
\r
190 * preempted by a task. Co-routines execute cooperatively so one
\r
191 * co-routine cannot be preempted by another, but can be preempted by a task.
\r
193 * If an application comprises of both tasks and co-routines then
\r
194 * vCoRoutineSchedule should be called from the idle task (in an idle task
\r
199 // This idle task hook will schedule a co-routine each time it is called.
\r
200 // The rest of the idle task will execute between co-routine calls.
\r
201 void vApplicationIdleHook( void )
\r
203 vCoRoutineSchedule();
\r
206 // Alternatively, if you do not require any other part of the idle task to
\r
207 // execute, the idle task hook can call vCoRoutineScheduler() within an
\r
209 void vApplicationIdleHook( void )
\r
213 vCoRoutineSchedule();
\r
217 * \defgroup vCoRoutineSchedule vCoRoutineSchedule
\r
220 void vCoRoutineSchedule( void );
\r
225 crSTART( xCoRoutineHandle xHandle );</pre>
\r
227 * This macro MUST always be called at the start of a co-routine function.
\r
231 // Co-routine to be created.
\r
232 void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
234 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
235 static long ulAVariable;
\r
237 // Must start every co-routine with a call to crSTART();
\r
238 crSTART( xHandle );
\r
242 // Co-routine functionality goes here.
\r
245 // Must end every co-routine with a call to crEND();
\r
248 * \defgroup crSTART crSTART
\r
251 #define crSTART( pxCRCB ) switch( ( ( corCRCB * )( pxCRCB ) )->uxState ) { case 0:
\r
258 * This macro MUST always be called at the end of a co-routine function.
\r
262 // Co-routine to be created.
\r
263 void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
265 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
266 static long ulAVariable;
\r
268 // Must start every co-routine with a call to crSTART();
\r
269 crSTART( xHandle );
\r
273 // Co-routine functionality goes here.
\r
276 // Must end every co-routine with a call to crEND();
\r
279 * \defgroup crSTART crSTART
\r
285 * These macros are intended for internal use by the co-routine implementation
\r
286 * only. The macros should not be used directly by application writers.
\r
288 #define crSET_STATE0( xHandle ) ( ( corCRCB * )( xHandle ) )->uxState = (__LINE__ * 2); return; case (__LINE__ * 2):
\r
289 #define crSET_STATE1( xHandle ) ( ( corCRCB * )( xHandle ) )->uxState = ((__LINE__ * 2)+1); return; case ((__LINE__ * 2)+1):
\r
294 crDELAY( xCoRoutineHandle xHandle, portTickType xTicksToDelay );</pre>
\r
296 * Delay a co-routine for a fixed period of time.
\r
298 * crDELAY can only be called from the co-routine function itself - not
\r
299 * from within a function called by the co-routine function. This is because
\r
300 * co-routines do not maintain their own stack.
\r
302 * @param xHandle The handle of the co-routine to delay. This is the xHandle
\r
303 * parameter of the co-routine function.
\r
305 * @param xTickToDelay The number of ticks that the co-routine should delay
\r
306 * for. The actual amount of time this equates to is defined by
\r
307 * configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant portTICK_RATE_MS
\r
308 * can be used to convert ticks to milliseconds.
\r
312 // Co-routine to be created.
\r
313 void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
315 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
316 // This may not be necessary for const variables.
\r
317 // We are to delay for 200ms.
\r
318 static const xTickType xDelayTime = 200 / portTICK_RATE_MS;
\r
320 // Must start every co-routine with a call to crSTART();
\r
321 crSTART( xHandle );
\r
325 // Delay for 200ms.
\r
326 crDELAY( xHandle, xDelayTime );
\r
328 // Do something here.
\r
331 // Must end every co-routine with a call to crEND();
\r
334 * \defgroup crDELAY crDELAY
\r
337 #define crDELAY( xHandle, xTicksToDelay ) \
\r
338 if( ( xTicksToDelay ) > 0 ) \
\r
340 vCoRoutineAddToDelayedList( ( xTicksToDelay ), NULL ); \
\r
342 crSET_STATE0( ( xHandle ) );
\r
347 xCoRoutineHandle xHandle,
\r
348 xQueueHandle pxQueue,
\r
349 void *pvItemToQueue,
\r
350 portTickType xTicksToWait,
\r
351 portBASE_TYPE *pxResult
\r
354 * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
\r
355 * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
\r
357 * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
\r
358 * xQueueSend() and xQueueReceive() can only be used from tasks.
\r
360 * crQUEUE_SEND can only be called from the co-routine function itself - not
\r
361 * from within a function called by the co-routine function. This is because
\r
362 * co-routines do not maintain their own stack.
\r
364 * See the co-routine section of the WEB documentation for information on
\r
365 * passing data between tasks and co-routines and between ISR's and
\r
368 * @param xHandle The handle of the calling co-routine. This is the xHandle
\r
369 * parameter of the co-routine function.
\r
371 * @param pxQueue The handle of the queue on which the data will be posted.
\r
372 * The handle is obtained as the return value when the queue is created using
\r
373 * the xQueueCreate() API function.
\r
375 * @param pvItemToQueue A pointer to the data being posted onto the queue.
\r
376 * The number of bytes of each queued item is specified when the queue is
\r
377 * created. This number of bytes is copied from pvItemToQueue into the queue
\r
380 * @param xTickToDelay The number of ticks that the co-routine should block
\r
381 * to wait for space to become available on the queue, should space not be
\r
382 * available immediately. The actual amount of time this equates to is defined
\r
383 * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
\r
384 * portTICK_RATE_MS can be used to convert ticks to milliseconds (see example
\r
387 * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
\r
388 * data was successfully posted onto the queue, otherwise it will be set to an
\r
389 * error defined within ProjDefs.h.
\r
393 // Co-routine function that blocks for a fixed period then posts a number onto
\r
395 static void prvCoRoutineFlashTask( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
397 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
398 static portBASE_TYPE xNumberToPost = 0;
\r
399 static portBASE_TYPE xResult;
\r
401 // Co-routines must begin with a call to crSTART().
\r
402 crSTART( xHandle );
\r
406 // This assumes the queue has already been created.
\r
407 crQUEUE_SEND( xHandle, xCoRoutineQueue, &xNumberToPost, NO_DELAY, &xResult );
\r
409 if( xResult != pdPASS )
\r
411 // The message was not posted!
\r
414 // Increment the number to be posted onto the queue.
\r
417 // Delay for 100 ticks.
\r
418 crDELAY( xHandle, 100 );
\r
421 // Co-routines must end with a call to crEND().
\r
424 * \defgroup crQUEUE_SEND crQUEUE_SEND
\r
427 #define crQUEUE_SEND( xHandle, pxQueue, pvItemToQueue, xTicksToWait, pxResult ) \
\r
429 *( pxResult ) = xQueueCRSend( ( pxQueue) , ( pvItemToQueue) , ( xTicksToWait ) ); \
\r
430 if( *( pxResult ) == errQUEUE_BLOCKED ) \
\r
432 crSET_STATE0( ( xHandle ) ); \
\r
433 *pxResult = xQueueCRSend( ( pxQueue ), ( pvItemToQueue ), 0 ); \
\r
435 if( *pxResult == errQUEUE_YIELD ) \
\r
437 crSET_STATE1( ( xHandle ) ); \
\r
438 *pxResult = pdPASS; \
\r
446 xCoRoutineHandle xHandle,
\r
447 xQueueHandle pxQueue,
\r
449 portTickType xTicksToWait,
\r
450 portBASE_TYPE *pxResult
\r
453 * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine
\r
454 * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.
\r
456 * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas
\r
457 * xQueueSend() and xQueueReceive() can only be used from tasks.
\r
459 * crQUEUE_RECEIVE can only be called from the co-routine function itself - not
\r
460 * from within a function called by the co-routine function. This is because
\r
461 * co-routines do not maintain their own stack.
\r
463 * See the co-routine section of the WEB documentation for information on
\r
464 * passing data between tasks and co-routines and between ISR's and
\r
467 * @param xHandle The handle of the calling co-routine. This is the xHandle
\r
468 * parameter of the co-routine function.
\r
470 * @param pxQueue The handle of the queue from which the data will be received.
\r
471 * The handle is obtained as the return value when the queue is created using
\r
472 * the xQueueCreate() API function.
\r
474 * @param pvBuffer The buffer into which the received item is to be copied.
\r
475 * The number of bytes of each queued item is specified when the queue is
\r
476 * created. This number of bytes is copied into pvBuffer.
\r
478 * @param xTickToDelay The number of ticks that the co-routine should block
\r
479 * to wait for data to become available from the queue, should data not be
\r
480 * available immediately. The actual amount of time this equates to is defined
\r
481 * by configTICK_RATE_HZ (set in FreeRTOSConfig.h). The constant
\r
482 * portTICK_RATE_MS can be used to convert ticks to milliseconds (see the
\r
483 * crQUEUE_SEND example).
\r
485 * @param pxResult The variable pointed to by pxResult will be set to pdPASS if
\r
486 * data was successfully retrieved from the queue, otherwise it will be set to
\r
487 * an error code as defined within ProjDefs.h.
\r
491 // A co-routine receives the number of an LED to flash from a queue. It
\r
492 // blocks on the queue until the number is received.
\r
493 static void prvCoRoutineFlashWorkTask( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
495 // Variables in co-routines must be declared static if they must maintain value across a blocking call.
\r
496 static portBASE_TYPE xResult;
\r
497 static unsigned portBASE_TYPE uxLEDToFlash;
\r
499 // All co-routines must start with a call to crSTART().
\r
500 crSTART( xHandle );
\r
504 // Wait for data to become available on the queue.
\r
505 crQUEUE_RECEIVE( xHandle, xCoRoutineQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
\r
507 if( xResult == pdPASS )
\r
509 // We received the LED to flash - flash it!
\r
510 vParTestToggleLED( uxLEDToFlash );
\r
516 * \defgroup crQUEUE_RECEIVE crQUEUE_RECEIVE
\r
519 #define crQUEUE_RECEIVE( xHandle, pxQueue, pvBuffer, xTicksToWait, pxResult ) \
\r
521 *( pxResult ) = xQueueCRReceive( ( pxQueue) , ( pvBuffer ), ( xTicksToWait ) ); \
\r
522 if( *( pxResult ) == errQUEUE_BLOCKED ) \
\r
524 crSET_STATE0( ( xHandle ) ); \
\r
525 *( pxResult ) = xQueueCRReceive( ( pxQueue) , ( pvBuffer ), 0 ); \
\r
527 if( *( pxResult ) == errQUEUE_YIELD ) \
\r
529 crSET_STATE1( ( xHandle ) ); \
\r
530 *( pxResult ) = pdPASS; \
\r
537 crQUEUE_SEND_FROM_ISR(
\r
538 xQueueHandle pxQueue,
\r
539 void *pvItemToQueue,
\r
540 portBASE_TYPE xCoRoutinePreviouslyWoken
\r
543 * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
\r
544 * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
\r
545 * functions used by tasks.
\r
547 * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
\r
548 * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
\r
549 * xQueueReceiveFromISR() can only be used to pass data between a task and and
\r
552 * crQUEUE_SEND_FROM_ISR can only be called from an ISR to send data to a queue
\r
553 * that is being used from within a co-routine.
\r
555 * See the co-routine section of the WEB documentation for information on
\r
556 * passing data between tasks and co-routines and between ISR's and
\r
559 * @param xQueue The handle to the queue on which the item is to be posted.
\r
561 * @param pvItemToQueue A pointer to the item that is to be placed on the
\r
562 * queue. The size of the items the queue will hold was defined when the
\r
563 * queue was created, so this many bytes will be copied from pvItemToQueue
\r
564 * into the queue storage area.
\r
566 * @param xCoRoutinePreviouslyWoken This is included so an ISR can post onto
\r
567 * the same queue multiple times from a single interrupt. The first call
\r
568 * should always pass in pdFALSE. Subsequent calls should pass in
\r
569 * the value returned from the previous call.
\r
571 * @return pdTRUE if a co-routine was woken by posting onto the queue. This is
\r
572 * used by the ISR to determine if a context switch may be required following
\r
577 // A co-routine that blocks on a queue waiting for characters to be received.
\r
578 static void vReceivingCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
581 portBASE_TYPE xResult;
\r
583 // All co-routines must start with a call to crSTART().
\r
584 crSTART( xHandle );
\r
588 // Wait for data to become available on the queue. This assumes the
\r
589 // queue xCommsRxQueue has already been created!
\r
590 crQUEUE_RECEIVE( xHandle, xCommsRxQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );
\r
592 // Was a character received?
\r
593 if( xResult == pdPASS )
\r
595 // Process the character here.
\r
599 // All co-routines must end with a call to crEND().
\r
603 // An ISR that uses a queue to send characters received on a serial port to
\r
605 void vUART_ISR( void )
\r
608 portBASE_TYPE xCRWokenByPost = pdFALSE;
\r
610 // We loop around reading characters until there are none left in the UART.
\r
611 while( UART_RX_REG_NOT_EMPTY() )
\r
613 // Obtain the character from the UART.
\r
614 cRxedChar = UART_RX_REG;
\r
616 // Post the character onto a queue. xCRWokenByPost will be pdFALSE
\r
617 // the first time around the loop. If the post causes a co-routine
\r
618 // to be woken (unblocked) then xCRWokenByPost will be set to pdTRUE.
\r
619 // In this manner we can ensure that if more than one co-routine is
\r
620 // blocked on the queue only one is woken by this ISR no matter how
\r
621 // many characters are posted to the queue.
\r
622 xCRWokenByPost = crQUEUE_SEND_FROM_ISR( xCommsRxQueue, &cRxedChar, xCRWokenByPost );
\r
625 * \defgroup crQUEUE_SEND_FROM_ISR crQUEUE_SEND_FROM_ISR
\r
628 #define crQUEUE_SEND_FROM_ISR( pxQueue, pvItemToQueue, xCoRoutinePreviouslyWoken ) xQueueCRSendFromISR( ( pxQueue ), ( pvItemToQueue ), ( xCoRoutinePreviouslyWoken ) )
\r
634 crQUEUE_SEND_FROM_ISR(
\r
635 xQueueHandle pxQueue,
\r
637 portBASE_TYPE * pxCoRoutineWoken
\r
640 * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the
\r
641 * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()
\r
642 * functions used by tasks.
\r
644 * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to
\r
645 * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and
\r
646 * xQueueReceiveFromISR() can only be used to pass data between a task and and
\r
649 * crQUEUE_RECEIVE_FROM_ISR can only be called from an ISR to receive data
\r
650 * from a queue that is being used from within a co-routine (a co-routine
\r
651 * posted to the queue).
\r
653 * See the co-routine section of the WEB documentation for information on
\r
654 * passing data between tasks and co-routines and between ISR's and
\r
657 * @param xQueue The handle to the queue on which the item is to be posted.
\r
659 * @param pvBuffer A pointer to a buffer into which the received item will be
\r
660 * placed. The size of the items the queue will hold was defined when the
\r
661 * queue was created, so this many bytes will be copied from the queue into
\r
664 * @param pxCoRoutineWoken A co-routine may be blocked waiting for space to become
\r
665 * available on the queue. If crQUEUE_RECEIVE_FROM_ISR causes such a
\r
666 * co-routine to unblock *pxCoRoutineWoken will get set to pdTRUE, otherwise
\r
667 * *pxCoRoutineWoken will remain unchanged.
\r
669 * @return pdTRUE an item was successfully received from the queue, otherwise
\r
674 // A co-routine that posts a character to a queue then blocks for a fixed
\r
675 // period. The character is incremented each time.
\r
676 static void vSendingCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )
\r
678 // cChar holds its value while this co-routine is blocked and must therefore
\r
679 // be declared static.
\r
680 static char cCharToTx = 'a';
\r
681 portBASE_TYPE xResult;
\r
683 // All co-routines must start with a call to crSTART().
\r
684 crSTART( xHandle );
\r
688 // Send the next character to the queue.
\r
689 crQUEUE_SEND( xHandle, xCoRoutineQueue, &cCharToTx, NO_DELAY, &xResult );
\r
691 if( xResult == pdPASS )
\r
693 // The character was successfully posted to the queue.
\r
697 // Could not post the character to the queue.
\r
700 // Enable the UART Tx interrupt to cause an interrupt in this
\r
701 // hypothetical UART. The interrupt will obtain the character
\r
702 // from the queue and send it.
\r
703 ENABLE_RX_INTERRUPT();
\r
705 // Increment to the next character then block for a fixed period.
\r
706 // cCharToTx will maintain its value across the delay as it is
\r
707 // declared static.
\r
709 if( cCharToTx > 'x' )
\r
716 // All co-routines must end with a call to crEND().
\r
720 // An ISR that uses a queue to receive characters to send on a UART.
\r
721 void vUART_ISR( void )
\r
724 portBASE_TYPE xCRWokenByPost = pdFALSE;
\r
726 while( UART_TX_REG_EMPTY() )
\r
728 // Are there any characters in the queue waiting to be sent?
\r
729 // xCRWokenByPost will automatically be set to pdTRUE if a co-routine
\r
730 // is woken by the post - ensuring that only a single co-routine is
\r
731 // woken no matter how many times we go around this loop.
\r
732 if( crQUEUE_RECEIVE_FROM_ISR( pxQueue, &cCharToTx, &xCRWokenByPost ) )
\r
734 SEND_CHARACTER( cCharToTx );
\r
738 * \defgroup crQUEUE_RECEIVE_FROM_ISR crQUEUE_RECEIVE_FROM_ISR
\r
741 #define crQUEUE_RECEIVE_FROM_ISR( pxQueue, pvBuffer, pxCoRoutineWoken ) xQueueCRReceiveFromISR( ( pxQueue ), ( pvBuffer ), ( pxCoRoutineWoken ) )
\r
744 * This function is intended for internal use by the co-routine macros only.
\r
745 * The macro nature of the co-routine implementation requires that the
\r
746 * prototype appears here. The function should not be used by application
\r
749 * Removes the current co-routine from its ready list and places it in the
\r
750 * appropriate delayed list.
\r
752 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList );
\r
755 * This function is intended for internal use by the queue implementation only.
\r
756 * The function should not be used by application writers.
\r
758 * Removes the highest priority co-routine from the event list and places it in
\r
759 * the pending ready list.
\r
761 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList );
\r
767 #endif /* CO_ROUTINE_H */
\r