Control of mbed using OSC. Based on code from the Make Controller. Right now you can turn the onboard LEDs on/off and toggle 8 digital out pins. More I/O will be done in the future.

Dependencies:   mbed

Committer:
pehrhovey
Date:
Wed Mar 17 03:17:38 2010 +0000
Revision:
0:439354122597

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pehrhovey 0:439354122597 1 /**
pehrhovey 0:439354122597 2 * @file
pehrhovey 0:439354122597 3 * Ethernet Interface Skeleton
pehrhovey 0:439354122597 4 *
pehrhovey 0:439354122597 5 */
pehrhovey 0:439354122597 6
pehrhovey 0:439354122597 7 /*
pehrhovey 0:439354122597 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
pehrhovey 0:439354122597 9 * All rights reserved.
pehrhovey 0:439354122597 10 *
pehrhovey 0:439354122597 11 * Redistribution and use in source and binary forms, with or without modification,
pehrhovey 0:439354122597 12 * are permitted provided that the following conditions are met:
pehrhovey 0:439354122597 13 *
pehrhovey 0:439354122597 14 * 1. Redistributions of source code must retain the above copyright notice,
pehrhovey 0:439354122597 15 * this list of conditions and the following disclaimer.
pehrhovey 0:439354122597 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
pehrhovey 0:439354122597 17 * this list of conditions and the following disclaimer in the documentation
pehrhovey 0:439354122597 18 * and/or other materials provided with the distribution.
pehrhovey 0:439354122597 19 * 3. The name of the author may not be used to endorse or promote products
pehrhovey 0:439354122597 20 * derived from this software without specific prior written permission.
pehrhovey 0:439354122597 21 *
pehrhovey 0:439354122597 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
pehrhovey 0:439354122597 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
pehrhovey 0:439354122597 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
pehrhovey 0:439354122597 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
pehrhovey 0:439354122597 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
pehrhovey 0:439354122597 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
pehrhovey 0:439354122597 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
pehrhovey 0:439354122597 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
pehrhovey 0:439354122597 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
pehrhovey 0:439354122597 31 * OF SUCH DAMAGE.
pehrhovey 0:439354122597 32 *
pehrhovey 0:439354122597 33 * This file is part of the lwIP TCP/IP stack.
pehrhovey 0:439354122597 34 *
pehrhovey 0:439354122597 35 * Author: Adam Dunkels <adam@sics.se>
pehrhovey 0:439354122597 36 *
pehrhovey 0:439354122597 37 */
pehrhovey 0:439354122597 38
pehrhovey 0:439354122597 39 /*
pehrhovey 0:439354122597 40 * This file is a skeleton for developing Ethernet network interface
pehrhovey 0:439354122597 41 * drivers for lwIP. Add code to the low_level functions and do a
pehrhovey 0:439354122597 42 * search-and-replace for the word "ethernetif" to replace it with
pehrhovey 0:439354122597 43 * something that better describes your network interface.
pehrhovey 0:439354122597 44 */
pehrhovey 0:439354122597 45
pehrhovey 0:439354122597 46 #include "lwip/opt.h"
pehrhovey 0:439354122597 47
pehrhovey 0:439354122597 48 #if 0 /* don't build, this is only a skeleton, see previous comment */
pehrhovey 0:439354122597 49
pehrhovey 0:439354122597 50 #include "lwip/def.h"
pehrhovey 0:439354122597 51 #include "lwip/mem.h"
pehrhovey 0:439354122597 52 #include "lwip/pbuf.h"
pehrhovey 0:439354122597 53 #include "lwip/sys.h"
pehrhovey 0:439354122597 54 #include <lwip/stats.h>
pehrhovey 0:439354122597 55 #include <lwip/snmp.h>
pehrhovey 0:439354122597 56 #include "netif/etharp.h"
pehrhovey 0:439354122597 57 #include "netif/ppp_oe.h"
pehrhovey 0:439354122597 58
pehrhovey 0:439354122597 59 /* Define those to better describe your network interface. */
pehrhovey 0:439354122597 60 #define IFNAME0 'e'
pehrhovey 0:439354122597 61 #define IFNAME1 'n'
pehrhovey 0:439354122597 62
pehrhovey 0:439354122597 63 /**
pehrhovey 0:439354122597 64 * Helper struct to hold private data used to operate your ethernet interface.
pehrhovey 0:439354122597 65 * Keeping the ethernet address of the MAC in this struct is not necessary
pehrhovey 0:439354122597 66 * as it is already kept in the struct netif.
pehrhovey 0:439354122597 67 * But this is only an example, anyway...
pehrhovey 0:439354122597 68 */
pehrhovey 0:439354122597 69 struct ethernetif {
pehrhovey 0:439354122597 70 struct eth_addr *ethaddr;
pehrhovey 0:439354122597 71 /* Add whatever per-interface state that is needed here. */
pehrhovey 0:439354122597 72 };
pehrhovey 0:439354122597 73
pehrhovey 0:439354122597 74 /* Forward declarations. */
pehrhovey 0:439354122597 75 static void ethernetif_input(struct netif *netif);
pehrhovey 0:439354122597 76
pehrhovey 0:439354122597 77 /**
pehrhovey 0:439354122597 78 * In this function, the hardware should be initialized.
pehrhovey 0:439354122597 79 * Called from ethernetif_init().
pehrhovey 0:439354122597 80 *
pehrhovey 0:439354122597 81 * @param netif the already initialized lwip network interface structure
pehrhovey 0:439354122597 82 * for this ethernetif
pehrhovey 0:439354122597 83 */
pehrhovey 0:439354122597 84 static void
pehrhovey 0:439354122597 85 low_level_init(struct netif *netif)
pehrhovey 0:439354122597 86 {
pehrhovey 0:439354122597 87 struct ethernetif *ethernetif = netif->state;
pehrhovey 0:439354122597 88
pehrhovey 0:439354122597 89 /* set MAC hardware address length */
pehrhovey 0:439354122597 90 netif->hwaddr_len = ETHARP_HWADDR_LEN;
pehrhovey 0:439354122597 91
pehrhovey 0:439354122597 92 /* set MAC hardware address */
pehrhovey 0:439354122597 93 netif->hwaddr[0] = ;
pehrhovey 0:439354122597 94 ...
pehrhovey 0:439354122597 95 netif->hwaddr[5] = ;
pehrhovey 0:439354122597 96
pehrhovey 0:439354122597 97 /* maximum transfer unit */
pehrhovey 0:439354122597 98 netif->mtu = 1500;
pehrhovey 0:439354122597 99
pehrhovey 0:439354122597 100 /* device capabilities */
pehrhovey 0:439354122597 101 /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
pehrhovey 0:439354122597 102 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
pehrhovey 0:439354122597 103
pehrhovey 0:439354122597 104 /* Do whatever else is needed to initialize interface. */
pehrhovey 0:439354122597 105 }
pehrhovey 0:439354122597 106
pehrhovey 0:439354122597 107 /**
pehrhovey 0:439354122597 108 * This function should do the actual transmission of the packet. The packet is
pehrhovey 0:439354122597 109 * contained in the pbuf that is passed to the function. This pbuf
pehrhovey 0:439354122597 110 * might be chained.
pehrhovey 0:439354122597 111 *
pehrhovey 0:439354122597 112 * @param netif the lwip network interface structure for this ethernetif
pehrhovey 0:439354122597 113 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
pehrhovey 0:439354122597 114 * @return ERR_OK if the packet could be sent
pehrhovey 0:439354122597 115 * an err_t value if the packet couldn't be sent
pehrhovey 0:439354122597 116 *
pehrhovey 0:439354122597 117 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
pehrhovey 0:439354122597 118 * strange results. You might consider waiting for space in the DMA queue
pehrhovey 0:439354122597 119 * to become availale since the stack doesn't retry to send a packet
pehrhovey 0:439354122597 120 * dropped because of memory failure (except for the TCP timers).
pehrhovey 0:439354122597 121 */
pehrhovey 0:439354122597 122
pehrhovey 0:439354122597 123 static err_t
pehrhovey 0:439354122597 124 low_level_output(struct netif *netif, struct pbuf *p)
pehrhovey 0:439354122597 125 {
pehrhovey 0:439354122597 126 struct ethernetif *ethernetif = netif->state;
pehrhovey 0:439354122597 127 struct pbuf *q;
pehrhovey 0:439354122597 128
pehrhovey 0:439354122597 129 initiate transfer();
pehrhovey 0:439354122597 130
pehrhovey 0:439354122597 131 #if ETH_PAD_SIZE
pehrhovey 0:439354122597 132 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
pehrhovey 0:439354122597 133 #endif
pehrhovey 0:439354122597 134
pehrhovey 0:439354122597 135 for(q = p; q != NULL; q = q->next) {
pehrhovey 0:439354122597 136 /* Send the data from the pbuf to the interface, one pbuf at a
pehrhovey 0:439354122597 137 time. The size of the data in each pbuf is kept in the ->len
pehrhovey 0:439354122597 138 variable. */
pehrhovey 0:439354122597 139 send data from(q->payload, q->len);
pehrhovey 0:439354122597 140 }
pehrhovey 0:439354122597 141
pehrhovey 0:439354122597 142 signal that packet should be sent();
pehrhovey 0:439354122597 143
pehrhovey 0:439354122597 144 #if ETH_PAD_SIZE
pehrhovey 0:439354122597 145 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
pehrhovey 0:439354122597 146 #endif
pehrhovey 0:439354122597 147
pehrhovey 0:439354122597 148 LINK_STATS_INC(link.xmit);
pehrhovey 0:439354122597 149
pehrhovey 0:439354122597 150 return ERR_OK;
pehrhovey 0:439354122597 151 }
pehrhovey 0:439354122597 152
pehrhovey 0:439354122597 153 /**
pehrhovey 0:439354122597 154 * Should allocate a pbuf and transfer the bytes of the incoming
pehrhovey 0:439354122597 155 * packet from the interface into the pbuf.
pehrhovey 0:439354122597 156 *
pehrhovey 0:439354122597 157 * @param netif the lwip network interface structure for this ethernetif
pehrhovey 0:439354122597 158 * @return a pbuf filled with the received packet (including MAC header)
pehrhovey 0:439354122597 159 * NULL on memory error
pehrhovey 0:439354122597 160 */
pehrhovey 0:439354122597 161 static struct pbuf *
pehrhovey 0:439354122597 162 low_level_input(struct netif *netif)
pehrhovey 0:439354122597 163 {
pehrhovey 0:439354122597 164 struct ethernetif *ethernetif = netif->state;
pehrhovey 0:439354122597 165 struct pbuf *p, *q;
pehrhovey 0:439354122597 166 u16_t len;
pehrhovey 0:439354122597 167
pehrhovey 0:439354122597 168 /* Obtain the size of the packet and put it into the "len"
pehrhovey 0:439354122597 169 variable. */
pehrhovey 0:439354122597 170 len = ;
pehrhovey 0:439354122597 171
pehrhovey 0:439354122597 172 #if ETH_PAD_SIZE
pehrhovey 0:439354122597 173 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
pehrhovey 0:439354122597 174 #endif
pehrhovey 0:439354122597 175
pehrhovey 0:439354122597 176 /* We allocate a pbuf chain of pbufs from the pool. */
pehrhovey 0:439354122597 177 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
pehrhovey 0:439354122597 178
pehrhovey 0:439354122597 179 if (p != NULL) {
pehrhovey 0:439354122597 180
pehrhovey 0:439354122597 181 #if ETH_PAD_SIZE
pehrhovey 0:439354122597 182 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
pehrhovey 0:439354122597 183 #endif
pehrhovey 0:439354122597 184
pehrhovey 0:439354122597 185 /* We iterate over the pbuf chain until we have read the entire
pehrhovey 0:439354122597 186 * packet into the pbuf. */
pehrhovey 0:439354122597 187 for(q = p; q != NULL; q = q->next) {
pehrhovey 0:439354122597 188 /* Read enough bytes to fill this pbuf in the chain. The
pehrhovey 0:439354122597 189 * available data in the pbuf is given by the q->len
pehrhovey 0:439354122597 190 * variable.
pehrhovey 0:439354122597 191 * This does not necessarily have to be a memcpy, you can also preallocate
pehrhovey 0:439354122597 192 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
pehrhovey 0:439354122597 193 * actually received size. In this case, ensure the tot_len member of the
pehrhovey 0:439354122597 194 * pbuf is the sum of the chained pbuf len members.
pehrhovey 0:439354122597 195 */
pehrhovey 0:439354122597 196 read data into(q->payload, q->len);
pehrhovey 0:439354122597 197 }
pehrhovey 0:439354122597 198 acknowledge that packet has been read();
pehrhovey 0:439354122597 199
pehrhovey 0:439354122597 200 #if ETH_PAD_SIZE
pehrhovey 0:439354122597 201 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
pehrhovey 0:439354122597 202 #endif
pehrhovey 0:439354122597 203
pehrhovey 0:439354122597 204 LINK_STATS_INC(link.recv);
pehrhovey 0:439354122597 205 } else {
pehrhovey 0:439354122597 206 drop packet();
pehrhovey 0:439354122597 207 LINK_STATS_INC(link.memerr);
pehrhovey 0:439354122597 208 LINK_STATS_INC(link.drop);
pehrhovey 0:439354122597 209 }
pehrhovey 0:439354122597 210
pehrhovey 0:439354122597 211 return p;
pehrhovey 0:439354122597 212 }
pehrhovey 0:439354122597 213
pehrhovey 0:439354122597 214 /**
pehrhovey 0:439354122597 215 * This function should be called when a packet is ready to be read
pehrhovey 0:439354122597 216 * from the interface. It uses the function low_level_input() that
pehrhovey 0:439354122597 217 * should handle the actual reception of bytes from the network
pehrhovey 0:439354122597 218 * interface. Then the type of the received packet is determined and
pehrhovey 0:439354122597 219 * the appropriate input function is called.
pehrhovey 0:439354122597 220 *
pehrhovey 0:439354122597 221 * @param netif the lwip network interface structure for this ethernetif
pehrhovey 0:439354122597 222 */
pehrhovey 0:439354122597 223 static void
pehrhovey 0:439354122597 224 ethernetif_input(struct netif *netif)
pehrhovey 0:439354122597 225 {
pehrhovey 0:439354122597 226 struct ethernetif *ethernetif;
pehrhovey 0:439354122597 227 struct eth_hdr *ethhdr;
pehrhovey 0:439354122597 228 struct pbuf *p;
pehrhovey 0:439354122597 229
pehrhovey 0:439354122597 230 ethernetif = netif->state;
pehrhovey 0:439354122597 231
pehrhovey 0:439354122597 232 /* move received packet into a new pbuf */
pehrhovey 0:439354122597 233 p = low_level_input(netif);
pehrhovey 0:439354122597 234 /* no packet could be read, silently ignore this */
pehrhovey 0:439354122597 235 if (p == NULL) return;
pehrhovey 0:439354122597 236 /* points to packet payload, which starts with an Ethernet header */
pehrhovey 0:439354122597 237 ethhdr = p->payload;
pehrhovey 0:439354122597 238
pehrhovey 0:439354122597 239 switch (htons(ethhdr->type)) {
pehrhovey 0:439354122597 240 /* IP or ARP packet? */
pehrhovey 0:439354122597 241 case ETHTYPE_IP:
pehrhovey 0:439354122597 242 case ETHTYPE_ARP:
pehrhovey 0:439354122597 243 #if PPPOE_SUPPORT
pehrhovey 0:439354122597 244 /* PPPoE packet? */
pehrhovey 0:439354122597 245 case ETHTYPE_PPPOEDISC:
pehrhovey 0:439354122597 246 case ETHTYPE_PPPOE:
pehrhovey 0:439354122597 247 #endif /* PPPOE_SUPPORT */
pehrhovey 0:439354122597 248 /* full packet send to tcpip_thread to process */
pehrhovey 0:439354122597 249 if (netif->input(p, netif)!=ERR_OK)
pehrhovey 0:439354122597 250 { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
pehrhovey 0:439354122597 251 pbuf_free(p);
pehrhovey 0:439354122597 252 p = NULL;
pehrhovey 0:439354122597 253 }
pehrhovey 0:439354122597 254 break;
pehrhovey 0:439354122597 255
pehrhovey 0:439354122597 256 default:
pehrhovey 0:439354122597 257 pbuf_free(p);
pehrhovey 0:439354122597 258 p = NULL;
pehrhovey 0:439354122597 259 break;
pehrhovey 0:439354122597 260 }
pehrhovey 0:439354122597 261 }
pehrhovey 0:439354122597 262
pehrhovey 0:439354122597 263 /**
pehrhovey 0:439354122597 264 * Should be called at the beginning of the program to set up the
pehrhovey 0:439354122597 265 * network interface. It calls the function low_level_init() to do the
pehrhovey 0:439354122597 266 * actual setup of the hardware.
pehrhovey 0:439354122597 267 *
pehrhovey 0:439354122597 268 * This function should be passed as a parameter to netif_add().
pehrhovey 0:439354122597 269 *
pehrhovey 0:439354122597 270 * @param netif the lwip network interface structure for this ethernetif
pehrhovey 0:439354122597 271 * @return ERR_OK if the loopif is initialized
pehrhovey 0:439354122597 272 * ERR_MEM if private data couldn't be allocated
pehrhovey 0:439354122597 273 * any other err_t on error
pehrhovey 0:439354122597 274 */
pehrhovey 0:439354122597 275 err_t
pehrhovey 0:439354122597 276 ethernetif_init(struct netif *netif)
pehrhovey 0:439354122597 277 {
pehrhovey 0:439354122597 278 struct ethernetif *ethernetif;
pehrhovey 0:439354122597 279
pehrhovey 0:439354122597 280 LWIP_ASSERT("netif != NULL", (netif != NULL));
pehrhovey 0:439354122597 281
pehrhovey 0:439354122597 282 ethernetif = mem_malloc(sizeof(struct ethernetif));
pehrhovey 0:439354122597 283 if (ethernetif == NULL) {
pehrhovey 0:439354122597 284 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
pehrhovey 0:439354122597 285 return ERR_MEM;
pehrhovey 0:439354122597 286 }
pehrhovey 0:439354122597 287
pehrhovey 0:439354122597 288 #if LWIP_NETIF_HOSTNAME
pehrhovey 0:439354122597 289 /* Initialize interface hostname */
pehrhovey 0:439354122597 290 netif->hostname = "lwip";
pehrhovey 0:439354122597 291 #endif /* LWIP_NETIF_HOSTNAME */
pehrhovey 0:439354122597 292
pehrhovey 0:439354122597 293 /*
pehrhovey 0:439354122597 294 * Initialize the snmp variables and counters inside the struct netif.
pehrhovey 0:439354122597 295 * The last argument should be replaced with your link speed, in units
pehrhovey 0:439354122597 296 * of bits per second.
pehrhovey 0:439354122597 297 */
pehrhovey 0:439354122597 298 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
pehrhovey 0:439354122597 299
pehrhovey 0:439354122597 300 netif->state = ethernetif;
pehrhovey 0:439354122597 301 netif->name[0] = IFNAME0;
pehrhovey 0:439354122597 302 netif->name[1] = IFNAME1;
pehrhovey 0:439354122597 303 /* We directly use etharp_output() here to save a function call.
pehrhovey 0:439354122597 304 * You can instead declare your own function an call etharp_output()
pehrhovey 0:439354122597 305 * from it if you have to do some checks before sending (e.g. if link
pehrhovey 0:439354122597 306 * is available...) */
pehrhovey 0:439354122597 307 netif->output = etharp_output;
pehrhovey 0:439354122597 308 netif->linkoutput = low_level_output;
pehrhovey 0:439354122597 309
pehrhovey 0:439354122597 310 ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
pehrhovey 0:439354122597 311
pehrhovey 0:439354122597 312 /* initialize the hardware */
pehrhovey 0:439354122597 313 low_level_init(netif);
pehrhovey 0:439354122597 314
pehrhovey 0:439354122597 315 return ERR_OK;
pehrhovey 0:439354122597 316 }
pehrhovey 0:439354122597 317
pehrhovey 0:439354122597 318 #endif /* 0 */