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