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