W5500 from SeeedStudio on NUCLEO-L476RG

Dependents:   coap-example Borsch coap-example

Fork of W5500Interface by AMETEK Powervar

Files at this revision

API Documentation at this revision

Comitter:
sgnezdov
Date:
Thu Jul 06 05:46:17 2017 +0000
Parent:
4:80e302a610fd
Commit message:
fined tuned debugging; turned off debugging

Changed in this revision

W5500Interface.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 80e302a610fd -r 41393623ead4 W5500Interface.cpp
--- a/W5500Interface.cpp	Thu Jul 06 04:51:34 2017 +0000
+++ b/W5500Interface.cpp	Thu Jul 06 05:46:17 2017 +0000
@@ -34,9 +34,17 @@
 DigitalOut led1(LED1);
 
 #define SKT(h) ((w5500_socket*)h)
-#define w5500_WAIT_TIMEOUT   5000
+#define w5500_WAIT_TIMEOUT   1500
 #define w5500_ACCEPT_TIMEOUT 3000
-#define w5500_INTF_DBG 1
+
+#define w5500_INTF_DBG 0
+
+#if w5500_INTF_DBG
+#define DBG(...) do{debug("[%s:%d]", __PRETTY_FUNCTION__,__LINE__);debug(__VA_ARGS__);} while(0);
+#else
+#define DBG(...) while(0);
+#endif
+
 
 /* Interface implementation */
 
@@ -206,17 +214,13 @@
    
     //new up an int to store the socket fd 
     *handle = h;
-#ifdef w5500_INTF_DBG
-    debug("[socket_open] fd: %d\n", sock_fd);
-#endif
+    DBG("fd: %d\n", sock_fd);
     return 0;
 }
 
 void W5500Interface::signal_event(nsapi_socket_t handle)
 {
-#ifdef w5500_INTF_DBG
-    debug("[signal_event] fd: %d\n", SKT(handle)->fd);
-#endif
+    DBG("fd: %d\n", SKT(handle)->fd);
     if (SKT(handle)->callback != NULL) {
         SKT(handle)->callback(SKT(handle)->callback_data);
     }
@@ -225,9 +229,7 @@
 nsapi_error_t W5500Interface::socket_close(nsapi_socket_t handle)
 {
     if (handle == NULL) return 0;
-#ifdef w5500_INTF_DBG
-    debug("[socket_close] fd: %d\n", SKT(handle)->fd);
-#endif
+    DBG("fd: %d\n", SKT(handle)->fd);
     WIZnet_Chip::close(SKT(handle)->fd);
     
     SKT(handle)->fd = -1;
@@ -240,9 +242,7 @@
     if (handle < 0) {
         return NSAPI_ERROR_DEVICE_ERROR;
     }
-#ifdef w5500_INTF_DBG
-    debug("[socket_bind] fd: %d, port: %d\n", SKT(handle)->fd, address.get_port());
-#endif
+    DBG("fd: %d, port: %d\n", SKT(handle)->fd, address.get_port());
 
     switch (SKT(handle)->proto) {
         case NSAPI_UDP:
@@ -273,9 +273,7 @@
 
 nsapi_error_t W5500Interface::socket_listen(nsapi_socket_t handle, int backlog)
 {
-#ifdef w5500_INTF_DBG
-    debug("[socket_listen] fd: %d\n", SKT(handle)->fd);
-#endif
+    DBG("fd: %d\n", SKT(handle)->fd);
     if (SKT(handle)->fd < 0) {
         return NSAPI_ERROR_NO_SOCKET;
     }
@@ -288,9 +286,7 @@
 
 nsapi_size_or_error_t W5500Interface::socket_connect(nsapi_socket_t handle, const SocketAddress &address)
 {
-#ifdef w5500_INTF_DBG
-    debug("[socket_connect] fd: %d\n", SKT(handle)->fd);
-#endif
+    DBG("fd: %d\n", SKT(handle)->fd);
     //check for a valid socket
     if (SKT(handle)->fd < 0) {
         return NSAPI_ERROR_NO_SOCKET;
@@ -312,9 +308,7 @@
 
 nsapi_error_t W5500Interface::socket_accept(nsapi_socket_t server, nsapi_socket_t *handle, SocketAddress *address)
 {
-#ifdef w5500_INTF_DBG
-    debug("[socket_accept] fd: %d\n", SKT(handle)->fd);
-#endif
+    DBG("fd: %d\n", SKT(handle)->fd);
     if (SKT(server)->fd < 0) {
         return NSAPI_ERROR_NO_SOCKET;
     }
@@ -342,7 +336,7 @@
     *handle = get_sock(SKT(server)->fd);
     
     if (!(*handle)) {
-        error("No more socketa for binding");
+        error("No more sockets for binding");
         return NSAPI_ERROR_NO_SOCKET;
     }
    
@@ -389,9 +383,7 @@
 
 nsapi_size_or_error_t W5500Interface::socket_send(nsapi_socket_t handle, const void *data, nsapi_size_t size)
 {
-#ifdef w5500_INTF_DBG
-    debug("[socket_send] fd: %d\n", SKT(handle)->fd);
-#endif
+    DBG("fd: %d\n", SKT(handle)->fd);
     int writtenLen = 0;
     while (writtenLen < size) {
         int _size =  WIZnet_Chip::wait_writeable(SKT(handle)->fd, w5500_WAIT_TIMEOUT);
@@ -403,7 +395,7 @@
         }
         int ret = WIZnet_Chip::send(SKT(handle)->fd, (char*)data, (int)_size);
         if (ret < 0) {
-            debug("[socket_send] returned error -1\n");
+            DBG("returning error -1\n");
             return -1;
         }
         writtenLen += ret;
@@ -413,9 +405,7 @@
 
 nsapi_size_or_error_t W5500Interface::socket_recv(nsapi_socket_t handle, void *data, nsapi_size_t size)
 {
-#ifdef w5500_INTF_DBG
-    debug("[socket_recv] fd: %d\n", SKT(handle)->fd);
-#endif
+    DBG("fd: %d\n", SKT(handle)->fd);
     // add to cover exception.
     if ((SKT(handle)->fd < 0) || !SKT(handle)->connected) {
         return -1;
@@ -436,23 +426,18 @@
 nsapi_size_or_error_t W5500Interface::socket_sendto(nsapi_socket_t handle, const SocketAddress &address,
             const void *data, nsapi_size_t size)
 {
-#ifdef w5500_INTF_DBG
-    debug("[socket_sendto] fd: %d\n", SKT(handle)->fd);
-#endif
-#ifdef w5500_INTF_DBG
-    debug("[socket_sendto] to ip: %s:%d\n", address.get_ip_address(), address.get_port());
-#endif
+    DBG("fd: %d, ip: %s:%d\n", SKT(handle)->fd, address.get_ip_address(), address.get_port());
     if (WIZnet_Chip::is_closed(SKT(handle)->fd)) {
         nsapi_error_t err = socket_bind(handle, address);
         if (err < 0) {
-            debug("[socket_sendto] failed to bind socket: %d\n", err);
+            DBG("failed to bind socket: %d\n", err);
             return err;
         }
     }
     //compare with original: int size = eth->wait_writeable(_sock_fd, _blocking ? -1 : _timeout, length-1); 
     int len = WIZnet_Chip::wait_writeable(SKT(handle)->fd, w5500_WAIT_TIMEOUT, size-1);
     if (len < 0) {
-        debug("[socket_sendto] error: NSAPI_ERROR_WOULD_BLOCK\n");
+        DBG("error: NSAPI_ERROR_WOULD_BLOCK\n");
         return NSAPI_ERROR_WOULD_BLOCK;;
     }
     
@@ -462,41 +447,38 @@
     WIZnet_Chip::sreg<uint16_t>(SKT(handle)->fd, Sn_DPORT, address.get_port());
     
     nsapi_size_or_error_t err = WIZnet_Chip::send(SKT(handle)->fd, (const char*)data, size);
-    debug("[socket_sendto] rv: %d, size: %d\n", err, size);
+    DBG("rv: %d, size: %d\n", err, size);
+#if w5500_INTF_DBG
     if (err > 0) {
         debug("[socket_sendto] data: ");
         for(int i = 0; i < err; i++) {
             if ((i%16) == 0) {
                 debug("\n");
             }
-            uint8_t* buf = (uint8_t*)data;
-            debug(" %02x", buf[i]);
+            debug(" %02x", ((uint8_t*)data)[i]);
         }
         if ((err-1%16) != 0) {
             debug("\n");
         }
     }
+#endif
     return err;
 }
 
 nsapi_size_or_error_t W5500Interface::socket_recvfrom(nsapi_socket_t handle, SocketAddress *address,
             void *buffer, nsapi_size_t size)
 {
-#ifdef w5500_INTF_DBG
-    debug("[socket_recvfrom] fd: %d\n", SKT(handle)->fd);
-#endif
+    DBG("fd: %d\n", SKT(handle)->fd);
     //check for null pointers
     if (buffer == NULL) {
-#ifdef w5500_INTF_DBG
-        debug("[socket_recvfrom] buffer is NULL; receive is ABORTED\n");
-#endif        
+        DBG("buffer is NULL; receive is ABORTED\n");
         return -1;
     }
    
     uint8_t info[8];
     int len = WIZnet_Chip::wait_readable(SKT(handle)->fd, w5500_WAIT_TIMEOUT, sizeof(info));
     if (len < 0) {
-        debug("[socket_recvfrom] error: NSAPI_ERROR_WOULD_BLOCK\n");
+        DBG("error: NSAPI_ERROR_WOULD_BLOCK\n");
         return NSAPI_ERROR_WOULD_BLOCK;
     }
 
@@ -508,7 +490,7 @@
     uint16_t port = info[4]<<8|info[5];
     // original behavior was to terminate execution if address is NULL
     if (address != NULL) {
-        //debug("[socket_recvfrom] warn: addressis NULL");
+        //DBG("[socket_recvfrom] warn: addressis NULL");
         address->set_ip_address(addr);
         address->set_port(port);
     }
@@ -516,35 +498,34 @@
     int udp_size = info[6]<<8|info[7];
 
     if (udp_size > (len-sizeof(info))) {
-        debug("[socket_recvfrom] error: udp_size > (len-sizeof(info))\n");
+        DBG("error: udp_size > (len-sizeof(info))\n");
         return -1;
     }
     
     //receive from socket
     nsapi_size_or_error_t err = WIZnet_Chip::recv(SKT(handle)->fd, (char*)buffer, udp_size);
-    debug("[socket_recvfrom] rv: %d\n", err);
+    DBG("rv: %d\n", err);
+#if w5500_INTF_DBG
     if (err > 0) {
         debug("[socket_recvfrom] buffer:");
         for(int i = 0; i < err; i++) {
             if ((i%16) == 0) {
                 debug("\n");
             }
-            uint8_t* buf = (uint8_t*)buffer;
-            debug(" %02x", buf[i]);
+            debug(" %02x", ((uint8_t*)buffer)[i]);
         }
         if ((err-1%16) != 0) {
             debug("\n");
         }
     }
+#endif
     return  err;
 }
 
 void W5500Interface::socket_attach(void *handle, void (*callback)(void *), void *data)
 {
     if (handle == NULL) return;
-#ifdef w5500_INTF_DBG
-    debug("[socket_attach] fd: %d, callback: %p\n", SKT(handle)->fd, callback);
-#endif
+    DBG("fd: %d, callback: %p\n", SKT(handle)->fd, callback);
     SKT(handle)->callback       = callback;
     SKT(handle)->callback_data  = data;
 }
\ No newline at end of file