This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500, W5200 and W5100. One of them can be selected by enabling it in wiznet.h.
Fork of WIZnet_Library by
WIZnetInterface/WIZnet/W5200.h@8:cb8808b47e69, 2015-05-31 (annotated)
- Committer:
- Bongjun
- Date:
- Sun May 31 10:25:40 2015 +0000
- Revision:
- 8:cb8808b47e69
- Parent:
- 1:8138a268fbd2
fix some codes of reading Sn_RX_RSR, Sn_TX_FSR in W5100.cpp, W5200.cpp; added is_fin_received() in W5100, W5200 files
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 | |
Bongjun | 8:cb8808b47e69 | 150 | /* |
Bongjun | 8:cb8808b47e69 | 151 | * Check if FIN received. |
Bongjun | 8:cb8808b47e69 | 152 | * |
Bongjun | 8:cb8808b47e69 | 153 | * @returns true if successful |
Bongjun | 8:cb8808b47e69 | 154 | */ |
Bongjun | 8:cb8808b47e69 | 155 | bool is_fin_received(int socket); |
Bongjun | 8:cb8808b47e69 | 156 | |
jbkim | 0:b72d22e10709 | 157 | /* |
jbkim | 0:b72d22e10709 | 158 | * Close a tcp connection |
jbkim | 0:b72d22e10709 | 159 | * |
jbkim | 0:b72d22e10709 | 160 | * @ returns true if successful |
jbkim | 0:b72d22e10709 | 161 | */ |
jbkim | 0:b72d22e10709 | 162 | bool close(int socket); |
jbkim | 0:b72d22e10709 | 163 | |
jbkim | 0:b72d22e10709 | 164 | /* |
jbkim | 0:b72d22e10709 | 165 | * @param str string to be sent |
jbkim | 0:b72d22e10709 | 166 | * @param len string length |
jbkim | 0:b72d22e10709 | 167 | */ |
jbkim | 0:b72d22e10709 | 168 | int send(int socket, const char * str, int len); |
jbkim | 0:b72d22e10709 | 169 | |
jbkim | 0:b72d22e10709 | 170 | int recv(int socket, char* buf, int len); |
jbkim | 0:b72d22e10709 | 171 | |
jbkim | 0:b72d22e10709 | 172 | /* |
jbkim | 0:b72d22e10709 | 173 | * Return true if the module is using dhcp |
jbkim | 0:b72d22e10709 | 174 | * |
jbkim | 0:b72d22e10709 | 175 | * @returns true if the module is using dhcp |
jbkim | 0:b72d22e10709 | 176 | */ |
jbkim | 0:b72d22e10709 | 177 | bool isDHCP() { |
jbkim | 0:b72d22e10709 | 178 | return dhcp; |
jbkim | 0:b72d22e10709 | 179 | } |
jbkim | 0:b72d22e10709 | 180 | |
jbkim | 0:b72d22e10709 | 181 | bool gethostbyname(const char* host, uint32_t* ip); |
jbkim | 0:b72d22e10709 | 182 | |
jbkim | 0:b72d22e10709 | 183 | static WIZnet_Chip * getInstance() { |
jbkim | 0:b72d22e10709 | 184 | return inst; |
jbkim | 0:b72d22e10709 | 185 | }; |
jbkim | 0:b72d22e10709 | 186 | |
jbkim | 0:b72d22e10709 | 187 | int new_socket(); |
jbkim | 0:b72d22e10709 | 188 | uint16_t new_port(); |
jbkim | 0:b72d22e10709 | 189 | void scmd(int socket, Command cmd); |
jbkim | 0:b72d22e10709 | 190 | |
jbkim | 0:b72d22e10709 | 191 | template<typename T> |
jbkim | 0:b72d22e10709 | 192 | void sreg(int socket, uint16_t addr, T data) { |
jbkim | 0:b72d22e10709 | 193 | reg_wr<T>(addr+0x100*socket, data); |
jbkim | 0:b72d22e10709 | 194 | } |
jbkim | 0:b72d22e10709 | 195 | |
jbkim | 0:b72d22e10709 | 196 | template<typename T> |
jbkim | 0:b72d22e10709 | 197 | T sreg(int socket, uint16_t addr) { |
jbkim | 0:b72d22e10709 | 198 | return reg_rd<T>(addr+0x100*socket); |
jbkim | 0:b72d22e10709 | 199 | } |
jbkim | 0:b72d22e10709 | 200 | |
jbkim | 0:b72d22e10709 | 201 | template<typename T> |
jbkim | 0:b72d22e10709 | 202 | void reg_wr(uint16_t addr, T data) { |
jbkim | 0:b72d22e10709 | 203 | uint8_t buf[sizeof(T)]; |
jbkim | 0:b72d22e10709 | 204 | *reinterpret_cast<T*>(buf) = data; |
jbkim | 0:b72d22e10709 | 205 | for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian |
jbkim | 0:b72d22e10709 | 206 | uint8_t t = buf[i]; |
jbkim | 0:b72d22e10709 | 207 | buf[i] = buf[sizeof(buf)-1-i]; |
jbkim | 0:b72d22e10709 | 208 | buf[sizeof(buf)-1-i] = t; |
jbkim | 0:b72d22e10709 | 209 | } |
jbkim | 0:b72d22e10709 | 210 | spi_write(addr, buf, sizeof(buf)); |
jbkim | 0:b72d22e10709 | 211 | } |
jbkim | 0:b72d22e10709 | 212 | |
jbkim | 0:b72d22e10709 | 213 | template<typename T> |
jbkim | 0:b72d22e10709 | 214 | T reg_rd(uint16_t addr) { |
jbkim | 0:b72d22e10709 | 215 | uint8_t buf[sizeof(T)]; |
jbkim | 0:b72d22e10709 | 216 | spi_read(addr, buf, sizeof(buf)); |
jbkim | 0:b72d22e10709 | 217 | for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian |
jbkim | 0:b72d22e10709 | 218 | uint8_t t = buf[i]; |
jbkim | 0:b72d22e10709 | 219 | buf[i] = buf[sizeof(buf)-1-i]; |
jbkim | 0:b72d22e10709 | 220 | buf[sizeof(buf)-1-i] = t; |
jbkim | 0:b72d22e10709 | 221 | } |
jbkim | 0:b72d22e10709 | 222 | return *reinterpret_cast<T*>(buf); |
jbkim | 0:b72d22e10709 | 223 | } |
jbkim | 0:b72d22e10709 | 224 | |
jbkim | 0:b72d22e10709 | 225 | void reg_rd_mac(uint16_t addr, uint8_t* data) { |
jbkim | 0:b72d22e10709 | 226 | spi_read(addr, data, 6); |
jbkim | 0:b72d22e10709 | 227 | } |
jbkim | 0:b72d22e10709 | 228 | |
jbkim | 0:b72d22e10709 | 229 | void reg_wr_ip(uint16_t addr, const char* ip) { |
jbkim | 0:b72d22e10709 | 230 | uint8_t buf[4]; |
jbkim | 0:b72d22e10709 | 231 | char* p = (char*)ip; |
jbkim | 0:b72d22e10709 | 232 | for(int i = 0; i < 4; i++) { |
jbkim | 0:b72d22e10709 | 233 | buf[i] = atoi(p); |
jbkim | 0:b72d22e10709 | 234 | p = strchr(p, '.'); |
jbkim | 0:b72d22e10709 | 235 | if (p == NULL) { |
jbkim | 0:b72d22e10709 | 236 | break; |
jbkim | 0:b72d22e10709 | 237 | } |
jbkim | 0:b72d22e10709 | 238 | p++; |
jbkim | 0:b72d22e10709 | 239 | } |
jbkim | 0:b72d22e10709 | 240 | spi_write(addr, buf, sizeof(buf)); |
jbkim | 0:b72d22e10709 | 241 | } |
jbkim | 0:b72d22e10709 | 242 | |
jbkim | 0:b72d22e10709 | 243 | void sreg_ip(int socket, uint16_t addr, const char* ip) { |
jbkim | 0:b72d22e10709 | 244 | reg_wr_ip(addr+0x100*socket, ip); |
jbkim | 0:b72d22e10709 | 245 | } |
jbkim | 0:b72d22e10709 | 246 | |
jbkim | 0:b72d22e10709 | 247 | protected: |
jbkim | 1:8138a268fbd2 | 248 | uint8_t mac[6]; |
jbkim | 0:b72d22e10709 | 249 | uint32_t ip; |
jbkim | 0:b72d22e10709 | 250 | uint32_t netmask; |
jbkim | 0:b72d22e10709 | 251 | uint32_t gateway; |
jbkim | 0:b72d22e10709 | 252 | uint32_t dnsaddr; |
jbkim | 0:b72d22e10709 | 253 | bool dhcp; |
jbkim | 0:b72d22e10709 | 254 | |
jbkim | 0:b72d22e10709 | 255 | static WIZnet_Chip* inst; |
jbkim | 0:b72d22e10709 | 256 | |
jbkim | 0:b72d22e10709 | 257 | void reg_wr_mac(uint16_t addr, uint8_t* data) { |
jbkim | 0:b72d22e10709 | 258 | spi_write(addr, data, 6); |
jbkim | 0:b72d22e10709 | 259 | } |
jbkim | 0:b72d22e10709 | 260 | |
jbkim | 0:b72d22e10709 | 261 | void spi_write(uint16_t addr, const uint8_t *buf, uint16_t len); |
jbkim | 0:b72d22e10709 | 262 | void spi_read(uint16_t addr, uint8_t *buf, uint16_t len); |
jbkim | 0:b72d22e10709 | 263 | SPI* spi; |
jbkim | 0:b72d22e10709 | 264 | DigitalOut cs; |
jbkim | 0:b72d22e10709 | 265 | DigitalOut reset_pin; |
jbkim | 0:b72d22e10709 | 266 | }; |
jbkim | 0:b72d22e10709 | 267 | |
jbkim | 0:b72d22e10709 | 268 | extern uint32_t str_to_ip(const char* str); |
jbkim | 0:b72d22e10709 | 269 | extern void printfBytes(char* str, uint8_t* buf, int len); |
jbkim | 0:b72d22e10709 | 270 | extern void printHex(uint8_t* buf, int len); |
jbkim | 0:b72d22e10709 | 271 | extern void debug_hex(uint8_t* buf, int len); |