WIZ820io(W5200) network interface、EthernetNetIf compatible.

/media/uploads/va009039/wiz820ionetif.jpg

example

#include "WIZ820ioNetIf.h"
#include "HTTPClient.h"
#include "HTTPServer.h"

#if defined(TARGET_KL25Z)
WIZ820ioNetIf eth(PTD2,PTD3,PTD1,PTD0,PTD5);
#endif
HTTPClient http;
HTTPStream stream;

void callback(HTTPResult r){
    printf("callback %d %s\n", r, HTTPClient::ResultStr(r));
}

int main() {
    int err = eth.setup();
    if (err < 0) {
        printf("setup error %d\n", err);
        exit(-1);
    }    

    HTTPServer svr;
    svr.addHandler<SimpleHandler>("/");
    svr.bind(80);

    const char* uri = "http://va009039-mbed.appspot.com/kl25z/";
    http.get(uri, &stream, callback);
    uint8_t buf[256];
    int total = 0;
    stream.readNext(buf, sizeof(buf));
    while(1) {
        if(stream.readable()) {
            int len = stream.readLen();
            total += len;
            printf("%d %d\n", total, len);
            stream.readNext(buf, sizeof(buf));
        }
        Net::poll();
    }
}
Committer:
va009039
Date:
Fri Mar 22 11:51:24 2013 +0000
Revision:
0:bdeec5f86894
[mbed] converted /w5200NetIf/w5200NetIf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:bdeec5f86894 1 // W5200NetIf.cpp 2012/4/23
va009039 0:bdeec5f86894 2 #include "W5200NetIf.h"
va009039 0:bdeec5f86894 3 #include "DHCPClient.h"
va009039 0:bdeec5f86894 4 #include "w5100.h"
va009039 0:bdeec5f86894 5
va009039 0:bdeec5f86894 6 extern SPI* pSPI; // w5100.cpp
va009039 0:bdeec5f86894 7 extern DigitalOut* pCS; // w5100.cpp
va009039 0:bdeec5f86894 8
va009039 0:bdeec5f86894 9 W5200NetIf:: W5200NetIf():MyNetIf(), m_netmask(255,255,255,255), m_gateway(), m_hostname(NULL) {
va009039 0:bdeec5f86894 10 m_hostname = NULL;
va009039 0:bdeec5f86894 11 m_useDhcp = true;
va009039 0:bdeec5f86894 12 }
va009039 0:bdeec5f86894 13
va009039 0:bdeec5f86894 14 W5200NetIf::W5200NetIf(IpAddr ip, IpAddr netmask, IpAddr gateway, IpAddr dns) :MyNetIf(), m_hostname(NULL) {
va009039 0:bdeec5f86894 15 m_hostname = NULL;
va009039 0:bdeec5f86894 16 m_ip = ip;
va009039 0:bdeec5f86894 17 m_netmask = netmask;
va009039 0:bdeec5f86894 18 m_gateway = gateway;
va009039 0:bdeec5f86894 19 m_dns = dns;
va009039 0:bdeec5f86894 20 m_useDhcp = false;
va009039 0:bdeec5f86894 21 }
va009039 0:bdeec5f86894 22
va009039 0:bdeec5f86894 23 W5200Err W5200NetIf::IPrenew(int timeout_ms) {
va009039 0:bdeec5f86894 24 if (pSPI == NULL || pCS == NULL) {
va009039 0:bdeec5f86894 25 return W5200_SETUP_ERR;
va009039 0:bdeec5f86894 26 }
va009039 0:bdeec5f86894 27 printf("DHCP Started, waiting for IP...\n");
va009039 0:bdeec5f86894 28 DHCPClient dhcp;
va009039 0:bdeec5f86894 29 int err = dhcp.setup(timeout_ms);
va009039 0:bdeec5f86894 30 if (err == (-1)) {
va009039 0:bdeec5f86894 31 printf("Timeout.\n");
va009039 0:bdeec5f86894 32 return W5200_TIMEOUT;
va009039 0:bdeec5f86894 33 }
va009039 0:bdeec5f86894 34 printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
va009039 0:bdeec5f86894 35 m_ip = IpAddr(dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
va009039 0:bdeec5f86894 36 m_netmask = IpAddr(dhcp.netmask[0],dhcp.netmask[1],dhcp.netmask[2],dhcp.netmask[3]);
va009039 0:bdeec5f86894 37 m_gateway = IpAddr(dhcp.gateway[0],dhcp.gateway[1],dhcp.gateway[2],dhcp.gateway[3]);
va009039 0:bdeec5f86894 38 uint8_t t[4];
va009039 0:bdeec5f86894 39 t[0] = m_ip[0];
va009039 0:bdeec5f86894 40 t[1] = m_ip[1];
va009039 0:bdeec5f86894 41 t[2] = m_ip[2];
va009039 0:bdeec5f86894 42 t[3] = m_ip[3];
va009039 0:bdeec5f86894 43 W5100.writeSIPR(t);
va009039 0:bdeec5f86894 44 t[0] = m_netmask[0];
va009039 0:bdeec5f86894 45 t[1] = m_netmask[1];
va009039 0:bdeec5f86894 46 t[2] = m_netmask[2];
va009039 0:bdeec5f86894 47 t[3] = m_netmask[3];
va009039 0:bdeec5f86894 48 W5100.writeSUBR(t);
va009039 0:bdeec5f86894 49 t[0] = m_gateway[0];
va009039 0:bdeec5f86894 50 t[1] = m_gateway[1];
va009039 0:bdeec5f86894 51 t[2] = m_gateway[2];
va009039 0:bdeec5f86894 52 t[3] = m_gateway[3];
va009039 0:bdeec5f86894 53 W5100.writeGAR(t);
va009039 0:bdeec5f86894 54 m_dns = IpAddr(dhcp.dnsaddr[0],dhcp.dnsaddr[1],dhcp.dnsaddr[2],dhcp.dnsaddr[3]);
va009039 0:bdeec5f86894 55 return W5200_OK;
va009039 0:bdeec5f86894 56 }
va009039 0:bdeec5f86894 57
va009039 0:bdeec5f86894 58 W5200Err W5200NetIf::IPrelease(int timeout_ms) {
va009039 0:bdeec5f86894 59 return W5200_OK;
va009039 0:bdeec5f86894 60 }
va009039 0:bdeec5f86894 61
va009039 0:bdeec5f86894 62 W5200Err W5200NetIf::setup(int timeout_ms) {
va009039 0:bdeec5f86894 63 if (pSPI == NULL || pCS == NULL) {
va009039 0:bdeec5f86894 64 return W5200_SETUP_ERR;
va009039 0:bdeec5f86894 65 }
va009039 0:bdeec5f86894 66 MyNetIf::init();
va009039 0:bdeec5f86894 67 uint8_t mac[6] = {0x00,0x00,0x5e,0x00,0x01,0x01};
va009039 0:bdeec5f86894 68 W5100.setMACAddress(mac);
va009039 0:bdeec5f86894 69 printf("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\n", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
va009039 0:bdeec5f86894 70 if(! m_useDhcp) {
va009039 0:bdeec5f86894 71 return W5200_OK;
va009039 0:bdeec5f86894 72 }
va009039 0:bdeec5f86894 73 return IPrenew(timeout_ms);
va009039 0:bdeec5f86894 74 }
va009039 0:bdeec5f86894 75
va009039 0:bdeec5f86894 76 SPI* W5200NetIf::attachSPI(SPI* spi) {
va009039 0:bdeec5f86894 77 if (spi) {
va009039 0:bdeec5f86894 78 pSPI = spi;
va009039 0:bdeec5f86894 79 }
va009039 0:bdeec5f86894 80 return pSPI;
va009039 0:bdeec5f86894 81 }
va009039 0:bdeec5f86894 82
va009039 0:bdeec5f86894 83 DigitalOut* W5200NetIf::attachCS(DigitalOut* cs) {
va009039 0:bdeec5f86894 84 if (cs) {
va009039 0:bdeec5f86894 85 pCS = cs;
va009039 0:bdeec5f86894 86 }
va009039 0:bdeec5f86894 87 return pCS;
va009039 0:bdeec5f86894 88 }