This is Library using WIZnet Hardware TCP/IP chip, W5500 and WIZnet TCP/IP Offload Engine, W7500.

Dependents:   HTTP_SDcard_file_server_WIZwiki-W7500 SSD1306_smart_watch TCPEchoServer-WIZwiki-W7500 httpServer-WIZwiki-W7500 ... more

Fork of WIZnetInterface by Soohwan Kim

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EthernetInterface.cpp Source File

EthernetInterface.cpp

00001 /* Copyright (C) 2012 mbed.org, MIT License
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00005  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00006  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00007  * furnished to do so, subject to the following conditions:
00008  *
00009  * The above copyright notice and this permission notice shall be included in all copies or
00010  * substantial portions of the Software.
00011  *
00012  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017  */
00018 
00019 #include "EthernetInterface.h"
00020 #include "DHCPClient.h"
00021 
00022 #if (not defined TARGET_WIZwiki_W7500) && (not defined TARGET_WIZwiki_W7500P) && (not defined TARGET_WIZwiki_W7500ECO)
00023 EthernetInterface::EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
00024         WIZnet_Chip(mosi, miso, sclk, cs, reset)
00025 {
00026     ip_set = false;
00027 }
00028 
00029 EthernetInterface::EthernetInterface(SPI* spi, PinName cs, PinName reset) :
00030         WIZnet_Chip(spi, cs, reset)
00031 {
00032     ip_set = false;
00033 }
00034 #endif
00035 
00036 int EthernetInterface::init()
00037 {
00038     dhcp = true;
00039     reset();
00040     
00041     return 0;
00042 }
00043 
00044 int EthernetInterface::init(uint8_t * mac)
00045 {
00046     dhcp = true;
00047     //
00048     for (int i =0; i < 6; i++) this->mac[i] = mac[i];
00049     //
00050     reset();
00051     
00052     return 0;
00053 }
00054 
00055 int EthernetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway)
00056 {
00057     dhcp = false;
00058     //
00059     for (int i =0; i < 6; i++) this->mac[i] = mac[i];
00060     //
00061     this->ip = str_to_ip(ip);
00062     strcpy(ip_string, ip);
00063     ip_set = true;
00064     this->netmask = str_to_ip(mask);
00065     this->gateway = str_to_ip(gateway);
00066     reset();
00067 
00068     // @Jul. 8. 2014 add code. should be called to write chip.
00069     setmac();
00070     setip();
00071     
00072     return 0;
00073 }
00074 
00075 // Connect Bring the interface up, start DHCP if needed.
00076 int EthernetInterface::connect()
00077 {
00078     if (dhcp) {
00079         int r = IPrenew();
00080         if (r < 0) {
00081             return r;
00082         }
00083     }
00084     
00085     if (WIZnet_Chip::setip() == false) return -1;
00086     return 0;
00087 }
00088 
00089 // Disconnect Bring the interface down.
00090 int EthernetInterface::disconnect()
00091 {
00092     //if (WIZnet_Chip::disconnect() == false) return -1;
00093     return 0;
00094 }
00095 
00096 
00097 char* EthernetInterface::getIPAddress()
00098 {
00099     uint32_t ip = reg_rd<uint32_t>(SIPR);
00100     snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", 
00101                 (uint8_t)((ip>>24)&0xff), 
00102                 (uint8_t)((ip>>16)&0xff), 
00103                 (uint8_t)((ip>>8)&0xff), 
00104                 (uint8_t)(ip&0xff));
00105     return ip_string;
00106 }
00107 
00108 char* EthernetInterface::getNetworkMask()
00109 {
00110     uint32_t ip = reg_rd<uint32_t>(SUBR);
00111     snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", 
00112                 (uint8_t)((ip>>24)&0xff), 
00113                 (uint8_t)((ip>>16)&0xff), 
00114                 (uint8_t)((ip>>8)&0xff), 
00115                 (uint8_t)(ip&0xff));
00116     return mask_string;
00117 }
00118 
00119 char* EthernetInterface::getGateway()
00120 {
00121     uint32_t ip = reg_rd<uint32_t>(GAR);
00122     snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", 
00123                 (uint8_t)((ip>>24)&0xff), 
00124                 (uint8_t)((ip>>16)&0xff), 
00125                 (uint8_t)((ip>>8)&0xff), 
00126                 (uint8_t)(ip&0xff));
00127     return gw_string;
00128 }
00129 
00130 char* EthernetInterface::getMACAddress()
00131 {
00132     uint8_t mac[6];
00133     reg_rd_mac(SHAR, mac);
00134     snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
00135     //ethernet_address(mac_string);
00136     return mac_string; 
00137     
00138 }
00139 
00140 int EthernetInterface::IPrenew(int timeout_ms)
00141 {
00142     DHCPClient dhcp;
00143     int err = dhcp.setup(timeout_ms);
00144     if (err == (-1)) {
00145         return -1;
00146     }
00147 //    printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
00148     ip      = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3];
00149     gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
00150     netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
00151     dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
00152     return 0;
00153 }
00154