Ethernet interface for W5500 with bug fixed in socket::close()

Fork of EthernetInterfaceW5500 by W5500-Ethernet-Interface Makers

Files at this revision

API Documentation at this revision

Comitter:
Bongjun
Date:
Thu Jul 17 07:10:36 2014 +0000
Parent:
9:615198a7b82b
Child:
11:274fda7476d0
Commit message:
Some update & code clean for W5500 only refer from WIZ550ioInterface, WIZnetLibrary and WiflyInterface.

Changed in this revision

DHCPClient/DHCPClient.cpp Show annotated file Show diff for this revision Revisions of this file
DHCPClient/DHCPClient.h Show annotated file Show diff for this revision Revisions of this file
DNSClient/DNSClient.cpp Show annotated file Show diff for this revision Revisions of this file
EthernetInterfaceW5500.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Endpoint.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Endpoint.h Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/Socket.h Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketConnection.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketConnection.h Show annotated file Show diff for this revision Revisions of this file
Socket/TCPSocketServer.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.cpp Show annotated file Show diff for this revision Revisions of this file
Socket/UDPSocket.h Show annotated file Show diff for this revision Revisions of this file
WIZ550ioInterface.cpp Show diff for this revision Revisions of this file
WIZ550ioInterface.h Show annotated file Show diff for this revision Revisions of this file
WIZ820io/WIZ820io.cpp Show diff for this revision Revisions of this file
WIZ820io/WIZ820io.h Show diff for this revision Revisions of this file
WIZnet/W5500.cpp Show annotated file Show diff for this revision Revisions of this file
WIZnet/W5500.h Show annotated file Show diff for this revision Revisions of this file
WIZnet/wiznet.h Show annotated file Show diff for this revision Revisions of this file
--- a/DHCPClient/DHCPClient.cpp	Mon Dec 23 13:51:35 2013 +0000
+++ b/DHCPClient/DHCPClient.cpp	Thu Jul 17 07:10:36 2014 +0000
@@ -157,7 +157,7 @@
 
 int DHCPClient::setup(int timeout_ms)
 {
-    eth = WIZ820io::getInstance();
+    eth = WIZnet_Chip::getInstance();
     if (eth == NULL) {
         return -1;
     }    
--- a/DHCPClient/DHCPClient.h	Mon Dec 23 13:51:35 2013 +0000
+++ b/DHCPClient/DHCPClient.h	Thu Jul 17 07:10:36 2014 +0000
@@ -1,7 +1,7 @@
 // DHCPClient.h 2013/4/10
 #ifndef DHCPCLIENT_H
 #define DHCPCLIENT_H
-#include "WIZ820io.h"
+#include "wiznet.h"
 #include "UDPSocket.h"
 
 #define DHCP_OFFSET_OP 0
@@ -49,6 +49,6 @@
     int m_retry;
     uint8_t m_buf[DHCP_MAX_PACKET_SIZE];
     int m_pos;
-    WIZ820io* eth;
+    WIZnet_Chip* eth;
 };
 #endif //DHCPCLIENT_H
--- a/DNSClient/DNSClient.cpp	Mon Dec 23 13:51:35 2013 +0000
+++ b/DNSClient/DNSClient.cpp	Thu Jul 17 07:10:36 2014 +0000
@@ -4,7 +4,7 @@
 #include "DNSClient.h"
 #include "UDPSocket.h"
 #include "dnsname.h"
-#include "WIZ820io.h"
+#include "wiznet.h"
 
 #define DBG_DNS 0
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterfaceW5500.cpp	Thu Jul 17 07:10:36 2014 +0000
@@ -0,0 +1,144 @@
+// EthernetInterfaceW5500.cpp 2014/7/17
+
+#include "EthernetInterfaceW5500.h"
+#include "DHCPClient.h"
+
+EthernetInterfaceW5500::EthernetInterfaceW5500(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
+        WIZnet_Chip(mosi, miso, sclk, cs, reset)
+{
+    ip_set = false;
+}
+
+EthernetInterfaceW5500::EthernetInterfaceW5500(SPI* spi, PinName cs, PinName reset) :
+        WIZnet_Chip(spi, cs, reset)
+{
+    ip_set = false;
+}
+
+int EthernetInterfaceW5500::init()
+{
+    dhcp = true;
+    //
+    //for (int i =0; i < 6; i++) this->mac[i] = mac[i];
+    //
+    reset();
+    return 0;
+}
+
+int EthernetInterfaceW5500::init(uint8_t * mac)
+{
+    dhcp = true;
+    //
+    for (int i =0; i < 6; i++) this->mac[i] = mac[i];
+    //
+    reset();
+    setmac();    
+    return 0;
+}
+
+// add this function, because sometimes no needed MAC address in init calling.
+int EthernetInterfaceW5500::init(const char* ip, const char* mask, const char* gateway)
+{
+    dhcp = false;
+    //
+    //for (int i =0; i < 6; i++) this->mac[i] = mac[i];
+    //
+    this->ip = str_to_ip(ip);
+    strcpy(ip_string, ip);
+    ip_set = true;
+    this->netmask = str_to_ip(mask);
+    this->gateway = str_to_ip(gateway);
+    reset();
+    
+    // @Jul. 8. 2014 add code. should be called to write chip.
+    setip(); 
+    
+    return 0;
+}
+
+int EthernetInterfaceW5500::init(uint8_t * mac, const char* ip, const char* mask, const char* gateway)
+{
+    dhcp = false;
+    //
+    for (int i =0; i < 6; i++) this->mac[i] = mac[i];
+    //
+    this->ip = str_to_ip(ip);
+    strcpy(ip_string, ip);
+    ip_set = true;
+    this->netmask = str_to_ip(mask);
+    this->gateway = str_to_ip(gateway);
+    reset();
+
+    // @Jul. 8. 2014 add code. should be called to write chip.
+    setmac();
+    setip();
+    
+    return 0;
+}
+
+// Connect Bring the interface up, start DHCP if needed.
+int EthernetInterfaceW5500::connect()
+{
+    if (dhcp) {
+        int r = IPrenew();
+        if (r < 0) {
+            return r;
+        }
+    }
+    
+    if (WIZnet_Chip::setip() == false) return -1;
+    return 0;
+}
+
+// Disconnect Bring the interface down.
+int EthernetInterfaceW5500::disconnect()
+{
+    if (WIZnet_Chip::disconnect() == false) return -1;
+    return 0;
+}
+
+char* EthernetInterfaceW5500::getIPAddress()
+{
+    uint32_t ip = reg_rd<uint32_t>(SIPR);
+    snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
+    return ip_string;
+}
+
+char* EthernetInterfaceW5500::getNetworkMask()
+{
+    uint32_t ip = reg_rd<uint32_t>(SUBR);
+    snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
+    return mask_string;
+}
+
+char* EthernetInterfaceW5500::getGateway()
+{
+    uint32_t ip = reg_rd<uint32_t>(GAR);
+    snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
+    return gw_string;
+}
+
+char* EthernetInterfaceW5500::getMACAddress()
+{
+    uint8_t mac[6];
+    reg_rd_mac(SHAR, mac);
+    snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
+    return mac_string; 
+}
+
+int EthernetInterfaceW5500::IPrenew(int timeout_ms)
+{
+//    printf("DHCP Started, waiting for IP...\n");  
+    DHCPClient dhcp;
+    int err = dhcp.setup(timeout_ms);
+    if (err == (-1)) {
+//        printf("Timeout.\n");
+        return -1;
+    }
+//    printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
+    ip      = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3];
+    gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
+    netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
+    dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
+    return 0;
+}
--- a/Socket/Endpoint.cpp	Mon Dec 23 13:51:35 2013 +0000
+++ b/Socket/Endpoint.cpp	Thu Jul 17 07:10:36 2014 +0000
@@ -17,21 +17,28 @@
  */
 #include "Socket/Socket.h"
 #include "Socket/Endpoint.h"
+//#include <cstring>
+//#include <cstdio>
 
-Endpoint::Endpoint()  {
+Endpoint::Endpoint()
+{
     reset_address();
 }
 Endpoint::~Endpoint() {}
 
-void Endpoint::reset_address(void) {
+void Endpoint::reset_address(void)
+{
     _ipAddress[0] = '\0';
     _port = 0;
 }
 
-int Endpoint::set_address(const char* host, const int port) {
+int Endpoint::set_address(const char* host, const int port)
+{
     //Resolve DNS address or populate hard-coded IP address
-    WIZ820io* eth = WIZ820io::getInstance();
+    // added code for searching instance of WIZnet chip. refer from Wifly Library.
+    WIZnet_Chip* eth = WIZnet_Chip::getInstance();
     if (eth == NULL) {
+        error("Endpoint constructor error: no WIZnet chip instance available!\r\n");
         return -1;
     }
     uint32_t addr;
@@ -43,10 +50,12 @@
     return 0;
 }
 
-char* Endpoint::get_address() {
+char* Endpoint::get_address()
+{
     return _ipAddress;
 }
 
-int   Endpoint::get_port() {
+int   Endpoint::get_port()
+{
     return _port;
 }
--- a/Socket/Endpoint.h	Mon Dec 23 13:51:35 2013 +0000
+++ b/Socket/Endpoint.h	Thu Jul 17 07:10:36 2014 +0000
@@ -18,7 +18,7 @@
 #ifndef ENDPOINT_H
 #define ENDPOINT_H
 
-#include "WIZ820io.h"
+#include "wiznet.h"
 
 class UDPSocket;
 
--- a/Socket/Socket.cpp	Mon Dec 23 13:51:35 2013 +0000
+++ b/Socket/Socket.cpp	Thu Jul 17 07:10:36 2014 +0000
@@ -15,25 +15,31 @@
  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
- 
+
 #include "Socket.h"
 
-Socket::Socket() : _sock_fd(-1),_blocking(true), _timeout(1500) {
-    eth = WIZ820io::getInstance();
+Socket::Socket() : _sock_fd(-1),_blocking(true), _timeout(1500)
+{
+    eth = WIZnet_Chip::getInstance();
     if (eth == NULL) {
-        error("Socket constructor error: no WIZ8820io instance available!\r\n");
-    }       
+        error("Socket constructor error: no W5500 instance available!\r\n");
+    }
 }
 
-void Socket::set_blocking(bool blocking, unsigned int timeout) {
+void Socket::set_blocking(bool blocking, unsigned int timeout)
+{
     _blocking = blocking;
     _timeout = timeout;
 }
 
-int Socket::close() {
+int Socket::close()
+{
+    // add this code refer from EthernetInterface.
+    _sock_fd = -1;
     return (eth->close(_sock_fd)) ? 0 : -1;
 }
 
-Socket::~Socket() {
+Socket::~Socket()
+{
     close(); //Don't want to leak
 }
--- a/Socket/Socket.h	Mon Dec 23 13:51:35 2013 +0000
+++ b/Socket/Socket.h	Thu Jul 17 07:10:36 2014 +0000
@@ -18,7 +18,7 @@
 #ifndef SOCKET_H_
 #define SOCKET_H_
 
-#include "WIZ820io.h"
+#include "wiznet.h"
 
 #define htons(x) __REV16(x)
 #define ntohs(x) __REV16(x)
@@ -27,30 +27,31 @@
 
 /** Socket file descriptor and select wrapper
   */
-class Socket {
+class Socket
+{
 public:
     /** Socket
      */
     Socket();
-    
+
     /** Set blocking or non-blocking mode of the socket and a timeout on
         blocking socket operations
     \param blocking  true for blocking mode, false for non-blocking mode.
     \param timeout   timeout in ms [Default: (1500)ms].
     */
     void set_blocking(bool blocking, unsigned int timeout=1500);
-    
+
     /** Close the socket file descriptor
      */
     int close();
-    
+
     ~Socket();
-    
+
 protected:
     int _sock_fd;
     bool _blocking;
     int _timeout;
-    WIZ820io* eth;
+    WIZnet_Chip* eth;
 };
 
 
--- a/Socket/TCPSocketConnection.cpp	Mon Dec 23 13:51:35 2013 +0000
+++ b/Socket/TCPSocketConnection.cpp	Thu Jul 17 07:10:36 2014 +0000
@@ -15,15 +15,21 @@
  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  */
+#include "TCPSocketConnection.h"
+#include <cstring>
 
-#include "TCPSocketConnection.h"
+using std::memset;
+using std::memcpy;
 
-TCPSocketConnection::TCPSocketConnection()
+// not a big code.
+// refer from EthernetInterface by mbed official driver
+TCPSocketConnection::TCPSocketConnection() :
+    _is_connected(false)
 {
 }
 
 int TCPSocketConnection::connect(const char* host, const int port)
-{  
+{
     if (_sock_fd < 0) {
         _sock_fd = eth->new_socket();
         if (_sock_fd < 0) {
@@ -32,20 +38,29 @@
     }
     if (set_address(host, port) != 0) {
         return -1;
-    }    
+    }
     if (!eth->connect(_sock_fd, get_address(), port)) {
         return -1;
     }
+    // add code refer from EthernetInterface.
+    _is_connected = true;
+
     return 0;
 }
 
 bool TCPSocketConnection::is_connected(void)
 {
-    return eth->is_connected(_sock_fd);
+    // force update recent state.
+    _is_connected = eth->is_connected(_sock_fd);
+    return _is_connected;
 }
 
 int TCPSocketConnection::send(char* data, int length)
 {
+    // add to cover exception.
+    if ((_sock_fd < 0) || !_is_connected)
+        return -1;
+
     int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout);
     if (size < 0) {
         return -1;
@@ -80,6 +95,10 @@
 // -1 if unsuccessful, else number of bytes received
 int TCPSocketConnection::receive(char* data, int length)
 {
+    // add to cover exception.
+    if ((_sock_fd < 0) || !_is_connected)
+        return -1;
+
     int size = eth->wait_readable(_sock_fd, _blocking ? -1 : _timeout);
     if (size < 0) {
         return -1;
@@ -109,4 +128,4 @@
         readLen += ret;
     }
     return readLen;
-}
+}
\ No newline at end of file
--- a/Socket/TCPSocketConnection.h	Mon Dec 23 13:51:35 2013 +0000
+++ b/Socket/TCPSocketConnection.h	Thu Jul 17 07:10:36 2014 +0000
@@ -19,59 +19,62 @@
 #ifndef TCPSOCKET_H
 #define TCPSOCKET_H
 
-#include "Socket.h"
-#include "Endpoint.h"
-
+#include "Socket/Socket.h"
+#include "Socket/Endpoint.h"
 /**
 TCP socket connection
 */
-class TCPSocketConnection: public Socket, public Endpoint {
+class TCPSocketConnection: public Socket, public Endpoint
+{
     friend class TCPSocketServer;
-    
+
 public:
     /** TCP socket connection
     */
     TCPSocketConnection();
-    
+
     /** Connects this TCP socket to the server
     \param host The host to connect to. It can either be an IP Address or a hostname that will be resolved with DNS.
     \param port The host's port to connect to.
     \return 0 on success, -1 on failure.
     */
     int connect(const char* host, const int port);
-    
+
     /** Check if the socket is connected
     \return true if connected, false otherwise.
     */
     bool is_connected(void);
-    
+
     /** Send data to the remote host.
     \param data The buffer to send to the host.
     \param length The length of the buffer to send.
     \return the number of written bytes on success (>=0) or -1 on failure
      */
     int send(char* data, int length);
-    
+
     /** Send all the data to the remote host.
     \param data The buffer to send to the host.
     \param length The length of the buffer to send.
     \return the number of written bytes on success (>=0) or -1 on failure
     */
     int send_all(char* data, int length);
-    
+
     /** Receive data from the remote host.
     \param data The buffer in which to store the data received from the host.
     \param length The maximum length of the buffer.
     \return the number of received bytes on success (>=0) or -1 on failure
      */
     int receive(char* data, int length);
-    
+
     /** Receive all the data from the remote host.
     \param data The buffer in which to store the data received from the host.
     \param length The maximum length of the buffer.
     \return the number of received bytes on success (>=0) or -1 on failure
     */
     int receive_all(char* data, int length);
+
+private:
+    bool _is_connected;
 };
 
 #endif
--- a/Socket/TCPSocketServer.cpp	Mon Dec 23 13:51:35 2013 +0000
+++ b/Socket/TCPSocketServer.cpp	Thu Jul 17 07:10:36 2014 +0000
@@ -21,7 +21,8 @@
 TCPSocketServer::TCPSocketServer() {}
 
 // Server initialization
-int TCPSocketServer::bind(int port) {
+int TCPSocketServer::bind(int port)
+{
     if (_sock_fd < 0) {
         _sock_fd = eth->new_socket();
         if (_sock_fd < 0) {
@@ -37,7 +38,8 @@
     return 0;
 }
 
-int TCPSocketServer::listen(int backlog) {
+int TCPSocketServer::listen(int backlog)
+{
     if (_sock_fd < 0) {
         return -1;
     }
@@ -49,7 +51,8 @@
 }
 
 
-int TCPSocketServer::accept(TCPSocketConnection& connection) {
+int TCPSocketServer::accept(TCPSocketConnection& connection)
+{
     if (_sock_fd < 0) {
         return -1;
     }
@@ -69,6 +72,7 @@
     snprintf(host, sizeof(host), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
     uint16_t port = eth->sreg<uint16_t>(_sock_fd, Sn_DPORT);
     connection._sock_fd = _sock_fd;
+    connection._is_connected = true;
     connection.set_address(host, port);
     return 0;
 }
--- a/Socket/UDPSocket.cpp	Mon Dec 23 13:51:35 2013 +0000
+++ b/Socket/UDPSocket.cpp	Thu Jul 17 07:10:36 2014 +0000
@@ -23,13 +23,13 @@
 UDPSocket::UDPSocket()
 {
 }
-
+// After init function, bind() should be called.
 int UDPSocket::init(void)
 {
     if (_sock_fd < 0) {
         _sock_fd = eth->new_socket();
     }
-    eth->setProtocol(_sock_fd, UDP);
+    if (eth->setProtocol(_sock_fd, UDP) == false) return -1;
     return 0;
 }
 
@@ -77,7 +77,7 @@
     }
     eth->recv(_sock_fd, (char*)info, sizeof(info));
     readEndpoint(remote, info);
-    int udp_size = info[6]<<8|info[7]; 
+    int udp_size = info[6]<<8|info[7];
     //TEST_ASSERT(udp_size <= (size-sizeof(info)));
     if (udp_size > (size-sizeof(info))) {
         return -1;
--- a/Socket/UDPSocket.h	Mon Dec 23 13:51:35 2013 +0000
+++ b/Socket/UDPSocket.h	Thu Jul 17 07:10:36 2014 +0000
@@ -19,30 +19,31 @@
 #ifndef UDPSOCKET_H
 #define UDPSOCKET_H
 
-#include "Endpoint.h"
-#include "Socket.h"
+#include "Socket/Socket.h"
+#include "Socket/Endpoint.h"
 
 /**
 UDP Socket
 */
-class UDPSocket: public Socket {
+class UDPSocket: public Socket
+{
 
 public:
     /** Instantiate an UDP Socket.
     */
     UDPSocket();
-    
+
     /** Init the UDP Client Socket without binding it to any specific port
     \return 0 on success, -1 on failure.
     */
     int init(void);
-    
+
     /** Bind a UDP Server Socket to a specific port
     \param port The port to listen for incoming connections on
     \return 0 on success, -1 on failure.
     */
     int bind(int port = -1);
-    
+
     /** Send a packet to a remote endpoint
     \param remote   The remote endpoint
     \param packet   The packet to be sent
@@ -50,7 +51,7 @@
     \return the number of written bytes on success (>=0) or -1 on failure
     */
     int sendTo(Endpoint &remote, char *packet, int length);
-    
+
     /** Receive a packet from a remote endpoint
     \param remote   The remote endpoint
     \param buffer   The buffer for storing the incoming packet data. If a packet
@@ -59,7 +60,7 @@
     \return the number of received bytes on success (>=0) or -1 on failure
     */
     int receiveFrom(Endpoint &remote, char *buffer, int length);
-    
+
 private:
     void confEndpoint(Endpoint & ep);
     void readEndpoint(Endpoint & ep, uint8_t info[]);
--- a/WIZ550ioInterface.cpp	Mon Dec 23 13:51:35 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,98 +0,0 @@
-// WIZ550ioInterface.cpp 2013/4/5
-#include "WIZ550ioInterface.h"
-#include "DHCPClient.h"
-
-WIZ550ioInterface::WIZ550ioInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset) :
-    WIZ820io(mosi, miso, sclk, cs, reset)
-{
-    ip_set = false;
-}
-
-WIZ550ioInterface::WIZ550ioInterface(SPI* spi, PinName cs, PinName reset) :
-    WIZ820io(spi, cs, reset)
-{
-    ip_set = false;
-}
-
-int WIZ550ioInterface::init()
-{
-    dhcp = true;
-    reset();
-    return 0;
-}
-
-int WIZ550ioInterface::init(const char* ip, const char* mask, const char* gateway)
-{
-    dhcp = false;
-    this->ip = str_to_ip(ip);
-    strcpy(ip_string, ip);
-    ip_set = true;
-    this->netmask = str_to_ip(mask);
-    this->gateway = str_to_ip(gateway);
-    reset();
-    return 0;
-}
-
-int WIZ550ioInterface::connect()
-{
-    if (dhcp) {
-        int r = IPrenew();
-        if (r < 0) {
-            return r;
-        }
-    }
-    if (WIZ820io::join() == false) return -1;
-    return 0;
-}
-
-int WIZ550ioInterface::disconnect()
-{
-    if (WIZ820io::disconnect() == false) return -1;
-    return 0;
-}
-
-char* WIZ550ioInterface::getIPAddress()
-{
-    uint32_t ip = reg_rd<uint32_t>(SIPR);
-    snprintf(ip_string, sizeof(ip_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
-    return ip_string;
-}
-
-char* WIZ550ioInterface::getNetworkMask()
-{
-    uint32_t ip = reg_rd<uint32_t>(SUBR);
-    snprintf(mask_string, sizeof(mask_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
-    return mask_string;
-}
-
-char* WIZ550ioInterface::getGateway()
-{
-    uint32_t ip = reg_rd<uint32_t>(GAR);
-    snprintf(gw_string, sizeof(gw_string), "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff);
-    return gw_string;
-}
-
-char* WIZ550ioInterface::getMACAddress()
-{
-    uint8_t mac[6];
-    reg_rd_mac(SHAR, mac);
-    snprintf(mac_string, sizeof(mac_string), "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
-    return mac_string;
-}
-
-int WIZ550ioInterface::IPrenew(int timeout_ms)
-{
-//    printf("DHCP Started, waiting for IP...\n");  
-    DHCPClient dhcp;
-    int err = dhcp.setup(timeout_ms);
-    if (err == (-1)) {
-//        printf("Timeout.\n");
-        return -1;
-    }
-//    printf("Connected, IP: %d.%d.%d.%d\n", dhcp.yiaddr[0], dhcp.yiaddr[1], dhcp.yiaddr[2], dhcp.yiaddr[3]);
-    ip      = (dhcp.yiaddr[0] <<24) | (dhcp.yiaddr[1] <<16) | (dhcp.yiaddr[2] <<8) | dhcp.yiaddr[3];
-    gateway = (dhcp.gateway[0]<<24) | (dhcp.gateway[1]<<16) | (dhcp.gateway[2]<<8) | dhcp.gateway[3];
-    netmask = (dhcp.netmask[0]<<24) | (dhcp.netmask[1]<<16) | (dhcp.netmask[2]<<8) | dhcp.netmask[3];
-    dnsaddr = (dhcp.dnsaddr[0]<<24) | (dhcp.dnsaddr[1]<<16) | (dhcp.dnsaddr[2]<<8) | dhcp.dnsaddr[3];
-    return 0;
-}
--- a/WIZ550ioInterface.h	Mon Dec 23 13:51:35 2013 +0000
+++ b/WIZ550ioInterface.h	Thu Jul 17 07:10:36 2014 +0000
@@ -1,30 +1,12 @@
-// WIZ550ioInterface.h 2013/4/10
-/* Copyright (C) 2012 mbed.org, MIT License
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
+// EthernetInterfaceW5500.h 2014/7/17
 
 #pragma once
+#include "wiznet.h"
 
-#include "WIZ820io.h"
-
- /** Interface using WIZ820io to connect to an IP-based network
+ /** Interface using Wiznet chip to connect to an IP-based network
  *
  */
-class WIZ550ioInterface: public WIZ820io {
+class EthernetInterfaceW5500: public WIZnet_Chip {
 public:
 
     /**
@@ -33,26 +15,42 @@
     * \param mosi mbed pin to use for SPI
     * \param miso mbed pin to use for SPI
     * \param sclk mbed pin to use for SPI
-    * \param cs
-    * \param reset reset pin of the WIZ820io module
+    * \param cs chip select of the WIZnet_Chip
+    * \param reset reset pin of the WIZnet_Chip
     */
-  WIZ550ioInterface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
-  WIZ550ioInterface(SPI* spi, PinName cs, PinName reset);
+    EthernetInterfaceW5500(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
+    EthernetInterfaceW5500(SPI* spi, PinName cs, PinName reset);
 
-  /** Initialize the interface with DHCP.
+  /** Initialize the interface with DHCP w/o MAC address
   * Initialize the interface and configure it to use DHCP (no connection at this point).
   * \return 0 on success, a negative number on failure
   */
   int init(); //With DHCP
+  /** Initialize the interface with DHCP.
+  * Initialize the interface and configure it to use DHCP (no connection at this point).
+  * \param mac the MAC address to use
+  * \return 0 on success, a negative number on failure
+  */
+  int init(uint8_t * mac); //With DHCP
 
-  /** Initialize the interface with a static IP address.
+  /** Initialize the interface with a static IP address without MAC.
   * Initialize the interface and configure it with the following static configuration (no connection at this point).
   * \param ip the IP address to use
   * \param mask the IP address mask
   * \param gateway the gateway to use
   * \return 0 on success, a negative number on failure
   */
+
   int init(const char* ip, const char* mask, const char* gateway);
+  /** Initialize the interface with a static IP address.
+  * Initialize the interface and configure it with the following static configuration (no connection at this point).
+  * \param mac the MAC address to use
+  * \param ip the IP address to use
+  * \param mask the IP address mask
+  * \param gateway the gateway to use
+  * \return 0 on success, a negative number on failure
+  */
+  int init(uint8_t * mac, const char* ip, const char* mask, const char* gateway);
 
   /** Connect
   * Bring the interface up, start DHCP if needed.
--- a/WIZ820io/WIZ820io.cpp	Mon Dec 23 13:51:35 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,377 +0,0 @@
-/* Copyright (C) 2012 mbed.org, MIT License
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- */
-
-#include "mbed.h"
-#include "mbed_debug.h"
-#include "WIZ820io.h"
-#include "DNSClient.h"
-
-//Debug is disabled by default
-#if 0
-#define DBG(...) do{debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0);
-//#define DBG(x, ...) debug("[WIZ820io:DBG]"x"\r\n", ##__VA_ARGS__);
-#define WARN(x, ...) debug("[WIZ820io:WARN]"x"\r\n", ##__VA_ARGS__);
-#define ERR(x, ...) debug("[WIZ820io:ERR]"x"\r\n", ##__VA_ARGS__);
-#else
-#define DBG(x, ...)
-#define WARN(x, ...)
-#define ERR(x, ...)
-#endif
-
-#if 1
-#define INFO(x, ...) debug("[WIZ820io:INFO]"x"\r\n", ##__VA_ARGS__);
-#else
-#define INFO(x, ...)
-#endif
-
-#define DBG_SPI 0
-
-WIZ820io* WIZ820io::inst;
-
-WIZ820io::WIZ820io(PinName mosi, PinName miso, PinName sclk, PinName _cs, PinName _reset):
-    cs(_cs), reset_pin(_reset)
-{
-    spi = new SPI(mosi, miso, sclk);
-    cs = 1;
-    reset_pin = 1;
-    inst = this;
-}
-
-WIZ820io::WIZ820io(SPI* spi, PinName _cs, PinName _reset):
-    cs(_cs), reset_pin(_reset)
-{
-    this->spi = spi;
-    cs = 1;
-    reset_pin = 1;
-    inst = this;
-}
-
-bool WIZ820io::join()
-{
-    reg_wr<uint32_t>(SIPR, ip);
-    reg_wr<uint32_t>(GAR, gateway);
-    reg_wr<uint32_t>(SUBR, netmask);
-    return true;
-}
-
-bool WIZ820io::setProtocol(int socket, Protocol p)
-{
-    if (socket < 0) {
-        return false;
-    }
-    sreg<uint8_t>(socket, Sn_MR, p);
-    return true;
-}
-
-bool WIZ820io::connect(int socket, const char * host, int port, int timeout_ms)
-{
-    if (socket < 0) {
-        return false;
-    }
-    sreg<uint8_t>(socket, Sn_MR, TCP);
-    scmd(socket, OPEN);
-    sreg_ip(socket, Sn_DIPR, host);
-    sreg<uint16_t>(socket, Sn_DPORT, port);
-    sreg<uint16_t>(socket, Sn_PORT, new_port());
-    scmd(socket, CONNECT);
-    Timer t;
-    t.reset();
-    t.start();
-    while(!is_connected(socket)) {
-        if (t.read_ms() > timeout_ms) {
-            return false;
-        }
-    }
-    return true;
-}
-
-bool WIZ820io::gethostbyname(const char* host, uint32_t* ip)
-{
-    uint32_t addr = str_to_ip(host);
-    char buf[17];
-    snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
-    if (strcmp(buf, host) == 0) {
-        *ip = addr;
-        return true;
-    }
-    DNSClient client;
-    if(client.lookup(host)) {
-        *ip = client.ip;
-        return true;
-    }
-    return false;
-}
-
-bool WIZ820io::disconnect()
-{
-    return true;
-}
-
-bool WIZ820io::is_connected(int socket)
-{
-    if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) {
-        return true;
-    }
-    return false;
-}
-
-void WIZ820io::reset()
-{
-    reset_pin = 1;
-    reset_pin = 0;
-    //wait_us(2); // 2us
-    wait_us(500); // 500us (w5500)
-    reset_pin = 1;
-    //wait_ms(150); // 150ms
-    wait_ms(300); // 300ms (w5500)
-    
-    //reg_wr<uint8_t>(MR, 1<<7);
-    
-//#ifdef TARGET_LPC1114
-//    uint8_t mac[6] = {0x00,0x02,0xf7,0xf0,0x00,0x00};
-//#else
-//    uint8_t mac[6];
-//    mbed_mac_address((char*)mac);
-//#endif
-//    reg_wr_mac(SHAR, mac);
-    
-    // set RX and TX buffer size
-    for (int socket = 0; socket < MAX_SOCK_NUM; socket++) {
-        sreg<uint8_t>(socket, Sn_RXBUF_SIZE, 2);
-        sreg<uint8_t>(socket, Sn_TXBUF_SIZE, 2);
-    }
-}
-
-bool WIZ820io::close(int socket)
-{
-    if (socket < 0) {
-        return false;
-    }
-    // if not connected, return
-    if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) {
-        return true;
-    }
-    if (sreg<uint8_t>(socket, Sn_MR) == TCP) {
-        scmd(socket, DISCON);
-    }
-    scmd(socket, CLOSE);
-    sreg<uint8_t>(socket, Sn_IR, 0xff);
-    return true;
-}
-
-int WIZ820io::wait_readable(int socket, int wait_time_ms, int req_size)
-{
-    if (socket < 0) {
-        return -1;
-    }
-    Timer t;
-    t.reset();
-    t.start();
-    while(1) {
-        int size = sreg<uint16_t>(socket, Sn_RX_RSR);
-        if (size > req_size) {
-            return size;
-        }
-        if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
-            break;
-        }
-    }
-    return -1;
-}
-
-int WIZ820io::wait_writeable(int socket, int wait_time_ms, int req_size)
-{
-    if (socket < 0) {
-        return -1;
-    }
-    Timer t;
-    t.reset();
-    t.start();
-    while(1) {
-        int size = sreg<uint16_t>(socket, Sn_TX_FSR);
-        if (size > req_size) {
-            return size;
-        }
-        if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
-            break;
-        }
-    }
-    return -1;
-}
-
-int WIZ820io::send(int socket, const char * str, int len)
-{
-    if (socket < 0) {
-        return -1;
-    }
-    uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR);
-    uint8_t cntl_byte = (0x14 + (socket << 5));
-    spi_write(ptr, cntl_byte, (uint8_t*)str, len);
-    sreg<uint16_t>(socket, Sn_TX_WR, ptr + len);
-    scmd(socket, SEND);
-    
-    while ((sreg<uint8_t>(socket, Sn_IR) & INT_SEND_OK) != INT_SEND_OK) {
-        if (sreg<uint8_t>(socket, Sn_SR) == CLOSED) {
-            close(socket);
-            return 0;
-        }
-    }
-    sreg<uint8_t>(socket, Sn_IR, INT_SEND_OK);
-
-    return len;
-}
-
-int WIZ820io::recv(int socket, char* buf, int len)
-{
-    if (socket < 0) {
-        return -1;
-    }
-    uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD);
-    uint8_t cntl_byte = (0x18 + (socket << 5));
-    spi_read(ptr, cntl_byte, (uint8_t*)buf, len);
-    sreg<uint16_t>(socket, Sn_RX_RD, ptr + len);
-    scmd(socket, RECV);
-    return len;
-}
-
-int WIZ820io::new_socket()
-{
-    for(int s = 0; s < 8; s++) {
-        if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) {
-            return s;
-        }
-    }
-    return -1;
-}
-
-uint16_t WIZ820io::new_port()
-{
-    uint16_t port = rand();
-    port |= 49152;
-    return port;
-}
-
-void WIZ820io::scmd(int socket, Command cmd)
-{
-    sreg<uint8_t>(socket, Sn_CR, cmd);
-    while(sreg<uint8_t>(socket, Sn_CR));
-}
-
-void WIZ820io::spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len)
-{
-    cs = 0;
-    spi->write(addr >> 8);
-    spi->write(addr & 0xff);
-    spi->write(cb);
-    for(int i = 0; i < len; i++) {
-        spi->write(buf[i]);
-    }
-    cs = 1;
-
-#if DBG_SPI
-    debug("[SPI]W %04x(%02x %d)", addr, cb, len);
-    for(int i = 0; i < len; i++) {
-        debug(" %02x", buf[i]);
-        if (i > 16) {
-            debug(" ...");
-            break;
-        }
-    }
-    debug("\r\n");
-#endif    
-}
-
-void WIZ820io::spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len)
-{
-    cs = 0;
-    spi->write(addr >> 8);
-    spi->write(addr & 0xff);
-    spi->write(cb);
-    for(int i = 0; i < len; i++) {
-        buf[i] = spi->write(0);
-    }
-    cs = 1;
-
-#if DBG_SPI
-    debug("[SPI]R %04x(%02x %d)", addr, cb, len);
-    for(int i = 0; i < len; i++) {
-        debug(" %02x", buf[i]);
-        if (i > 16) {
-            debug(" ...");
-            break;
-        }
-    }
-    debug("\r\n");
-    if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) {
-        wait_ms(200);
-    }
-#endif    
-}
-
-uint32_t str_to_ip(const char* str)
-{
-    uint32_t ip = 0;
-    char* p = (char*)str;
-    for(int i = 0; i < 4; i++) {
-        ip |= atoi(p);
-        p = strchr(p, '.');
-        if (p == NULL) {
-            break;
-        }
-        ip <<= 8;
-        p++;
-    }
-    return ip;
-}
-
-void printfBytes(char* str, uint8_t* buf, int len)
-{
-    printf("%s %d:", str, len);
-    for(int i = 0; i < len; i++) {
-        printf(" %02x", buf[i]);
-    }
-    printf("\n");  
-}
-
-void printHex(uint8_t* buf, int len)
-{
-    for(int i = 0; i < len; i++) {
-        if ((i%16) == 0) {
-            printf("%p", buf+i);
-        }
-        printf(" %02x", buf[i]);
-        if ((i%16) == 15) {
-            printf("\n");
-        }
-    }
-    printf("\n");
-}
-
-void debug_hex(uint8_t* buf, int len)
-{
-    for(int i = 0; i < len; i++) {
-        if ((i%16) == 0) {
-            debug("%p", buf+i);
-        }
-        debug(" %02x", buf[i]);
-        if ((i%16) == 15) {
-            debug("\n");
-        }
-    }
-    debug("\n");
-}
--- a/WIZ820io/WIZ820io.h	Mon Dec 23 13:51:35 2013 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,273 +0,0 @@
-/* Copyright (C) 2012 mbed.org, MIT License
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
- * and associated documentation files (the "Software"), to deal in the Software without restriction,
- * including without limitation the rights to use, copy, modify, merge, publish, distribute,
- * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies or
- * substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
- * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
- * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- *
- */
-
-#pragma once
-
-#include "mbed.h"
-#include "mbed_debug.h"
-
-#define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
-
-#define DEFAULT_WAIT_RESP_TIMEOUT 500
-
-enum Protocol {
-    CLOSED = 0,
-    TCP    = 1,
-    UDP    = 2,
-};
-
-enum Command {
-    OPEN      = 0x01,
-    LISTEN    = 0x02,
-    CONNECT   = 0x04,
-    DISCON    = 0x08,
-    CLOSE     = 0x10,
-    SEND      = 0x20,
-    SEND_MAC  = 0x21, 
-    SEND_KEEP = 0x22,
-    RECV      = 0x40,
-    
-};
-
-enum Interrupt {
-    INT_CON     = 0x01,
-    INT_DISCON  = 0x02,
-    INT_RECV    = 0x04,
-    INT_TIMEOUT = 0x08,
-    INT_SEND_OK = 0x10,
-};
-
-enum Status {
-    SOCK_CLOSED      = 0x00,
-    SOCK_INIT        = 0x13,
-    SOCK_LISTEN      = 0x14,
-    SOCK_SYNSENT     = 0x15,
-    SOCK_ESTABLISHED = 0x17,
-    SOCK_CLOSE_WAIT  = 0x1c,
-    SOCK_UDP         = 0x22,
-};
-
-#define MAX_SOCK_NUM 8
-
-#define MR        0x0000
-#define GAR       0x0001
-#define SUBR      0x0005
-#define SHAR      0x0009
-#define SIPR      0x000f
-#define PHYSTATUS 0x0035
-// socket
-#define Sn_MR         0x0000
-#define Sn_CR         0x0001
-#define Sn_IR         0x0002
-#define Sn_SR         0x0003
-#define Sn_PORT       0x0004
-#define Sn_DIPR       0x000c
-#define Sn_DPORT      0x0010
-#define Sn_RXBUF_SIZE 0x001e
-#define Sn_TXBUF_SIZE 0x001f
-#define Sn_TX_FSR     0x0020
-#define Sn_TX_WR      0x0024
-#define Sn_RX_RSR     0x0026
-#define Sn_RX_RD      0x0028
-
-class WIZ820io {
-public:
-    /*
-    * Constructor
-    *
-    * @param tx mbed pin to use for tx line of Serial interface
-    * @param rx mbed pin to use for rx line of Serial interface
-    * @param reset reset pin of the WIZ820io module
-    */
-    WIZ820io(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
-    WIZ820io(SPI* spi, PinName cs, PinName reset);
-
-    /*
-    * Connect the WIZ820io module to the ssid contained in the constructor.
-    *
-    * @return true if connected, false otherwise
-    */
-    bool join();
-
-    /*
-    * Disconnect the WIZ820io module from the access point
-    *
-    * @ returns true if successful
-    */
-    bool disconnect();
-
-    /*
-    * Open a tcp connection with the specified host on the specified port
-    *
-    * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
-    * @param port port
-    * @ returns true if successful
-    */
-    bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000);
-
-    /*
-    * Set the protocol (UDP or TCP)
-    *
-    * @param p protocol
-    * @ returns true if successful
-    */
-    bool setProtocol(int socket, Protocol p);
-
-    /*
-    * Reset the WIZ820io module
-    */
-    void reset();
-    
-   
-    int wait_readable(int socket, int wait_time_ms, int req_size = 0);
-
-    int wait_writeable(int socket, int wait_time_ms, int req_size = 0);
-
-    /*
-    * Check if a tcp link is active
-    *
-    * @returns true if successful
-    */
-    bool is_connected(int socket);
-
-    /*
-    * Close a tcp connection
-    *
-    * @ returns true if successful
-    */
-    bool close(int socket);
-
-    /*
-    * @param str string to be sent
-    * @param len string length
-    */
-    int send(int socket, const char * str, int len);
-
-    int recv(int socket, char* buf, int len);
-
-    /*
-    * Return true if the module is using dhcp
-    *
-    * @returns true if the module is using dhcp
-    */
-    bool isDHCP() {
-        return dhcp;
-    }
-
-    bool gethostbyname(const char* host, uint32_t* ip);
-
-    static WIZ820io * getInstance() {
-        return inst;
-    };
-
-    int new_socket();
-    uint16_t new_port();
-    void scmd(int socket, Command cmd);
-
-    template<typename T>
-    void sreg(int socket, uint16_t addr, T data) {
-        reg_wr<T>(addr, (0x0C + (socket << 5)), data);
-    }
-
-    template<typename T>
-    T sreg(int socket, uint16_t addr) {
-        return reg_rd<T>(addr, (0x08 + (socket << 5)));
-    }
-
-    template<typename T>
-    void reg_wr(uint16_t addr, T data) {
-        return reg_wr(addr, 0x04, data);
-    }
-    
-    template<typename T>
-    void reg_wr(uint16_t addr, uint8_t cb, T data) {
-        uint8_t buf[sizeof(T)];
-        *reinterpret_cast<T*>(buf) = data;
-        for(int i = 0; i < sizeof(buf)/2; i++) { //  Little Endian to Big Endian
-            uint8_t t = buf[i];
-            buf[i] = buf[sizeof(buf)-1-i];
-            buf[sizeof(buf)-1-i] = t;
-        }
-        spi_write(addr, cb, buf, sizeof(buf));
-    }
-
-    template<typename T>
-    T reg_rd(uint16_t addr) {
-        return reg_rd<T>(addr, 0x00);
-    }
-
-    template<typename T>
-    T reg_rd(uint16_t addr, uint8_t cb) {
-        uint8_t buf[sizeof(T)];
-        spi_read(addr, cb, buf, sizeof(buf));
-        for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian
-            uint8_t t = buf[i];
-            buf[i] = buf[sizeof(buf)-1-i];
-            buf[sizeof(buf)-1-i] = t;
-        }
-        return *reinterpret_cast<T*>(buf);
-    }
-
-    void reg_rd_mac(uint16_t addr, uint8_t* data) {
-        spi_read(addr, 0x00, data, 6);
-    }
-
-    void reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip) {
-        uint8_t buf[4];
-        char* p = (char*)ip;
-        for(int i = 0; i < 4; i++) {
-            buf[i] = atoi(p);
-            p = strchr(p, '.');
-            if (p == NULL) {
-                break;
-            }
-            p++;
-        }
-        spi_write(addr, cb, buf, sizeof(buf));
-    }
-
-    void sreg_ip(int socket, uint16_t addr, const char* ip) {
-        reg_wr_ip(addr, (0x0C + (socket << 5)), ip);
-    }
-
-protected:
-    uint32_t ip;
-    uint32_t netmask;
-    uint32_t gateway;
-    uint32_t dnsaddr;
-    bool dhcp;
-
-    static WIZ820io* inst;
-
-    void reg_wr_mac(uint16_t addr, uint8_t* data) {
-        spi_write(addr, 0x04, data, 6);
-    }
-
-    void spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len);
-    void spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len);
-    SPI* spi;
-    DigitalOut cs;
-    DigitalOut reset_pin;
-};
-
-// WIZ820io.cpp
-extern uint32_t str_to_ip(const char* str);
-extern void printfBytes(char* str, uint8_t* buf, int len);
-extern void printHex(uint8_t* buf, int len);
-extern void debug_hex(uint8_t* buf, int len);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZnet/W5500.cpp	Thu Jul 17 07:10:36 2014 +0000
@@ -0,0 +1,384 @@
+
+#include "mbed.h"
+#include "mbed_debug.h"
+#include "wiznet.h"
+#include "DNSClient.h"
+
+#ifdef USE_W5500
+
+//Debug is disabled by default
+#if 0
+#define DBG(...) do{debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0);
+//#define DBG(x, ...) debug("[W5500:DBG]"x"\r\n", ##__VA_ARGS__);
+#define WARN(x, ...) debug("[W5500:WARN]"x"\r\n", ##__VA_ARGS__);
+#define ERR(x, ...) debug("[W5500:ERR]"x"\r\n", ##__VA_ARGS__);
+#else
+#define DBG(x, ...)
+#define WARN(x, ...)
+#define ERR(x, ...)
+#endif
+
+#if 1
+#define INFO(x, ...) debug("[W5500:INFO]"x"\r\n", ##__VA_ARGS__);
+#else
+#define INFO(x, ...)
+#endif
+
+#define DBG_SPI 0
+
+WIZnet_Chip* WIZnet_Chip::inst;
+
+WIZnet_Chip::WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName _cs, PinName _reset):
+    cs(_cs), reset_pin(_reset)
+{
+    spi = new SPI(mosi, miso, sclk);
+    cs = 1;
+    reset_pin = 1;
+    inst = this;
+}
+
+WIZnet_Chip::WIZnet_Chip(SPI* spi, PinName _cs, PinName _reset):
+    cs(_cs), reset_pin(_reset)
+{
+    this->spi = spi;
+    cs = 1;
+    reset_pin = 1;
+    inst = this;
+}
+
+bool WIZnet_Chip::setmac()
+{
+
+    for (int i =0; i < 6; i++) reg_wr<uint8_t>(SHAR+i, mac[i]);
+
+    return true;
+}
+
+// Set the IP
+bool WIZnet_Chip::setip()
+{
+    reg_wr<uint32_t>(SIPR, ip);
+    reg_wr<uint32_t>(GAR, gateway);
+    reg_wr<uint32_t>(SUBR, netmask);
+    return true;
+}
+
+bool WIZnet_Chip::setProtocol(int socket, Protocol p)
+{
+    if (socket < 0) {
+        return false;
+    }
+    sreg<uint8_t>(socket, Sn_MR, p);
+    return true;
+}
+
+bool WIZnet_Chip::connect(int socket, const char * host, int port, int timeout_ms)
+{
+    if (socket < 0) {
+        return false;
+    }
+    sreg<uint8_t>(socket, Sn_MR, TCP);
+    scmd(socket, OPEN);
+    sreg_ip(socket, Sn_DIPR, host);
+    sreg<uint16_t>(socket, Sn_DPORT, port);
+    sreg<uint16_t>(socket, Sn_PORT, new_port());
+    scmd(socket, CONNECT);
+    Timer t;
+    t.reset();
+    t.start();
+    while(!is_connected(socket)) {
+        if (t.read_ms() > timeout_ms) {
+            return false;
+        }
+    }
+    return true;
+}
+
+bool WIZnet_Chip::gethostbyname(const char* host, uint32_t* ip)
+{
+    uint32_t addr = str_to_ip(host);
+    char buf[17];
+    snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
+    if (strcmp(buf, host) == 0) {
+        *ip = addr;
+        return true;
+    }
+    DNSClient client;
+    if(client.lookup(host)) {
+        *ip = client.ip;
+        return true;
+    }
+    return false;
+}
+
+bool WIZnet_Chip::disconnect()
+{
+    return true;
+}
+
+bool WIZnet_Chip::is_connected(int socket)
+{
+    uint8_t tmpSn_SR;
+    tmpSn_SR = sreg<uint8_t>(socket, Sn_SR);
+    // packet sending is possible, when state is SOCK_CLOSE_WAIT.
+    if ((tmpSn_SR == SOCK_ESTABLISHED) || (tmpSn_SR == SOCK_CLOSE_WAIT)) {
+        return true;
+    }
+    return false;
+}
+
+// Reset the chip & set the buffer
+void WIZnet_Chip::reset()
+{
+    reset_pin = 1;
+    reset_pin = 0;
+    wait_us(500); // 500us (w5500)
+    reset_pin = 1;
+    wait_ms(400); // 400ms (w5500)
+
+#if defined(USE_WIZ550IO_MAC)
+    //reg_rd_mac(SHAR, mac); // read the MAC address inside the module
+#endif
+
+    //reg_wr_mac(SHAR, mac);
+
+    // set RX and TX buffer size
+    for (int socket = 0; socket < MAX_SOCK_NUM; socket++) {
+        sreg<uint8_t>(socket, Sn_RXBUF_SIZE, 2);
+        sreg<uint8_t>(socket, Sn_TXBUF_SIZE, 2);
+    }
+}
+
+
+bool WIZnet_Chip::close(int socket)
+{
+    if (socket < 0) {
+        return false;
+    }
+    // if not connected, return
+    if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) {
+        return true;
+    }
+    if (sreg<uint8_t>(socket, Sn_MR) == TCP) {
+        scmd(socket, DISCON);
+    }
+    scmd(socket, CLOSE);
+    sreg<uint8_t>(socket, Sn_IR, 0xff);
+    return true;
+}
+
+int WIZnet_Chip::wait_readable(int socket, int wait_time_ms, int req_size)
+{
+    if (socket < 0) {
+        return -1;
+    }
+    Timer t;
+    t.reset();
+    t.start();
+    while(1) {
+        int size = sreg<uint16_t>(socket, Sn_RX_RSR);
+        if (size > req_size) {
+            return size;
+        }
+        if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
+            break;
+        }
+    }
+    return -1;
+}
+
+int WIZnet_Chip::wait_writeable(int socket, int wait_time_ms, int req_size)
+{
+    if (socket < 0) {
+        return -1;
+    }
+    Timer t;
+    t.reset();
+    t.start();
+    while(1) {
+        int size = sreg<uint16_t>(socket, Sn_TX_FSR);
+        if (size > req_size) {
+            return size;
+        }
+        if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
+            break;
+        }
+    }
+    return -1;
+}
+
+int WIZnet_Chip::send(int socket, const char * str, int len)
+{
+    if (socket < 0) {
+        return -1;
+    }
+    uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR);
+    uint8_t cntl_byte = (0x14 + (socket << 5));
+    spi_write(ptr, cntl_byte, (uint8_t*)str, len);
+    sreg<uint16_t>(socket, Sn_TX_WR, ptr + len);
+    scmd(socket, SEND);
+    uint8_t tmp_Sn_IR;
+    while (( (tmp_Sn_IR = sreg<uint8_t>(socket, Sn_IR)) & INT_SEND_OK) != INT_SEND_OK) {
+        // @Jul.10, 2014 fix contant name, and udp sendto function.
+        switch (sreg<uint8_t>(socket, Sn_SR)) {
+            case SOCK_CLOSED :
+                close(socket);
+                return 0;
+                //break;
+            case SOCK_UDP :
+                // ARP timeout is possible.
+                if ((tmp_Sn_IR & INT_TIMEOUT) == INT_TIMEOUT) {
+                    sreg<uint8_t>(socket, Sn_IR, INT_TIMEOUT);
+                    return 0;
+                }
+                break;
+            default :
+                break;
+        }
+    }
+    sreg<uint8_t>(socket, Sn_IR, INT_SEND_OK);
+
+    return len;
+}
+
+int WIZnet_Chip::recv(int socket, char* buf, int len)
+{
+    if (socket < 0) {
+        return -1;
+    }
+    uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD);
+    uint8_t cntl_byte = (0x18 + (socket << 5));
+    spi_read(ptr, cntl_byte, (uint8_t*)buf, len);
+    sreg<uint16_t>(socket, Sn_RX_RD, ptr + len);
+    scmd(socket, RECV);
+    return len;
+}
+
+int WIZnet_Chip::new_socket()
+{
+    for(int s = 0; s < MAX_SOCK_NUM; s++) {
+        if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) {
+            return s;
+        }
+    }
+    return -1;
+}
+
+uint16_t WIZnet_Chip::new_port()
+{
+    uint16_t port = rand();
+    port |= 49152;
+    return port;
+}
+
+void WIZnet_Chip::scmd(int socket, Command cmd)
+{
+    sreg<uint8_t>(socket, Sn_CR, cmd);
+    while(sreg<uint8_t>(socket, Sn_CR));
+}
+
+void WIZnet_Chip::spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len)
+{
+    cs = 0;
+    spi->write(addr >> 8);
+    spi->write(addr & 0xff);
+    spi->write(cb);
+    for(int i = 0; i < len; i++) {
+        spi->write(buf[i]);
+    }
+    cs = 1;
+
+#if DBG_SPI
+    debug("[SPI]W %04x(%02x %d)", addr, cb, len);
+    for(int i = 0; i < len; i++) {
+        debug(" %02x", buf[i]);
+        if (i > 16) {
+            debug(" ...");
+            break;
+        }
+    }
+    debug("\r\n");
+#endif
+}
+
+void WIZnet_Chip::spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len)
+{
+    cs = 0;
+    spi->write(addr >> 8);
+    spi->write(addr & 0xff);
+    spi->write(cb);
+    for(int i = 0; i < len; i++) {
+        buf[i] = spi->write(0);
+    }
+    cs = 1;
+
+#if DBG_SPI
+    debug("[SPI]R %04x(%02x %d)", addr, cb, len);
+    for(int i = 0; i < len; i++) {
+        debug(" %02x", buf[i]);
+        if (i > 16) {
+            debug(" ...");
+            break;
+        }
+    }
+    debug("\r\n");
+    if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) {
+        wait_ms(200);
+    }
+#endif
+}
+
+uint32_t str_to_ip(const char* str)
+{
+    uint32_t ip = 0;
+    char* p = (char*)str;
+    for(int i = 0; i < 4; i++) {
+        ip |= atoi(p);
+        p = strchr(p, '.');
+        if (p == NULL) {
+            break;
+        }
+        ip <<= 8;
+        p++;
+    }
+    return ip;
+}
+
+void printfBytes(char* str, uint8_t* buf, int len)
+{
+    printf("%s %d:", str, len);
+    for(int i = 0; i < len; i++) {
+        printf(" %02x", buf[i]);
+    }
+    printf("\n");
+}
+
+void printHex(uint8_t* buf, int len)
+{
+    for(int i = 0; i < len; i++) {
+        if ((i%16) == 0) {
+            printf("%p", buf+i);
+        }
+        printf(" %02x", buf[i]);
+        if ((i%16) == 15) {
+            printf("\n");
+        }
+    }
+    printf("\n");
+}
+
+void debug_hex(uint8_t* buf, int len)
+{
+    for(int i = 0; i < len; i++) {
+        if ((i%16) == 0) {
+            debug("%p", buf+i);
+        }
+        debug(" %02x", buf[i]);
+        if ((i%16) == 15) {
+            debug("\n");
+        }
+    }
+    debug("\n");
+}
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZnet/W5500.h	Thu Jul 17 07:10:36 2014 +0000
@@ -0,0 +1,262 @@
+
+#pragma once
+
+#include "mbed.h"
+#include "mbed_debug.h"
+
+#define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
+
+#define DEFAULT_WAIT_RESP_TIMEOUT 500
+
+enum Protocol {
+    CLOSED = 0,
+    TCP    = 1,
+    UDP    = 2,
+};
+
+enum Command {
+    OPEN      = 0x01,
+    LISTEN    = 0x02,
+    CONNECT   = 0x04,
+    DISCON    = 0x08,
+    CLOSE     = 0x10,
+    SEND      = 0x20,
+    SEND_MAC  = 0x21, 
+    SEND_KEEP = 0x22,
+    RECV      = 0x40,
+    
+};
+
+enum Interrupt {
+    INT_CON     = 0x01,
+    INT_DISCON  = 0x02,
+    INT_RECV    = 0x04,
+    INT_TIMEOUT = 0x08,
+    INT_SEND_OK = 0x10,
+};
+
+enum Status {
+    SOCK_CLOSED      = 0x00,
+    SOCK_INIT        = 0x13,
+    SOCK_LISTEN      = 0x14,
+    SOCK_SYNSENT     = 0x15,
+    SOCK_ESTABLISHED = 0x17,
+    SOCK_CLOSE_WAIT  = 0x1c,
+    SOCK_UDP         = 0x22,
+};
+
+#define MAX_SOCK_NUM 8
+
+#define MR        0x0000
+#define GAR       0x0001
+#define SUBR      0x0005
+#define SHAR      0x0009
+#define SIPR      0x000f
+#define PHYSTATUS 0x0035
+
+// W5500 socket register
+#define Sn_MR         0x0000
+#define Sn_CR         0x0001
+#define Sn_IR         0x0002
+#define Sn_SR         0x0003
+#define Sn_PORT       0x0004
+#define Sn_DIPR       0x000c
+#define Sn_DPORT      0x0010
+#define Sn_RXBUF_SIZE 0x001e
+#define Sn_TXBUF_SIZE 0x001f
+#define Sn_TX_FSR     0x0020
+#define Sn_TX_WR      0x0024
+#define Sn_RX_RSR     0x0026
+#define Sn_RX_RD      0x0028
+
+class WIZnet_Chip {
+public:
+    /*
+    * Constructor
+    *
+    * @param spi spi class
+    * @param cs cs of the W5500
+    * @param reset reset pin of the W5500
+    */
+    WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
+    WIZnet_Chip(SPI* spi, PinName cs, PinName reset);
+
+    /*
+    * Set MAC Address to W5500
+    *
+    * @return true if connected, false otherwise
+    */ 
+    bool setmac();
+
+    /*
+    * Set Network Informations (SrcIP, Netmask, Gataway)
+    *
+    * @return true if connected, false otherwise
+    */ 
+    bool setip();
+
+    /*
+    * Disconnect the connection
+    *
+    * @ returns true 
+    */
+    bool disconnect();
+
+    /*
+    * Open a tcp connection with the specified host on the specified port
+    *
+    * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
+    * @param port port
+    * @ returns true if successful
+    */
+    bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000);
+
+    /*
+    * Set the protocol (UDP or TCP)
+    *
+    * @param p protocol
+    * @ returns true if successful
+    */
+    bool setProtocol(int socket, Protocol p);
+
+    /*
+    * Reset the W5500
+    */
+    void reset();
+   
+    int wait_readable(int socket, int wait_time_ms, int req_size = 0);
+
+    int wait_writeable(int socket, int wait_time_ms, int req_size = 0);
+
+    /*
+    * Check if a tcp link is active
+    *
+    * @returns true if successful
+    */
+    bool is_connected(int socket);
+
+    /*
+    * Close a tcp connection
+    *
+    * @ returns true if successful
+    */
+    bool close(int socket);
+
+    /*
+    * @param str string to be sent
+    * @param len string length
+    */
+    int send(int socket, const char * str, int len);
+
+    int recv(int socket, char* buf, int len);
+
+    /*
+    * Return true if the module is using dhcp
+    *
+    * @returns true if the module is using dhcp
+    */
+    bool isDHCP() {
+        return dhcp;
+    }
+
+    bool gethostbyname(const char* host, uint32_t* ip);
+
+    static WIZnet_Chip * getInstance() {
+        return inst;
+    };
+
+    int new_socket();
+    uint16_t new_port();
+    void scmd(int socket, Command cmd);
+
+    template<typename T>
+    void sreg(int socket, uint16_t addr, T data) {
+        reg_wr<T>(addr, (0x0C + (socket << 5)), data);
+    }
+
+    template<typename T>
+    T sreg(int socket, uint16_t addr) {
+        return reg_rd<T>(addr, (0x08 + (socket << 5)));
+    }
+
+    template<typename T>
+    void reg_wr(uint16_t addr, T data) {
+        return reg_wr(addr, 0x04, data);
+    }
+    
+    template<typename T>
+    void reg_wr(uint16_t addr, uint8_t cb, T data) {
+        uint8_t buf[sizeof(T)];
+        *reinterpret_cast<T*>(buf) = data;
+        for(int i = 0; i < sizeof(buf)/2; i++) { //  Little Endian to Big Endian
+            uint8_t t = buf[i];
+            buf[i] = buf[sizeof(buf)-1-i];
+            buf[sizeof(buf)-1-i] = t;
+        }
+        spi_write(addr, cb, buf, sizeof(buf));
+    }
+
+    template<typename T>
+    T reg_rd(uint16_t addr) {
+        return reg_rd<T>(addr, 0x00);
+    }
+
+    template<typename T>
+    T reg_rd(uint16_t addr, uint8_t cb) {
+        uint8_t buf[sizeof(T)];
+        spi_read(addr, cb, buf, sizeof(buf));
+        for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian
+            uint8_t t = buf[i];
+            buf[i] = buf[sizeof(buf)-1-i];
+            buf[sizeof(buf)-1-i] = t;
+        }
+        return *reinterpret_cast<T*>(buf);
+    }
+
+    void reg_rd_mac(uint16_t addr, uint8_t* data) {
+        spi_read(addr, 0x00, data, 6);
+    }
+
+    void reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip) {
+        uint8_t buf[4];
+        char* p = (char*)ip;
+        for(int i = 0; i < 4; i++) {
+            buf[i] = atoi(p);
+            p = strchr(p, '.');
+            if (p == NULL) {
+                break;
+            }
+            p++;
+        }
+        spi_write(addr, cb, buf, sizeof(buf));
+    }
+
+    void sreg_ip(int socket, uint16_t addr, const char* ip) {
+        reg_wr_ip(addr, (0x0C + (socket << 5)), ip);
+    }
+
+protected:
+    uint8_t mac[6];
+    uint32_t ip;
+    uint32_t netmask;
+    uint32_t gateway;
+    uint32_t dnsaddr;
+    bool dhcp;
+
+    static WIZnet_Chip* inst;
+
+    void reg_wr_mac(uint16_t addr, uint8_t* data) {
+        spi_write(addr, 0x04, data, 6);
+    }
+
+    void spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len);
+    void spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len);
+    SPI* spi;
+    DigitalOut cs;
+    DigitalOut reset_pin;
+};
+
+extern uint32_t str_to_ip(const char* str);
+extern void printfBytes(char* str, uint8_t* buf, int len);
+extern void printHex(uint8_t* buf, int len);
+extern void debug_hex(uint8_t* buf, int len);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZnet/wiznet.h	Thu Jul 17 07:10:36 2014 +0000
@@ -0,0 +1,25 @@
+
+#pragma once
+
+//#include "mbed.h"
+//#include "mbed_debug.h"
+
+#define USE_W5500
+//#define USE_W5200 // don't use this library
+//#define USE_W5100 // don't use this library
+
+#if defined(USE_W5500)
+#include "W5500.h"
+//#define USE_WIZ550IO_MAC    // want to use the default MAC address stored in the WIZ550io
+#endif
+
+/*
+// current library don't want to support old chips.
+#if defined(USE_W5200)
+#include "W5200.h"
+#endif
+
+#if defined(USE_W5100)
+#include "W5100.h"
+#endif
+*/
\ No newline at end of file