W5500 from SeeedStudio on NUCLEO-L476RG

Dependents:   coap-example Borsch coap-example

Fork of W5500Interface by AMETEK Powervar

Committer:
dgriffin65
Date:
Thu Jun 15 20:19:23 2017 +0000
Revision:
1:2dee44ea52a9
Parent:
0:77e050d1fb12
Child:
2:06b6f9f7c071
Updated to mbed-os

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dgriffin65 0:77e050d1fb12 1 /* W5500 implementation of NetworkInterfaceAPI
dgriffin65 0:77e050d1fb12 2 * Copyright (c) 2015 ARM Limited
dgriffin65 0:77e050d1fb12 3 *
dgriffin65 0:77e050d1fb12 4 * Licensed under the Apache License, Version 2.0 (the "License");
dgriffin65 0:77e050d1fb12 5 * you may not use this file except in compliance with the License.
dgriffin65 0:77e050d1fb12 6 * You may obtain a copy of the License at
dgriffin65 0:77e050d1fb12 7 *
dgriffin65 0:77e050d1fb12 8 * http://www.apache.org/licenses/LICENSE-2.0
dgriffin65 0:77e050d1fb12 9 *
dgriffin65 0:77e050d1fb12 10 * Unless required by applicable law or agreed to in writing, software
dgriffin65 0:77e050d1fb12 11 * distributed under the License is distributed on an "AS IS" BASIS,
dgriffin65 0:77e050d1fb12 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
dgriffin65 0:77e050d1fb12 13 * See the License for the specific language governing permissions and
dgriffin65 0:77e050d1fb12 14 * limitations under the License.
dgriffin65 0:77e050d1fb12 15 */
dgriffin65 0:77e050d1fb12 16
dgriffin65 0:77e050d1fb12 17 #ifndef W5500_INTERFACE_H
dgriffin65 0:77e050d1fb12 18 #define W5500_INTERFACE_H
dgriffin65 0:77e050d1fb12 19
dgriffin65 0:77e050d1fb12 20 #include "NetworkStack.h"
dgriffin65 0:77e050d1fb12 21 #include "EthInterface.h"
dgriffin65 0:77e050d1fb12 22
dgriffin65 0:77e050d1fb12 23 #include "rtos.h"
dgriffin65 0:77e050d1fb12 24 #include "WIZnet/W5500.h"
dgriffin65 0:77e050d1fb12 25
dgriffin65 1:2dee44ea52a9 26 /** w5500_socket struct
dgriffin65 1:2dee44ea52a9 27 * W5500 socket
dgriffin65 1:2dee44ea52a9 28 */
dgriffin65 1:2dee44ea52a9 29
dgriffin65 1:2dee44ea52a9 30 struct w5500_socket {
dgriffin65 1:2dee44ea52a9 31 int fd;
dgriffin65 1:2dee44ea52a9 32 nsapi_protocol_t proto;
dgriffin65 1:2dee44ea52a9 33 void (*callback)(void *);
dgriffin65 1:2dee44ea52a9 34 void *callback_data;
dgriffin65 1:2dee44ea52a9 35 bool connected;
dgriffin65 1:2dee44ea52a9 36 };
dgriffin65 1:2dee44ea52a9 37
dgriffin65 0:77e050d1fb12 38 /** W5500Interface class
dgriffin65 0:77e050d1fb12 39 * Implementation of the NetworkStack for the W5500
dgriffin65 0:77e050d1fb12 40 */
dgriffin65 1:2dee44ea52a9 41
dgriffin65 0:77e050d1fb12 42 class W5500Interface : public NetworkStack, public EthInterface, public WIZnet_Chip
dgriffin65 0:77e050d1fb12 43 {
dgriffin65 0:77e050d1fb12 44 public:
dgriffin65 0:77e050d1fb12 45 W5500Interface(SPI* spi, PinName cs, PinName reset);
dgriffin65 0:77e050d1fb12 46 W5500Interface(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
dgriffin65 0:77e050d1fb12 47
dgriffin65 0:77e050d1fb12 48 int init();
dgriffin65 0:77e050d1fb12 49 int init(uint8_t * mac);
dgriffin65 0:77e050d1fb12 50 int init(const char* ip, const char* mask, const char* gateway);
dgriffin65 0:77e050d1fb12 51 int init(uint8_t * mac, const char* ip, const char* mask, const char* gateway);
dgriffin65 0:77e050d1fb12 52
dgriffin65 0:77e050d1fb12 53 /** Start the interface
dgriffin65 0:77e050d1fb12 54 * @return 0 on success, negative on failure
dgriffin65 0:77e050d1fb12 55 */
dgriffin65 0:77e050d1fb12 56 virtual int connect();
dgriffin65 0:77e050d1fb12 57
dgriffin65 0:77e050d1fb12 58 /** Stop the interface
dgriffin65 0:77e050d1fb12 59 * @return 0 on success, negative on failure
dgriffin65 0:77e050d1fb12 60 */
dgriffin65 0:77e050d1fb12 61 virtual int disconnect();
dgriffin65 0:77e050d1fb12 62
dgriffin65 0:77e050d1fb12 63 /** Get the internally stored IP address
dgriffin65 0:77e050d1fb12 64 * @return IP address of the interface or null if not yet connected
dgriffin65 0:77e050d1fb12 65 */
dgriffin65 0:77e050d1fb12 66 virtual const char *get_ip_address();
dgriffin65 0:77e050d1fb12 67
dgriffin65 0:77e050d1fb12 68 /** Get the internally stored MAC address
dgriffin65 0:77e050d1fb12 69 * @return MAC address of the interface
dgriffin65 0:77e050d1fb12 70 */
dgriffin65 0:77e050d1fb12 71 virtual const char *get_mac_address();
dgriffin65 0:77e050d1fb12 72
dgriffin65 0:77e050d1fb12 73 protected:
dgriffin65 0:77e050d1fb12 74 /** Opens a socket
dgriffin65 0:77e050d1fb12 75 *
dgriffin65 0:77e050d1fb12 76 * Creates a network socket and stores it in the specified handle.
dgriffin65 0:77e050d1fb12 77 * The handle must be passed to following calls on the socket.
dgriffin65 0:77e050d1fb12 78 *
dgriffin65 0:77e050d1fb12 79 * A stack may have a finite number of sockets, in this case
dgriffin65 0:77e050d1fb12 80 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
dgriffin65 0:77e050d1fb12 81 *
dgriffin65 0:77e050d1fb12 82 * @param handle Destination for the handle to a newly created socket
dgriffin65 0:77e050d1fb12 83 * @param proto Protocol of socket to open, NSAPI_TCP or NSAPI_UDP
dgriffin65 0:77e050d1fb12 84 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 85 */
dgriffin65 0:77e050d1fb12 86 virtual nsapi_error_t socket_open(nsapi_socket_t *handle, nsapi_protocol_t proto);
dgriffin65 0:77e050d1fb12 87
dgriffin65 0:77e050d1fb12 88 /** Close the socket
dgriffin65 0:77e050d1fb12 89 *
dgriffin65 0:77e050d1fb12 90 * Closes any open connection and deallocates any memory associated
dgriffin65 0:77e050d1fb12 91 * with the socket.
dgriffin65 0:77e050d1fb12 92 *
dgriffin65 0:77e050d1fb12 93 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 94 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 95 */
dgriffin65 0:77e050d1fb12 96 virtual nsapi_error_t socket_close(nsapi_socket_t handle);
dgriffin65 0:77e050d1fb12 97
dgriffin65 0:77e050d1fb12 98 /** Bind a specific address to a socket
dgriffin65 0:77e050d1fb12 99 *
dgriffin65 0:77e050d1fb12 100 * Binding a socket specifies the address and port on which to recieve
dgriffin65 0:77e050d1fb12 101 * data. If the IP address is zeroed, only the port is bound.
dgriffin65 0:77e050d1fb12 102 *
dgriffin65 0:77e050d1fb12 103 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 104 * @param address Local address to bind
dgriffin65 0:77e050d1fb12 105 * @return 0 on success, negative error code on failure.
dgriffin65 0:77e050d1fb12 106 */
dgriffin65 0:77e050d1fb12 107 virtual nsapi_error_t socket_bind(nsapi_socket_t handle, const SocketAddress &address);
dgriffin65 0:77e050d1fb12 108
dgriffin65 0:77e050d1fb12 109 /** Listen for connections on a TCP socket
dgriffin65 0:77e050d1fb12 110 *
dgriffin65 0:77e050d1fb12 111 * Marks the socket as a passive socket that can be used to accept
dgriffin65 0:77e050d1fb12 112 * incoming connections.
dgriffin65 0:77e050d1fb12 113 *
dgriffin65 0:77e050d1fb12 114 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 115 * @param backlog Number of pending connections that can be queued
dgriffin65 0:77e050d1fb12 116 * simultaneously
dgriffin65 0:77e050d1fb12 117 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 118 */
dgriffin65 0:77e050d1fb12 119 virtual nsapi_error_t socket_listen(nsapi_socket_t handle, int backlog);
dgriffin65 0:77e050d1fb12 120
dgriffin65 0:77e050d1fb12 121 /** Connects TCP socket to a remote host
dgriffin65 0:77e050d1fb12 122 *
dgriffin65 0:77e050d1fb12 123 * Initiates a connection to a remote server specified by the
dgriffin65 0:77e050d1fb12 124 * indicated address.
dgriffin65 0:77e050d1fb12 125 *
dgriffin65 0:77e050d1fb12 126 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 127 * @param address The SocketAddress of the remote host
dgriffin65 0:77e050d1fb12 128 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 129 */
dgriffin65 0:77e050d1fb12 130 virtual nsapi_error_t socket_connect(nsapi_socket_t handle, const SocketAddress &address);
dgriffin65 0:77e050d1fb12 131
dgriffin65 0:77e050d1fb12 132 /** Accepts a connection on a TCP socket
dgriffin65 0:77e050d1fb12 133 *
dgriffin65 0:77e050d1fb12 134 * The server socket must be bound and set to listen for connections.
dgriffin65 0:77e050d1fb12 135 * On a new connection, creates a network socket and stores it in the
dgriffin65 0:77e050d1fb12 136 * specified handle. The handle must be passed to following calls on
dgriffin65 0:77e050d1fb12 137 * the socket.
dgriffin65 0:77e050d1fb12 138 *
dgriffin65 0:77e050d1fb12 139 * A stack may have a finite number of sockets, in this case
dgriffin65 0:77e050d1fb12 140 * NSAPI_ERROR_NO_SOCKET is returned if no socket is available.
dgriffin65 0:77e050d1fb12 141 *
dgriffin65 0:77e050d1fb12 142 * This call is non-blocking. If accept would block,
dgriffin65 0:77e050d1fb12 143 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
dgriffin65 0:77e050d1fb12 144 *
dgriffin65 0:77e050d1fb12 145 * @param server Socket handle to server to accept from
dgriffin65 0:77e050d1fb12 146 * @param handle Destination for a handle to the newly created socket
dgriffin65 0:77e050d1fb12 147 * @param address Destination for the remote address or NULL
dgriffin65 0:77e050d1fb12 148 * @return 0 on success, negative error code on failure
dgriffin65 0:77e050d1fb12 149 */
dgriffin65 0:77e050d1fb12 150 virtual nsapi_error_t socket_accept(nsapi_socket_t server,
dgriffin65 0:77e050d1fb12 151 nsapi_socket_t *handle, SocketAddress *address=0);
dgriffin65 0:77e050d1fb12 152
dgriffin65 0:77e050d1fb12 153 /** Send data over a TCP socket
dgriffin65 0:77e050d1fb12 154 *
dgriffin65 0:77e050d1fb12 155 * The socket must be connected to a remote host. Returns the number of
dgriffin65 0:77e050d1fb12 156 * bytes sent from the buffer.
dgriffin65 0:77e050d1fb12 157 *
dgriffin65 0:77e050d1fb12 158 * This call is non-blocking. If send would block,
dgriffin65 0:77e050d1fb12 159 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
dgriffin65 0:77e050d1fb12 160 *
dgriffin65 0:77e050d1fb12 161 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 162 * @param data Buffer of data to send to the host
dgriffin65 0:77e050d1fb12 163 * @param size Size of the buffer in bytes
dgriffin65 0:77e050d1fb12 164 * @return Number of sent bytes on success, negative error
dgriffin65 0:77e050d1fb12 165 * code on failure
dgriffin65 0:77e050d1fb12 166 */
dgriffin65 0:77e050d1fb12 167 virtual nsapi_size_or_error_t socket_send(nsapi_socket_t handle,
dgriffin65 0:77e050d1fb12 168 const void *data, nsapi_size_t size);
dgriffin65 0:77e050d1fb12 169
dgriffin65 0:77e050d1fb12 170 /** Receive data over a TCP socket
dgriffin65 0:77e050d1fb12 171 *
dgriffin65 0:77e050d1fb12 172 * The socket must be connected to a remote host. Returns the number of
dgriffin65 0:77e050d1fb12 173 * bytes received into the buffer.
dgriffin65 0:77e050d1fb12 174 *
dgriffin65 0:77e050d1fb12 175 * This call is non-blocking. If recv would block,
dgriffin65 0:77e050d1fb12 176 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
dgriffin65 0:77e050d1fb12 177 *
dgriffin65 0:77e050d1fb12 178 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 179 * @param data Destination buffer for data received from the host
dgriffin65 0:77e050d1fb12 180 * @param size Size of the buffer in bytes
dgriffin65 0:77e050d1fb12 181 * @return Number of received bytes on success, negative error
dgriffin65 0:77e050d1fb12 182 * code on failure
dgriffin65 0:77e050d1fb12 183 */
dgriffin65 0:77e050d1fb12 184 virtual nsapi_size_or_error_t socket_recv(nsapi_socket_t handle,
dgriffin65 0:77e050d1fb12 185 void *data, nsapi_size_t size);
dgriffin65 0:77e050d1fb12 186
dgriffin65 0:77e050d1fb12 187 /** Send a packet over a UDP socket
dgriffin65 0:77e050d1fb12 188 *
dgriffin65 0:77e050d1fb12 189 * Sends data to the specified address. Returns the number of bytes
dgriffin65 0:77e050d1fb12 190 * sent from the buffer.
dgriffin65 0:77e050d1fb12 191 *
dgriffin65 0:77e050d1fb12 192 * This call is non-blocking. If sendto would block,
dgriffin65 0:77e050d1fb12 193 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
dgriffin65 0:77e050d1fb12 194 *
dgriffin65 0:77e050d1fb12 195 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 196 * @param address The SocketAddress of the remote host
dgriffin65 0:77e050d1fb12 197 * @param data Buffer of data to send to the host
dgriffin65 0:77e050d1fb12 198 * @param size Size of the buffer in bytes
dgriffin65 0:77e050d1fb12 199 * @return Number of sent bytes on success, negative error
dgriffin65 0:77e050d1fb12 200 * code on failure
dgriffin65 0:77e050d1fb12 201 */
dgriffin65 0:77e050d1fb12 202 virtual nsapi_size_or_error_t socket_sendto(nsapi_socket_t handle, const SocketAddress &address,
dgriffin65 0:77e050d1fb12 203 const void *data, nsapi_size_t size);
dgriffin65 0:77e050d1fb12 204
dgriffin65 0:77e050d1fb12 205 /** Receive a packet over a UDP socket
dgriffin65 0:77e050d1fb12 206 *
dgriffin65 0:77e050d1fb12 207 * Receives data and stores the source address in address if address
dgriffin65 0:77e050d1fb12 208 * is not NULL. Returns the number of bytes received into the buffer.
dgriffin65 0:77e050d1fb12 209 *
dgriffin65 0:77e050d1fb12 210 * This call is non-blocking. If recvfrom would block,
dgriffin65 0:77e050d1fb12 211 * NSAPI_ERROR_WOULD_BLOCK is returned immediately.
dgriffin65 0:77e050d1fb12 212 *
dgriffin65 0:77e050d1fb12 213 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 214 * @param address Destination for the source address or NULL
dgriffin65 0:77e050d1fb12 215 * @param data Destination buffer for data received from the host
dgriffin65 0:77e050d1fb12 216 * @param size Size of the buffer in bytes
dgriffin65 0:77e050d1fb12 217 * @return Number of received bytes on success, negative error
dgriffin65 0:77e050d1fb12 218 * code on failure
dgriffin65 0:77e050d1fb12 219 */
dgriffin65 0:77e050d1fb12 220 virtual nsapi_size_or_error_t socket_recvfrom(nsapi_socket_t handle, SocketAddress *address,
dgriffin65 0:77e050d1fb12 221 void *buffer, nsapi_size_t size);
dgriffin65 0:77e050d1fb12 222
dgriffin65 0:77e050d1fb12 223 /** Register a callback on state change of the socket
dgriffin65 0:77e050d1fb12 224 *
dgriffin65 0:77e050d1fb12 225 * The specified callback will be called on state changes such as when
dgriffin65 0:77e050d1fb12 226 * the socket can recv/send/accept successfully and on when an error
dgriffin65 0:77e050d1fb12 227 * occurs. The callback may also be called spuriously without reason.
dgriffin65 0:77e050d1fb12 228 *
dgriffin65 0:77e050d1fb12 229 * The callback may be called in an interrupt context and should not
dgriffin65 0:77e050d1fb12 230 * perform expensive operations such as recv/send calls.
dgriffin65 0:77e050d1fb12 231 *
dgriffin65 0:77e050d1fb12 232 * @param handle Socket handle
dgriffin65 0:77e050d1fb12 233 * @param callback Function to call on state change
dgriffin65 0:77e050d1fb12 234 * @param data Argument to pass to callback
dgriffin65 0:77e050d1fb12 235 */
dgriffin65 0:77e050d1fb12 236 virtual void socket_attach(nsapi_socket_t handle, void (*callback)(void *), void *data);
dgriffin65 0:77e050d1fb12 237
dgriffin65 0:77e050d1fb12 238 virtual NetworkStack* get_stack() {return this;}
dgriffin65 0:77e050d1fb12 239
dgriffin65 0:77e050d1fb12 240 private:
dgriffin65 0:77e050d1fb12 241 char ip_string[20];
dgriffin65 0:77e050d1fb12 242 char mask_string[20];
dgriffin65 0:77e050d1fb12 243 char gw_string[20];
dgriffin65 0:77e050d1fb12 244 char mac_string[20];
dgriffin65 0:77e050d1fb12 245 bool ip_set;
dgriffin65 0:77e050d1fb12 246
dgriffin65 0:77e050d1fb12 247 int listen_port;
dgriffin65 1:2dee44ea52a9 248
dgriffin65 1:2dee44ea52a9 249 void signal_event(nsapi_socket_t handle);
dgriffin65 1:2dee44ea52a9 250
dgriffin65 1:2dee44ea52a9 251 //w5500 socket management
dgriffin65 1:2dee44ea52a9 252 struct w5500_socket w5500_sockets[MAX_SOCK_NUM];
dgriffin65 1:2dee44ea52a9 253 w5500_socket* get_sock(int fd);
dgriffin65 1:2dee44ea52a9 254 void init_socks();
dgriffin65 1:2dee44ea52a9 255
dgriffin65 1:2dee44ea52a9 256 Thread *_daemon;
dgriffin65 1:2dee44ea52a9 257 void daemon();
dgriffin65 1:2dee44ea52a9 258
dgriffin65 0:77e050d1fb12 259 };
dgriffin65 0:77e050d1fb12 260
dgriffin65 0:77e050d1fb12 261 #endif