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 WIZnet

Committer:
bangbh
Date:
Fri Apr 24 08:19:14 2015 +0000
Revision:
6:ca8405b9564d
Parent:
4:37a5586c4f64
Child:
9:f390679a0468
"bool is_fin_received()" method added.

Who changed what in which revision?

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