]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_A/rt_Mailbox.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / rtos / rtx / TARGET_CORTEX_A / rt_Mailbox.c
1 /*----------------------------------------------------------------------------
2 * RL-ARM - RTX
3 *----------------------------------------------------------------------------
4 * Name: RT_MAILBOX.C
5 * Purpose: Implements waits and wake-ups for mailbox messages
6 * Rev.: V4.60
7 *----------------------------------------------------------------------------
8 *
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.
21 *
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 *---------------------------------------------------------------------------*/
34
35 #include "rt_TypeDef.h"
36 #include "RTX_Config.h"
37 #include "rt_System.h"
38 #include "rt_List.h"
39 #include "rt_Mailbox.h"
40 #include "rt_MemBox.h"
41 #include "rt_Task.h"
42 #ifdef __CORTEX_A9
43 #include "rt_HAL_CA.h"
44 #else
45 #include "rt_HAL_CM.h"
46 #endif
47
48
49 /*----------------------------------------------------------------------------
50 * Functions
51 *---------------------------------------------------------------------------*/
52
53
54 /*--------------------------- rt_mbx_init -----------------------------------*/
55
56 void rt_mbx_init (OS_ID mailbox, U16 mbx_size) {
57 /* Initialize a mailbox */
58 P_MCB p_MCB = mailbox;
59
60 p_MCB->cb_type = MCB;
61 p_MCB->state = 0;
62 p_MCB->isr_st = 0;
63 p_MCB->p_lnk = NULL;
64 p_MCB->first = 0;
65 p_MCB->last = 0;
66 p_MCB->count = 0;
67 p_MCB->size = (mbx_size + sizeof(void *) - sizeof(struct OS_MCB)) /
68 (U32)sizeof (void *);
69 }
70
71
72 /*--------------------------- rt_mbx_send -----------------------------------*/
73
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;
77 P_TCB p_TCB;
78
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);
82 #ifdef __CMSIS_RTOS
83 rt_ret_val2(p_TCB, 0x10/*osEventMessage*/, (U32)p_msg);
84 #else
85 *p_TCB->msg = p_msg;
86 rt_ret_val (p_TCB, OS_R_MBX);
87 #endif
88 rt_rmv_dly (p_TCB);
89 rt_dispatch (p_TCB);
90 }
91 else {
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. */
97 if (timeout == 0) {
98 return (OS_R_TMO);
99 }
100 if (p_MCB->p_lnk != NULL) {
101 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
102 }
103 else {
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 */
108 p_MCB->state = 2;
109 }
110 os_tsk.run->msg = p_msg;
111 rt_block (timeout, WAIT_MBX);
112 return (OS_R_TMO);
113 }
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) {
118 p_MCB->first = 0;
119 }
120 }
121 return (OS_R_OK);
122 }
123
124
125 /*--------------------------- rt_mbx_wait -----------------------------------*/
126
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;
130 P_TCB p_TCB;
131
132 /* If a message is available in the fifo buffer */
133 /* remove it from the fifo buffer and return. */
134 if (p_MCB->count) {
135 *message = p_MCB->msg[p_MCB->last];
136 if (++p_MCB->last == p_MCB->size) {
137 p_MCB->last = 0;
138 }
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);
142 #ifdef __CMSIS_RTOS
143 rt_ret_val(p_TCB, 0/*osOK*/);
144 #else
145 rt_ret_val(p_TCB, OS_R_OK);
146 #endif
147 p_MCB->msg[p_MCB->first] = p_TCB->msg;
148 if (++p_MCB->first == p_MCB->size) {
149 p_MCB->first = 0;
150 }
151 rt_rmv_dly (p_TCB);
152 rt_dispatch (p_TCB);
153 }
154 else {
155 rt_dec (&p_MCB->count);
156 }
157 return (OS_R_OK);
158 }
159 /* No message available: wait for one */
160 if (timeout == 0) {
161 return (OS_R_TMO);
162 }
163 if (p_MCB->p_lnk != NULL) {
164 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
165 }
166 else {
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 */
171 p_MCB->state = 1;
172 }
173 rt_block(timeout, WAIT_MBX);
174 #ifndef __CMSIS_RTOS
175 os_tsk.run->msg = message;
176 #endif
177 return (OS_R_TMO);
178 }
179
180
181 /*--------------------------- rt_mbx_check ----------------------------------*/
182
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;
187
188 return (p_MCB->size - p_MCB->count);
189 }
190
191
192 /*--------------------------- isr_mbx_send ----------------------------------*/
193
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;
197
198 rt_psq_enq (p_MCB, (U32)p_msg);
199 rt_psh_req ();
200 }
201
202
203 /*--------------------------- isr_mbx_receive -------------------------------*/
204
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;
209
210 if (p_MCB->count) {
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);
216 rt_psh_req ();
217 }
218 rt_dec (&p_MCB->count);
219 if (++p_MCB->last == p_MCB->size) {
220 p_MCB->last = 0;
221 }
222 return (OS_R_MBX);
223 }
224 return (OS_R_OK);
225 }
226
227
228 /*--------------------------- rt_mbx_psh ------------------------------------*/
229
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. */
232 P_TCB p_TCB;
233 void *mem;
234
235 if (p_CB->p_lnk != NULL) switch (p_CB->state) {
236 #ifdef __CMSIS_RTOS
237 case 3:
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;
244 rt_rmv_dly (p_TCB);
245 rt_put_prio (&os_rdy, p_TCB);
246 break;
247 #endif
248 case 2:
249 /* Task is waiting to send a message, remove it from the waiting list */
250 p_TCB = rt_get_first ((P_XCB)p_CB);
251 #ifdef __CMSIS_RTOS
252 rt_ret_val(p_TCB, 0/*osOK*/);
253 #else
254 rt_ret_val(p_TCB, OS_R_OK);
255 #endif
256 p_CB->msg[p_CB->first] = p_TCB->msg;
257 rt_inc (&p_CB->count);
258 if (++p_CB->first == p_CB->size) {
259 p_CB->first = 0;
260 }
261 p_TCB->state = READY;
262 rt_rmv_dly (p_TCB);
263 rt_put_prio (&os_rdy, p_TCB);
264 break;
265 case 1:
266 /* Task is waiting for a message, pass the message to the task directly */
267 p_TCB = rt_get_first ((P_XCB)p_CB);
268 #ifdef __CMSIS_RTOS
269 rt_ret_val2(p_TCB, 0x10/*osEventMessage*/, (U32)p_msg);
270 #else
271 *p_TCB->msg = p_msg;
272 rt_ret_val (p_TCB, OS_R_MBX);
273 #endif
274 p_TCB->state = READY;
275 rt_rmv_dly (p_TCB);
276 rt_put_prio (&os_rdy, p_TCB);
277 break;
278 } else {
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) {
284 p_CB->first = 0;
285 }
286 }
287 else {
288 os_error (OS_ERR_MBX_OVF);
289 }
290 }
291 }
292
293 /*----------------------------------------------------------------------------
294 * end of file
295 *---------------------------------------------------------------------------*/
296
Imprint / Impressum