WIZ820io(W5200) network interface, EthernetInterface compatible.
Dependents: Seeed_Ethernet_Shield_V2_HelloWorld Seeed_Ethernet_Shield Cayenne-WIZ820ioInterface Seeed_Ethernet_Shield
Fork of WiflyInterface by
WIZ820io/WIZ820io.h@5:fb15c35d1e28, 2013-08-27 (annotated)
- Committer:
- va009039
- Date:
- Tue Aug 27 12:50:11 2013 +0000
- Revision:
- 5:fb15c35d1e28
- Parent:
- Wifly/Wifly.h@4:0bcec6272784
WIZ820ioInterface?first commit
Who changed what in which revision?
User | Revision | Line number | New 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 | |
va009039 | 5:fb15c35d1e28 | 48 | enum Status { |
va009039 | 5:fb15c35d1e28 | 49 | SOCK_CLOSED = 0x00, |
va009039 | 5:fb15c35d1e28 | 50 | SOCK_INIT = 0x13, |
va009039 | 5:fb15c35d1e28 | 51 | SOCK_LISTEN = 0x14, |
va009039 | 5:fb15c35d1e28 | 52 | SOCK_SYNSENT = 0x15, |
va009039 | 5:fb15c35d1e28 | 53 | SOCK_ESTABLISHED = 0x17, |
va009039 | 5:fb15c35d1e28 | 54 | SOCK_CLOSE_WAIT = 0x1c, |
va009039 | 5:fb15c35d1e28 | 55 | SOCK_UDP = 0x22, |
samux | 1:fb4494783863 | 56 | }; |
samux | 1:fb4494783863 | 57 | |
va009039 | 5:fb15c35d1e28 | 58 | #define MAX_SOCK_NUM 8 |
samux | 1:fb4494783863 | 59 | |
va009039 | 5:fb15c35d1e28 | 60 | #define MR 0x0000 |
va009039 | 5:fb15c35d1e28 | 61 | #define GAR 0x0001 |
va009039 | 5:fb15c35d1e28 | 62 | #define SUBR 0x0005 |
va009039 | 5:fb15c35d1e28 | 63 | #define SHAR 0x0009 |
va009039 | 5:fb15c35d1e28 | 64 | #define SIPR 0x000f |
va009039 | 5:fb15c35d1e28 | 65 | #define PHYSTATUS 0x0035 |
va009039 | 5:fb15c35d1e28 | 66 | // socket |
va009039 | 5:fb15c35d1e28 | 67 | #define Sn_MR 0x4000 |
va009039 | 5:fb15c35d1e28 | 68 | #define Sn_CR 0x4001 |
va009039 | 5:fb15c35d1e28 | 69 | #define Sn_SR 0x4003 |
va009039 | 5:fb15c35d1e28 | 70 | #define Sn_PORT 0x4004 |
va009039 | 5:fb15c35d1e28 | 71 | #define Sn_DIPR 0x400c |
va009039 | 5:fb15c35d1e28 | 72 | #define Sn_DPORT 0x4010 |
va009039 | 5:fb15c35d1e28 | 73 | #define Sn_TX_FSR 0x4020 |
va009039 | 5:fb15c35d1e28 | 74 | #define Sn_TX_WR 0x4024 |
va009039 | 5:fb15c35d1e28 | 75 | #define Sn_RX_RSR 0x4026 |
va009039 | 5:fb15c35d1e28 | 76 | #define Sn_RX_RD 0x4028 |
va009039 | 5:fb15c35d1e28 | 77 | |
va009039 | 5:fb15c35d1e28 | 78 | class WIZ820io { |
samux | 1:fb4494783863 | 79 | public: |
samux | 1:fb4494783863 | 80 | /* |
samux | 1:fb4494783863 | 81 | * Constructor |
samux | 1:fb4494783863 | 82 | * |
samux | 1:fb4494783863 | 83 | * @param tx mbed pin to use for tx line of Serial interface |
samux | 1:fb4494783863 | 84 | * @param rx mbed pin to use for rx line of Serial interface |
va009039 | 5:fb15c35d1e28 | 85 | * @param reset reset pin of the WIZ820io module |
samux | 1:fb4494783863 | 86 | */ |
va009039 | 5:fb15c35d1e28 | 87 | WIZ820io(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset); |
va009039 | 5:fb15c35d1e28 | 88 | WIZ820io(SPI* spi, PinName cs, PinName reset); |
samux | 1:fb4494783863 | 89 | |
samux | 1:fb4494783863 | 90 | /* |
va009039 | 5:fb15c35d1e28 | 91 | * Connect the WIZ820io module to the ssid contained in the constructor. |
samux | 1:fb4494783863 | 92 | * |
samux | 1:fb4494783863 | 93 | * @return true if connected, false otherwise |
samux | 1:fb4494783863 | 94 | */ |
samux | 1:fb4494783863 | 95 | bool join(); |
samux | 1:fb4494783863 | 96 | |
samux | 1:fb4494783863 | 97 | /* |
va009039 | 5:fb15c35d1e28 | 98 | * Disconnect the WIZ820io module from the access point |
samux | 1:fb4494783863 | 99 | * |
samux | 1:fb4494783863 | 100 | * @ returns true if successful |
samux | 1:fb4494783863 | 101 | */ |
samux | 1:fb4494783863 | 102 | bool disconnect(); |
samux | 1:fb4494783863 | 103 | |
samux | 1:fb4494783863 | 104 | /* |
samux | 1:fb4494783863 | 105 | * Open a tcp connection with the specified host on the specified port |
samux | 1:fb4494783863 | 106 | * |
samux | 1:fb4494783863 | 107 | * @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 | 108 | * @param port port |
samux | 1:fb4494783863 | 109 | * @ returns true if successful |
samux | 1:fb4494783863 | 110 | */ |
va009039 | 5:fb15c35d1e28 | 111 | bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000); |
samux | 1:fb4494783863 | 112 | |
samux | 1:fb4494783863 | 113 | /* |
samux | 1:fb4494783863 | 114 | * Set the protocol (UDP or TCP) |
samux | 1:fb4494783863 | 115 | * |
samux | 1:fb4494783863 | 116 | * @param p protocol |
samux | 1:fb4494783863 | 117 | * @ returns true if successful |
samux | 1:fb4494783863 | 118 | */ |
va009039 | 5:fb15c35d1e28 | 119 | bool setProtocol(int socket, Protocol p); |
samux | 1:fb4494783863 | 120 | |
samux | 1:fb4494783863 | 121 | /* |
va009039 | 5:fb15c35d1e28 | 122 | * Reset the WIZ820io module |
samux | 1:fb4494783863 | 123 | */ |
samux | 1:fb4494783863 | 124 | void reset(); |
samux | 3:9aa05e19c62e | 125 | |
va009039 | 5:fb15c35d1e28 | 126 | |
va009039 | 5:fb15c35d1e28 | 127 | int wait_readable(int socket, int wait_time_ms, int req_size = 0); |
samux | 1:fb4494783863 | 128 | |
va009039 | 5:fb15c35d1e28 | 129 | int wait_writeable(int socket, int wait_time_ms, int req_size = 0); |
samux | 1:fb4494783863 | 130 | |
samux | 1:fb4494783863 | 131 | /* |
samux | 1:fb4494783863 | 132 | * Check if a tcp link is active |
samux | 1:fb4494783863 | 133 | * |
samux | 1:fb4494783863 | 134 | * @returns true if successful |
samux | 1:fb4494783863 | 135 | */ |
va009039 | 5:fb15c35d1e28 | 136 | bool is_connected(int socket); |
samux | 1:fb4494783863 | 137 | |
samux | 1:fb4494783863 | 138 | /* |
samux | 1:fb4494783863 | 139 | * Close a tcp connection |
samux | 1:fb4494783863 | 140 | * |
samux | 1:fb4494783863 | 141 | * @ returns true if successful |
samux | 1:fb4494783863 | 142 | */ |
va009039 | 5:fb15c35d1e28 | 143 | bool close(int socket); |
samux | 1:fb4494783863 | 144 | |
samux | 1:fb4494783863 | 145 | /* |
samux | 1:fb4494783863 | 146 | * @param str string to be sent |
samux | 1:fb4494783863 | 147 | * @param len string length |
samux | 1:fb4494783863 | 148 | */ |
va009039 | 5:fb15c35d1e28 | 149 | int send(int socket, const char * str, int len); |
samux | 1:fb4494783863 | 150 | |
va009039 | 5:fb15c35d1e28 | 151 | int recv(int socket, char* buf, int len); |
va009039 | 5:fb15c35d1e28 | 152 | |
samux | 4:0bcec6272784 | 153 | /* |
samux | 4:0bcec6272784 | 154 | * Return true if the module is using dhcp |
samux | 4:0bcec6272784 | 155 | * |
samux | 4:0bcec6272784 | 156 | * @returns true if the module is using dhcp |
samux | 4:0bcec6272784 | 157 | */ |
samux | 4:0bcec6272784 | 158 | bool isDHCP() { |
va009039 | 5:fb15c35d1e28 | 159 | return dhcp; |
samux | 4:0bcec6272784 | 160 | } |
samux | 1:fb4494783863 | 161 | |
va009039 | 5:fb15c35d1e28 | 162 | bool gethostbyname(const char* host, uint32_t* ip); |
samux | 1:fb4494783863 | 163 | |
va009039 | 5:fb15c35d1e28 | 164 | static WIZ820io * getInstance() { |
samux | 1:fb4494783863 | 165 | return inst; |
samux | 1:fb4494783863 | 166 | }; |
samux | 1:fb4494783863 | 167 | |
va009039 | 5:fb15c35d1e28 | 168 | int new_socket(); |
va009039 | 5:fb15c35d1e28 | 169 | uint16_t new_port(); |
va009039 | 5:fb15c35d1e28 | 170 | void scmd(int socket, Command cmd); |
va009039 | 5:fb15c35d1e28 | 171 | |
va009039 | 5:fb15c35d1e28 | 172 | template<typename T> |
va009039 | 5:fb15c35d1e28 | 173 | void sreg(int socket, uint16_t addr, T data) { |
va009039 | 5:fb15c35d1e28 | 174 | reg_wr<T>(addr+0x100*socket, data); |
va009039 | 5:fb15c35d1e28 | 175 | } |
va009039 | 5:fb15c35d1e28 | 176 | |
va009039 | 5:fb15c35d1e28 | 177 | template<typename T> |
va009039 | 5:fb15c35d1e28 | 178 | T sreg(int socket, uint16_t addr) { |
va009039 | 5:fb15c35d1e28 | 179 | return reg_rd<T>(addr+0x100*socket); |
va009039 | 5:fb15c35d1e28 | 180 | } |
samux | 1:fb4494783863 | 181 | |
va009039 | 5:fb15c35d1e28 | 182 | template<typename T> |
va009039 | 5:fb15c35d1e28 | 183 | void reg_wr(uint16_t addr, T data) { |
va009039 | 5:fb15c35d1e28 | 184 | uint8_t buf[sizeof(T)]; |
va009039 | 5:fb15c35d1e28 | 185 | *reinterpret_cast<T*>(buf) = data; |
va009039 | 5:fb15c35d1e28 | 186 | for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian |
va009039 | 5:fb15c35d1e28 | 187 | uint8_t t = buf[i]; |
va009039 | 5:fb15c35d1e28 | 188 | buf[i] = buf[sizeof(buf)-1-i]; |
va009039 | 5:fb15c35d1e28 | 189 | buf[sizeof(buf)-1-i] = t; |
va009039 | 5:fb15c35d1e28 | 190 | } |
va009039 | 5:fb15c35d1e28 | 191 | spi_write(addr, buf, sizeof(buf)); |
va009039 | 5:fb15c35d1e28 | 192 | } |
va009039 | 5:fb15c35d1e28 | 193 | |
va009039 | 5:fb15c35d1e28 | 194 | template<typename T> |
va009039 | 5:fb15c35d1e28 | 195 | T reg_rd(uint16_t addr) { |
va009039 | 5:fb15c35d1e28 | 196 | uint8_t buf[sizeof(T)]; |
va009039 | 5:fb15c35d1e28 | 197 | spi_read(addr, buf, sizeof(buf)); |
va009039 | 5:fb15c35d1e28 | 198 | for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian |
va009039 | 5:fb15c35d1e28 | 199 | uint8_t t = buf[i]; |
va009039 | 5:fb15c35d1e28 | 200 | buf[i] = buf[sizeof(buf)-1-i]; |
va009039 | 5:fb15c35d1e28 | 201 | buf[sizeof(buf)-1-i] = t; |
va009039 | 5:fb15c35d1e28 | 202 | } |
va009039 | 5:fb15c35d1e28 | 203 | return *reinterpret_cast<T*>(buf); |
va009039 | 5:fb15c35d1e28 | 204 | } |
samux | 1:fb4494783863 | 205 | |
va009039 | 5:fb15c35d1e28 | 206 | void reg_rd_mac(uint16_t addr, uint8_t* data) { |
va009039 | 5:fb15c35d1e28 | 207 | spi_read(addr, data, 6); |
va009039 | 5:fb15c35d1e28 | 208 | } |
samux | 1:fb4494783863 | 209 | |
va009039 | 5:fb15c35d1e28 | 210 | void reg_wr_ip(uint16_t addr, const char* ip) { |
va009039 | 5:fb15c35d1e28 | 211 | uint8_t buf[4]; |
va009039 | 5:fb15c35d1e28 | 212 | char* p = (char*)ip; |
va009039 | 5:fb15c35d1e28 | 213 | for(int i = 0; i < 4; i++) { |
va009039 | 5:fb15c35d1e28 | 214 | buf[i] = atoi(p); |
va009039 | 5:fb15c35d1e28 | 215 | p = strchr(p, '.'); |
va009039 | 5:fb15c35d1e28 | 216 | if (p == NULL) { |
va009039 | 5:fb15c35d1e28 | 217 | break; |
va009039 | 5:fb15c35d1e28 | 218 | } |
va009039 | 5:fb15c35d1e28 | 219 | p++; |
va009039 | 5:fb15c35d1e28 | 220 | } |
va009039 | 5:fb15c35d1e28 | 221 | spi_write(addr, buf, sizeof(buf)); |
va009039 | 5:fb15c35d1e28 | 222 | } |
samux | 1:fb4494783863 | 223 | |
va009039 | 5:fb15c35d1e28 | 224 | void sreg_ip(int socket, uint16_t addr, const char* ip) { |
va009039 | 5:fb15c35d1e28 | 225 | reg_wr_ip(addr+0x100*socket, ip); |
va009039 | 5:fb15c35d1e28 | 226 | } |
va009039 | 5:fb15c35d1e28 | 227 | |
va009039 | 5:fb15c35d1e28 | 228 | protected: |
va009039 | 5:fb15c35d1e28 | 229 | uint32_t ip; |
va009039 | 5:fb15c35d1e28 | 230 | uint32_t netmask; |
va009039 | 5:fb15c35d1e28 | 231 | uint32_t gateway; |
va009039 | 5:fb15c35d1e28 | 232 | uint32_t dnsaddr; |
va009039 | 5:fb15c35d1e28 | 233 | bool dhcp; |
samux | 1:fb4494783863 | 234 | |
va009039 | 5:fb15c35d1e28 | 235 | static WIZ820io* inst; |
va009039 | 5:fb15c35d1e28 | 236 | |
va009039 | 5:fb15c35d1e28 | 237 | void reg_wr_mac(uint16_t addr, uint8_t* data) { |
va009039 | 5:fb15c35d1e28 | 238 | spi_write(addr, data, 6); |
va009039 | 5:fb15c35d1e28 | 239 | } |
va009039 | 5:fb15c35d1e28 | 240 | |
va009039 | 5:fb15c35d1e28 | 241 | void spi_write(uint16_t addr, const uint8_t *buf, uint16_t len); |
va009039 | 5:fb15c35d1e28 | 242 | void spi_read(uint16_t addr, uint8_t *buf, uint16_t len); |
va009039 | 5:fb15c35d1e28 | 243 | SPI* spi; |
va009039 | 5:fb15c35d1e28 | 244 | DigitalOut cs; |
va009039 | 5:fb15c35d1e28 | 245 | DigitalOut reset_pin; |
samux | 1:fb4494783863 | 246 | }; |
samux | 1:fb4494783863 | 247 | |
va009039 | 5:fb15c35d1e28 | 248 | // WIZ820io.cpp |
va009039 | 5:fb15c35d1e28 | 249 | extern uint32_t str_to_ip(const char* str); |
va009039 | 5:fb15c35d1e28 | 250 | extern void printfBytes(char* str, uint8_t* buf, int len); |
va009039 | 5:fb15c35d1e28 | 251 | extern void printHex(uint8_t* buf, int len); |
va009039 | 5:fb15c35d1e28 | 252 | extern void debug_hex(uint8_t* buf, int len); |