25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

965 lines
42 KiB

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