W5500 from SeeedStudio on NUCLEO-L476RG
Dependents: coap-example Borsch coap-example
Fork of W5500Interface by
W5500.cpp
00001 00002 #include "mbed.h" 00003 #include "mbed_debug.h" 00004 #include "wiznet.h" 00005 00006 #ifdef USE_W5500 00007 00008 //Debug is disabled by default 00009 #if 0 00010 #define DBG(...) do{debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0); 00011 //#define DBG(x, ...) debug("[W5500:DBG]"x"\r\n", ##__VA_ARGS__); 00012 #define WARN(x, ...) debug("[W5500:WARN]"x"\r\n", ##__VA_ARGS__); 00013 #define ERR(x, ...) debug("[W5500:ERR]"x"\r\n", ##__VA_ARGS__); 00014 #else 00015 #define DBG(x, ...) 00016 #define WARN(x, ...) 00017 #define ERR(x, ...) 00018 #endif 00019 00020 #if 1 00021 #define INFO(x, ...) debug("[W5500:INFO]"x"\r\n", ##__VA_ARGS__); 00022 #else 00023 #define INFO(x, ...) 00024 #endif 00025 00026 #define DBG_SPI 0 00027 00028 WIZnet_Chip* WIZnet_Chip::inst; 00029 00030 WIZnet_Chip::WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName _cs, PinName _reset): 00031 cs(_cs), reset_pin(_reset) 00032 { 00033 spi = new SPI(mosi, miso, sclk); 00034 cs = 1; 00035 reset_pin = 1; 00036 inst = this; 00037 dhcp = false; 00038 } 00039 00040 WIZnet_Chip::WIZnet_Chip(SPI* spi, PinName _cs, PinName _reset): 00041 cs(_cs), reset_pin(_reset) 00042 { 00043 this->spi = spi; 00044 cs = 1; 00045 reset_pin = 1; 00046 inst = this; 00047 dhcp = false; 00048 } 00049 00050 bool WIZnet_Chip::setmac() 00051 { 00052 reg_wr_mac(SHAR, mac); 00053 return true; 00054 } 00055 00056 // Set the IP 00057 bool WIZnet_Chip::setip() 00058 { 00059 reg_wr<uint32_t>(SIPR, ip); 00060 reg_wr<uint32_t>(GAR, gateway); 00061 reg_wr<uint32_t>(SUBR, netmask); 00062 return true; 00063 } 00064 00065 bool WIZnet_Chip::setProtocol(int socket, Protocol p) 00066 { 00067 if (socket < 0) { 00068 return false; 00069 } 00070 sreg<uint8_t>(socket, Sn_MR, p); 00071 return true; 00072 } 00073 00074 bool WIZnet_Chip::connect(int socket, const char * host, int port, int timeout_ms) 00075 { 00076 if (socket < 0) { 00077 return false; 00078 } 00079 sreg<uint8_t>(socket, Sn_MR, TCP); 00080 scmd(socket, OPEN); 00081 sreg_ip(socket, Sn_DIPR, host); 00082 sreg<uint16_t>(socket, Sn_DPORT, port); 00083 sreg<uint16_t>(socket, Sn_PORT, new_port()); 00084 scmd(socket, CONNECT); 00085 Timer t; 00086 t.reset(); 00087 t.start(); 00088 while(!is_connected(socket)) { 00089 if (t.read_ms() > timeout_ms) { 00090 return false; 00091 } 00092 } 00093 return true; 00094 } 00095 00096 bool WIZnet_Chip::gethostbyname(const char* host, uint32_t* ip) 00097 { 00098 #if 0 00099 uint32_t addr = str_to_ip(host); 00100 char buf[17]; 00101 snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff); 00102 if (strcmp(buf, host) == 0) { 00103 *ip = addr; 00104 return true; 00105 } 00106 DNSClient client; 00107 if(client.lookup(host)) { 00108 *ip = client.ip; 00109 return true; 00110 } 00111 #endif 00112 return false; 00113 } 00114 00115 bool WIZnet_Chip::disconnect() 00116 { 00117 return true; 00118 } 00119 00120 bool WIZnet_Chip::is_connected(int socket) 00121 { 00122 uint8_t tmpSn_SR; 00123 tmpSn_SR = sreg<uint8_t>(socket, Sn_SR); 00124 // packet sending is possible, when state is SOCK_CLOSE_WAIT. 00125 if ((tmpSn_SR == SOCK_ESTABLISHED) || (tmpSn_SR == SOCK_CLOSE_WAIT)) { 00126 return true; 00127 } 00128 return false; 00129 } 00130 00131 // Reset the chip & set the buffer 00132 void WIZnet_Chip::reset() 00133 { 00134 reset_pin = 1; 00135 reset_pin = 0; 00136 wait_us(500); // 500us (w5500) 00137 reset_pin = 1; 00138 wait_ms(400); // 400ms (w5500) 00139 00140 #if defined(USE_WIZ550IO_MAC) 00141 //reg_rd_mac(SHAR, mac); // read the MAC address inside the module 00142 #endif 00143 00144 //reg_wr_mac(SHAR, mac); 00145 00146 // set RX and TX buffer size 00147 for (int socket = 0; socket < MAX_SOCK_NUM; socket++) { 00148 sreg<uint8_t>(socket, Sn_RXBUF_SIZE, 2); 00149 sreg<uint8_t>(socket, Sn_TXBUF_SIZE, 2); 00150 } 00151 00152 reg_rd_mac(SHAR, mac); // read the MAC address inside the module 00153 } 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 if (sreg<uint8_t>(socket, Sn_MR) == TCP) { 00166 scmd(socket, DISCON); 00167 } 00168 scmd(socket, CLOSE); 00169 sreg<uint8_t>(socket, Sn_IR, 0xff); 00170 return true; 00171 } 00172 00173 bool WIZnet_Chip::is_closed(int socket) 00174 { 00175 if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) { 00176 return true; 00177 } 00178 return false; 00179 } 00180 00181 int WIZnet_Chip::wait_readable(int socket, int wait_time_ms, int req_size) 00182 { 00183 if (socket < 0) { 00184 return -1; 00185 } 00186 00187 Timer t; 00188 t.reset(); 00189 t.start(); 00190 while(1) { 00191 //int size = sreg<uint16_t>(socket, Sn_RX_RSR); 00192 int size1, size2; 00193 // during the reading Sn_RX_RSR, it has the possible change of this register. 00194 // so read twice and get same value then use size information. 00195 while (1) { 00196 size1 = sreg<uint16_t>(socket, Sn_RX_RSR); 00197 size2 = sreg<uint16_t>(socket, Sn_RX_RSR); 00198 00199 if (size1 == size2) { 00200 break; 00201 } 00202 00203 if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { 00204 return NSAPI_ERROR_WOULD_BLOCK; 00205 } 00206 00207 if (!is_connected(socket)) { 00208 return -1; 00209 } 00210 } 00211 00212 if (size1 > req_size) { 00213 return size1; 00214 } 00215 if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { 00216 break; 00217 } 00218 } 00219 return NSAPI_ERROR_WOULD_BLOCK; 00220 } 00221 00222 int WIZnet_Chip::wait_writeable(int socket, int wait_time_ms, int req_size) 00223 { 00224 if (socket < 0) { 00225 return -1; 00226 } 00227 Timer t; 00228 t.reset(); 00229 t.start(); 00230 while(1) { 00231 //int size = sreg<uint16_t>(socket, Sn_TX_FSR); 00232 int size1, size2; 00233 // during the reading Sn_TX_FSR, it has the possible change of this register. 00234 // so read twice and get same value then use size information. 00235 do { 00236 size1 = sreg<uint16_t>(socket, Sn_TX_FSR); 00237 size2 = sreg<uint16_t>(socket, Sn_TX_FSR); 00238 if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { 00239 return NSAPI_ERROR_WOULD_BLOCK; 00240 } 00241 } while (size1 != size2); 00242 if (size1 > req_size) { 00243 return size1; 00244 } 00245 if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) { 00246 break; 00247 } 00248 } 00249 return NSAPI_ERROR_WOULD_BLOCK; 00250 } 00251 00252 int WIZnet_Chip::send(int socket, const char * str, int len) 00253 { 00254 if (socket < 0) { 00255 return -1; 00256 } 00257 uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR); 00258 uint8_t cntl_byte = (0x14 + (socket << 5)); 00259 spi_write(ptr, cntl_byte, (uint8_t*)str, len); 00260 sreg<uint16_t>(socket, Sn_TX_WR, ptr + len); 00261 scmd(socket, SEND); 00262 uint8_t tmp_Sn_IR; 00263 while (( (tmp_Sn_IR = sreg<uint8_t>(socket, Sn_IR)) & INT_SEND_OK) != INT_SEND_OK) { 00264 // @Jul.10, 2014 fix contant name, and udp sendto function. 00265 switch (sreg<uint8_t>(socket, Sn_SR)) { 00266 case SOCK_CLOSED : 00267 close(socket); 00268 return 0; 00269 //break; 00270 case SOCK_UDP : 00271 // ARP timeout is possible. 00272 if ((tmp_Sn_IR & INT_TIMEOUT) == INT_TIMEOUT) { 00273 sreg<uint8_t>(socket, Sn_IR, INT_TIMEOUT); 00274 return 0; 00275 } 00276 break; 00277 default : 00278 break; 00279 } 00280 } 00281 sreg<uint8_t>(socket, Sn_IR, INT_SEND_OK); 00282 00283 return len; 00284 } 00285 00286 int WIZnet_Chip::recv(int socket, char* buf, int len) 00287 { 00288 if (socket < 0) { 00289 return -1; 00290 } 00291 uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD); 00292 uint8_t cntl_byte = (0x18 + (socket << 5)); 00293 spi_read(ptr, cntl_byte, (uint8_t*)buf, len); 00294 sreg<uint16_t>(socket, Sn_RX_RD, ptr + len); 00295 scmd(socket, RECV); 00296 return len; 00297 } 00298 00299 int WIZnet_Chip::new_socket() 00300 { 00301 for(int s = 0; s < MAX_SOCK_NUM; s++) { 00302 if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) { 00303 return s; 00304 } 00305 } 00306 return -1; 00307 } 00308 00309 uint16_t WIZnet_Chip::new_port() 00310 { 00311 uint16_t port = rand(); 00312 port |= 49152; 00313 return port; 00314 } 00315 00316 void WIZnet_Chip::scmd(int socket, Command cmd) 00317 { 00318 sreg<uint8_t>(socket, Sn_CR, cmd); 00319 while(sreg<uint8_t>(socket, Sn_CR)); 00320 } 00321 00322 void WIZnet_Chip::spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len) 00323 { 00324 spi->lock(); 00325 cs = 0; 00326 spi->write(addr >> 8); 00327 spi->write(addr & 0xff); 00328 spi->write(cb); 00329 for(int i = 0; i < len; i++) { 00330 spi->write(buf[i]); 00331 } 00332 cs = 1; 00333 00334 #if DBG_SPI 00335 debug("[SPI]W %04x(%02x %d)", addr, cb, len); 00336 for(int i = 0; i < len; i++) { 00337 debug(" %02x", buf[i]); 00338 if (i > 16) { 00339 debug(" ..."); 00340 break; 00341 } 00342 } 00343 debug("\r\n"); 00344 #endif 00345 spi->unlock(); 00346 00347 } 00348 00349 void WIZnet_Chip::spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len) 00350 { 00351 spi->lock(); 00352 cs = 0; 00353 spi->write(addr >> 8); 00354 spi->write(addr & 0xff); 00355 spi->write(cb); 00356 for(int i = 0; i < len; i++) { 00357 buf[i] = spi->write(0); 00358 } 00359 cs = 1; 00360 00361 #if DBG_SPI 00362 debug("[SPI]R %04x(%02x %d)", addr, cb, len); 00363 for(int i = 0; i < len; i++) { 00364 debug(" %02x", buf[i]); 00365 if (i > 16) { 00366 debug(" ..."); 00367 break; 00368 } 00369 } 00370 debug("\r\n"); 00371 if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) { 00372 wait_ms(200); 00373 } 00374 #endif 00375 spi->unlock(); 00376 } 00377 00378 uint32_t str_to_ip(const char* str) 00379 { 00380 uint32_t ip = 0; 00381 char* p = (char*)str; 00382 for(int i = 0; i < 4; i++) { 00383 ip |= atoi(p); 00384 p = strchr(p, '.'); 00385 if (p == NULL) { 00386 break; 00387 } 00388 ip <<= 8; 00389 p++; 00390 } 00391 return ip; 00392 } 00393 00394 void printfBytes(char* str, uint8_t* buf, int len) 00395 { 00396 printf("%s %d:", str, len); 00397 for(int i = 0; i < len; i++) { 00398 printf(" %02x", buf[i]); 00399 } 00400 printf("\n"); 00401 } 00402 00403 void printHex(uint8_t* buf, int len) 00404 { 00405 for(int i = 0; i < len; i++) { 00406 if ((i%16) == 0) { 00407 printf("%p", buf+i); 00408 } 00409 printf(" %02x", buf[i]); 00410 if ((i%16) == 15) { 00411 printf("\n"); 00412 } 00413 } 00414 printf("\n"); 00415 } 00416 00417 void debug_hex(uint8_t* buf, int len) 00418 { 00419 for(int i = 0; i < len; i++) { 00420 if ((i%16) == 0) { 00421 debug("%p", buf+i); 00422 } 00423 debug(" %02x", buf[i]); 00424 if ((i%16) == 15) { 00425 debug("\n"); 00426 } 00427 } 00428 debug("\n"); 00429 } 00430 00431 #endif
Generated on Fri Jul 15 2022 13:32:25 by 1.7.2