This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500, W5200 and W5100. One of them can be selected by enabling it in wiznet.h.

Dependencies:   mbed

Fork of WIZnet_Library by WIZnet

Committer:
jbkim
Date:
Fri May 09 01:17:51 2014 +0000
Revision:
1:8138a268fbd2
Parent:
0:b72d22e10709
Wiznet Ethernet library 1st release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jbkim 0:b72d22e10709 1 /* Copyright (C) 2012 mbed.org, MIT License
jbkim 0:b72d22e10709 2 *
jbkim 0:b72d22e10709 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
jbkim 0:b72d22e10709 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
jbkim 0:b72d22e10709 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
jbkim 0:b72d22e10709 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
jbkim 0:b72d22e10709 7 * furnished to do so, subject to the following conditions:
jbkim 0:b72d22e10709 8 *
jbkim 0:b72d22e10709 9 * The above copyright notice and this permission notice shall be included in all copies or
jbkim 0:b72d22e10709 10 * substantial portions of the Software.
jbkim 0:b72d22e10709 11 *
jbkim 0:b72d22e10709 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
jbkim 0:b72d22e10709 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
jbkim 0:b72d22e10709 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
jbkim 0:b72d22e10709 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jbkim 0:b72d22e10709 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
jbkim 0:b72d22e10709 17 *
jbkim 0:b72d22e10709 18 */
jbkim 0:b72d22e10709 19
jbkim 0:b72d22e10709 20 #pragma once
jbkim 0:b72d22e10709 21
jbkim 0:b72d22e10709 22 #include "mbed.h"
jbkim 0:b72d22e10709 23 #include "mbed_debug.h"
jbkim 0:b72d22e10709 24
jbkim 0:b72d22e10709 25 #define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
jbkim 0:b72d22e10709 26
jbkim 0:b72d22e10709 27 #define DEFAULT_WAIT_RESP_TIMEOUT 500
jbkim 0:b72d22e10709 28
jbkim 0:b72d22e10709 29 enum Protocol {
jbkim 0:b72d22e10709 30 CLOSED = 0,
jbkim 0:b72d22e10709 31 TCP = 1,
jbkim 0:b72d22e10709 32 UDP = 2,
jbkim 0:b72d22e10709 33 };
jbkim 0:b72d22e10709 34
jbkim 0:b72d22e10709 35 enum Command {
jbkim 0:b72d22e10709 36 OPEN = 0x01,
jbkim 0:b72d22e10709 37 LISTEN = 0x02,
jbkim 0:b72d22e10709 38 CONNECT = 0x04,
jbkim 0:b72d22e10709 39 DISCON = 0x08,
jbkim 0:b72d22e10709 40 CLOSE = 0x10,
jbkim 0:b72d22e10709 41 SEND = 0x20,
jbkim 0:b72d22e10709 42 SEND_MAC = 0x21,
jbkim 0:b72d22e10709 43 SEND_KEEP = 0x22,
jbkim 0:b72d22e10709 44 RECV = 0x40,
jbkim 0:b72d22e10709 45
jbkim 0:b72d22e10709 46 };
jbkim 0:b72d22e10709 47
jbkim 0:b72d22e10709 48 enum Interrupt {
jbkim 0:b72d22e10709 49 INT_CON = 0x01,
jbkim 0:b72d22e10709 50 INT_DISCON = 0x02,
jbkim 0:b72d22e10709 51 INT_RECV = 0x04,
jbkim 0:b72d22e10709 52 INT_TIMEOUT = 0x08,
jbkim 0:b72d22e10709 53 INT_SEND_OK = 0x10,
jbkim 0:b72d22e10709 54 };
jbkim 0:b72d22e10709 55
jbkim 0:b72d22e10709 56 enum Status {
jbkim 0:b72d22e10709 57 SOCK_CLOSED = 0x00,
jbkim 0:b72d22e10709 58 SOCK_INIT = 0x13,
jbkim 0:b72d22e10709 59 SOCK_LISTEN = 0x14,
jbkim 0:b72d22e10709 60 SOCK_SYNSENT = 0x15,
jbkim 0:b72d22e10709 61 SOCK_ESTABLISHED = 0x17,
jbkim 0:b72d22e10709 62 SOCK_CLOSE_WAIT = 0x1c,
jbkim 0:b72d22e10709 63 SOCK_UDP = 0x22,
jbkim 0:b72d22e10709 64 };
jbkim 0:b72d22e10709 65
jbkim 0:b72d22e10709 66 #define MAX_SOCK_NUM 8
jbkim 0:b72d22e10709 67
jbkim 0:b72d22e10709 68 #define MR 0x0000
jbkim 0:b72d22e10709 69 #define GAR 0x0001
jbkim 0:b72d22e10709 70 #define SUBR 0x0005
jbkim 0:b72d22e10709 71 #define SHAR 0x0009
jbkim 0:b72d22e10709 72 #define SIPR 0x000f
jbkim 0:b72d22e10709 73 #define PHYSTATUS 0x0035
jbkim 0:b72d22e10709 74
jbkim 0:b72d22e10709 75 // W5500 socket register
jbkim 0:b72d22e10709 76 #define Sn_MR 0x0000
jbkim 0:b72d22e10709 77 #define Sn_CR 0x0001
jbkim 0:b72d22e10709 78 #define Sn_IR 0x0002
jbkim 0:b72d22e10709 79 #define Sn_SR 0x0003
jbkim 0:b72d22e10709 80 #define Sn_PORT 0x0004
jbkim 0:b72d22e10709 81 #define Sn_DIPR 0x000c
jbkim 0:b72d22e10709 82 #define Sn_DPORT 0x0010
jbkim 0:b72d22e10709 83 #define Sn_RXBUF_SIZE 0x001e
jbkim 0:b72d22e10709 84 #define Sn_TXBUF_SIZE 0x001f
jbkim 0:b72d22e10709 85 #define Sn_TX_FSR 0x0020
jbkim 0:b72d22e10709 86 #define Sn_TX_WR 0x0024
jbkim 0:b72d22e10709 87 #define Sn_RX_RSR 0x0026
jbkim 0:b72d22e10709 88 #define Sn_RX_RD 0x0028
jbkim 0:b72d22e10709 89
jbkim 0:b72d22e10709 90 class WIZnet_Chip {
jbkim 0:b72d22e10709 91 public:
jbkim 0:b72d22e10709 92 /*
jbkim 0:b72d22e10709 93 * Constructor
jbkim 0:b72d22e10709 94 *
jbkim 0:b72d22e10709 95 * @param spi spi class
jbkim 0:b72d22e10709 96 * @param cs cs of the W5500
jbkim 0:b72d22e10709 97 * @param reset reset pin of the W5500
jbkim 0:b72d22e10709 98 */
jbkim 0:b72d22e10709 99 WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
jbkim 0:b72d22e10709 100 WIZnet_Chip(SPI* spi, PinName cs, PinName reset);
jbkim 0:b72d22e10709 101
jbkim 0:b72d22e10709 102 /*
jbkim 0:b72d22e10709 103 * Connect the W5500 to the ssid contained in the constructor.
jbkim 0:b72d22e10709 104 *
jbkim 0:b72d22e10709 105 * @return true if connected, false otherwise
jbkim 0:b72d22e10709 106 */
jbkim 0:b72d22e10709 107 bool setip();
jbkim 0:b72d22e10709 108
jbkim 0:b72d22e10709 109 /*
jbkim 0:b72d22e10709 110 * Disconnect the connection
jbkim 0:b72d22e10709 111 *
jbkim 0:b72d22e10709 112 * @ returns true
jbkim 0:b72d22e10709 113 */
jbkim 0:b72d22e10709 114 bool disconnect();
jbkim 0:b72d22e10709 115
jbkim 0:b72d22e10709 116 /*
jbkim 0:b72d22e10709 117 * Open a tcp connection with the specified host on the specified port
jbkim 0:b72d22e10709 118 *
jbkim 0:b72d22e10709 119 * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
jbkim 0:b72d22e10709 120 * @param port port
jbkim 0:b72d22e10709 121 * @ returns true if successful
jbkim 0:b72d22e10709 122 */
jbkim 0:b72d22e10709 123 bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000);
jbkim 0:b72d22e10709 124
jbkim 0:b72d22e10709 125 /*
jbkim 0:b72d22e10709 126 * Set the protocol (UDP or TCP)
jbkim 0:b72d22e10709 127 *
jbkim 0:b72d22e10709 128 * @param p protocol
jbkim 0:b72d22e10709 129 * @ returns true if successful
jbkim 0:b72d22e10709 130 */
jbkim 0:b72d22e10709 131 bool setProtocol(int socket, Protocol p);
jbkim 0:b72d22e10709 132
jbkim 0:b72d22e10709 133 /*
jbkim 0:b72d22e10709 134 * Reset the W5500
jbkim 0:b72d22e10709 135 */
jbkim 0:b72d22e10709 136 void reset();
jbkim 0:b72d22e10709 137
jbkim 0:b72d22e10709 138 int wait_readable(int socket, int wait_time_ms, int req_size = 0);
jbkim 0:b72d22e10709 139
jbkim 0:b72d22e10709 140 int wait_writeable(int socket, int wait_time_ms, int req_size = 0);
jbkim 0:b72d22e10709 141
jbkim 0:b72d22e10709 142 /*
jbkim 0:b72d22e10709 143 * Check if a tcp link is active
jbkim 0:b72d22e10709 144 *
jbkim 0:b72d22e10709 145 * @returns true if successful
jbkim 0:b72d22e10709 146 */
jbkim 0:b72d22e10709 147 bool is_connected(int socket);
jbkim 0:b72d22e10709 148
jbkim 0:b72d22e10709 149 /*
jbkim 0:b72d22e10709 150 * Close a tcp connection
jbkim 0:b72d22e10709 151 *
jbkim 0:b72d22e10709 152 * @ returns true if successful
jbkim 0:b72d22e10709 153 */
jbkim 0:b72d22e10709 154 bool close(int socket);
jbkim 0:b72d22e10709 155
jbkim 0:b72d22e10709 156 /*
jbkim 0:b72d22e10709 157 * @param str string to be sent
jbkim 0:b72d22e10709 158 * @param len string length
jbkim 0:b72d22e10709 159 */
jbkim 0:b72d22e10709 160 int send(int socket, const char * str, int len);
jbkim 0:b72d22e10709 161
jbkim 0:b72d22e10709 162 int recv(int socket, char* buf, int len);
jbkim 0:b72d22e10709 163
jbkim 0:b72d22e10709 164 /*
jbkim 0:b72d22e10709 165 * Return true if the module is using dhcp
jbkim 0:b72d22e10709 166 *
jbkim 0:b72d22e10709 167 * @returns true if the module is using dhcp
jbkim 0:b72d22e10709 168 */
jbkim 0:b72d22e10709 169 bool isDHCP() {
jbkim 0:b72d22e10709 170 return dhcp;
jbkim 0:b72d22e10709 171 }
jbkim 0:b72d22e10709 172
jbkim 0:b72d22e10709 173 bool gethostbyname(const char* host, uint32_t* ip);
jbkim 0:b72d22e10709 174
jbkim 0:b72d22e10709 175 static WIZnet_Chip * getInstance() {
jbkim 0:b72d22e10709 176 return inst;
jbkim 0:b72d22e10709 177 };
jbkim 0:b72d22e10709 178
jbkim 0:b72d22e10709 179 int new_socket();
jbkim 0:b72d22e10709 180 uint16_t new_port();
jbkim 0:b72d22e10709 181 void scmd(int socket, Command cmd);
jbkim 0:b72d22e10709 182
jbkim 0:b72d22e10709 183 template<typename T>
jbkim 0:b72d22e10709 184 void sreg(int socket, uint16_t addr, T data) {
jbkim 0:b72d22e10709 185 reg_wr<T>(addr, (0x0C + (socket << 5)), data);
jbkim 0:b72d22e10709 186 }
jbkim 0:b72d22e10709 187
jbkim 0:b72d22e10709 188 template<typename T>
jbkim 0:b72d22e10709 189 T sreg(int socket, uint16_t addr) {
jbkim 0:b72d22e10709 190 return reg_rd<T>(addr, (0x08 + (socket << 5)));
jbkim 0:b72d22e10709 191 }
jbkim 0:b72d22e10709 192
jbkim 0:b72d22e10709 193 template<typename T>
jbkim 0:b72d22e10709 194 void reg_wr(uint16_t addr, T data) {
jbkim 0:b72d22e10709 195 return reg_wr(addr, 0x04, data);
jbkim 0:b72d22e10709 196 }
jbkim 0:b72d22e10709 197
jbkim 0:b72d22e10709 198 template<typename T>
jbkim 0:b72d22e10709 199 void reg_wr(uint16_t addr, uint8_t cb, T data) {
jbkim 0:b72d22e10709 200 uint8_t buf[sizeof(T)];
jbkim 0:b72d22e10709 201 *reinterpret_cast<T*>(buf) = data;
jbkim 0:b72d22e10709 202 for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian
jbkim 0:b72d22e10709 203 uint8_t t = buf[i];
jbkim 0:b72d22e10709 204 buf[i] = buf[sizeof(buf)-1-i];
jbkim 0:b72d22e10709 205 buf[sizeof(buf)-1-i] = t;
jbkim 0:b72d22e10709 206 }
jbkim 0:b72d22e10709 207 spi_write(addr, cb, buf, sizeof(buf));
jbkim 0:b72d22e10709 208 }
jbkim 0:b72d22e10709 209
jbkim 0:b72d22e10709 210 template<typename T>
jbkim 0:b72d22e10709 211 T reg_rd(uint16_t addr) {
jbkim 0:b72d22e10709 212 return reg_rd<T>(addr, 0x00);
jbkim 0:b72d22e10709 213 }
jbkim 0:b72d22e10709 214
jbkim 0:b72d22e10709 215 template<typename T>
jbkim 0:b72d22e10709 216 T reg_rd(uint16_t addr, uint8_t cb) {
jbkim 0:b72d22e10709 217 uint8_t buf[sizeof(T)];
jbkim 0:b72d22e10709 218 spi_read(addr, cb, buf, sizeof(buf));
jbkim 0:b72d22e10709 219 for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian
jbkim 0:b72d22e10709 220 uint8_t t = buf[i];
jbkim 0:b72d22e10709 221 buf[i] = buf[sizeof(buf)-1-i];
jbkim 0:b72d22e10709 222 buf[sizeof(buf)-1-i] = t;
jbkim 0:b72d22e10709 223 }
jbkim 0:b72d22e10709 224 return *reinterpret_cast<T*>(buf);
jbkim 0:b72d22e10709 225 }
jbkim 0:b72d22e10709 226
jbkim 0:b72d22e10709 227 void reg_rd_mac(uint16_t addr, uint8_t* data) {
jbkim 0:b72d22e10709 228 spi_read(addr, 0x00, data, 6);
jbkim 0:b72d22e10709 229 }
jbkim 0:b72d22e10709 230
jbkim 0:b72d22e10709 231 void reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip) {
jbkim 0:b72d22e10709 232 uint8_t buf[4];
jbkim 0:b72d22e10709 233 char* p = (char*)ip;
jbkim 0:b72d22e10709 234 for(int i = 0; i < 4; i++) {
jbkim 0:b72d22e10709 235 buf[i] = atoi(p);
jbkim 0:b72d22e10709 236 p = strchr(p, '.');
jbkim 0:b72d22e10709 237 if (p == NULL) {
jbkim 0:b72d22e10709 238 break;
jbkim 0:b72d22e10709 239 }
jbkim 0:b72d22e10709 240 p++;
jbkim 0:b72d22e10709 241 }
jbkim 0:b72d22e10709 242 spi_write(addr, cb, buf, sizeof(buf));
jbkim 0:b72d22e10709 243 }
jbkim 0:b72d22e10709 244
jbkim 0:b72d22e10709 245 void sreg_ip(int socket, uint16_t addr, const char* ip) {
jbkim 0:b72d22e10709 246 reg_wr_ip(addr, (0x0C + (socket << 5)), ip);
jbkim 0:b72d22e10709 247 }
jbkim 0:b72d22e10709 248
jbkim 0:b72d22e10709 249 protected:
jbkim 1:8138a268fbd2 250 uint8_t mac[6];
jbkim 0:b72d22e10709 251 uint32_t ip;
jbkim 0:b72d22e10709 252 uint32_t netmask;
jbkim 0:b72d22e10709 253 uint32_t gateway;
jbkim 0:b72d22e10709 254 uint32_t dnsaddr;
jbkim 0:b72d22e10709 255 bool dhcp;
jbkim 0:b72d22e10709 256
jbkim 0:b72d22e10709 257 static WIZnet_Chip* inst;
jbkim 0:b72d22e10709 258
jbkim 0:b72d22e10709 259 void reg_wr_mac(uint16_t addr, uint8_t* data) {
jbkim 0:b72d22e10709 260 spi_write(addr, 0x04, data, 6);
jbkim 0:b72d22e10709 261 }
jbkim 0:b72d22e10709 262
jbkim 0:b72d22e10709 263 void spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len);
jbkim 0:b72d22e10709 264 void spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len);
jbkim 0:b72d22e10709 265 SPI* spi;
jbkim 0:b72d22e10709 266 DigitalOut cs;
jbkim 0:b72d22e10709 267 DigitalOut reset_pin;
jbkim 0:b72d22e10709 268 };
jbkim 0:b72d22e10709 269
jbkim 0:b72d22e10709 270 extern uint32_t str_to_ip(const char* str);
jbkim 0:b72d22e10709 271 extern void printfBytes(char* str, uint8_t* buf, int len);
jbkim 0:b72d22e10709 272 extern void printHex(uint8_t* buf, int len);
jbkim 0:b72d22e10709 273 extern void debug_hex(uint8_t* buf, int len);