wiznet.h

Dependencies:   mbed

Dependents:   RTOS_WebServer

Fork of WIZnet_Library by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers W5500.cpp Source File

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     if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) {
00130         return true;
00131     }
00132     return false;
00133 }
00134 
00135 // Reset the chip & set the buffer
00136 void WIZnet_Chip::reset()
00137 {
00138     reset_pin = 1;
00139     reset_pin = 0;
00140     wait_us(500); // 500us (w5500)
00141     reset_pin = 1;
00142     wait_ms(400); // 400ms (w5500)
00143 
00144 #if defined(USE_WIZ550IO_MAC)
00145     reg_rd_mac(SHAR, mac); // read the MAC address inside the module
00146 #endif
00147 
00148     reg_wr_mac(SHAR, mac);
00149     
00150     // set RX and TX buffer size
00151     for (int socket = 0; socket < MAX_SOCK_NUM; socket++) {
00152         sreg<uint8_t>(socket, Sn_RXBUF_SIZE, 2);
00153         sreg<uint8_t>(socket, Sn_TXBUF_SIZE, 2);
00154     }
00155 }
00156 
00157 
00158 bool WIZnet_Chip::close(int socket)
00159 {
00160     if (socket < 0) {
00161         return false;
00162     }
00163     // if not connected, return
00164     if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) {
00165         return true;
00166     }
00167     if (sreg<uint8_t>(socket, Sn_MR) == TCP) {
00168         scmd(socket, DISCON);
00169     }
00170     scmd(socket, CLOSE);
00171     sreg<uint8_t>(socket, Sn_IR, 0xff);
00172     return true;
00173 }
00174 
00175 int WIZnet_Chip::wait_readable(int socket, int wait_time_ms, int req_size)
00176 {
00177     if (socket < 0) {
00178         return -1;
00179     }
00180     Timer t;
00181     t.reset();
00182     t.start();
00183     while(1) {
00184         int size = sreg<uint16_t>(socket, Sn_RX_RSR);
00185         if (size > req_size) {
00186             return size;
00187         }
00188         if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
00189             break;
00190         }
00191     }
00192     return -1;
00193 }
00194 
00195 int WIZnet_Chip::wait_writeable(int socket, int wait_time_ms, int req_size)
00196 {
00197     if (socket < 0) {
00198         return -1;
00199     }
00200     Timer t;
00201     t.reset();
00202     t.start();
00203     while(1) {
00204         int size = sreg<uint16_t>(socket, Sn_TX_FSR);
00205         if (size > req_size) {
00206             return size;
00207         }
00208         if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
00209             break;
00210         }
00211     }
00212     return -1;
00213 }
00214 
00215 int WIZnet_Chip::send(int socket, const char * str, int len)
00216 {
00217     if (socket < 0) {
00218         return -1;
00219     }
00220     uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR);
00221     uint8_t cntl_byte = (0x14 + (socket << 5));
00222     spi_write(ptr, cntl_byte, (uint8_t*)str, len);
00223     sreg<uint16_t>(socket, Sn_TX_WR, ptr + len);
00224     scmd(socket, SEND);
00225     
00226     while ((sreg<uint8_t>(socket, Sn_IR) & INT_SEND_OK) != INT_SEND_OK) {
00227         if (sreg<uint8_t>(socket, Sn_SR) == CLOSED) {
00228             close(socket);
00229             return 0;
00230         }
00231     }
00232     sreg<uint8_t>(socket, Sn_IR, INT_SEND_OK);
00233 
00234     return len;
00235 }
00236 
00237 int WIZnet_Chip::recv(int socket, char* buf, int len)
00238 {
00239     if (socket < 0) {
00240         return -1;
00241     }
00242     uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD);
00243     uint8_t cntl_byte = (0x18 + (socket << 5));
00244     spi_read(ptr, cntl_byte, (uint8_t*)buf, len);
00245     sreg<uint16_t>(socket, Sn_RX_RD, ptr + len);
00246     scmd(socket, RECV);
00247     return len;
00248 }
00249 
00250 int WIZnet_Chip::new_socket()
00251 {
00252     for(int s = 0; s < MAX_SOCK_NUM; s++) {
00253         if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) {
00254             return s;
00255         }
00256     }
00257     return -1;
00258 }
00259 
00260 uint16_t WIZnet_Chip::new_port()
00261 {
00262     uint16_t port = rand();
00263     port |= 49152;
00264     return port;
00265 }
00266 
00267 void WIZnet_Chip::scmd(int socket, Command cmd)
00268 {
00269     sreg<uint8_t>(socket, Sn_CR, cmd);
00270     while(sreg<uint8_t>(socket, Sn_CR));
00271 }
00272 
00273 void WIZnet_Chip::spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len)
00274 {
00275     cs = 0;
00276     spi->write(addr >> 8);
00277     spi->write(addr & 0xff);
00278     spi->write(cb);
00279     for(int i = 0; i < len; i++) {
00280         spi->write(buf[i]);
00281     }
00282     cs = 1;
00283 
00284 #if DBG_SPI
00285     debug("[SPI]W %04x(%02x %d)", addr, cb, len);
00286     for(int i = 0; i < len; i++) {
00287         debug(" %02x", buf[i]);
00288         if (i > 16) {
00289             debug(" ...");
00290             break;
00291         }
00292     }
00293     debug("\r\n");
00294 #endif    
00295 }
00296 
00297 void WIZnet_Chip::spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len)
00298 {
00299     cs = 0;
00300     spi->write(addr >> 8);
00301     spi->write(addr & 0xff);
00302     spi->write(cb);
00303     for(int i = 0; i < len; i++) {
00304         buf[i] = spi->write(0);
00305     }
00306     cs = 1;
00307 
00308 #if DBG_SPI
00309     debug("[SPI]R %04x(%02x %d)", addr, cb, len);
00310     for(int i = 0; i < len; i++) {
00311         debug(" %02x", buf[i]);
00312         if (i > 16) {
00313             debug(" ...");
00314             break;
00315         }
00316     }
00317     debug("\r\n");
00318     if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) {
00319         wait_ms(200);
00320     }
00321 #endif    
00322 }
00323 
00324 uint32_t str_to_ip(const char* str)
00325 {
00326     uint32_t ip = 0;
00327     char* p = (char*)str;
00328     for(int i = 0; i < 4; i++) {
00329         ip |= atoi(p);
00330         p = strchr(p, '.');
00331         if (p == NULL) {
00332             break;
00333         }
00334         ip <<= 8;
00335         p++;
00336     }
00337     return ip;
00338 }
00339 
00340 void printfBytes(char* str, uint8_t* buf, int len)
00341 {
00342     printf("%s %d:", str, len);
00343     for(int i = 0; i < len; i++) {
00344         printf(" %02x", buf[i]);
00345     }
00346     printf("\n");  
00347 }
00348 
00349 void printHex(uint8_t* buf, int len)
00350 {
00351     for(int i = 0; i < len; i++) {
00352         if ((i%16) == 0) {
00353             printf("%p", buf+i);
00354         }
00355         printf(" %02x", buf[i]);
00356         if ((i%16) == 15) {
00357             printf("\n");
00358         }
00359     }
00360     printf("\n");
00361 }
00362 
00363 void debug_hex(uint8_t* buf, int len)
00364 {
00365     for(int i = 0; i < len; i++) {
00366         if ((i%16) == 0) {
00367             debug("%p", buf+i);
00368         }
00369         debug(" %02x", buf[i]);
00370         if ((i%16) == 15) {
00371             debug("\n");
00372         }
00373     }
00374     debug("\n");
00375 }
00376 
00377 #endif