Control a robot over the internet using UDP and a Ethernet interface.

Dependencies:   EthernetInterface Motor TextLCD mbed-rtos mbed Socket lwip-eth lwip-sys lwip

Committer:
apatel336
Date:
Thu Oct 17 13:26:38 2013 +0000
Revision:
0:1496281373a5
Initial Release

Who changed what in which revision?

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