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