This is the Interface library for WIZnet W5500 chip which forked of EthernetInterfaceW5500, WIZnetInterface and WIZ550ioInterface. This library has simple name as "W5500Interface". and can be used for Wiz550io users also.

Dependents:   EvrythngApi Websocket_Ethernet_HelloWorld_W5500 Websocket_Ethernet_W5500 CurrentWeatherData_W5500 ... more

Information

It has EthernetInterface class like official EthernetInterface , but uses Wiznet chip driver codes.

So this library can use only the WIZnet W5500 or WIZ550io users.

This library has referred to many project such as WIZ550ioInterface, WiflyInterface and WIZnet Library.

Thanks all.

Committer:
Bongjun
Date:
Wed Aug 20 00:28:37 2014 +0000
Revision:
0:e11e8793c3ce
Child:
5:8aefaef88f79
first release.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Bongjun 0:e11e8793c3ce 1
Bongjun 0:e11e8793c3ce 2 #pragma once
Bongjun 0:e11e8793c3ce 3
Bongjun 0:e11e8793c3ce 4 #include "mbed.h"
Bongjun 0:e11e8793c3ce 5 #include "mbed_debug.h"
Bongjun 0:e11e8793c3ce 6
Bongjun 0:e11e8793c3ce 7 #define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
Bongjun 0:e11e8793c3ce 8
Bongjun 0:e11e8793c3ce 9 #define DEFAULT_WAIT_RESP_TIMEOUT 500
Bongjun 0:e11e8793c3ce 10
Bongjun 0:e11e8793c3ce 11 enum Protocol {
Bongjun 0:e11e8793c3ce 12 CLOSED = 0,
Bongjun 0:e11e8793c3ce 13 TCP = 1,
Bongjun 0:e11e8793c3ce 14 UDP = 2,
Bongjun 0:e11e8793c3ce 15 };
Bongjun 0:e11e8793c3ce 16
Bongjun 0:e11e8793c3ce 17 enum Command {
Bongjun 0:e11e8793c3ce 18 OPEN = 0x01,
Bongjun 0:e11e8793c3ce 19 LISTEN = 0x02,
Bongjun 0:e11e8793c3ce 20 CONNECT = 0x04,
Bongjun 0:e11e8793c3ce 21 DISCON = 0x08,
Bongjun 0:e11e8793c3ce 22 CLOSE = 0x10,
Bongjun 0:e11e8793c3ce 23 SEND = 0x20,
Bongjun 0:e11e8793c3ce 24 SEND_MAC = 0x21,
Bongjun 0:e11e8793c3ce 25 SEND_KEEP = 0x22,
Bongjun 0:e11e8793c3ce 26 RECV = 0x40,
Bongjun 0:e11e8793c3ce 27
Bongjun 0:e11e8793c3ce 28 };
Bongjun 0:e11e8793c3ce 29
Bongjun 0:e11e8793c3ce 30 enum Interrupt {
Bongjun 0:e11e8793c3ce 31 INT_CON = 0x01,
Bongjun 0:e11e8793c3ce 32 INT_DISCON = 0x02,
Bongjun 0:e11e8793c3ce 33 INT_RECV = 0x04,
Bongjun 0:e11e8793c3ce 34 INT_TIMEOUT = 0x08,
Bongjun 0:e11e8793c3ce 35 INT_SEND_OK = 0x10,
Bongjun 0:e11e8793c3ce 36 };
Bongjun 0:e11e8793c3ce 37
Bongjun 0:e11e8793c3ce 38 enum Status {
Bongjun 0:e11e8793c3ce 39 SOCK_CLOSED = 0x00,
Bongjun 0:e11e8793c3ce 40 SOCK_INIT = 0x13,
Bongjun 0:e11e8793c3ce 41 SOCK_LISTEN = 0x14,
Bongjun 0:e11e8793c3ce 42 SOCK_SYNSENT = 0x15,
Bongjun 0:e11e8793c3ce 43 SOCK_ESTABLISHED = 0x17,
Bongjun 0:e11e8793c3ce 44 SOCK_CLOSE_WAIT = 0x1c,
Bongjun 0:e11e8793c3ce 45 SOCK_UDP = 0x22,
Bongjun 0:e11e8793c3ce 46 };
Bongjun 0:e11e8793c3ce 47
Bongjun 0:e11e8793c3ce 48 #define MAX_SOCK_NUM 8
Bongjun 0:e11e8793c3ce 49
Bongjun 0:e11e8793c3ce 50 #define MR 0x0000
Bongjun 0:e11e8793c3ce 51 #define GAR 0x0001
Bongjun 0:e11e8793c3ce 52 #define SUBR 0x0005
Bongjun 0:e11e8793c3ce 53 #define SHAR 0x0009
Bongjun 0:e11e8793c3ce 54 #define SIPR 0x000f
Bongjun 0:e11e8793c3ce 55 #define PHYSTATUS 0x0035
Bongjun 0:e11e8793c3ce 56
Bongjun 0:e11e8793c3ce 57 // W5500 socket register
Bongjun 0:e11e8793c3ce 58 #define Sn_MR 0x0000
Bongjun 0:e11e8793c3ce 59 #define Sn_CR 0x0001
Bongjun 0:e11e8793c3ce 60 #define Sn_IR 0x0002
Bongjun 0:e11e8793c3ce 61 #define Sn_SR 0x0003
Bongjun 0:e11e8793c3ce 62 #define Sn_PORT 0x0004
Bongjun 0:e11e8793c3ce 63 #define Sn_DIPR 0x000c
Bongjun 0:e11e8793c3ce 64 #define Sn_DPORT 0x0010
Bongjun 0:e11e8793c3ce 65 #define Sn_RXBUF_SIZE 0x001e
Bongjun 0:e11e8793c3ce 66 #define Sn_TXBUF_SIZE 0x001f
Bongjun 0:e11e8793c3ce 67 #define Sn_TX_FSR 0x0020
Bongjun 0:e11e8793c3ce 68 #define Sn_TX_WR 0x0024
Bongjun 0:e11e8793c3ce 69 #define Sn_RX_RSR 0x0026
Bongjun 0:e11e8793c3ce 70 #define Sn_RX_RD 0x0028
Bongjun 0:e11e8793c3ce 71
Bongjun 0:e11e8793c3ce 72 class WIZnet_Chip {
Bongjun 0:e11e8793c3ce 73 public:
Bongjun 0:e11e8793c3ce 74 /*
Bongjun 0:e11e8793c3ce 75 * Constructor
Bongjun 0:e11e8793c3ce 76 *
Bongjun 0:e11e8793c3ce 77 * @param spi spi class
Bongjun 0:e11e8793c3ce 78 * @param cs cs of the W5500
Bongjun 0:e11e8793c3ce 79 * @param reset reset pin of the W5500
Bongjun 0:e11e8793c3ce 80 */
Bongjun 0:e11e8793c3ce 81 WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
Bongjun 0:e11e8793c3ce 82 WIZnet_Chip(SPI* spi, PinName cs, PinName reset);
Bongjun 0:e11e8793c3ce 83
Bongjun 0:e11e8793c3ce 84 /*
Bongjun 0:e11e8793c3ce 85 * Set MAC Address to W5500
Bongjun 0:e11e8793c3ce 86 *
Bongjun 0:e11e8793c3ce 87 * @return true if connected, false otherwise
Bongjun 0:e11e8793c3ce 88 */
Bongjun 0:e11e8793c3ce 89 bool setmac();
Bongjun 0:e11e8793c3ce 90
Bongjun 0:e11e8793c3ce 91 /*
Bongjun 0:e11e8793c3ce 92 * Set Network Informations (SrcIP, Netmask, Gataway)
Bongjun 0:e11e8793c3ce 93 *
Bongjun 0:e11e8793c3ce 94 * @return true if connected, false otherwise
Bongjun 0:e11e8793c3ce 95 */
Bongjun 0:e11e8793c3ce 96 bool setip();
Bongjun 0:e11e8793c3ce 97
Bongjun 0:e11e8793c3ce 98 /*
Bongjun 0:e11e8793c3ce 99 * Disconnect the connection
Bongjun 0:e11e8793c3ce 100 *
Bongjun 0:e11e8793c3ce 101 * @ returns true
Bongjun 0:e11e8793c3ce 102 */
Bongjun 0:e11e8793c3ce 103 bool disconnect();
Bongjun 0:e11e8793c3ce 104
Bongjun 0:e11e8793c3ce 105 /*
Bongjun 0:e11e8793c3ce 106 * Open a tcp connection with the specified host on the specified port
Bongjun 0:e11e8793c3ce 107 *
Bongjun 0:e11e8793c3ce 108 * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
Bongjun 0:e11e8793c3ce 109 * @param port port
Bongjun 0:e11e8793c3ce 110 * @ returns true if successful
Bongjun 0:e11e8793c3ce 111 */
Bongjun 0:e11e8793c3ce 112 bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000);
Bongjun 0:e11e8793c3ce 113
Bongjun 0:e11e8793c3ce 114 /*
Bongjun 0:e11e8793c3ce 115 * Set the protocol (UDP or TCP)
Bongjun 0:e11e8793c3ce 116 *
Bongjun 0:e11e8793c3ce 117 * @param p protocol
Bongjun 0:e11e8793c3ce 118 * @ returns true if successful
Bongjun 0:e11e8793c3ce 119 */
Bongjun 0:e11e8793c3ce 120 bool setProtocol(int socket, Protocol p);
Bongjun 0:e11e8793c3ce 121
Bongjun 0:e11e8793c3ce 122 /*
Bongjun 0:e11e8793c3ce 123 * Reset the W5500
Bongjun 0:e11e8793c3ce 124 */
Bongjun 0:e11e8793c3ce 125 void reset();
Bongjun 0:e11e8793c3ce 126
Bongjun 0:e11e8793c3ce 127 int wait_readable(int socket, int wait_time_ms, int req_size = 0);
Bongjun 0:e11e8793c3ce 128
Bongjun 0:e11e8793c3ce 129 int wait_writeable(int socket, int wait_time_ms, int req_size = 0);
Bongjun 0:e11e8793c3ce 130
Bongjun 0:e11e8793c3ce 131 /*
Bongjun 0:e11e8793c3ce 132 * Check if a tcp link is active
Bongjun 0:e11e8793c3ce 133 *
Bongjun 0:e11e8793c3ce 134 * @returns true if successful
Bongjun 0:e11e8793c3ce 135 */
Bongjun 0:e11e8793c3ce 136 bool is_connected(int socket);
Bongjun 0:e11e8793c3ce 137
Bongjun 0:e11e8793c3ce 138 /*
Bongjun 0:e11e8793c3ce 139 * Close a tcp connection
Bongjun 0:e11e8793c3ce 140 *
Bongjun 0:e11e8793c3ce 141 * @ returns true if successful
Bongjun 0:e11e8793c3ce 142 */
Bongjun 0:e11e8793c3ce 143 bool close(int socket);
Bongjun 0:e11e8793c3ce 144
Bongjun 0:e11e8793c3ce 145 /*
Bongjun 0:e11e8793c3ce 146 * @param str string to be sent
Bongjun 0:e11e8793c3ce 147 * @param len string length
Bongjun 0:e11e8793c3ce 148 */
Bongjun 0:e11e8793c3ce 149 int send(int socket, const char * str, int len);
Bongjun 0:e11e8793c3ce 150
Bongjun 0:e11e8793c3ce 151 int recv(int socket, char* buf, int len);
Bongjun 0:e11e8793c3ce 152
Bongjun 0:e11e8793c3ce 153 /*
Bongjun 0:e11e8793c3ce 154 * Return true if the module is using dhcp
Bongjun 0:e11e8793c3ce 155 *
Bongjun 0:e11e8793c3ce 156 * @returns true if the module is using dhcp
Bongjun 0:e11e8793c3ce 157 */
Bongjun 0:e11e8793c3ce 158 bool isDHCP() {
Bongjun 0:e11e8793c3ce 159 return dhcp;
Bongjun 0:e11e8793c3ce 160 }
Bongjun 0:e11e8793c3ce 161
Bongjun 0:e11e8793c3ce 162 bool gethostbyname(const char* host, uint32_t* ip);
Bongjun 0:e11e8793c3ce 163
Bongjun 0:e11e8793c3ce 164 static WIZnet_Chip * getInstance() {
Bongjun 0:e11e8793c3ce 165 return inst;
Bongjun 0:e11e8793c3ce 166 };
Bongjun 0:e11e8793c3ce 167
Bongjun 0:e11e8793c3ce 168 int new_socket();
Bongjun 0:e11e8793c3ce 169 uint16_t new_port();
Bongjun 0:e11e8793c3ce 170 void scmd(int socket, Command cmd);
Bongjun 0:e11e8793c3ce 171
Bongjun 0:e11e8793c3ce 172 template<typename T>
Bongjun 0:e11e8793c3ce 173 void sreg(int socket, uint16_t addr, T data) {
Bongjun 0:e11e8793c3ce 174 reg_wr<T>(addr, (0x0C + (socket << 5)), data);
Bongjun 0:e11e8793c3ce 175 }
Bongjun 0:e11e8793c3ce 176
Bongjun 0:e11e8793c3ce 177 template<typename T>
Bongjun 0:e11e8793c3ce 178 T sreg(int socket, uint16_t addr) {
Bongjun 0:e11e8793c3ce 179 return reg_rd<T>(addr, (0x08 + (socket << 5)));
Bongjun 0:e11e8793c3ce 180 }
Bongjun 0:e11e8793c3ce 181
Bongjun 0:e11e8793c3ce 182 template<typename T>
Bongjun 0:e11e8793c3ce 183 void reg_wr(uint16_t addr, T data) {
Bongjun 0:e11e8793c3ce 184 return reg_wr(addr, 0x04, data);
Bongjun 0:e11e8793c3ce 185 }
Bongjun 0:e11e8793c3ce 186
Bongjun 0:e11e8793c3ce 187 template<typename T>
Bongjun 0:e11e8793c3ce 188 void reg_wr(uint16_t addr, uint8_t cb, T data) {
Bongjun 0:e11e8793c3ce 189 uint8_t buf[sizeof(T)];
Bongjun 0:e11e8793c3ce 190 *reinterpret_cast<T*>(buf) = data;
Bongjun 0:e11e8793c3ce 191 for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian
Bongjun 0:e11e8793c3ce 192 uint8_t t = buf[i];
Bongjun 0:e11e8793c3ce 193 buf[i] = buf[sizeof(buf)-1-i];
Bongjun 0:e11e8793c3ce 194 buf[sizeof(buf)-1-i] = t;
Bongjun 0:e11e8793c3ce 195 }
Bongjun 0:e11e8793c3ce 196 spi_write(addr, cb, buf, sizeof(buf));
Bongjun 0:e11e8793c3ce 197 }
Bongjun 0:e11e8793c3ce 198
Bongjun 0:e11e8793c3ce 199 template<typename T>
Bongjun 0:e11e8793c3ce 200 T reg_rd(uint16_t addr) {
Bongjun 0:e11e8793c3ce 201 return reg_rd<T>(addr, 0x00);
Bongjun 0:e11e8793c3ce 202 }
Bongjun 0:e11e8793c3ce 203
Bongjun 0:e11e8793c3ce 204 template<typename T>
Bongjun 0:e11e8793c3ce 205 T reg_rd(uint16_t addr, uint8_t cb) {
Bongjun 0:e11e8793c3ce 206 uint8_t buf[sizeof(T)];
Bongjun 0:e11e8793c3ce 207 spi_read(addr, cb, buf, sizeof(buf));
Bongjun 0:e11e8793c3ce 208 for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian
Bongjun 0:e11e8793c3ce 209 uint8_t t = buf[i];
Bongjun 0:e11e8793c3ce 210 buf[i] = buf[sizeof(buf)-1-i];
Bongjun 0:e11e8793c3ce 211 buf[sizeof(buf)-1-i] = t;
Bongjun 0:e11e8793c3ce 212 }
Bongjun 0:e11e8793c3ce 213 return *reinterpret_cast<T*>(buf);
Bongjun 0:e11e8793c3ce 214 }
Bongjun 0:e11e8793c3ce 215
Bongjun 0:e11e8793c3ce 216 void reg_rd_mac(uint16_t addr, uint8_t* data) {
Bongjun 0:e11e8793c3ce 217 spi_read(addr, 0x00, data, 6);
Bongjun 0:e11e8793c3ce 218 }
Bongjun 0:e11e8793c3ce 219
Bongjun 0:e11e8793c3ce 220 void reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip) {
Bongjun 0:e11e8793c3ce 221 uint8_t buf[4];
Bongjun 0:e11e8793c3ce 222 char* p = (char*)ip;
Bongjun 0:e11e8793c3ce 223 for(int i = 0; i < 4; i++) {
Bongjun 0:e11e8793c3ce 224 buf[i] = atoi(p);
Bongjun 0:e11e8793c3ce 225 p = strchr(p, '.');
Bongjun 0:e11e8793c3ce 226 if (p == NULL) {
Bongjun 0:e11e8793c3ce 227 break;
Bongjun 0:e11e8793c3ce 228 }
Bongjun 0:e11e8793c3ce 229 p++;
Bongjun 0:e11e8793c3ce 230 }
Bongjun 0:e11e8793c3ce 231 spi_write(addr, cb, buf, sizeof(buf));
Bongjun 0:e11e8793c3ce 232 }
Bongjun 0:e11e8793c3ce 233
Bongjun 0:e11e8793c3ce 234 void sreg_ip(int socket, uint16_t addr, const char* ip) {
Bongjun 0:e11e8793c3ce 235 reg_wr_ip(addr, (0x0C + (socket << 5)), ip);
Bongjun 0:e11e8793c3ce 236 }
Bongjun 0:e11e8793c3ce 237
Bongjun 0:e11e8793c3ce 238 protected:
Bongjun 0:e11e8793c3ce 239 uint8_t mac[6];
Bongjun 0:e11e8793c3ce 240 uint32_t ip;
Bongjun 0:e11e8793c3ce 241 uint32_t netmask;
Bongjun 0:e11e8793c3ce 242 uint32_t gateway;
Bongjun 0:e11e8793c3ce 243 uint32_t dnsaddr;
Bongjun 0:e11e8793c3ce 244 bool dhcp;
Bongjun 0:e11e8793c3ce 245
Bongjun 0:e11e8793c3ce 246 static WIZnet_Chip* inst;
Bongjun 0:e11e8793c3ce 247
Bongjun 0:e11e8793c3ce 248 void reg_wr_mac(uint16_t addr, uint8_t* data) {
Bongjun 0:e11e8793c3ce 249 spi_write(addr, 0x04, data, 6);
Bongjun 0:e11e8793c3ce 250 }
Bongjun 0:e11e8793c3ce 251
Bongjun 0:e11e8793c3ce 252 void spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len);
Bongjun 0:e11e8793c3ce 253 void spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len);
Bongjun 0:e11e8793c3ce 254 SPI* spi;
Bongjun 0:e11e8793c3ce 255 DigitalOut cs;
Bongjun 0:e11e8793c3ce 256 DigitalOut reset_pin;
Bongjun 0:e11e8793c3ce 257 };
Bongjun 0:e11e8793c3ce 258
Bongjun 0:e11e8793c3ce 259 extern uint32_t str_to_ip(const char* str);
Bongjun 0:e11e8793c3ce 260 extern void printfBytes(char* str, uint8_t* buf, int len);
Bongjun 0:e11e8793c3ce 261 extern void printHex(uint8_t* buf, int len);
Bongjun 0:e11e8793c3ce 262 extern void debug_hex(uint8_t* buf, int len);