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

Committer:
Yann
Date:
Fri Feb 11 10:17:20 2011 +0000
Revision:
1:995212d326ca
Parent:
0:f30e2135b0db
V0.0.0.2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Yann 0:f30e2135b0db 1 /**
Yann 0:f30e2135b0db 2 * @file
Yann 0:f30e2135b0db 3 * Ethernet Interface Skeleton
Yann 0:f30e2135b0db 4 *
Yann 0:f30e2135b0db 5 */
Yann 0:f30e2135b0db 6
Yann 0:f30e2135b0db 7 /*
Yann 0:f30e2135b0db 8 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
Yann 0:f30e2135b0db 9 * All rights reserved.
Yann 0:f30e2135b0db 10 *
Yann 0:f30e2135b0db 11 * Redistribution and use in source and binary forms, with or without modification,
Yann 0:f30e2135b0db 12 * are permitted provided that the following conditions are met:
Yann 0:f30e2135b0db 13 *
Yann 0:f30e2135b0db 14 * 1. Redistributions of source code must retain the above copyright notice,
Yann 0:f30e2135b0db 15 * this list of conditions and the following disclaimer.
Yann 0:f30e2135b0db 16 * 2. Redistributions in binary form must reproduce the above copyright notice,
Yann 0:f30e2135b0db 17 * this list of conditions and the following disclaimer in the documentation
Yann 0:f30e2135b0db 18 * and/or other materials provided with the distribution.
Yann 0:f30e2135b0db 19 * 3. The name of the author may not be used to endorse or promote products
Yann 0:f30e2135b0db 20 * derived from this software without specific prior written permission.
Yann 0:f30e2135b0db 21 *
Yann 0:f30e2135b0db 22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
Yann 0:f30e2135b0db 23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Yann 0:f30e2135b0db 24 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
Yann 0:f30e2135b0db 25 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
Yann 0:f30e2135b0db 26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
Yann 0:f30e2135b0db 27 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Yann 0:f30e2135b0db 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Yann 0:f30e2135b0db 29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
Yann 0:f30e2135b0db 30 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
Yann 0:f30e2135b0db 31 * OF SUCH DAMAGE.
Yann 0:f30e2135b0db 32 *
Yann 0:f30e2135b0db 33 * This file is part of the lwIP TCP/IP stack.
Yann 0:f30e2135b0db 34 *
Yann 0:f30e2135b0db 35 * Author: Adam Dunkels <adam@sics.se>
Yann 0:f30e2135b0db 36 *
Yann 0:f30e2135b0db 37 */
Yann 0:f30e2135b0db 38
Yann 0:f30e2135b0db 39 /*
Yann 0:f30e2135b0db 40 * This file is a skeleton for developing Ethernet network interface
Yann 0:f30e2135b0db 41 * drivers for lwIP. Add code to the low_level functions and do a
Yann 0:f30e2135b0db 42 * search-and-replace for the word "ethernetif" to replace it with
Yann 0:f30e2135b0db 43 * something that better describes your network interface.
Yann 0:f30e2135b0db 44 */
Yann 0:f30e2135b0db 45
Yann 0:f30e2135b0db 46 #include "lwip/opt.h"
Yann 0:f30e2135b0db 47
Yann 0:f30e2135b0db 48 #if 0 /* don't build, this is only a skeleton, see previous comment */
Yann 0:f30e2135b0db 49
Yann 0:f30e2135b0db 50 #include "lwip/def.h"
Yann 0:f30e2135b0db 51 #include "lwip/mem.h"
Yann 0:f30e2135b0db 52 #include "lwip/pbuf.h"
Yann 0:f30e2135b0db 53 #include "lwip/sys.h"
Yann 0:f30e2135b0db 54 #include <lwip/stats.h>
Yann 0:f30e2135b0db 55 #include <lwip/snmp.h>
Yann 0:f30e2135b0db 56 #include "netif/etharp.h"
Yann 0:f30e2135b0db 57 #include "netif/ppp_oe.h"
Yann 0:f30e2135b0db 58
Yann 0:f30e2135b0db 59 /* Define those to better describe your network interface. */
Yann 0:f30e2135b0db 60 #define IFNAME0 'e'
Yann 0:f30e2135b0db 61 #define IFNAME1 'n'
Yann 0:f30e2135b0db 62
Yann 0:f30e2135b0db 63 /**
Yann 0:f30e2135b0db 64 * Helper struct to hold private data used to operate your ethernet interface.
Yann 0:f30e2135b0db 65 * Keeping the ethernet address of the MAC in this struct is not necessary
Yann 0:f30e2135b0db 66 * as it is already kept in the struct netif.
Yann 0:f30e2135b0db 67 * But this is only an example, anyway...
Yann 0:f30e2135b0db 68 */
Yann 0:f30e2135b0db 69 struct ethernetif {
Yann 0:f30e2135b0db 70 struct eth_addr *ethaddr;
Yann 0:f30e2135b0db 71 /* Add whatever per-interface state that is needed here. */
Yann 0:f30e2135b0db 72 };
Yann 0:f30e2135b0db 73
Yann 0:f30e2135b0db 74 /* Forward declarations. */
Yann 0:f30e2135b0db 75 static void ethernetif_input(struct netif *netif);
Yann 0:f30e2135b0db 76
Yann 0:f30e2135b0db 77 /**
Yann 0:f30e2135b0db 78 * In this function, the hardware should be initialized.
Yann 0:f30e2135b0db 79 * Called from ethernetif_init().
Yann 0:f30e2135b0db 80 *
Yann 0:f30e2135b0db 81 * @param netif the already initialized lwip network interface structure
Yann 0:f30e2135b0db 82 * for this ethernetif
Yann 0:f30e2135b0db 83 */
Yann 0:f30e2135b0db 84 static void
Yann 0:f30e2135b0db 85 low_level_init(struct netif *netif)
Yann 0:f30e2135b0db 86 {
Yann 0:f30e2135b0db 87 struct ethernetif *ethernetif = netif->state;
Yann 0:f30e2135b0db 88
Yann 0:f30e2135b0db 89 /* set MAC hardware address length */
Yann 0:f30e2135b0db 90 netif->hwaddr_len = ETHARP_HWADDR_LEN;
Yann 0:f30e2135b0db 91
Yann 0:f30e2135b0db 92 /* set MAC hardware address */
Yann 0:f30e2135b0db 93 netif->hwaddr[0] = ;
Yann 0:f30e2135b0db 94 ...
Yann 0:f30e2135b0db 95 netif->hwaddr[5] = ;
Yann 0:f30e2135b0db 96
Yann 0:f30e2135b0db 97 /* maximum transfer unit */
Yann 0:f30e2135b0db 98 netif->mtu = 1500;
Yann 0:f30e2135b0db 99
Yann 0:f30e2135b0db 100 /* device capabilities */
Yann 0:f30e2135b0db 101 /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
Yann 0:f30e2135b0db 102 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP;
Yann 0:f30e2135b0db 103
Yann 0:f30e2135b0db 104 /* Do whatever else is needed to initialize interface. */
Yann 0:f30e2135b0db 105 }
Yann 0:f30e2135b0db 106
Yann 0:f30e2135b0db 107 /**
Yann 0:f30e2135b0db 108 * This function should do the actual transmission of the packet. The packet is
Yann 0:f30e2135b0db 109 * contained in the pbuf that is passed to the function. This pbuf
Yann 0:f30e2135b0db 110 * might be chained.
Yann 0:f30e2135b0db 111 *
Yann 0:f30e2135b0db 112 * @param netif the lwip network interface structure for this ethernetif
Yann 0:f30e2135b0db 113 * @param p the MAC packet to send (e.g. IP packet including MAC addresses and type)
Yann 0:f30e2135b0db 114 * @return ERR_OK if the packet could be sent
Yann 0:f30e2135b0db 115 * an err_t value if the packet couldn't be sent
Yann 0:f30e2135b0db 116 *
Yann 0:f30e2135b0db 117 * @note Returning ERR_MEM here if a DMA queue of your MAC is full can lead to
Yann 0:f30e2135b0db 118 * strange results. You might consider waiting for space in the DMA queue
Yann 0:f30e2135b0db 119 * to become availale since the stack doesn't retry to send a packet
Yann 0:f30e2135b0db 120 * dropped because of memory failure (except for the TCP timers).
Yann 0:f30e2135b0db 121 */
Yann 0:f30e2135b0db 122
Yann 0:f30e2135b0db 123 static err_t
Yann 0:f30e2135b0db 124 low_level_output(struct netif *netif, struct pbuf *p)
Yann 0:f30e2135b0db 125 {
Yann 0:f30e2135b0db 126 struct ethernetif *ethernetif = netif->state;
Yann 0:f30e2135b0db 127 struct pbuf *q;
Yann 0:f30e2135b0db 128
Yann 0:f30e2135b0db 129 initiate transfer();
Yann 0:f30e2135b0db 130
Yann 0:f30e2135b0db 131 #if ETH_PAD_SIZE
Yann 0:f30e2135b0db 132 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
Yann 0:f30e2135b0db 133 #endif
Yann 0:f30e2135b0db 134
Yann 0:f30e2135b0db 135 for(q = p; q != NULL; q = q->next) {
Yann 0:f30e2135b0db 136 /* Send the data from the pbuf to the interface, one pbuf at a
Yann 0:f30e2135b0db 137 time. The size of the data in each pbuf is kept in the ->len
Yann 0:f30e2135b0db 138 variable. */
Yann 0:f30e2135b0db 139 send data from(q->payload, q->len);
Yann 0:f30e2135b0db 140 }
Yann 0:f30e2135b0db 141
Yann 0:f30e2135b0db 142 signal that packet should be sent();
Yann 0:f30e2135b0db 143
Yann 0:f30e2135b0db 144 #if ETH_PAD_SIZE
Yann 0:f30e2135b0db 145 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
Yann 0:f30e2135b0db 146 #endif
Yann 0:f30e2135b0db 147
Yann 0:f30e2135b0db 148 LINK_STATS_INC(link.xmit);
Yann 0:f30e2135b0db 149
Yann 0:f30e2135b0db 150 return ERR_OK;
Yann 0:f30e2135b0db 151 }
Yann 0:f30e2135b0db 152
Yann 0:f30e2135b0db 153 /**
Yann 0:f30e2135b0db 154 * Should allocate a pbuf and transfer the bytes of the incoming
Yann 0:f30e2135b0db 155 * packet from the interface into the pbuf.
Yann 0:f30e2135b0db 156 *
Yann 0:f30e2135b0db 157 * @param netif the lwip network interface structure for this ethernetif
Yann 0:f30e2135b0db 158 * @return a pbuf filled with the received packet (including MAC header)
Yann 0:f30e2135b0db 159 * NULL on memory error
Yann 0:f30e2135b0db 160 */
Yann 0:f30e2135b0db 161 static struct pbuf *
Yann 0:f30e2135b0db 162 low_level_input(struct netif *netif)
Yann 0:f30e2135b0db 163 {
Yann 0:f30e2135b0db 164 struct ethernetif *ethernetif = netif->state;
Yann 0:f30e2135b0db 165 struct pbuf *p, *q;
Yann 0:f30e2135b0db 166 u16_t len;
Yann 0:f30e2135b0db 167
Yann 0:f30e2135b0db 168 /* Obtain the size of the packet and put it into the "len"
Yann 0:f30e2135b0db 169 variable. */
Yann 0:f30e2135b0db 170 len = ;
Yann 0:f30e2135b0db 171
Yann 0:f30e2135b0db 172 #if ETH_PAD_SIZE
Yann 0:f30e2135b0db 173 len += ETH_PAD_SIZE; /* allow room for Ethernet padding */
Yann 0:f30e2135b0db 174 #endif
Yann 0:f30e2135b0db 175
Yann 0:f30e2135b0db 176 /* We allocate a pbuf chain of pbufs from the pool. */
Yann 0:f30e2135b0db 177 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
Yann 0:f30e2135b0db 178
Yann 0:f30e2135b0db 179 if (p != NULL) {
Yann 0:f30e2135b0db 180
Yann 0:f30e2135b0db 181 #if ETH_PAD_SIZE
Yann 0:f30e2135b0db 182 pbuf_header(p, -ETH_PAD_SIZE); /* drop the padding word */
Yann 0:f30e2135b0db 183 #endif
Yann 0:f30e2135b0db 184
Yann 0:f30e2135b0db 185 /* We iterate over the pbuf chain until we have read the entire
Yann 0:f30e2135b0db 186 * packet into the pbuf. */
Yann 0:f30e2135b0db 187 for(q = p; q != NULL; q = q->next) {
Yann 0:f30e2135b0db 188 /* Read enough bytes to fill this pbuf in the chain. The
Yann 0:f30e2135b0db 189 * available data in the pbuf is given by the q->len
Yann 0:f30e2135b0db 190 * variable.
Yann 0:f30e2135b0db 191 * This does not necessarily have to be a memcpy, you can also preallocate
Yann 0:f30e2135b0db 192 * pbufs for a DMA-enabled MAC and after receiving truncate it to the
Yann 0:f30e2135b0db 193 * actually received size. In this case, ensure the tot_len member of the
Yann 0:f30e2135b0db 194 * pbuf is the sum of the chained pbuf len members.
Yann 0:f30e2135b0db 195 */
Yann 0:f30e2135b0db 196 read data into(q->payload, q->len);
Yann 0:f30e2135b0db 197 }
Yann 0:f30e2135b0db 198 acknowledge that packet has been read();
Yann 0:f30e2135b0db 199
Yann 0:f30e2135b0db 200 #if ETH_PAD_SIZE
Yann 0:f30e2135b0db 201 pbuf_header(p, ETH_PAD_SIZE); /* reclaim the padding word */
Yann 0:f30e2135b0db 202 #endif
Yann 0:f30e2135b0db 203
Yann 0:f30e2135b0db 204 LINK_STATS_INC(link.recv);
Yann 0:f30e2135b0db 205 } else {
Yann 0:f30e2135b0db 206 drop packet();
Yann 0:f30e2135b0db 207 LINK_STATS_INC(link.memerr);
Yann 0:f30e2135b0db 208 LINK_STATS_INC(link.drop);
Yann 0:f30e2135b0db 209 }
Yann 0:f30e2135b0db 210
Yann 0:f30e2135b0db 211 return p;
Yann 0:f30e2135b0db 212 }
Yann 0:f30e2135b0db 213
Yann 0:f30e2135b0db 214 /**
Yann 0:f30e2135b0db 215 * This function should be called when a packet is ready to be read
Yann 0:f30e2135b0db 216 * from the interface. It uses the function low_level_input() that
Yann 0:f30e2135b0db 217 * should handle the actual reception of bytes from the network
Yann 0:f30e2135b0db 218 * interface. Then the type of the received packet is determined and
Yann 0:f30e2135b0db 219 * the appropriate input function is called.
Yann 0:f30e2135b0db 220 *
Yann 0:f30e2135b0db 221 * @param netif the lwip network interface structure for this ethernetif
Yann 0:f30e2135b0db 222 */
Yann 0:f30e2135b0db 223 static void
Yann 0:f30e2135b0db 224 ethernetif_input(struct netif *netif)
Yann 0:f30e2135b0db 225 {
Yann 0:f30e2135b0db 226 struct ethernetif *ethernetif;
Yann 0:f30e2135b0db 227 struct eth_hdr *ethhdr;
Yann 0:f30e2135b0db 228 struct pbuf *p;
Yann 0:f30e2135b0db 229
Yann 0:f30e2135b0db 230 ethernetif = netif->state;
Yann 0:f30e2135b0db 231
Yann 0:f30e2135b0db 232 /* move received packet into a new pbuf */
Yann 0:f30e2135b0db 233 p = low_level_input(netif);
Yann 0:f30e2135b0db 234 /* no packet could be read, silently ignore this */
Yann 0:f30e2135b0db 235 if (p == NULL) return;
Yann 0:f30e2135b0db 236 /* points to packet payload, which starts with an Ethernet header */
Yann 0:f30e2135b0db 237 ethhdr = p->payload;
Yann 0:f30e2135b0db 238
Yann 0:f30e2135b0db 239 switch (htons(ethhdr->type)) {
Yann 0:f30e2135b0db 240 /* IP or ARP packet? */
Yann 0:f30e2135b0db 241 case ETHTYPE_IP:
Yann 0:f30e2135b0db 242 case ETHTYPE_ARP:
Yann 0:f30e2135b0db 243 #if PPPOE_SUPPORT
Yann 0:f30e2135b0db 244 /* PPPoE packet? */
Yann 0:f30e2135b0db 245 case ETHTYPE_PPPOEDISC:
Yann 0:f30e2135b0db 246 case ETHTYPE_PPPOE:
Yann 0:f30e2135b0db 247 #endif /* PPPOE_SUPPORT */
Yann 0:f30e2135b0db 248 /* full packet send to tcpip_thread to process */
Yann 0:f30e2135b0db 249 if (netif->input(p, netif)!=ERR_OK)
Yann 0:f30e2135b0db 250 { LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_input: IP input error\n"));
Yann 0:f30e2135b0db 251 pbuf_free(p);
Yann 0:f30e2135b0db 252 p = NULL;
Yann 0:f30e2135b0db 253 }
Yann 0:f30e2135b0db 254 break;
Yann 0:f30e2135b0db 255
Yann 0:f30e2135b0db 256 default:
Yann 0:f30e2135b0db 257 pbuf_free(p);
Yann 0:f30e2135b0db 258 p = NULL;
Yann 0:f30e2135b0db 259 break;
Yann 0:f30e2135b0db 260 }
Yann 0:f30e2135b0db 261 }
Yann 0:f30e2135b0db 262
Yann 0:f30e2135b0db 263 /**
Yann 0:f30e2135b0db 264 * Should be called at the beginning of the program to set up the
Yann 0:f30e2135b0db 265 * network interface. It calls the function low_level_init() to do the
Yann 0:f30e2135b0db 266 * actual setup of the hardware.
Yann 0:f30e2135b0db 267 *
Yann 0:f30e2135b0db 268 * This function should be passed as a parameter to netif_add().
Yann 0:f30e2135b0db 269 *
Yann 0:f30e2135b0db 270 * @param netif the lwip network interface structure for this ethernetif
Yann 0:f30e2135b0db 271 * @return ERR_OK if the loopif is initialized
Yann 0:f30e2135b0db 272 * ERR_MEM if private data couldn't be allocated
Yann 0:f30e2135b0db 273 * any other err_t on error
Yann 0:f30e2135b0db 274 */
Yann 0:f30e2135b0db 275 err_t
Yann 0:f30e2135b0db 276 ethernetif_init(struct netif *netif)
Yann 0:f30e2135b0db 277 {
Yann 0:f30e2135b0db 278 struct ethernetif *ethernetif;
Yann 0:f30e2135b0db 279
Yann 0:f30e2135b0db 280 LWIP_ASSERT("netif != NULL", (netif != NULL));
Yann 0:f30e2135b0db 281
Yann 0:f30e2135b0db 282 ethernetif = mem_malloc(sizeof(struct ethernetif));
Yann 0:f30e2135b0db 283 if (ethernetif == NULL) {
Yann 0:f30e2135b0db 284 LWIP_DEBUGF(NETIF_DEBUG, ("ethernetif_init: out of memory\n"));
Yann 0:f30e2135b0db 285 return ERR_MEM;
Yann 0:f30e2135b0db 286 }
Yann 0:f30e2135b0db 287
Yann 0:f30e2135b0db 288 #if LWIP_NETIF_HOSTNAME
Yann 0:f30e2135b0db 289 /* Initialize interface hostname */
Yann 0:f30e2135b0db 290 netif->hostname = "lwip";
Yann 0:f30e2135b0db 291 #endif /* LWIP_NETIF_HOSTNAME */
Yann 0:f30e2135b0db 292
Yann 0:f30e2135b0db 293 /*
Yann 0:f30e2135b0db 294 * Initialize the snmp variables and counters inside the struct netif.
Yann 0:f30e2135b0db 295 * The last argument should be replaced with your link speed, in units
Yann 0:f30e2135b0db 296 * of bits per second.
Yann 0:f30e2135b0db 297 */
Yann 0:f30e2135b0db 298 NETIF_INIT_SNMP(netif, snmp_ifType_ethernet_csmacd, LINK_SPEED_OF_YOUR_NETIF_IN_BPS);
Yann 0:f30e2135b0db 299
Yann 0:f30e2135b0db 300 netif->state = ethernetif;
Yann 0:f30e2135b0db 301 netif->name[0] = IFNAME0;
Yann 0:f30e2135b0db 302 netif->name[1] = IFNAME1;
Yann 0:f30e2135b0db 303 /* We directly use etharp_output() here to save a function call.
Yann 0:f30e2135b0db 304 * You can instead declare your own function an call etharp_output()
Yann 0:f30e2135b0db 305 * from it if you have to do some checks before sending (e.g. if link
Yann 0:f30e2135b0db 306 * is available...) */
Yann 0:f30e2135b0db 307 netif->output = etharp_output;
Yann 0:f30e2135b0db 308 netif->linkoutput = low_level_output;
Yann 0:f30e2135b0db 309
Yann 0:f30e2135b0db 310 ethernetif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
Yann 0:f30e2135b0db 311
Yann 0:f30e2135b0db 312 /* initialize the hardware */
Yann 0:f30e2135b0db 313 low_level_init(netif);
Yann 0:f30e2135b0db 314
Yann 0:f30e2135b0db 315 return ERR_OK;
Yann 0:f30e2135b0db 316 }
Yann 0:f30e2135b0db 317
Yann 0:f30e2135b0db 318 #endif /* 0 */