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:
Bongjun
Date:
Wed Aug 20 00:28:37 2014 +0000
Revision:
0:e11e8793c3ce
Child:
1:8f4374f932b4
first release.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bongjun 0:e11e8793c3ce 1 // EthernetInterface for W5500 2014/8/20
Bongjun 0:e11e8793c3ce 2
Bongjun 0:e11e8793c3ce 3 #include "EthernetInterface.h"
Bongjun 0:e11e8793c3ce 4 #include "DHCPClient.h"
Bongjun 0:e11e8793c3ce 5
Bongjun 0:e11e8793c3ce 6 EthernetInterface::EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
Bongjun 0:e11e8793c3ce 7 WIZnet_Chip(mosi, miso, sclk, cs, reset)
Bongjun 0:e11e8793c3ce 8 {
Bongjun 0:e11e8793c3ce 9 ip_set = false;
Bongjun 0:e11e8793c3ce 10 }
Bongjun 0:e11e8793c3ce 11
Bongjun 0:e11e8793c3ce 12 EthernetInterface::EthernetInterface(SPI* spi, PinName cs, PinName reset) :
Bongjun 0:e11e8793c3ce 13 WIZnet_Chip(spi, cs, reset)
Bongjun 0:e11e8793c3ce 14 {
Bongjun 0:e11e8793c3ce 15 ip_set = false;
Bongjun 0:e11e8793c3ce 16 }
Bongjun 0:e11e8793c3ce 17
Bongjun 0:e11e8793c3ce 18 int EthernetInterface::init()
Bongjun 0:e11e8793c3ce 19 {
Bongjun 0:e11e8793c3ce 20 dhcp = true;
Bongjun 0:e11e8793c3ce 21 //
Bongjun 0:e11e8793c3ce 22 //for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 23 //
Bongjun 0:e11e8793c3ce 24 reset();
Bongjun 0:e11e8793c3ce 25 return 0;
Bongjun 0:e11e8793c3ce 26 }
Bongjun 0:e11e8793c3ce 27
Bongjun 0:e11e8793c3ce 28 int EthernetInterface::init(uint8_t * mac)
Bongjun 0:e11e8793c3ce 29 {
Bongjun 0:e11e8793c3ce 30 dhcp = true;
Bongjun 0:e11e8793c3ce 31 //
Bongjun 0:e11e8793c3ce 32 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 33 //
Bongjun 0:e11e8793c3ce 34 reset();
Bongjun 0:e11e8793c3ce 35 setmac();
Bongjun 0:e11e8793c3ce 36 return 0;
Bongjun 0:e11e8793c3ce 37 }
Bongjun 0:e11e8793c3ce 38
Bongjun 0:e11e8793c3ce 39 // add this function, because sometimes no needed MAC address in init calling.
Bongjun 0:e11e8793c3ce 40 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway)
Bongjun 0:e11e8793c3ce 41 {
Bongjun 0:e11e8793c3ce 42 dhcp = false;
Bongjun 0:e11e8793c3ce 43 //
Bongjun 0:e11e8793c3ce 44 //for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 45 //
Bongjun 0:e11e8793c3ce 46 this->ip = str_to_ip(ip);
Bongjun 0:e11e8793c3ce 47 strcpy(ip_string, ip);
Bongjun 0:e11e8793c3ce 48 ip_set = true;
Bongjun 0:e11e8793c3ce 49 this->netmask = str_to_ip(mask);
Bongjun 0:e11e8793c3ce 50 this->gateway = str_to_ip(gateway);
Bongjun 0:e11e8793c3ce 51 reset();
Bongjun 0:e11e8793c3ce 52
Bongjun 0:e11e8793c3ce 53 // @Jul. 8. 2014 add code. should be called to write chip.
Bongjun 0:e11e8793c3ce 54 setip();
Bongjun 0:e11e8793c3ce 55
Bongjun 0:e11e8793c3ce 56 return 0;
Bongjun 0:e11e8793c3ce 57 }
Bongjun 0:e11e8793c3ce 58
Bongjun 0:e11e8793c3ce 59 int EthernetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway)
Bongjun 0:e11e8793c3ce 60 {
Bongjun 0:e11e8793c3ce 61 dhcp = false;
Bongjun 0:e11e8793c3ce 62 //
Bongjun 0:e11e8793c3ce 63 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Bongjun 0:e11e8793c3ce 64 //
Bongjun 0:e11e8793c3ce 65 this->ip = str_to_ip(ip);
Bongjun 0:e11e8793c3ce 66 strcpy(ip_string, ip);
Bongjun 0:e11e8793c3ce 67 ip_set = true;
Bongjun 0:e11e8793c3ce 68 this->netmask = str_to_ip(mask);
Bongjun 0:e11e8793c3ce 69 this->gateway = str_to_ip(gateway);
Bongjun 0:e11e8793c3ce 70 reset();
Bongjun 0:e11e8793c3ce 71
Bongjun 0:e11e8793c3ce 72 // @Jul. 8. 2014 add code. should be called to write chip.
Bongjun 0:e11e8793c3ce 73 setmac();
Bongjun 0:e11e8793c3ce 74 setip();
Bongjun 0:e11e8793c3ce 75
Bongjun 0:e11e8793c3ce 76 return 0;
Bongjun 0:e11e8793c3ce 77 }
Bongjun 0:e11e8793c3ce 78
Bongjun 0:e11e8793c3ce 79 // Connect Bring the interface up, start DHCP if needed.
Bongjun 0:e11e8793c3ce 80 int EthernetInterface::connect()
Bongjun 0:e11e8793c3ce 81 {
Bongjun 0:e11e8793c3ce 82 if (dhcp) {
Bongjun 0:e11e8793c3ce 83 int r = IPrenew();
Bongjun 0:e11e8793c3ce 84 if (r < 0) {
Bongjun 0:e11e8793c3ce 85 return r;
Bongjun 0:e11e8793c3ce 86 }
Bongjun 0:e11e8793c3ce 87 }
Bongjun 0:e11e8793c3ce 88
Bongjun 0:e11e8793c3ce 89 if (WIZnet_Chip::setip() == false) return -1;
Bongjun 0:e11e8793c3ce 90 return 0;
Bongjun 0:e11e8793c3ce 91 }
Bongjun 0:e11e8793c3ce 92
Bongjun 0:e11e8793c3ce 93 // Disconnect Bring the interface down.
Bongjun 0:e11e8793c3ce 94 int EthernetInterface::disconnect()
Bongjun 0:e11e8793c3ce 95 {
Bongjun 0:e11e8793c3ce 96 if (WIZnet_Chip::disconnect() == false) return -1;
Bongjun 0:e11e8793c3ce 97 return 0;
Bongjun 0:e11e8793c3ce 98 }
Bongjun 0:e11e8793c3ce 99
Bongjun 0:e11e8793c3ce 100 char* EthernetInterface::getIPAddress()
Bongjun 0:e11e8793c3ce 101 {
Bongjun 0:e11e8793c3ce 102 uint32_t ip = reg_rd<uint32_t>(SIPR);
Bongjun 0:e11e8793c3ce 103 snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Bongjun 0:e11e8793c3ce 104 return ip_string;
Bongjun 0:e11e8793c3ce 105 }
Bongjun 0:e11e8793c3ce 106
Bongjun 0:e11e8793c3ce 107 char* EthernetInterface::getNetworkMask()
Bongjun 0:e11e8793c3ce 108 {
Bongjun 0:e11e8793c3ce 109 uint32_t ip = reg_rd<uint32_t>(SUBR);
Bongjun 0:e11e8793c3ce 110 snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Bongjun 0:e11e8793c3ce 111 return mask_string;
Bongjun 0:e11e8793c3ce 112 }
Bongjun 0:e11e8793c3ce 113
Bongjun 0:e11e8793c3ce 114 char* EthernetInterface::getGateway()
Bongjun 0:e11e8793c3ce 115 {
Bongjun 0:e11e8793c3ce 116 uint32_t ip = reg_rd<uint32_t>(GAR);
Bongjun 0:e11e8793c3ce 117 snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
Bongjun 0:e11e8793c3ce 118 return gw_string;
Bongjun 0:e11e8793c3ce 119 }
Bongjun 0:e11e8793c3ce 120
Bongjun 0:e11e8793c3ce 121 char* EthernetInterface::getMACAddress()
Bongjun 0:e11e8793c3ce 122 {
Bongjun 0:e11e8793c3ce 123 uint8_t mac[6];
Bongjun 0:e11e8793c3ce 124 reg_rd_mac(SHAR, mac);
Bongjun 0:e11e8793c3ce 125 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 126 return mac_string;
Bongjun 0:e11e8793c3ce 127 }
Bongjun 0:e11e8793c3ce 128
Bongjun 0:e11e8793c3ce 129 int EthernetInterface::IPrenew(int timeout_ms)
Bongjun 0:e11e8793c3ce 130 {
Bongjun 0:e11e8793c3ce 131 // printf("DHCP Started, waiting for IP...\n");
Bongjun 0:e11e8793c3ce 132 DHCPClient dhcp;
Bongjun 0:e11e8793c3ce 133 int err = dhcp.setup(timeout_ms);
Bongjun 0:e11e8793c3ce 134 if (err == (-1)) {
Bongjun 0:e11e8793c3ce 135 // printf("Timeout.\n");
Bongjun 0:e11e8793c3ce 136 return -1;
Bongjun 0:e11e8793c3ce 137 }
Bongjun 0:e11e8793c3ce 138 // printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
Bongjun 0:e11e8793c3ce 139 ip = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3];
Bongjun 0:e11e8793c3ce 140 gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
Bongjun 0:e11e8793c3ce 141 netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
Bongjun 0:e11e8793c3ce 142 dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
Bongjun 0:e11e8793c3ce 143 return 0;
Bongjun 0:e11e8793c3ce 144 }