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 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 bool WIZnet_Chip::is_fin_received(int socket) 00144 { 00145 uint8_t tmpSn_SR; 00146 tmpSn_SR = sreg<uint8_t>(socket, Sn_SR); 00147 // packet sending is possible, when state is SOCK_CLOSE_WAIT. 00148 if (tmpSn_SR == SOCK_CLOSE_WAIT) { 00149 return true; 00150 } 00151 return false; 00152 } 00153 00154 void WIZnet_Chip::reset() 00155 { 00156 reset_pin = 1; 00157 reset_pin = 0; 00158 wait_us(2); // 2us 00159 reset_pin = 1; 00160 wait_ms(150); // 150ms 00161 00162 reg_wr<uint8_t>(MR, 1<<7); 00163 00164 reg_wr_mac(SHAR, mac); 00165 } 00166 00167 bool WIZnet_Chip::close(int socket) 00168 { 00169 if (socket < 0) { 00170 return false; 00171 } 00172 // if not connected, return 00173 if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) { 00174 return true; 00175 } 00176 00177 scmd(socket, CLOSE); 00178 sreg<uint8_t>(socket, Sn_IR, 0xff); 00179 return true; 00180 } 00181 00182 int WIZnet_Chip::wait_readable(int socket, int wait_time_ms, int req_size) 00183 { 00184 if (socket < 0) { 00185 return -1; 00186 } 00187 Timer t; 00188 t.reset(); 00189 t.start(); 00190 while(1) { 00191 int size = 0; int size1 = 0; 00192 do { 00193 size = sreg<uint16_t>(socket, Sn_RX_RSR); 00194 if (size != 0) size1 = sreg<uint16_t>(socket, Sn_RX_RSR); 00195 }while(size != size1); 00196 00197 if (size > req_size) { 00198 return size; 00199 } 00200 if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { 00201 break; 00202 } 00203 } 00204 return -1; 00205 } 00206 00207 int WIZnet_Chip::wait_writeable(int socket, int wait_time_ms, int req_size) 00208 { 00209 if (socket < 0) { 00210 return -1; 00211 } 00212 Timer t; 00213 t.reset(); 00214 t.start(); 00215 while(1) { 00216 int size = sreg<uint16_t>(socket, Sn_TX_FSR); 00217 if (size > req_size) { 00218 return size; 00219 } 00220 if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { 00221 break; 00222 } 00223 } 00224 return -1; 00225 } 00226 00227 int WIZnet_Chip::send(int socket, const char * str, int len) 00228 { 00229 if (socket < 0) { 00230 return -1; 00231 } 00232 uint16_t base = 0x4000 + socket * 0x800; // each socket has 2K buffer 00233 uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR); 00234 uint16_t dst = base + (ptr&(0x800-1)); 00235 if ((dst + len) > (base+0x800)) { 00236 int len2 = base + 0x800 - dst; 00237 spi_write(dst, (uint8_t*)str, len2); 00238 spi_write(base, (uint8_t*)str+len2, len-len2); 00239 } else { 00240 spi_write(dst, (uint8_t*)str, len); 00241 } 00242 sreg<uint16_t>(socket, Sn_TX_WR, ptr + len); 00243 scmd(socket, SEND); 00244 return len; 00245 } 00246 00247 int WIZnet_Chip::recv(int socket, char* buf, int len) 00248 { 00249 if (socket < 0) { 00250 return -1; 00251 } 00252 uint16_t base = 0x6000 + socket * 0x800; // each socket has 2K buffer 00253 uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD); 00254 uint16_t src = base + (ptr&(0x800-1)); 00255 if ((src + len) > (base+0x800)) { 00256 int len2 = base + 0x800 - src; 00257 spi_read(src, (uint8_t*)buf, len2); 00258 spi_read(base, (uint8_t*)buf+len2, len-len2); 00259 } else { 00260 spi_read(src, (uint8_t*)buf, len); 00261 } 00262 sreg<uint16_t>(socket, Sn_RX_RD, ptr + len); 00263 scmd(socket, RECV); 00264 return len; 00265 } 00266 00267 int WIZnet_Chip::new_socket() 00268 { 00269 for(int s = 0; s < MAX_SOCK_NUM; s++) { 00270 if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) { 00271 return s; 00272 } 00273 } 00274 return -1; 00275 } 00276 00277 uint16_t WIZnet_Chip::new_port() 00278 { 00279 uint16_t port = rand(); 00280 port |= 49152; 00281 return port; 00282 } 00283 00284 void WIZnet_Chip::scmd(int socket, Command cmd) 00285 { 00286 sreg<uint8_t>(socket, Sn_CR, cmd); 00287 while(sreg<uint8_t>(socket, Sn_CR)); 00288 } 00289 00290 void WIZnet_Chip::spi_write(uint16_t addr, const uint8_t *buf, uint16_t len) 00291 { 00292 for(int i = 0; i < len; i++) { 00293 cs = 0; 00294 spi->write(0xf0); 00295 spi->write(addr >> 8); 00296 spi->write(addr & 0xff); 00297 addr++; 00298 spi->write(buf[i]); 00299 cs = 1; 00300 } 00301 #if DBG_SPI 00302 debug("[SPI]W %04x(%d)", addr, len); 00303 for(int i = 0; i < len; i++) { 00304 debug(" %02x", buf[i]); 00305 if (i > 16) { 00306 debug(" ..."); 00307 break; 00308 } 00309 } 00310 debug("\r\n"); 00311 #endif 00312 } 00313 00314 void WIZnet_Chip::spi_read(uint16_t addr, uint8_t *buf, uint16_t len) 00315 { 00316 for(int i = 0; i < len; i++) { 00317 cs = 0; 00318 spi->write(0x0f); 00319 spi->write(addr >> 8); 00320 spi->write(addr & 0xff); 00321 addr++; 00322 buf[i] = spi->write(0); 00323 cs = 1; 00324 } 00325 #if DBG_SPI 00326 debug("[SPI]R %04x(%d)", addr, len); 00327 for(int i = 0; i < len; i++) { 00328 debug(" %02x", buf[i]); 00329 if (i > 16) { 00330 debug(" ..."); 00331 break; 00332 } 00333 } 00334 debug("\r\n"); 00335 if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) { 00336 wait_ms(200); 00337 } 00338 #endif 00339 } 00340 00341 uint32_t str_to_ip(const char* str) 00342 { 00343 uint32_t ip = 0; 00344 char* p = (char*)str; 00345 for(int i = 0; i < 4; i++) { 00346 ip |= atoi(p); 00347 p = strchr(p, '.'); 00348 if (p == NULL) { 00349 break; 00350 } 00351 ip <<= 8; 00352 p++; 00353 } 00354 return ip; 00355 } 00356 00357 void printfBytes(char* str, uint8_t* buf, int len) 00358 { 00359 printf("%s %d:", str, len); 00360 for(int i = 0; i < len; i++) { 00361 printf(" %02x", buf[i]); 00362 } 00363 printf("\n"); 00364 } 00365 00366 void printHex(uint8_t* buf, int len) 00367 { 00368 for(int i = 0; i < len; i++) { 00369 if ((i%16) == 0) { 00370 printf("%p", buf+i); 00371 } 00372 printf(" %02x", buf[i]); 00373 if ((i%16) == 15) { 00374 printf("\n"); 00375 } 00376 } 00377 printf("\n"); 00378 } 00379 00380 void debug_hex(uint8_t* buf, int len) 00381 { 00382 for(int i = 0; i < len; i++) { 00383 if ((i%16) == 0) { 00384 debug("%p", buf+i); 00385 } 00386 debug(" %02x", buf[i]); 00387 if ((i%16) == 15) { 00388 debug("\n"); 00389 } 00390 } 00391 debug("\n"); 00392 } 00393 00394 #endif
Generated on Thu Jul 14 2022 08:03:44 by
