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