Lorcan Smith / Mbed 2 deprecated Enet_SPI

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers eth_drv.cpp Source File

eth_drv.cpp

00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "netCfg.h"
00025 #if NET_ETH
00026 
00027 #include "mbed.h"
00028 
00029 Ethernet *pEth = NULL;
00030 #ifdef __cplusplus
00031 extern "C" {
00032 #endif
00033 
00034 #include "lwip/opt.h"
00035 
00036 #include "lwip/def.h"
00037 #include "lwip/pbuf.h"
00038 #include "lwip/sys.h"
00039 #include "lwip/stats.h"
00040 #include "lwip/snmp_structs.h"
00041 #include "netif/etharp.h"
00042 #include "string.h"
00043 
00044 //#include "eth_drv.h"
00045 
00046 #define IFNAME0 'E'
00047 #define IFNAME1 'X'
00048 
00049 #define min(x,y) (((x)<(y))?(x):(y))
00050 
00051 
00052 struct netif* eth_netif;
00053 
00054 static err_t eth_output(struct netif *netif, struct pbuf *p) {
00055   #if ETH_PAD_SIZE
00056     pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
00057   #endif
00058 
00059   do {
00060     pEth->write((const char *)p->payload, p->len);
00061   } while((p = p->next)!=NULL);
00062 
00063   pEth->send();
00064 
00065   #if ETH_PAD_SIZE
00066     pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
00067   #endif
00068   
00069   LINK_STATS_INC(link.xmit);
00070   return ERR_OK;
00071 }
00072 
00073 /*
00074 void show(char *buf, int size) {
00075     printf("Destination:  %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
00076             buf[0], buf[1], buf[2], buf[3], buf[4], buf[5]);
00077     printf("Source: %02hx:%02hx:%02hx:%02hx:%02hx:%02hx\n",
00078             buf[6], buf[7], buf[8], buf[9], buf[10], buf[11]);
00079   
00080     printf("Type %hd\n", htons((short)buf[12]));
00081     
00082    // hexview(buf, size);
00083 }
00084 */
00085 
00086 void eth_poll() {
00087   struct eth_hdr *ethhdr;
00088   struct pbuf *frame, *p;
00089   int len, read;
00090 
00091   while((len = pEth->receive()) != 0) {
00092       frame = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
00093       if(frame == NULL) {
00094           return;
00095       }
00096       p = frame;
00097       /* no packet could be read, silently ignore this */
00098       if (p == NULL) return;
00099       do {
00100          read = pEth->read((char *)p->payload, p->len);
00101          p = p->next;
00102       } while(p != NULL && read != 0);
00103            
00104       #if ETH_PAD_SIZE
00105           pbuf_header(p, ETH_PAD_SIZE);
00106       #endif
00107 
00108       ethhdr = (struct eth_hdr *)(frame->payload);
00109       
00110      // show((char*)ethhdr, 13);
00111       
00112       /*
00113       switch(htons(ethhdr->type)) {
00114           
00115           case ETHTYPE_IP:
00116               etharp_ip_input(gnetif, frame);
00117               pbuf_header(frame, -((s16_t) sizeof(struct eth_hdr)));
00118               gnetif->input(frame, gnetif);
00119               break;
00120           
00121           case ETHTYPE_ARP:
00122               etharp_arp_input(gnetif, (struct eth_addr *)(gnetif->hwaddr), frame);
00123               break;
00124           
00125           default:
00126               break;
00127       }*/
00128       
00129       
00130       
00131       //ethernet_input(frame, gnetif);
00132       
00133       switch (htons(ethhdr->type)) {
00134       /* IP or ARP packet? */
00135       case ETHTYPE_IP:
00136       case ETHTYPE_ARP:
00137       #if PPPOE_SUPPORT
00138       /* PPPoE packet? */
00139       case ETHTYPE_PPPOEDISC:
00140       case ETHTYPE_PPPOE:
00141       #endif /* PPPOE_SUPPORT */
00142       /* full packet send to tcpip_thread to process */
00143         //if (netif->input(p, gnetif)!=ERR_OK)
00144         if (ethernet_input(frame, eth_netif)!=ERR_OK)
00145         { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
00146           pbuf_free(frame);
00147           frame = NULL;
00148         }
00149         break;
00150 
00151       default:
00152         pbuf_free(frame);
00153         frame = NULL;
00154         break;
00155       }
00156       
00157       /* pbuf_free(frame); */
00158   }
00159 
00160   
00161  
00162   
00163 }
00164 
00165 err_t eth_init(struct netif *netif) {
00166   LWIP_ASSERT("netif != NULL", (netif != NULL));
00167   
00168   NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, 0x2EA);
00169   
00170   /* maximum transfer unit */
00171   netif->mtu = 0x2EA;
00172   
00173   /* device capabilities */
00174   /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
00175   netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_IGMP;
00176 
00177   netif->state = NULL;
00178   eth_netif = netif;
00179 
00180   netif->name[0] = IFNAME0;
00181   netif->name[1] = IFNAME1;
00182 
00183   /* We directly use etharp_output() here to save a function call.
00184    * You can instead declare your own function an call etharp_output()
00185    * from it if you have to do some checks before sending (e.g. if link
00186    * is available...) */
00187   netif->output          = etharp_output;
00188   netif->linkoutput      = eth_output;
00189 
00190   pEth = new Ethernet();
00191 
00192   return ERR_OK;
00193 }
00194 
00195 void eth_free()
00196 {
00197   if(pEth)
00198     delete pEth;
00199   pEth = NULL;
00200 }
00201 
00202 void eth_address(char* mac) {
00203     pEth->address(mac);
00204 }
00205 
00206 #ifdef __cplusplus
00207 };
00208 #endif
00209 
00210 #endif