Revised to prevent DHCPClient::discover from hanging.
Fork of WIZ820ioInterface by
WIZ820io/WIZ820io.cpp@7:c0cd6680bcb7, 2014-02-02 (annotated)
- Committer:
- ban4jp
- Date:
- Sun Feb 02 14:20:31 2014 +0000
- Revision:
- 7:c0cd6680bcb7
- Parent:
- 5:fb15c35d1e28
- Child:
- 9:c1722862c13b
Fixed compatibility.
Who changed what in which revision?
User | Revision | Line number | New 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 | |
va009039 | 5:fb15c35d1e28 | 72 | bool WIZ820io::setProtocol(int socket, Protocol p) |
va009039 | 5:fb15c35d1e28 | 73 | { |
va009039 | 5:fb15c35d1e28 | 74 | if (socket < 0) { |
va009039 | 5:fb15c35d1e28 | 75 | return false; |
samux | 1:fb4494783863 | 76 | } |
va009039 | 5:fb15c35d1e28 | 77 | sreg<uint8_t>(socket, Sn_MR, p); |
va009039 | 5:fb15c35d1e28 | 78 | return true; |
samux | 1:fb4494783863 | 79 | } |
samux | 1:fb4494783863 | 80 | |
va009039 | 5:fb15c35d1e28 | 81 | bool WIZ820io::connect(int socket, const char * host, int port, int timeout_ms) |
samux | 1:fb4494783863 | 82 | { |
va009039 | 5:fb15c35d1e28 | 83 | if (socket < 0) { |
va009039 | 5:fb15c35d1e28 | 84 | return false; |
va009039 | 5:fb15c35d1e28 | 85 | } |
va009039 | 5:fb15c35d1e28 | 86 | sreg<uint8_t>(socket, Sn_MR, TCP); |
va009039 | 5:fb15c35d1e28 | 87 | scmd(socket, OPEN); |
va009039 | 5:fb15c35d1e28 | 88 | sreg_ip(socket, Sn_DIPR, host); |
va009039 | 5:fb15c35d1e28 | 89 | sreg<uint16_t>(socket, Sn_DPORT, port); |
va009039 | 5:fb15c35d1e28 | 90 | sreg<uint16_t>(socket, Sn_PORT, new_port()); |
va009039 | 5:fb15c35d1e28 | 91 | scmd(socket, CONNECT); |
va009039 | 5:fb15c35d1e28 | 92 | Timer t; |
va009039 | 5:fb15c35d1e28 | 93 | t.reset(); |
va009039 | 5:fb15c35d1e28 | 94 | t.start(); |
va009039 | 5:fb15c35d1e28 | 95 | while(!is_connected(socket)) { |
va009039 | 5:fb15c35d1e28 | 96 | if (t.read_ms() > timeout_ms) { |
va009039 | 5:fb15c35d1e28 | 97 | return false; |
va009039 | 5:fb15c35d1e28 | 98 | } |
va009039 | 5:fb15c35d1e28 | 99 | } |
va009039 | 5:fb15c35d1e28 | 100 | return true; |
va009039 | 5:fb15c35d1e28 | 101 | } |
samux | 1:fb4494783863 | 102 | |
va009039 | 5:fb15c35d1e28 | 103 | bool WIZ820io::gethostbyname(const char* host, uint32_t* ip) |
va009039 | 5:fb15c35d1e28 | 104 | { |
va009039 | 5:fb15c35d1e28 | 105 | uint32_t addr = str_to_ip(host); |
va009039 | 5:fb15c35d1e28 | 106 | char buf[17]; |
va009039 | 5:fb15c35d1e28 | 107 | snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff); |
va009039 | 5:fb15c35d1e28 | 108 | if (strcmp(buf, host) == 0) { |
va009039 | 5:fb15c35d1e28 | 109 | *ip = addr; |
va009039 | 5:fb15c35d1e28 | 110 | return true; |
va009039 | 5:fb15c35d1e28 | 111 | } |
va009039 | 5:fb15c35d1e28 | 112 | DNSClient client; |
va009039 | 5:fb15c35d1e28 | 113 | if(client.lookup(host)) { |
va009039 | 5:fb15c35d1e28 | 114 | *ip = client.ip; |
va009039 | 5:fb15c35d1e28 | 115 | return true; |
va009039 | 5:fb15c35d1e28 | 116 | } |
va009039 | 5:fb15c35d1e28 | 117 | return false; |
va009039 | 5:fb15c35d1e28 | 118 | } |
samux | 1:fb4494783863 | 119 | |
va009039 | 5:fb15c35d1e28 | 120 | bool WIZ820io::disconnect() |
va009039 | 5:fb15c35d1e28 | 121 | { |
va009039 | 5:fb15c35d1e28 | 122 | return true; |
va009039 | 5:fb15c35d1e28 | 123 | } |
samux | 1:fb4494783863 | 124 | |
va009039 | 5:fb15c35d1e28 | 125 | bool WIZ820io::is_connected(int socket) |
va009039 | 5:fb15c35d1e28 | 126 | { |
va009039 | 5:fb15c35d1e28 | 127 | if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) { |
samux | 1:fb4494783863 | 128 | return true; |
samux | 1:fb4494783863 | 129 | } |
samux | 1:fb4494783863 | 130 | return false; |
samux | 1:fb4494783863 | 131 | } |
samux | 1:fb4494783863 | 132 | |
va009039 | 5:fb15c35d1e28 | 133 | void WIZ820io::reset() |
samux | 1:fb4494783863 | 134 | { |
va009039 | 5:fb15c35d1e28 | 135 | reset_pin = 1; |
va009039 | 5:fb15c35d1e28 | 136 | reset_pin = 0; |
va009039 | 5:fb15c35d1e28 | 137 | wait_us(2); // 2us |
va009039 | 5:fb15c35d1e28 | 138 | reset_pin = 1; |
va009039 | 5:fb15c35d1e28 | 139 | wait_ms(150); // 150ms |
ban4jp | 7:c0cd6680bcb7 | 140 | |
va009039 | 5:fb15c35d1e28 | 141 | reg_wr<uint8_t>(MR, 1<<7); |
ban4jp | 7:c0cd6680bcb7 | 142 | |
va009039 | 5:fb15c35d1e28 | 143 | #ifdef TARGET_LPC1114 |
va009039 | 5:fb15c35d1e28 | 144 | uint8_t mac[6] = {0x00,0x02,0xf7,0xf0,0x00,0x00}; |
va009039 | 5:fb15c35d1e28 | 145 | #else |
va009039 | 5:fb15c35d1e28 | 146 | uint8_t mac[6]; |
va009039 | 5:fb15c35d1e28 | 147 | mbed_mac_address((char*)mac); |
va009039 | 5:fb15c35d1e28 | 148 | #endif |
va009039 | 5:fb15c35d1e28 | 149 | reg_wr_mac(SHAR, mac); |
samux | 1:fb4494783863 | 150 | } |
samux | 1:fb4494783863 | 151 | |
va009039 | 5:fb15c35d1e28 | 152 | bool WIZ820io::close(int socket) |
samux | 1:fb4494783863 | 153 | { |
va009039 | 5:fb15c35d1e28 | 154 | if (socket < 0) { |
samux | 1:fb4494783863 | 155 | return false; |
samux | 1:fb4494783863 | 156 | } |
va009039 | 5:fb15c35d1e28 | 157 | // if not connected, return |
va009039 | 5:fb15c35d1e28 | 158 | if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) { |
samux | 1:fb4494783863 | 159 | return true; |
samux | 1:fb4494783863 | 160 | } |
va009039 | 5:fb15c35d1e28 | 161 | if (sreg<uint8_t>(socket, Sn_MR) == TCP) { |
va009039 | 5:fb15c35d1e28 | 162 | scmd(socket, DISCON); |
va009039 | 5:fb15c35d1e28 | 163 | } |
va009039 | 5:fb15c35d1e28 | 164 | scmd(socket, CLOSE); |
ban4jp | 7:c0cd6680bcb7 | 165 | sreg<uint8_t>(socket, Sn_IR, 0xff); |
samux | 1:fb4494783863 | 166 | return true; |
samux | 1:fb4494783863 | 167 | } |
samux | 1:fb4494783863 | 168 | |
va009039 | 5:fb15c35d1e28 | 169 | int WIZ820io::wait_readable(int socket, int wait_time_ms, int req_size) |
samux | 1:fb4494783863 | 170 | { |
va009039 | 5:fb15c35d1e28 | 171 | if (socket < 0) { |
va009039 | 5:fb15c35d1e28 | 172 | return -1; |
va009039 | 5:fb15c35d1e28 | 173 | } |
va009039 | 5:fb15c35d1e28 | 174 | Timer t; |
va009039 | 5:fb15c35d1e28 | 175 | t.reset(); |
va009039 | 5:fb15c35d1e28 | 176 | t.start(); |
va009039 | 5:fb15c35d1e28 | 177 | while(1) { |
va009039 | 5:fb15c35d1e28 | 178 | int size = sreg<uint16_t>(socket, Sn_RX_RSR); |
va009039 | 5:fb15c35d1e28 | 179 | if (size > req_size) { |
va009039 | 5:fb15c35d1e28 | 180 | return size; |
va009039 | 5:fb15c35d1e28 | 181 | } |
va009039 | 5:fb15c35d1e28 | 182 | if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { |
va009039 | 5:fb15c35d1e28 | 183 | break; |
va009039 | 5:fb15c35d1e28 | 184 | } |
va009039 | 5:fb15c35d1e28 | 185 | } |
va009039 | 5:fb15c35d1e28 | 186 | return -1; |
samux | 1:fb4494783863 | 187 | } |
samux | 1:fb4494783863 | 188 | |
va009039 | 5:fb15c35d1e28 | 189 | int WIZ820io::wait_writeable(int socket, int wait_time_ms, int req_size) |
samux | 1:fb4494783863 | 190 | { |
va009039 | 5:fb15c35d1e28 | 191 | if (socket < 0) { |
va009039 | 5:fb15c35d1e28 | 192 | return -1; |
va009039 | 5:fb15c35d1e28 | 193 | } |
va009039 | 5:fb15c35d1e28 | 194 | Timer t; |
va009039 | 5:fb15c35d1e28 | 195 | t.reset(); |
va009039 | 5:fb15c35d1e28 | 196 | t.start(); |
va009039 | 5:fb15c35d1e28 | 197 | while(1) { |
va009039 | 5:fb15c35d1e28 | 198 | int size = sreg<uint16_t>(socket, Sn_TX_FSR); |
va009039 | 5:fb15c35d1e28 | 199 | if (size > req_size) { |
va009039 | 5:fb15c35d1e28 | 200 | return size; |
va009039 | 5:fb15c35d1e28 | 201 | } |
va009039 | 5:fb15c35d1e28 | 202 | if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { |
va009039 | 5:fb15c35d1e28 | 203 | break; |
va009039 | 5:fb15c35d1e28 | 204 | } |
va009039 | 5:fb15c35d1e28 | 205 | } |
va009039 | 5:fb15c35d1e28 | 206 | return -1; |
samux | 3:9aa05e19c62e | 207 | } |
samux | 3:9aa05e19c62e | 208 | |
va009039 | 5:fb15c35d1e28 | 209 | int WIZ820io::send(int socket, const char * str, int len) |
samux | 1:fb4494783863 | 210 | { |
va009039 | 5:fb15c35d1e28 | 211 | if (socket < 0) { |
va009039 | 5:fb15c35d1e28 | 212 | return -1; |
va009039 | 5:fb15c35d1e28 | 213 | } |
va009039 | 5:fb15c35d1e28 | 214 | uint16_t base = 0x8000 + socket * 0x800; |
va009039 | 5:fb15c35d1e28 | 215 | uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR); |
va009039 | 5:fb15c35d1e28 | 216 | uint16_t dst = base + (ptr&(0x800-1)); |
va009039 | 5:fb15c35d1e28 | 217 | if ((dst + len) > (base+0x800)) { |
va009039 | 5:fb15c35d1e28 | 218 | int len2 = base + 0x800 - dst; |
va009039 | 5:fb15c35d1e28 | 219 | spi_write(dst, (uint8_t*)str, len2); |
va009039 | 5:fb15c35d1e28 | 220 | spi_write(base, (uint8_t*)str+len2, len-len2); |
va009039 | 5:fb15c35d1e28 | 221 | } else { |
va009039 | 5:fb15c35d1e28 | 222 | spi_write(dst, (uint8_t*)str, len); |
va009039 | 5:fb15c35d1e28 | 223 | } |
va009039 | 5:fb15c35d1e28 | 224 | sreg<uint16_t>(socket, Sn_TX_WR, ptr + len); |
va009039 | 5:fb15c35d1e28 | 225 | scmd(socket, SEND); |
va009039 | 5:fb15c35d1e28 | 226 | return len; |
samux | 1:fb4494783863 | 227 | } |
samux | 1:fb4494783863 | 228 | |
va009039 | 5:fb15c35d1e28 | 229 | int WIZ820io::recv(int socket, char* buf, int len) |
samux | 1:fb4494783863 | 230 | { |
va009039 | 5:fb15c35d1e28 | 231 | if (socket < 0) { |
va009039 | 5:fb15c35d1e28 | 232 | return -1; |
va009039 | 5:fb15c35d1e28 | 233 | } |
va009039 | 5:fb15c35d1e28 | 234 | uint16_t base = 0xc000 + socket * 0x800; |
va009039 | 5:fb15c35d1e28 | 235 | uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD); |
va009039 | 5:fb15c35d1e28 | 236 | uint16_t src = base + (ptr&(0x800-1)); |
va009039 | 5:fb15c35d1e28 | 237 | if ((src + len) > (base+0x800)) { |
va009039 | 5:fb15c35d1e28 | 238 | int len2 = base + 0x800 - src; |
va009039 | 5:fb15c35d1e28 | 239 | spi_read(src, (uint8_t*)buf, len2); |
va009039 | 5:fb15c35d1e28 | 240 | spi_read(base, (uint8_t*)buf+len2, len-len2); |
va009039 | 5:fb15c35d1e28 | 241 | } else { |
va009039 | 5:fb15c35d1e28 | 242 | spi_read(src, (uint8_t*)buf, len); |
va009039 | 5:fb15c35d1e28 | 243 | } |
va009039 | 5:fb15c35d1e28 | 244 | sreg<uint16_t>(socket, Sn_RX_RD, ptr + len); |
va009039 | 5:fb15c35d1e28 | 245 | scmd(socket, RECV); |
va009039 | 5:fb15c35d1e28 | 246 | return len; |
samux | 1:fb4494783863 | 247 | } |
samux | 1:fb4494783863 | 248 | |
va009039 | 5:fb15c35d1e28 | 249 | int WIZ820io::new_socket() |
samux | 1:fb4494783863 | 250 | { |
va009039 | 5:fb15c35d1e28 | 251 | for(int s = 0; s < 8; s++) { |
va009039 | 5:fb15c35d1e28 | 252 | if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) { |
va009039 | 5:fb15c35d1e28 | 253 | return s; |
va009039 | 5:fb15c35d1e28 | 254 | } |
va009039 | 5:fb15c35d1e28 | 255 | } |
va009039 | 5:fb15c35d1e28 | 256 | return -1; |
samux | 1:fb4494783863 | 257 | } |
samux | 1:fb4494783863 | 258 | |
va009039 | 5:fb15c35d1e28 | 259 | uint16_t WIZ820io::new_port() |
samux | 1:fb4494783863 | 260 | { |
va009039 | 5:fb15c35d1e28 | 261 | uint16_t port = rand(); |
va009039 | 5:fb15c35d1e28 | 262 | port |= 49152; |
va009039 | 5:fb15c35d1e28 | 263 | return port; |
samux | 1:fb4494783863 | 264 | } |
samux | 1:fb4494783863 | 265 | |
va009039 | 5:fb15c35d1e28 | 266 | void WIZ820io::scmd(int socket, Command cmd) |
samux | 1:fb4494783863 | 267 | { |
va009039 | 5:fb15c35d1e28 | 268 | sreg<uint8_t>(socket, Sn_CR, cmd); |
ban4jp | 7:c0cd6680bcb7 | 269 | while(sreg<uint8_t>(socket, Sn_CR)); |
samux | 1:fb4494783863 | 270 | } |
samux | 1:fb4494783863 | 271 | |
va009039 | 5:fb15c35d1e28 | 272 | void WIZ820io::spi_write(uint16_t addr, const uint8_t *buf, uint16_t len) |
samux | 1:fb4494783863 | 273 | { |
va009039 | 5:fb15c35d1e28 | 274 | cs = 0; |
va009039 | 5:fb15c35d1e28 | 275 | spi->write(addr >> 8); |
va009039 | 5:fb15c35d1e28 | 276 | spi->write(addr & 0xff); |
va009039 | 5:fb15c35d1e28 | 277 | spi->write((0x80 | ((len & 0x7f00) >> 8))); |
va009039 | 5:fb15c35d1e28 | 278 | spi->write(len & 0xff); |
va009039 | 5:fb15c35d1e28 | 279 | for(int i = 0; i < len; i++) { |
va009039 | 5:fb15c35d1e28 | 280 | spi->write(buf[i]); |
va009039 | 5:fb15c35d1e28 | 281 | } |
va009039 | 5:fb15c35d1e28 | 282 | cs = 1; |
va009039 | 5:fb15c35d1e28 | 283 | |
va009039 | 5:fb15c35d1e28 | 284 | #if DBG_SPI |
va009039 | 5:fb15c35d1e28 | 285 | debug("[SPI]W %04x(%d)", addr, len); |
va009039 | 5:fb15c35d1e28 | 286 | for(int i = 0; i < len; i++) { |
va009039 | 5:fb15c35d1e28 | 287 | debug(" %02x", buf[i]); |
va009039 | 5:fb15c35d1e28 | 288 | if (i > 16) { |
va009039 | 5:fb15c35d1e28 | 289 | debug(" ..."); |
va009039 | 5:fb15c35d1e28 | 290 | break; |
va009039 | 5:fb15c35d1e28 | 291 | } |
va009039 | 5:fb15c35d1e28 | 292 | } |
va009039 | 5:fb15c35d1e28 | 293 | debug("\r\n"); |
va009039 | 5:fb15c35d1e28 | 294 | #endif |
va009039 | 5:fb15c35d1e28 | 295 | } |
va009039 | 5:fb15c35d1e28 | 296 | |
va009039 | 5:fb15c35d1e28 | 297 | void WIZ820io::spi_read(uint16_t addr, uint8_t *buf, uint16_t len) |
va009039 | 5:fb15c35d1e28 | 298 | { |
va009039 | 5:fb15c35d1e28 | 299 | cs = 0; |
va009039 | 5:fb15c35d1e28 | 300 | spi->write(addr >> 8); |
va009039 | 5:fb15c35d1e28 | 301 | spi->write(addr & 0xff); |
va009039 | 5:fb15c35d1e28 | 302 | spi->write((0x00 | ((len & 0x7f00) >> 8))); |
va009039 | 5:fb15c35d1e28 | 303 | spi->write(len & 0xff); |
va009039 | 5:fb15c35d1e28 | 304 | for(int i = 0; i < len; i++) { |
va009039 | 5:fb15c35d1e28 | 305 | buf[i] = spi->write(0); |
va009039 | 5:fb15c35d1e28 | 306 | } |
va009039 | 5:fb15c35d1e28 | 307 | cs = 1; |
va009039 | 5:fb15c35d1e28 | 308 | |
va009039 | 5:fb15c35d1e28 | 309 | #if DBG_SPI |
va009039 | 5:fb15c35d1e28 | 310 | debug("[SPI]R %04x(%d)", addr, len); |
va009039 | 5:fb15c35d1e28 | 311 | for(int i = 0; i < len; i++) { |
va009039 | 5:fb15c35d1e28 | 312 | debug(" %02x", buf[i]); |
va009039 | 5:fb15c35d1e28 | 313 | if (i > 16) { |
va009039 | 5:fb15c35d1e28 | 314 | debug(" ..."); |
va009039 | 5:fb15c35d1e28 | 315 | break; |
va009039 | 5:fb15c35d1e28 | 316 | } |
va009039 | 5:fb15c35d1e28 | 317 | } |
va009039 | 5:fb15c35d1e28 | 318 | debug("\r\n"); |
va009039 | 5:fb15c35d1e28 | 319 | if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) { |
va009039 | 5:fb15c35d1e28 | 320 | wait_ms(200); |
va009039 | 5:fb15c35d1e28 | 321 | } |
va009039 | 5:fb15c35d1e28 | 322 | #endif |
samux | 1:fb4494783863 | 323 | } |
samux | 1:fb4494783863 | 324 | |
va009039 | 5:fb15c35d1e28 | 325 | uint32_t str_to_ip(const char* str) |
samux | 1:fb4494783863 | 326 | { |
va009039 | 5:fb15c35d1e28 | 327 | uint32_t ip = 0; |
va009039 | 5:fb15c35d1e28 | 328 | char* p = (char*)str; |
va009039 | 5:fb15c35d1e28 | 329 | for(int i = 0; i < 4; i++) { |
va009039 | 5:fb15c35d1e28 | 330 | ip |= atoi(p); |
va009039 | 5:fb15c35d1e28 | 331 | p = strchr(p, '.'); |
va009039 | 5:fb15c35d1e28 | 332 | if (p == NULL) { |
va009039 | 5:fb15c35d1e28 | 333 | break; |
va009039 | 5:fb15c35d1e28 | 334 | } |
va009039 | 5:fb15c35d1e28 | 335 | ip <<= 8; |
va009039 | 5:fb15c35d1e28 | 336 | p++; |
va009039 | 5:fb15c35d1e28 | 337 | } |
va009039 | 5:fb15c35d1e28 | 338 | return ip; |
va009039 | 5:fb15c35d1e28 | 339 | } |
samux | 1:fb4494783863 | 340 | |
va009039 | 5:fb15c35d1e28 | 341 | void printfBytes(char* str, uint8_t* buf, int len) |
va009039 | 5:fb15c35d1e28 | 342 | { |
va009039 | 5:fb15c35d1e28 | 343 | printf("%s %d:", str, len); |
va009039 | 5:fb15c35d1e28 | 344 | for(int i = 0; i < len; i++) { |
va009039 | 5:fb15c35d1e28 | 345 | printf(" %02x", buf[i]); |
va009039 | 5:fb15c35d1e28 | 346 | } |
va009039 | 5:fb15c35d1e28 | 347 | printf("\n"); |
va009039 | 5:fb15c35d1e28 | 348 | } |
samux | 1:fb4494783863 | 349 | |
va009039 | 5:fb15c35d1e28 | 350 | void printHex(uint8_t* buf, int len) |
va009039 | 5:fb15c35d1e28 | 351 | { |
va009039 | 5:fb15c35d1e28 | 352 | for(int i = 0; i < len; i++) { |
va009039 | 5:fb15c35d1e28 | 353 | if ((i%16) == 0) { |
va009039 | 5:fb15c35d1e28 | 354 | printf("%p", buf+i); |
samux | 1:fb4494783863 | 355 | } |
va009039 | 5:fb15c35d1e28 | 356 | printf(" %02x", buf[i]); |
va009039 | 5:fb15c35d1e28 | 357 | if ((i%16) == 15) { |
va009039 | 5:fb15c35d1e28 | 358 | printf("\n"); |
va009039 | 5:fb15c35d1e28 | 359 | } |
samux | 1:fb4494783863 | 360 | } |
va009039 | 5:fb15c35d1e28 | 361 | printf("\n"); |
va009039 | 5:fb15c35d1e28 | 362 | } |
samux | 1:fb4494783863 | 363 | |
va009039 | 5:fb15c35d1e28 | 364 | void debug_hex(uint8_t* buf, int len) |
va009039 | 5:fb15c35d1e28 | 365 | { |
va009039 | 5:fb15c35d1e28 | 366 | for(int i = 0; i < len; i++) { |
va009039 | 5:fb15c35d1e28 | 367 | if ((i%16) == 0) { |
va009039 | 5:fb15c35d1e28 | 368 | debug("%p", buf+i); |
samux | 1:fb4494783863 | 369 | } |
va009039 | 5:fb15c35d1e28 | 370 | debug(" %02x", buf[i]); |
va009039 | 5:fb15c35d1e28 | 371 | if ((i%16) == 15) { |
va009039 | 5:fb15c35d1e28 | 372 | debug("\n"); |
va009039 | 5:fb15c35d1e28 | 373 | } |
samux | 1:fb4494783863 | 374 | } |
va009039 | 5:fb15c35d1e28 | 375 | debug("\n"); |
va009039 | 5:fb15c35d1e28 | 376 | } |