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