Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: W5500-SNTPClient-example
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 //static char ip_string[17]; 00032 //static uint32_t ipaddr ; 00033 EthernetInterface::EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) : 00034 WIZnet_Chip(mosi, miso, sclk, cs, reset) 00035 { 00036 ip_set = false; 00037 } 00038 00039 EthernetInterface::EthernetInterface(SPI* spi, PinName cs, PinName reset) : 00040 WIZnet_Chip(spi, cs, reset) 00041 { 00042 ip_set = false; 00043 } 00044 00045 00046 int EthernetInterface::init() 00047 { 00048 dhcp = true; 00049 // 00050 //for (int i =0; i < 6; i++) this->mac[i] = mac[i]; 00051 // 00052 reset(); 00053 return 0; 00054 } 00055 00056 int EthernetInterface::init(uint8_t * mac) 00057 { 00058 dhcp = true; 00059 // 00060 for (int i =0; i < 6; i++) this->mac[i] = mac[i]; 00061 // 00062 reset(); 00063 setmac(); 00064 return 0; 00065 } 00066 00067 // add this function, because sometimes no needed MAC address in init calling. 00068 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway) 00069 { 00070 dhcp = false; 00071 // 00072 //for (int i =0; i < 6; i++) this->mac[i] = mac[i]; 00073 // 00074 this->ip = str_to_ip(ip); 00075 strcpy(ip_string, ip); 00076 ip_set = true; 00077 this->netmask = str_to_ip(mask); 00078 this->gateway = str_to_ip(gateway); 00079 reset(); 00080 00081 // @Jul. 8. 2014 add code. should be called to write chip. 00082 setip(); 00083 00084 return 0; 00085 } 00086 00087 int EthernetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway) 00088 { 00089 dhcp = false; 00090 // 00091 for (int i =0; i < 6; i++) this->mac[i] = mac[i]; 00092 // 00093 this->ip = str_to_ip(ip); 00094 strcpy(ip_string, ip); 00095 ip_set = true; 00096 this->netmask = str_to_ip(mask); 00097 this->gateway = str_to_ip(gateway); 00098 reset(); 00099 00100 // @Jul. 8. 2014 add code. should be called to write chip. 00101 setmac(); 00102 setip(); 00103 00104 return 0; 00105 } 00106 00107 // Connect Bring the interface up, start DHCP if needed. 00108 int EthernetInterface::connect() 00109 { 00110 if (dhcp) { 00111 int r = IPrenew(); 00112 if (r < 0) { 00113 return r; 00114 } 00115 } 00116 00117 if (WIZnet_Chip::setip() == false) return -1; 00118 return 0; 00119 } 00120 00121 // Disconnect Bring the interface down. 00122 int EthernetInterface::disconnect() 00123 { 00124 if (WIZnet_Chip::disconnect() == false) return -1; 00125 return 0; 00126 } 00127 00128 /* 00129 void EthernetInterface::getip(void) 00130 { 00131 //ip = 0x12345678; 00132 ipaddr = reg_rd<uint32_t>(SIPR); 00133 //snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ipaddr>>24)&0xff, (ipaddr>>16)&0xff, (ipaddr>>8)&0xff, ipaddr&0xff); 00134 } 00135 */ 00136 char* EthernetInterface::getIPAddress() 00137 { 00138 //Suint32_t ip = inet_getip(SIPR);//reg_rd<uint32_t>(SIPR); 00139 //static uint32_t ip = getip(); 00140 //uint32_t ip = getip(); 00141 00142 uint32_t ip = reg_rd<uint32_t>(SIPR); 00143 snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); 00144 return ip_string; 00145 } 00146 00147 char* EthernetInterface::getNetworkMask() 00148 { 00149 uint32_t ip = reg_rd<uint32_t>(SUBR); 00150 snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); 00151 return mask_string; 00152 } 00153 00154 char* EthernetInterface::getGateway() 00155 { 00156 uint32_t ip = reg_rd<uint32_t>(GAR); 00157 snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff); 00158 return gw_string; 00159 } 00160 00161 char* EthernetInterface::getMACAddress() 00162 { 00163 uint8_t mac[6]; 00164 reg_rd_mac(SHAR, mac); 00165 snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); 00166 return mac_string; 00167 } 00168 00169 int EthernetInterface::IPrenew(int timeout_ms) 00170 { 00171 // printf("DHCP Started, waiting for IP...\n"); 00172 DHCPClient dhcp; 00173 int err = dhcp.setup(timeout_ms); 00174 if (err == (-1)) { 00175 // printf("Timeout.\n"); 00176 return -1; 00177 } 00178 // printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]); 00179 ip = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3]; 00180 gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3]; 00181 netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3]; 00182 dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3]; 00183 return 0; 00184 }
Generated on Wed Jul 13 2022 07:54:50 by
1.7.2