W5500 driver for mbed OS 5

Dependents:   http-webserver-example mbed-os-example-sockets

Fork of W5500Interface by Sergei G

Files at this revision

API Documentation at this revision

Comitter:
Bongjun
Date:
Thu Aug 16 07:33:40 2018 +0000
Parent:
17:60f75e78f35d
Commit message:
change spi frame bits : 32->8

Changed in this revision

W5500Interface.cpp Show annotated file Show diff for this revision Revisions of this file
W5500Interface.h Show annotated file 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
--- a/W5500Interface.cpp	Tue Aug 14 01:20:36 2018 +0000
+++ b/W5500Interface.cpp	Thu Aug 16 07:33:40 2018 +0000
@@ -135,7 +135,8 @@
 int W5500Interface::init(const char* ip, const char* mask, const char* gateway)
 {
     _dhcp_enable = false;
-    
+
+
     _w5500.ip = str_to_ip(ip);
     strcpy(ip_string, ip);
     ip_set = true;
@@ -219,7 +220,7 @@
     if (_dhcp_enable) {
         init(); // init default mac address
         int err = IPrenew(15000);
-		if (err < 0) return err;
+        if (err < 0) return err;
     }
 
     if (_w5500.setip() == false) return NSAPI_ERROR_DHCP_FAILURE;
@@ -658,7 +659,6 @@
 }
 */
 
-
 nsapi_error_t W5500Interface::gethostbyname(const char *host,
         SocketAddress *address, nsapi_version_t version)
 {
@@ -677,3 +677,15 @@
     }
     return NSAPI_ERROR_OK;
 }
+
+nsapi_error_t W5500Interface::set_network(const char *ip_address, const char *netmask, const char *gateway)
+{
+    init(ip_address, netmask, gateway);
+    return NSAPI_ERROR_OK;
+}
+
+nsapi_error_t W5500Interface::set_dhcp(bool dhcp)
+{
+    _dhcp_enable = dhcp;
+    return NSAPI_ERROR_OK;
+}
--- a/W5500Interface.h	Tue Aug 14 01:20:36 2018 +0000
+++ b/W5500Interface.h	Thu Aug 16 07:33:40 2018 +0000
@@ -109,6 +109,29 @@
      */
     virtual const char *get_mac_address();
     
+    /** Set a static IP address
+     *
+     *  Configures this network interface to use a static IP address.
+     *  Implicitly disables DHCP, which can be enabled in set_dhcp.
+     *  Requires that the network is disconnected.
+     *
+     *  @param ip_address Null-terminated representation of the local IP address
+     *  @param netmask    Null-terminated representation of the local network mask
+     *  @param gateway    Null-terminated representation of the local gateway
+     *  @return           0 on success, negative error code on failure
+     */
+    virtual nsapi_error_t set_network(const char *ip_address, const char *netmask, const char *gateway);
+
+    /** Enable or disable DHCP on the network
+     *
+     *  Enables DHCP on connecting the network. Defaults to enabled unless
+     *  a static IP address has been assigned. Requires that the network is
+     *  disconnected.
+     *
+     *  @param dhcp     True to enable DHCP
+     *  @return         0 on success, negative error code on failure
+     */
+    virtual nsapi_error_t set_dhcp(bool dhcp);
 
 	bool setDnsServerIP(const char* ip_address);
 
@@ -304,10 +327,10 @@
 private:
 	WIZnet_Chip _w5500;
 
-    char ip_string[20];
-    char netmask_string[20];
-    char gateway_string[20];
-    char mac_string[20];
+    char ip_string[NSAPI_IPv4_SIZE];
+    char netmask_string[NSAPI_IPv4_SIZE];
+    char gateway_string[NSAPI_IPv4_SIZE];
+    char mac_string[NSAPI_MAC_SIZE];
     bool ip_set;    
     
     int listen_port;
--- a/WIZnet/W5500.cpp	Tue Aug 14 01:20:36 2018 +0000
+++ b/WIZnet/W5500.cpp	Thu Aug 16 07:33:40 2018 +0000
@@ -34,12 +34,23 @@
 WIZnet_Chip::WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName _cs, PinName _reset):
     cs(_cs), reset_pin(_reset)
 {
+    connected_reset_pin = false;
     spi = new SPI(mosi, miso, sclk);
     DBG("SPI interface init...\n");
-    spi->format(32, 0);
+    spi->format(8, 0);
     spi->frequency(MBED_CONF_W5500_SPI_SPEED);
-    cs = 1;
-    reset_pin = 1;
+
+    if (_cs != NC) {
+        cs = 1;
+    } else {
+        DBG("CS pin is necessary. So You have to set CS pin name\n");
+    }
+
+    if (_reset != NC) {
+        reset_pin = 1;
+        connected_reset_pin = true;
+    }
+
     inst = this;
 }
 
@@ -134,22 +145,24 @@
 void WIZnet_Chip::reset()
 {
 //    reset_pin = 1;
-    reset_pin = 0;
-    wait_us(500); // 500us (w5500)
-    reset_pin = 1;
-    wait_ms(400); // 400ms (w5500)
+    if(connected_reset_pin) {
+        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);
     }
-    
+
     reg_rd_mac(SHAR, mac); // read the MAC address inside the module
 }
 
@@ -184,7 +197,7 @@
     if (socket < 0) {
         return -1;
     }
-    
+
     Timer t;
     t.reset();
     t.start();
@@ -196,15 +209,15 @@
         while (1) {
             size1 = sreg<uint16_t>(socket, Sn_RX_RSR);
             size2 = sreg<uint16_t>(socket, Sn_RX_RSR);
-            
+
             if (size1 == size2) {
                 break;
             }
-            
+
             if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
-               return NSAPI_ERROR_WOULD_BLOCK;
+                return NSAPI_ERROR_WOULD_BLOCK;
             }
-            
+
             if (!is_connected(socket)) {
                 return -1;
             }
@@ -229,7 +242,7 @@
     t.reset();
     t.start();
     t.start();
-        
+
     while(1) {
         //int size = sreg<uint16_t>(socket, Sn_TX_FSR);
         int size1, size2;
@@ -239,11 +252,11 @@
             size1 = sreg<uint16_t>(socket, Sn_TX_FSR);
             size2 = sreg<uint16_t>(socket, Sn_TX_FSR);
             DBG("The time taken was %d %d %f seconds\n", wait_time_ms, t.read_ms(), t.read());
-            
+
             if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
-                
+
                 return NSAPI_ERROR_WOULD_BLOCK;
-            }        
+            }
         } while (size1 != size2);
         if (size1 > req_size) {
             return size1;
--- a/WIZnet/W5500.h	Tue Aug 14 01:20:36 2018 +0000
+++ b/WIZnet/W5500.h	Thu Aug 16 07:33:40 2018 +0000
@@ -22,10 +22,10 @@
     DISCON    = 0x08,
     CLOSE     = 0x10,
     SEND      = 0x20,
-    SEND_MAC  = 0x21, 
+    SEND_MAC  = 0x21,
     SEND_KEEP = 0x22,
     RECV      = 0x40,
-    
+
 };
 
 enum Interrupt {
@@ -75,7 +75,8 @@
 
 
 
-class WIZnet_Chip {
+class WIZnet_Chip
+{
 public:
     /*
     * Constructor
@@ -93,20 +94,20 @@
     * 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 
+    * @ returns true
     */
     bool disconnect();
 
@@ -131,14 +132,14 @@
     * Set local port number
     *
     * @return true if connected, false otherwise
-    */ 
-	bool setLocalPort(int socket, uint16_t port);
+    */
+    bool setLocalPort(int socket, uint16_t port);
 
     /*
     * 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);
@@ -196,7 +197,7 @@
     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)];
@@ -253,7 +254,7 @@
     uint32_t netmask;
     uint32_t gateway;
     uint32_t dnsaddr;
-    bool dhcp;
+    //bool dhcp;
 
 protected:
     static WIZnet_Chip* inst;
@@ -267,6 +268,7 @@
     SPI* spi;
     DigitalOut cs;
     DigitalOut reset_pin;
+    bool connected_reset_pin;
 };
 
 /*