no upgrade or change at this. move to new Library for WIZ550io, W5500 -> http://mbed.org/teams/EthernetInterfaceW5500-makers/code/W5500Interface/

Dependents:   LPC11U68_NTPClient_HelloWorld_WIZ550io

Fork of WIZ550ioInterface by ban4jp -

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WIZ820io.cpp Source File

WIZ820io.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 "WIZ820io.h"
00022 #include "DNSClient.h"
00023 
00024 //Debug is disabled by default
00025 #if 0
00026 #define DBG(...) do{debug("%p %d %s ", this,__LINE__,__PRETTY_FUNCTION__); debug(__VA_ARGS__); } while(0);
00027 //#define DBG(x, ...) debug("[WIZ820io:DBG]"x"\r\n", ##__VA_ARGS__);
00028 #define WARN(x, ...) debug("[WIZ820io:WARN]"x"\r\n", ##__VA_ARGS__);
00029 #define ERR(x, ...) debug("[WIZ820io:ERR]"x"\r\n", ##__VA_ARGS__);
00030 #else
00031 #define DBG(x, ...)
00032 #define WARN(x, ...)
00033 #define ERR(x, ...)
00034 #endif
00035 
00036 #if 1
00037 #define INFO(x, ...) debug("[WIZ820io:INFO]"x"\r\n", ##__VA_ARGS__);
00038 #else
00039 #define INFO(x, ...)
00040 #endif
00041 
00042 #define DBG_SPI 0
00043 
00044 WIZ820io* WIZ820io::inst;
00045 
00046 WIZ820io::WIZ820io(PinName mosi, PinName miso, PinName sclk, PinName _cs, PinName _reset):
00047     cs(_cs), reset_pin(_reset)
00048 {
00049     spi = new SPI(mosi, miso, sclk);
00050     cs = 1;
00051     reset_pin = 1;
00052     inst = this;
00053 }
00054 
00055 WIZ820io::WIZ820io(SPI* spi, PinName _cs, PinName _reset):
00056     cs(_cs), reset_pin(_reset)
00057 {
00058     this->spi = spi;
00059     cs = 1;
00060     reset_pin = 1;
00061     inst = this;
00062 }
00063 
00064 bool WIZ820io::join()
00065 {
00066     reg_wr<uint32_t>(SIPR, ip);
00067     reg_wr<uint32_t>(GAR, gateway);
00068     reg_wr<uint32_t>(SUBR, netmask);
00069     return true;
00070 }
00071 
00072 bool WIZ820io::join(uint32_t ip, uint32_t gateway, uint32_t netmask)
00073 {
00074     this->ip = ip;
00075     this->gateway = gateway;
00076     this->netmask = netmask;
00077     join();
00078     return true;
00079 }
00080 
00081 bool WIZ820io::setProtocol(int socket, Protocol p)
00082 {
00083     if (socket < 0) {
00084         return false;
00085     }
00086     sreg<uint8_t>(socket, Sn_MR, p);
00087     return true;
00088 }
00089 
00090 bool WIZ820io::connect(int socket, const char * host, int port, int timeout_ms)
00091 {
00092     if (socket < 0) {
00093         return false;
00094     }
00095     sreg<uint8_t>(socket, Sn_MR, TCP);
00096     scmd(socket, OPEN);
00097     sreg_ip(socket, Sn_DIPR, host);
00098     sreg<uint16_t>(socket, Sn_DPORT, port);
00099     sreg<uint16_t>(socket, Sn_PORT, new_port());
00100     scmd(socket, CONNECT);
00101     Timer t;
00102     t.reset();
00103     t.start();
00104     while(!is_connected(socket)) {
00105         if (t.read_ms() > timeout_ms) {
00106             return false;
00107         }
00108     }
00109     return true;
00110 }
00111 
00112 bool WIZ820io::gethostbyname(const char* host, uint32_t* ip)
00113 {
00114     uint32_t addr = str_to_ip(host);
00115     char buf[17];
00116     snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
00117     if (strcmp(buf, host) == 0) {
00118         *ip = addr;
00119         return true;
00120     }
00121     DNSClient client;
00122     if(client.lookup(host)) {
00123         *ip = client.ip;
00124         return true;
00125     }
00126     return false;
00127 }
00128 
00129 bool WIZ820io::disconnect()
00130 {
00131     return true;
00132 }
00133 
00134 bool WIZ820io::is_connected(int socket)
00135 {
00136     if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) {
00137         return true;
00138     }
00139     return false;
00140 }
00141 
00142 void WIZ820io::reset()
00143 {
00144     reset_pin = 1;
00145     reset_pin = 0;
00146     //wait_us(2); // 2us
00147     wait_us(500); // 500us (w5500)
00148     reset_pin = 1;
00149     //wait_ms(150); // 150ms
00150     wait_ms(300); // 300ms (w5500)
00151     
00152     //reg_wr<uint8_t>(MR, 1<<7);
00153     
00154 //#ifdef TARGET_LPC1114
00155 //    uint8_t mac[6] = {0x00,0x02,0xf7,0xf0,0x00,0x00};
00156 //#else
00157 //    uint8_t mac[6];
00158 //    mbed_mac_address((char*)mac);
00159 //#endif
00160 //    reg_wr_mac(SHAR, mac);
00161     
00162     // set RX and TX buffer size
00163     for (int socket = 0; socket < MAX_SOCK_NUM; socket++) {
00164         sreg<uint8_t>(socket, Sn_RXBUF_SIZE, 2);
00165         sreg<uint8_t>(socket, Sn_TXBUF_SIZE, 2);
00166     }
00167 }
00168 
00169 bool WIZ820io::close(int socket)
00170 {
00171     if (socket < 0) {
00172         return false;
00173     }
00174     // if not connected, return
00175     if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) {
00176         return true;
00177     }
00178     if (sreg<uint8_t>(socket, Sn_MR) == TCP) {
00179         scmd(socket, DISCON);
00180     }
00181     scmd(socket, CLOSE);
00182     sreg<uint8_t>(socket, Sn_IR, 0xff);
00183     return true;
00184 }
00185 
00186 int WIZ820io::wait_readable(int socket, int wait_time_ms, int req_size)
00187 {
00188     if (socket < 0) {
00189         return -1;
00190     }
00191     Timer t;
00192     t.reset();
00193     t.start();
00194     while(1) {
00195         int size = sreg<uint16_t>(socket, Sn_RX_RSR);
00196         if (size > req_size) {
00197             return size;
00198         }
00199         if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
00200             break;
00201         }
00202     }
00203     return -1;
00204 }
00205 
00206 int WIZ820io::wait_writeable(int socket, int wait_time_ms, int req_size)
00207 {
00208     if (socket < 0) {
00209         return -1;
00210     }
00211     Timer t;
00212     t.reset();
00213     t.start();
00214     while(1) {
00215         int size = sreg<uint16_t>(socket, Sn_TX_FSR);
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 WIZ820io::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     
00237     while ((sreg<uint8_t>(socket, Sn_IR) & INT_SEND_OK) != INT_SEND_OK) {
00238         if (sreg<uint8_t>(socket, Sn_SR) == CLOSED) {
00239             close(socket);
00240             return 0;
00241         }
00242     }
00243     sreg<uint8_t>(socket, Sn_IR, INT_SEND_OK);
00244 
00245     return len;
00246 }
00247 
00248 int WIZ820io::recv(int socket, char* buf, int len)
00249 {
00250     if (socket < 0) {
00251         return -1;
00252     }
00253     uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD);
00254     uint8_t cntl_byte = (0x18 + (socket << 5));
00255     spi_read(ptr, cntl_byte, (uint8_t*)buf, len);
00256     sreg<uint16_t>(socket, Sn_RX_RD, ptr + len);
00257     scmd(socket, RECV);
00258     return len;
00259 }
00260 
00261 int WIZ820io::new_socket()
00262 {
00263     for(int s = 0; s < 8; s++) {
00264         if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) {
00265             return s;
00266         }
00267     }
00268     return -1;
00269 }
00270 
00271 uint16_t WIZ820io::new_port()
00272 {
00273     uint16_t port = rand();
00274     port |= 49152;
00275     return port;
00276 }
00277 
00278 void WIZ820io::scmd(int socket, Command cmd)
00279 {
00280     sreg<uint8_t>(socket, Sn_CR, cmd);
00281     while(sreg<uint8_t>(socket, Sn_CR));
00282 }
00283 
00284 void WIZ820io::spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len)
00285 {
00286     cs = 0;
00287     spi->write(addr >> 8);
00288     spi->write(addr & 0xff);
00289     spi->write(cb);
00290     for(int i = 0; i < len; i++) {
00291         spi->write(buf[i]);
00292     }
00293     cs = 1;
00294 
00295 #if DBG_SPI
00296     debug("[SPI]W %04x(%02x %d)", addr, cb, len);
00297     for(int i = 0; i < len; i++) {
00298         debug(" %02x", buf[i]);
00299         if (i > 16) {
00300             debug(" ...");
00301             break;
00302         }
00303     }
00304     debug("\r\n");
00305 #endif    
00306 }
00307 
00308 void WIZ820io::spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len)
00309 {
00310     cs = 0;
00311     spi->write(addr >> 8);
00312     spi->write(addr & 0xff);
00313     spi->write(cb);
00314     for(int i = 0; i < len; i++) {
00315         buf[i] = spi->write(0);
00316     }
00317     cs = 1;
00318 
00319 #if DBG_SPI
00320     debug("[SPI]R %04x(%02x %d)", addr, cb, len);
00321     for(int i = 0; i < len; i++) {
00322         debug(" %02x", buf[i]);
00323         if (i > 16) {
00324             debug(" ...");
00325             break;
00326         }
00327     }
00328     debug("\r\n");
00329     if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) {
00330         wait_ms(200);
00331     }
00332 #endif    
00333 }
00334 
00335 uint32_t str_to_ip(const char* str)
00336 {
00337     uint32_t ip = 0;
00338     char* p = (char*)str;
00339     for(int i = 0; i < 4; i++) {
00340         ip |= atoi(p);
00341         p = strchr(p, '.');
00342         if (p == NULL) {
00343             break;
00344         }
00345         ip <<= 8;
00346         p++;
00347     }
00348     return ip;
00349 }
00350 
00351 void printfBytes(char* str, uint8_t* buf, int len)
00352 {
00353     printf("%s %d:", str, len);
00354     for(int i = 0; i < len; i++) {
00355         printf(" %02x", buf[i]);
00356     }
00357     printf("\n");  
00358 }
00359 
00360 void printHex(uint8_t* buf, int len)
00361 {
00362     for(int i = 0; i < len; i++) {
00363         if ((i%16) == 0) {
00364             printf("%p", buf+i);
00365         }
00366         printf(" %02x", buf[i]);
00367         if ((i%16) == 15) {
00368             printf("\n");
00369         }
00370     }
00371     printf("\n");
00372 }
00373 
00374 void debug_hex(uint8_t* buf, int len)
00375 {
00376     for(int i = 0; i < len; i++) {
00377         if ((i%16) == 0) {
00378             debug("%p", buf+i);
00379         }
00380         debug(" %02x", buf[i]);
00381         if ((i%16) == 15) {
00382             debug("\n");
00383         }
00384     }
00385     debug("\n");
00386 }