no upgrade or change at this. move to new Library for WIZ550io, W5500 -> http://mbed.org/teams/EthernetInterfaceW5500-makers/code/W5500Interface/

Dependents:   LPC11U68_NTPClient_HelloWorld_WIZ550io

Fork of WIZ550ioInterface by ban4jp -

please get the new Library for WIZ550io, W5500 (WIZnet) http://mbed.org/teams/EthernetInterfaceW5500-makers/code/W5500Interface/

Import libraryW5500Interface

This is the Interface library for WIZnet W5500 chip which forked of EthernetInterfaceW5500, WIZnetInterface and WIZ550ioInterface. This library has simple name as "W5500Interface". and can be used for Wiz550io users also.

Committer:
Bongjun
Date:
Tue Jul 08 03:49:33 2014 +0000
Revision:
11:5a5a3f373a6b
Parent:
10:4da8370d14da
update some code after comparing WIZnet Library

Who changed what in which revision?

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