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