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:
kaizen
Date:
Fri Sep 26 08:05:41 2014 +0000
Revision:
5:8aefaef88f79
Parent:
1:8f4374f932b4
Child:
6:677dfa3984d1
Modified for using MQTT protocol ( IBMIoTClient )

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"
Bongjun 0:e11e8793c3ce 31
Bongjun 0:e11e8793c3ce 32 EthernetInterface::EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
Bongjun 0:e11e8793c3ce 33 WIZnet_Chip(mosi, miso, sclk, cs, reset)
Bongjun 0:e11e8793c3ce 34 {
Bongjun 0:e11e8793c3ce 35 ip_set = false;
Bongjun 0:e11e8793c3ce 36 }
Bongjun 0:e11e8793c3ce 37
Bongjun 0:e11e8793c3ce 38 EthernetInterface::EthernetInterface(SPI* spi, PinName cs, PinName reset) :
Bongjun 0:e11e8793c3ce 39 WIZnet_Chip(spi, cs, reset)
Bongjun 0:e11e8793c3ce 40 {
Bongjun 0:e11e8793c3ce 41 ip_set = false;
Bongjun 0:e11e8793c3ce 42 }
Bongjun 0:e11e8793c3ce 43
kaizen 5:8aefaef88f79 44 ////#if define(TARGET_KL25Z)
kaizen 5:8aefaef88f79 45 //EthernetInterface::EthernetInterface() : WIZnet_Chip(D11, D12, D13, D10, D9)
kaizen 5:8aefaef88f79 46 //{
kaizen 5:8aefaef88f79 47 // wait(1); // 1 second for stable state
kaizen 5:8aefaef88f79 48 //
kaizen 5:8aefaef88f79 49 // ip_set = false;
kaizen 5:8aefaef88f79 50 //}
kaizen 5:8aefaef88f79 51
Bongjun 0:e11e8793c3ce 52 int EthernetInterface::init()
Bongjun 0:e11e8793c3ce 53 {
Bongjun 0:e11e8793c3ce 54 dhcp = true;
Bongjun 0:e11e8793c3ce 55 //
Bongjun 0:e11e8793c3ce 56 //for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 57 //
Bongjun 0:e11e8793c3ce 58 reset();
Bongjun 0:e11e8793c3ce 59 return 0;
Bongjun 0:e11e8793c3ce 60 }
Bongjun 0:e11e8793c3ce 61
Bongjun 0:e11e8793c3ce 62 int EthernetInterface::init(uint8_t * mac)
Bongjun 0:e11e8793c3ce 63 {
Bongjun 0:e11e8793c3ce 64 dhcp = true;
Bongjun 0:e11e8793c3ce 65 //
Bongjun 0:e11e8793c3ce 66 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 67 //
Bongjun 0:e11e8793c3ce 68 reset();
Bongjun 0:e11e8793c3ce 69 setmac();
Bongjun 0:e11e8793c3ce 70 return 0;
Bongjun 0:e11e8793c3ce 71 }
Bongjun 0:e11e8793c3ce 72
Bongjun 0:e11e8793c3ce 73 // add this function, because sometimes no needed MAC address in init calling.
Bongjun 0:e11e8793c3ce 74 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway)
Bongjun 0:e11e8793c3ce 75 {
Bongjun 0:e11e8793c3ce 76 dhcp = false;
Bongjun 0:e11e8793c3ce 77 //
Bongjun 0:e11e8793c3ce 78 //for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 79 //
Bongjun 0:e11e8793c3ce 80 this->ip = str_to_ip(ip);
Bongjun 0:e11e8793c3ce 81 strcpy(ip_string, ip);
Bongjun 0:e11e8793c3ce 82 ip_set = true;
Bongjun 0:e11e8793c3ce 83 this->netmask = str_to_ip(mask);
Bongjun 0:e11e8793c3ce 84 this->gateway = str_to_ip(gateway);
Bongjun 0:e11e8793c3ce 85 reset();
Bongjun 0:e11e8793c3ce 86
Bongjun 0:e11e8793c3ce 87 // @Jul. 8. 2014 add code. should be called to write chip.
Bongjun 0:e11e8793c3ce 88 setip();
Bongjun 0:e11e8793c3ce 89
Bongjun 0:e11e8793c3ce 90 return 0;
Bongjun 0:e11e8793c3ce 91 }
Bongjun 0:e11e8793c3ce 92
Bongjun 0:e11e8793c3ce 93 int EthernetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway)
Bongjun 0:e11e8793c3ce 94 {
Bongjun 0:e11e8793c3ce 95 dhcp = false;
Bongjun 0:e11e8793c3ce 96 //
Bongjun 0:e11e8793c3ce 97 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 98 //
Bongjun 0:e11e8793c3ce 99 this->ip = str_to_ip(ip);
Bongjun 0:e11e8793c3ce 100 strcpy(ip_string, ip);
Bongjun 0:e11e8793c3ce 101 ip_set = true;
Bongjun 0:e11e8793c3ce 102 this->netmask = str_to_ip(mask);
Bongjun 0:e11e8793c3ce 103 this->gateway = str_to_ip(gateway);
Bongjun 0:e11e8793c3ce 104 reset();
Bongjun 0:e11e8793c3ce 105
Bongjun 0:e11e8793c3ce 106 // @Jul. 8. 2014 add code. should be called to write chip.
Bongjun 0:e11e8793c3ce 107 setmac();
Bongjun 0:e11e8793c3ce 108 setip();
Bongjun 0:e11e8793c3ce 109
Bongjun 0:e11e8793c3ce 110 return 0;
Bongjun 0:e11e8793c3ce 111 }
Bongjun 0:e11e8793c3ce 112
Bongjun 0:e11e8793c3ce 113 // Connect Bring the interface up, start DHCP if needed.
Bongjun 0:e11e8793c3ce 114 int EthernetInterface::connect()
Bongjun 0:e11e8793c3ce 115 {
Bongjun 0:e11e8793c3ce 116 if (dhcp) {
Bongjun 0:e11e8793c3ce 117 int r = IPrenew();
Bongjun 0:e11e8793c3ce 118 if (r < 0) {
Bongjun 0:e11e8793c3ce 119 return r;
Bongjun 0:e11e8793c3ce 120 }
Bongjun 0:e11e8793c3ce 121 }
Bongjun 0:e11e8793c3ce 122
Bongjun 0:e11e8793c3ce 123 if (WIZnet_Chip::setip() == false) return -1;
Bongjun 0:e11e8793c3ce 124 return 0;
Bongjun 0:e11e8793c3ce 125 }
Bongjun 0:e11e8793c3ce 126
Bongjun 0:e11e8793c3ce 127 // Disconnect Bring the interface down.
Bongjun 0:e11e8793c3ce 128 int EthernetInterface::disconnect()
Bongjun 0:e11e8793c3ce 129 {
Bongjun 0:e11e8793c3ce 130 if (WIZnet_Chip::disconnect() == false) return -1;
Bongjun 0:e11e8793c3ce 131 return 0;
Bongjun 0:e11e8793c3ce 132 }
Bongjun 0:e11e8793c3ce 133
Bongjun 0:e11e8793c3ce 134 char* EthernetInterface::getIPAddress()
Bongjun 0:e11e8793c3ce 135 {
Bongjun 0:e11e8793c3ce 136 uint32_t ip = reg_rd<uint32_t>(SIPR);
Bongjun 0:e11e8793c3ce 137 snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Bongjun 0:e11e8793c3ce 138 return ip_string;
Bongjun 0:e11e8793c3ce 139 }
Bongjun 0:e11e8793c3ce 140
Bongjun 0:e11e8793c3ce 141 char* EthernetInterface::getNetworkMask()
Bongjun 0:e11e8793c3ce 142 {
Bongjun 0:e11e8793c3ce 143 uint32_t ip = reg_rd<uint32_t>(SUBR);
Bongjun 0:e11e8793c3ce 144 snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Bongjun 0:e11e8793c3ce 145 return mask_string;
Bongjun 0:e11e8793c3ce 146 }
Bongjun 0:e11e8793c3ce 147
Bongjun 0:e11e8793c3ce 148 char* EthernetInterface::getGateway()
Bongjun 0:e11e8793c3ce 149 {
Bongjun 0:e11e8793c3ce 150 uint32_t ip = reg_rd<uint32_t>(GAR);
Bongjun 0:e11e8793c3ce 151 snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Bongjun 0:e11e8793c3ce 152 return gw_string;
Bongjun 0:e11e8793c3ce 153 }
Bongjun 0:e11e8793c3ce 154
Bongjun 0:e11e8793c3ce 155 char* EthernetInterface::getMACAddress()
Bongjun 0:e11e8793c3ce 156 {
Bongjun 0:e11e8793c3ce 157 uint8_t mac[6];
Bongjun 0:e11e8793c3ce 158 reg_rd_mac(SHAR, mac);
Bongjun 0:e11e8793c3ce 159 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 160 return mac_string;
Bongjun 0:e11e8793c3ce 161 }
Bongjun 0:e11e8793c3ce 162
Bongjun 0:e11e8793c3ce 163 int EthernetInterface::IPrenew(int timeout_ms)
Bongjun 0:e11e8793c3ce 164 {
Bongjun 0:e11e8793c3ce 165 // printf("DHCP Started, waiting for IP...\n");
Bongjun 0:e11e8793c3ce 166 DHCPClient dhcp;
Bongjun 0:e11e8793c3ce 167 int err = dhcp.setup(timeout_ms);
Bongjun 0:e11e8793c3ce 168 if (err == (-1)) {
Bongjun 0:e11e8793c3ce 169 // printf("Timeout.\n");
Bongjun 0:e11e8793c3ce 170 return -1;
Bongjun 0:e11e8793c3ce 171 }
Bongjun 0:e11e8793c3ce 172 // printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
Bongjun 0:e11e8793c3ce 173 ip = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3];
Bongjun 0:e11e8793c3ce 174 gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
Bongjun 0:e11e8793c3ce 175 netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
Bongjun 0:e11e8793c3ce 176 dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
Bongjun 0:e11e8793c3ce 177 return 0;
Bongjun 0:e11e8793c3ce 178 }