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