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/W5100.cpp@9:45167b342fec, 2018-08-08 (annotated)
- Committer:
- Vektor
- Date:
- Wed Aug 08 09:45:44 2018 +0000
- Revision:
- 9:45167b342fec
- Parent:
- 8:cb8808b47e69
Change default chip to 5100
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 | #include "mbed.h" |
jbkim | 0:b72d22e10709 | 20 | #include "mbed_debug.h" |
jbkim | 0:b72d22e10709 | 21 | #include "wiznet.h" |
jbkim | 0:b72d22e10709 | 22 | #include "DNSClient.h" |
jbkim | 0:b72d22e10709 | 23 | |
jbkim | 0:b72d22e10709 | 24 | #ifdef USE_W5100 |
jbkim | 0:b72d22e10709 | 25 | |
jbkim | 0:b72d22e10709 | 26 | //Debug is disabled by default |
jbkim | 0:b72d22e10709 | 27 | #if 0 |
jbkim | 0:b72d22e10709 | 28 | #define DBG(...) do{debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0); |
jbkim | 0:b72d22e10709 | 29 | //#define DBG(x, ...) debug("[WIZnet_Chip:DBG]"x"\r\n", ##__VA_ARGS__); |
jbkim | 0:b72d22e10709 | 30 | #define WARN(x, ...) debug("[WIZnet_Chip:WARN]"x"\r\n", ##__VA_ARGS__); |
jbkim | 0:b72d22e10709 | 31 | #define ERR(x, ...) debug("[WIZnet_Chip:ERR]"x"\r\n", ##__VA_ARGS__); |
jbkim | 0:b72d22e10709 | 32 | #else |
jbkim | 0:b72d22e10709 | 33 | #define DBG(x, ...) |
jbkim | 0:b72d22e10709 | 34 | #define WARN(x, ...) |
jbkim | 0:b72d22e10709 | 35 | #define ERR(x, ...) |
jbkim | 0:b72d22e10709 | 36 | #endif |
jbkim | 0:b72d22e10709 | 37 | |
jbkim | 0:b72d22e10709 | 38 | #if 1 |
jbkim | 0:b72d22e10709 | 39 | #define INFO(x, ...) debug("[WIZnet_Chip:INFO]"x"\r\n", ##__VA_ARGS__); |
jbkim | 0:b72d22e10709 | 40 | #else |
jbkim | 0:b72d22e10709 | 41 | #define INFO(x, ...) |
jbkim | 0:b72d22e10709 | 42 | #endif |
jbkim | 0:b72d22e10709 | 43 | |
jbkim | 0:b72d22e10709 | 44 | #define DBG_SPI 0 |
jbkim | 0:b72d22e10709 | 45 | |
jbkim | 0:b72d22e10709 | 46 | WIZnet_Chip* WIZnet_Chip::inst; |
jbkim | 0:b72d22e10709 | 47 | |
jbkim | 0:b72d22e10709 | 48 | WIZnet_Chip::WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName _cs, PinName _reset): |
jbkim | 0:b72d22e10709 | 49 | cs(_cs), reset_pin(_reset) |
jbkim | 0:b72d22e10709 | 50 | { |
jbkim | 0:b72d22e10709 | 51 | spi = new SPI(mosi, miso, sclk); |
jbkim | 0:b72d22e10709 | 52 | |
jbkim | 0:b72d22e10709 | 53 | spi->format(8,0); |
jbkim | 0:b72d22e10709 | 54 | spi->frequency(2000000); |
jbkim | 0:b72d22e10709 | 55 | |
jbkim | 0:b72d22e10709 | 56 | cs = 1; |
jbkim | 0:b72d22e10709 | 57 | reset_pin = 1; |
jbkim | 0:b72d22e10709 | 58 | inst = this; |
jbkim | 0:b72d22e10709 | 59 | } |
jbkim | 0:b72d22e10709 | 60 | |
jbkim | 0:b72d22e10709 | 61 | WIZnet_Chip::WIZnet_Chip(SPI* spi, PinName _cs, PinName _reset): |
jbkim | 0:b72d22e10709 | 62 | cs(_cs), reset_pin(_reset) |
jbkim | 0:b72d22e10709 | 63 | { |
jbkim | 0:b72d22e10709 | 64 | this->spi = spi; |
jbkim | 0:b72d22e10709 | 65 | |
jbkim | 0:b72d22e10709 | 66 | this->spi->format(8,0); |
jbkim | 0:b72d22e10709 | 67 | this->spi->frequency(2000000); |
jbkim | 0:b72d22e10709 | 68 | |
jbkim | 0:b72d22e10709 | 69 | cs = 1; |
jbkim | 0:b72d22e10709 | 70 | reset_pin = 1; |
jbkim | 0:b72d22e10709 | 71 | inst = this; |
jbkim | 0:b72d22e10709 | 72 | } |
jbkim | 0:b72d22e10709 | 73 | |
jbkim | 0:b72d22e10709 | 74 | bool WIZnet_Chip::setip() |
jbkim | 0:b72d22e10709 | 75 | { |
jbkim | 0:b72d22e10709 | 76 | reg_wr<uint32_t>(SIPR, ip); |
jbkim | 0:b72d22e10709 | 77 | reg_wr<uint32_t>(GAR, gateway); |
jbkim | 0:b72d22e10709 | 78 | reg_wr<uint32_t>(SUBR, netmask); |
jbkim | 0:b72d22e10709 | 79 | return true; |
jbkim | 0:b72d22e10709 | 80 | } |
jbkim | 0:b72d22e10709 | 81 | |
jbkim | 0:b72d22e10709 | 82 | bool WIZnet_Chip::setProtocol(int socket, Protocol p) |
jbkim | 0:b72d22e10709 | 83 | { |
jbkim | 0:b72d22e10709 | 84 | if (socket < 0) { |
jbkim | 0:b72d22e10709 | 85 | return false; |
jbkim | 0:b72d22e10709 | 86 | } |
jbkim | 0:b72d22e10709 | 87 | sreg<uint8_t>(socket, Sn_MR, p); |
jbkim | 0:b72d22e10709 | 88 | return true; |
jbkim | 0:b72d22e10709 | 89 | } |
jbkim | 0:b72d22e10709 | 90 | |
jbkim | 0:b72d22e10709 | 91 | bool WIZnet_Chip::connect(int socket, const char * host, int port, int timeout_ms) |
jbkim | 0:b72d22e10709 | 92 | { |
jbkim | 0:b72d22e10709 | 93 | if (socket < 0) { |
jbkim | 0:b72d22e10709 | 94 | return false; |
jbkim | 0:b72d22e10709 | 95 | } |
jbkim | 0:b72d22e10709 | 96 | sreg<uint8_t>(socket, Sn_MR, TCP); |
jbkim | 0:b72d22e10709 | 97 | scmd(socket, OPEN); |
jbkim | 0:b72d22e10709 | 98 | sreg_ip(socket, Sn_DIPR, host); |
jbkim | 0:b72d22e10709 | 99 | sreg<uint16_t>(socket, Sn_DPORT, port); |
jbkim | 0:b72d22e10709 | 100 | sreg<uint16_t>(socket, Sn_PORT, new_port()); |
jbkim | 0:b72d22e10709 | 101 | scmd(socket, CONNECT); |
jbkim | 0:b72d22e10709 | 102 | Timer t; |
jbkim | 0:b72d22e10709 | 103 | t.reset(); |
jbkim | 0:b72d22e10709 | 104 | t.start(); |
jbkim | 0:b72d22e10709 | 105 | while(!is_connected(socket)) { |
jbkim | 0:b72d22e10709 | 106 | if (t.read_ms() > timeout_ms) { |
jbkim | 0:b72d22e10709 | 107 | return false; |
jbkim | 0:b72d22e10709 | 108 | } |
jbkim | 0:b72d22e10709 | 109 | } |
jbkim | 0:b72d22e10709 | 110 | return true; |
jbkim | 0:b72d22e10709 | 111 | } |
jbkim | 0:b72d22e10709 | 112 | |
jbkim | 0:b72d22e10709 | 113 | bool WIZnet_Chip::gethostbyname(const char* host, uint32_t* ip) |
jbkim | 0:b72d22e10709 | 114 | { |
jbkim | 0:b72d22e10709 | 115 | uint32_t addr = str_to_ip(host); |
jbkim | 0:b72d22e10709 | 116 | char buf[17]; |
jbkim | 0:b72d22e10709 | 117 | snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff); |
jbkim | 0:b72d22e10709 | 118 | if (strcmp(buf, host) == 0) { |
jbkim | 0:b72d22e10709 | 119 | *ip = addr; |
jbkim | 0:b72d22e10709 | 120 | return true; |
jbkim | 0:b72d22e10709 | 121 | } |
jbkim | 0:b72d22e10709 | 122 | DNSClient client; |
jbkim | 0:b72d22e10709 | 123 | if(client.lookup(host)) { |
jbkim | 0:b72d22e10709 | 124 | *ip = client.ip; |
jbkim | 0:b72d22e10709 | 125 | return true; |
jbkim | 0:b72d22e10709 | 126 | } |
jbkim | 0:b72d22e10709 | 127 | return false; |
jbkim | 0:b72d22e10709 | 128 | } |
jbkim | 0:b72d22e10709 | 129 | |
jbkim | 0:b72d22e10709 | 130 | bool WIZnet_Chip::disconnect() |
jbkim | 0:b72d22e10709 | 131 | { |
jbkim | 0:b72d22e10709 | 132 | return true; |
jbkim | 0:b72d22e10709 | 133 | } |
jbkim | 0:b72d22e10709 | 134 | |
jbkim | 0:b72d22e10709 | 135 | bool WIZnet_Chip::is_connected(int socket) |
jbkim | 0:b72d22e10709 | 136 | { |
jbkim | 0:b72d22e10709 | 137 | if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) { |
jbkim | 0:b72d22e10709 | 138 | return true; |
jbkim | 0:b72d22e10709 | 139 | } |
jbkim | 0:b72d22e10709 | 140 | return false; |
jbkim | 0:b72d22e10709 | 141 | } |
jbkim | 0:b72d22e10709 | 142 | |
Bongjun | 8:cb8808b47e69 | 143 | |
Bongjun | 8:cb8808b47e69 | 144 | bool WIZnet_Chip::is_fin_received(int socket) |
Bongjun | 8:cb8808b47e69 | 145 | { |
Bongjun | 8:cb8808b47e69 | 146 | uint8_t tmpSn_SR; |
Bongjun | 8:cb8808b47e69 | 147 | tmpSn_SR = sreg<uint8_t>(socket, Sn_SR); |
Bongjun | 8:cb8808b47e69 | 148 | // packet sending is possible, when state is SOCK_CLOSE_WAIT. |
Bongjun | 8:cb8808b47e69 | 149 | if (tmpSn_SR == SOCK_CLOSE_WAIT) { |
Bongjun | 8:cb8808b47e69 | 150 | return true; |
Bongjun | 8:cb8808b47e69 | 151 | } |
Bongjun | 8:cb8808b47e69 | 152 | return false; |
Bongjun | 8:cb8808b47e69 | 153 | } |
Bongjun | 8:cb8808b47e69 | 154 | |
jbkim | 0:b72d22e10709 | 155 | void WIZnet_Chip::reset() |
jbkim | 0:b72d22e10709 | 156 | { |
jbkim | 0:b72d22e10709 | 157 | reset_pin = 1; |
jbkim | 0:b72d22e10709 | 158 | reset_pin = 0; |
jbkim | 0:b72d22e10709 | 159 | wait_us(2); // 2us |
jbkim | 0:b72d22e10709 | 160 | reset_pin = 1; |
jbkim | 0:b72d22e10709 | 161 | wait_ms(150); // 150ms |
jbkim | 0:b72d22e10709 | 162 | |
jbkim | 0:b72d22e10709 | 163 | reg_wr<uint8_t>(MR, 1<<7); |
jbkim | 0:b72d22e10709 | 164 | |
jbkim | 0:b72d22e10709 | 165 | reg_wr_mac(SHAR, mac); |
jbkim | 0:b72d22e10709 | 166 | } |
jbkim | 0:b72d22e10709 | 167 | |
jbkim | 0:b72d22e10709 | 168 | bool WIZnet_Chip::close(int socket) |
jbkim | 0:b72d22e10709 | 169 | { |
jbkim | 0:b72d22e10709 | 170 | if (socket < 0) { |
jbkim | 0:b72d22e10709 | 171 | return false; |
jbkim | 0:b72d22e10709 | 172 | } |
jbkim | 0:b72d22e10709 | 173 | // if not connected, return |
jbkim | 0:b72d22e10709 | 174 | if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) { |
jbkim | 0:b72d22e10709 | 175 | return true; |
jbkim | 0:b72d22e10709 | 176 | } |
jbkim | 0:b72d22e10709 | 177 | |
jbkim | 0:b72d22e10709 | 178 | scmd(socket, CLOSE); |
jbkim | 0:b72d22e10709 | 179 | sreg<uint8_t>(socket, Sn_IR, 0xff); |
jbkim | 0:b72d22e10709 | 180 | return true; |
jbkim | 0:b72d22e10709 | 181 | } |
jbkim | 0:b72d22e10709 | 182 | |
jbkim | 0:b72d22e10709 | 183 | int WIZnet_Chip::wait_readable(int socket, int wait_time_ms, int req_size) |
jbkim | 0:b72d22e10709 | 184 | { |
jbkim | 0:b72d22e10709 | 185 | if (socket < 0) { |
jbkim | 0:b72d22e10709 | 186 | return -1; |
jbkim | 0:b72d22e10709 | 187 | } |
jbkim | 0:b72d22e10709 | 188 | Timer t; |
jbkim | 0:b72d22e10709 | 189 | t.reset(); |
jbkim | 0:b72d22e10709 | 190 | t.start(); |
jbkim | 0:b72d22e10709 | 191 | while(1) { |
jbkim | 0:b72d22e10709 | 192 | int size = 0; int size1 = 0; |
jbkim | 0:b72d22e10709 | 193 | do { |
jbkim | 0:b72d22e10709 | 194 | size = sreg<uint16_t>(socket, Sn_RX_RSR); |
jbkim | 0:b72d22e10709 | 195 | if (size != 0) size1 = sreg<uint16_t>(socket, Sn_RX_RSR); |
jbkim | 0:b72d22e10709 | 196 | }while(size != size1); |
jbkim | 0:b72d22e10709 | 197 | |
jbkim | 0:b72d22e10709 | 198 | if (size > req_size) { |
jbkim | 0:b72d22e10709 | 199 | return size; |
jbkim | 0:b72d22e10709 | 200 | } |
jbkim | 0:b72d22e10709 | 201 | if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { |
jbkim | 0:b72d22e10709 | 202 | break; |
jbkim | 0:b72d22e10709 | 203 | } |
jbkim | 0:b72d22e10709 | 204 | } |
jbkim | 0:b72d22e10709 | 205 | return -1; |
jbkim | 0:b72d22e10709 | 206 | } |
jbkim | 0:b72d22e10709 | 207 | |
jbkim | 0:b72d22e10709 | 208 | int WIZnet_Chip::wait_writeable(int socket, int wait_time_ms, int req_size) |
jbkim | 0:b72d22e10709 | 209 | { |
jbkim | 0:b72d22e10709 | 210 | if (socket < 0) { |
jbkim | 0:b72d22e10709 | 211 | return -1; |
jbkim | 0:b72d22e10709 | 212 | } |
jbkim | 0:b72d22e10709 | 213 | Timer t; |
jbkim | 0:b72d22e10709 | 214 | t.reset(); |
jbkim | 0:b72d22e10709 | 215 | t.start(); |
jbkim | 0:b72d22e10709 | 216 | while(1) { |
Bongjun | 8:cb8808b47e69 | 217 | //int size = sreg<uint16_t>(socket, Sn_TX_FSR); |
Bongjun | 8:cb8808b47e69 | 218 | // during the reading Sn_TX_FSR, it has the possible change of this register. |
Bongjun | 8:cb8808b47e69 | 219 | // so read twice and get same value then use size information. |
Bongjun | 8:cb8808b47e69 | 220 | int size, size2; |
Bongjun | 8:cb8808b47e69 | 221 | do { |
Bongjun | 8:cb8808b47e69 | 222 | size = sreg<uint16_t>(socket, Sn_TX_FSR); |
Bongjun | 8:cb8808b47e69 | 223 | size2 = sreg<uint16_t>(socket, Sn_TX_FSR); |
Bongjun | 8:cb8808b47e69 | 224 | } while (size != size2); |
jbkim | 0:b72d22e10709 | 225 | if (size > req_size) { |
jbkim | 0:b72d22e10709 | 226 | return size; |
jbkim | 0:b72d22e10709 | 227 | } |
jbkim | 0:b72d22e10709 | 228 | if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { |
jbkim | 0:b72d22e10709 | 229 | break; |
jbkim | 0:b72d22e10709 | 230 | } |
jbkim | 0:b72d22e10709 | 231 | } |
jbkim | 0:b72d22e10709 | 232 | return -1; |
jbkim | 0:b72d22e10709 | 233 | } |
jbkim | 0:b72d22e10709 | 234 | |
jbkim | 0:b72d22e10709 | 235 | int WIZnet_Chip::send(int socket, const char * str, int len) |
jbkim | 0:b72d22e10709 | 236 | { |
jbkim | 0:b72d22e10709 | 237 | if (socket < 0) { |
jbkim | 0:b72d22e10709 | 238 | return -1; |
jbkim | 0:b72d22e10709 | 239 | } |
jbkim | 0:b72d22e10709 | 240 | uint16_t base = 0x4000 + socket * 0x800; // each socket has 2K buffer |
jbkim | 0:b72d22e10709 | 241 | uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR); |
jbkim | 0:b72d22e10709 | 242 | uint16_t dst = base + (ptr&(0x800-1)); |
jbkim | 0:b72d22e10709 | 243 | if ((dst + len) > (base+0x800)) { |
jbkim | 0:b72d22e10709 | 244 | int len2 = base + 0x800 - dst; |
jbkim | 0:b72d22e10709 | 245 | spi_write(dst, (uint8_t*)str, len2); |
jbkim | 0:b72d22e10709 | 246 | spi_write(base, (uint8_t*)str+len2, len-len2); |
jbkim | 0:b72d22e10709 | 247 | } else { |
jbkim | 0:b72d22e10709 | 248 | spi_write(dst, (uint8_t*)str, len); |
jbkim | 0:b72d22e10709 | 249 | } |
jbkim | 0:b72d22e10709 | 250 | sreg<uint16_t>(socket, Sn_TX_WR, ptr + len); |
jbkim | 0:b72d22e10709 | 251 | scmd(socket, SEND); |
jbkim | 0:b72d22e10709 | 252 | return len; |
jbkim | 0:b72d22e10709 | 253 | } |
jbkim | 0:b72d22e10709 | 254 | |
jbkim | 0:b72d22e10709 | 255 | int WIZnet_Chip::recv(int socket, char* buf, int len) |
jbkim | 0:b72d22e10709 | 256 | { |
jbkim | 0:b72d22e10709 | 257 | if (socket < 0) { |
jbkim | 0:b72d22e10709 | 258 | return -1; |
jbkim | 0:b72d22e10709 | 259 | } |
jbkim | 0:b72d22e10709 | 260 | uint16_t base = 0x6000 + socket * 0x800; // each socket has 2K buffer |
jbkim | 0:b72d22e10709 | 261 | uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD); |
jbkim | 0:b72d22e10709 | 262 | uint16_t src = base + (ptr&(0x800-1)); |
jbkim | 0:b72d22e10709 | 263 | if ((src + len) > (base+0x800)) { |
jbkim | 0:b72d22e10709 | 264 | int len2 = base + 0x800 - src; |
jbkim | 0:b72d22e10709 | 265 | spi_read(src, (uint8_t*)buf, len2); |
jbkim | 0:b72d22e10709 | 266 | spi_read(base, (uint8_t*)buf+len2, len-len2); |
jbkim | 0:b72d22e10709 | 267 | } else { |
jbkim | 0:b72d22e10709 | 268 | spi_read(src, (uint8_t*)buf, len); |
jbkim | 0:b72d22e10709 | 269 | } |
jbkim | 0:b72d22e10709 | 270 | sreg<uint16_t>(socket, Sn_RX_RD, ptr + len); |
jbkim | 0:b72d22e10709 | 271 | scmd(socket, RECV); |
jbkim | 0:b72d22e10709 | 272 | return len; |
jbkim | 0:b72d22e10709 | 273 | } |
jbkim | 0:b72d22e10709 | 274 | |
jbkim | 0:b72d22e10709 | 275 | int WIZnet_Chip::new_socket() |
jbkim | 0:b72d22e10709 | 276 | { |
jbkim | 0:b72d22e10709 | 277 | for(int s = 0; s < MAX_SOCK_NUM; s++) { |
jbkim | 0:b72d22e10709 | 278 | if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) { |
jbkim | 0:b72d22e10709 | 279 | return s; |
jbkim | 0:b72d22e10709 | 280 | } |
jbkim | 0:b72d22e10709 | 281 | } |
jbkim | 0:b72d22e10709 | 282 | return -1; |
jbkim | 0:b72d22e10709 | 283 | } |
jbkim | 0:b72d22e10709 | 284 | |
jbkim | 0:b72d22e10709 | 285 | uint16_t WIZnet_Chip::new_port() |
jbkim | 0:b72d22e10709 | 286 | { |
jbkim | 0:b72d22e10709 | 287 | uint16_t port = rand(); |
jbkim | 0:b72d22e10709 | 288 | port |= 49152; |
jbkim | 0:b72d22e10709 | 289 | return port; |
jbkim | 0:b72d22e10709 | 290 | } |
jbkim | 0:b72d22e10709 | 291 | |
jbkim | 0:b72d22e10709 | 292 | void WIZnet_Chip::scmd(int socket, Command cmd) |
jbkim | 0:b72d22e10709 | 293 | { |
jbkim | 0:b72d22e10709 | 294 | sreg<uint8_t>(socket, Sn_CR, cmd); |
jbkim | 0:b72d22e10709 | 295 | while(sreg<uint8_t>(socket, Sn_CR)); |
jbkim | 0:b72d22e10709 | 296 | } |
jbkim | 0:b72d22e10709 | 297 | |
jbkim | 0:b72d22e10709 | 298 | void WIZnet_Chip::spi_write(uint16_t addr, const uint8_t *buf, uint16_t len) |
jbkim | 0:b72d22e10709 | 299 | { |
jbkim | 0:b72d22e10709 | 300 | for(int i = 0; i < len; i++) { |
jbkim | 0:b72d22e10709 | 301 | cs = 0; |
jbkim | 0:b72d22e10709 | 302 | spi->write(0xf0); |
jbkim | 0:b72d22e10709 | 303 | spi->write(addr >> 8); |
jbkim | 0:b72d22e10709 | 304 | spi->write(addr & 0xff); |
jbkim | 0:b72d22e10709 | 305 | addr++; |
jbkim | 0:b72d22e10709 | 306 | spi->write(buf[i]); |
jbkim | 0:b72d22e10709 | 307 | cs = 1; |
jbkim | 0:b72d22e10709 | 308 | } |
jbkim | 0:b72d22e10709 | 309 | #if DBG_SPI |
jbkim | 0:b72d22e10709 | 310 | debug("[SPI]W %04x(%d)", addr, len); |
jbkim | 0:b72d22e10709 | 311 | for(int i = 0; i < len; i++) { |
jbkim | 0:b72d22e10709 | 312 | debug(" %02x", buf[i]); |
jbkim | 0:b72d22e10709 | 313 | if (i > 16) { |
jbkim | 0:b72d22e10709 | 314 | debug(" ..."); |
jbkim | 0:b72d22e10709 | 315 | break; |
jbkim | 0:b72d22e10709 | 316 | } |
jbkim | 0:b72d22e10709 | 317 | } |
jbkim | 0:b72d22e10709 | 318 | debug("\r\n"); |
jbkim | 0:b72d22e10709 | 319 | #endif |
jbkim | 0:b72d22e10709 | 320 | } |
jbkim | 0:b72d22e10709 | 321 | |
jbkim | 0:b72d22e10709 | 322 | void WIZnet_Chip::spi_read(uint16_t addr, uint8_t *buf, uint16_t len) |
jbkim | 0:b72d22e10709 | 323 | { |
jbkim | 0:b72d22e10709 | 324 | for(int i = 0; i < len; i++) { |
jbkim | 0:b72d22e10709 | 325 | cs = 0; |
jbkim | 0:b72d22e10709 | 326 | spi->write(0x0f); |
jbkim | 0:b72d22e10709 | 327 | spi->write(addr >> 8); |
jbkim | 0:b72d22e10709 | 328 | spi->write(addr & 0xff); |
jbkim | 0:b72d22e10709 | 329 | addr++; |
jbkim | 0:b72d22e10709 | 330 | buf[i] = spi->write(0); |
jbkim | 0:b72d22e10709 | 331 | cs = 1; |
jbkim | 0:b72d22e10709 | 332 | } |
jbkim | 0:b72d22e10709 | 333 | #if DBG_SPI |
jbkim | 0:b72d22e10709 | 334 | debug("[SPI]R %04x(%d)", addr, len); |
jbkim | 0:b72d22e10709 | 335 | for(int i = 0; i < len; i++) { |
jbkim | 0:b72d22e10709 | 336 | debug(" %02x", buf[i]); |
jbkim | 0:b72d22e10709 | 337 | if (i > 16) { |
jbkim | 0:b72d22e10709 | 338 | debug(" ..."); |
jbkim | 0:b72d22e10709 | 339 | break; |
jbkim | 0:b72d22e10709 | 340 | } |
jbkim | 0:b72d22e10709 | 341 | } |
jbkim | 0:b72d22e10709 | 342 | debug("\r\n"); |
jbkim | 0:b72d22e10709 | 343 | if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) { |
jbkim | 0:b72d22e10709 | 344 | wait_ms(200); |
jbkim | 0:b72d22e10709 | 345 | } |
jbkim | 0:b72d22e10709 | 346 | #endif |
jbkim | 0:b72d22e10709 | 347 | } |
jbkim | 0:b72d22e10709 | 348 | |
jbkim | 0:b72d22e10709 | 349 | uint32_t str_to_ip(const char* str) |
jbkim | 0:b72d22e10709 | 350 | { |
jbkim | 0:b72d22e10709 | 351 | uint32_t ip = 0; |
jbkim | 0:b72d22e10709 | 352 | char* p = (char*)str; |
jbkim | 0:b72d22e10709 | 353 | for(int i = 0; i < 4; i++) { |
jbkim | 0:b72d22e10709 | 354 | ip |= atoi(p); |
jbkim | 0:b72d22e10709 | 355 | p = strchr(p, '.'); |
jbkim | 0:b72d22e10709 | 356 | if (p == NULL) { |
jbkim | 0:b72d22e10709 | 357 | break; |
jbkim | 0:b72d22e10709 | 358 | } |
jbkim | 0:b72d22e10709 | 359 | ip <<= 8; |
jbkim | 0:b72d22e10709 | 360 | p++; |
jbkim | 0:b72d22e10709 | 361 | } |
jbkim | 0:b72d22e10709 | 362 | return ip; |
jbkim | 0:b72d22e10709 | 363 | } |
jbkim | 0:b72d22e10709 | 364 | |
jbkim | 0:b72d22e10709 | 365 | void printfBytes(char* str, uint8_t* buf, int len) |
jbkim | 0:b72d22e10709 | 366 | { |
jbkim | 0:b72d22e10709 | 367 | printf("%s %d:", str, len); |
jbkim | 0:b72d22e10709 | 368 | for(int i = 0; i < len; i++) { |
jbkim | 0:b72d22e10709 | 369 | printf(" %02x", buf[i]); |
jbkim | 0:b72d22e10709 | 370 | } |
jbkim | 0:b72d22e10709 | 371 | printf("\n"); |
jbkim | 0:b72d22e10709 | 372 | } |
jbkim | 0:b72d22e10709 | 373 | |
jbkim | 0:b72d22e10709 | 374 | void printHex(uint8_t* buf, int len) |
jbkim | 0:b72d22e10709 | 375 | { |
jbkim | 0:b72d22e10709 | 376 | for(int i = 0; i < len; i++) { |
jbkim | 0:b72d22e10709 | 377 | if ((i%16) == 0) { |
jbkim | 0:b72d22e10709 | 378 | printf("%p", buf+i); |
jbkim | 0:b72d22e10709 | 379 | } |
jbkim | 0:b72d22e10709 | 380 | printf(" %02x", buf[i]); |
jbkim | 0:b72d22e10709 | 381 | if ((i%16) == 15) { |
jbkim | 0:b72d22e10709 | 382 | printf("\n"); |
jbkim | 0:b72d22e10709 | 383 | } |
jbkim | 0:b72d22e10709 | 384 | } |
jbkim | 0:b72d22e10709 | 385 | printf("\n"); |
jbkim | 0:b72d22e10709 | 386 | } |
jbkim | 0:b72d22e10709 | 387 | |
jbkim | 0:b72d22e10709 | 388 | void debug_hex(uint8_t* buf, int len) |
jbkim | 0:b72d22e10709 | 389 | { |
jbkim | 0:b72d22e10709 | 390 | for(int i = 0; i < len; i++) { |
jbkim | 0:b72d22e10709 | 391 | if ((i%16) == 0) { |
jbkim | 0:b72d22e10709 | 392 | debug("%p", buf+i); |
jbkim | 0:b72d22e10709 | 393 | } |
jbkim | 0:b72d22e10709 | 394 | debug(" %02x", buf[i]); |
jbkim | 0:b72d22e10709 | 395 | if ((i%16) == 15) { |
jbkim | 0:b72d22e10709 | 396 | debug("\n"); |
jbkim | 0:b72d22e10709 | 397 | } |
jbkim | 0:b72d22e10709 | 398 | } |
jbkim | 0:b72d22e10709 | 399 | debug("\n"); |
jbkim | 0:b72d22e10709 | 400 | } |
jbkim | 0:b72d22e10709 | 401 | |
jbkim | 0:b72d22e10709 | 402 | #endif |