11111

Dependents:   espyun1

Fork of WIZnetInterface by WIZnet

Committer:
Soohwan Kim
Date:
Mon Jun 15 11:18:37 2015 +0900
Revision:
0:6f28332c466f
Child:
3:f8c6efc8bf83
initial version

Who changed what in which revision?

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