WIZnet WIZ550io (w5500) support

Dependents:   HTTPClient_HelloWorld_WIZ550io NTPClient_HelloWorld_WIZ550io

Fork of WIZ820ioInterface by ban4jp -

Committer:
ban4jp
Date:
Mon Dec 23 13:51:35 2013 +0000
Revision:
9:615198a7b82b
Parent:
7:93e358253dd8
Use on-board MAC Address.; Fixed compatibility.

Who changed what in which revision?

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