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