PageRenderTime 205ms CodeModel.GetById 37ms app.highlight 122ms RepoModel.GetById 25ms app.codeStats 1ms

/src/compiler/ucos-vs2008/UCOS_SIM/src/ucosii/os_q.c

http://ftk.googlecode.com/
C | 765 lines | 454 code | 48 blank | 263 comment | 110 complexity | 080c3056e17cfdc01f0e6d4cb1fedf1b MD5 | raw file
  1/*
  2*********************************************************************************************************
  3*                                                uC/OS-II
  4*                                          The Real-Time Kernel
  5*                                        MESSAGE QUEUE MANAGEMENT
  6*
  7*                          (c) Copyright 1992-2005, Jean J. Labrosse, Weston, FL
  8*                                           All Rights Reserved
  9*
 10* File    : OS_Q.C
 11* By      : Jean J. Labrosse
 12* Version : V2.80
 13*********************************************************************************************************
 14*/
 15
 16#ifndef  OS_MASTER_FILE

 17#include <ucos_ii.h>

 18#endif

 19
 20#if (OS_Q_EN > 0) && (OS_MAX_QS > 0)

 21/*
 22*********************************************************************************************************
 23*                                      ACCEPT MESSAGE FROM QUEUE
 24*
 25* Description: This function checks the queue to see if a message is available.  Unlike OSQPend(),
 26*              OSQAccept() does not suspend the calling task if a message is not available.
 27*
 28* Arguments  : pevent        is a pointer to the event control block
 29*
 30*              err           is a pointer to where an error message will be deposited.  Possible error
 31*                            messages are:
 32*
 33*                            OS_NO_ERR           The call was successful and your task received a
 34*                                                message.
 35*                            OS_ERR_EVENT_TYPE   You didn't pass a pointer to a queue
 36*                            OS_ERR_PEVENT_NULL  If 'pevent' is a NULL pointer
 37*                            OS_Q_EMPTY          The queue did not contain any messages
 38*
 39* Returns    : != (void *)0  is the message in the queue if one is available.  The message is removed
 40*                            from the so the next time OSQAccept() is called, the queue will contain
 41*                            one less entry.
 42*              == (void *)0  if you received a NULL pointer message
 43*                            if the queue is empty or,
 44*                            if 'pevent' is a NULL pointer or,
 45*                            if you passed an invalid event type
 46*
 47* Note(s)    : As of V2.60, you can now pass NULL pointers through queues.  Because of this, the argument
 48*              'err' has been added to the API to tell you about the outcome of the call.
 49*********************************************************************************************************
 50*/
 51
 52#if OS_Q_ACCEPT_EN > 0

 53void  *OSQAccept (OS_EVENT *pevent, INT8U *err)
 54{
 55    void      *msg;
 56    OS_Q      *pq;
 57#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */

 58    OS_CPU_SR  cpu_sr = 0;
 59#endif

 60
 61
 62
 63#if OS_ARG_CHK_EN > 0

 64    if (err == (INT8U *)0) {                     /* Validate 'err'                                     */
 65        return ((void *)0);
 66    }
 67    if (pevent == (OS_EVENT *)0) {               /* Validate 'pevent'                                  */
 68        *err = OS_ERR_PEVENT_NULL;
 69        return ((void *)0);
 70    }
 71#endif

 72    if (pevent->OSEventType != OS_EVENT_TYPE_Q) {/* Validate event block type                          */
 73        *err = OS_ERR_EVENT_TYPE;
 74        return ((void *)0);
 75    }
 76    OS_ENTER_CRITICAL();
 77    pq = (OS_Q *)pevent->OSEventPtr;             /* Point at queue control block                       */
 78    if (pq->OSQEntries > 0) {                    /* See if any messages in the queue                   */
 79        msg = *pq->OSQOut++;                     /* Yes, extract oldest message from the queue         */
 80        pq->OSQEntries--;                        /* Update the number of entries in the queue          */
 81        if (pq->OSQOut == pq->OSQEnd) {          /* Wrap OUT pointer if we are at the end of the queue */
 82            pq->OSQOut = pq->OSQStart;
 83        }
 84        *err = OS_NO_ERR;
 85    } else {
 86        *err = OS_Q_EMPTY;
 87        msg  = (void *)0;                        /* Queue is empty                                     */
 88    }
 89    OS_EXIT_CRITICAL();
 90    return (msg);                                /* Return message received (or NULL)                  */
 91}
 92#endif

 93/*$PAGE*/
 94/*
 95*********************************************************************************************************
 96*                                        CREATE A MESSAGE QUEUE
 97*
 98* Description: This function creates a message queue if free event control blocks are available.
 99*
100* Arguments  : start         is a pointer to the base address of the message queue storage area.  The
101*                            storage area MUST be declared as an array of pointers to 'void' as follows
102*
103*                            void *MessageStorage[size]
104*
105*              size          is the number of elements in the storage area
106*
107* Returns    : != (OS_EVENT *)0  is a pointer to the event control clock (OS_EVENT) associated with the
108*                                created queue
109*              == (OS_EVENT *)0  if no event control blocks were available or an error was detected
110*********************************************************************************************************
111*/
112
113OS_EVENT  *OSQCreate (void **start, INT16U size)
114{
115    OS_EVENT  *pevent;
116    OS_Q      *pq;
117#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */

118    OS_CPU_SR  cpu_sr = 0;
119#endif

120
121
122
123    if (OSIntNesting > 0) {                      /* See if called from ISR ...                         */
124        return ((OS_EVENT *)0);                  /* ... can't CREATE from an ISR                       */
125    }
126    OS_ENTER_CRITICAL();
127    pevent = OSEventFreeList;                    /* Get next free event control block                  */
128    if (OSEventFreeList != (OS_EVENT *)0) {      /* See if pool of free ECB pool was empty             */
129        OSEventFreeList = (OS_EVENT *)OSEventFreeList->OSEventPtr;
130    }
131    OS_EXIT_CRITICAL();
132    if (pevent != (OS_EVENT *)0) {               /* See if we have an event control block              */
133        OS_ENTER_CRITICAL();
134        pq = OSQFreeList;                        /* Get a free queue control block                     */
135        if (pq != (OS_Q *)0) {                   /* Were we able to get a queue control block ?        */
136            OSQFreeList            = OSQFreeList->OSQPtr; /* Yes, Adjust free list pointer to next free*/
137            OS_EXIT_CRITICAL();
138            pq->OSQStart           = start;               /*      Initialize the queue                 */
139            pq->OSQEnd             = &start[size];
140            pq->OSQIn              = start;
141            pq->OSQOut             = start;
142            pq->OSQSize            = size;
143            pq->OSQEntries         = 0;
144            pevent->OSEventType    = OS_EVENT_TYPE_Q;
145            pevent->OSEventCnt     = 0;
146            pevent->OSEventPtr     = pq;
147#if OS_EVENT_NAME_SIZE > 1

148            pevent->OSEventName[0] = '?';                  /* Unknown name                             */
149            pevent->OSEventName[1] = OS_ASCII_NUL;
150#endif

151            OS_EventWaitListInit(pevent);                 /*      Initalize the wait list              */
152        } else {
153            pevent->OSEventPtr = (void *)OSEventFreeList; /* No,  Return event control block on error  */
154            OSEventFreeList    = pevent;
155            OS_EXIT_CRITICAL();
156            pevent = (OS_EVENT *)0;
157        }
158    }
159    return (pevent);
160}
161/*$PAGE*/
162/*
163*********************************************************************************************************
164*                                        DELETE A MESSAGE QUEUE
165*
166* Description: This function deletes a message queue and readies all tasks pending on the queue.
167*
168* Arguments  : pevent        is a pointer to the event control block associated with the desired
169*                            queue.
170*
171*              opt           determines delete options as follows:
172*                            opt == OS_DEL_NO_PEND   Delete the queue ONLY if no task pending
173*                            opt == OS_DEL_ALWAYS    Deletes the queue even if tasks are waiting.
174*                                                    In this case, all the tasks pending will be readied.
175*
176*              err           is a pointer to an error code that can contain one of the following values:
177*                            OS_NO_ERR               The call was successful and the queue was deleted
178*                            OS_ERR_DEL_ISR          If you tried to delete the queue from an ISR
179*                            OS_ERR_INVALID_OPT      An invalid option was specified
180*                            OS_ERR_TASK_WAITING     One or more tasks were waiting on the queue
181*                            OS_ERR_EVENT_TYPE       If you didn't pass a pointer to a queue
182*                            OS_ERR_PEVENT_NULL      If 'pevent' is a NULL pointer.
183*
184* Returns    : pevent        upon error
185*              (OS_EVENT *)0 if the queue was successfully deleted.
186*
187* Note(s)    : 1) This function must be used with care.  Tasks that would normally expect the presence of
188*                 the queue MUST check the return code of OSQPend().
189*              2) OSQAccept() callers will not know that the intended queue has been deleted unless
190*                 they check 'pevent' to see that it's a NULL pointer.
191*              3) This call can potentially disable interrupts for a long time.  The interrupt disable
192*                 time is directly proportional to the number of tasks waiting on the queue.
193*              4) Because ALL tasks pending on the queue will be readied, you MUST be careful in
194*                 applications where the queue is used for mutual exclusion because the resource(s)
195*                 will no longer be guarded by the queue.
196*              5) If the storage for the message queue was allocated dynamically (i.e. using a malloc()
197*                 type call) then your application MUST release the memory storage by call the counterpart
198*                 call of the dynamic allocation scheme used.  If the queue storage was created statically
199*                 then, the storage can be reused.
200*********************************************************************************************************
201*/
202
203#if OS_Q_DEL_EN > 0

204OS_EVENT  *OSQDel (OS_EVENT *pevent, INT8U opt, INT8U *err)
205{
206    BOOLEAN    tasks_waiting;
207    OS_EVENT  *pevent_return;
208    OS_Q      *pq;
209#if OS_CRITICAL_METHOD == 3                                /* Allocate storage for CPU status register */

210    OS_CPU_SR  cpu_sr = 0;
211#endif

212
213
214
215#if OS_ARG_CHK_EN > 0

216    if (err == (INT8U *)0) {                               /* Validate 'err'                           */
217        return (pevent);
218    }
219    if (pevent == (OS_EVENT *)0) {                         /* Validate 'pevent'                        */
220        *err = OS_ERR_PEVENT_NULL;
221        return (pevent);
222    }
223#endif

224    if (pevent->OSEventType != OS_EVENT_TYPE_Q) {          /* Validate event block type                */
225        *err = OS_ERR_EVENT_TYPE;
226        return (pevent);
227    }
228    if (OSIntNesting > 0) {                                /* See if called from ISR ...               */
229        *err = OS_ERR_DEL_ISR;                             /* ... can't DELETE from an ISR             */
230        return (pevent);
231    }
232    OS_ENTER_CRITICAL();
233    if (pevent->OSEventGrp != 0) {                         /* See if any tasks waiting on queue        */
234        tasks_waiting = TRUE;                              /* Yes                                      */
235    } else {
236        tasks_waiting = FALSE;                             /* No                                       */
237    }
238    switch (opt) {
239        case OS_DEL_NO_PEND:                               /* Delete queue only if no task waiting     */
240             if (tasks_waiting == FALSE) {
241#if OS_EVENT_NAME_SIZE > 1

242                 pevent->OSEventName[0] = '?';             /* Unknown name                             */
243                 pevent->OSEventName[1] = OS_ASCII_NUL;
244#endif

245                 pq                     = (OS_Q *)pevent->OSEventPtr;  /* Return OS_Q to free list     */
246                 pq->OSQPtr             = OSQFreeList;
247                 OSQFreeList            = pq;
248                 pevent->OSEventType    = OS_EVENT_TYPE_UNUSED;
249                 pevent->OSEventPtr     = OSEventFreeList; /* Return Event Control Block to free list  */
250                 pevent->OSEventCnt     = 0;
251                 OSEventFreeList        = pevent;          /* Get next free event control block        */
252                 OS_EXIT_CRITICAL();
253                 *err                   = OS_NO_ERR;
254                 pevent_return          = (OS_EVENT *)0;   /* Queue has been deleted                   */
255             } else {
256                 OS_EXIT_CRITICAL();
257                 *err                   = OS_ERR_TASK_WAITING;
258                 pevent_return          = pevent;
259             }
260             break;
261
262        case OS_DEL_ALWAYS:                                /* Always delete the queue                  */
263             while (pevent->OSEventGrp != 0) {             /* Ready ALL tasks waiting for queue        */
264                 (void)OS_EventTaskRdy(pevent, (void *)0, OS_STAT_Q);
265             }
266#if OS_EVENT_NAME_SIZE > 1

267             pevent->OSEventName[0] = '?';                 /* Unknown name                             */
268             pevent->OSEventName[1] = OS_ASCII_NUL;
269#endif

270             pq                     = (OS_Q *)pevent->OSEventPtr;   /* Return OS_Q to free list        */
271             pq->OSQPtr             = OSQFreeList;
272             OSQFreeList            = pq;
273             pevent->OSEventType    = OS_EVENT_TYPE_UNUSED;
274             pevent->OSEventPtr     = OSEventFreeList;     /* Return Event Control Block to free list  */
275             pevent->OSEventCnt     = 0;
276             OSEventFreeList        = pevent;              /* Get next free event control block        */
277             OS_EXIT_CRITICAL();
278             if (tasks_waiting == TRUE) {                  /* Reschedule only if task(s) were waiting  */
279                 OS_Sched();                               /* Find highest priority task ready to run  */
280             }
281             *err                   = OS_NO_ERR;
282             pevent_return          = (OS_EVENT *)0;       /* Queue has been deleted                   */
283             break;
284
285        default:
286             OS_EXIT_CRITICAL();
287             *err                   = OS_ERR_INVALID_OPT;
288             pevent_return          = pevent;
289             break;
290    }
291    return (pevent_return);
292}
293#endif

294
295/*$PAGE*/
296/*
297*********************************************************************************************************
298*                                             FLUSH QUEUE
299*
300* Description : This function is used to flush the contents of the message queue.
301*
302* Arguments   : none
303*
304* Returns     : OS_NO_ERR           upon success
305*               OS_ERR_EVENT_TYPE   If you didn't pass a pointer to a queue
306*               OS_ERR_PEVENT_NULL  If 'pevent' is a NULL pointer
307*
308* WARNING     : You should use this function with great care because, when to flush the queue, you LOOSE
309*               the references to what the queue entries are pointing to and thus, you could cause
310*               'memory leaks'.  In other words, the data you are pointing to that's being referenced
311*               by the queue entries should, most likely, need to be de-allocated (i.e. freed).
312*********************************************************************************************************
313*/
314
315#if OS_Q_FLUSH_EN > 0

316INT8U  OSQFlush (OS_EVENT *pevent)
317{
318    OS_Q      *pq;
319#if OS_CRITICAL_METHOD == 3                           /* Allocate storage for CPU status register      */

320    OS_CPU_SR  cpu_sr = 0;
321#endif

322
323
324
325#if OS_ARG_CHK_EN > 0

326    if (pevent == (OS_EVENT *)0) {                    /* Validate 'pevent'                             */
327        return (OS_ERR_PEVENT_NULL);
328    }
329    if (pevent->OSEventType != OS_EVENT_TYPE_Q) {     /* Validate event block type                     */
330        return (OS_ERR_EVENT_TYPE);
331    }
332#endif

333    OS_ENTER_CRITICAL();
334    pq             = (OS_Q *)pevent->OSEventPtr;      /* Point to queue storage structure              */
335    pq->OSQIn      = pq->OSQStart;
336    pq->OSQOut     = pq->OSQStart;
337    pq->OSQEntries = 0;
338    OS_EXIT_CRITICAL();
339    return (OS_NO_ERR);
340}
341#endif

342
343/*$PAGE*/
344/*
345*********************************************************************************************************
346*                                     PEND ON A QUEUE FOR A MESSAGE
347*
348* Description: This function waits for a message to be sent to a queue
349*
350* Arguments  : pevent        is a pointer to the event control block associated with the desired queue
351*
352*              timeout       is an optional timeout period (in clock ticks).  If non-zero, your task will
353*                            wait for a message to arrive at the queue up to the amount of time
354*                            specified by this argument.  If you specify 0, however, your task will wait
355*                            forever at the specified queue or, until a message arrives.
356*
357*              err           is a pointer to where an error message will be deposited.  Possible error
358*                            messages are:
359*
360*                            OS_NO_ERR           The call was successful and your task received a
361*                                                message.
362*                            OS_TIMEOUT          A message was not received within the specified timeout
363*                            OS_ERR_EVENT_TYPE   You didn't pass a pointer to a queue
364*                            OS_ERR_PEVENT_NULL  If 'pevent' is a NULL pointer
365*                            OS_ERR_PEND_ISR     If you called this function from an ISR and the result
366*                                                would lead to a suspension.
367*
368* Returns    : != (void *)0  is a pointer to the message received
369*              == (void *)0  if you received a NULL pointer message or,
370*                            if no message was received or,
371*                            if 'pevent' is a NULL pointer or,
372*                            if you didn't pass a pointer to a queue.
373*
374* Note(s)    : As of V2.60, this function allows you to receive NULL pointer messages.
375*********************************************************************************************************
376*/
377
378void  *OSQPend (OS_EVENT *pevent, INT16U timeout, INT8U *err)
379{
380    void      *msg;
381    OS_Q      *pq;
382#if OS_CRITICAL_METHOD == 3                      /* Allocate storage for CPU status register           */

383    OS_CPU_SR  cpu_sr = 0;
384#endif

385
386
387
388#if OS_ARG_CHK_EN > 0

389    if (err == (INT8U *)0) {                     /* Validate 'err'                                     */
390        return ((void *)0);
391    }
392    if (pevent == (OS_EVENT *)0) {               /* Validate 'pevent'                                  */
393        *err = OS_ERR_PEVENT_NULL;
394        return ((void *)0);
395    }
396    if (pevent->OSEventType != OS_EVENT_TYPE_Q) {/* Validate event block type                          */
397        *err = OS_ERR_EVENT_TYPE;
398        return ((void *)0);
399    }
400#endif

401    if (OSIntNesting > 0) {                      /* See if called from ISR ...                         */
402        *err = OS_ERR_PEND_ISR;                  /* ... can't PEND from an ISR                         */
403        return ((void *)0);
404    }
405    OS_ENTER_CRITICAL();
406    pq = (OS_Q *)pevent->OSEventPtr;             /* Point at queue control block                       */
407    if (pq->OSQEntries > 0) {                    /* See if any messages in the queue                   */
408        msg = *pq->OSQOut++;                     /* Yes, extract oldest message from the queue         */
409        pq->OSQEntries--;                        /* Update the number of entries in the queue          */
410        if (pq->OSQOut == pq->OSQEnd) {          /* Wrap OUT pointer if we are at the end of the queue */
411            pq->OSQOut = pq->OSQStart;
412        }
413        OS_EXIT_CRITICAL();
414        *err = OS_NO_ERR;
415        return (msg);                            /* Return message received                            */
416    }
417    OSTCBCur->OSTCBStat   |= OS_STAT_Q;          /* Task will have to pend for a message to be posted  */
418    OSTCBCur->OSTCBPendTO  = FALSE;
419    OSTCBCur->OSTCBDly     = timeout;            /* Load timeout into TCB                              */
420    OS_EventTaskWait(pevent);                    /* Suspend task until event or timeout occurs         */
421    OS_EXIT_CRITICAL();
422    OS_Sched();                                  /* Find next highest priority task ready to run       */
423    OS_ENTER_CRITICAL();
424    if (OSTCBCur->OSTCBPendTO == TRUE) {         /* Was task readied because of a timeout?             */
425        OS_EventTO(pevent);                      /* Yes                                                */
426        OS_EXIT_CRITICAL();
427        *err = OS_TIMEOUT;                       /*     Indicate a timeout occured                     */
428        return ((void *)0);                      /*     No message received                            */
429    }
430    msg                     = OSTCBCur->OSTCBMsg;/* No, Extract message from TCB (Put there by QPost)  */
431    OSTCBCur->OSTCBMsg      = (void *)0;
432    OSTCBCur->OSTCBStat     = OS_STAT_RDY;
433    OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0;     /*     No longer waiting for event                    */
434    OS_EXIT_CRITICAL();
435    *err                    = OS_NO_ERR;
436    return (msg);                                /*     Return message received                        */
437}
438/*$PAGE*/
439/*
440*********************************************************************************************************
441*                                        POST MESSAGE TO A QUEUE
442*
443* Description: This function sends a message to a queue
444*
445* Arguments  : pevent        is a pointer to the event control block associated with the desired queue
446*
447*              msg           is a pointer to the message to send.
448*
449* Returns    : OS_NO_ERR             The call was successful and the message was sent
450*              OS_Q_FULL             If the queue cannot accept any more messages because it is full.
451*              OS_ERR_EVENT_TYPE     If you didn't pass a pointer to a queue.
452*              OS_ERR_PEVENT_NULL    If 'pevent' is a NULL pointer
453*
454* Note(s)    : As of V2.60, this function allows you to send NULL pointer messages.
455*********************************************************************************************************
456*/
457
458#if OS_Q_POST_EN > 0

459INT8U  OSQPost (OS_EVENT *pevent, void *msg)
460{
461    OS_Q      *pq;
462#if OS_CRITICAL_METHOD == 3                            /* Allocate storage for CPU status register     */

463    OS_CPU_SR  cpu_sr = 0;
464#endif

465
466
467
468#if OS_ARG_CHK_EN > 0

469    if (pevent == (OS_EVENT *)0) {                     /* Validate 'pevent'                            */
470        return (OS_ERR_PEVENT_NULL);
471    }
472#endif

473    if (pevent->OSEventType != OS_EVENT_TYPE_Q) {      /* Validate event block type                    */
474        return (OS_ERR_EVENT_TYPE);
475    }
476    OS_ENTER_CRITICAL();
477    if (pevent->OSEventGrp != 0) {                     /* See if any task pending on queue             */
478        (void)OS_EventTaskRdy(pevent, msg, OS_STAT_Q); /* Ready highest priority task waiting on event */
479        OS_EXIT_CRITICAL();
480        OS_Sched();                                    /* Find highest priority task ready to run      */
481        return (OS_NO_ERR);
482    }
483    pq = (OS_Q *)pevent->OSEventPtr;                   /* Point to queue control block                 */
484    if (pq->OSQEntries >= pq->OSQSize) {               /* Make sure queue is not full                  */
485        OS_EXIT_CRITICAL();
486        return (OS_Q_FULL);
487    }
488    *pq->OSQIn++ = msg;                                /* Insert message into queue                    */
489    pq->OSQEntries++;                                  /* Update the nbr of entries in the queue       */
490    if (pq->OSQIn == pq->OSQEnd) {                     /* Wrap IN ptr if we are at end of queue        */
491        pq->OSQIn = pq->OSQStart;
492    }
493    OS_EXIT_CRITICAL();
494    return (OS_NO_ERR);
495}
496#endif

497/*$PAGE*/
498/*
499*********************************************************************************************************
500*                                   POST MESSAGE TO THE FRONT OF A QUEUE
501*
502* Description: This function sends a message to a queue but unlike OSQPost(), the message is posted at
503*              the front instead of the end of the queue.  Using OSQPostFront() allows you to send
504*              'priority' messages.
505*
506* Arguments  : pevent        is a pointer to the event control block associated with the desired queue
507*
508*              msg           is a pointer to the message to send.
509*
510* Returns    : OS_NO_ERR             The call was successful and the message was sent
511*              OS_Q_FULL             If the queue cannot accept any more messages because it is full.
512*              OS_ERR_EVENT_TYPE     If you didn't pass a pointer to a queue.
513*              OS_ERR_PEVENT_NULL    If 'pevent' is a NULL pointer
514*
515* Note(s)    : As of V2.60, this function allows you to send NULL pointer messages.
516*********************************************************************************************************
517*/
518
519#if OS_Q_POST_FRONT_EN > 0

520INT8U  OSQPostFront (OS_EVENT *pevent, void *msg)
521{
522    OS_Q      *pq;
523#if OS_CRITICAL_METHOD == 3                           /* Allocate storage for CPU status register      */

524    OS_CPU_SR  cpu_sr = 0;
525#endif

526
527
528
529#if OS_ARG_CHK_EN > 0

530    if (pevent == (OS_EVENT *)0) {                    /* Validate 'pevent'                             */
531        return (OS_ERR_PEVENT_NULL);
532    }
533#endif

534    if (pevent->OSEventType != OS_EVENT_TYPE_Q) {     /* Validate event block type                     */
535        return (OS_ERR_EVENT_TYPE);
536    }
537    OS_ENTER_CRITICAL();
538    if (pevent->OSEventGrp != 0) {                    /* See if any task pending on queue              */
539        (void)OS_EventTaskRdy(pevent, msg, OS_STAT_Q);/* Ready highest priority task waiting on event  */
540        OS_EXIT_CRITICAL();
541        OS_Sched();                                   /* Find highest priority task ready to run       */
542        return (OS_NO_ERR);
543    }
544    pq = (OS_Q *)pevent->OSEventPtr;                  /* Point to queue control block                  */
545    if (pq->OSQEntries >= pq->OSQSize) {              /* Make sure queue is not full                   */
546        OS_EXIT_CRITICAL();
547        return (OS_Q_FULL);
548    }
549    if (pq->OSQOut == pq->OSQStart) {                 /* Wrap OUT ptr if we are at the 1st queue entry */
550        pq->OSQOut = pq->OSQEnd;
551    }
552    pq->OSQOut--;
553    *pq->OSQOut = msg;                                /* Insert message into queue                     */
554    pq->OSQEntries++;                                 /* Update the nbr of entries in the queue        */
555    OS_EXIT_CRITICAL();
556    return (OS_NO_ERR);
557}
558#endif

559/*$PAGE*/
560/*
561*********************************************************************************************************
562*                                        POST MESSAGE TO A QUEUE
563*
564* Description: This function sends a message to a queue.  This call has been added to reduce code size
565*              since it can replace both OSQPost() and OSQPostFront().  Also, this function adds the
566*              capability to broadcast a message to ALL tasks waiting on the message queue.
567*
568* Arguments  : pevent        is a pointer to the event control block associated with the desired queue
569*
570*              msg           is a pointer to the message to send.
571*
572*              opt           determines the type of POST performed:
573*                            OS_POST_OPT_NONE         POST to a single waiting task
574*                                                     (Identical to OSQPost())
575*                            OS_POST_OPT_BROADCAST    POST to ALL tasks that are waiting on the queue
576*                            OS_POST_OPT_FRONT        POST as LIFO (Simulates OSQPostFront())
577*
578*                            Below is a list of ALL the possible combination of these flags:
579*
580*                                 1) OS_POST_OPT_NONE
581*                                    identical to OSQPost()
582*
583*                                 2) OS_POST_OPT_FRONT
584*                                    identical to OSQPostFront()
585*
586*                                 3) OS_POST_OPT_BROADCAST
587*                                    identical to OSQPost() but will broadcast 'msg' to ALL waiting tasks
588*
589*                                 4) OS_POST_OPT_FRONT + OS_POST_OPT_BROADCAST  is identical to
590*                                    OSQPostFront() except that will broadcast 'msg' to ALL waiting tasks
591*
592* Returns    : OS_NO_ERR             The call was successful and the message was sent
593*              OS_Q_FULL             If the queue cannot accept any more messages because it is full.
594*              OS_ERR_EVENT_TYPE     If you didn't pass a pointer to a queue.
595*              OS_ERR_PEVENT_NULL    If 'pevent' is a NULL pointer
596*
597* Warning    : Interrupts can be disabled for a long time if you do a 'broadcast'.  In fact, the
598*              interrupt disable time is proportional to the number of tasks waiting on the queue.
599*********************************************************************************************************
600*/
601
602#if OS_Q_POST_OPT_EN > 0

603INT8U  OSQPostOpt (OS_EVENT *pevent, void *msg, INT8U opt)
604{
605    OS_Q      *pq;
606#if OS_CRITICAL_METHOD == 3                           /* Allocate storage for CPU status register      */

607    OS_CPU_SR  cpu_sr = 0;
608#endif

609
610
611
612#if OS_ARG_CHK_EN > 0

613    if (pevent == (OS_EVENT *)0) {                    /* Validate 'pevent'                             */
614        return (OS_ERR_PEVENT_NULL);
615    }
616#endif

617    if (pevent->OSEventType != OS_EVENT_TYPE_Q) {     /* Validate event block type                     */
618        return (OS_ERR_EVENT_TYPE);
619    }
620    OS_ENTER_CRITICAL();
621    if (pevent->OSEventGrp != 0x00) {                 /* See if any task pending on queue              */
622        if ((opt & OS_POST_OPT_BROADCAST) != 0x00) {  /* Do we need to post msg to ALL waiting tasks ? */
623            while (pevent->OSEventGrp != 0) {         /* Yes, Post to ALL tasks waiting on queue       */
624                (void)OS_EventTaskRdy(pevent, msg, OS_STAT_Q);
625            }
626        } else {
627            (void)OS_EventTaskRdy(pevent, msg, OS_STAT_Q);  /* No,  Post to HPT waiting on queue       */
628        }
629        OS_EXIT_CRITICAL();
630        OS_Sched();                                         /* Find highest priority task ready to run */
631        return (OS_NO_ERR);
632    }
633    pq = (OS_Q *)pevent->OSEventPtr;                  /* Point to queue control block                  */
634    if (pq->OSQEntries >= pq->OSQSize) {              /* Make sure queue is not full                   */
635        OS_EXIT_CRITICAL();
636        return (OS_Q_FULL);
637    }
638    if ((opt & OS_POST_OPT_FRONT) != 0x00) {          /* Do we post to the FRONT of the queue?         */
639        if (pq->OSQOut == pq->OSQStart) {             /* Yes, Post as LIFO, Wrap OUT pointer if we ... */
640            pq->OSQOut = pq->OSQEnd;                  /*      ... are at the 1st queue entry           */
641        }
642        pq->OSQOut--;
643        *pq->OSQOut = msg;                            /*      Insert message into queue                */
644    } else {                                          /* No,  Post as FIFO                             */
645        *pq->OSQIn++ = msg;                           /*      Insert message into queue                */
646        if (pq->OSQIn == pq->OSQEnd) {                /*      Wrap IN ptr if we are at end of queue    */
647            pq->OSQIn = pq->OSQStart;
648        }
649    }
650    pq->OSQEntries++;                                 /* Update the nbr of entries in the queue        */
651    OS_EXIT_CRITICAL();
652    return (OS_NO_ERR);
653}
654#endif

655/*$PAGE*/
656/*
657*********************************************************************************************************
658*                                        QUERY A MESSAGE QUEUE
659*
660* Description: This function obtains information about a message queue.
661*
662* Arguments  : pevent        is a pointer to the event control block associated with the desired queue
663*
664*              p_q_data      is a pointer to a structure that will contain information about the message
665*                            queue.
666*
667* Returns    : OS_NO_ERR           The call was successful and the message was sent
668*              OS_ERR_EVENT_TYPE   If you are attempting to obtain data from a non queue.
669*              OS_ERR_PEVENT_NULL  If 'pevent'   is a NULL pointer
670*              OS_ERR_PDATA_NULL   If 'p_q_data' is a NULL pointer
671*********************************************************************************************************
672*/
673
674#if OS_Q_QUERY_EN > 0

675INT8U  OSQQuery (OS_EVENT *pevent, OS_Q_DATA *p_q_data)
676{
677    OS_Q      *pq;
678    INT8U      i;
679#if OS_LOWEST_PRIO <= 63

680    INT8U     *psrc;
681    INT8U     *pdest;
682#else

683    INT16U    *psrc;
684    INT16U    *pdest;
685#endif

686#if OS_CRITICAL_METHOD == 3                            /* Allocate storage for CPU status register     */

687    OS_CPU_SR  cpu_sr = 0;
688#endif

689
690
691
692#if OS_ARG_CHK_EN > 0

693    if (pevent == (OS_EVENT *)0) {                     /* Validate 'pevent'                            */
694        return (OS_ERR_PEVENT_NULL);
695    }
696    if (p_q_data == (OS_Q_DATA *)0) {                  /* Validate 'p_q_data'                          */
697        return (OS_ERR_PDATA_NULL);
698    }
699#endif

700    if (pevent->OSEventType != OS_EVENT_TYPE_Q) {      /* Validate event block type                    */
701        return (OS_ERR_EVENT_TYPE);
702    }
703    OS_ENTER_CRITICAL();
704    p_q_data->OSEventGrp = pevent->OSEventGrp;         /* Copy message queue wait list                 */
705    psrc                 = &pevent->OSEventTbl[0];
706    pdest                = &p_q_data->OSEventTbl[0];
707    for (i = 0; i < OS_EVENT_TBL_SIZE; i++) {
708        *pdest++ = *psrc++;
709    }
710    pq = (OS_Q *)pevent->OSEventPtr;
711    if (pq->OSQEntries > 0) {
712        p_q_data->OSMsg = *pq->OSQOut;                 /* Get next message to return if available      */
713    } else {
714        p_q_data->OSMsg = (void *)0;
715    }
716    p_q_data->OSNMsgs = pq->OSQEntries;
717    p_q_data->OSQSize = pq->OSQSize;
718    OS_EXIT_CRITICAL();
719    return (OS_NO_ERR);
720}
721#endif                                                 /* OS_Q_QUERY_EN                                */

722
723/*$PAGE*/
724/*
725*********************************************************************************************************
726*                                      QUEUE MODULE INITIALIZATION
727*
728* Description : This function is called by uC/OS-II to initialize the message queue module.  Your
729*               application MUST NOT call this function.
730*
731* Arguments   :  none
732*
733* Returns     : none
734*
735* Note(s)    : This function is INTERNAL to uC/OS-II and your application should not call it.
736*********************************************************************************************************
737*/
738
739void  OS_QInit (void)
740{
741#if OS_MAX_QS == 1

742    OSQFreeList         = &OSQTbl[0];                /* Only ONE queue!                                */
743    OSQFreeList->OSQPtr = (OS_Q *)0;
744#endif

745
746#if OS_MAX_QS >= 2

747    INT16U  i;
748    OS_Q   *pq1;
749    OS_Q   *pq2;
750
751
752
753    OS_MemClr((INT8U *)&OSQTbl[0], sizeof(OSQTbl));  /* Clear the queue table                          */
754    pq1 = &OSQTbl[0];
755    pq2 = &OSQTbl[1];
756    for (i = 0; i < (OS_MAX_QS - 1); i++) {          /* Init. list of free QUEUE control blocks        */
757        pq1->OSQPtr = pq2;
758        pq1++;
759        pq2++;
760    }
761    pq1->OSQPtr = (OS_Q *)0;
762    OSQFreeList = &OSQTbl[0];
763#endif

764}
765#endif                                               /* OS_Q_EN                                        */