Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of WIZ820ioInterface by
WIZ820io.cpp
00001 /* Copyright (C) 2012 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 #include "mbed.h" 00020 #include "mbed_debug.h" 00021 #include "WIZ820io.h" 00022 #include "DNSClient.h" 00023 00024 //Debug is disabled by default 00025 #if 1 00026 #define DBG(...) do{debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0); 00027 //#define DBG(x, ...) debug("[WIZ820io:DBG]"x"\r\n", ##__VA_ARGS__); 00028 #define WARN(x, ...) debug("[WIZ820io:WARN]"x"\r\n", ##__VA_ARGS__); 00029 #define ERR(x, ...) debug("[WIZ820io:ERR]"x"\r\n", ##__VA_ARGS__); 00030 #else 00031 #define DBG(x, ...) 00032 #define WARN(x, ...) 00033 #define ERR(x, ...) 00034 #endif 00035 00036 #if 1 00037 #define INFO(x, ...) debug("[WIZ820io:INFO]"x"\r\n", ##__VA_ARGS__); 00038 #else 00039 #define INFO(x, ...) 00040 #endif 00041 00042 #define DBG_SPI 1 00043 00044 WIZ820io* WIZ820io::inst; 00045 00046 WIZ820io::WIZ820io(PinName mosi, PinName miso, PinName sclk, PinName _cs, PinName _reset): 00047 cs(_cs), reset_pin(_reset) 00048 { 00049 spi = new SPI(mosi, miso, sclk); 00050 cs = 1; 00051 reset_pin = 1; 00052 inst = this; 00053 } 00054 00055 WIZ820io::WIZ820io(SPI* spi, PinName _cs, PinName _reset): 00056 cs(_cs), reset_pin(_reset) 00057 { 00058 this->spi = spi; 00059 cs = 1; 00060 reset_pin = 1; 00061 inst = this; 00062 } 00063 00064 bool WIZ820io::join() 00065 { 00066 reg_wr<uint32_t>(SIPR, ip); 00067 reg_wr<uint32_t>(GAR, gateway); 00068 reg_wr<uint32_t>(SUBR, netmask); 00069 return true; 00070 } 00071 00072 bool WIZ820io::setProtocol(int socket, Protocol p) 00073 { 00074 if (socket < 0) { 00075 return false; 00076 } 00077 sreg<uint8_t>(socket, Sn_MR, p); 00078 return true; 00079 } 00080 00081 bool WIZ820io::connect(int socket, const char * host, int port, int timeout_ms) 00082 { 00083 if (socket < 0) { 00084 return false; 00085 } 00086 sreg<uint8_t>(socket, Sn_MR, TCP); 00087 scmd(socket, OPEN); 00088 sreg_ip(socket, Sn_DIPR, host); 00089 sreg<uint16_t>(socket, Sn_DPORT, port); 00090 sreg<uint16_t>(socket, Sn_PORT, new_port()); 00091 scmd(socket, CONNECT); 00092 Timer t; 00093 t.reset(); 00094 t.start(); 00095 while(!is_connected(socket)) { 00096 if (t.read_ms() > timeout_ms) { 00097 return false; 00098 } 00099 } 00100 return true; 00101 } 00102 00103 bool WIZ820io::gethostbyname(const char* host, uint32_t* ip) 00104 { 00105 uint32_t addr = str_to_ip(host); 00106 char buf[17]; 00107 snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff); 00108 if (strcmp(buf, host) == 0) { 00109 *ip = addr; 00110 return true; 00111 } 00112 DNSClient client; 00113 if(client.lookup(host)) { 00114 *ip = client.ip; 00115 return true; 00116 } 00117 return false; 00118 } 00119 00120 bool WIZ820io::disconnect() 00121 { 00122 return true; 00123 } 00124 00125 bool WIZ820io::is_connected(int socket) 00126 { 00127 if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) { 00128 return true; 00129 } 00130 return false; 00131 } 00132 00133 void WIZ820io::reset() 00134 { 00135 reset_pin = 1; 00136 reset_pin = 0; 00137 wait_us(2); // 2us 00138 reset_pin = 1; 00139 wait_ms(150); // 150ms 00140 00141 reg_wr<uint8_t>(MR, 1<<7); 00142 00143 #ifdef TARGET_LPC1114 00144 uint8_t mac[6] = {0x00,0x02,0xf7,0xf0,0x00,0x00}; 00145 #else 00146 uint8_t mac[6] = {0x00,0x02,0xf7,0xf0,0x00,0x00}; 00147 //mbed_mac_address((char*)mac); 00148 #endif 00149 printf("Writing MAC...\r\n"); 00150 reg_wr_mac(SHAR, mac); 00151 } 00152 00153 bool WIZ820io::close(int socket) 00154 { 00155 if (socket < 0) { 00156 return false; 00157 } 00158 // if not connected, return 00159 if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) { 00160 return true; 00161 } 00162 if (sreg<uint8_t>(socket, Sn_MR) == TCP) { 00163 scmd(socket, DISCON); 00164 } 00165 scmd(socket, CLOSE); 00166 sreg<uint8_t>(socket, Sn_IR, 0xff); 00167 return true; 00168 } 00169 00170 int WIZ820io::wait_readable(int socket, int wait_time_ms, int req_size) 00171 { 00172 if (socket < 0) { 00173 return -1; 00174 } 00175 Timer t; 00176 t.reset(); 00177 t.start(); 00178 while(1) { 00179 int size = sreg<uint16_t>(socket, Sn_RX_RSR); 00180 if (size > req_size) { 00181 return size; 00182 } 00183 if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { 00184 break; 00185 } 00186 } 00187 return -1; 00188 } 00189 00190 int WIZ820io::wait_writeable(int socket, int wait_time_ms, int req_size) 00191 { 00192 if (socket < 0) { 00193 return -1; 00194 } 00195 Timer t; 00196 t.reset(); 00197 t.start(); 00198 while(1) { 00199 int size = sreg<uint16_t>(socket, Sn_TX_FSR); 00200 if (size > req_size) { 00201 return size; 00202 } 00203 if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { 00204 break; 00205 } 00206 } 00207 return -1; 00208 } 00209 00210 int WIZ820io::send(int socket, const char * str, int len) 00211 { 00212 if (socket < 0) { 00213 return -1; 00214 } 00215 uint16_t base = 0x8000 + socket * 0x800; 00216 uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR); 00217 uint16_t dst = base + (ptr&(0x800-1)); 00218 if ((dst + len) > (base+0x800)) { 00219 int len2 = base + 0x800 - dst; 00220 spi_write(dst, (uint8_t*)str, len2); 00221 spi_write(base, (uint8_t*)str+len2, len-len2); 00222 } else { 00223 spi_write(dst, (uint8_t*)str, len); 00224 } 00225 sreg<uint16_t>(socket, Sn_TX_WR, ptr + len); 00226 scmd(socket, SEND); 00227 return len; 00228 } 00229 00230 int WIZ820io::recv(int socket, char* buf, int len) 00231 { 00232 if (socket < 0) { 00233 return -1; 00234 } 00235 uint16_t base = 0xc000 + socket * 0x800; 00236 uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD); 00237 uint16_t src = base + (ptr&(0x800-1)); 00238 if ((src + len) > (base+0x800)) { 00239 int len2 = base + 0x800 - src; 00240 spi_read(src, (uint8_t*)buf, len2); 00241 spi_read(base, (uint8_t*)buf+len2, len-len2); 00242 } else { 00243 spi_read(src, (uint8_t*)buf, len); 00244 } 00245 sreg<uint16_t>(socket, Sn_RX_RD, ptr + len); 00246 scmd(socket, RECV); 00247 return len; 00248 } 00249 00250 int WIZ820io::new_socket() 00251 { 00252 for(int s = 0; s < 8; s++) { 00253 if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) { 00254 return s; 00255 } 00256 } 00257 return -1; 00258 } 00259 00260 uint16_t WIZ820io::new_port() 00261 { 00262 uint16_t port = rand(); 00263 port |= 49152; 00264 return port; 00265 } 00266 00267 void WIZ820io::scmd(int socket, Command cmd) 00268 { 00269 sreg<uint8_t>(socket, Sn_CR, cmd); 00270 while(sreg<uint8_t>(socket, Sn_CR)); 00271 } 00272 00273 void WIZ820io::spi_write(uint16_t addr, const uint8_t *buf, uint16_t len) 00274 { 00275 cs = 0; 00276 spi->write(addr >> 8); 00277 spi->write(addr & 0xff); 00278 spi->write((0x80 | ((len & 0x7f00) >> 8))); 00279 spi->write(len & 0xff); 00280 for(int i = 0; i < len; i++) { 00281 spi->write(buf[i]); 00282 } 00283 cs = 1; 00284 00285 #if DBG_SPI 00286 debug("[SPI]W %04x(%d)", addr, len); 00287 for(int i = 0; i < len; i++) { 00288 debug(" %02x", buf[i]); 00289 if (i > 16) { 00290 debug(" ..."); 00291 break; 00292 } 00293 } 00294 debug("\r\n"); 00295 #endif 00296 } 00297 00298 void WIZ820io::spi_read(uint16_t addr, uint8_t *buf, uint16_t len) 00299 { 00300 cs = 0; 00301 spi->write(addr >> 8); 00302 spi->write(addr & 0xff); 00303 spi->write((0x00 | ((len & 0x7f00) >> 8))); 00304 spi->write(len & 0xff); 00305 for(int i = 0; i < len; i++) { 00306 buf[i] = spi->write(0); 00307 } 00308 cs = 1; 00309 00310 #if DBG_SPI 00311 debug("[SPI]R %04x(%d)", addr, len); 00312 for(int i = 0; i < len; i++) { 00313 debug(" %02x", buf[i]); 00314 if (i > 16) { 00315 debug(" ..."); 00316 break; 00317 } 00318 } 00319 debug("\r\n"); 00320 if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) { 00321 wait_ms(200); 00322 } 00323 #endif 00324 } 00325 00326 uint32_t str_to_ip(const char* str) 00327 { 00328 uint32_t ip = 0; 00329 char* p = (char*)str; 00330 for(int i = 0; i < 4; i++) { 00331 ip |= atoi(p); 00332 p = strchr(p, '.'); 00333 if (p == NULL) { 00334 break; 00335 } 00336 ip <<= 8; 00337 p++; 00338 } 00339 return ip; 00340 } 00341 00342 void printfBytes(char* str, uint8_t* buf, int len) 00343 { 00344 printf("%s %d:", str, len); 00345 for(int i = 0; i < len; i++) { 00346 printf(" %02x", buf[i]); 00347 } 00348 printf("\n"); 00349 } 00350 00351 void printHex(uint8_t* buf, int len) 00352 { 00353 for(int i = 0; i < len; i++) { 00354 if ((i%16) == 0) { 00355 printf("%p", buf+i); 00356 } 00357 printf(" %02x", buf[i]); 00358 if ((i%16) == 15) { 00359 printf("\n"); 00360 } 00361 } 00362 printf("\n"); 00363 } 00364 00365 void debug_hex(uint8_t* buf, int len) 00366 { 00367 for(int i = 0; i < len; i++) { 00368 if ((i%16) == 0) { 00369 debug("%p", buf+i); 00370 } 00371 debug(" %02x", buf[i]); 00372 if ((i%16) == 15) { 00373 debug("\n"); 00374 } 00375 } 00376 debug("\n"); 00377 }
Generated on Thu Jul 14 2022 10:39:51 by
