MQTT client test with W5200 ethernet shield

Dependents:   IBMIoTClientEthernetExample_W5200

Fork of W5500Interface by W5500-Ethernet-Interface Makers

Committer:
hillkim7
Date:
Thu Dec 25 11:18:46 2014 +0000
Revision:
11:313e091ab3f9
The IBM MQTT client demo program that is tested with Nucleo F401 and Seeedstudio Ethernet Shield. It is based on Wiznet sample program.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hillkim7 11:313e091ab3f9 1 /* Copyright (C) 2012 mbed.org, MIT License
hillkim7 11:313e091ab3f9 2 *
hillkim7 11:313e091ab3f9 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
hillkim7 11:313e091ab3f9 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
hillkim7 11:313e091ab3f9 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
hillkim7 11:313e091ab3f9 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
hillkim7 11:313e091ab3f9 7 * furnished to do so, subject to the following conditions:
hillkim7 11:313e091ab3f9 8 *
hillkim7 11:313e091ab3f9 9 * The above copyright notice and this permission notice shall be included in all copies or
hillkim7 11:313e091ab3f9 10 * substantial portions of the Software.
hillkim7 11:313e091ab3f9 11 *
hillkim7 11:313e091ab3f9 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
hillkim7 11:313e091ab3f9 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
hillkim7 11:313e091ab3f9 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
hillkim7 11:313e091ab3f9 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hillkim7 11:313e091ab3f9 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
hillkim7 11:313e091ab3f9 17 */
hillkim7 11:313e091ab3f9 18
hillkim7 11:313e091ab3f9 19 #include "mbed.h"
hillkim7 11:313e091ab3f9 20 #include "mbed_debug.h"
hillkim7 11:313e091ab3f9 21 #include "wiznet.h"
hillkim7 11:313e091ab3f9 22 #include "DNSClient.h"
hillkim7 11:313e091ab3f9 23
hillkim7 11:313e091ab3f9 24 #ifdef USE_W5200
hillkim7 11:313e091ab3f9 25
hillkim7 11:313e091ab3f9 26 //Debug is disabled by default
hillkim7 11:313e091ab3f9 27 #if 0
hillkim7 11:313e091ab3f9 28 #define DBG(...) do{debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0);
hillkim7 11:313e091ab3f9 29 //#define DBG(x, ...) debug("[WIZnet_Chip:DBG]"x"\r\n", ##__VA_ARGS__);
hillkim7 11:313e091ab3f9 30 #define WARN(x, ...) debug("[WIZnet_Chip:WARN]"x"\r\n", ##__VA_ARGS__);
hillkim7 11:313e091ab3f9 31 #define ERR(x, ...) debug("[WIZnet_Chip:ERR]"x"\r\n", ##__VA_ARGS__);
hillkim7 11:313e091ab3f9 32 #else
hillkim7 11:313e091ab3f9 33 #define DBG(x, ...)
hillkim7 11:313e091ab3f9 34 #define WARN(x, ...)
hillkim7 11:313e091ab3f9 35 #define ERR(x, ...)
hillkim7 11:313e091ab3f9 36 #endif
hillkim7 11:313e091ab3f9 37
hillkim7 11:313e091ab3f9 38 #if 1
hillkim7 11:313e091ab3f9 39 #define INFO(x, ...) debug("[WIZnet_Chip:INFO]"x"\r\n", ##__VA_ARGS__);
hillkim7 11:313e091ab3f9 40 #else
hillkim7 11:313e091ab3f9 41 #define INFO(x, ...)
hillkim7 11:313e091ab3f9 42 #endif
hillkim7 11:313e091ab3f9 43
hillkim7 11:313e091ab3f9 44 #if 0
hillkim7 11:313e091ab3f9 45 #define W printf
hillkim7 11:313e091ab3f9 46 #else
hillkim7 11:313e091ab3f9 47 #define W(x, ...)
hillkim7 11:313e091ab3f9 48 #endif
hillkim7 11:313e091ab3f9 49
hillkim7 11:313e091ab3f9 50 #define DBG_SPI 0
hillkim7 11:313e091ab3f9 51
hillkim7 11:313e091ab3f9 52 #define W5200_USE_DEFAULT_MAC 1
hillkim7 11:313e091ab3f9 53
hillkim7 11:313e091ab3f9 54 #if W5200_USE_DEFAULT_MAC
hillkim7 11:313e091ab3f9 55 // Random MAC
hillkim7 11:313e091ab3f9 56 static const uint8_t default_mac[] = {0xD0, 0xAE, 0xBC, 0x11, 0x22, 0x02};
hillkim7 11:313e091ab3f9 57 #endif
hillkim7 11:313e091ab3f9 58
hillkim7 11:313e091ab3f9 59 WIZnet_Chip* WIZnet_Chip::inst;
hillkim7 11:313e091ab3f9 60
hillkim7 11:313e091ab3f9 61 WIZnet_Chip::WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName _cs, PinName _reset):
hillkim7 11:313e091ab3f9 62 cs(_cs), reset_pin(_reset)
hillkim7 11:313e091ab3f9 63 {
hillkim7 11:313e091ab3f9 64 spi = new SPI(mosi, miso, sclk);
hillkim7 11:313e091ab3f9 65 cs = 1;
hillkim7 11:313e091ab3f9 66 reset_pin = 1;
hillkim7 11:313e091ab3f9 67 inst = this;
hillkim7 11:313e091ab3f9 68 #if W5200_USE_DEFAULT_MAC
hillkim7 11:313e091ab3f9 69 memcpy(mac, default_mac, sizeof(default_mac));
hillkim7 11:313e091ab3f9 70 #endif
hillkim7 11:313e091ab3f9 71 }
hillkim7 11:313e091ab3f9 72
hillkim7 11:313e091ab3f9 73 WIZnet_Chip::WIZnet_Chip(SPI* spi, PinName _cs, PinName _reset):
hillkim7 11:313e091ab3f9 74 cs(_cs), reset_pin(_reset)
hillkim7 11:313e091ab3f9 75 {
hillkim7 11:313e091ab3f9 76 this->spi = spi;
hillkim7 11:313e091ab3f9 77 cs = 1;
hillkim7 11:313e091ab3f9 78 reset_pin = 1;
hillkim7 11:313e091ab3f9 79 inst = this;
hillkim7 11:313e091ab3f9 80 #if W5200_USE_DEFAULT_MAC
hillkim7 11:313e091ab3f9 81 memcpy(mac, default_mac, sizeof(default_mac));
hillkim7 11:313e091ab3f9 82 #endif
hillkim7 11:313e091ab3f9 83 }
hillkim7 11:313e091ab3f9 84
hillkim7 11:313e091ab3f9 85 bool WIZnet_Chip::setmac()
hillkim7 11:313e091ab3f9 86 {
hillkim7 11:313e091ab3f9 87
hillkim7 11:313e091ab3f9 88 for (int i =0; i < 6; i++) reg_wr<uint8_t>(SHAR+i, mac[i]);
hillkim7 11:313e091ab3f9 89
hillkim7 11:313e091ab3f9 90 return true;
hillkim7 11:313e091ab3f9 91 }
hillkim7 11:313e091ab3f9 92
hillkim7 11:313e091ab3f9 93 bool WIZnet_Chip::setip()
hillkim7 11:313e091ab3f9 94 {
hillkim7 11:313e091ab3f9 95 reg_wr<uint32_t>(SIPR, ip);
hillkim7 11:313e091ab3f9 96 reg_wr<uint32_t>(GAR, gateway);
hillkim7 11:313e091ab3f9 97 reg_wr<uint32_t>(SUBR, netmask);
hillkim7 11:313e091ab3f9 98 return true;
hillkim7 11:313e091ab3f9 99 }
hillkim7 11:313e091ab3f9 100
hillkim7 11:313e091ab3f9 101 bool WIZnet_Chip::linkstatus()
hillkim7 11:313e091ab3f9 102 {
hillkim7 11:313e091ab3f9 103 /*
hillkim7 11:313e091ab3f9 104 if ( (reg_rd<uint8_t>(PHYCFGR) & 0x01) != 0x01 )
hillkim7 11:313e091ab3f9 105 return false;
hillkim7 11:313e091ab3f9 106 */
hillkim7 11:313e091ab3f9 107
hillkim7 11:313e091ab3f9 108 return true;
hillkim7 11:313e091ab3f9 109 }
hillkim7 11:313e091ab3f9 110
hillkim7 11:313e091ab3f9 111 bool WIZnet_Chip::setProtocol(int socket, Protocol p)
hillkim7 11:313e091ab3f9 112 {
hillkim7 11:313e091ab3f9 113 if (socket < 0) {
hillkim7 11:313e091ab3f9 114 return false;
hillkim7 11:313e091ab3f9 115 }
hillkim7 11:313e091ab3f9 116 sreg<uint8_t>(socket, Sn_MR, p);
hillkim7 11:313e091ab3f9 117 return true;
hillkim7 11:313e091ab3f9 118 }
hillkim7 11:313e091ab3f9 119
hillkim7 11:313e091ab3f9 120 bool WIZnet_Chip::connect(int socket, const char * host, int port, int timeout_ms)
hillkim7 11:313e091ab3f9 121 {
hillkim7 11:313e091ab3f9 122 if (socket < 0) {
hillkim7 11:313e091ab3f9 123 return false;
hillkim7 11:313e091ab3f9 124 }
hillkim7 11:313e091ab3f9 125 sreg<uint8_t>(socket, Sn_MR, TCP);
hillkim7 11:313e091ab3f9 126 scmd(socket, OPEN);
hillkim7 11:313e091ab3f9 127 sreg_ip(socket, Sn_DIPR, host);
hillkim7 11:313e091ab3f9 128 sreg<uint16_t>(socket, Sn_DPORT, port);
hillkim7 11:313e091ab3f9 129 sreg<uint16_t>(socket, Sn_PORT, new_port());
hillkim7 11:313e091ab3f9 130 scmd(socket, CONNECT);
hillkim7 11:313e091ab3f9 131 Timer t;
hillkim7 11:313e091ab3f9 132 t.reset();
hillkim7 11:313e091ab3f9 133 t.start();
hillkim7 11:313e091ab3f9 134 while(!is_connected(socket)) {
hillkim7 11:313e091ab3f9 135 if (t.read_ms() > timeout_ms) {
hillkim7 11:313e091ab3f9 136 return false;
hillkim7 11:313e091ab3f9 137 }
hillkim7 11:313e091ab3f9 138 }
hillkim7 11:313e091ab3f9 139 W("\033[1;36m""{W5200}connect %d""\033[0m""\n", socket);
hillkim7 11:313e091ab3f9 140 return true;
hillkim7 11:313e091ab3f9 141 }
hillkim7 11:313e091ab3f9 142
hillkim7 11:313e091ab3f9 143 bool WIZnet_Chip::gethostbyname(const char* host, uint32_t* ip)
hillkim7 11:313e091ab3f9 144 {
hillkim7 11:313e091ab3f9 145 uint32_t addr = str_to_ip(host);
hillkim7 11:313e091ab3f9 146 char buf[17];
hillkim7 11:313e091ab3f9 147 snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
hillkim7 11:313e091ab3f9 148 if (strcmp(buf, host) == 0) {
hillkim7 11:313e091ab3f9 149 *ip = addr;
hillkim7 11:313e091ab3f9 150 return true;
hillkim7 11:313e091ab3f9 151 }
hillkim7 11:313e091ab3f9 152 DNSClient client;
hillkim7 11:313e091ab3f9 153 if(client.lookup(host)) {
hillkim7 11:313e091ab3f9 154 *ip = client.ip;
hillkim7 11:313e091ab3f9 155 return true;
hillkim7 11:313e091ab3f9 156 }
hillkim7 11:313e091ab3f9 157 return false;
hillkim7 11:313e091ab3f9 158 }
hillkim7 11:313e091ab3f9 159
hillkim7 11:313e091ab3f9 160 bool WIZnet_Chip::disconnect()
hillkim7 11:313e091ab3f9 161 {
hillkim7 11:313e091ab3f9 162 return true;
hillkim7 11:313e091ab3f9 163 }
hillkim7 11:313e091ab3f9 164
hillkim7 11:313e091ab3f9 165 bool WIZnet_Chip::is_connected(int socket)
hillkim7 11:313e091ab3f9 166 {
hillkim7 11:313e091ab3f9 167 if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) {
hillkim7 11:313e091ab3f9 168 return true;
hillkim7 11:313e091ab3f9 169 }
hillkim7 11:313e091ab3f9 170 return false;
hillkim7 11:313e091ab3f9 171 }
hillkim7 11:313e091ab3f9 172
hillkim7 11:313e091ab3f9 173 void WIZnet_Chip::reset()
hillkim7 11:313e091ab3f9 174 {
hillkim7 11:313e091ab3f9 175 reset_pin = 1;
hillkim7 11:313e091ab3f9 176 reset_pin = 0;
hillkim7 11:313e091ab3f9 177 wait_us(2); // 2us
hillkim7 11:313e091ab3f9 178 reset_pin = 1;
hillkim7 11:313e091ab3f9 179 wait_ms(150); // 150ms
hillkim7 11:313e091ab3f9 180
hillkim7 11:313e091ab3f9 181 reg_wr<uint8_t>(MR, 1<<7);
hillkim7 11:313e091ab3f9 182
hillkim7 11:313e091ab3f9 183 reg_wr_mac(SHAR, mac);
hillkim7 11:313e091ab3f9 184 }
hillkim7 11:313e091ab3f9 185
hillkim7 11:313e091ab3f9 186 bool WIZnet_Chip::close(int socket)
hillkim7 11:313e091ab3f9 187 {
hillkim7 11:313e091ab3f9 188 if (socket < 0) {
hillkim7 11:313e091ab3f9 189 return false;
hillkim7 11:313e091ab3f9 190 }
hillkim7 11:313e091ab3f9 191 // if not connected, return
hillkim7 11:313e091ab3f9 192 if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) {
hillkim7 11:313e091ab3f9 193 return true;
hillkim7 11:313e091ab3f9 194 }
hillkim7 11:313e091ab3f9 195 if (sreg<uint8_t>(socket, Sn_MR) == TCP) {
hillkim7 11:313e091ab3f9 196 scmd(socket, DISCON);
hillkim7 11:313e091ab3f9 197 }
hillkim7 11:313e091ab3f9 198 scmd(socket, CLOSE);
hillkim7 11:313e091ab3f9 199 sreg<uint8_t>(socket, Sn_IR, 0xff);
hillkim7 11:313e091ab3f9 200 W("\033[1;36m""{W5200}close %d""\033[0m""\n", socket);
hillkim7 11:313e091ab3f9 201 return true;
hillkim7 11:313e091ab3f9 202 }
hillkim7 11:313e091ab3f9 203
hillkim7 11:313e091ab3f9 204 int WIZnet_Chip::wait_readable(int socket, int wait_time_ms, int req_size)
hillkim7 11:313e091ab3f9 205 {
hillkim7 11:313e091ab3f9 206 if (socket < 0) {
hillkim7 11:313e091ab3f9 207 return -1;
hillkim7 11:313e091ab3f9 208 }
hillkim7 11:313e091ab3f9 209 Timer t;
hillkim7 11:313e091ab3f9 210 t.reset();
hillkim7 11:313e091ab3f9 211 t.start();
hillkim7 11:313e091ab3f9 212 while(1) {
hillkim7 11:313e091ab3f9 213 int size = sreg<uint16_t>(socket, Sn_RX_RSR);
hillkim7 11:313e091ab3f9 214 if (size > req_size) {
hillkim7 11:313e091ab3f9 215 return size;
hillkim7 11:313e091ab3f9 216 }
hillkim7 11:313e091ab3f9 217 if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
hillkim7 11:313e091ab3f9 218 break;
hillkim7 11:313e091ab3f9 219 }
hillkim7 11:313e091ab3f9 220 }
hillkim7 11:313e091ab3f9 221 return -1;
hillkim7 11:313e091ab3f9 222 }
hillkim7 11:313e091ab3f9 223
hillkim7 11:313e091ab3f9 224 int WIZnet_Chip::wait_writeable(int socket, int wait_time_ms, int req_size)
hillkim7 11:313e091ab3f9 225 {
hillkim7 11:313e091ab3f9 226 if (socket < 0) {
hillkim7 11:313e091ab3f9 227 return -1;
hillkim7 11:313e091ab3f9 228 }
hillkim7 11:313e091ab3f9 229 Timer t;
hillkim7 11:313e091ab3f9 230 t.reset();
hillkim7 11:313e091ab3f9 231 t.start();
hillkim7 11:313e091ab3f9 232 while(1) {
hillkim7 11:313e091ab3f9 233 int size = sreg<uint16_t>(socket, Sn_TX_FSR);
hillkim7 11:313e091ab3f9 234 if (size > req_size) {
hillkim7 11:313e091ab3f9 235 return size;
hillkim7 11:313e091ab3f9 236 }
hillkim7 11:313e091ab3f9 237 if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
hillkim7 11:313e091ab3f9 238 break;
hillkim7 11:313e091ab3f9 239 }
hillkim7 11:313e091ab3f9 240 }
hillkim7 11:313e091ab3f9 241 return -1;
hillkim7 11:313e091ab3f9 242 }
hillkim7 11:313e091ab3f9 243
hillkim7 11:313e091ab3f9 244 int WIZnet_Chip::send(int socket, const char * str, int len)
hillkim7 11:313e091ab3f9 245 {
hillkim7 11:313e091ab3f9 246 if (socket < 0) {
hillkim7 11:313e091ab3f9 247 return -1;
hillkim7 11:313e091ab3f9 248 }
hillkim7 11:313e091ab3f9 249 uint16_t base = 0x8000 + socket * 0x800;
hillkim7 11:313e091ab3f9 250 uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR);
hillkim7 11:313e091ab3f9 251 uint16_t dst = base + (ptr&(0x800-1));
hillkim7 11:313e091ab3f9 252 if ((dst + len) > (base+0x800)) {
hillkim7 11:313e091ab3f9 253 int len2 = base + 0x800 - dst;
hillkim7 11:313e091ab3f9 254 spi_write(dst, (uint8_t*)str, len2);
hillkim7 11:313e091ab3f9 255 spi_write(base, (uint8_t*)str+len2, len-len2);
hillkim7 11:313e091ab3f9 256 } else {
hillkim7 11:313e091ab3f9 257 spi_write(dst, (uint8_t*)str, len);
hillkim7 11:313e091ab3f9 258 }
hillkim7 11:313e091ab3f9 259 sreg<uint16_t>(socket, Sn_TX_WR, ptr + len);
hillkim7 11:313e091ab3f9 260 scmd(socket, SEND);
hillkim7 11:313e091ab3f9 261 return len;
hillkim7 11:313e091ab3f9 262 }
hillkim7 11:313e091ab3f9 263
hillkim7 11:313e091ab3f9 264 int WIZnet_Chip::recv(int socket, char* buf, int len)
hillkim7 11:313e091ab3f9 265 {
hillkim7 11:313e091ab3f9 266 if (socket < 0) {
hillkim7 11:313e091ab3f9 267 return -1;
hillkim7 11:313e091ab3f9 268 }
hillkim7 11:313e091ab3f9 269 uint16_t base = 0xc000 + socket * 0x800;
hillkim7 11:313e091ab3f9 270 uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD);
hillkim7 11:313e091ab3f9 271 uint16_t src = base + (ptr&(0x800-1));
hillkim7 11:313e091ab3f9 272 if ((src + len) > (base+0x800)) {
hillkim7 11:313e091ab3f9 273 int len2 = base + 0x800 - src;
hillkim7 11:313e091ab3f9 274 spi_read(src, (uint8_t*)buf, len2);
hillkim7 11:313e091ab3f9 275 spi_read(base, (uint8_t*)buf+len2, len-len2);
hillkim7 11:313e091ab3f9 276 } else {
hillkim7 11:313e091ab3f9 277 spi_read(src, (uint8_t*)buf, len);
hillkim7 11:313e091ab3f9 278 }
hillkim7 11:313e091ab3f9 279 sreg<uint16_t>(socket, Sn_RX_RD, ptr + len);
hillkim7 11:313e091ab3f9 280 scmd(socket, RECV);
hillkim7 11:313e091ab3f9 281 return len;
hillkim7 11:313e091ab3f9 282 }
hillkim7 11:313e091ab3f9 283
hillkim7 11:313e091ab3f9 284 int WIZnet_Chip::new_socket()
hillkim7 11:313e091ab3f9 285 {
hillkim7 11:313e091ab3f9 286 for(int s = 0; s < 8; s++) {
hillkim7 11:313e091ab3f9 287 if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) {
hillkim7 11:313e091ab3f9 288 return s;
hillkim7 11:313e091ab3f9 289 }
hillkim7 11:313e091ab3f9 290 }
hillkim7 11:313e091ab3f9 291 return -1;
hillkim7 11:313e091ab3f9 292 }
hillkim7 11:313e091ab3f9 293
hillkim7 11:313e091ab3f9 294 uint16_t WIZnet_Chip::new_port()
hillkim7 11:313e091ab3f9 295 {
hillkim7 11:313e091ab3f9 296 uint16_t port = rand();
hillkim7 11:313e091ab3f9 297 port |= 49152;
hillkim7 11:313e091ab3f9 298 return port;
hillkim7 11:313e091ab3f9 299 }
hillkim7 11:313e091ab3f9 300
hillkim7 11:313e091ab3f9 301 void WIZnet_Chip::scmd(int socket, Command cmd)
hillkim7 11:313e091ab3f9 302 {
hillkim7 11:313e091ab3f9 303 sreg<uint8_t>(socket, Sn_CR, cmd);
hillkim7 11:313e091ab3f9 304 while(sreg<uint8_t>(socket, Sn_CR));
hillkim7 11:313e091ab3f9 305 }
hillkim7 11:313e091ab3f9 306
hillkim7 11:313e091ab3f9 307 void WIZnet_Chip::spi_write(uint16_t addr, const uint8_t *buf, uint16_t len)
hillkim7 11:313e091ab3f9 308 {
hillkim7 11:313e091ab3f9 309 cs = 0;
hillkim7 11:313e091ab3f9 310 spi->write(addr >> 8);
hillkim7 11:313e091ab3f9 311 spi->write(addr & 0xff);
hillkim7 11:313e091ab3f9 312 spi->write((0x80 | ((len & 0x7f00) >> 8)));
hillkim7 11:313e091ab3f9 313 spi->write(len & 0xff);
hillkim7 11:313e091ab3f9 314 for(int i = 0; i < len; i++) {
hillkim7 11:313e091ab3f9 315 spi->write(buf[i]);
hillkim7 11:313e091ab3f9 316 }
hillkim7 11:313e091ab3f9 317 cs = 1;
hillkim7 11:313e091ab3f9 318
hillkim7 11:313e091ab3f9 319 #if DBG_SPI
hillkim7 11:313e091ab3f9 320 debug("[SPI]W %04x(%d)", addr, len);
hillkim7 11:313e091ab3f9 321 for(int i = 0; i < len; i++) {
hillkim7 11:313e091ab3f9 322 debug(" %02x", buf[i]);
hillkim7 11:313e091ab3f9 323 if (i > 16) {
hillkim7 11:313e091ab3f9 324 debug(" ...");
hillkim7 11:313e091ab3f9 325 break;
hillkim7 11:313e091ab3f9 326 }
hillkim7 11:313e091ab3f9 327 }
hillkim7 11:313e091ab3f9 328 debug("\r\n");
hillkim7 11:313e091ab3f9 329 #endif
hillkim7 11:313e091ab3f9 330 }
hillkim7 11:313e091ab3f9 331
hillkim7 11:313e091ab3f9 332 void WIZnet_Chip::spi_read(uint16_t addr, uint8_t *buf, uint16_t len)
hillkim7 11:313e091ab3f9 333 {
hillkim7 11:313e091ab3f9 334 cs = 0;
hillkim7 11:313e091ab3f9 335 spi->write(addr >> 8);
hillkim7 11:313e091ab3f9 336 spi->write(addr & 0xff);
hillkim7 11:313e091ab3f9 337 spi->write((0x00 | ((len & 0x7f00) >> 8)));
hillkim7 11:313e091ab3f9 338 spi->write(len & 0xff);
hillkim7 11:313e091ab3f9 339 for(int i = 0; i < len; i++) {
hillkim7 11:313e091ab3f9 340 buf[i] = spi->write(0);
hillkim7 11:313e091ab3f9 341 }
hillkim7 11:313e091ab3f9 342 cs = 1;
hillkim7 11:313e091ab3f9 343
hillkim7 11:313e091ab3f9 344 #if DBG_SPI
hillkim7 11:313e091ab3f9 345 debug("[SPI]R %04x(%d)", addr, len);
hillkim7 11:313e091ab3f9 346 for(int i = 0; i < len; i++) {
hillkim7 11:313e091ab3f9 347 debug(" %02x", buf[i]);
hillkim7 11:313e091ab3f9 348 if (i > 16) {
hillkim7 11:313e091ab3f9 349 debug(" ...");
hillkim7 11:313e091ab3f9 350 break;
hillkim7 11:313e091ab3f9 351 }
hillkim7 11:313e091ab3f9 352 }
hillkim7 11:313e091ab3f9 353 debug("\r\n");
hillkim7 11:313e091ab3f9 354 if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) {
hillkim7 11:313e091ab3f9 355 wait_ms(200);
hillkim7 11:313e091ab3f9 356 }
hillkim7 11:313e091ab3f9 357 #endif
hillkim7 11:313e091ab3f9 358 }
hillkim7 11:313e091ab3f9 359
hillkim7 11:313e091ab3f9 360 uint32_t str_to_ip(const char* str)
hillkim7 11:313e091ab3f9 361 {
hillkim7 11:313e091ab3f9 362 uint32_t ip = 0;
hillkim7 11:313e091ab3f9 363 char* p = (char*)str;
hillkim7 11:313e091ab3f9 364 for(int i = 0; i < 4; i++) {
hillkim7 11:313e091ab3f9 365 ip |= atoi(p);
hillkim7 11:313e091ab3f9 366 p = strchr(p, '.');
hillkim7 11:313e091ab3f9 367 if (p == NULL) {
hillkim7 11:313e091ab3f9 368 break;
hillkim7 11:313e091ab3f9 369 }
hillkim7 11:313e091ab3f9 370 ip <<= 8;
hillkim7 11:313e091ab3f9 371 p++;
hillkim7 11:313e091ab3f9 372 }
hillkim7 11:313e091ab3f9 373 return ip;
hillkim7 11:313e091ab3f9 374 }
hillkim7 11:313e091ab3f9 375
hillkim7 11:313e091ab3f9 376 void printfBytes(char* str, uint8_t* buf, int len)
hillkim7 11:313e091ab3f9 377 {
hillkim7 11:313e091ab3f9 378 printf("%s %d:", str, len);
hillkim7 11:313e091ab3f9 379 for(int i = 0; i < len; i++) {
hillkim7 11:313e091ab3f9 380 printf(" %02x", buf[i]);
hillkim7 11:313e091ab3f9 381 }
hillkim7 11:313e091ab3f9 382 printf("\n");
hillkim7 11:313e091ab3f9 383 }
hillkim7 11:313e091ab3f9 384
hillkim7 11:313e091ab3f9 385 void printHex(uint8_t* buf, int len)
hillkim7 11:313e091ab3f9 386 {
hillkim7 11:313e091ab3f9 387 for(int i = 0; i < len; i++) {
hillkim7 11:313e091ab3f9 388 if ((i%16) == 0) {
hillkim7 11:313e091ab3f9 389 printf("%p", buf+i);
hillkim7 11:313e091ab3f9 390 }
hillkim7 11:313e091ab3f9 391 printf(" %02x", buf[i]);
hillkim7 11:313e091ab3f9 392 if ((i%16) == 15) {
hillkim7 11:313e091ab3f9 393 printf("\n");
hillkim7 11:313e091ab3f9 394 }
hillkim7 11:313e091ab3f9 395 }
hillkim7 11:313e091ab3f9 396 printf("\n");
hillkim7 11:313e091ab3f9 397 }
hillkim7 11:313e091ab3f9 398
hillkim7 11:313e091ab3f9 399 void debug_hex(uint8_t* buf, int len)
hillkim7 11:313e091ab3f9 400 {
hillkim7 11:313e091ab3f9 401 for(int i = 0; i < len; i++) {
hillkim7 11:313e091ab3f9 402 if ((i%16) == 0) {
hillkim7 11:313e091ab3f9 403 debug("%p", buf+i);
hillkim7 11:313e091ab3f9 404 }
hillkim7 11:313e091ab3f9 405 debug(" %02x", buf[i]);
hillkim7 11:313e091ab3f9 406 if ((i%16) == 15) {
hillkim7 11:313e091ab3f9 407 debug("\n");
hillkim7 11:313e091ab3f9 408 }
hillkim7 11:313e091ab3f9 409 }
hillkim7 11:313e091ab3f9 410 debug("\n");
hillkim7 11:313e091ab3f9 411 }
hillkim7 11:313e091ab3f9 412
hillkim7 11:313e091ab3f9 413 #endif
hillkim7 11:313e091ab3f9 414