Hill Kim / W5200Interface

Dependents:   IBMIoTClientEthernetExample_W5200

Fork of W5500Interface by W5500-Ethernet-Interface Makers

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::linkstatus()
00067 {
00068     if ( (reg_rd<uint8_t>(PHYCFGR) & 0x01) != 0x01 )
00069         return false;
00070 
00071     return true;
00072 }
00073 
00074 
00075 bool WIZnet_Chip::setProtocol(int socket, Protocol p)
00076 {
00077     if (socket < 0) {
00078         return false;
00079     }
00080     sreg<uint8_t>(socket, Sn_MR, p);
00081     return true;
00082 }
00083 
00084 bool WIZnet_Chip::connect(int socket, const char * host, int port, int timeout_ms)
00085 {
00086     if (socket < 0) {
00087         return false;
00088     }
00089     sreg<uint8_t>(socket, Sn_MR, TCP);
00090     scmd(socket, OPEN);
00091     sreg_ip(socket, Sn_DIPR, host);
00092     sreg<uint16_t>(socket, Sn_DPORT, port);
00093     sreg<uint16_t>(socket, Sn_PORT, new_port());
00094     scmd(socket, CONNECT);
00095     Timer t;
00096     t.reset();
00097     t.start();
00098     while(!is_connected(socket)) {
00099         if (t.read_ms() > timeout_ms) {
00100             return false;
00101         }
00102     }
00103     return true;
00104 }
00105 
00106 bool WIZnet_Chip::gethostbyname(const char* host, uint32_t* ip)
00107 {
00108     uint32_t addr = str_to_ip(host);
00109     char buf[17];
00110     snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
00111     if (strcmp(buf, host) == 0) {
00112         *ip = addr;
00113         return true;
00114     }
00115     DNSClient client;
00116     if(client.lookup(host)) {
00117         *ip = client.ip;
00118         return true;
00119     }
00120     return false;
00121 }
00122 
00123 bool WIZnet_Chip::disconnect()
00124 {
00125     return true;
00126 }
00127 
00128 bool WIZnet_Chip::is_connected(int socket)
00129 {
00130     uint8_t tmpSn_SR;
00131     tmpSn_SR = sreg<uint8_t>(socket, Sn_SR);
00132     // packet sending is possible, when state is SOCK_CLOSE_WAIT.
00133     if ((tmpSn_SR == SOCK_ESTABLISHED) || (tmpSn_SR == SOCK_CLOSE_WAIT)) {
00134         return true;
00135     }
00136     return false;
00137 }
00138 
00139 // Reset the chip & set the buffer
00140 void WIZnet_Chip::reset()
00141 {
00142     reset_pin = 1;
00143     reset_pin = 0;
00144     wait_us(500); // 500us (w5500)
00145     reset_pin = 1;
00146     wait_ms(400); // 400ms (w5500)
00147 
00148 #if defined(USE_WIZ550IO_MAC)
00149     //reg_rd_mac(SHAR, mac); // read the MAC address inside the module
00150 #endif
00151 
00152     //reg_wr_mac(SHAR, mac);
00153 
00154     // set RX and TX buffer size
00155     for (int socket = 0; socket < MAX_SOCK_NUM; socket++) {
00156         sreg<uint8_t>(socket, Sn_RXBUF_SIZE, 2);
00157         sreg<uint8_t>(socket, Sn_TXBUF_SIZE, 2);
00158     }
00159 }
00160 
00161 
00162 bool WIZnet_Chip::close(int socket)
00163 {
00164     if (socket < 0) {
00165         return false;
00166     }
00167     // if not connected, return
00168     if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) {
00169         return true;
00170     }
00171     if (sreg<uint8_t>(socket, Sn_MR) == TCP) {
00172         scmd(socket, DISCON);
00173     }
00174     scmd(socket, CLOSE);
00175     sreg<uint8_t>(socket, Sn_IR, 0xff);
00176     return true;
00177 }
00178 
00179 int WIZnet_Chip::wait_readable(int socket, int wait_time_ms, int req_size)
00180 {
00181     if (socket < 0) {
00182         return -1;
00183     }
00184     Timer t;
00185     t.reset();
00186     t.start();
00187     while(1) {
00188         //int size = sreg<uint16_t>(socket, Sn_RX_RSR);
00189         int size, size2;
00190         // during the reading Sn_RX_RSR, it has the possible change of this register.
00191         // so read twice and get same value then use size information.
00192         do {
00193             size = sreg<uint16_t>(socket, Sn_RX_RSR);
00194             size2 = sreg<uint16_t>(socket, Sn_RX_RSR);
00195         } while (size != size2);
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         int size, size2;
00218         // during the reading Sn_TX_FSR, it has the possible change of this register.
00219         // so read twice and get same value then use size information.
00220         do {
00221             size = sreg<uint16_t>(socket, Sn_TX_FSR);
00222             size2 = sreg<uint16_t>(socket, Sn_TX_FSR);
00223         } while (size != size2);
00224         if (size > req_size) {
00225             return size;
00226         }
00227         if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
00228             break;
00229         }
00230     }
00231     return -1;
00232 }
00233 
00234 int WIZnet_Chip::send(int socket, const char * str, int len)
00235 {
00236     if (socket < 0) {
00237         return -1;
00238     }
00239     uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR);
00240     uint8_t cntl_byte = (0x14 + (socket << 5));
00241     spi_write(ptr, cntl_byte, (uint8_t*)str, len);
00242     sreg<uint16_t>(socket, Sn_TX_WR, ptr + len);
00243     scmd(socket, SEND);
00244     uint8_t tmp_Sn_IR;
00245     while (( (tmp_Sn_IR = sreg<uint8_t>(socket, Sn_IR)) & INT_SEND_OK) != INT_SEND_OK) {
00246         // @Jul.10, 2014 fix contant name, and udp sendto function.
00247         switch (sreg<uint8_t>(socket, Sn_SR)) {
00248             case SOCK_CLOSED :
00249                 close(socket);
00250                 return 0;
00251                 //break;
00252             case SOCK_UDP :
00253                 // ARP timeout is possible.
00254                 if ((tmp_Sn_IR & INT_TIMEOUT) == INT_TIMEOUT) {
00255                     sreg<uint8_t>(socket, Sn_IR, INT_TIMEOUT);
00256                     return 0;
00257                 }
00258                 break;
00259             default :
00260                 break;
00261         }
00262     }
00263     sreg<uint8_t>(socket, Sn_IR, INT_SEND_OK);
00264 
00265     return len;
00266 }
00267 
00268 int WIZnet_Chip::recv(int socket, char* buf, int len)
00269 {
00270     if (socket < 0) {
00271         return -1;
00272     }
00273     uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD);
00274     uint8_t cntl_byte = (0x18 + (socket << 5));
00275     spi_read(ptr, cntl_byte, (uint8_t*)buf, len);
00276     sreg<uint16_t>(socket, Sn_RX_RD, ptr + len);
00277     scmd(socket, RECV);
00278     return len;
00279 }
00280 
00281 int WIZnet_Chip::new_socket()
00282 {
00283     for(int s = 0; s < MAX_SOCK_NUM; s++) {
00284         if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) {
00285             return s;
00286         }
00287     }
00288     return -1;
00289 }
00290 
00291 uint16_t WIZnet_Chip::new_port()
00292 {
00293     uint16_t port = rand();
00294     port |= 49152;
00295     return port;
00296 }
00297 
00298 void WIZnet_Chip::scmd(int socket, Command cmd)
00299 {
00300     sreg<uint8_t>(socket, Sn_CR, cmd);
00301     while(sreg<uint8_t>(socket, Sn_CR));
00302 }
00303 
00304 void WIZnet_Chip::spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len)
00305 {
00306     cs = 0;
00307     spi->write(addr >> 8);
00308     spi->write(addr & 0xff);
00309     spi->write(cb);
00310     for(int i = 0; i < len; i++) {
00311         spi->write(buf[i]);
00312     }
00313     cs = 1;
00314 
00315 #if DBG_SPI
00316     debug("[SPI]W %04x(%02x %d)", addr, cb, len);
00317     for(int i = 0; i < len; i++) {
00318         debug(" %02x", buf[i]);
00319         if (i > 16) {
00320             debug(" ...");
00321             break;
00322         }
00323     }
00324     debug("\r\n");
00325 #endif
00326 }
00327 
00328 void WIZnet_Chip::spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len)
00329 {
00330     cs = 0;
00331     spi->write(addr >> 8);
00332     spi->write(addr & 0xff);
00333     spi->write(cb);
00334     for(int i = 0; i < len; i++) {
00335         buf[i] = spi->write(0);
00336     }
00337     cs = 1;
00338 
00339 #if DBG_SPI
00340     debug("[SPI]R %04x(%02x %d)", addr, cb, len);
00341     for(int i = 0; i < len; i++) {
00342         debug(" %02x", buf[i]);
00343         if (i > 16) {
00344             debug(" ...");
00345             break;
00346         }
00347     }
00348     debug("\r\n");
00349     if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) {
00350         wait_ms(200);
00351     }
00352 #endif
00353 }
00354 
00355 uint32_t str_to_ip(const char* str)
00356 {
00357     uint32_t ip = 0;
00358     char* p = (char*)str;
00359     for(int i = 0; i < 4; i++) {
00360         ip |= atoi(p);
00361         p = strchr(p, '.');
00362         if (p == NULL) {
00363             break;
00364         }
00365         ip <<= 8;
00366         p++;
00367     }
00368     return ip;
00369 }
00370 
00371 void printfBytes(char* str, uint8_t* buf, int len)
00372 {
00373     printf("%s %d:", str, len);
00374     for(int i = 0; i < len; i++) {
00375         printf(" %02x", buf[i]);
00376     }
00377     printf("\n");
00378 }
00379 
00380 void printHex(uint8_t* buf, int len)
00381 {
00382     for(int i = 0; i < len; i++) {
00383         if ((i%16) == 0) {
00384             printf("%p", buf+i);
00385         }
00386         printf(" %02x", buf[i]);
00387         if ((i%16) == 15) {
00388             printf("\n");
00389         }
00390     }
00391     printf("\n");
00392 }
00393 
00394 void debug_hex(uint8_t* buf, int len)
00395 {
00396     for(int i = 0; i < len; i++) {
00397         if ((i%16) == 0) {
00398             debug("%p", buf+i);
00399         }
00400         debug(" %02x", buf[i]);
00401         if ((i%16) == 15) {
00402             debug("\n");
00403         }
00404     }
00405     debug("\n");
00406 }
00407 
00408 #endif