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