W5500 driver for mbed OS 5

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

Fork of W5500Interface by Sergei G

Committer:
Bongjun
Date:
Thu Aug 09 08:10:27 2018 +0000
Revision:
6:e2ab76b2be07
Parent:
2:06b6f9f7c071
Child:
8:c71c66d43703
changes for mbed OS 5 web server example; - add internal W5500 instance; - changes for web server example

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bongjun 6:e2ab76b2be07 1 /**
Bongjun 6:e2ab76b2be07 2 ******************************************************************************
Bongjun 6:e2ab76b2be07 3 * @file W5500Interface.h
Bongjun 6:e2ab76b2be07 4 * @author Bongjun Hur (modified version from Sergei G (https://os.mbed.com/users/sgnezdov/))
Bongjun 6:e2ab76b2be07 5 * @brief Header file of the NetworkStack for the W5500 Device
Bongjun 6:e2ab76b2be07 6 ******************************************************************************
Bongjun 6:e2ab76b2be07 7 * @attention
Bongjun 6:e2ab76b2be07 8 *
Bongjun 6:e2ab76b2be07 9 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
Bongjun 6:e2ab76b2be07 10 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
Bongjun 6:e2ab76b2be07 11 * TIME. AS A RESULT, WIZnet SHALL NOT BE HELD LIABLE FOR ANY
Bongjun 6:e2ab76b2be07 12 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
Bongjun 6:e2ab76b2be07 13 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
Bongjun 6:e2ab76b2be07 14 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
Bongjun 6:e2ab76b2be07 15 *
Bongjun 6:e2ab76b2be07 16 * <h2><center>&copy; COPYRIGHT 2017,2018 WIZnet Co.,Ltd.</center></h2>
Bongjun 6:e2ab76b2be07 17 ******************************************************************************
Bongjun 6:e2ab76b2be07 18 */
Bongjun 6:e2ab76b2be07 19
Bongjun 6:e2ab76b2be07 20 #ifndef W5500_INTERFACE_H
Bongjun 6:e2ab76b2be07 21 #define W5500_INTERFACE_H
Bongjun 6:e2ab76b2be07 22
Bongjun 6:e2ab76b2be07 23 #include "mbed.h"
Bongjun 6:e2ab76b2be07 24 #include "W5500.h"
Bongjun 6:e2ab76b2be07 25 //#include "rtos.h"
Bongjun 6:e2ab76b2be07 26
Bongjun 6:e2ab76b2be07 27 /** w5500_socket struct
Bongjun 6:e2ab76b2be07 28 * W5500 socket
Bongjun 6:e2ab76b2be07 29 */
Bongjun 6:e2ab76b2be07 30
Bongjun 6:e2ab76b2be07 31 struct w5500_socket {
Bongjun 6:e2ab76b2be07 32 int fd;
Bongjun 6:e2ab76b2be07 33 nsapi_protocol_t proto;
Bongjun 6:e2ab76b2be07 34 bool connected;
Bongjun 6:e2ab76b2be07 35 void (*callback)(void *);
Bongjun 6:e2ab76b2be07 36 void *callback_data;
Bongjun 6:e2ab76b2be07 37 };
Bongjun 6:e2ab76b2be07 38
Bongjun 6:e2ab76b2be07 39 /** W5500Interface class
Bongjun 6:e2ab76b2be07 40 * Implementation of the NetworkStack for the W5500
Bongjun 6:e2ab76b2be07 41 */
Bongjun 6:e2ab76b2be07 42
Bongjun 6:e2ab76b2be07 43 class W5500Interface : public NetworkStack, public EthInterface
Bongjun 6:e2ab76b2be07 44 {
Bongjun 6:e2ab76b2be07 45 public:
Bongjun 6:e2ab76b2be07 46 W5500Interface(SPI* spi, PinName cs, PinName reset);
Bongjun 6:e2ab76b2be07 47 W5500Interface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
Bongjun 6:e2ab76b2be07 48
Bongjun 6:e2ab76b2be07 49 int init();
Bongjun 6:e2ab76b2be07 50 int init(uint8_t * mac);
Bongjun 6:e2ab76b2be07 51 int init(const char* ip, const char* mask, const char* gateway);
Bongjun 6:e2ab76b2be07 52 int init(uint8_t * mac, const char* ip, const char* mask, const char* gateway);
Bongjun 6:e2ab76b2be07 53
Bongjun 6:e2ab76b2be07 54 /** Start the interface
Bongjun 6:e2ab76b2be07 55 * @return 0 on success, negative on failure
Bongjun 6:e2ab76b2be07 56 */
Bongjun 6:e2ab76b2be07 57 virtual int connect();
Bongjun 6:e2ab76b2be07 58
Bongjun 6:e2ab76b2be07 59 /** Stop the interface
Bongjun 6:e2ab76b2be07 60 * @return 0 on success, negative on failure
Bongjun 6:e2ab76b2be07 61 */
Bongjun 6:e2ab76b2be07 62 virtual int disconnect();
Bongjun 6:e2ab76b2be07 63
Bongjun 6:e2ab76b2be07 64 /** Get the internally stored IP address
Bongjun 6:e2ab76b2be07 65 * @return IP address of the interface or null if not yet connected
Bongjun 6:e2ab76b2be07 66 */
Bongjun 6:e2ab76b2be07 67 virtual const char *get_ip_address();
Bongjun 6:e2ab76b2be07 68
Bongjun 6:e2ab76b2be07 69 /** Get MAC address and fill mac with it.
Bongjun 6:e2ab76b2be07 70 */
Bongjun 6:e2ab76b2be07 71 void get_mac(uint8_t mac[6]) ;
Bongjun 6:e2ab76b2be07 72
Bongjun 6:e2ab76b2be07 73 /** Get the internally stored MAC address
Bongjun 6:e2ab76b2be07 74 * @return MAC address of the interface
Bongjun 6:e2ab76b2be07 75 */
Bongjun 6:e2ab76b2be07 76 virtual const char *get_mac_address();
Bongjun 6:e2ab76b2be07 77
Bongjun 6:e2ab76b2be07 78 protected:
Bongjun 6:e2ab76b2be07 79 /** Opens a socket
Bongjun 6:e2ab76b2be07 80 *
Bongjun 6:e2ab76b2be07 81 * Creates a network socket and stores it in the specified handle.
Bongjun 6:e2ab76b2be07 82 * The handle must be passed to following calls on the socket.
Bongjun 6:e2ab76b2be07 83 *
Bongjun 6:e2ab76b2be07 84 * A stack may have a finite number of sockets, in this case
Bongjun 6:e2ab76b2be07 85 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
Bongjun 6:e2ab76b2be07 86 *
Bongjun 6:e2ab76b2be07 87 * @param handle Destination for the handle to a newly created socket
Bongjun 6:e2ab76b2be07 88 * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
Bongjun 6:e2ab76b2be07 89 * @return 0 on success, negative error code on failure
Bongjun 6:e2ab76b2be07 90 */
Bongjun 6:e2ab76b2be07 91 virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto);
Bongjun 6:e2ab76b2be07 92 // virtual int socket_open(void **handle, nsapi_protocol_t proto);
Bongjun 6:e2ab76b2be07 93
Bongjun 6:e2ab76b2be07 94 /** Close the socket
Bongjun 6:e2ab76b2be07 95 *
Bongjun 6:e2ab76b2be07 96 * Closes any open connection and deallocates any memory associated
Bongjun 6:e2ab76b2be07 97 * with the socket.
Bongjun 6:e2ab76b2be07 98 *
Bongjun 6:e2ab76b2be07 99 * @param handle Socket handle
Bongjun 6:e2ab76b2be07 100 * @return 0 on success, negative error code on failure
Bongjun 6:e2ab76b2be07 101 */
Bongjun 6:e2ab76b2be07 102 virtual nsapi_error_t socket_close(nsapi_socket_t handle);
Bongjun 6:e2ab76b2be07 103
Bongjun 6:e2ab76b2be07 104 /** Bind a specific address to a socket
Bongjun 6:e2ab76b2be07 105 *
Bongjun 6:e2ab76b2be07 106 * Binding a socket specifies the address and port on which to recieve
Bongjun 6:e2ab76b2be07 107 * data. If the IP address is zeroed, only the port is bound.
Bongjun 6:e2ab76b2be07 108 *
Bongjun 6:e2ab76b2be07 109 * @param handle Socket handle
Bongjun 6:e2ab76b2be07 110 * @param address Local address to bind
Bongjun 6:e2ab76b2be07 111 * @return 0 on success, negative error code on failure.
Bongjun 6:e2ab76b2be07 112 */
Bongjun 6:e2ab76b2be07 113 virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address);
Bongjun 6:e2ab76b2be07 114
Bongjun 6:e2ab76b2be07 115 /** Listen for connections on a TCP socket
Bongjun 6:e2ab76b2be07 116 *
Bongjun 6:e2ab76b2be07 117 * Marks the socket as a passive socket that can be used to accept
Bongjun 6:e2ab76b2be07 118 * incoming connections.
Bongjun 6:e2ab76b2be07 119 *
Bongjun 6:e2ab76b2be07 120 * @param handle Socket handle
Bongjun 6:e2ab76b2be07 121 * @param backlog Number of pending connections that can be queued
Bongjun 6:e2ab76b2be07 122 * simultaneously
Bongjun 6:e2ab76b2be07 123 * @return 0 on success, negative error code on failure
Bongjun 6:e2ab76b2be07 124 */
Bongjun 6:e2ab76b2be07 125 virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog);
Bongjun 6:e2ab76b2be07 126
Bongjun 6:e2ab76b2be07 127 /** Connects TCP socket to a remote host
Bongjun 6:e2ab76b2be07 128 *
Bongjun 6:e2ab76b2be07 129 * Initiates a connection to a remote server specified by the
Bongjun 6:e2ab76b2be07 130 * indicated address.
Bongjun 6:e2ab76b2be07 131 *
Bongjun 6:e2ab76b2be07 132 * @param handle Socket handle
Bongjun 6:e2ab76b2be07 133 * @param address The SocketAddress of the remote host
Bongjun 6:e2ab76b2be07 134 * @return 0 on success, negative error code on failure
Bongjun 6:e2ab76b2be07 135 */
Bongjun 6:e2ab76b2be07 136 virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address);
Bongjun 6:e2ab76b2be07 137
Bongjun 6:e2ab76b2be07 138 /** Accepts a connection on a TCP socket
Bongjun 6:e2ab76b2be07 139 *
Bongjun 6:e2ab76b2be07 140 * The server socket must be bound and set to listen for connections.
Bongjun 6:e2ab76b2be07 141 * On a new connection, creates a network socket and stores it in the
Bongjun 6:e2ab76b2be07 142 * specified handle. The handle must be passed to following calls on
Bongjun 6:e2ab76b2be07 143 * the socket.
Bongjun 6:e2ab76b2be07 144 *
Bongjun 6:e2ab76b2be07 145 * A stack may have a finite number of sockets, in this case
Bongjun 6:e2ab76b2be07 146 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
Bongjun 6:e2ab76b2be07 147 *
Bongjun 6:e2ab76b2be07 148 * This call is non-blocking. If accept would block,
Bongjun 6:e2ab76b2be07 149 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
Bongjun 6:e2ab76b2be07 150 *
Bongjun 6:e2ab76b2be07 151 * @param server Socket handle to server to accept from
Bongjun 6:e2ab76b2be07 152 * @param handle Destination for a handle to the newly created socket
Bongjun 6:e2ab76b2be07 153 * @param address Destination for the remote address or NULL
Bongjun 6:e2ab76b2be07 154 * @return 0 on success, negative error code on failure
Bongjun 6:e2ab76b2be07 155 */
Bongjun 6:e2ab76b2be07 156 virtual nsapi_error_t socket_accept(nsapi_socket_t server,
Bongjun 6:e2ab76b2be07 157 nsapi_socket_t *handle, SocketAddress *address=0);
Bongjun 6:e2ab76b2be07 158
Bongjun 6:e2ab76b2be07 159 /** Send data over a TCP socket
Bongjun 6:e2ab76b2be07 160 *
Bongjun 6:e2ab76b2be07 161 * The socket must be connected to a remote host. Returns the number of
Bongjun 6:e2ab76b2be07 162 * bytes sent from the buffer.
Bongjun 6:e2ab76b2be07 163 *
Bongjun 6:e2ab76b2be07 164 * This call is non-blocking. If send would block,
Bongjun 6:e2ab76b2be07 165 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
Bongjun 6:e2ab76b2be07 166 *
Bongjun 6:e2ab76b2be07 167 * @param handle Socket handle
Bongjun 6:e2ab76b2be07 168 * @param data Buffer of data to send to the host
Bongjun 6:e2ab76b2be07 169 * @param size Size of the buffer in bytes
Bongjun 6:e2ab76b2be07 170 * @return Number of sent bytes on success, negative error
Bongjun 6:e2ab76b2be07 171 * code on failure
Bongjun 6:e2ab76b2be07 172 */
Bongjun 6:e2ab76b2be07 173 virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle,
Bongjun 6:e2ab76b2be07 174 const void *data, nsapi_size_t size);
Bongjun 6:e2ab76b2be07 175
Bongjun 6:e2ab76b2be07 176 /** Receive data over a TCP socket
Bongjun 6:e2ab76b2be07 177 *
Bongjun 6:e2ab76b2be07 178 * The socket must be connected to a remote host. Returns the number of
Bongjun 6:e2ab76b2be07 179 * bytes received into the buffer.
Bongjun 6:e2ab76b2be07 180 *
Bongjun 6:e2ab76b2be07 181 * This call is non-blocking. If recv would block,
Bongjun 6:e2ab76b2be07 182 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
Bongjun 6:e2ab76b2be07 183 *
Bongjun 6:e2ab76b2be07 184 * @param handle Socket handle
Bongjun 6:e2ab76b2be07 185 * @param data Destination buffer for data received from the host
Bongjun 6:e2ab76b2be07 186 * @param size Size of the buffer in bytes
Bongjun 6:e2ab76b2be07 187 * @return Number of received bytes on success, negative error
Bongjun 6:e2ab76b2be07 188 * code on failure
Bongjun 6:e2ab76b2be07 189 */
Bongjun 6:e2ab76b2be07 190 virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle,
Bongjun 6:e2ab76b2be07 191 void *data, nsapi_size_t size);
Bongjun 6:e2ab76b2be07 192
Bongjun 6:e2ab76b2be07 193 /** Send a packet over a UDP socket
Bongjun 6:e2ab76b2be07 194 *
Bongjun 6:e2ab76b2be07 195 * Sends data to the specified address. Returns the number of bytes
Bongjun 6:e2ab76b2be07 196 * sent from the buffer.
Bongjun 6:e2ab76b2be07 197 *
Bongjun 6:e2ab76b2be07 198 * This call is non-blocking. If sendto would block,
Bongjun 6:e2ab76b2be07 199 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
Bongjun 6:e2ab76b2be07 200 *
Bongjun 6:e2ab76b2be07 201 * @param handle Socket handle
Bongjun 6:e2ab76b2be07 202 * @param address The SocketAddress of the remote host
Bongjun 6:e2ab76b2be07 203 * @param data Buffer of data to send to the host
Bongjun 6:e2ab76b2be07 204 * @param size Size of the buffer in bytes
Bongjun 6:e2ab76b2be07 205 * @return Number of sent bytes on success, negative error
Bongjun 6:e2ab76b2be07 206 * code on failure
Bongjun 6:e2ab76b2be07 207 */
Bongjun 6:e2ab76b2be07 208 virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address,
Bongjun 6:e2ab76b2be07 209 const void *data, nsapi_size_t size);
Bongjun 6:e2ab76b2be07 210
Bongjun 6:e2ab76b2be07 211 /** Receive a packet over a UDP socket
Bongjun 6:e2ab76b2be07 212 *
Bongjun 6:e2ab76b2be07 213 * Receives data and stores the source address in address if address
Bongjun 6:e2ab76b2be07 214 * is not NULL. Returns the number of bytes received into the buffer.
Bongjun 6:e2ab76b2be07 215 *
Bongjun 6:e2ab76b2be07 216 * This call is non-blocking. If recvfrom would block,
Bongjun 6:e2ab76b2be07 217 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
Bongjun 6:e2ab76b2be07 218 *
Bongjun 6:e2ab76b2be07 219 * @param handle Socket handle
Bongjun 6:e2ab76b2be07 220 * @param address Destination for the source address or NULL
Bongjun 6:e2ab76b2be07 221 * @param data Destination buffer for data received from the host
Bongjun 6:e2ab76b2be07 222 * @param size Size of the buffer in bytes
Bongjun 6:e2ab76b2be07 223 * @return Number of received bytes on success, negative error
Bongjun 6:e2ab76b2be07 224 * code on failure
Bongjun 6:e2ab76b2be07 225 */
Bongjun 6:e2ab76b2be07 226 virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address,
Bongjun 6:e2ab76b2be07 227 void *buffer, nsapi_size_t size);
Bongjun 6:e2ab76b2be07 228
Bongjun 6:e2ab76b2be07 229 /** Register a callback on state change of the socket
Bongjun 6:e2ab76b2be07 230 *
Bongjun 6:e2ab76b2be07 231 * The specified callback will be called on state changes such as when
Bongjun 6:e2ab76b2be07 232 * the socket can recv/send/accept successfully and on when an error
Bongjun 6:e2ab76b2be07 233 * occurs. The callback may also be called spuriously without reason.
Bongjun 6:e2ab76b2be07 234 *
Bongjun 6:e2ab76b2be07 235 * The callback may be called in an interrupt context and should not
Bongjun 6:e2ab76b2be07 236 * perform expensive operations such as recv/send calls.
Bongjun 6:e2ab76b2be07 237 *
Bongjun 6:e2ab76b2be07 238 * @param handle Socket handle
Bongjun 6:e2ab76b2be07 239 * @param callback Function to call on state change
Bongjun 6:e2ab76b2be07 240 * @param data Argument to pass to callback
Bongjun 6:e2ab76b2be07 241 */
Bongjun 6:e2ab76b2be07 242 virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data);
Bongjun 6:e2ab76b2be07 243
Bongjun 6:e2ab76b2be07 244 virtual NetworkStack* get_stack() {return this;}
Bongjun 6:e2ab76b2be07 245
Bongjun 6:e2ab76b2be07 246 private:
Bongjun 6:e2ab76b2be07 247 WIZnet_Chip _w5500;
Bongjun 6:e2ab76b2be07 248
Bongjun 6:e2ab76b2be07 249 char ip_string[20];
Bongjun 6:e2ab76b2be07 250 char mask_string[20];
Bongjun 6:e2ab76b2be07 251 char gw_string[20];
Bongjun 6:e2ab76b2be07 252 char mac_string[20];
Bongjun 6:e2ab76b2be07 253 bool ip_set;
Bongjun 6:e2ab76b2be07 254
Bongjun 6:e2ab76b2be07 255 int listen_port;
Bongjun 6:e2ab76b2be07 256
Bongjun 6:e2ab76b2be07 257 //void signal_event(nsapi_socket_t handle);
Bongjun 6:e2ab76b2be07 258 //void event();
Bongjun 6:e2ab76b2be07 259
Bongjun 6:e2ab76b2be07 260 //w5500 socket management
Bongjun 6:e2ab76b2be07 261 struct w5500_socket w5500_sockets[MAX_SOCK_NUM];
Bongjun 6:e2ab76b2be07 262 w5500_socket* get_sock(int fd);
Bongjun 6:e2ab76b2be07 263 void init_socks();
Bongjun 6:e2ab76b2be07 264 /*
Bongjun 6:e2ab76b2be07 265 Thread *_daemon;
Bongjun 6:e2ab76b2be07 266 void daemon();
Bongjun 6:e2ab76b2be07 267 */
Bongjun 6:e2ab76b2be07 268
Bongjun 6:e2ab76b2be07 269 };
Bongjun 6:e2ab76b2be07 270
Bongjun 6:e2ab76b2be07 271 #endif