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