Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Wed Dec 15 18:01:30 2010 +0000
Revision:
7:4e2468d7d5cb
Parent:
4:966a0265edfc

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
segundo 0:ac1725ba162c 1
segundo 0:ac1725ba162c 2 /*
segundo 0:ac1725ba162c 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
segundo 0:ac1725ba162c 4
segundo 0:ac1725ba162c 5 Permission is hereby granted, free of charge, to any person obtaining a copy
segundo 0:ac1725ba162c 6 of this software and associated documentation files (the "Software"), to deal
segundo 0:ac1725ba162c 7 in the Software without restriction, including without limitation the rights
segundo 0:ac1725ba162c 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
segundo 0:ac1725ba162c 9 copies of the Software, and to permit persons to whom the Software is
segundo 0:ac1725ba162c 10 furnished to do so, subject to the following conditions:
segundo 0:ac1725ba162c 11
segundo 0:ac1725ba162c 12 The above copyright notice and this permission notice shall be included in
segundo 0:ac1725ba162c 13 all copies or substantial portions of the Software.
segundo 0:ac1725ba162c 14
segundo 0:ac1725ba162c 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
segundo 0:ac1725ba162c 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
segundo 0:ac1725ba162c 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
segundo 0:ac1725ba162c 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
segundo 0:ac1725ba162c 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
segundo 0:ac1725ba162c 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
segundo 0:ac1725ba162c 21 THE SOFTWARE.
segundo 0:ac1725ba162c 22 */
segundo 0:ac1725ba162c 23
segundo 0:ac1725ba162c 24 #include "netCfg.h"
segundo 0:ac1725ba162c 25 #if NET_ETH
segundo 0:ac1725ba162c 26
segundo 0:ac1725ba162c 27 #include "mbed.h"
segundo 0:ac1725ba162c 28
segundo 0:ac1725ba162c 29 Ethernet *pEth = NULL;
segundo 0:ac1725ba162c 30 #ifdef __cplusplus
segundo 0:ac1725ba162c 31 extern "C" {
segundo 0:ac1725ba162c 32 #endif
segundo 0:ac1725ba162c 33
segundo 0:ac1725ba162c 34 #include "lwip/opt.h"
segundo 0:ac1725ba162c 35
segundo 0:ac1725ba162c 36 #include "lwip/def.h"
segundo 0:ac1725ba162c 37 #include "lwip/pbuf.h"
segundo 0:ac1725ba162c 38 #include "lwip/sys.h"
segundo 0:ac1725ba162c 39 #include "lwip/stats.h"
segundo 0:ac1725ba162c 40 #include "netif/etharp.h"
segundo 0:ac1725ba162c 41 #include "string.h"
segundo 0:ac1725ba162c 42
segundo 0:ac1725ba162c 43 //#include "eth_drv.h"
segundo 0:ac1725ba162c 44
segundo 0:ac1725ba162c 45 #define IFNAME0 'E'
segundo 0:ac1725ba162c 46 #define IFNAME1 'X'
segundo 0:ac1725ba162c 47
segundo 0:ac1725ba162c 48 #define min(x,y) (((x)<(y))?(x):(y))
segundo 0:ac1725ba162c 49
segundo 0:ac1725ba162c 50 struct netif* eth_netif;
segundo 0:ac1725ba162c 51
segundo 0:ac1725ba162c 52 static err_t eth_output(struct netif *netif, struct pbuf *p) {
segundo 0:ac1725ba162c 53 #if ETH_PAD_SIZE
segundo 0:ac1725ba162c 54 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
segundo 0:ac1725ba162c 55 #endif
segundo 0:ac1725ba162c 56
segundo 0:ac1725ba162c 57 do {
segundo 0:ac1725ba162c 58 pEth->write((const char *)p->payload, p->len);
segundo 0:ac1725ba162c 59 } while((p = p->next)!=NULL);
segundo 0:ac1725ba162c 60
segundo 0:ac1725ba162c 61 pEth->send();
segundo 0:ac1725ba162c 62
segundo 0:ac1725ba162c 63 #if ETH_PAD_SIZE
segundo 0:ac1725ba162c 64 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
segundo 0:ac1725ba162c 65 #endif
segundo 0:ac1725ba162c 66
segundo 0:ac1725ba162c 67 LINK_STATS_INC(link.xmit);
segundo 0:ac1725ba162c 68 return ERR_OK;
segundo 0:ac1725ba162c 69 }
segundo 0:ac1725ba162c 70
segundo 0:ac1725ba162c 71 /*
segundo 0:ac1725ba162c 72 void show(char *buf, int size) {
segundo 0:ac1725ba162c 73 printf("Destination: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
segundo 0:ac1725ba162c 74 buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
segundo 0:ac1725ba162c 75 printf("Source: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
segundo 0:ac1725ba162c 76 buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
segundo 0:ac1725ba162c 77
segundo 0:ac1725ba162c 78 printf("Type %hd\n", htons((short)buf[12]));
segundo 0:ac1725ba162c 79
segundo 0:ac1725ba162c 80 // hexview(buf, size);
segundo 0:ac1725ba162c 81 }
segundo 0:ac1725ba162c 82 */
segundo 0:ac1725ba162c 83
segundo 0:ac1725ba162c 84 void eth_poll() {
segundo 0:ac1725ba162c 85 struct eth_hdr *ethhdr;
segundo 0:ac1725ba162c 86 struct pbuf *frame, *p;
segundo 0:ac1725ba162c 87 int len, read;
segundo 0:ac1725ba162c 88
segundo 0:ac1725ba162c 89 while((len = pEth->receive()) != 0) {
segundo 0:ac1725ba162c 90 frame = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
segundo 0:ac1725ba162c 91 if(frame == NULL) {
segundo 0:ac1725ba162c 92 return;
segundo 0:ac1725ba162c 93 }
segundo 0:ac1725ba162c 94 p = frame;
segundo 0:ac1725ba162c 95 /* no packet could be read, silently ignore this */
segundo 0:ac1725ba162c 96 if (p == NULL) return;
segundo 0:ac1725ba162c 97 do {
segundo 0:ac1725ba162c 98 read = pEth->read((char *)p->payload, p->len);
segundo 0:ac1725ba162c 99 p = p->next;
segundo 0:ac1725ba162c 100 } while(p != NULL && read != 0);
segundo 0:ac1725ba162c 101
segundo 0:ac1725ba162c 102 #if ETH_PAD_SIZE
segundo 0:ac1725ba162c 103 pbuf_header(p, ETH_PAD_SIZE);
segundo 0:ac1725ba162c 104 #endif
segundo 0:ac1725ba162c 105
segundo 0:ac1725ba162c 106 ethhdr = (struct eth_hdr *)(frame->payload);
segundo 0:ac1725ba162c 107
segundo 0:ac1725ba162c 108 // show((char*)ethhdr, 13);
segundo 0:ac1725ba162c 109
segundo 0:ac1725ba162c 110 /*
segundo 0:ac1725ba162c 111 switch(htons(ethhdr->type)) {
segundo 0:ac1725ba162c 112
segundo 0:ac1725ba162c 113 case ETHTYPE_IP:
segundo 0:ac1725ba162c 114 etharp_ip_input(gnetif, frame);
segundo 0:ac1725ba162c 115 pbuf_header(frame, -((s16_t) sizeof(struct eth_hdr)));
segundo 0:ac1725ba162c 116 gnetif->input(frame, gnetif);
segundo 0:ac1725ba162c 117 break;
segundo 0:ac1725ba162c 118
segundo 0:ac1725ba162c 119 case ETHTYPE_ARP:
segundo 0:ac1725ba162c 120 etharp_arp_input(gnetif, (struct eth_addr *)(gnetif->hwaddr), frame);
segundo 0:ac1725ba162c 121 break;
segundo 0:ac1725ba162c 122
segundo 0:ac1725ba162c 123 default:
segundo 0:ac1725ba162c 124 break;
segundo 0:ac1725ba162c 125 }*/
segundo 0:ac1725ba162c 126
segundo 0:ac1725ba162c 127
segundo 0:ac1725ba162c 128
segundo 0:ac1725ba162c 129 //ethernet_input(frame, gnetif);
segundo 0:ac1725ba162c 130
segundo 0:ac1725ba162c 131 switch (htons(ethhdr->type)) {
segundo 0:ac1725ba162c 132 /* IP or ARP packet? */
segundo 0:ac1725ba162c 133 case ETHTYPE_IP:
segundo 0:ac1725ba162c 134 case ETHTYPE_ARP:
segundo 0:ac1725ba162c 135 #if PPPOE_SUPPORT
segundo 0:ac1725ba162c 136 /* PPPoE packet? */
segundo 0:ac1725ba162c 137 case ETHTYPE_PPPOEDISC:
segundo 0:ac1725ba162c 138 case ETHTYPE_PPPOE:
segundo 0:ac1725ba162c 139 #endif /* PPPOE_SUPPORT */
segundo 0:ac1725ba162c 140 /* full packet send to tcpip_thread to process */
segundo 0:ac1725ba162c 141 //if (netif->input(p, gnetif)!=ERR_OK)
segundo 0:ac1725ba162c 142 if (ethernet_input(frame, eth_netif)!=ERR_OK)
segundo 0:ac1725ba162c 143 { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
segundo 0:ac1725ba162c 144 pbuf_free(frame);
segundo 0:ac1725ba162c 145 frame = NULL;
segundo 0:ac1725ba162c 146 }
segundo 0:ac1725ba162c 147 break;
segundo 0:ac1725ba162c 148
segundo 0:ac1725ba162c 149 default:
segundo 0:ac1725ba162c 150 pbuf_free(frame);
segundo 0:ac1725ba162c 151 frame = NULL;
segundo 0:ac1725ba162c 152 break;
segundo 0:ac1725ba162c 153 }
segundo 0:ac1725ba162c 154
segundo 0:ac1725ba162c 155 /* pbuf_free(frame); */
segundo 0:ac1725ba162c 156 }
segundo 0:ac1725ba162c 157
segundo 0:ac1725ba162c 158
segundo 0:ac1725ba162c 159
segundo 0:ac1725ba162c 160
segundo 0:ac1725ba162c 161 }
segundo 0:ac1725ba162c 162
segundo 0:ac1725ba162c 163 err_t eth_init(struct netif *netif) {
segundo 0:ac1725ba162c 164 LWIP_ASSERT("netif != NULL", (netif != NULL));
segundo 0:ac1725ba162c 165
segundo 0:ac1725ba162c 166 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 0x2EA);
segundo 0:ac1725ba162c 167
segundo 0:ac1725ba162c 168 /* maximum transfer unit */
segundo 0:ac1725ba162c 169 netif->mtu = 0x2EA;
segundo 0:ac1725ba162c 170
segundo 0:ac1725ba162c 171 /* device capabilities */
segundo 0:ac1725ba162c 172 /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
segundo 0:ac1725ba162c 173 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_IGMP;
segundo 0:ac1725ba162c 174
segundo 0:ac1725ba162c 175 netif->state = NULL;
segundo 0:ac1725ba162c 176 eth_netif = netif;
segundo 0:ac1725ba162c 177
segundo 0:ac1725ba162c 178 netif->name[0] = IFNAME0;
segundo 0:ac1725ba162c 179 netif->name[1] = IFNAME1;
segundo 0:ac1725ba162c 180
segundo 0:ac1725ba162c 181 /* We directly use etharp_output() here to save a function call.
segundo 0:ac1725ba162c 182 * You can instead declare your own function an call etharp_output()
segundo 0:ac1725ba162c 183 * from it if you have to do some checks before sending (e.g. if link
segundo 0:ac1725ba162c 184 * is available...) */
segundo 0:ac1725ba162c 185 netif->output = etharp_output;
segundo 0:ac1725ba162c 186 netif->linkoutput = eth_output;
segundo 0:ac1725ba162c 187
segundo 4:966a0265edfc 188 if (!pEth) pEth = new Ethernet(); // only create Ethernet object if required
segundo 0:ac1725ba162c 189
segundo 0:ac1725ba162c 190 return ERR_OK;
segundo 0:ac1725ba162c 191 }
segundo 0:ac1725ba162c 192
segundo 0:ac1725ba162c 193 void eth_free()
segundo 0:ac1725ba162c 194 {
segundo 0:ac1725ba162c 195 if(pEth)
segundo 0:ac1725ba162c 196 delete pEth;
segundo 0:ac1725ba162c 197 pEth = NULL;
segundo 0:ac1725ba162c 198 }
segundo 0:ac1725ba162c 199
segundo 0:ac1725ba162c 200 void eth_address(char* mac) {
segundo 0:ac1725ba162c 201 pEth->address(mac);
segundo 0:ac1725ba162c 202 }
segundo 0:ac1725ba162c 203
segundo 4:966a0265edfc 204 Ethernet* eth_interface() {
segundo 4:966a0265edfc 205 return pEth;
segundo 4:966a0265edfc 206 }
segundo 4:966a0265edfc 207
segundo 0:ac1725ba162c 208 #ifdef __cplusplus
segundo 0:ac1725ba162c 209 };
segundo 0:ac1725ba162c 210 #endif
segundo 0:ac1725ba162c 211
segundo 0:ac1725ba162c 212 #endif