Fork of official mbed Real Time Operating System based on the RTX implementation of the CMSIS-RTOS API open standard.

Dependents:   Final_Coffee_Roaster

Fork of mbed-rtos by mbed official

Committer:
mbed_official
Date:
Mon Oct 06 10:00:12 2014 +0100
Revision:
43:aaa1b2c7c64c
Parent:
31:015df9e602b6
Synchronized with git revision b30176a071a49d95914b97d2ab98240f3a1e2cae

Full URL: https://github.com/mbedmicro/mbed/commit/b30176a071a49d95914b97d2ab98240f3a1e2cae/

Platform: LPC824 - new platform addition

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 6:350b53afb889 1 /*----------------------------------------------------------------------------
emilmont 6:350b53afb889 2 * RL-ARM - RTX
emilmont 6:350b53afb889 3 *----------------------------------------------------------------------------
emilmont 6:350b53afb889 4 * Name: RT_MAILBOX.C
emilmont 6:350b53afb889 5 * Purpose: Implements waits and wake-ups for mailbox messages
emilmont 6:350b53afb889 6 * Rev.: V4.60
emilmont 6:350b53afb889 7 *----------------------------------------------------------------------------
emilmont 6:350b53afb889 8 *
emilmont 6:350b53afb889 9 * Copyright (c) 1999-2009 KEIL, 2009-2012 ARM Germany GmbH
emilmont 6:350b53afb889 10 * All rights reserved.
emilmont 6:350b53afb889 11 * Redistribution and use in source and binary forms, with or without
emilmont 6:350b53afb889 12 * modification, are permitted provided that the following conditions are met:
emilmont 6:350b53afb889 13 * - Redistributions of source code must retain the above copyright
emilmont 6:350b53afb889 14 * notice, this list of conditions and the following disclaimer.
emilmont 6:350b53afb889 15 * - Redistributions in binary form must reproduce the above copyright
emilmont 6:350b53afb889 16 * notice, this list of conditions and the following disclaimer in the
emilmont 6:350b53afb889 17 * documentation and/or other materials provided with the distribution.
mbed_official 31:015df9e602b6 18 * - Neither the name of ARM nor the names of its contributors may be used
mbed_official 31:015df9e602b6 19 * to endorse or promote products derived from this software without
emilmont 6:350b53afb889 20 * specific prior written permission.
emilmont 6:350b53afb889 21 *
mbed_official 31:015df9e602b6 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mbed_official 31:015df9e602b6 23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
emilmont 6:350b53afb889 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
emilmont 6:350b53afb889 25 * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
emilmont 6:350b53afb889 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
mbed_official 31:015df9e602b6 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
mbed_official 31:015df9e602b6 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
mbed_official 31:015df9e602b6 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
mbed_official 31:015df9e602b6 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
emilmont 6:350b53afb889 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
emilmont 6:350b53afb889 32 * POSSIBILITY OF SUCH DAMAGE.
emilmont 6:350b53afb889 33 *---------------------------------------------------------------------------*/
emilmont 6:350b53afb889 34
emilmont 6:350b53afb889 35 #include "rt_TypeDef.h"
emilmont 11:db1fc233faa9 36 #include "RTX_Conf.h"
emilmont 6:350b53afb889 37 #include "rt_System.h"
emilmont 6:350b53afb889 38 #include "rt_List.h"
emilmont 6:350b53afb889 39 #include "rt_Mailbox.h"
emilmont 6:350b53afb889 40 #include "rt_MemBox.h"
emilmont 6:350b53afb889 41 #include "rt_Task.h"
emilmont 6:350b53afb889 42 #include "rt_HAL_CM.h"
emilmont 6:350b53afb889 43
emilmont 6:350b53afb889 44
emilmont 6:350b53afb889 45 /*----------------------------------------------------------------------------
emilmont 6:350b53afb889 46 * Functions
emilmont 6:350b53afb889 47 *---------------------------------------------------------------------------*/
emilmont 6:350b53afb889 48
emilmont 6:350b53afb889 49
emilmont 6:350b53afb889 50 /*--------------------------- rt_mbx_init -----------------------------------*/
emilmont 6:350b53afb889 51
emilmont 6:350b53afb889 52 void rt_mbx_init (OS_ID mailbox, U16 mbx_size) {
emilmont 6:350b53afb889 53 /* Initialize a mailbox */
emilmont 6:350b53afb889 54 P_MCB p_MCB = mailbox;
emilmont 6:350b53afb889 55
emilmont 6:350b53afb889 56 p_MCB->cb_type = MCB;
emilmont 6:350b53afb889 57 p_MCB->state = 0;
emilmont 6:350b53afb889 58 p_MCB->isr_st = 0;
emilmont 6:350b53afb889 59 p_MCB->p_lnk = NULL;
emilmont 6:350b53afb889 60 p_MCB->first = 0;
emilmont 6:350b53afb889 61 p_MCB->last = 0;
emilmont 6:350b53afb889 62 p_MCB->count = 0;
emilmont 6:350b53afb889 63 p_MCB->size = (mbx_size + sizeof(void *) - sizeof(struct OS_MCB)) /
emilmont 6:350b53afb889 64 (U32)sizeof (void *);
emilmont 6:350b53afb889 65 }
emilmont 6:350b53afb889 66
emilmont 6:350b53afb889 67
emilmont 6:350b53afb889 68 /*--------------------------- rt_mbx_send -----------------------------------*/
emilmont 6:350b53afb889 69
emilmont 6:350b53afb889 70 OS_RESULT rt_mbx_send (OS_ID mailbox, void *p_msg, U16 timeout) {
emilmont 6:350b53afb889 71 /* Send message to a mailbox */
emilmont 6:350b53afb889 72 P_MCB p_MCB = mailbox;
emilmont 6:350b53afb889 73 P_TCB p_TCB;
emilmont 6:350b53afb889 74
emilmont 6:350b53afb889 75 if ((p_MCB->p_lnk != NULL) && (p_MCB->state == 1)) {
emilmont 6:350b53afb889 76 /* A task is waiting for message */
emilmont 6:350b53afb889 77 p_TCB = rt_get_first ((P_XCB)p_MCB);
emilmont 6:350b53afb889 78 #ifdef __CMSIS_RTOS
emilmont 6:350b53afb889 79 rt_ret_val2(p_TCB, 0x10/*osEventMessage*/, (U32)p_msg);
emilmont 6:350b53afb889 80 #else
emilmont 6:350b53afb889 81 *p_TCB->msg = p_msg;
emilmont 6:350b53afb889 82 rt_ret_val (p_TCB, OS_R_MBX);
emilmont 6:350b53afb889 83 #endif
emilmont 6:350b53afb889 84 rt_rmv_dly (p_TCB);
emilmont 6:350b53afb889 85 rt_dispatch (p_TCB);
emilmont 6:350b53afb889 86 }
emilmont 6:350b53afb889 87 else {
emilmont 6:350b53afb889 88 /* Store message in mailbox queue */
emilmont 6:350b53afb889 89 if (p_MCB->count == p_MCB->size) {
emilmont 6:350b53afb889 90 /* No free message entry, wait for one. If message queue is full, */
emilmont 6:350b53afb889 91 /* then no task is waiting for message. The 'p_MCB->p_lnk' list */
emilmont 6:350b53afb889 92 /* pointer can now be reused for send message waits task list. */
emilmont 6:350b53afb889 93 if (timeout == 0) {
emilmont 6:350b53afb889 94 return (OS_R_TMO);
emilmont 6:350b53afb889 95 }
emilmont 6:350b53afb889 96 if (p_MCB->p_lnk != NULL) {
emilmont 6:350b53afb889 97 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
emilmont 6:350b53afb889 98 }
emilmont 6:350b53afb889 99 else {
emilmont 6:350b53afb889 100 p_MCB->p_lnk = os_tsk.run;
emilmont 6:350b53afb889 101 os_tsk.run->p_lnk = NULL;
emilmont 6:350b53afb889 102 os_tsk.run->p_rlnk = (P_TCB)p_MCB;
mbed_official 31:015df9e602b6 103 /* Task is waiting to send a message */
emilmont 6:350b53afb889 104 p_MCB->state = 2;
emilmont 6:350b53afb889 105 }
emilmont 6:350b53afb889 106 os_tsk.run->msg = p_msg;
emilmont 6:350b53afb889 107 rt_block (timeout, WAIT_MBX);
emilmont 6:350b53afb889 108 return (OS_R_TMO);
emilmont 6:350b53afb889 109 }
emilmont 6:350b53afb889 110 /* Yes, there is a free entry in a mailbox. */
emilmont 6:350b53afb889 111 p_MCB->msg[p_MCB->first] = p_msg;
emilmont 6:350b53afb889 112 rt_inc (&p_MCB->count);
emilmont 6:350b53afb889 113 if (++p_MCB->first == p_MCB->size) {
emilmont 6:350b53afb889 114 p_MCB->first = 0;
emilmont 6:350b53afb889 115 }
emilmont 6:350b53afb889 116 }
emilmont 6:350b53afb889 117 return (OS_R_OK);
emilmont 6:350b53afb889 118 }
emilmont 6:350b53afb889 119
emilmont 6:350b53afb889 120
emilmont 6:350b53afb889 121 /*--------------------------- rt_mbx_wait -----------------------------------*/
emilmont 6:350b53afb889 122
emilmont 6:350b53afb889 123 OS_RESULT rt_mbx_wait (OS_ID mailbox, void **message, U16 timeout) {
emilmont 6:350b53afb889 124 /* Receive a message; possibly wait for it */
emilmont 6:350b53afb889 125 P_MCB p_MCB = mailbox;
emilmont 6:350b53afb889 126 P_TCB p_TCB;
emilmont 6:350b53afb889 127
emilmont 6:350b53afb889 128 /* If a message is available in the fifo buffer */
emilmont 6:350b53afb889 129 /* remove it from the fifo buffer and return. */
emilmont 6:350b53afb889 130 if (p_MCB->count) {
emilmont 6:350b53afb889 131 *message = p_MCB->msg[p_MCB->last];
emilmont 6:350b53afb889 132 if (++p_MCB->last == p_MCB->size) {
emilmont 6:350b53afb889 133 p_MCB->last = 0;
emilmont 6:350b53afb889 134 }
emilmont 6:350b53afb889 135 if ((p_MCB->p_lnk != NULL) && (p_MCB->state == 2)) {
emilmont 6:350b53afb889 136 /* A task is waiting to send message */
emilmont 6:350b53afb889 137 p_TCB = rt_get_first ((P_XCB)p_MCB);
emilmont 6:350b53afb889 138 #ifdef __CMSIS_RTOS
emilmont 6:350b53afb889 139 rt_ret_val(p_TCB, 0/*osOK*/);
emilmont 6:350b53afb889 140 #else
emilmont 6:350b53afb889 141 rt_ret_val(p_TCB, OS_R_OK);
emilmont 6:350b53afb889 142 #endif
emilmont 6:350b53afb889 143 p_MCB->msg[p_MCB->first] = p_TCB->msg;
emilmont 6:350b53afb889 144 if (++p_MCB->first == p_MCB->size) {
emilmont 6:350b53afb889 145 p_MCB->first = 0;
emilmont 6:350b53afb889 146 }
emilmont 6:350b53afb889 147 rt_rmv_dly (p_TCB);
emilmont 6:350b53afb889 148 rt_dispatch (p_TCB);
emilmont 6:350b53afb889 149 }
emilmont 6:350b53afb889 150 else {
emilmont 6:350b53afb889 151 rt_dec (&p_MCB->count);
emilmont 6:350b53afb889 152 }
emilmont 6:350b53afb889 153 return (OS_R_OK);
emilmont 6:350b53afb889 154 }
emilmont 6:350b53afb889 155 /* No message available: wait for one */
emilmont 6:350b53afb889 156 if (timeout == 0) {
emilmont 6:350b53afb889 157 return (OS_R_TMO);
emilmont 6:350b53afb889 158 }
emilmont 6:350b53afb889 159 if (p_MCB->p_lnk != NULL) {
emilmont 6:350b53afb889 160 rt_put_prio ((P_XCB)p_MCB, os_tsk.run);
emilmont 6:350b53afb889 161 }
emilmont 6:350b53afb889 162 else {
emilmont 6:350b53afb889 163 p_MCB->p_lnk = os_tsk.run;
emilmont 6:350b53afb889 164 os_tsk.run->p_lnk = NULL;
emilmont 6:350b53afb889 165 os_tsk.run->p_rlnk = (P_TCB)p_MCB;
mbed_official 31:015df9e602b6 166 /* Task is waiting to receive a message */
emilmont 6:350b53afb889 167 p_MCB->state = 1;
emilmont 6:350b53afb889 168 }
emilmont 6:350b53afb889 169 rt_block(timeout, WAIT_MBX);
emilmont 6:350b53afb889 170 #ifndef __CMSIS_RTOS
emilmont 6:350b53afb889 171 os_tsk.run->msg = message;
emilmont 6:350b53afb889 172 #endif
emilmont 6:350b53afb889 173 return (OS_R_TMO);
emilmont 6:350b53afb889 174 }
emilmont 6:350b53afb889 175
emilmont 6:350b53afb889 176
emilmont 6:350b53afb889 177 /*--------------------------- rt_mbx_check ----------------------------------*/
emilmont 6:350b53afb889 178
emilmont 6:350b53afb889 179 OS_RESULT rt_mbx_check (OS_ID mailbox) {
emilmont 6:350b53afb889 180 /* Check for free space in a mailbox. Returns the number of messages */
emilmont 6:350b53afb889 181 /* that can be stored to a mailbox. It returns 0 when mailbox is full. */
emilmont 6:350b53afb889 182 P_MCB p_MCB = mailbox;
emilmont 6:350b53afb889 183
emilmont 6:350b53afb889 184 return (p_MCB->size - p_MCB->count);
emilmont 6:350b53afb889 185 }
emilmont 6:350b53afb889 186
emilmont 6:350b53afb889 187
emilmont 6:350b53afb889 188 /*--------------------------- isr_mbx_send ----------------------------------*/
emilmont 6:350b53afb889 189
emilmont 6:350b53afb889 190 void isr_mbx_send (OS_ID mailbox, void *p_msg) {
emilmont 6:350b53afb889 191 /* Same function as "os_mbx_send", but to be called by ISRs. */
emilmont 6:350b53afb889 192 P_MCB p_MCB = mailbox;
emilmont 6:350b53afb889 193
emilmont 6:350b53afb889 194 rt_psq_enq (p_MCB, (U32)p_msg);
emilmont 6:350b53afb889 195 rt_psh_req ();
emilmont 6:350b53afb889 196 }
emilmont 6:350b53afb889 197
emilmont 6:350b53afb889 198
emilmont 6:350b53afb889 199 /*--------------------------- isr_mbx_receive -------------------------------*/
emilmont 6:350b53afb889 200
emilmont 6:350b53afb889 201 OS_RESULT isr_mbx_receive (OS_ID mailbox, void **message) {
emilmont 6:350b53afb889 202 /* Receive a message in the interrupt function. The interrupt function */
emilmont 6:350b53afb889 203 /* should not wait for a message since this would block the rtx os. */
emilmont 6:350b53afb889 204 P_MCB p_MCB = mailbox;
emilmont 6:350b53afb889 205
emilmont 6:350b53afb889 206 if (p_MCB->count) {
emilmont 6:350b53afb889 207 /* A message is available in the fifo buffer. */
emilmont 6:350b53afb889 208 *message = p_MCB->msg[p_MCB->last];
emilmont 6:350b53afb889 209 if (p_MCB->state == 2) {
emilmont 6:350b53afb889 210 /* A task is locked waiting to send message */
emilmont 6:350b53afb889 211 rt_psq_enq (p_MCB, 0);
emilmont 6:350b53afb889 212 rt_psh_req ();
emilmont 6:350b53afb889 213 }
emilmont 6:350b53afb889 214 rt_dec (&p_MCB->count);
emilmont 6:350b53afb889 215 if (++p_MCB->last == p_MCB->size) {
emilmont 6:350b53afb889 216 p_MCB->last = 0;
emilmont 6:350b53afb889 217 }
emilmont 6:350b53afb889 218 return (OS_R_MBX);
emilmont 6:350b53afb889 219 }
emilmont 6:350b53afb889 220 return (OS_R_OK);
emilmont 6:350b53afb889 221 }
emilmont 6:350b53afb889 222
emilmont 6:350b53afb889 223
emilmont 6:350b53afb889 224 /*--------------------------- rt_mbx_psh ------------------------------------*/
emilmont 6:350b53afb889 225
emilmont 6:350b53afb889 226 void rt_mbx_psh (P_MCB p_CB, void *p_msg) {
emilmont 6:350b53afb889 227 /* Store the message to the mailbox queue or pass it to task directly. */
emilmont 6:350b53afb889 228 P_TCB p_TCB;
emilmont 6:350b53afb889 229 void *mem;
emilmont 6:350b53afb889 230
emilmont 6:350b53afb889 231 if (p_CB->p_lnk != NULL) switch (p_CB->state) {
emilmont 6:350b53afb889 232 #ifdef __CMSIS_RTOS
emilmont 6:350b53afb889 233 case 3:
emilmont 6:350b53afb889 234 /* Task is waiting to allocate memory, remove it from the waiting list */
emilmont 6:350b53afb889 235 mem = rt_alloc_box(p_msg);
emilmont 6:350b53afb889 236 if (mem == NULL) break;
emilmont 6:350b53afb889 237 p_TCB = rt_get_first ((P_XCB)p_CB);
emilmont 6:350b53afb889 238 rt_ret_val(p_TCB, (U32)mem);
emilmont 6:350b53afb889 239 p_TCB->state = READY;
emilmont 6:350b53afb889 240 rt_rmv_dly (p_TCB);
emilmont 6:350b53afb889 241 rt_put_prio (&os_rdy, p_TCB);
emilmont 6:350b53afb889 242 break;
emilmont 6:350b53afb889 243 #endif
emilmont 6:350b53afb889 244 case 2:
emilmont 6:350b53afb889 245 /* Task is waiting to send a message, remove it from the waiting list */
emilmont 6:350b53afb889 246 p_TCB = rt_get_first ((P_XCB)p_CB);
emilmont 6:350b53afb889 247 #ifdef __CMSIS_RTOS
emilmont 6:350b53afb889 248 rt_ret_val(p_TCB, 0/*osOK*/);
emilmont 6:350b53afb889 249 #else
emilmont 6:350b53afb889 250 rt_ret_val(p_TCB, OS_R_OK);
emilmont 6:350b53afb889 251 #endif
emilmont 6:350b53afb889 252 p_CB->msg[p_CB->first] = p_TCB->msg;
emilmont 6:350b53afb889 253 rt_inc (&p_CB->count);
emilmont 6:350b53afb889 254 if (++p_CB->first == p_CB->size) {
emilmont 6:350b53afb889 255 p_CB->first = 0;
emilmont 6:350b53afb889 256 }
emilmont 6:350b53afb889 257 p_TCB->state = READY;
emilmont 6:350b53afb889 258 rt_rmv_dly (p_TCB);
emilmont 6:350b53afb889 259 rt_put_prio (&os_rdy, p_TCB);
emilmont 6:350b53afb889 260 break;
emilmont 6:350b53afb889 261 case 1:
emilmont 6:350b53afb889 262 /* Task is waiting for a message, pass the message to the task directly */
emilmont 6:350b53afb889 263 p_TCB = rt_get_first ((P_XCB)p_CB);
emilmont 6:350b53afb889 264 #ifdef __CMSIS_RTOS
emilmont 6:350b53afb889 265 rt_ret_val2(p_TCB, 0x10/*osEventMessage*/, (U32)p_msg);
emilmont 6:350b53afb889 266 #else
emilmont 6:350b53afb889 267 *p_TCB->msg = p_msg;
emilmont 6:350b53afb889 268 rt_ret_val (p_TCB, OS_R_MBX);
emilmont 6:350b53afb889 269 #endif
emilmont 6:350b53afb889 270 p_TCB->state = READY;
emilmont 6:350b53afb889 271 rt_rmv_dly (p_TCB);
emilmont 6:350b53afb889 272 rt_put_prio (&os_rdy, p_TCB);
emilmont 6:350b53afb889 273 break;
emilmont 6:350b53afb889 274 } else {
emilmont 6:350b53afb889 275 /* No task is waiting for a message, store it to the mailbox queue */
emilmont 6:350b53afb889 276 if (p_CB->count < p_CB->size) {
emilmont 6:350b53afb889 277 p_CB->msg[p_CB->first] = p_msg;
emilmont 6:350b53afb889 278 rt_inc (&p_CB->count);
emilmont 6:350b53afb889 279 if (++p_CB->first == p_CB->size) {
emilmont 6:350b53afb889 280 p_CB->first = 0;
emilmont 6:350b53afb889 281 }
emilmont 6:350b53afb889 282 }
emilmont 6:350b53afb889 283 else {
emilmont 6:350b53afb889 284 os_error (OS_ERR_MBX_OVF);
emilmont 6:350b53afb889 285 }
emilmont 6:350b53afb889 286 }
emilmont 6:350b53afb889 287 }
emilmont 6:350b53afb889 288
emilmont 6:350b53afb889 289 /*----------------------------------------------------------------------------
emilmont 6:350b53afb889 290 * end of file
emilmont 6:350b53afb889 291 *---------------------------------------------------------------------------*/
emilmont 6:350b53afb889 292