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