This is WIZnet Ethernet Interface using Hardware TCP/IP chip, W5500, W5200 and W5100. One of them can be selected by enabling it in wiznet.h.
Fork of WIZnet_Library by
W5100.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 "wiznet.h" 00022 #include "DNSClient.h" 00023 00024 #ifdef USE_W5100 00025 00026 //Debug is disabled by default 00027 #if 0 00028 #define DBG(...) do{debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0); 00029 //#define DBG(x, ...) debug("[WIZnet_Chip:DBG]"x"\r\n", ##__VA_ARGS__); 00030 #define WARN(x, ...) debug("[WIZnet_Chip:WARN]"x"\r\n", ##__VA_ARGS__); 00031 #define ERR(x, ...) debug("[WIZnet_Chip:ERR]"x"\r\n", ##__VA_ARGS__); 00032 #else 00033 #define DBG(x, ...) 00034 #define WARN(x, ...) 00035 #define ERR(x, ...) 00036 #endif 00037 00038 #if 1 00039 #define INFO(x, ...) debug("[WIZnet_Chip:INFO]"x"\r\n", ##__VA_ARGS__); 00040 #else 00041 #define INFO(x, ...) 00042 #endif 00043 00044 #define DBG_SPI 0 00045 00046 WIZnet_Chip* WIZnet_Chip::inst; 00047 00048 WIZnet_Chip::WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName _cs, PinName _reset): 00049 cs(_cs), reset_pin(_reset) 00050 { 00051 spi = new SPI(mosi, miso, sclk); 00052 00053 spi->format(8,0); 00054 spi->frequency(2000000); 00055 00056 cs = 1; 00057 reset_pin = 1; 00058 inst = this; 00059 } 00060 00061 WIZnet_Chip::WIZnet_Chip(SPI* spi, PinName _cs, PinName _reset): 00062 cs(_cs), reset_pin(_reset) 00063 { 00064 this->spi = spi; 00065 00066 this->spi->format(8,0); 00067 this->spi->frequency(2000000); 00068 00069 cs = 1; 00070 reset_pin = 1; 00071 inst = this; 00072 } 00073 00074 bool WIZnet_Chip::setip() 00075 { 00076 reg_wr<uint32_t>(SIPR, ip); 00077 reg_wr<uint32_t>(GAR, gateway); 00078 reg_wr<uint32_t>(SUBR, netmask); 00079 return true; 00080 } 00081 00082 bool WIZnet_Chip::setProtocol(int socket, Protocol p) 00083 { 00084 if (socket < 0) { 00085 return false; 00086 } 00087 sreg<uint8_t>(socket, Sn_MR, p); 00088 return true; 00089 } 00090 00091 bool WIZnet_Chip::connect(int socket, const char * host, int port, int timeout_ms) 00092 { 00093 if (socket < 0) { 00094 return false; 00095 } 00096 sreg<uint8_t>(socket, Sn_MR, TCP); 00097 scmd(socket, OPEN); 00098 sreg_ip(socket, Sn_DIPR, host); 00099 sreg<uint16_t>(socket, Sn_DPORT, port); 00100 sreg<uint16_t>(socket, Sn_PORT, new_port()); 00101 scmd(socket, CONNECT); 00102 Timer t; 00103 t.reset(); 00104 t.start(); 00105 while(!is_connected(socket)) { 00106 if (t.read_ms() > timeout_ms) { 00107 return false; 00108 } 00109 } 00110 return true; 00111 } 00112 00113 bool WIZnet_Chip::gethostbyname(const char* host, uint32_t* ip) 00114 { 00115 uint32_t addr = str_to_ip(host); 00116 char buf[17]; 00117 snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff); 00118 if (strcmp(buf, host) == 0) { 00119 *ip = addr; 00120 return true; 00121 } 00122 DNSClient client; 00123 if(client.lookup(host)) { 00124 *ip = client.ip; 00125 return true; 00126 } 00127 return false; 00128 } 00129 00130 bool WIZnet_Chip::disconnect() 00131 { 00132 return true; 00133 } 00134 00135 bool WIZnet_Chip::is_connected(int socket) 00136 { 00137 if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) { 00138 return true; 00139 } 00140 return false; 00141 } 00142 00143 void WIZnet_Chip::reset() 00144 { 00145 reset_pin = 1; 00146 reset_pin = 0; 00147 wait_us(2); // 2us 00148 reset_pin = 1; 00149 wait_ms(150); // 150ms 00150 00151 reg_wr<uint8_t>(MR, 1<<7); 00152 00153 reg_wr_mac(SHAR, mac); 00154 } 00155 00156 bool WIZnet_Chip::close(int socket) 00157 { 00158 if (socket < 0) { 00159 return false; 00160 } 00161 // if not connected, return 00162 if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) { 00163 return true; 00164 } 00165 00166 scmd(socket, CLOSE); 00167 sreg<uint8_t>(socket, Sn_IR, 0xff); 00168 return true; 00169 } 00170 00171 int WIZnet_Chip::wait_readable(int socket, int wait_time_ms, int req_size) 00172 { 00173 if (socket < 0) { 00174 return -1; 00175 } 00176 Timer t; 00177 t.reset(); 00178 t.start(); 00179 while(1) { 00180 int size = 0; int size1 = 0; 00181 do { 00182 size = sreg<uint16_t>(socket, Sn_RX_RSR); 00183 if (size != 0) size1 = sreg<uint16_t>(socket, Sn_RX_RSR); 00184 }while(size != size1); 00185 00186 if (size > req_size) { 00187 return size; 00188 } 00189 if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { 00190 break; 00191 } 00192 } 00193 return -1; 00194 } 00195 00196 int WIZnet_Chip::wait_writeable(int socket, int wait_time_ms, int req_size) 00197 { 00198 if (socket < 0) { 00199 return -1; 00200 } 00201 Timer t; 00202 t.reset(); 00203 t.start(); 00204 while(1) { 00205 int size = sreg<uint16_t>(socket, Sn_TX_FSR); 00206 if (size > req_size) { 00207 return size; 00208 } 00209 if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { 00210 break; 00211 } 00212 } 00213 return -1; 00214 } 00215 00216 int WIZnet_Chip::send(int socket, const char * str, int len) 00217 { 00218 if (socket < 0) { 00219 return -1; 00220 } 00221 uint16_t base = 0x4000 + socket * 0x800; // each socket has 2K buffer 00222 uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR); 00223 uint16_t dst = base + (ptr&(0x800-1)); 00224 if ((dst + len) > (base+0x800)) { 00225 int len2 = base + 0x800 - dst; 00226 spi_write(dst, (uint8_t*)str, len2); 00227 spi_write(base, (uint8_t*)str+len2, len-len2); 00228 } else { 00229 spi_write(dst, (uint8_t*)str, len); 00230 } 00231 sreg<uint16_t>(socket, Sn_TX_WR, ptr + len); 00232 scmd(socket, SEND); 00233 return len; 00234 } 00235 00236 int WIZnet_Chip::recv(int socket, char* buf, int len) 00237 { 00238 if (socket < 0) { 00239 return -1; 00240 } 00241 uint16_t base = 0x6000 + socket * 0x800; // each socket has 2K buffer 00242 uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD); 00243 uint16_t src = base + (ptr&(0x800-1)); 00244 if ((src + len) > (base+0x800)) { 00245 int len2 = base + 0x800 - src; 00246 spi_read(src, (uint8_t*)buf, len2); 00247 spi_read(base, (uint8_t*)buf+len2, len-len2); 00248 } else { 00249 spi_read(src, (uint8_t*)buf, len); 00250 } 00251 sreg<uint16_t>(socket, Sn_RX_RD, ptr + len); 00252 scmd(socket, RECV); 00253 return len; 00254 } 00255 00256 int WIZnet_Chip::new_socket() 00257 { 00258 for(int s = 0; s < MAX_SOCK_NUM; s++) { 00259 if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) { 00260 return s; 00261 } 00262 } 00263 return -1; 00264 } 00265 00266 uint16_t WIZnet_Chip::new_port() 00267 { 00268 uint16_t port = rand(); 00269 port |= 49152; 00270 return port; 00271 } 00272 00273 void WIZnet_Chip::scmd(int socket, Command cmd) 00274 { 00275 sreg<uint8_t>(socket, Sn_CR, cmd); 00276 while(sreg<uint8_t>(socket, Sn_CR)); 00277 } 00278 00279 void WIZnet_Chip::spi_write(uint16_t addr, const uint8_t *buf, uint16_t len) 00280 { 00281 for(int i = 0; i < len; i++) { 00282 cs = 0; 00283 spi->write(0xf0); 00284 spi->write(addr >> 8); 00285 spi->write(addr & 0xff); 00286 addr++; 00287 spi->write(buf[i]); 00288 cs = 1; 00289 } 00290 #if DBG_SPI 00291 debug("[SPI]W %04x(%d)", addr, len); 00292 for(int i = 0; i < len; i++) { 00293 debug(" %02x", buf[i]); 00294 if (i > 16) { 00295 debug(" ..."); 00296 break; 00297 } 00298 } 00299 debug("\r\n"); 00300 #endif 00301 } 00302 00303 void WIZnet_Chip::spi_read(uint16_t addr, uint8_t *buf, uint16_t len) 00304 { 00305 for(int i = 0; i < len; i++) { 00306 cs = 0; 00307 spi->write(0x0f); 00308 spi->write(addr >> 8); 00309 spi->write(addr & 0xff); 00310 addr++; 00311 buf[i] = spi->write(0); 00312 cs = 1; 00313 } 00314 #if DBG_SPI 00315 debug("[SPI]R %04x(%d)", addr, len); 00316 for(int i = 0; i < len; i++) { 00317 debug(" %02x", buf[i]); 00318 if (i > 16) { 00319 debug(" ..."); 00320 break; 00321 } 00322 } 00323 debug("\r\n"); 00324 if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) { 00325 wait_ms(200); 00326 } 00327 #endif 00328 } 00329 00330 uint32_t str_to_ip(const char* str) 00331 { 00332 uint32_t ip = 0; 00333 char* p = (char*)str; 00334 for(int i = 0; i < 4; i++) { 00335 ip |= atoi(p); 00336 p = strchr(p, '.'); 00337 if (p == NULL) { 00338 break; 00339 } 00340 ip <<= 8; 00341 p++; 00342 } 00343 return ip; 00344 } 00345 00346 void printfBytes(char* str, uint8_t* buf, int len) 00347 { 00348 printf("%s %d:", str, len); 00349 for(int i = 0; i < len; i++) { 00350 printf(" %02x", buf[i]); 00351 } 00352 printf("\n"); 00353 } 00354 00355 void printHex(uint8_t* buf, int len) 00356 { 00357 for(int i = 0; i < len; i++) { 00358 if ((i%16) == 0) { 00359 printf("%p", buf+i); 00360 } 00361 printf(" %02x", buf[i]); 00362 if ((i%16) == 15) { 00363 printf("\n"); 00364 } 00365 } 00366 printf("\n"); 00367 } 00368 00369 void debug_hex(uint8_t* buf, int len) 00370 { 00371 for(int i = 0; i < len; i++) { 00372 if ((i%16) == 0) { 00373 debug("%p", buf+i); 00374 } 00375 debug(" %02x", buf[i]); 00376 if ((i%16) == 15) { 00377 debug("\n"); 00378 } 00379 } 00380 debug("\n"); 00381 } 00382 00383 #endif
Generated on Tue Jul 12 2022 19:24:11 by 1.7.2