UCOS_TI_LM3S_Keil
 全部 结构体 文件 函数 变量 类型定义 宏定义 
os_q.c
浏览该文件的文档.
1 /*
2 *********************************************************************************************************
3 * uC/OS-II
4 * The Real-Time Kernel
5 * MESSAGE QUEUE MANAGEMENT
6 *
7 * (c) Copyright 1992-2009, Micrium, Weston, FL
8 * All Rights Reserved
9 *
10 * File : OS_Q.C
11 * By : Jean J. Labrosse
12 * Version : V2.89
13 *
14 * LICENSING TERMS:
15 * ---------------
16 * uC/OS-II is provided in source form for FREE evaluation, for educational use or for peaceful research.
17 * If you plan on using uC/OS-II in a commercial product you need to contact Micrim to properly license
18 * its use in your product. We provide ALL the source code for your convenience and to help you experience
19 * uC/OS-II. The fact that the source is provided does NOT mean that you can use it without paying a
20 * licensing fee.
21 *********************************************************************************************************
22 */
23 
24 #ifndef OS_MASTER_FILE
25 #include <ucos_ii.h>
26 #endif
27 
28 #if (OS_Q_EN > 0u) && (OS_MAX_QS > 0u)
29 /*
30 *********************************************************************************************************
31 * ACCEPT MESSAGE FROM QUEUE
32 *
33 * Description: This function checks the queue to see if a message is available. Unlike OSQPend(),
34 * OSQAccept() does not suspend the calling task if a message is not available.
35 *
36 * Arguments : pevent is a pointer to the event control block
37 *
38 * perr is a pointer to where an error message will be deposited. Possible error
39 * messages are:
40 *
41 * OS_ERR_NONE The call was successful and your task received a
42 * message.
43 * OS_ERR_EVENT_TYPE You didn't pass a pointer to a queue
44 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer
45 * OS_ERR_Q_EMPTY The queue did not contain any messages
46 *
47 * Returns : != (void *)0 is the message in the queue if one is available. The message is removed
48 * from the so the next time OSQAccept() is called, the queue will contain
49 * one less entry.
50 * == (void *)0 if you received a NULL pointer message
51 * if the queue is empty or,
52 * if 'pevent' is a NULL pointer or,
53 * if you passed an invalid event type
54 *
55 * Note(s) : As of V2.60, you can now pass NULL pointers through queues. Because of this, the argument
56 * 'perr' has been added to the API to tell you about the outcome of the call.
57 *********************************************************************************************************
58 */
59 
60 #if OS_Q_ACCEPT_EN > 0u
61 void *OSQAccept (OS_EVENT *pevent,
62  INT8U *perr)
63 {
64  void *pmsg;
65  OS_Q *pq;
66 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
67  OS_CPU_SR cpu_sr = 0u;
68 #endif
69 
70 
71 
72 #if OS_ARG_CHK_EN > 0u
73  if (perr == (INT8U *)0) { /* Validate 'perr' */
74  return ((void *)0);
75  }
76  if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
77  *perr = OS_ERR_PEVENT_NULL;
78  return ((void *)0);
79  }
80 #endif
81  if (pevent->OSEventType != OS_EVENT_TYPE_Q) {/* Validate event block type */
82  *perr = OS_ERR_EVENT_TYPE;
83  return ((void *)0);
84  }
86  pq = (OS_Q *)pevent->OSEventPtr; /* Point at queue control block */
87  if (pq->OSQEntries > 0u) { /* See if any messages in the queue */
88  pmsg = *pq->OSQOut++; /* Yes, extract oldest message from the queue */
89  pq->OSQEntries--; /* Update the number of entries in the queue */
90  if (pq->OSQOut == pq->OSQEnd) { /* Wrap OUT pointer if we are at the end of the queue */
91  pq->OSQOut = pq->OSQStart;
92  }
93  *perr = OS_ERR_NONE;
94  } else {
95  *perr = OS_ERR_Q_EMPTY;
96  pmsg = (void *)0; /* Queue is empty */
97  }
99  return (pmsg); /* Return message received (or NULL) */
100 }
101 #endif
102 /*$PAGE*/
103 /*
104 *********************************************************************************************************
105 * CREATE A MESSAGE QUEUE
106 *
107 * Description: This function creates a message queue if free event control blocks are available.
108 *
109 * Arguments : start is a pointer to the base address of the message queue storage area. The
110 * storage area MUST be declared as an array of pointers to 'void' as follows
111 *
112 * void *MessageStorage[size]
113 *
114 * size is the number of elements in the storage area
115 *
116 * Returns : != (OS_EVENT *)0 is a pointer to the event control clock (OS_EVENT) associated with the
117 * created queue
118 * == (OS_EVENT *)0 if no event control blocks were available or an error was detected
119 *********************************************************************************************************
120 */
121 
122 OS_EVENT *OSQCreate (void **start,
123  INT16U size)
124 {
125  OS_EVENT *pevent;
126  OS_Q *pq;
127 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
128  OS_CPU_SR cpu_sr = 0u;
129 #endif
130 
131 
132 
133  if (OSIntNesting > 0u) { /* See if called from ISR ... */
134  return ((OS_EVENT *)0); /* ... can't CREATE from an ISR */
135  }
137  pevent = OSEventFreeList; /* Get next free event control block */
138  if (OSEventFreeList != (OS_EVENT *)0) { /* See if pool of free ECB pool was empty */
140  }
142  if (pevent != (OS_EVENT *)0) { /* See if we have an event control block */
144  pq = OSQFreeList; /* Get a free queue control block */
145  if (pq != (OS_Q *)0) { /* Were we able to get a queue control block ? */
146  OSQFreeList = OSQFreeList->OSQPtr; /* Yes, Adjust free list pointer to next free*/
148  pq->OSQStart = start; /* Initialize the queue */
149  pq->OSQEnd = &start[size];
150  pq->OSQIn = start;
151  pq->OSQOut = start;
152  pq->OSQSize = size;
153  pq->OSQEntries = 0u;
154  pevent->OSEventType = OS_EVENT_TYPE_Q;
155  pevent->OSEventCnt = 0u;
156  pevent->OSEventPtr = pq;
157 #if OS_EVENT_NAME_EN > 0u
158  pevent->OSEventName = (INT8U *)"?";
159 #endif
160  OS_EventWaitListInit(pevent); /* Initalize the wait list */
161  } else {
162  pevent->OSEventPtr = (void *)OSEventFreeList; /* No, Return event control block on error */
163  OSEventFreeList = pevent;
165  pevent = (OS_EVENT *)0;
166  }
167  }
168  return (pevent);
169 }
170 /*$PAGE*/
171 /*
172 *********************************************************************************************************
173 * DELETE A MESSAGE QUEUE
174 *
175 * Description: This function deletes a message queue and readies all tasks pending on the queue.
176 *
177 * Arguments : pevent is a pointer to the event control block associated with the desired
178 * queue.
179 *
180 * opt determines delete options as follows:
181 * opt == OS_DEL_NO_PEND Delete the queue ONLY if no task pending
182 * opt == OS_DEL_ALWAYS Deletes the queue even if tasks are waiting.
183 * In this case, all the tasks pending will be readied.
184 *
185 * perr is a pointer to an error code that can contain one of the following values:
186 * OS_ERR_NONE The call was successful and the queue was deleted
187 * OS_ERR_DEL_ISR If you tried to delete the queue from an ISR
188 * OS_ERR_INVALID_OPT An invalid option was specified
189 * OS_ERR_TASK_WAITING One or more tasks were waiting on the queue
190 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a queue
191 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
192 *
193 * Returns : pevent upon error
194 * (OS_EVENT *)0 if the queue was successfully deleted.
195 *
196 * Note(s) : 1) This function must be used with care. Tasks that would normally expect the presence of
197 * the queue MUST check the return code of OSQPend().
198 * 2) OSQAccept() callers will not know that the intended queue has been deleted unless
199 * they check 'pevent' to see that it's a NULL pointer.
200 * 3) This call can potentially disable interrupts for a long time. The interrupt disable
201 * time is directly proportional to the number of tasks waiting on the queue.
202 * 4) Because ALL tasks pending on the queue will be readied, you MUST be careful in
203 * applications where the queue is used for mutual exclusion because the resource(s)
204 * will no longer be guarded by the queue.
205 * 5) If the storage for the message queue was allocated dynamically (i.e. using a malloc()
206 * type call) then your application MUST release the memory storage by call the counterpart
207 * call of the dynamic allocation scheme used. If the queue storage was created statically
208 * then, the storage can be reused.
209 *********************************************************************************************************
210 */
211 
212 #if OS_Q_DEL_EN > 0u
214  INT8U opt,
215  INT8U *perr)
216 {
217  BOOLEAN tasks_waiting;
218  OS_EVENT *pevent_return;
219  OS_Q *pq;
220 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
221  OS_CPU_SR cpu_sr = 0u;
222 #endif
223 
224 
225 
226 #if OS_ARG_CHK_EN > 0u
227  if (perr == (INT8U *)0) { /* Validate 'perr' */
228  return (pevent);
229  }
230  if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
231  *perr = OS_ERR_PEVENT_NULL;
232  return (pevent);
233  }
234 #endif
235  if (pevent->OSEventType != OS_EVENT_TYPE_Q) { /* Validate event block type */
236  *perr = OS_ERR_EVENT_TYPE;
237  return (pevent);
238  }
239  if (OSIntNesting > 0u) { /* See if called from ISR ... */
240  *perr = OS_ERR_DEL_ISR; /* ... can't DELETE from an ISR */
241  return (pevent);
242  }
244  if (pevent->OSEventGrp != 0u) { /* See if any tasks waiting on queue */
245  tasks_waiting = OS_TRUE; /* Yes */
246  } else {
247  tasks_waiting = OS_FALSE; /* No */
248  }
249  switch (opt) {
250  case OS_DEL_NO_PEND: /* Delete queue only if no task waiting */
251  if (tasks_waiting == OS_FALSE) {
252 #if OS_EVENT_NAME_EN > 0u
253  pevent->OSEventName = (INT8U *)"?";
254 #endif
255  pq = (OS_Q *)pevent->OSEventPtr; /* Return OS_Q to free list */
256  pq->OSQPtr = OSQFreeList;
257  OSQFreeList = pq;
259  pevent->OSEventPtr = OSEventFreeList; /* Return Event Control Block to free list */
260  pevent->OSEventCnt = 0u;
261  OSEventFreeList = pevent; /* Get next free event control block */
263  *perr = OS_ERR_NONE;
264  pevent_return = (OS_EVENT *)0; /* Queue has been deleted */
265  } else {
267  *perr = OS_ERR_TASK_WAITING;
268  pevent_return = pevent;
269  }
270  break;
271 
272  case OS_DEL_ALWAYS: /* Always delete the queue */
273  while (pevent->OSEventGrp != 0u) { /* Ready ALL tasks waiting for queue */
274  (void)OS_EventTaskRdy(pevent, (void *)0, OS_STAT_Q, OS_STAT_PEND_OK);
275  }
276 #if OS_EVENT_NAME_EN > 0u
277  pevent->OSEventName = (INT8U *)"?";
278 #endif
279  pq = (OS_Q *)pevent->OSEventPtr; /* Return OS_Q to free list */
280  pq->OSQPtr = OSQFreeList;
281  OSQFreeList = pq;
283  pevent->OSEventPtr = OSEventFreeList; /* Return Event Control Block to free list */
284  pevent->OSEventCnt = 0u;
285  OSEventFreeList = pevent; /* Get next free event control block */
287  if (tasks_waiting == OS_TRUE) { /* Reschedule only if task(s) were waiting */
288  OS_Sched(); /* Find highest priority task ready to run */
289  }
290  *perr = OS_ERR_NONE;
291  pevent_return = (OS_EVENT *)0; /* Queue has been deleted */
292  break;
293 
294  default:
296  *perr = OS_ERR_INVALID_OPT;
297  pevent_return = pevent;
298  break;
299  }
300  return (pevent_return);
301 }
302 #endif
303 
304 /*$PAGE*/
305 /*
306 *********************************************************************************************************
307 * FLUSH QUEUE
308 *
309 * Description : This function is used to flush the contents of the message queue.
310 *
311 * Arguments : none
312 *
313 * Returns : OS_ERR_NONE upon success
314 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a queue
315 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer
316 *
317 * WARNING : You should use this function with great care because, when to flush the queue, you LOOSE
318 * the references to what the queue entries are pointing to and thus, you could cause
319 * 'memory leaks'. In other words, the data you are pointing to that's being referenced
320 * by the queue entries should, most likely, need to be de-allocated (i.e. freed).
321 *********************************************************************************************************
322 */
323 
324 #if OS_Q_FLUSH_EN > 0u
326 {
327  OS_Q *pq;
328 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
329  OS_CPU_SR cpu_sr = 0u;
330 #endif
331 
332 
333 
334 #if OS_ARG_CHK_EN > 0u
335  if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
336  return (OS_ERR_PEVENT_NULL);
337  }
338  if (pevent->OSEventType != OS_EVENT_TYPE_Q) { /* Validate event block type */
339  return (OS_ERR_EVENT_TYPE);
340  }
341 #endif
343  pq = (OS_Q *)pevent->OSEventPtr; /* Point to queue storage structure */
344  pq->OSQIn = pq->OSQStart;
345  pq->OSQOut = pq->OSQStart;
346  pq->OSQEntries = 0u;
348  return (OS_ERR_NONE);
349 }
350 #endif
351 
352 /*$PAGE*/
353 /*
354 *********************************************************************************************************
355 * PEND ON A QUEUE FOR A MESSAGE
356 *
357 * Description: This function waits for a message to be sent to a queue
358 *
359 * Arguments : pevent is a pointer to the event control block associated with the desired queue
360 *
361 * timeout is an optional timeout period (in clock ticks). If non-zero, your task will
362 * wait for a message to arrive at the queue up to the amount of time
363 * specified by this argument. If you specify 0, however, your task will wait
364 * forever at the specified queue or, until a message arrives.
365 *
366 * perr is a pointer to where an error message will be deposited. Possible error
367 * messages are:
368 *
369 * OS_ERR_NONE The call was successful and your task received a
370 * message.
371 * OS_ERR_TIMEOUT A message was not received within the specified 'timeout'.
372 * OS_ERR_PEND_ABORT The wait on the queue was aborted.
373 * OS_ERR_EVENT_TYPE You didn't pass a pointer to a queue
374 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer
375 * OS_ERR_PEND_ISR If you called this function from an ISR and the result
376 * would lead to a suspension.
377 * OS_ERR_PEND_LOCKED If you called this function with the scheduler is locked
378 *
379 * Returns : != (void *)0 is a pointer to the message received
380 * == (void *)0 if you received a NULL pointer message or,
381 * if no message was received or,
382 * if 'pevent' is a NULL pointer or,
383 * if you didn't pass a pointer to a queue.
384 *
385 * Note(s) : As of V2.60, this function allows you to receive NULL pointer messages.
386 *********************************************************************************************************
387 */
388 
389 void *OSQPend (OS_EVENT *pevent,
390  INT32U timeout,
391  INT8U *perr)
392 {
393  void *pmsg;
394  OS_Q *pq;
395 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
396  OS_CPU_SR cpu_sr = 0u;
397 #endif
398 
399 
400 
401 #if OS_ARG_CHK_EN > 0u
402  if (perr == (INT8U *)0) { /* Validate 'perr' */
403  return ((void *)0);
404  }
405  if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
406  *perr = OS_ERR_PEVENT_NULL;
407  return ((void *)0);
408  }
409 #endif
410  if (pevent->OSEventType != OS_EVENT_TYPE_Q) {/* Validate event block type */
411  *perr = OS_ERR_EVENT_TYPE;
412  return ((void *)0);
413  }
414  if (OSIntNesting > 0u) { /* See if called from ISR ... */
415  *perr = OS_ERR_PEND_ISR; /* ... can't PEND from an ISR */
416  return ((void *)0);
417  }
418  if (OSLockNesting > 0u) { /* See if called with scheduler locked ... */
419  *perr = OS_ERR_PEND_LOCKED; /* ... can't PEND when locked */
420  return ((void *)0);
421  }
423  pq = (OS_Q *)pevent->OSEventPtr; /* Point at queue control block */
424  if (pq->OSQEntries > 0u) { /* See if any messages in the queue */
425  pmsg = *pq->OSQOut++; /* Yes, extract oldest message from the queue */
426  pq->OSQEntries--; /* Update the number of entries in the queue */
427  if (pq->OSQOut == pq->OSQEnd) { /* Wrap OUT pointer if we are at the end of the queue */
428  pq->OSQOut = pq->OSQStart;
429  }
431  *perr = OS_ERR_NONE;
432  return (pmsg); /* Return message received */
433  }
434  OSTCBCur->OSTCBStat |= OS_STAT_Q; /* Task will have to pend for a message to be posted */
436  OSTCBCur->OSTCBDly = timeout; /* Load timeout into TCB */
437  OS_EventTaskWait(pevent); /* Suspend task until event or timeout occurs */
439  OS_Sched(); /* Find next highest priority task ready to run */
441  switch (OSTCBCur->OSTCBStatPend) { /* See if we timed-out or aborted */
442  case OS_STAT_PEND_OK: /* Extract message from TCB (Put there by QPost) */
443  pmsg = OSTCBCur->OSTCBMsg;
444  *perr = OS_ERR_NONE;
445  break;
446 
447  case OS_STAT_PEND_ABORT:
448  pmsg = (void *)0;
449  *perr = OS_ERR_PEND_ABORT; /* Indicate that we aborted */
450  break;
451 
452  case OS_STAT_PEND_TO:
453  default:
454  OS_EventTaskRemove(OSTCBCur, pevent);
455  pmsg = (void *)0;
456  *perr = OS_ERR_TIMEOUT; /* Indicate that we didn't get event within TO */
457  break;
458  }
459  OSTCBCur->OSTCBStat = OS_STAT_RDY; /* Set task status to ready */
460  OSTCBCur->OSTCBStatPend = OS_STAT_PEND_OK; /* Clear pend status */
461  OSTCBCur->OSTCBEventPtr = (OS_EVENT *)0; /* Clear event pointers */
462 #if (OS_EVENT_MULTI_EN > 0u)
464 #endif
465  OSTCBCur->OSTCBMsg = (void *)0; /* Clear received message */
467  return (pmsg); /* Return received message */
468 }
469 /*$PAGE*/
470 /*
471 *********************************************************************************************************
472 * ABORT WAITING ON A MESSAGE QUEUE
473 *
474 * Description: This function aborts & readies any tasks currently waiting on a queue. This function
475 * should be used to fault-abort the wait on the queue, rather than to normally signal
476 * the queue via OSQPost(), OSQPostFront() or OSQPostOpt().
477 *
478 * Arguments : pevent is a pointer to the event control block associated with the desired queue.
479 *
480 * opt determines the type of ABORT performed:
481 * OS_PEND_OPT_NONE ABORT wait for a single task (HPT) waiting on the
482 * queue
483 * OS_PEND_OPT_BROADCAST ABORT wait for ALL tasks that are waiting on the
484 * queue
485 *
486 * perr is a pointer to where an error message will be deposited. Possible error
487 * messages are:
488 *
489 * OS_ERR_NONE No tasks were waiting on the queue.
490 * OS_ERR_PEND_ABORT At least one task waiting on the queue was readied
491 * and informed of the aborted wait; check return value
492 * for the number of tasks whose wait on the queue
493 * was aborted.
494 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a queue.
495 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer.
496 *
497 * Returns : == 0 if no tasks were waiting on the queue, or upon error.
498 * > 0 if one or more tasks waiting on the queue are now readied and informed.
499 *********************************************************************************************************
500 */
501 
502 #if OS_Q_PEND_ABORT_EN > 0u
504  INT8U opt,
505  INT8U *perr)
506 {
507  INT8U nbr_tasks;
508 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
509  OS_CPU_SR cpu_sr = 0u;
510 #endif
511 
512 
513 
514 #if OS_ARG_CHK_EN > 0u
515  if (perr == (INT8U *)0) { /* Validate 'perr' */
516  return (0u);
517  }
518  if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
519  *perr = OS_ERR_PEVENT_NULL;
520  return (0u);
521  }
522 #endif
523  if (pevent->OSEventType != OS_EVENT_TYPE_Q) { /* Validate event block type */
524  *perr = OS_ERR_EVENT_TYPE;
525  return (0u);
526  }
528  if (pevent->OSEventGrp != 0u) { /* See if any task waiting on queue? */
529  nbr_tasks = 0u;
530  switch (opt) {
531  case OS_PEND_OPT_BROADCAST: /* Do we need to abort ALL waiting tasks? */
532  while (pevent->OSEventGrp != 0u) { /* Yes, ready ALL tasks waiting on queue */
533  (void)OS_EventTaskRdy(pevent, (void *)0, OS_STAT_Q, OS_STAT_PEND_ABORT);
534  nbr_tasks++;
535  }
536  break;
537 
538  case OS_PEND_OPT_NONE:
539  default: /* No, ready HPT waiting on queue */
540  (void)OS_EventTaskRdy(pevent, (void *)0, OS_STAT_Q, OS_STAT_PEND_ABORT);
541  nbr_tasks++;
542  break;
543  }
545  OS_Sched(); /* Find HPT ready to run */
546  *perr = OS_ERR_PEND_ABORT;
547  return (nbr_tasks);
548  }
550  *perr = OS_ERR_NONE;
551  return (0u); /* No tasks waiting on queue */
552 }
553 #endif
554 
555 /*$PAGE*/
556 /*
557 *********************************************************************************************************
558 * POST MESSAGE TO A QUEUE
559 *
560 * Description: This function sends a message to a queue
561 *
562 * Arguments : pevent is a pointer to the event control block associated with the desired queue
563 *
564 * pmsg is a pointer to the message to send.
565 *
566 * Returns : OS_ERR_NONE The call was successful and the message was sent
567 * OS_ERR_Q_FULL If the queue cannot accept any more messages because it is full.
568 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a queue.
569 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer
570 *
571 * Note(s) : As of V2.60, this function allows you to send NULL pointer messages.
572 *********************************************************************************************************
573 */
574 
575 #if OS_Q_POST_EN > 0u
577  void *pmsg)
578 {
579  OS_Q *pq;
580 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
581  OS_CPU_SR cpu_sr = 0u;
582 #endif
583 
584 
585 
586 #if OS_ARG_CHK_EN > 0u
587  if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
588  return (OS_ERR_PEVENT_NULL);
589  }
590 #endif
591  if (pevent->OSEventType != OS_EVENT_TYPE_Q) { /* Validate event block type */
592  return (OS_ERR_EVENT_TYPE);
593  }
595  if (pevent->OSEventGrp != 0u) { /* See if any task pending on queue */
596  /* Ready highest priority task waiting on event */
597  (void)OS_EventTaskRdy(pevent, pmsg, OS_STAT_Q, OS_STAT_PEND_OK);
599  OS_Sched(); /* Find highest priority task ready to run */
600  return (OS_ERR_NONE);
601  }
602  pq = (OS_Q *)pevent->OSEventPtr; /* Point to queue control block */
603  if (pq->OSQEntries >= pq->OSQSize) { /* Make sure queue is not full */
605  return (OS_ERR_Q_FULL);
606  }
607  *pq->OSQIn++ = pmsg; /* Insert message into queue */
608  pq->OSQEntries++; /* Update the nbr of entries in the queue */
609  if (pq->OSQIn == pq->OSQEnd) { /* Wrap IN ptr if we are at end of queue */
610  pq->OSQIn = pq->OSQStart;
611  }
613  return (OS_ERR_NONE);
614 }
615 #endif
616 /*$PAGE*/
617 /*
618 *********************************************************************************************************
619 * POST MESSAGE TO THE FRONT OF A QUEUE
620 *
621 * Description: This function sends a message to a queue but unlike OSQPost(), the message is posted at
622 * the front instead of the end of the queue. Using OSQPostFront() allows you to send
623 * 'priority' messages.
624 *
625 * Arguments : pevent is a pointer to the event control block associated with the desired queue
626 *
627 * pmsg is a pointer to the message to send.
628 *
629 * Returns : OS_ERR_NONE The call was successful and the message was sent
630 * OS_ERR_Q_FULL If the queue cannot accept any more messages because it is full.
631 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a queue.
632 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer
633 *
634 * Note(s) : As of V2.60, this function allows you to send NULL pointer messages.
635 *********************************************************************************************************
636 */
637 
638 #if OS_Q_POST_FRONT_EN > 0
640  void *pmsg)
641 {
642  OS_Q *pq;
643 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
644  OS_CPU_SR cpu_sr = 0u;
645 #endif
646 
647 
648 
649 #if OS_ARG_CHK_EN > 0u
650  if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
651  return (OS_ERR_PEVENT_NULL);
652  }
653 #endif
654  if (pevent->OSEventType != OS_EVENT_TYPE_Q) { /* Validate event block type */
655  return (OS_ERR_EVENT_TYPE);
656  }
658  if (pevent->OSEventGrp != 0u) { /* See if any task pending on queue */
659  /* Ready highest priority task waiting on event */
660  (void)OS_EventTaskRdy(pevent, pmsg, OS_STAT_Q, OS_STAT_PEND_OK);
662  OS_Sched(); /* Find highest priority task ready to run */
663  return (OS_ERR_NONE);
664  }
665  pq = (OS_Q *)pevent->OSEventPtr; /* Point to queue control block */
666  if (pq->OSQEntries >= pq->OSQSize) { /* Make sure queue is not full */
668  return (OS_ERR_Q_FULL);
669  }
670  if (pq->OSQOut == pq->OSQStart) { /* Wrap OUT ptr if we are at the 1st queue entry */
671  pq->OSQOut = pq->OSQEnd;
672  }
673  pq->OSQOut--;
674  *pq->OSQOut = pmsg; /* Insert message into queue */
675  pq->OSQEntries++; /* Update the nbr of entries in the queue */
677  return (OS_ERR_NONE);
678 }
679 #endif
680 /*$PAGE*/
681 /*
682 *********************************************************************************************************
683 * POST MESSAGE TO A QUEUE
684 *
685 * Description: This function sends a message to a queue. This call has been added to reduce code size
686 * since it can replace both OSQPost() and OSQPostFront(). Also, this function adds the
687 * capability to broadcast a message to ALL tasks waiting on the message queue.
688 *
689 * Arguments : pevent is a pointer to the event control block associated with the desired queue
690 *
691 * pmsg is a pointer to the message to send.
692 *
693 * opt determines the type of POST performed:
694 * OS_POST_OPT_NONE POST to a single waiting task
695 * (Identical to OSQPost())
696 * OS_POST_OPT_BROADCAST POST to ALL tasks that are waiting on the queue
697 * OS_POST_OPT_FRONT POST as LIFO (Simulates OSQPostFront())
698 * OS_POST_OPT_NO_SCHED Indicates that the scheduler will NOT be invoked
699 *
700 * Returns : OS_ERR_NONE The call was successful and the message was sent
701 * OS_ERR_Q_FULL If the queue cannot accept any more messages because it is full.
702 * OS_ERR_EVENT_TYPE If you didn't pass a pointer to a queue.
703 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer
704 *
705 * Warning : Interrupts can be disabled for a long time if you do a 'broadcast'. In fact, the
706 * interrupt disable time is proportional to the number of tasks waiting on the queue.
707 *********************************************************************************************************
708 */
709 
710 #if OS_Q_POST_OPT_EN > 0u
712  void *pmsg,
713  INT8U opt)
714 {
715  OS_Q *pq;
716 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
717  OS_CPU_SR cpu_sr = 0u;
718 #endif
719 
720 
721 
722 #if OS_ARG_CHK_EN > 0u
723  if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
724  return (OS_ERR_PEVENT_NULL);
725  }
726 #endif
727  if (pevent->OSEventType != OS_EVENT_TYPE_Q) { /* Validate event block type */
728  return (OS_ERR_EVENT_TYPE);
729  }
731  if (pevent->OSEventGrp != 0x00u) { /* See if any task pending on queue */
732  if ((opt & OS_POST_OPT_BROADCAST) != 0x00u) { /* Do we need to post msg to ALL waiting tasks ? */
733  while (pevent->OSEventGrp != 0u) { /* Yes, Post to ALL tasks waiting on queue */
734  (void)OS_EventTaskRdy(pevent, pmsg, OS_STAT_Q, OS_STAT_PEND_OK);
735  }
736  } else { /* No, Post to HPT waiting on queue */
737  (void)OS_EventTaskRdy(pevent, pmsg, OS_STAT_Q, OS_STAT_PEND_OK);
738  }
740  if ((opt & OS_POST_OPT_NO_SCHED) == 0u) { /* See if scheduler needs to be invoked */
741  OS_Sched(); /* Find highest priority task ready to run */
742  }
743  return (OS_ERR_NONE);
744  }
745  pq = (OS_Q *)pevent->OSEventPtr; /* Point to queue control block */
746  if (pq->OSQEntries >= pq->OSQSize) { /* Make sure queue is not full */
748  return (OS_ERR_Q_FULL);
749  }
750  if ((opt & OS_POST_OPT_FRONT) != 0x00u) { /* Do we post to the FRONT of the queue? */
751  if (pq->OSQOut == pq->OSQStart) { /* Yes, Post as LIFO, Wrap OUT pointer if we ... */
752  pq->OSQOut = pq->OSQEnd; /* ... are at the 1st queue entry */
753  }
754  pq->OSQOut--;
755  *pq->OSQOut = pmsg; /* Insert message into queue */
756  } else { /* No, Post as FIFO */
757  *pq->OSQIn++ = pmsg; /* Insert message into queue */
758  if (pq->OSQIn == pq->OSQEnd) { /* Wrap IN ptr if we are at end of queue */
759  pq->OSQIn = pq->OSQStart;
760  }
761  }
762  pq->OSQEntries++; /* Update the nbr of entries in the queue */
764  return (OS_ERR_NONE);
765 }
766 #endif
767 /*$PAGE*/
768 /*
769 *********************************************************************************************************
770 * QUERY A MESSAGE QUEUE
771 *
772 * Description: This function obtains information about a message queue.
773 *
774 * Arguments : pevent is a pointer to the event control block associated with the desired queue
775 *
776 * p_q_data is a pointer to a structure that will contain information about the message
777 * queue.
778 *
779 * Returns : OS_ERR_NONE The call was successful and the message was sent
780 * OS_ERR_EVENT_TYPE If you are attempting to obtain data from a non queue.
781 * OS_ERR_PEVENT_NULL If 'pevent' is a NULL pointer
782 * OS_ERR_PDATA_NULL If 'p_q_data' is a NULL pointer
783 *********************************************************************************************************
784 */
785 
786 #if OS_Q_QUERY_EN > 0u
788  OS_Q_DATA *p_q_data)
789 {
790  OS_Q *pq;
791  INT8U i;
792 #if OS_LOWEST_PRIO <= 63u
793  INT8U *psrc;
794  INT8U *pdest;
795 #else
796  INT16U *psrc;
797  INT16U *pdest;
798 #endif
799 #if OS_CRITICAL_METHOD == 3u /* Allocate storage for CPU status register */
800  OS_CPU_SR cpu_sr = 0u;
801 #endif
802 
803 
804 
805 #if OS_ARG_CHK_EN > 0u
806  if (pevent == (OS_EVENT *)0) { /* Validate 'pevent' */
807  return (OS_ERR_PEVENT_NULL);
808  }
809  if (p_q_data == (OS_Q_DATA *)0) { /* Validate 'p_q_data' */
810  return (OS_ERR_PDATA_NULL);
811  }
812 #endif
813  if (pevent->OSEventType != OS_EVENT_TYPE_Q) { /* Validate event block type */
814  return (OS_ERR_EVENT_TYPE);
815  }
817  p_q_data->OSEventGrp = pevent->OSEventGrp; /* Copy message queue wait list */
818  psrc = &pevent->OSEventTbl[0];
819  pdest = &p_q_data->OSEventTbl[0];
820  for (i = 0u; i < OS_EVENT_TBL_SIZE; i++) {
821  *pdest++ = *psrc++;
822  }
823  pq = (OS_Q *)pevent->OSEventPtr;
824  if (pq->OSQEntries > 0u) {
825  p_q_data->OSMsg = *pq->OSQOut; /* Get next message to return if available */
826  } else {
827  p_q_data->OSMsg = (void *)0;
828  }
829  p_q_data->OSNMsgs = pq->OSQEntries;
830  p_q_data->OSQSize = pq->OSQSize;
832  return (OS_ERR_NONE);
833 }
834 #endif /* OS_Q_QUERY_EN */
835 
836 /*$PAGE*/
837 /*
838 *********************************************************************************************************
839 * QUEUE MODULE INITIALIZATION
840 *
841 * Description : This function is called by uC/OS-II to initialize the message queue module. Your
842 * application MUST NOT call this function.
843 *
844 * Arguments : none
845 *
846 * Returns : none
847 *
848 * Note(s) : This function is INTERNAL to uC/OS-II and your application should not call it.
849 *********************************************************************************************************
850 */
851 
852 void OS_QInit (void)
853 {
854 #if OS_MAX_QS == 1u
855  OSQFreeList = &OSQTbl[0]; /* Only ONE queue! */
856  OSQFreeList->OSQPtr = (OS_Q *)0;
857 #endif
858 
859 #if OS_MAX_QS >= 2u
860  INT16U i;
861  OS_Q *pq1;
862  OS_Q *pq2;
863 
864 
865 
866  OS_MemClr((INT8U *)&OSQTbl[0], sizeof(OSQTbl)); /* Clear the queue table */
867  pq1 = &OSQTbl[0];
868  pq2 = &OSQTbl[1];
869  for (i = 0u; i < (OS_MAX_QS - 1u); i++) { /* Init. list of free QUEUE control blocks */
870  pq1->OSQPtr = pq2;
871  pq1++;
872  pq2++;
873  }
874  pq1->OSQPtr = (OS_Q *)0;
875  OSQFreeList = &OSQTbl[0];
876 #endif
877 }
878 #endif /* OS_Q_EN */