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