]> git.sur5r.net Git - freertos/blob - Source/include/croutine.h
Added xQueueSendToBack, xQueueSendToFront, xQueuePeek and xSemaphoreCreateMutex ...
[freertos] / Source / include / croutine.h
1 /*\r
2         FreeRTOS.org V4.4.0 - Copyright (C) 2003-2007 Richard Barry.\r
3 \r
4         This file is part of the FreeRTOS.org distribution.\r
5 \r
6         FreeRTOS.org is free software; you can redistribute it and/or modify\r
7         it under the terms of the GNU General Public License as published by\r
8         the Free Software Foundation; either version 2 of the License, or\r
9         (at your option) any later version.\r
10 \r
11         FreeRTOS.org is distributed in the hope that it will be useful,\r
12         but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14         GNU General Public License for more details.\r
15 \r
16         You should have received a copy of the GNU General Public License\r
17         along with FreeRTOS.org; if not, write to the Free Software\r
18         Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19 \r
20         A special exception to the GPL can be applied should you wish to distribute\r
21         a combined work that includes FreeRTOS.org, without being obliged to provide\r
22         the source code for any proprietary components.  See the licensing section\r
23         of http://www.FreeRTOS.org for full details of how and when the exception\r
24         can be applied.\r
25 \r
26         ***************************************************************************\r
27         See http://www.FreeRTOS.org for documentation, latest information, license\r
28         and contact details.  Please ensure to read the configuration and relevant\r
29         port sections of the online documentation.\r
30 \r
31         Also see http://www.SafeRTOS.com for an IEC 61508 compliant version along\r
32         with commercial development and support options.\r
33         ***************************************************************************\r
34 */\r
35 #ifndef CO_ROUTINE_H\r
36 #define CO_ROUTINE_H\r
37 \r
38 #include "list.h"\r
39 \r
40 /* Used to hide the implementation of the co-routine control block.  The\r
41 control block structure however has to be included in the header due to\r
42 the macro implementation of the co-routine functionality. */\r
43 typedef void * xCoRoutineHandle;\r
44 \r
45 /* Defines the prototype to which co-routine functions must conform. */\r
46 typedef void (*crCOROUTINE_CODE)( xCoRoutineHandle, unsigned portBASE_TYPE );\r
47 \r
48 typedef struct corCoRoutineControlBlock\r
49 {\r
50         crCOROUTINE_CODE                pxCoRoutineFunction;\r
51         xListItem                               xGenericListItem;       /*< List item used to place the CRCB in ready and blocked queues. */\r
52         xListItem                               xEventListItem;         /*< List item used to place the CRCB in event lists. */\r
53         unsigned portBASE_TYPE  uxPriority;                     /*< The priority of the co-routine in relation to other co-routines. */\r
54         unsigned portBASE_TYPE  uxIndex;                        /*< Used to distinguish between co-routines when multiple co-routines use the same co-routine function. */\r
55         unsigned portSHORT              uxState;                        /*< Used internally by the co-routine implementation. */\r
56 } corCRCB; /* Co-routine control block.  Note must be identical in size down to uxPriority with tskTCB. */\r
57 \r
58 /**\r
59  * croutine. h\r
60  *<pre>\r
61  portBASE_TYPE xCoRoutineCreate(\r
62                                  crCOROUTINE_CODE pxCoRoutineCode,\r
63                                  unsigned portBASE_TYPE uxPriority,\r
64                                  unsigned portBASE_TYPE uxIndex\r
65                                );</pre>\r
66  *\r
67  * Create a new co-routine and add it to the list of co-routines that are\r
68  * ready to run.\r
69  *\r
70  * @param pxCoRoutineCode Pointer to the co-routine function.  Co-routine\r
71  * functions require special syntax - see the co-routine section of the WEB\r
72  * documentation for more information.\r
73  *\r
74  * @param uxPriority The priority with respect to other co-routines at which\r
75  *  the co-routine will run.\r
76  *\r
77  * @param uxIndex Used to distinguish between different co-routines that\r
78  * execute the same function.  See the example below and the co-routine section\r
79  * of the WEB documentation for further information.\r
80  *\r
81  * @return pdPASS if the co-routine was successfully created and added to a ready\r
82  * list, otherwise an error code defined with ProjDefs.h.\r
83  *\r
84  * Example usage:\r
85    <pre>\r
86  // Co-routine to be created.\r
87  void vFlashCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
88  {\r
89  // Variables in co-routines must be declared static if they must maintain value across a blocking call.\r
90  // This may not be necessary for const variables.\r
91  static const char cLedToFlash[ 2 ] = { 5, 6 };\r
92  static const portTickType xTimeToDelay[ 2 ] = { 200, 400 };\r
93 \r
94      // Must start every co-routine with a call to crSTART();\r
95      crSTART( xHandle );\r
96 \r
97      for( ;; )\r
98      {\r
99          // This co-routine just delays for a fixed period, then toggles\r
100          // an LED.  Two co-routines are created using this function, so\r
101          // the uxIndex parameter is used to tell the co-routine which\r
102          // LED to flash and how long to delay.  This assumes xQueue has\r
103          // already been created.\r
104          vParTestToggleLED( cLedToFlash[ uxIndex ] );\r
105          crDELAY( xHandle, uxFlashRates[ uxIndex ] );\r
106      }\r
107 \r
108      // Must end every co-routine with a call to crEND();\r
109      crEND();\r
110  }\r
111 \r
112  // Function that creates two co-routines.\r
113  void vOtherFunction( void )\r
114  {\r
115  unsigned char ucParameterToPass;\r
116  xTaskHandle xHandle;\r
117                 \r
118      // Create two co-routines at priority 0.  The first is given index 0\r
119      // so (from the code above) toggles LED 5 every 200 ticks.  The second\r
120      // is given index 1 so toggles LED 6 every 400 ticks.\r
121      for( uxIndex = 0; uxIndex < 2; uxIndex++ )\r
122      {\r
123          xCoRoutineCreate( vFlashCoRoutine, 0, uxIndex );\r
124      }\r
125  }\r
126    </pre>\r
127  * \defgroup xCoRoutineCreate xCoRoutineCreate\r
128  * \ingroup Tasks\r
129  */\r
130 signed portBASE_TYPE xCoRoutineCreate( crCOROUTINE_CODE pxCoRoutineCode, unsigned portBASE_TYPE uxPriority, unsigned portBASE_TYPE uxIndex );\r
131 \r
132 \r
133 /**\r
134  * croutine. h\r
135  *<pre>\r
136  void vCoRoutineSchedule( void );</pre>\r
137  *\r
138  * Run a co-routine.\r
139  *\r
140  * vCoRoutineSchedule() executes the highest priority co-routine that is able\r
141  * to run.  The co-routine will execute until it either blocks, yields or is\r
142  * preempted by a task.  Co-routines execute cooperatively so one\r
143  * co-routine cannot be preempted by another, but can be preempted by a task.\r
144  *\r
145  * If an application comprises of both tasks and co-routines then\r
146  * vCoRoutineSchedule should be called from the idle task (in an idle task\r
147  * hook).\r
148  *\r
149  * Example usage:\r
150    <pre>\r
151  // This idle task hook will schedule a co-routine each time it is called.\r
152  // The rest of the idle task will execute between co-routine calls.\r
153  void vApplicationIdleHook( void )\r
154  {\r
155         vCoRoutineSchedule();\r
156  }\r
157 \r
158  // Alternatively, if you do not require any other part of the idle task to\r
159  // execute, the idle task hook can call vCoRoutineScheduler() within an\r
160  // infinite loop.\r
161  void vApplicationIdleHook( void )\r
162  {\r
163     for( ;; )\r
164     {\r
165         vCoRoutineSchedule();\r
166     }\r
167  }\r
168  </pre>\r
169  * \defgroup vCoRoutineSchedule vCoRoutineSchedule\r
170  * \ingroup Tasks\r
171  */\r
172 void vCoRoutineSchedule( void );\r
173 \r
174 /**\r
175  * croutine. h\r
176  * <pre>\r
177  crSTART( xCoRoutineHandle xHandle );</pre>\r
178  *\r
179  * This macro MUST always be called at the start of a co-routine function.\r
180  *\r
181  * Example usage:\r
182    <pre>\r
183  // Co-routine to be created.\r
184  void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
185  {\r
186  // Variables in co-routines must be declared static if they must maintain value across a blocking call.\r
187  static portLONG ulAVariable;\r
188 \r
189      // Must start every co-routine with a call to crSTART();\r
190      crSTART( xHandle );\r
191 \r
192      for( ;; )\r
193      {\r
194           // Co-routine functionality goes here.\r
195      }\r
196 \r
197      // Must end every co-routine with a call to crEND();\r
198      crEND();\r
199  }</pre>\r
200  * \defgroup crSTART crSTART\r
201  * \ingroup Tasks\r
202  */\r
203 #define crSTART( pxCRCB ) switch( ( ( corCRCB * )pxCRCB )->uxState ) { case 0:\r
204 \r
205 /**\r
206  * croutine. h\r
207  * <pre>\r
208  crEND();</pre>\r
209  *\r
210  * This macro MUST always be called at the end of a co-routine function.\r
211  *\r
212  * Example usage:\r
213    <pre>\r
214  // Co-routine to be created.\r
215  void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
216  {\r
217  // Variables in co-routines must be declared static if they must maintain value across a blocking call.\r
218  static portLONG ulAVariable;\r
219 \r
220      // Must start every co-routine with a call to crSTART();\r
221      crSTART( xHandle );\r
222 \r
223      for( ;; )\r
224      {\r
225           // Co-routine functionality goes here.\r
226      }\r
227 \r
228      // Must end every co-routine with a call to crEND();\r
229      crEND();\r
230  }</pre>\r
231  * \defgroup crSTART crSTART\r
232  * \ingroup Tasks\r
233  */\r
234 #define crEND() }\r
235 \r
236 /*\r
237  * These macros are intended for internal use by the co-routine implementation\r
238  * only.  The macros should not be used directly by application writers.\r
239  */\r
240 #define crSET_STATE0( xHandle ) ( ( corCRCB * )xHandle)->uxState = (__LINE__ * 2); return; case (__LINE__ * 2):\r
241 #define crSET_STATE1( xHandle ) ( ( corCRCB * )xHandle)->uxState = ((__LINE__ * 2)+1); return; case ((__LINE__ * 2)+1):\r
242 \r
243 /**\r
244  * croutine. h\r
245  *<pre>\r
246  crDELAY( xCoRoutineHandle xHandle, portTickType xTicksToDelay );</pre>\r
247  *\r
248  * Delay a co-routine for a fixed period of time.\r
249  *\r
250  * crDELAY can only be called from the co-routine function itself - not\r
251  * from within a function called by the co-routine function.  This is because\r
252  * co-routines do not maintain their own stack.\r
253  *\r
254  * @param xHandle The handle of the co-routine to delay.  This is the xHandle\r
255  * parameter of the co-routine function.\r
256  *\r
257  * @param xTickToDelay The number of ticks that the co-routine should delay\r
258  * for.  The actual amount of time this equates to is defined by\r
259  * configTICK_RATE_HZ (set in FreeRTOSConfig.h).  The constant portTICK_RATE_MS\r
260  * can be used to convert ticks to milliseconds.\r
261  *\r
262  * Example usage:\r
263    <pre>\r
264  // Co-routine to be created.\r
265  void vACoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
266  {\r
267  // Variables in co-routines must be declared static if they must maintain value across a blocking call.\r
268  // This may not be necessary for const variables.\r
269  // We are to delay for 200ms.\r
270  static const xTickType xDelayTime = 200 / portTICK_RATE_MS;\r
271 \r
272      // Must start every co-routine with a call to crSTART();\r
273      crSTART( xHandle );\r
274 \r
275      for( ;; )\r
276      {\r
277         // Delay for 200ms.\r
278         crDELAY( xHandle, xDelayTime );\r
279 \r
280         // Do something here.\r
281      }\r
282 \r
283      // Must end every co-routine with a call to crEND();\r
284      crEND();\r
285  }</pre>\r
286  * \defgroup crDELAY crDELAY\r
287  * \ingroup Tasks\r
288  */\r
289 #define crDELAY( xHandle, xTicksToDelay )                                                                                               \\r
290         if( xTicksToDelay > 0 )                                                                                                                         \\r
291         {                                                                                                                                                                       \\r
292                 vCoRoutineAddToDelayedList( xTicksToDelay, NULL );                                                              \\r
293         }                                                                                                                                                                       \\r
294         crSET_STATE0( xHandle );\r
295 \r
296 /**\r
297  * <pre>\r
298  crQUEUE_SEND(\r
299                   xCoRoutineHandle xHandle,\r
300                   xQueueHandle pxQueue,\r
301                   void *pvItemToQueue,\r
302                   portTickType xTicksToWait,\r
303                   portBASE_TYPE *pxResult\r
304              )</pre>\r
305  *\r
306  * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine\r
307  * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.\r
308  *\r
309  * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas\r
310  * xQueueSend() and xQueueReceive() can only be used from tasks.\r
311  *\r
312  * crQUEUE_SEND can only be called from the co-routine function itself - not\r
313  * from within a function called by the co-routine function.  This is because\r
314  * co-routines do not maintain their own stack.\r
315  *\r
316  * See the co-routine section of the WEB documentation for information on\r
317  * passing data between tasks and co-routines and between ISR's and\r
318  * co-routines.\r
319  *\r
320  * @param xHandle The handle of the calling co-routine.  This is the xHandle\r
321  * parameter of the co-routine function.\r
322  *\r
323  * @param pxQueue The handle of the queue on which the data will be posted.\r
324  * The handle is obtained as the return value when the queue is created using\r
325  * the xQueueCreate() API function.\r
326  *\r
327  * @param pvItemToQueue A pointer to the data being posted onto the queue.\r
328  * The number of bytes of each queued item is specified when the queue is\r
329  * created.  This number of bytes is copied from pvItemToQueue into the queue\r
330  * itself.\r
331  *\r
332  * @param xTickToDelay The number of ticks that the co-routine should block\r
333  * to wait for space to become available on the queue, should space not be\r
334  * available immediately. The actual amount of time this equates to is defined\r
335  * by configTICK_RATE_HZ (set in FreeRTOSConfig.h).  The constant\r
336  * portTICK_RATE_MS can be used to convert ticks to milliseconds (see example\r
337  * below).\r
338  *\r
339  * @param pxResult The variable pointed to by pxResult will be set to pdPASS if\r
340  * data was successfully posted onto the queue, otherwise it will be set to an\r
341  * error defined within ProjDefs.h.\r
342  *\r
343  * Example usage:\r
344    <pre>\r
345  // Co-routine function that blocks for a fixed period then posts a number onto\r
346  // a queue.\r
347  static void prvCoRoutineFlashTask( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
348  {\r
349  // Variables in co-routines must be declared static if they must maintain value across a blocking call.\r
350  static portBASE_TYPE xNumberToPost = 0;\r
351  static portBASE_TYPE xResult;\r
352 \r
353     // Co-routines must begin with a call to crSTART().\r
354     crSTART( xHandle );\r
355 \r
356     for( ;; )\r
357     {\r
358         // This assumes the queue has already been created.\r
359         crQUEUE_SEND( xHandle, xCoRoutineQueue, &xNumberToPost, NO_DELAY, &xResult );\r
360 \r
361         if( xResult != pdPASS )\r
362         {\r
363             // The message was not posted!\r
364         }\r
365 \r
366         // Increment the number to be posted onto the queue.\r
367         xNumberToPost++;\r
368 \r
369         // Delay for 100 ticks.\r
370         crDELAY( xHandle, 100 );\r
371     }\r
372 \r
373     // Co-routines must end with a call to crEND().\r
374     crEND();\r
375  }</pre>\r
376  * \defgroup crQUEUE_SEND crQUEUE_SEND\r
377  * \ingroup Tasks\r
378  */\r
379 #define crQUEUE_SEND( xHandle, pxQueue, pvItemToQueue, xTicksToWait, pxResult )                 \\r
380 {                                                                                                                                                                               \\r
381         *pxResult = xQueueCRSend( pxQueue, pvItemToQueue, xTicksToWait );                                       \\r
382         if( *pxResult == errQUEUE_BLOCKED )                                                                                                     \\r
383         {                                                                                                                                                                       \\r
384                 crSET_STATE0( xHandle );                                                                                                                \\r
385                 *pxResult = xQueueCRSend( pxQueue, pvItemToQueue, 0 );                                                  \\r
386         }                                                                                                                                                                       \\r
387         if( *pxResult == errQUEUE_YIELD )                                                                                                       \\r
388         {                                                                                                                                                                       \\r
389                 crSET_STATE1( xHandle );                                                                                                                \\r
390                 *pxResult = pdPASS;                                                                                                                             \\r
391         }                                                                                                                                                                       \\r
392 }\r
393 \r
394 /**\r
395  * croutine. h\r
396  * <pre>\r
397   crQUEUE_RECEIVE(\r
398                      xCoRoutineHandle xHandle,\r
399                      xQueueHandle pxQueue,\r
400                      void *pvBuffer,\r
401                      portTickType xTicksToWait,\r
402                      portBASE_TYPE *pxResult\r
403                  )</pre>\r
404  *\r
405  * The macro's crQUEUE_SEND() and crQUEUE_RECEIVE() are the co-routine\r
406  * equivalent to the xQueueSend() and xQueueReceive() functions used by tasks.\r
407  *\r
408  * crQUEUE_SEND and crQUEUE_RECEIVE can only be used from a co-routine whereas\r
409  * xQueueSend() and xQueueReceive() can only be used from tasks.\r
410  *\r
411  * crQUEUE_RECEIVE can only be called from the co-routine function itself - not\r
412  * from within a function called by the co-routine function.  This is because\r
413  * co-routines do not maintain their own stack.\r
414  *\r
415  * See the co-routine section of the WEB documentation for information on\r
416  * passing data between tasks and co-routines and between ISR's and\r
417  * co-routines.\r
418  *\r
419  * @param xHandle The handle of the calling co-routine.  This is the xHandle\r
420  * parameter of the co-routine function.\r
421  *\r
422  * @param pxQueue The handle of the queue from which the data will be received.\r
423  * The handle is obtained as the return value when the queue is created using\r
424  * the xQueueCreate() API function.\r
425  *\r
426  * @param pvBuffer The buffer into which the received item is to be copied.\r
427  * The number of bytes of each queued item is specified when the queue is\r
428  * created.  This number of bytes is copied into pvBuffer.\r
429  *\r
430  * @param xTickToDelay The number of ticks that the co-routine should block\r
431  * to wait for data to become available from the queue, should data not be\r
432  * available immediately. The actual amount of time this equates to is defined\r
433  * by configTICK_RATE_HZ (set in FreeRTOSConfig.h).  The constant\r
434  * portTICK_RATE_MS can be used to convert ticks to milliseconds (see the\r
435  * crQUEUE_SEND example).\r
436  *\r
437  * @param pxResult The variable pointed to by pxResult will be set to pdPASS if\r
438  * data was successfully retrieved from the queue, otherwise it will be set to\r
439  * an error code as defined within ProjDefs.h.\r
440  *\r
441  * Example usage:\r
442  <pre>\r
443  // A co-routine receives the number of an LED to flash from a queue.  It\r
444  // blocks on the queue until the number is received.\r
445  static void prvCoRoutineFlashWorkTask( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
446  {\r
447  // Variables in co-routines must be declared static if they must maintain value across a blocking call.\r
448  static portBASE_TYPE xResult;\r
449  static unsigned portBASE_TYPE uxLEDToFlash;\r
450 \r
451     // All co-routines must start with a call to crSTART().\r
452     crSTART( xHandle );\r
453 \r
454     for( ;; )\r
455     {\r
456         // Wait for data to become available on the queue.\r
457         crQUEUE_RECEIVE( xHandle, xCoRoutineQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );\r
458 \r
459         if( xResult == pdPASS )\r
460         {\r
461             // We received the LED to flash - flash it!\r
462             vParTestToggleLED( uxLEDToFlash );\r
463         }\r
464     }\r
465 \r
466     crEND();\r
467  }</pre>\r
468  * \defgroup crQUEUE_RECEIVE crQUEUE_RECEIVE\r
469  * \ingroup Tasks\r
470  */\r
471 #define crQUEUE_RECEIVE( xHandle, pxQueue, pvBuffer, xTicksToWait, pxResult )                   \\r
472 {                                                                                                                                                                               \\r
473         *pxResult = xQueueCRReceive( pxQueue, pvBuffer, xTicksToWait );                                         \\r
474         if( *pxResult == errQUEUE_BLOCKED )                                                                                             \\r
475         {                                                                                                                                                                       \\r
476                 crSET_STATE0( xHandle );                                                                                                                \\r
477                 *pxResult = xQueueCRReceive( pxQueue, pvBuffer, 0 );                                                    \\r
478         }                                                                                                                                                                       \\r
479         if( *pxResult == errQUEUE_YIELD )                                                                                                       \\r
480         {                                                                                                                                                                       \\r
481                 crSET_STATE1( xHandle );                                                                                                                \\r
482                 *pxResult = pdPASS;                                                                                                                             \\r
483         }                                                                                                                                                                       \\r
484 }\r
485 \r
486 /**\r
487  * croutine. h\r
488  * <pre>\r
489   crQUEUE_SEND_FROM_ISR(\r
490                             xQueueHandle pxQueue,\r
491                             void *pvItemToQueue,\r
492                             portBASE_TYPE xCoRoutinePreviouslyWoken\r
493                        )</pre>\r
494  *\r
495  * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the\r
496  * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()\r
497  * functions used by tasks.\r
498  *\r
499  * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to\r
500  * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and\r
501  * xQueueReceiveFromISR() can only be used to pass data between a task and and\r
502  * ISR.\r
503  *\r
504  * crQUEUE_SEND_FROM_ISR can only be called from an ISR to send data to a queue\r
505  * that is being used from within a co-routine.\r
506  *\r
507  * See the co-routine section of the WEB documentation for information on\r
508  * passing data between tasks and co-routines and between ISR's and\r
509  * co-routines.\r
510  *\r
511  * @param xQueue The handle to the queue on which the item is to be posted.\r
512  *\r
513  * @param pvItemToQueue A pointer to the item that is to be placed on the\r
514  * queue.  The size of the items the queue will hold was defined when the\r
515  * queue was created, so this many bytes will be copied from pvItemToQueue\r
516  * into the queue storage area.\r
517  *\r
518  * @param xCoRoutinePreviouslyWoken This is included so an ISR can post onto\r
519  * the same queue multiple times from a single interrupt.  The first call\r
520  * should always pass in pdFALSE.  Subsequent calls should pass in\r
521  * the value returned from the previous call.\r
522  *\r
523  * @return pdTRUE if a co-routine was woken by posting onto the queue.  This is\r
524  * used by the ISR to determine if a context switch may be required following\r
525  * the ISR.\r
526  *\r
527  * Example usage:\r
528  <pre>\r
529  // A co-routine that blocks on a queue waiting for characters to be received.\r
530  static void vReceivingCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
531  {\r
532  portCHAR cRxedChar;\r
533  portBASE_TYPE xResult;\r
534 \r
535      // All co-routines must start with a call to crSTART().\r
536      crSTART( xHandle );\r
537 \r
538      for( ;; )\r
539      {\r
540          // Wait for data to become available on the queue.  This assumes the\r
541          // queue xCommsRxQueue has already been created!\r
542          crQUEUE_RECEIVE( xHandle, xCommsRxQueue, &uxLEDToFlash, portMAX_DELAY, &xResult );\r
543 \r
544          // Was a character received?\r
545          if( xResult == pdPASS )\r
546          {\r
547              // Process the character here.\r
548          }\r
549      }\r
550 \r
551      // All co-routines must end with a call to crEND().\r
552      crEND();\r
553  }\r
554 \r
555  // An ISR that uses a queue to send characters received on a serial port to\r
556  // a co-routine.\r
557  void vUART_ISR( void )\r
558  {\r
559  portCHAR cRxedChar;\r
560  portBASE_TYPE xCRWokenByPost = pdFALSE;\r
561 \r
562      // We loop around reading characters until there are none left in the UART.\r
563      while( UART_RX_REG_NOT_EMPTY() )\r
564      {\r
565          // Obtain the character from the UART.\r
566          cRxedChar = UART_RX_REG;\r
567 \r
568          // Post the character onto a queue.  xCRWokenByPost will be pdFALSE\r
569          // the first time around the loop.  If the post causes a co-routine\r
570          // to be woken (unblocked) then xCRWokenByPost will be set to pdTRUE.\r
571          // In this manner we can ensure that if more than one co-routine is\r
572          // blocked on the queue only one is woken by this ISR no matter how\r
573          // many characters are posted to the queue.\r
574          xCRWokenByPost = crQUEUE_SEND_FROM_ISR( xCommsRxQueue, &cRxedChar, xCRWokenByPost );\r
575      }\r
576  }</pre>\r
577  * \defgroup crQUEUE_SEND_FROM_ISR crQUEUE_SEND_FROM_ISR\r
578  * \ingroup Tasks\r
579  */\r
580 #define crQUEUE_SEND_FROM_ISR( pxQueue, pvItemToQueue, xCoRoutinePreviouslyWoken ) xQueueCRSendFromISR( pxQueue, pvItemToQueue, xCoRoutinePreviouslyWoken )\r
581 \r
582 \r
583 /**\r
584  * croutine. h\r
585  * <pre>\r
586   crQUEUE_SEND_FROM_ISR(\r
587                             xQueueHandle pxQueue,\r
588                             void *pvBuffer,\r
589                             portBASE_TYPE * pxCoRoutineWoken\r
590                        )</pre>\r
591  *\r
592  * The macro's crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() are the\r
593  * co-routine equivalent to the xQueueSendFromISR() and xQueueReceiveFromISR()\r
594  * functions used by tasks.\r
595  *\r
596  * crQUEUE_SEND_FROM_ISR() and crQUEUE_RECEIVE_FROM_ISR() can only be used to\r
597  * pass data between a co-routine and and ISR, whereas xQueueSendFromISR() and\r
598  * xQueueReceiveFromISR() can only be used to pass data between a task and and\r
599  * ISR.\r
600  *\r
601  * crQUEUE_RECEIVE_FROM_ISR can only be called from an ISR to receive data\r
602  * from a queue that is being used from within a co-routine (a co-routine\r
603  * posted to the queue).\r
604  *\r
605  * See the co-routine section of the WEB documentation for information on\r
606  * passing data between tasks and co-routines and between ISR's and\r
607  * co-routines.\r
608  *\r
609  * @param xQueue The handle to the queue on which the item is to be posted.\r
610  *\r
611  * @param pvBuffer A pointer to a buffer into which the received item will be\r
612  * placed.  The size of the items the queue will hold was defined when the\r
613  * queue was created, so this many bytes will be copied from the queue into\r
614  * pvBuffer.\r
615  *\r
616  * @param pxCoRoutineWoken A co-routine may be blocked waiting for space to become\r
617  * available on the queue.  If crQUEUE_RECEIVE_FROM_ISR causes such a\r
618  * co-routine to unblock *pxCoRoutineWoken will get set to pdTRUE, otherwise\r
619  * *pxCoRoutineWoken will remain unchanged.\r
620  *\r
621  * @return pdTRUE an item was successfully received from the queue, otherwise\r
622  * pdFALSE.\r
623  *\r
624  * Example usage:\r
625  <pre>\r
626  // A co-routine that posts a character to a queue then blocks for a fixed\r
627  // period.  The character is incremented each time.\r
628  static void vSendingCoRoutine( xCoRoutineHandle xHandle, unsigned portBASE_TYPE uxIndex )\r
629  {\r
630  // cChar holds its value while this co-routine is blocked and must therefore\r
631  // be declared static.\r
632  static portCHAR cCharToTx = 'a';\r
633  portBASE_TYPE xResult;\r
634 \r
635      // All co-routines must start with a call to crSTART().\r
636      crSTART( xHandle );\r
637 \r
638      for( ;; )\r
639      {\r
640          // Send the next character to the queue.\r
641          crQUEUE_SEND( xHandle, xCoRoutineQueue, &cCharToTx, NO_DELAY, &xResult );\r
642 \r
643          if( xResult == pdPASS )\r
644          {\r
645              // The character was successfully posted to the queue.\r
646          }\r
647                  else\r
648                  {\r
649                         // Could not post the character to the queue.\r
650                  }\r
651 \r
652          // Enable the UART Tx interrupt to cause an interrupt in this\r
653                  // hypothetical UART.  The interrupt will obtain the character\r
654                  // from the queue and send it.\r
655                  ENABLE_RX_INTERRUPT();\r
656 \r
657                  // Increment to the next character then block for a fixed period.\r
658                  // cCharToTx will maintain its value across the delay as it is\r
659                  // declared static.\r
660                  cCharToTx++;\r
661                  if( cCharToTx > 'x' )\r
662                  {\r
663                         cCharToTx = 'a';\r
664                  }\r
665                  crDELAY( 100 );\r
666      }\r
667 \r
668      // All co-routines must end with a call to crEND().\r
669      crEND();\r
670  }\r
671 \r
672  // An ISR that uses a queue to receive characters to send on a UART.\r
673  void vUART_ISR( void )\r
674  {\r
675  portCHAR cCharToTx;\r
676  portBASE_TYPE xCRWokenByPost = pdFALSE;\r
677 \r
678      while( UART_TX_REG_EMPTY() )\r
679      {\r
680          // Are there any characters in the queue waiting to be sent?\r
681                  // xCRWokenByPost will automatically be set to pdTRUE if a co-routine\r
682                  // is woken by the post - ensuring that only a single co-routine is\r
683                  // woken no matter how many times we go around this loop.\r
684          if( crQUEUE_RECEIVE_FROM_ISR( pxQueue, &cCharToTx, &xCRWokenByPost ) )\r
685                  {\r
686                          SEND_CHARACTER( cCharToTx );\r
687                  }\r
688      }\r
689  }</pre>\r
690  * \defgroup crQUEUE_RECEIVE_FROM_ISR crQUEUE_RECEIVE_FROM_ISR\r
691  * \ingroup Tasks\r
692  */\r
693 #define crQUEUE_RECEIVE_FROM_ISR( pxQueue, pvBuffer, pxCoRoutineWoken ) xQueueCRReceiveFromISR( pxQueue, pvBuffer, pxCoRoutineWoken )\r
694 \r
695 /*\r
696  * This function is intended for internal use by the co-routine macros only.\r
697  * The macro nature of the co-routine implementation requires that the\r
698  * prototype appears here.  The function should not be used by application\r
699  * writers.\r
700  *\r
701  * Removes the current co-routine from its ready list and places it in the\r
702  * appropriate delayed list.\r
703  */\r
704 void vCoRoutineAddToDelayedList( portTickType xTicksToDelay, xList *pxEventList );\r
705 \r
706 /*\r
707  * This function is intended for internal use by the queue implementation only.\r
708  * The function should not be used by application writers.\r
709  *\r
710  * Removes the highest priority co-routine from the event list and places it in\r
711  * the pending ready list.\r
712  */\r
713 signed portBASE_TYPE xCoRoutineRemoveFromEventList( const xList *pxEventList );\r
714 \r
715 \r
716 #endif /* CO_ROUTINE_H */\r