No more update~~ please use W5500Interface.

Fork of EthernetInterfaceW5500 by Bongjun Hur

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers W5500.cpp Source File

W5500.cpp

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