no upgrade or change at this. move to new Library for WIZ550io, W5500 -> http://mbed.org/teams/EthernetInterfaceW5500-makers/code/W5500Interface/

Dependents:   LPC11U68_NTPClient_HelloWorld_WIZ550io

Fork of WIZ550ioInterface by ban4jp -

please get the new Library for WIZ550io, W5500 (WIZnet) http://mbed.org/teams/EthernetInterfaceW5500-makers/code/W5500Interface/

Import libraryW5500Interface

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.

Committer:
Bongjun
Date:
Tue Jul 08 01:04:09 2014 +0000
Revision:
10:4da8370d14da
Parent:
9:615198a7b82b
Child:
11:5a5a3f373a6b
added manual init

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 /*
Bongjun 10:4da8370d14da 109 * Initialize the WIZ820io module with static values.
Bongjun 10:4da8370d14da 110 *
Bongjun 10:4da8370d14da 111 * @return true if connected, false otherwise
Bongjun 10:4da8370d14da 112 */
Bongjun 10:4da8370d14da 113 bool join(uint32_t srcip, uint32_t gw, uint32_t subnetmask);
Bongjun 10:4da8370d14da 114
Bongjun 10:4da8370d14da 115 /*
va009039 5:fb15c35d1e28 116 * Disconnect the WIZ820io module from the access point
samux 1:fb4494783863 117 *
samux 1:fb4494783863 118 * @ returns true if successful
samux 1:fb4494783863 119 */
samux 1:fb4494783863 120 bool disconnect();
samux 1:fb4494783863 121
samux 1:fb4494783863 122 /*
samux 1:fb4494783863 123 * Open a tcp connection with the specified host on the specified port
samux 1:fb4494783863 124 *
samux 1:fb4494783863 125 * @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 126 * @param port port
samux 1:fb4494783863 127 * @ returns true if successful
samux 1:fb4494783863 128 */
va009039 5:fb15c35d1e28 129 bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000);
samux 1:fb4494783863 130
samux 1:fb4494783863 131 /*
samux 1:fb4494783863 132 * Set the protocol (UDP or TCP)
samux 1:fb4494783863 133 *
samux 1:fb4494783863 134 * @param p protocol
samux 1:fb4494783863 135 * @ returns true if successful
samux 1:fb4494783863 136 */
va009039 5:fb15c35d1e28 137 bool setProtocol(int socket, Protocol p);
samux 1:fb4494783863 138
samux 1:fb4494783863 139 /*
va009039 5:fb15c35d1e28 140 * Reset the WIZ820io module
samux 1:fb4494783863 141 */
samux 1:fb4494783863 142 void reset();
samux 3:9aa05e19c62e 143
va009039 5:fb15c35d1e28 144
va009039 5:fb15c35d1e28 145 int wait_readable(int socket, int wait_time_ms, int req_size = 0);
samux 1:fb4494783863 146
va009039 5:fb15c35d1e28 147 int wait_writeable(int socket, int wait_time_ms, int req_size = 0);
samux 1:fb4494783863 148
samux 1:fb4494783863 149 /*
samux 1:fb4494783863 150 * Check if a tcp link is active
samux 1:fb4494783863 151 *
samux 1:fb4494783863 152 * @returns true if successful
samux 1:fb4494783863 153 */
va009039 5:fb15c35d1e28 154 bool is_connected(int socket);
samux 1:fb4494783863 155
samux 1:fb4494783863 156 /*
samux 1:fb4494783863 157 * Close a tcp connection
samux 1:fb4494783863 158 *
samux 1:fb4494783863 159 * @ returns true if successful
samux 1:fb4494783863 160 */
va009039 5:fb15c35d1e28 161 bool close(int socket);
samux 1:fb4494783863 162
samux 1:fb4494783863 163 /*
samux 1:fb4494783863 164 * @param str string to be sent
samux 1:fb4494783863 165 * @param len string length
samux 1:fb4494783863 166 */
va009039 5:fb15c35d1e28 167 int send(int socket, const char * str, int len);
samux 1:fb4494783863 168
va009039 5:fb15c35d1e28 169 int recv(int socket, char* buf, int len);
va009039 5:fb15c35d1e28 170
samux 4:0bcec6272784 171 /*
samux 4:0bcec6272784 172 * Return true if the module is using dhcp
samux 4:0bcec6272784 173 *
samux 4:0bcec6272784 174 * @returns true if the module is using dhcp
samux 4:0bcec6272784 175 */
samux 4:0bcec6272784 176 bool isDHCP() {
va009039 5:fb15c35d1e28 177 return dhcp;
samux 4:0bcec6272784 178 }
samux 1:fb4494783863 179
va009039 5:fb15c35d1e28 180 bool gethostbyname(const char* host, uint32_t* ip);
samux 1:fb4494783863 181
va009039 5:fb15c35d1e28 182 static WIZ820io * getInstance() {
samux 1:fb4494783863 183 return inst;
samux 1:fb4494783863 184 };
samux 1:fb4494783863 185
va009039 5:fb15c35d1e28 186 int new_socket();
va009039 5:fb15c35d1e28 187 uint16_t new_port();
va009039 5:fb15c35d1e28 188 void scmd(int socket, Command cmd);
va009039 5:fb15c35d1e28 189
va009039 5:fb15c35d1e28 190 template<typename T>
va009039 5:fb15c35d1e28 191 void sreg(int socket, uint16_t addr, T data) {
ban4jp 7:93e358253dd8 192 reg_wr<T>(addr, (0x0C + (socket << 5)), data);
va009039 5:fb15c35d1e28 193 }
va009039 5:fb15c35d1e28 194
va009039 5:fb15c35d1e28 195 template<typename T>
va009039 5:fb15c35d1e28 196 T sreg(int socket, uint16_t addr) {
ban4jp 7:93e358253dd8 197 return reg_rd<T>(addr, (0x08 + (socket << 5)));
va009039 5:fb15c35d1e28 198 }
samux 1:fb4494783863 199
va009039 5:fb15c35d1e28 200 template<typename T>
va009039 5:fb15c35d1e28 201 void reg_wr(uint16_t addr, T data) {
ban4jp 7:93e358253dd8 202 return reg_wr(addr, 0x04, data);
ban4jp 7:93e358253dd8 203 }
ban4jp 7:93e358253dd8 204
ban4jp 7:93e358253dd8 205 template<typename T>
ban4jp 7:93e358253dd8 206 void reg_wr(uint16_t addr, uint8_t cb, T data) {
va009039 5:fb15c35d1e28 207 uint8_t buf[sizeof(T)];
va009039 5:fb15c35d1e28 208 *reinterpret_cast<T*>(buf) = data;
va009039 5:fb15c35d1e28 209 for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian
va009039 5:fb15c35d1e28 210 uint8_t t = buf[i];
va009039 5:fb15c35d1e28 211 buf[i] = buf[sizeof(buf)-1-i];
va009039 5:fb15c35d1e28 212 buf[sizeof(buf)-1-i] = t;
va009039 5:fb15c35d1e28 213 }
ban4jp 7:93e358253dd8 214 spi_write(addr, cb, buf, sizeof(buf));
va009039 5:fb15c35d1e28 215 }
va009039 5:fb15c35d1e28 216
va009039 5:fb15c35d1e28 217 template<typename T>
va009039 5:fb15c35d1e28 218 T reg_rd(uint16_t addr) {
ban4jp 7:93e358253dd8 219 return reg_rd<T>(addr, 0x00);
ban4jp 7:93e358253dd8 220 }
ban4jp 7:93e358253dd8 221
ban4jp 7:93e358253dd8 222 template<typename T>
ban4jp 7:93e358253dd8 223 T reg_rd(uint16_t addr, uint8_t cb) {
va009039 5:fb15c35d1e28 224 uint8_t buf[sizeof(T)];
ban4jp 7:93e358253dd8 225 spi_read(addr, cb, buf, sizeof(buf));
va009039 5:fb15c35d1e28 226 for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian
va009039 5:fb15c35d1e28 227 uint8_t t = buf[i];
va009039 5:fb15c35d1e28 228 buf[i] = buf[sizeof(buf)-1-i];
va009039 5:fb15c35d1e28 229 buf[sizeof(buf)-1-i] = t;
va009039 5:fb15c35d1e28 230 }
va009039 5:fb15c35d1e28 231 return *reinterpret_cast<T*>(buf);
va009039 5:fb15c35d1e28 232 }
samux 1:fb4494783863 233
va009039 5:fb15c35d1e28 234 void reg_rd_mac(uint16_t addr, uint8_t* data) {
ban4jp 7:93e358253dd8 235 spi_read(addr, 0x00, data, 6);
va009039 5:fb15c35d1e28 236 }
samux 1:fb4494783863 237
ban4jp 7:93e358253dd8 238 void reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip) {
va009039 5:fb15c35d1e28 239 uint8_t buf[4];
va009039 5:fb15c35d1e28 240 char* p = (char*)ip;
va009039 5:fb15c35d1e28 241 for(int i = 0; i < 4; i++) {
va009039 5:fb15c35d1e28 242 buf[i] = atoi(p);
va009039 5:fb15c35d1e28 243 p = strchr(p, '.');
va009039 5:fb15c35d1e28 244 if (p == NULL) {
va009039 5:fb15c35d1e28 245 break;
va009039 5:fb15c35d1e28 246 }
va009039 5:fb15c35d1e28 247 p++;
va009039 5:fb15c35d1e28 248 }
ban4jp 7:93e358253dd8 249 spi_write(addr, cb, buf, sizeof(buf));
va009039 5:fb15c35d1e28 250 }
samux 1:fb4494783863 251
va009039 5:fb15c35d1e28 252 void sreg_ip(int socket, uint16_t addr, const char* ip) {
ban4jp 7:93e358253dd8 253 reg_wr_ip(addr, (0x0C + (socket << 5)), ip);
va009039 5:fb15c35d1e28 254 }
va009039 5:fb15c35d1e28 255
va009039 5:fb15c35d1e28 256 protected:
va009039 5:fb15c35d1e28 257 uint32_t ip;
va009039 5:fb15c35d1e28 258 uint32_t netmask;
va009039 5:fb15c35d1e28 259 uint32_t gateway;
va009039 5:fb15c35d1e28 260 uint32_t dnsaddr;
va009039 5:fb15c35d1e28 261 bool dhcp;
samux 1:fb4494783863 262
va009039 5:fb15c35d1e28 263 static WIZ820io* inst;
va009039 5:fb15c35d1e28 264
va009039 5:fb15c35d1e28 265 void reg_wr_mac(uint16_t addr, uint8_t* data) {
ban4jp 7:93e358253dd8 266 spi_write(addr, 0x04, data, 6);
va009039 5:fb15c35d1e28 267 }
va009039 5:fb15c35d1e28 268
ban4jp 7:93e358253dd8 269 void spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len);
ban4jp 7:93e358253dd8 270 void spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len);
va009039 5:fb15c35d1e28 271 SPI* spi;
va009039 5:fb15c35d1e28 272 DigitalOut cs;
va009039 5:fb15c35d1e28 273 DigitalOut reset_pin;
samux 1:fb4494783863 274 };
samux 1:fb4494783863 275
va009039 5:fb15c35d1e28 276 // WIZ820io.cpp
va009039 5:fb15c35d1e28 277 extern uint32_t str_to_ip(const char* str);
va009039 5:fb15c35d1e28 278 extern void printfBytes(char* str, uint8_t* buf, int len);
va009039 5:fb15c35d1e28 279 extern void printHex(uint8_t* buf, int len);
va009039 5:fb15c35d1e28 280 extern void debug_hex(uint8_t* buf, int len);