1 /*----------------------------------------------------------------------------
3 *----------------------------------------------------------------------------
5 * Purpose: Implements waits and wake-ups for mailbox messages
7 *----------------------------------------------------------------------------
9 * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
10 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * - Neither the name of ARM nor the names of its contributors may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
33 *---------------------------------------------------------------------------*/
35 #include "rt_TypeDef.h"
36 #include "RTX_Config.h"
37 #include "rt_System.h"
39 #include "rt_Mailbox.h"
40 #include "rt_MemBox.h"
43 #include "rt_HAL_CA.h"
45 #include "rt_HAL_CM.h"
49 /*----------------------------------------------------------------------------
51 *---------------------------------------------------------------------------*/
54 /*--------------------------- rt_mbx_init -----------------------------------*/
56 void rt_mbx_init (OS_ID mailbox, U16 mbx_size) {
57 /* Initialize a mailbox */
58 P_MCB p_MCB = mailbox;
67 p_MCB->size = (mbx_size + sizeof(void *) - sizeof(struct OS_MCB)) /
72 /*--------------------------- rt_mbx_send -----------------------------------*/
74 OS_RESULT rt_mbx_send (OS_ID mailbox, void *p_msg, U16 timeout) {
75 /* Send message to a mailbox */
76 P_MCB p_MCB = mailbox;
79 if ((p_MCB->p_lnk != NULL) && (p_MCB->state == 1)) {
80 /* A task is waiting for message */
81 p_TCB = rt_get_first ((P_XCB)p_MCB);
83 rt_ret_val2(p_TCB, 0x10/*osEventMessage*/, (U32)p_msg);
86 rt_ret_val (p_TCB, OS_R_MBX);
92 /* Store message in mailbox queue */
93 if (p_MCB->count == p_MCB->size) {
94 /* No free message entry, wait for one. If message queue is full, */
95 /* then no task is waiting for message. The 'p_MCB->p_lnk' list */
96 /* pointer can now be reused for send message waits task list. */
100 if (p_MCB->p_lnk != NULL) {
101 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
104 p_MCB->p_lnk = os_tsk.run;
105 os_tsk.run->p_lnk = NULL;
106 os_tsk.run->p_rlnk = (P_TCB)p_MCB;
107 /* Task is waiting to send a message */
110 os_tsk.run->msg = p_msg;
111 rt_block (timeout, WAIT_MBX);
114 /* Yes, there is a free entry in a mailbox. */
115 p_MCB->msg[p_MCB->first] = p_msg;
116 rt_inc (&p_MCB->count);
117 if (++p_MCB->first == p_MCB->size) {
125 /*--------------------------- rt_mbx_wait -----------------------------------*/
127 OS_RESULT rt_mbx_wait (OS_ID mailbox, void **message, U16 timeout) {
128 /* Receive a message; possibly wait for it */
129 P_MCB p_MCB = mailbox;
132 /* If a message is available in the fifo buffer */
133 /* remove it from the fifo buffer and return. */
135 *message = p_MCB->msg[p_MCB->last];
136 if (++p_MCB->last == p_MCB->size) {
139 if ((p_MCB->p_lnk != NULL) && (p_MCB->state == 2)) {
140 /* A task is waiting to send message */
141 p_TCB = rt_get_first ((P_XCB)p_MCB);
143 rt_ret_val(p_TCB, 0/*osOK*/);
145 rt_ret_val(p_TCB, OS_R_OK);
147 p_MCB->msg[p_MCB->first] = p_TCB->msg;
148 if (++p_MCB->first == p_MCB->size) {
155 rt_dec (&p_MCB->count);
159 /* No message available: wait for one */
163 if (p_MCB->p_lnk != NULL) {
164 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
167 p_MCB->p_lnk = os_tsk.run;
168 os_tsk.run->p_lnk = NULL;
169 os_tsk.run->p_rlnk = (P_TCB)p_MCB;
170 /* Task is waiting to receive a message */
173 rt_block(timeout, WAIT_MBX);
175 os_tsk.run->msg = message;
181 /*--------------------------- rt_mbx_check ----------------------------------*/
183 OS_RESULT rt_mbx_check (OS_ID mailbox) {
184 /* Check for free space in a mailbox. Returns the number of messages */
185 /* that can be stored to a mailbox. It returns 0 when mailbox is full. */
186 P_MCB p_MCB = mailbox;
188 return (p_MCB->size - p_MCB->count);
192 /*--------------------------- isr_mbx_send ----------------------------------*/
194 void isr_mbx_send (OS_ID mailbox, void *p_msg) {
195 /* Same function as "os_mbx_send", but to be called by ISRs. */
196 P_MCB p_MCB = mailbox;
198 rt_psq_enq (p_MCB, (U32)p_msg);
203 /*--------------------------- isr_mbx_receive -------------------------------*/
205 OS_RESULT isr_mbx_receive (OS_ID mailbox, void **message) {
206 /* Receive a message in the interrupt function. The interrupt function */
207 /* should not wait for a message since this would block the rtx os. */
208 P_MCB p_MCB = mailbox;
211 /* A message is available in the fifo buffer. */
212 *message = p_MCB->msg[p_MCB->last];
213 if (p_MCB->state == 2) {
214 /* A task is locked waiting to send message */
215 rt_psq_enq (p_MCB, 0);
218 rt_dec (&p_MCB->count);
219 if (++p_MCB->last == p_MCB->size) {
228 /*--------------------------- rt_mbx_psh ------------------------------------*/
230 void rt_mbx_psh (P_MCB p_CB, void *p_msg) {
231 /* Store the message to the mailbox queue or pass it to task directly. */
235 if (p_CB->p_lnk != NULL) switch (p_CB->state) {
238 /* Task is waiting to allocate memory, remove it from the waiting list */
239 mem = rt_alloc_box(p_msg);
240 if (mem == NULL) break;
241 p_TCB = rt_get_first ((P_XCB)p_CB);
242 rt_ret_val(p_TCB, (U32)mem);
243 p_TCB->state = READY;
245 rt_put_prio (&os_rdy, p_TCB);
249 /* Task is waiting to send a message, remove it from the waiting list */
250 p_TCB = rt_get_first ((P_XCB)p_CB);
252 rt_ret_val(p_TCB, 0/*osOK*/);
254 rt_ret_val(p_TCB, OS_R_OK);
256 p_CB->msg[p_CB->first] = p_TCB->msg;
257 rt_inc (&p_CB->count);
258 if (++p_CB->first == p_CB->size) {
261 p_TCB->state = READY;
263 rt_put_prio (&os_rdy, p_TCB);
266 /* Task is waiting for a message, pass the message to the task directly */
267 p_TCB = rt_get_first ((P_XCB)p_CB);
269 rt_ret_val2(p_TCB, 0x10/*osEventMessage*/, (U32)p_msg);
272 rt_ret_val (p_TCB, OS_R_MBX);
274 p_TCB->state = READY;
276 rt_put_prio (&os_rdy, p_TCB);
279 /* No task is waiting for a message, store it to the mailbox queue */
280 if (p_CB->count < p_CB->size) {
281 p_CB->msg[p_CB->first] = p_msg;
282 rt_inc (&p_CB->count);
283 if (++p_CB->first == p_CB->size) {
288 os_error (OS_ERR_MBX_OVF);
293 /*----------------------------------------------------------------------------
295 *---------------------------------------------------------------------------*/