OpenCores
URL https://opencores.org/ocsvn/openfire2/openfire2/trunk

Subversion Repositories openfire2

[/] [openfire2/] [trunk/] [sw/] [freertos/] [queue.h] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 toni32
/*
2
        FreeRTOS.org V4.2.0 - 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
*/
32
 
33
#ifndef QUEUE_H
34
#define QUEUE_H
35
 
36
typedef void * xQueueHandle;
37
 
38
/**
39
 * queue. h
40
 * <pre>
41
 xQueueHandle xQueueCreate(
42
                              unsigned portBASE_TYPE uxQueueLength,
43
                              unsigned portBASE_TYPE uxItemSize
44
                          );
45
 * </pre>
46
 *
47
 * Creates a new queue instance.  This allocates the storage required by the
48
 * new queue and returns a handle for the queue.
49
 *
50
 * @param uxQueueLength The maximum number of items that the queue can contain.
51
 *
52
 * @param uxItemSize The number of bytes each item in the queue will require.
53
 * Items are queued by copy, not by reference, so this is the number of bytes
54
 * that will be copied for each posted item.  Each item on the queue must be
55
 * the same size.
56
 *
57
 * @return If the queue is successfully create then a handle to the newly
58
 * created queue is returned.  If the queue cannot be created then 0 is
59
 * returned.
60
 *
61
 * Example usage:
62
   <pre>
63
 struct AMessage
64
 {
65
    portCHAR ucMessageID;
66
    portCHAR ucData[ 20 ];
67
 };
68
 
69
 void vATask( void *pvParameters )
70
 {
71
 xQueueHandle xQueue1, xQueue2;
72
 
73
    // Create a queue capable of containing 10 unsigned long values.
74
    xQueue1 = xQueueCreate( 10, sizeof( unsigned portLONG ) );
75
    if( xQueue1 == 0 )
76
    {
77
        // Queue was not created and must not be used.
78
    }
79
 
80
    // Create a queue capable of containing 10 pointers to AMessage structures.
81
    // These should be passed by pointer as they contain a lot of data.
82
    xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
83
    if( xQueue2 == 0 )
84
    {
85
        // Queue was not created and must not be used.
86
    }
87
 
88
    // ... Rest of task code.
89
 }
90
 </pre>
91
 * \defgroup xQueueCreate xQueueCreate
92
 * \ingroup QueueManagement
93
 */
94
xQueueHandle xQueueCreate( unsigned portBASE_TYPE uxQueueLength, unsigned portBASE_TYPE uxItemSize );
95
 
96
/**
97
 * queue. h
98
 * <pre>
99
 portBASE_TYPE xQueueSend(
100
                             xQueueHandle xQueue,
101
                             const void * pvItemToQueue,
102
                             portTickType xTicksToWait
103
                         );
104
 * </pre>
105
 *
106
 * Post an item on a queue.  The item is queued by copy, not by reference.
107
 * This function must not be called from an interrupt service routine.
108
 * See xQueueSendFromISR () for an alternative which may be used in an ISR.
109
 *
110
 * @param xQueue The handle to the queue on which the item is to be posted.
111
 *
112
 * @param pvItemToQueue A pointer to the item that is to be placed on the
113
 * queue.  The size of the items the queue will hold was defined when the
114
 * queue was created, so this many bytes will be copied from pvItemToQueue
115
 * into the queue storage area.
116
 *
117
 * @param xTicksToWait The maximum amount of time the task should block
118
 * waiting for space to become available on the queue, should it already
119
 * be full.  The call will return immediately if this is set to 0.  The
120
 * time is defined in tick periods so the constant portTICK_RATE_MS
121
 * should be used to convert to real time if this is required.
122
 *
123
 * @return pdTRUE if the item was successfully posted, otherwise errQUEUE_FULL.
124
 *
125
 * Example usage:
126
   <pre>
127
 struct AMessage
128
 {
129
    portCHAR ucMessageID;
130
    portCHAR ucData[ 20 ];
131
 } xMessage;
132
 
133
 unsigned portLONG ulVar = 10UL;
134
 
135
 void vATask( void *pvParameters )
136
 {
137
 xQueueHandle xQueue1, xQueue2;
138
 struct AMessage *pxMessage;
139
 
140
    // Create a queue capable of containing 10 unsigned long values.
141
    xQueue1 = xQueueCreate( 10, sizeof( unsigned portLONG ) );
142
 
143
    // Create a queue capable of containing 10 pointers to AMessage structures.
144
    // These should be passed by pointer as they contain a lot of data.
145
    xQueue2 = xQueueCreate( 10, sizeof( struct AMessage * ) );
146
 
147
    // ...
148
 
149
    if( xQueue1 != 0 )
150
    {
151
        // Send an unsigned long.  Wait for 10 ticks for space to become
152
        // available if necessary.
153
        if( xQueueSend( xQueue1, ( void * ) &ulVar, ( portTickType ) 10 ) != pdPASS )
154
        {
155
            // Failed to post the message, even after 10 ticks.
156
        }
157
    }
158
 
159
    if( xQueue2 != 0 )
160
    {
161
        // Send a pointer to a struct AMessage object.  Don't block if the
162
        // queue is already full.
163
        pxMessage = & xMessage;
164
        xQueueSend( xQueue2, ( void * ) &pxMessage, ( portTickType ) 0 );
165
    }
166
 
167
        // ... Rest of task code.
168
 }
169
 </pre>
170
 * \defgroup xQueueSend xQueueSend
171
 * \ingroup QueueManagement
172
 */
173
signed portBASE_TYPE xQueueSend( xQueueHandle xQueue, const void * pvItemToQueue, portTickType xTicksToWait );
174
 
175
/**
176
 * queue. h
177
 * <pre>
178
 portBASE_TYPE xQueueReceive(
179
                                xQueueHandle xQueue,
180
                                void *pvBuffer,
181
                                portTickType xTicksToWait
182
                            );</pre>
183
 *
184
 * Receive an item from a queue.  The item is received by copy so a buffer of
185
 * adequate size must be provided.  The number of bytes copied into the buffer
186
 * was defined when the queue was created.
187
 *
188
 * This function must not be used in an interrupt service routine.  See
189
 * xQueueReceiveFromISR for an alternative that can.
190
 *
191
 * @param pxQueue The handle to the queue from which the item is to be
192
 * received.
193
 *
194
 * @param pvBuffer Pointer to the buffer into which the received item will
195
 * be copied.
196
 *
197
 * @param xTicksToWait The maximum amount of time the task should block
198
 * waiting for an item to receive should the queue be empty at the time
199
 * of the call.    The time is defined in tick periods so the constant
200
 * portTICK_RATE_MS should be used to convert to real time if this is required.
201
 *
202
 * @return pdTRUE if an item was successfully received from the queue,
203
 * otherwise pdFALSE.
204
 *
205
 * Example usage:
206
   <pre>
207
 struct AMessage
208
 {
209
    portCHAR ucMessageID;
210
    portCHAR ucData[ 20 ];
211
 } xMessage;
212
 
213
 xQueueHandle xQueue;
214
 
215
 // Task to create a queue and post a value.
216
 void vATask( void *pvParameters )
217
 {
218
 struct AMessage *pxMessage;
219
 
220
    // Create a queue capable of containing 10 pointers to AMessage structures.
221
    // These should be passed by pointer as they contain a lot of data.
222
    xQueue = xQueueCreate( 10, sizeof( struct AMessage * ) );
223
    if( xQueue == 0 )
224
    {
225
        // Failed to create the queue.
226
    }
227
 
228
    // ...
229
 
230
    // Send a pointer to a struct AMessage object.  Don't block if the
231
    // queue is already full.
232
    pxMessage = & xMessage;
233
    xQueueSend( xQueue, ( void * ) &pxMessage, ( portTickType ) 0 );
234
 
235
        // ... Rest of task code.
236
 }
237
 
238
 // Task to receive from the queue.
239
 void vADifferentTask( void *pvParameters )
240
 {
241
 struct AMessage *pxRxedMessage;
242
 
243
    if( xQueue != 0 )
244
    {
245
        // Receive a message on the created queue.  Block for 10 ticks if a
246
        // message is not immediately available.
247
        if( xQueueReceive( xQueue, &( pxRxedMessage ), ( portTickType ) 10 ) )
248
        {
249
            // pcRxedMessage now points to the struct AMessage variable posted
250
            // by vATask.
251
        }
252
    }
253
 
254
        // ... Rest of task code.
255
 }
256
 </pre>
257
 * \defgroup xQueueReceive xQueueReceive
258
 * \ingroup QueueManagement
259
 */
260
signed portBASE_TYPE xQueueReceive( xQueueHandle xQueue, void *pvBuffer, portTickType xTicksToWait );
261
 
262
/**
263
 * queue. h
264
 * <pre>unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle xQueue );</pre>
265
 *
266
 * Return the number of messages stored in a queue.
267
 *
268
 * @param xQueue A handle to the queue being queried.
269
 *
270
 * @return The number of messages available in the queue.
271
 *
272
 * \page uxQueueMessagesWaiting uxQueueMessagesWaiting
273
 * \ingroup QueueManagement
274
 */
275
unsigned portBASE_TYPE uxQueueMessagesWaiting( xQueueHandle xQueue );
276
 
277
/**
278
 * queue. h
279
 * <pre>void vQueueDelete( xQueueHandle xQueue );</pre>
280
 *
281
 * Delete a queue - freeing all the memory allocated for storing of items
282
 * placed on the queue.
283
 *
284
 * @param xQueue A handle to the queue to be deleted.
285
 *
286
 * \page vQueueDelete vQueueDelete
287
 * \ingroup QueueManagement
288
 */
289
void vQueueDelete( xQueueHandle xQueue );
290
 
291
/**
292
 * queue. h
293
 * <pre>
294
 portBASE_TYPE xQueueSendFromISR(
295
                                    xQueueHandle pxQueue,
296
                                    const void *pvItemToQueue,
297
                                    portBASE_TYPE xTaskPreviouslyWoken
298
                                );
299
 </pre>
300
 *
301
 * Post an item on a queue.  It is safe to use this function from within an
302
 * interrupt service routine.
303
 *
304
 * Items are queued by copy not reference so it is preferable to only
305
 * queue small items, especially when called from an ISR.  In most cases
306
 * it would be preferable to store a pointer to the item being queued.
307
 *
308
 * @param xQueue The handle to the queue on which the item is to be posted.
309
 *
310
 * @param pvItemToQueue A pointer to the item that is to be placed on the
311
 * queue.  The size of the items the queue will hold was defined when the
312
 * queue was created, so this many bytes will be copied from pvItemToQueue
313
 * into the queue storage area.
314
 *
315
 * @param cTaskPreviouslyWoken This is included so an ISR can post onto
316
 * the same queue multiple times from a single interrupt.  The first call
317
 * should always pass in pdFALSE.  Subsequent calls should pass in
318
 * the value returned from the previous call.  See the file serial .c in the
319
 * PC port for a good example of this mechanism.
320
 *
321
 * @return pdTRUE if a task was woken by posting onto the queue.  This is
322
 * used by the ISR to determine if a context switch may be required following
323
 * the ISR.
324
 *
325
 * Example usage for buffered IO (where the ISR can obtain more than one value
326
 * per call):
327
   <pre>
328
 void vBufferISR( void )
329
 {
330
 portCHAR cIn;
331
 portBASE_TYPE xTaskWokenByPost;
332
 
333
    // We have not woken a task at the start of the ISR.
334
    cTaskWokenByPost = pdFALSE;
335
 
336
    // Loop until the buffer is empty.
337
    do
338
    {
339
        // Obtain a byte from the buffer.
340
        cIn = portINPUT_BYTE( RX_REGISTER_ADDRESS );
341
 
342
        // Post the byte.  The first time round the loop cTaskWokenByPost
343
        // will be pdFALSE.  If the queue send causes a task to wake we do
344
        // not want the task to run until we have finished the ISR, so
345
        // xQueueSendFromISR does not cause a context switch.  Also we
346
        // don't want subsequent posts to wake any other tasks, so we store
347
        // the return value back into cTaskWokenByPost so xQueueSendFromISR
348
        // knows not to wake any task the next iteration of the loop.
349
        xTaskWokenByPost = xQueueSendFromISR( xRxQueue, &cIn, cTaskWokenByPost );
350
 
351
    } while( portINPUT_BYTE( BUFFER_COUNT ) );
352
 
353
    // Now the buffer is empty we can switch context if necessary.
354
    if( cTaskWokenByPost )
355
    {
356
        taskYIELD ();
357
    }
358
 }
359
 </pre>
360
 *
361
 * \defgroup xQueueSendFromISR xQueueSendFromISR
362
 * \ingroup QueueManagement
363
 */
364
signed portBASE_TYPE xQueueSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xTaskPreviouslyWoken );
365
 
366
/**
367
 * queue. h
368
 * <pre>
369
 portBASE_TYPE xQueueReceiveFromISR(
370
                                       xQueueHandle pxQueue,
371
                                       void *pvBuffer,
372
                                       portBASE_TYPE *pxTaskWoken
373
                                   );
374
 * </pre>
375
 *
376
 * Receive an item from a queue.  It is safe to use this function from within an
377
 * interrupt service routine.
378
 *
379
 * @param pxQueue The handle to the queue from which the item is to be
380
 * received.
381
 *
382
 * @param pvBuffer Pointer to the buffer into which the received item will
383
 * be copied.
384
 *
385
 * @param pxTaskWoken A task may be blocked waiting for space to become
386
 * available on the queue.  If xQueueReceiveFromISR causes such a task to
387
 * unblock *pxTaskWoken will get set to pdTRUE, otherwise *pxTaskWoken will
388
 * remain unchanged.
389
 *
390
 * @return pdTRUE if an item was successfully received from the queue,
391
 * otherwise pdFALSE.
392
 *
393
 * Example usage:
394
   <pre>
395
 
396
 xQueueHandle xQueue;
397
 
398
 // Function to create a queue and post some values.
399
 void vAFunction( void *pvParameters )
400
 {
401
 portCHAR cValueToPost;
402
 const portTickType xBlockTime = ( portTickType )0xff;
403
 
404
    // Create a queue capable of containing 10 characters.
405
    xQueue = xQueueCreate( 10, sizeof( portCHAR ) );
406
    if( xQueue == 0 )
407
    {
408
        // Failed to create the queue.
409
    }
410
 
411
    // ...
412
 
413
    // Post some characters that will be used within an ISR.  If the queue
414
    // is full then this task will block for xBlockTime ticks.
415
    cValueToPost = 'a';
416
    xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );
417
    cValueToPost = 'b';
418
    xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );
419
 
420
    // ... keep posting characters ... this task may block when the queue
421
    // becomes full.
422
 
423
    cValueToPost = 'c';
424
    xQueueSend( xQueue, ( void * ) &cValueToPost, xBlockTime );
425
 }
426
 
427
 // ISR that outputs all the characters received on the queue.
428
 void vISR_Routine( void )
429
 {
430
 portBASE_TYPE xTaskWokenByReceive = pdFALSE;
431
 portCHAR cRxedChar;
432
 
433
    while( xQueueReceiveFromISR( xQueue, ( void * ) &cRxedChar, &xTaskWokenByReceive) )
434
    {
435
        // A character was received.  Output the character now.
436
        vOutputCharacter( cRxedChar );
437
 
438
        // If removing the character from the queue woke the task that was
439
        // posting onto the queue cTaskWokenByReceive will have been set to
440
        // pdTRUE.  No matter how many times this loop iterates only one
441
        // task will be woken.
442
    }
443
 
444
    if( cTaskWokenByPost != ( portCHAR ) pdFALSE;
445
    {
446
        taskYIELD ();
447
    }
448
 }
449
 </pre>
450
 * \defgroup xQueueReceiveFromISR xQueueReceiveFromISR
451
 * \ingroup QueueManagement
452
 */
453
signed portBASE_TYPE xQueueReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
454
 
455
 
456
/*
457
 * The functions defined above are for passing data to and from tasks.  The
458
 * functions below are the equivalents for passing data to and from
459
 * co-rtoutines.
460
 *
461
 * These functions are called from the co-routine macro implementation and
462
 * should not be called directly from application code.  Instead use the macro
463
 * wrappers defined within croutine.h.
464
 */
465
signed portBASE_TYPE xQueueCRSendFromISR( xQueueHandle pxQueue, const void *pvItemToQueue, signed portBASE_TYPE xCoRoutinePreviouslyWoken );
466
signed portBASE_TYPE xQueueCRReceiveFromISR( xQueueHandle pxQueue, void *pvBuffer, signed portBASE_TYPE *pxTaskWoken );
467
signed portBASE_TYPE xQueueCRSend( xQueueHandle pxQueue, const void *pvItemToQueue, portTickType xTicksToWait );
468
signed portBASE_TYPE xQueueCRReceive( xQueueHandle pxQueue, void *pvBuffer, portTickType xTicksToWait );
469
 
470
#endif
471
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.