This is Library using WIZnet Hardware TCP/IP chip, W5500 and WIZnet TCP/IP Offload Engine, W7500.

Dependents:   HTTP_SDcard_file_server_WIZwiki-W7500 SSD1306_smart_watch TCPEchoServer-WIZwiki-W7500 httpServer-WIZwiki-W7500 ... more

Fork of WIZnetInterface by Soohwan Kim

This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500 and TCP/IP Offload Engine, W7500.

[Users » embeddist » Code » WIZnetInterface](https://developer.mbed.org/users/embeddist/code/WIZnetInterface/) -> WIZnetInterface Lib will be released on [Team WIZnet](https://developer.mbed.org/teams/WIZnet/)

https://developer.mbed.org/media/cache/platforms/WIZwiki_W7500_enabled.JPG.250x250_q85.jpg

https://developer.mbed.org/media/cache/platforms/WIZwiki_W7500P_enabled2.JPG.250x250_q85.jpg

https://developer.mbed.org/media/cache/platforms/WIZwiki_W7500ECO_enabled2.JPG.250x250_q85.jpg

https://developer.mbed.org/media/cache/components/components/fetch.phpmediaoshw5500_ethernet_shieldw5500_main_picture2.png.200x200_q85.jpg

This library is an Ethernet Interface library port-based on [EthernetInterface](https://developer.mbed.org/users/mbed_official/code/EthernetInterface/docs/tip/).

For more detail, visit http://embeddist.blogspot.kr/2015/06/wiznetinterface-for-armmbed.html

Committer:
embeddist
Date:
Thu Oct 29 13:29:46 2015 +0000
Revision:
28:200e63e513a8
Parent:
27:1169973d836c
-Added defined TARGET_WIZwiki_W7500ECO ; -Fixed defined TARGET_WIZwiki_W7500P@eth_arch.h;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Soohwan Kim 0:6f28332c466f 1 /* Copyright (C) 2012 mbed.org, MIT License
Soohwan Kim 0:6f28332c466f 2 *
Soohwan Kim 0:6f28332c466f 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Soohwan Kim 0:6f28332c466f 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
Soohwan Kim 0:6f28332c466f 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
Soohwan Kim 0:6f28332c466f 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
Soohwan Kim 0:6f28332c466f 7 * furnished to do so, subject to the following conditions:
Soohwan Kim 0:6f28332c466f 8 *
Soohwan Kim 0:6f28332c466f 9 * The above copyright notice and this permission notice shall be included in all copies or
Soohwan Kim 0:6f28332c466f 10 * substantial portions of the Software.
Soohwan Kim 0:6f28332c466f 11 *
Soohwan Kim 0:6f28332c466f 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Soohwan Kim 0:6f28332c466f 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Soohwan Kim 0:6f28332c466f 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Soohwan Kim 0:6f28332c466f 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Soohwan Kim 0:6f28332c466f 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Soohwan Kim 0:6f28332c466f 17 */
Soohwan Kim 0:6f28332c466f 18
Soohwan Kim 0:6f28332c466f 19 #include "EthernetInterface.h"
Soohwan Kim 0:6f28332c466f 20 #include "DHCPClient.h"
embeddist 7:da6fcec0f3fe 21
embeddist 28:200e63e513a8 22 #if (not defined TARGET_WIZwiki_W7500) && (not defined TARGET_WIZwiki_W7500P) && (not defined TARGET_WIZwiki_W7500ECO)
embeddist 3:f8c6efc8bf83 23 EthernetInterface::EthernetInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
embeddist 3:f8c6efc8bf83 24 WIZnet_Chip(mosi, miso, sclk, cs, reset)
embeddist 3:f8c6efc8bf83 25 {
embeddist 3:f8c6efc8bf83 26 ip_set = false;
embeddist 3:f8c6efc8bf83 27 }
embeddist 3:f8c6efc8bf83 28
embeddist 3:f8c6efc8bf83 29 EthernetInterface::EthernetInterface(SPI* spi, PinName cs, PinName reset) :
embeddist 3:f8c6efc8bf83 30 WIZnet_Chip(spi, cs, reset)
embeddist 3:f8c6efc8bf83 31 {
embeddist 3:f8c6efc8bf83 32 ip_set = false;
embeddist 3:f8c6efc8bf83 33 }
embeddist 7:da6fcec0f3fe 34 #endif
Soohwan Kim 0:6f28332c466f 35
kaizen 20:bda61525ac71 36 int EthernetInterface::init()
kaizen 20:bda61525ac71 37 {
kaizen 20:bda61525ac71 38 dhcp = true;
kaizen 20:bda61525ac71 39 reset();
kaizen 20:bda61525ac71 40
kaizen 20:bda61525ac71 41 return 0;
kaizen 20:bda61525ac71 42 }
kaizen 20:bda61525ac71 43
Soohwan Kim 0:6f28332c466f 44 int EthernetInterface::init(uint8_t * mac)
Soohwan Kim 0:6f28332c466f 45 {
Soohwan Kim 0:6f28332c466f 46 dhcp = true;
Soohwan Kim 0:6f28332c466f 47 //
Soohwan Kim 0:6f28332c466f 48 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Soohwan Kim 0:6f28332c466f 49 //
Soohwan Kim 0:6f28332c466f 50 reset();
Soohwan Kim 0:6f28332c466f 51
Soohwan Kim 0:6f28332c466f 52 return 0;
Soohwan Kim 0:6f28332c466f 53 }
Soohwan Kim 0:6f28332c466f 54
Soohwan Kim 0:6f28332c466f 55 int EthernetInterface::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway)
Soohwan Kim 0:6f28332c466f 56 {
Soohwan Kim 0:6f28332c466f 57 dhcp = false;
Soohwan Kim 0:6f28332c466f 58 //
Soohwan Kim 0:6f28332c466f 59 for (int i =0; i < 6; i++) this->mac[i] = mac[i];
Soohwan Kim 0:6f28332c466f 60 //
Soohwan Kim 0:6f28332c466f 61 this->ip = str_to_ip(ip);
Soohwan Kim 0:6f28332c466f 62 strcpy(ip_string, ip);
Soohwan Kim 0:6f28332c466f 63 ip_set = true;
Soohwan Kim 0:6f28332c466f 64 this->netmask = str_to_ip(mask);
Soohwan Kim 0:6f28332c466f 65 this->gateway = str_to_ip(gateway);
Soohwan Kim 0:6f28332c466f 66 reset();
Soohwan Kim 4:4930f81bbe98 67
Soohwan Kim 4:4930f81bbe98 68 // @Jul. 8. 2014 add code. should be called to write chip.
Soohwan Kim 4:4930f81bbe98 69 setmac();
Soohwan Kim 4:4930f81bbe98 70 setip();
embeddist 3:f8c6efc8bf83 71
Soohwan Kim 0:6f28332c466f 72 return 0;
Soohwan Kim 0:6f28332c466f 73 }
Soohwan Kim 0:6f28332c466f 74
Soohwan Kim 0:6f28332c466f 75 // Connect Bring the interface up, start DHCP if needed.
Soohwan Kim 0:6f28332c466f 76 int EthernetInterface::connect()
Soohwan Kim 0:6f28332c466f 77 {
Soohwan Kim 0:6f28332c466f 78 if (dhcp) {
Soohwan Kim 0:6f28332c466f 79 int r = IPrenew();
Soohwan Kim 0:6f28332c466f 80 if (r < 0) {
Soohwan Kim 0:6f28332c466f 81 return r;
Soohwan Kim 0:6f28332c466f 82 }
Soohwan Kim 0:6f28332c466f 83 }
Soohwan Kim 0:6f28332c466f 84
Soohwan Kim 0:6f28332c466f 85 if (WIZnet_Chip::setip() == false) return -1;
Soohwan Kim 0:6f28332c466f 86 return 0;
Soohwan Kim 0:6f28332c466f 87 }
Soohwan Kim 0:6f28332c466f 88
Soohwan Kim 0:6f28332c466f 89 // Disconnect Bring the interface down.
Soohwan Kim 0:6f28332c466f 90 int EthernetInterface::disconnect()
Soohwan Kim 0:6f28332c466f 91 {
Soohwan Kim 0:6f28332c466f 92 //if (WIZnet_Chip::disconnect() == false) return -1;
Soohwan Kim 0:6f28332c466f 93 return 0;
Soohwan Kim 0:6f28332c466f 94 }
Soohwan Kim 0:6f28332c466f 95
Soohwan Kim 0:6f28332c466f 96
Soohwan Kim 0:6f28332c466f 97 char* EthernetInterface::getIPAddress()
Soohwan Kim 0:6f28332c466f 98 {
Soohwan Kim 0:6f28332c466f 99 uint32_t ip = reg_rd<uint32_t>(SIPR);
Soohwan Kim 0:6f28332c466f 100 snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d",
Soohwan Kim 0:6f28332c466f 101 (uint8_t)((ip>>24)&0xff),
Soohwan Kim 0:6f28332c466f 102 (uint8_t)((ip>>16)&0xff),
Soohwan Kim 0:6f28332c466f 103 (uint8_t)((ip>>8)&0xff),
Soohwan Kim 0:6f28332c466f 104 (uint8_t)(ip&0xff));
Soohwan Kim 0:6f28332c466f 105 return ip_string;
Soohwan Kim 0:6f28332c466f 106 }
Soohwan Kim 0:6f28332c466f 107
Soohwan Kim 0:6f28332c466f 108 char* EthernetInterface::getNetworkMask()
Soohwan Kim 0:6f28332c466f 109 {
Soohwan Kim 0:6f28332c466f 110 uint32_t ip = reg_rd<uint32_t>(SUBR);
Soohwan Kim 0:6f28332c466f 111 snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d",
Soohwan Kim 0:6f28332c466f 112 (uint8_t)((ip>>24)&0xff),
Soohwan Kim 0:6f28332c466f 113 (uint8_t)((ip>>16)&0xff),
Soohwan Kim 0:6f28332c466f 114 (uint8_t)((ip>>8)&0xff),
Soohwan Kim 0:6f28332c466f 115 (uint8_t)(ip&0xff));
Soohwan Kim 0:6f28332c466f 116 return mask_string;
Soohwan Kim 0:6f28332c466f 117 }
Soohwan Kim 0:6f28332c466f 118
Soohwan Kim 0:6f28332c466f 119 char* EthernetInterface::getGateway()
Soohwan Kim 0:6f28332c466f 120 {
Soohwan Kim 0:6f28332c466f 121 uint32_t ip = reg_rd<uint32_t>(GAR);
Soohwan Kim 0:6f28332c466f 122 snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d",
Soohwan Kim 0:6f28332c466f 123 (uint8_t)((ip>>24)&0xff),
Soohwan Kim 0:6f28332c466f 124 (uint8_t)((ip>>16)&0xff),
Soohwan Kim 0:6f28332c466f 125 (uint8_t)((ip>>8)&0xff),
Soohwan Kim 0:6f28332c466f 126 (uint8_t)(ip&0xff));
Soohwan Kim 0:6f28332c466f 127 return gw_string;
Soohwan Kim 0:6f28332c466f 128 }
Soohwan Kim 0:6f28332c466f 129
Soohwan Kim 0:6f28332c466f 130 char* EthernetInterface::getMACAddress()
Soohwan Kim 0:6f28332c466f 131 {
Soohwan Kim 0:6f28332c466f 132 uint8_t mac[6];
Soohwan Kim 0:6f28332c466f 133 reg_rd_mac(SHAR, mac);
Soohwan Kim 0:6f28332c466f 134 snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Soohwan Kim 0:6f28332c466f 135 //ethernet_address(mac_string);
Soohwan Kim 0:6f28332c466f 136 return mac_string;
Soohwan Kim 0:6f28332c466f 137
Soohwan Kim 0:6f28332c466f 138 }
Soohwan Kim 0:6f28332c466f 139
Soohwan Kim 0:6f28332c466f 140 int EthernetInterface::IPrenew(int timeout_ms)
Soohwan Kim 0:6f28332c466f 141 {
Soohwan Kim 0:6f28332c466f 142 DHCPClient dhcp;
Soohwan Kim 0:6f28332c466f 143 int err = dhcp.setup(timeout_ms);
Soohwan Kim 0:6f28332c466f 144 if (err == (-1)) {
Soohwan Kim 0:6f28332c466f 145 return -1;
Soohwan Kim 0:6f28332c466f 146 }
Soohwan Kim 0:6f28332c466f 147 // printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
Soohwan Kim 0:6f28332c466f 148 ip = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3];
Soohwan Kim 0:6f28332c466f 149 gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
Soohwan Kim 0:6f28332c466f 150 netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
Soohwan Kim 0:6f28332c466f 151 dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
Soohwan Kim 0:6f28332c466f 152 return 0;
Soohwan Kim 0:6f28332c466f 153 }
Soohwan Kim 0:6f28332c466f 154