ソースの整理中ですが、利用はできます。

Dependencies:   EthernetInterface HttpServer TextLCD mbed-rpc mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
yueee_yt
Date:
Wed Mar 12 04:39:15 2014 +0000
Revision:
2:14b689a85306
Parent:
0:7766f6712673
bug fix

Who changed what in which revision?

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