no chance
Fork of WIZnet_Library by
WIZnetInterface/WIZnet/W5200.h@4:3c94b1ba5411, 2014-10-16 (annotated)
- Committer:
- slawcio
- Date:
- Thu Oct 16 09:35:53 2014 +0000
- Revision:
- 4:3c94b1ba5411
- Parent:
- 1:8138a268fbd2
W5100
Who changed what in which revision?
User | Revision | Line number | New 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 | // W5200 socket |
jbkim | 0:b72d22e10709 | 76 | #define Sn_MR 0x4000 |
jbkim | 0:b72d22e10709 | 77 | #define Sn_CR 0x4001 |
jbkim | 0:b72d22e10709 | 78 | #define Sn_IR 0x4002 |
jbkim | 0:b72d22e10709 | 79 | #define Sn_SR 0x4003 |
jbkim | 0:b72d22e10709 | 80 | #define Sn_PORT 0x4004 |
jbkim | 0:b72d22e10709 | 81 | #define Sn_DIPR 0x400c |
jbkim | 0:b72d22e10709 | 82 | #define Sn_DPORT 0x4010 |
jbkim | 0:b72d22e10709 | 83 | #define Sn_RXBUF_SIZE 0x401e |
jbkim | 0:b72d22e10709 | 84 | #define Sn_TXBUF_SIZE 0x401f |
jbkim | 0:b72d22e10709 | 85 | #define Sn_TX_FSR 0x4020 |
jbkim | 0:b72d22e10709 | 86 | #define Sn_TX_WR 0x4024 |
jbkim | 0:b72d22e10709 | 87 | #define Sn_RX_RSR 0x4026 |
jbkim | 0:b72d22e10709 | 88 | #define Sn_RX_RD 0x4028 |
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 W5200 |
jbkim | 0:b72d22e10709 | 97 | * @param reset reset pin of the W5200 |
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 W5200 module 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 W5200 module from the access point |
jbkim | 0:b72d22e10709 | 111 | * |
jbkim | 0:b72d22e10709 | 112 | * @ returns true if successful |
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 W5200 module |
jbkim | 0:b72d22e10709 | 135 | */ |
jbkim | 0:b72d22e10709 | 136 | void reset(); |
jbkim | 0:b72d22e10709 | 137 | |
jbkim | 0:b72d22e10709 | 138 | |
jbkim | 0:b72d22e10709 | 139 | int wait_readable(int socket, int wait_time_ms, int req_size = 0); |
jbkim | 0:b72d22e10709 | 140 | |
jbkim | 0:b72d22e10709 | 141 | int wait_writeable(int socket, int wait_time_ms, int req_size = 0); |
jbkim | 0:b72d22e10709 | 142 | |
jbkim | 0:b72d22e10709 | 143 | /* |
jbkim | 0:b72d22e10709 | 144 | * Check if a tcp link is active |
jbkim | 0:b72d22e10709 | 145 | * |
jbkim | 0:b72d22e10709 | 146 | * @returns true if successful |
jbkim | 0:b72d22e10709 | 147 | */ |
jbkim | 0:b72d22e10709 | 148 | bool is_connected(int socket); |
jbkim | 0:b72d22e10709 | 149 | |
jbkim | 0:b72d22e10709 | 150 | /* |
jbkim | 0:b72d22e10709 | 151 | * Close a tcp connection |
jbkim | 0:b72d22e10709 | 152 | * |
jbkim | 0:b72d22e10709 | 153 | * @ returns true if successful |
jbkim | 0:b72d22e10709 | 154 | */ |
jbkim | 0:b72d22e10709 | 155 | bool close(int socket); |
jbkim | 0:b72d22e10709 | 156 | |
jbkim | 0:b72d22e10709 | 157 | /* |
jbkim | 0:b72d22e10709 | 158 | * @param str string to be sent |
jbkim | 0:b72d22e10709 | 159 | * @param len string length |
jbkim | 0:b72d22e10709 | 160 | */ |
jbkim | 0:b72d22e10709 | 161 | int send(int socket, const char * str, int len); |
jbkim | 0:b72d22e10709 | 162 | |
jbkim | 0:b72d22e10709 | 163 | int recv(int socket, char* buf, int len); |
jbkim | 0:b72d22e10709 | 164 | |
jbkim | 0:b72d22e10709 | 165 | /* |
jbkim | 0:b72d22e10709 | 166 | * Return true if the module is using dhcp |
jbkim | 0:b72d22e10709 | 167 | * |
jbkim | 0:b72d22e10709 | 168 | * @returns true if the module is using dhcp |
jbkim | 0:b72d22e10709 | 169 | */ |
jbkim | 0:b72d22e10709 | 170 | bool isDHCP() { |
jbkim | 0:b72d22e10709 | 171 | return dhcp; |
jbkim | 0:b72d22e10709 | 172 | } |
jbkim | 0:b72d22e10709 | 173 | |
jbkim | 0:b72d22e10709 | 174 | bool gethostbyname(const char* host, uint32_t* ip); |
jbkim | 0:b72d22e10709 | 175 | |
jbkim | 0:b72d22e10709 | 176 | static WIZnet_Chip * getInstance() { |
jbkim | 0:b72d22e10709 | 177 | return inst; |
jbkim | 0:b72d22e10709 | 178 | }; |
jbkim | 0:b72d22e10709 | 179 | |
jbkim | 0:b72d22e10709 | 180 | int new_socket(); |
jbkim | 0:b72d22e10709 | 181 | uint16_t new_port(); |
jbkim | 0:b72d22e10709 | 182 | void scmd(int socket, Command cmd); |
jbkim | 0:b72d22e10709 | 183 | |
jbkim | 0:b72d22e10709 | 184 | template<typename T> |
jbkim | 0:b72d22e10709 | 185 | void sreg(int socket, uint16_t addr, T data) { |
jbkim | 0:b72d22e10709 | 186 | reg_wr<T>(addr+0x100*socket, data); |
jbkim | 0:b72d22e10709 | 187 | } |
jbkim | 0:b72d22e10709 | 188 | |
jbkim | 0:b72d22e10709 | 189 | template<typename T> |
jbkim | 0:b72d22e10709 | 190 | T sreg(int socket, uint16_t addr) { |
jbkim | 0:b72d22e10709 | 191 | return reg_rd<T>(addr+0x100*socket); |
jbkim | 0:b72d22e10709 | 192 | } |
jbkim | 0:b72d22e10709 | 193 | |
jbkim | 0:b72d22e10709 | 194 | template<typename T> |
jbkim | 0:b72d22e10709 | 195 | void reg_wr(uint16_t addr, T data) { |
jbkim | 0:b72d22e10709 | 196 | uint8_t buf[sizeof(T)]; |
jbkim | 0:b72d22e10709 | 197 | *reinterpret_cast<T*>(buf) = data; |
jbkim | 0:b72d22e10709 | 198 | for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian |
jbkim | 0:b72d22e10709 | 199 | uint8_t t = buf[i]; |
jbkim | 0:b72d22e10709 | 200 | buf[i] = buf[sizeof(buf)-1-i]; |
jbkim | 0:b72d22e10709 | 201 | buf[sizeof(buf)-1-i] = t; |
jbkim | 0:b72d22e10709 | 202 | } |
jbkim | 0:b72d22e10709 | 203 | spi_write(addr, buf, sizeof(buf)); |
jbkim | 0:b72d22e10709 | 204 | } |
jbkim | 0:b72d22e10709 | 205 | |
jbkim | 0:b72d22e10709 | 206 | template<typename T> |
jbkim | 0:b72d22e10709 | 207 | T reg_rd(uint16_t addr) { |
jbkim | 0:b72d22e10709 | 208 | uint8_t buf[sizeof(T)]; |
jbkim | 0:b72d22e10709 | 209 | spi_read(addr, buf, sizeof(buf)); |
jbkim | 0:b72d22e10709 | 210 | for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian |
jbkim | 0:b72d22e10709 | 211 | uint8_t t = buf[i]; |
jbkim | 0:b72d22e10709 | 212 | buf[i] = buf[sizeof(buf)-1-i]; |
jbkim | 0:b72d22e10709 | 213 | buf[sizeof(buf)-1-i] = t; |
jbkim | 0:b72d22e10709 | 214 | } |
jbkim | 0:b72d22e10709 | 215 | return *reinterpret_cast<T*>(buf); |
jbkim | 0:b72d22e10709 | 216 | } |
jbkim | 0:b72d22e10709 | 217 | |
jbkim | 0:b72d22e10709 | 218 | void reg_rd_mac(uint16_t addr, uint8_t* data) { |
jbkim | 0:b72d22e10709 | 219 | spi_read(addr, data, 6); |
jbkim | 0:b72d22e10709 | 220 | } |
jbkim | 0:b72d22e10709 | 221 | |
jbkim | 0:b72d22e10709 | 222 | void reg_wr_ip(uint16_t addr, const char* ip) { |
jbkim | 0:b72d22e10709 | 223 | uint8_t buf[4]; |
jbkim | 0:b72d22e10709 | 224 | char* p = (char*)ip; |
jbkim | 0:b72d22e10709 | 225 | for(int i = 0; i < 4; i++) { |
jbkim | 0:b72d22e10709 | 226 | buf[i] = atoi(p); |
jbkim | 0:b72d22e10709 | 227 | p = strchr(p, '.'); |
jbkim | 0:b72d22e10709 | 228 | if (p == NULL) { |
jbkim | 0:b72d22e10709 | 229 | break; |
jbkim | 0:b72d22e10709 | 230 | } |
jbkim | 0:b72d22e10709 | 231 | p++; |
jbkim | 0:b72d22e10709 | 232 | } |
jbkim | 0:b72d22e10709 | 233 | spi_write(addr, buf, sizeof(buf)); |
jbkim | 0:b72d22e10709 | 234 | } |
jbkim | 0:b72d22e10709 | 235 | |
jbkim | 0:b72d22e10709 | 236 | void sreg_ip(int socket, uint16_t addr, const char* ip) { |
jbkim | 0:b72d22e10709 | 237 | reg_wr_ip(addr+0x100*socket, ip); |
jbkim | 0:b72d22e10709 | 238 | } |
jbkim | 0:b72d22e10709 | 239 | |
jbkim | 0:b72d22e10709 | 240 | protected: |
jbkim | 1:8138a268fbd2 | 241 | uint8_t mac[6]; |
jbkim | 0:b72d22e10709 | 242 | uint32_t ip; |
jbkim | 0:b72d22e10709 | 243 | uint32_t netmask; |
jbkim | 0:b72d22e10709 | 244 | uint32_t gateway; |
jbkim | 0:b72d22e10709 | 245 | uint32_t dnsaddr; |
jbkim | 0:b72d22e10709 | 246 | bool dhcp; |
jbkim | 0:b72d22e10709 | 247 | |
jbkim | 0:b72d22e10709 | 248 | static WIZnet_Chip* inst; |
jbkim | 0:b72d22e10709 | 249 | |
jbkim | 0:b72d22e10709 | 250 | void reg_wr_mac(uint16_t addr, uint8_t* data) { |
jbkim | 0:b72d22e10709 | 251 | spi_write(addr, data, 6); |
jbkim | 0:b72d22e10709 | 252 | } |
jbkim | 0:b72d22e10709 | 253 | |
jbkim | 0:b72d22e10709 | 254 | void spi_write(uint16_t addr, const uint8_t *buf, uint16_t len); |
jbkim | 0:b72d22e10709 | 255 | void spi_read(uint16_t addr, uint8_t *buf, uint16_t len); |
jbkim | 0:b72d22e10709 | 256 | SPI* spi; |
jbkim | 0:b72d22e10709 | 257 | DigitalOut cs; |
jbkim | 0:b72d22e10709 | 258 | DigitalOut reset_pin; |
jbkim | 0:b72d22e10709 | 259 | }; |
jbkim | 0:b72d22e10709 | 260 | |
jbkim | 0:b72d22e10709 | 261 | extern uint32_t str_to_ip(const char* str); |
jbkim | 0:b72d22e10709 | 262 | extern void printfBytes(char* str, uint8_t* buf, int len); |
jbkim | 0:b72d22e10709 | 263 | extern void printHex(uint8_t* buf, int len); |
jbkim | 0:b72d22e10709 | 264 | extern void debug_hex(uint8_t* buf, int len); |