WIZnetInterface using namespace

Dependents:   DualNetworkInterface-Basic

Fork of WIZnetInterface by WIZnet

Committer:
embeddist
Date:
Mon Jun 15 12:55:19 2015 +0000
Revision:
3:f8c6efc8bf83
Parent:
0:6f28332c466f
Child:
4:4930f81bbe98
on debugging: fixed some bugs

Who changed what in which revision?

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