This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500, W5200 and W5100. One of them can be selected by enabling it in wiznet.h.
Dependents: Embedded_web EmailButton EmailButton HTTPClient_Weather ... more
WIZnetInterface.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 "WIZnetInterface.h" 00020 #include "DHCPClient.h" 00021 00022 WIZnetInterface::WIZnetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) : 00023 WIZnet_Chip(mosi, miso, sclk, cs, reset) 00024 { 00025 ip_set = false; 00026 } 00027 00028 WIZnetInterface::WIZnetInterface(SPI* spi, PinName cs, PinName reset) : 00029 WIZnet_Chip(spi, cs, reset) 00030 { 00031 ip_set = false; 00032 } 00033 00034 int WIZnetInterface::init(uint8_t * mac) 00035 { 00036 dhcp = true; 00037 // 00038 for (int i =0; i < 6; i++) this->mac[i] = mac[i]; 00039 // 00040 reset(); 00041 return 0; 00042 } 00043 00044 int WIZnetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway) 00045 { 00046 dhcp = false; 00047 // 00048 for (int i =0; i < 6; i++) this->mac[i] = mac[i]; 00049 // 00050 this->ip = str_to_ip(ip); 00051 strcpy(ip_string, ip); 00052 ip_set = true; 00053 this->netmask = str_to_ip(mask); 00054 this->gateway = str_to_ip(gateway); 00055 reset(); 00056 return 0; 00057 } 00058 00059 // Connect Bring the interface up, start DHCP if needed. 00060 int WIZnetInterface::connect() 00061 { 00062 if (dhcp) { 00063 int r = IPrenew(); 00064 if (r < 0) { 00065 return r; 00066 } 00067 } 00068 00069 if (WIZnet_Chip::setip() == false) return -1; 00070 return 0; 00071 } 00072 00073 // Disconnect Bring the interface down. 00074 int WIZnetInterface::disconnect() 00075 { 00076 if (WIZnet_Chip::disconnect() == false) return -1; 00077 return 0; 00078 } 00079 00080 char* WIZnetInterface::getIPAddress() 00081 { 00082 uint32_t ip = reg_rd<uint32_t>(SIPR); 00083 snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); 00084 return ip_string; 00085 } 00086 00087 char* WIZnetInterface::getNetworkMask() 00088 { 00089 uint32_t ip = reg_rd<uint32_t>(SUBR); 00090 snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); 00091 return mask_string; 00092 } 00093 00094 char* WIZnetInterface::getGateway() 00095 { 00096 uint32_t ip = reg_rd<uint32_t>(GAR); 00097 snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); 00098 return gw_string; 00099 } 00100 00101 char* WIZnetInterface::getMACAddress() 00102 { 00103 uint8_t mac[6]; 00104 reg_rd_mac(SHAR, mac); 00105 snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 00106 return mac_string; 00107 00108 } 00109 00110 int WIZnetInterface::IPrenew(int timeout_ms) 00111 { 00112 DHCPClient dhcp; 00113 int err = dhcp.setup(timeout_ms); 00114 if (err == (-1)) { 00115 return -1; 00116 } 00117 // printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]); 00118 ip = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3]; 00119 gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3]; 00120 netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3]; 00121 dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3]; 00122 return 0; 00123 }
Generated on Wed Jul 13 2022 02:51:31 by 1.7.2