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