Norimasa Okamoto / WIZ820ioNetIf
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WIZ820ioNetIf.cpp Source File

WIZ820ioNetIf.cpp

00001 // WIZ820ioNetIf.cpp 2013/3/24
00002 #include "WIZ820ioNetIf.h"
00003 #include "DHCPClient.h"
00004 #include "w5200.h"
00005 extern SPI* pSPI; // w5200.cpp
00006 extern DigitalOut* pCS; // w5200.cpp
00007 
00008 DigitalOut* pRESET = NULL;
00009 
00010 WIZ820ioNetIf::WIZ820ioNetIf(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset)
00011                                 :MyNetIf(), m_netmask(255,255,255,255), m_gateway(), m_hostname(NULL)
00012 {
00013     m_hostname = NULL;
00014     m_useDhcp = true;
00015     pin_assign(mosi, miso, sclk, cs, reset);
00016 }
00017 
00018 void WIZ820ioNetIf::config(IpAddr ip, IpAddr netmask, IpAddr gateway, IpAddr dns)
00019 {
00020     m_ip = ip;
00021     m_netmask = netmask;
00022     m_gateway = gateway;
00023     m_dns = dns;
00024     m_useDhcp = false;
00025 }
00026 
00027 void WIZ820ioNetIf::pin_assign(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset)
00028 {
00029     pSPI = new SPI(mosi, miso, sclk);
00030     pCS = new DigitalOut(cs);
00031     if (reset != NC) {
00032         pRESET = new DigitalOut(reset);
00033     }
00034 }
00035 
00036 void hardware_reset() {
00037     if (pRESET) {
00038         pRESET->write(1);
00039         pRESET->write(0);
00040         wait_us(2);
00041         pRESET->write(1);
00042         wait_ms(150);
00043     }
00044 }
00045 
00046 bool wait_linkup(int timeout_ms = 5*1000) {
00047     Timer link_t;
00048     link_t.reset();
00049     link_t.start();
00050     uint8_t status = 0x00;
00051     while(link_t.read_ms() < timeout_ms) {
00052         status = W5200.readPHYSTATUS();
00053         if (status & 0x20) {
00054             printf("Link Up\n");
00055             return true;
00056         }
00057         wait_ms(50);
00058     }  
00059     printf("Link down\n");
00060     if (status & 0x08) {
00061         printf("Power down mode\n");
00062     }
00063     return false;
00064 }
00065 
00066 int WIZ820ioNetIf::setup(int timeout_ms) {
00067     hardware_reset();
00068     W5200.init();
00069     if (!wait_linkup()) {
00070         return W5200_SETUP_ERR_LINK_DOWN;
00071     }
00072 
00073     MyNetIf::init();
00074     uint8_t mac[6];
00075     mbed_mac_address((char*)mac);
00076     W5200.setMACAddress(mac);
00077     printf("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\n", mac[0],mac[1],mac[2],mac[3],mac[4],mac[5]);
00078     if(! m_useDhcp) {
00079         return W5200_OK;
00080     }
00081     return IPrenew(timeout_ms);
00082 }
00083 
00084 W5200Err WIZ820ioNetIf::IPrenew(int timeout_ms) {
00085     printf("DHCP Started, waiting for IP...\n");  
00086     DHCPClient dhcp;
00087     int err = dhcp.setup(timeout_ms);
00088     if (err == (-1)) {
00089         printf("Timeout.\n");
00090         return W5200_TIMEOUT;
00091     }
00092     printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
00093     m_ip = IpAddr(dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
00094     m_netmask = IpAddr(dhcp.netmask[0],dhcp.netmask[1],dhcp.netmask[2],dhcp.netmask[3]);
00095     m_gateway = IpAddr(dhcp.gateway[0],dhcp.gateway[1],dhcp.gateway[2],dhcp.gateway[3]);
00096     uint8_t t[4];
00097     t[0] = m_ip[0];
00098     t[1] = m_ip[1];
00099     t[2] = m_ip[2];
00100     t[3] = m_ip[3];
00101     W5200.writeSIPR(t);
00102     t[0] = m_netmask[0];
00103     t[1] = m_netmask[1];
00104     t[2] = m_netmask[2];
00105     t[3] = m_netmask[3];
00106     W5200.writeSUBR(t);
00107     t[0] = m_gateway[0];
00108     t[1] = m_gateway[1];
00109     t[2] = m_gateway[2];
00110     t[3] = m_gateway[3];
00111     W5200.writeGAR(t);
00112     m_dns = IpAddr(dhcp.dnsaddr[0],dhcp.dnsaddr[1],dhcp.dnsaddr[2],dhcp.dnsaddr[3]);
00113     return W5200_OK;
00114 }
00115 
00116 W5200Err WIZ820ioNetIf::IPrelease(int timeout_ms) {
00117     return W5200_OK;
00118 }