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