Hill Kim / W5200Interface

Dependents:   IBMIoTClientEthernetExample_W5200

Fork of W5500Interface by W5500-Ethernet-Interface Makers

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EthernetInterface.cpp Source File

EthernetInterface.cpp

00001 // EthernetInterface for W5500 2014/8/20
00002 /*
00003 // sample usgae. 
00004 // copy below code block to main code.
00005 
00006 #if defined(TARGET_LPC1114)
00007     SPI spi(dp2, dp1, dp6); // mosi, miso, sclk
00008     EthernetInterface eth(&spi, dp25, dp26); // spi, cs, reset
00009     wait(1); // 1 second for stable state
00010 #elif defined(TARGET_LPC1768)
00011     SPI spi(p11, p12, p13); // mosi, miso, sclk
00012     EthernetInterface eth(&spi, p14, p15); // spi, cs, reset
00013     wait(1); // 1 second for stable state
00014 #elif defined(TARGET_LPC11U68)
00015     SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk
00016     EthernetInterface eth(&spi, P0_2, P1_28);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io
00017     spi.format(8,0); // 8bit, mode 0
00018     spi.frequency(7000000); // 7MHz
00019     wait(1); // 1 second for stable state
00020 #endif
00021 
00022     eth.init(); //Use DHCP
00023     dbg.printf("init\r\n");
00024     eth.connect();
00025     dbg.printf("IP address: %s\r\n", eth.getIPAddress());
00026 
00027 */
00028 
00029 #include "EthernetInterface.h"
00030 #include "DHCPClient.h"
00031 
00032 EthernetInterface::EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
00033         WIZnet_Chip(mosi, miso, sclk, cs, reset)
00034 {
00035     ip_set = false;
00036 }
00037 
00038 EthernetInterface::EthernetInterface(SPI* spi, PinName cs, PinName reset) :
00039         WIZnet_Chip(spi, cs, reset)
00040 {
00041     ip_set = false;
00042 }
00043 
00044 
00045 int EthernetInterface::init()
00046 {
00047     dhcp = true;
00048     //
00049     //for (int i =0; i < 6; i++) this->mac[i] = mac[i];
00050     //
00051     reset();
00052     return 0;
00053 }
00054 
00055 int EthernetInterface::init(uint8_t * mac)
00056 {
00057     dhcp = true;
00058     //
00059     for (int i =0; i < 6; i++) this->mac[i] = mac[i];
00060     //
00061     reset();
00062     setmac();    
00063     return 0;
00064 }
00065 
00066 // add this function, because sometimes no needed MAC address in init calling.
00067 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway)
00068 {
00069     dhcp = false;
00070     //
00071     //for (int i =0; i < 6; i++) this->mac[i] = mac[i];
00072     //
00073     this->ip = str_to_ip(ip);
00074     strcpy(ip_string, ip);
00075     ip_set = true;
00076     this->netmask = str_to_ip(mask);
00077     this->gateway = str_to_ip(gateway);
00078     reset();
00079     
00080     // @Jul. 8. 2014 add code. should be called to write chip.
00081     setip(); 
00082     
00083     return 0;
00084 }
00085 
00086 int EthernetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway)
00087 {
00088     dhcp = false;
00089     //
00090     for (int i =0; i < 6; i++) this->mac[i] = mac[i];
00091     //
00092     this->ip = str_to_ip(ip);
00093     strcpy(ip_string, ip);
00094     ip_set = true;
00095     this->netmask = str_to_ip(mask);
00096     this->gateway = str_to_ip(gateway);
00097     reset();
00098 
00099     // @Jul. 8. 2014 add code. should be called to write chip.
00100     setmac();
00101     setip();
00102     
00103     return 0;
00104 }
00105 
00106 // Connect Bring the interface up, start DHCP if needed.
00107 int EthernetInterface::connect()
00108 {
00109     if (dhcp) {
00110         int r = IPrenew();
00111         if (r < 0) {
00112             return r;
00113         }
00114     }
00115     
00116     if (WIZnet_Chip::setip() == false) return -1;
00117     return 0;
00118 }
00119 
00120 // Disconnect Bring the interface down.
00121 int EthernetInterface::disconnect()
00122 {
00123     if (WIZnet_Chip::disconnect() == false) return -1;
00124     return 0;
00125 }
00126 
00127 char* EthernetInterface::getIPAddress()
00128 {
00129     uint32_t ip = reg_rd<uint32_t>(SIPR);
00130     snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
00131     return ip_string;
00132 }
00133 
00134 char* EthernetInterface::getNetworkMask()
00135 {
00136     uint32_t ip = reg_rd<uint32_t>(SUBR);
00137     snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
00138     return mask_string;
00139 }
00140 
00141 char* EthernetInterface::getGateway()
00142 {
00143     uint32_t ip = reg_rd<uint32_t>(GAR);
00144     snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
00145     return gw_string;
00146 }
00147 
00148 char* EthernetInterface::getMACAddress()
00149 {
00150     uint8_t mac[6];
00151     reg_rd_mac(SHAR, mac);
00152     snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
00153     return mac_string; 
00154 }
00155 
00156 int EthernetInterface::IPrenew(int timeout_ms)
00157 {
00158 //    printf("DHCP Started, waiting for IP...\n");  
00159     DHCPClient dhcp;
00160     int err = dhcp.setup(timeout_ms);
00161     if (err == (-1)) {
00162 //        printf("Timeout.\n");
00163         return -1;
00164     }
00165 //    printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
00166     ip      = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3];
00167     gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
00168     netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
00169     dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
00170     return 0;
00171 }