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