]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/rtos/rtx/TARGET_CORTEX_M/rt_Mailbox.c
Merge commit '1fe4406f374291ab2e86e95a97341fd9c475fcb8'
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / rtos / rtx / TARGET_CORTEX_M / 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_Conf.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 #include "rt_HAL_CM.h"
43
44
45 /*----------------------------------------------------------------------------
46 * Functions
47 *---------------------------------------------------------------------------*/
48
49
50 /*--------------------------- rt_mbx_init -----------------------------------*/
51
52 void rt_mbx_init (OS_ID mailbox, U16 mbx_size) {
53 /* Initialize a mailbox */
54 P_MCB p_MCB = mailbox;
55
56 p_MCB->cb_type = MCB;
57 p_MCB->state = 0;
58 p_MCB->isr_st = 0;
59 p_MCB->p_lnk = NULL;
60 p_MCB->first = 0;
61 p_MCB->last = 0;
62 p_MCB->count = 0;
63 p_MCB->size = (mbx_size + sizeof(void *) - sizeof(struct OS_MCB)) /
64 (U32)sizeof (void *);
65 }
66
67
68 /*--------------------------- rt_mbx_send -----------------------------------*/
69
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;
73 P_TCB p_TCB;
74
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);
78 #ifdef __CMSIS_RTOS
79 rt_ret_val2(p_TCB, 0x10/*osEventMessage*/, (U32)p_msg);
80 #else
81 *p_TCB->msg = p_msg;
82 rt_ret_val (p_TCB, OS_R_MBX);
83 #endif
84 rt_rmv_dly (p_TCB);
85 rt_dispatch (p_TCB);
86 }
87 else {
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. */
93 if (timeout == 0) {
94 return (OS_R_TMO);
95 }
96 if (p_MCB->p_lnk != NULL) {
97 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
98 }
99 else {
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 */
104 p_MCB->state = 2;
105 }
106 os_tsk.run->msg = p_msg;
107 rt_block (timeout, WAIT_MBX);
108 return (OS_R_TMO);
109 }
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) {
114 p_MCB->first = 0;
115 }
116 }
117 return (OS_R_OK);
118 }
119
120
121 /*--------------------------- rt_mbx_wait -----------------------------------*/
122
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;
126 P_TCB p_TCB;
127
128 /* If a message is available in the fifo buffer */
129 /* remove it from the fifo buffer and return. */
130 if (p_MCB->count) {
131 *message = p_MCB->msg[p_MCB->last];
132 if (++p_MCB->last == p_MCB->size) {
133 p_MCB->last = 0;
134 }
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);
138 #ifdef __CMSIS_RTOS
139 rt_ret_val(p_TCB, 0/*osOK*/);
140 #else
141 rt_ret_val(p_TCB, OS_R_OK);
142 #endif
143 p_MCB->msg[p_MCB->first] = p_TCB->msg;
144 if (++p_MCB->first == p_MCB->size) {
145 p_MCB->first = 0;
146 }
147 rt_rmv_dly (p_TCB);
148 rt_dispatch (p_TCB);
149 }
150 else {
151 rt_dec (&p_MCB->count);
152 }
153 return (OS_R_OK);
154 }
155 /* No message available: wait for one */
156 if (timeout == 0) {
157 return (OS_R_TMO);
158 }
159 if (p_MCB->p_lnk != NULL) {
160 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
161 }
162 else {
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 */
167 p_MCB->state = 1;
168 }
169 rt_block(timeout, WAIT_MBX);
170 #ifndef __CMSIS_RTOS
171 os_tsk.run->msg = message;
172 #endif
173 return (OS_R_TMO);
174 }
175
176
177 /*--------------------------- rt_mbx_check ----------------------------------*/
178
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;
183
184 return (p_MCB->size - p_MCB->count);
185 }
186
187
188 /*--------------------------- isr_mbx_send ----------------------------------*/
189
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;
193
194 rt_psq_enq (p_MCB, (U32)p_msg);
195 rt_psh_req ();
196 }
197
198
199 /*--------------------------- isr_mbx_receive -------------------------------*/
200
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;
205
206 if (p_MCB->count) {
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);
212 rt_psh_req ();
213 }
214 rt_dec (&p_MCB->count);
215 if (++p_MCB->last == p_MCB->size) {
216 p_MCB->last = 0;
217 }
218 return (OS_R_MBX);
219 }
220 return (OS_R_OK);
221 }
222
223
224 /*--------------------------- rt_mbx_psh ------------------------------------*/
225
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. */
228 P_TCB p_TCB;
229 void *mem;
230
231 if (p_CB->p_lnk != NULL) switch (p_CB->state) {
232 #ifdef __CMSIS_RTOS
233 case 3:
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;
240 rt_rmv_dly (p_TCB);
241 rt_put_prio (&os_rdy, p_TCB);
242 break;
243 #endif
244 case 2:
245 /* Task is waiting to send a message, remove it from the waiting list */
246 p_TCB = rt_get_first ((P_XCB)p_CB);
247 #ifdef __CMSIS_RTOS
248 rt_ret_val(p_TCB, 0/*osOK*/);
249 #else
250 rt_ret_val(p_TCB, OS_R_OK);
251 #endif
252 p_CB->msg[p_CB->first] = p_TCB->msg;
253 rt_inc (&p_CB->count);
254 if (++p_CB->first == p_CB->size) {
255 p_CB->first = 0;
256 }
257 p_TCB->state = READY;
258 rt_rmv_dly (p_TCB);
259 rt_put_prio (&os_rdy, p_TCB);
260 break;
261 case 1:
262 /* Task is waiting for a message, pass the message to the task directly */
263 p_TCB = rt_get_first ((P_XCB)p_CB);
264 #ifdef __CMSIS_RTOS
265 rt_ret_val2(p_TCB, 0x10/*osEventMessage*/, (U32)p_msg);
266 #else
267 *p_TCB->msg = p_msg;
268 rt_ret_val (p_TCB, OS_R_MBX);
269 #endif
270 p_TCB->state = READY;
271 rt_rmv_dly (p_TCB);
272 rt_put_prio (&os_rdy, p_TCB);
273 break;
274 } else {
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) {
280 p_CB->first = 0;
281 }
282 }
283 else {
284 os_error (OS_ERR_MBX_OVF);
285 }
286 }
287 }
288
289 /*----------------------------------------------------------------------------
290 * end of file
291 *---------------------------------------------------------------------------*/
292
Imprint / Impressum