]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_M/rt_Mailbox.c
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"
37 #include "rt_System.h"
39 #include "rt_Mailbox.h"
40 #include "rt_MemBox.h"
42 #include "rt_HAL_CM.h"
45 /*----------------------------------------------------------------------------
47 *---------------------------------------------------------------------------*/
50 /*--------------------------- rt_mbx_init -----------------------------------*/
52 void rt_mbx_init (OS_ID mailbox
, U16 mbx_size
) {
53 /* Initialize a mailbox */
54 P_MCB p_MCB
= mailbox
;
63 p_MCB
->size
= (mbx_size
+ sizeof(void *) - sizeof(struct OS_MCB
)) /
68 /*--------------------------- rt_mbx_send -----------------------------------*/
70 OS_RESULT
rt_mbx_send (OS_ID mailbox
, void *p_msg
, U16 timeout
) {
71 /* Send message to a mailbox */
72 P_MCB p_MCB
= mailbox
;
75 if ((p_MCB
->p_lnk
!= NULL
) && (p_MCB
->state
== 1)) {
76 /* A task is waiting for message */
77 p_TCB
= rt_get_first ((P_XCB
)p_MCB
);
79 rt_ret_val2(p_TCB
, 0x10/*osEventMessage*/, (U32
)p_msg
);
82 rt_ret_val (p_TCB
, OS_R_MBX
);
88 /* Store message in mailbox queue */
89 if (p_MCB
->count
== p_MCB
->size
) {
90 /* No free message entry, wait for one. If message queue is full, */
91 /* then no task is waiting for message. The 'p_MCB->p_lnk' list */
92 /* pointer can now be reused for send message waits task list. */
96 if (p_MCB
->p_lnk
!= NULL
) {
97 rt_put_prio ((P_XCB
)p_MCB
, os_tsk
.run
);
100 p_MCB
->p_lnk
= os_tsk
.run
;
101 os_tsk
.run
->p_lnk
= NULL
;
102 os_tsk
.run
->p_rlnk
= (P_TCB
)p_MCB
;
103 /* Task is waiting to send a message */
106 os_tsk
.run
->msg
= p_msg
;
107 rt_block (timeout
, WAIT_MBX
);
110 /* Yes, there is a free entry in a mailbox. */
111 p_MCB
->msg
[p_MCB
->first
] = p_msg
;
112 rt_inc (&p_MCB
->count
);
113 if (++p_MCB
->first
== p_MCB
->size
) {
121 /*--------------------------- rt_mbx_wait -----------------------------------*/
123 OS_RESULT
rt_mbx_wait (OS_ID mailbox
, void **message
, U16 timeout
) {
124 /* Receive a message; possibly wait for it */
125 P_MCB p_MCB
= mailbox
;
128 /* If a message is available in the fifo buffer */
129 /* remove it from the fifo buffer and return. */
131 *message
= p_MCB
->msg
[p_MCB
->last
];
132 if (++p_MCB
->last
== p_MCB
->size
) {
135 if ((p_MCB
->p_lnk
!= NULL
) && (p_MCB
->state
== 2)) {
136 /* A task is waiting to send message */
137 p_TCB
= rt_get_first ((P_XCB
)p_MCB
);
139 rt_ret_val(p_TCB
, 0/*osOK*/);
141 rt_ret_val(p_TCB
, OS_R_OK
);
143 p_MCB
->msg
[p_MCB
->first
] = p_TCB
->msg
;
144 if (++p_MCB
->first
== p_MCB
->size
) {
151 rt_dec (&p_MCB
->count
);
155 /* No message available: wait for one */
159 if (p_MCB
->p_lnk
!= NULL
) {
160 rt_put_prio ((P_XCB
)p_MCB
, os_tsk
.run
);
163 p_MCB
->p_lnk
= os_tsk
.run
;
164 os_tsk
.run
->p_lnk
= NULL
;
165 os_tsk
.run
->p_rlnk
= (P_TCB
)p_MCB
;
166 /* Task is waiting to receive a message */
169 rt_block(timeout
, WAIT_MBX
);
171 os_tsk
.run
->msg
= message
;
177 /*--------------------------- rt_mbx_check ----------------------------------*/
179 OS_RESULT
rt_mbx_check (OS_ID mailbox
) {
180 /* Check for free space in a mailbox. Returns the number of messages */
181 /* that can be stored to a mailbox. It returns 0 when mailbox is full. */
182 P_MCB p_MCB
= mailbox
;
184 return (p_MCB
->size
- p_MCB
->count
);
188 /*--------------------------- isr_mbx_send ----------------------------------*/
190 void isr_mbx_send (OS_ID mailbox
, void *p_msg
) {
191 /* Same function as "os_mbx_send", but to be called by ISRs. */
192 P_MCB p_MCB
= mailbox
;
194 rt_psq_enq (p_MCB
, (U32
)p_msg
);
199 /*--------------------------- isr_mbx_receive -------------------------------*/
201 OS_RESULT
isr_mbx_receive (OS_ID mailbox
, void **message
) {
202 /* Receive a message in the interrupt function. The interrupt function */
203 /* should not wait for a message since this would block the rtx os. */
204 P_MCB p_MCB
= mailbox
;
207 /* A message is available in the fifo buffer. */
208 *message
= p_MCB
->msg
[p_MCB
->last
];
209 if (p_MCB
->state
== 2) {
210 /* A task is locked waiting to send message */
211 rt_psq_enq (p_MCB
, 0);
214 rt_dec (&p_MCB
->count
);
215 if (++p_MCB
->last
== p_MCB
->size
) {
224 /*--------------------------- rt_mbx_psh ------------------------------------*/
226 void rt_mbx_psh (P_MCB p_CB
, void *p_msg
) {
227 /* Store the message to the mailbox queue or pass it to task directly. */
231 if (p_CB
->p_lnk
!= NULL
) switch (p_CB
->state
) {
234 /* Task is waiting to allocate memory, remove it from the waiting list */
235 mem
= rt_alloc_box(p_msg
);
236 if (mem
== NULL
) break;
237 p_TCB
= rt_get_first ((P_XCB
)p_CB
);
238 rt_ret_val(p_TCB
, (U32
)mem
);
239 p_TCB
->state
= READY
;
241 rt_put_prio (&os_rdy
, p_TCB
);
245 /* Task is waiting to send a message, remove it from the waiting list */
246 p_TCB
= rt_get_first ((P_XCB
)p_CB
);
248 rt_ret_val(p_TCB
, 0/*osOK*/);
250 rt_ret_val(p_TCB
, OS_R_OK
);
252 p_CB
->msg
[p_CB
->first
] = p_TCB
->msg
;
253 rt_inc (&p_CB
->count
);
254 if (++p_CB
->first
== p_CB
->size
) {
257 p_TCB
->state
= READY
;
259 rt_put_prio (&os_rdy
, p_TCB
);
262 /* Task is waiting for a message, pass the message to the task directly */
263 p_TCB
= rt_get_first ((P_XCB
)p_CB
);
265 rt_ret_val2(p_TCB
, 0x10/*osEventMessage*/, (U32
)p_msg
);
268 rt_ret_val (p_TCB
, OS_R_MBX
);
270 p_TCB
->state
= READY
;
272 rt_put_prio (&os_rdy
, p_TCB
);
275 /* No task is waiting for a message, store it to the mailbox queue */
276 if (p_CB
->count
< p_CB
->size
) {
277 p_CB
->msg
[p_CB
->first
] = p_msg
;
278 rt_inc (&p_CB
->count
);
279 if (++p_CB
->first
== p_CB
->size
) {
284 os_error (OS_ERR_MBX_OVF
);
289 /*----------------------------------------------------------------------------
291 *---------------------------------------------------------------------------*/