This is the Interface library for WIZnet W5500 chip which forked of EthernetInterfaceW5500, WIZnetInterface and WIZ550ioInterface. This library has simple name as "W5500Interface". and can be used for Wiz550io users also.

Dependents:   EvrythngApi Websocket_Ethernet_HelloWorld_W5500 Websocket_Ethernet_W5500 CurrentWeatherData_W5500 ... more

Information

It has EthernetInterface class like official EthernetInterface , but uses Wiznet chip driver codes.

So this library can use only the WIZnet W5500 or WIZ550io users.

This library has referred to many project such as WIZ550ioInterface, WiflyInterface and WIZnet Library.

Thanks all.

Committer:
embeddist
Date:
Tue Apr 28 13:52:23 2015 +0000
Revision:
11:5499fa2d8898
Parent:
6:677dfa3984d1
Remove the setting of tx/rx buffer in SWReset

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bongjun 0:e11e8793c3ce 1 // EthernetInterface for W5500 2014/8/20
Bongjun 1:8f4374f932b4 2 /*
Bongjun 1:8f4374f932b4 3 // sample usgae.
Bongjun 1:8f4374f932b4 4 // copy below code block to main code.
Bongjun 1:8f4374f932b4 5
Bongjun 1:8f4374f932b4 6 #if defined(TARGET_LPC1114)
Bongjun 1:8f4374f932b4 7 SPI spi(dp2, dp1, dp6); // mosi, miso, sclk
Bongjun 1:8f4374f932b4 8 EthernetInterface eth(&spi, dp25, dp26); // spi, cs, reset
Bongjun 1:8f4374f932b4 9 wait(1); // 1 second for stable state
Bongjun 1:8f4374f932b4 10 #elif defined(TARGET_LPC1768)
Bongjun 1:8f4374f932b4 11 SPI spi(p11, p12, p13); // mosi, miso, sclk
Bongjun 1:8f4374f932b4 12 EthernetInterface eth(&spi, p14, p15); // spi, cs, reset
Bongjun 1:8f4374f932b4 13 wait(1); // 1 second for stable state
Bongjun 1:8f4374f932b4 14 #elif defined(TARGET_LPC11U68)
Bongjun 1:8f4374f932b4 15 SPI spi(P0_9, P0_8, P1_29); // mosi, miso, sclk
Bongjun 1:8f4374f932b4 16 EthernetInterface eth(&spi, P0_2, P1_28);//, nRESET(p9); // reset pin is dummy, don't affect any pin of WIZ550io
Bongjun 1:8f4374f932b4 17 spi.format(8,0); // 8bit, mode 0
Bongjun 1:8f4374f932b4 18 spi.frequency(7000000); // 7MHz
Bongjun 1:8f4374f932b4 19 wait(1); // 1 second for stable state
Bongjun 1:8f4374f932b4 20 #endif
Bongjun 1:8f4374f932b4 21
Bongjun 1:8f4374f932b4 22 eth.init(); //Use DHCP
Bongjun 1:8f4374f932b4 23 dbg.printf("init\r\n");
Bongjun 1:8f4374f932b4 24 eth.connect();
Bongjun 1:8f4374f932b4 25 dbg.printf("IP address: %s\r\n", eth.getIPAddress());
Bongjun 1:8f4374f932b4 26
Bongjun 1:8f4374f932b4 27 */
Bongjun 0:e11e8793c3ce 28
Bongjun 0:e11e8793c3ce 29 #include "EthernetInterface.h"
Bongjun 0:e11e8793c3ce 30 #include "DHCPClient.h"
embeddist 11:5499fa2d8898 31 //static char ip_string[17];
embeddist 11:5499fa2d8898 32 //static uint32_t ipaddr ;
Bongjun 0:e11e8793c3ce 33 EthernetInterface::EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
Bongjun 0:e11e8793c3ce 34 WIZnet_Chip(mosi, miso, sclk, cs, reset)
Bongjun 0:e11e8793c3ce 35 {
Bongjun 0:e11e8793c3ce 36 ip_set = false;
Bongjun 0:e11e8793c3ce 37 }
Bongjun 0:e11e8793c3ce 38
Bongjun 0:e11e8793c3ce 39 EthernetInterface::EthernetInterface(SPI* spi, PinName cs, PinName reset) :
Bongjun 0:e11e8793c3ce 40 WIZnet_Chip(spi, cs, reset)
Bongjun 0:e11e8793c3ce 41 {
Bongjun 0:e11e8793c3ce 42 ip_set = false;
Bongjun 0:e11e8793c3ce 43 }
Bongjun 0:e11e8793c3ce 44
kaizen 5:8aefaef88f79 45
Bongjun 0:e11e8793c3ce 46 int EthernetInterface::init()
Bongjun 0:e11e8793c3ce 47 {
Bongjun 0:e11e8793c3ce 48 dhcp = true;
Bongjun 0:e11e8793c3ce 49 //
Bongjun 0:e11e8793c3ce 50 //for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 51 //
Bongjun 0:e11e8793c3ce 52 reset();
Bongjun 0:e11e8793c3ce 53 return 0;
Bongjun 0:e11e8793c3ce 54 }
Bongjun 0:e11e8793c3ce 55
Bongjun 0:e11e8793c3ce 56 int EthernetInterface::init(uint8_t * mac)
Bongjun 0:e11e8793c3ce 57 {
Bongjun 0:e11e8793c3ce 58 dhcp = true;
Bongjun 0:e11e8793c3ce 59 //
Bongjun 0:e11e8793c3ce 60 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 61 //
Bongjun 0:e11e8793c3ce 62 reset();
Bongjun 0:e11e8793c3ce 63 setmac();
Bongjun 0:e11e8793c3ce 64 return 0;
Bongjun 0:e11e8793c3ce 65 }
Bongjun 0:e11e8793c3ce 66
Bongjun 0:e11e8793c3ce 67 // add this function, because sometimes no needed MAC address in init calling.
Bongjun 0:e11e8793c3ce 68 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway)
Bongjun 0:e11e8793c3ce 69 {
Bongjun 0:e11e8793c3ce 70 dhcp = false;
Bongjun 0:e11e8793c3ce 71 //
Bongjun 0:e11e8793c3ce 72 //for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 73 //
Bongjun 0:e11e8793c3ce 74 this->ip = str_to_ip(ip);
Bongjun 0:e11e8793c3ce 75 strcpy(ip_string, ip);
Bongjun 0:e11e8793c3ce 76 ip_set = true;
Bongjun 0:e11e8793c3ce 77 this->netmask = str_to_ip(mask);
Bongjun 0:e11e8793c3ce 78 this->gateway = str_to_ip(gateway);
Bongjun 0:e11e8793c3ce 79 reset();
Bongjun 0:e11e8793c3ce 80
Bongjun 0:e11e8793c3ce 81 // @Jul. 8. 2014 add code. should be called to write chip.
Bongjun 0:e11e8793c3ce 82 setip();
Bongjun 0:e11e8793c3ce 83
Bongjun 0:e11e8793c3ce 84 return 0;
Bongjun 0:e11e8793c3ce 85 }
Bongjun 0:e11e8793c3ce 86
Bongjun 0:e11e8793c3ce 87 int EthernetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway)
Bongjun 0:e11e8793c3ce 88 {
Bongjun 0:e11e8793c3ce 89 dhcp = false;
Bongjun 0:e11e8793c3ce 90 //
Bongjun 0:e11e8793c3ce 91 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 92 //
Bongjun 0:e11e8793c3ce 93 this->ip = str_to_ip(ip);
Bongjun 0:e11e8793c3ce 94 strcpy(ip_string, ip);
Bongjun 0:e11e8793c3ce 95 ip_set = true;
Bongjun 0:e11e8793c3ce 96 this->netmask = str_to_ip(mask);
Bongjun 0:e11e8793c3ce 97 this->gateway = str_to_ip(gateway);
Bongjun 0:e11e8793c3ce 98 reset();
Bongjun 0:e11e8793c3ce 99
Bongjun 0:e11e8793c3ce 100 // @Jul. 8. 2014 add code. should be called to write chip.
Bongjun 0:e11e8793c3ce 101 setmac();
Bongjun 0:e11e8793c3ce 102 setip();
Bongjun 0:e11e8793c3ce 103
Bongjun 0:e11e8793c3ce 104 return 0;
Bongjun 0:e11e8793c3ce 105 }
Bongjun 0:e11e8793c3ce 106
Bongjun 0:e11e8793c3ce 107 // Connect Bring the interface up, start DHCP if needed.
Bongjun 0:e11e8793c3ce 108 int EthernetInterface::connect()
Bongjun 0:e11e8793c3ce 109 {
Bongjun 0:e11e8793c3ce 110 if (dhcp) {
Bongjun 0:e11e8793c3ce 111 int r = IPrenew();
Bongjun 0:e11e8793c3ce 112 if (r < 0) {
Bongjun 0:e11e8793c3ce 113 return r;
Bongjun 0:e11e8793c3ce 114 }
Bongjun 0:e11e8793c3ce 115 }
Bongjun 0:e11e8793c3ce 116
Bongjun 0:e11e8793c3ce 117 if (WIZnet_Chip::setip() == false) return -1;
Bongjun 0:e11e8793c3ce 118 return 0;
Bongjun 0:e11e8793c3ce 119 }
Bongjun 0:e11e8793c3ce 120
Bongjun 0:e11e8793c3ce 121 // Disconnect Bring the interface down.
Bongjun 0:e11e8793c3ce 122 int EthernetInterface::disconnect()
Bongjun 0:e11e8793c3ce 123 {
Bongjun 0:e11e8793c3ce 124 if (WIZnet_Chip::disconnect() == false) return -1;
Bongjun 0:e11e8793c3ce 125 return 0;
Bongjun 0:e11e8793c3ce 126 }
Bongjun 0:e11e8793c3ce 127
embeddist 11:5499fa2d8898 128 /*
embeddist 11:5499fa2d8898 129 void EthernetInterface::getip(void)
embeddist 11:5499fa2d8898 130 {
embeddist 11:5499fa2d8898 131 //ip = 0x12345678;
embeddist 11:5499fa2d8898 132 ipaddr = reg_rd<uint32_t>(SIPR);
embeddist 11:5499fa2d8898 133 //snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ipaddr>>24)&0xff, (ipaddr>>16)&0xff, (ipaddr>>8)&0xff, ipaddr&0xff);
embeddist 11:5499fa2d8898 134 }
embeddist 11:5499fa2d8898 135 */
Bongjun 0:e11e8793c3ce 136 char* EthernetInterface::getIPAddress()
Bongjun 0:e11e8793c3ce 137 {
embeddist 11:5499fa2d8898 138 //Suint32_t ip = inet_getip(SIPR);//reg_rd<uint32_t>(SIPR);
embeddist 11:5499fa2d8898 139 //static uint32_t ip = getip();
embeddist 11:5499fa2d8898 140 //uint32_t ip = getip();
embeddist 11:5499fa2d8898 141
Bongjun 0:e11e8793c3ce 142 uint32_t ip = reg_rd<uint32_t>(SIPR);
embeddist 11:5499fa2d8898 143 snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Bongjun 0:e11e8793c3ce 144 return ip_string;
Bongjun 0:e11e8793c3ce 145 }
Bongjun 0:e11e8793c3ce 146
Bongjun 0:e11e8793c3ce 147 char* EthernetInterface::getNetworkMask()
Bongjun 0:e11e8793c3ce 148 {
Bongjun 0:e11e8793c3ce 149 uint32_t ip = reg_rd<uint32_t>(SUBR);
Bongjun 0:e11e8793c3ce 150 snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Bongjun 0:e11e8793c3ce 151 return mask_string;
Bongjun 0:e11e8793c3ce 152 }
Bongjun 0:e11e8793c3ce 153
Bongjun 0:e11e8793c3ce 154 char* EthernetInterface::getGateway()
Bongjun 0:e11e8793c3ce 155 {
Bongjun 0:e11e8793c3ce 156 uint32_t ip = reg_rd<uint32_t>(GAR);
Bongjun 0:e11e8793c3ce 157 snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Bongjun 0:e11e8793c3ce 158 return gw_string;
Bongjun 0:e11e8793c3ce 159 }
Bongjun 0:e11e8793c3ce 160
Bongjun 0:e11e8793c3ce 161 char* EthernetInterface::getMACAddress()
Bongjun 0:e11e8793c3ce 162 {
Bongjun 0:e11e8793c3ce 163 uint8_t mac[6];
Bongjun 0:e11e8793c3ce 164 reg_rd_mac(SHAR, mac);
Bongjun 0:e11e8793c3ce 165 snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Bongjun 0:e11e8793c3ce 166 return mac_string;
Bongjun 0:e11e8793c3ce 167 }
Bongjun 0:e11e8793c3ce 168
Bongjun 0:e11e8793c3ce 169 int EthernetInterface::IPrenew(int timeout_ms)
Bongjun 0:e11e8793c3ce 170 {
Bongjun 0:e11e8793c3ce 171 // printf("DHCP Started, waiting for IP...\n");
Bongjun 0:e11e8793c3ce 172 DHCPClient dhcp;
Bongjun 0:e11e8793c3ce 173 int err = dhcp.setup(timeout_ms);
Bongjun 0:e11e8793c3ce 174 if (err == (-1)) {
Bongjun 0:e11e8793c3ce 175 // printf("Timeout.\n");
Bongjun 0:e11e8793c3ce 176 return -1;
Bongjun 0:e11e8793c3ce 177 }
Bongjun 0:e11e8793c3ce 178 // printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
Bongjun 0:e11e8793c3ce 179 ip = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3];
Bongjun 0:e11e8793c3ce 180 gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
Bongjun 0:e11e8793c3ce 181 netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
Bongjun 0:e11e8793c3ce 182 dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
Bongjun 0:e11e8793c3ce 183 return 0;
Bongjun 0:e11e8793c3ce 184 }