ban4jp - / WIZ550ioInterface

Dependents:   HTTPClient_HelloWorld_WIZ550io NTPClient_HelloWorld_WIZ550io

Fork of WIZ820ioInterface 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::setProtocol(int socket, Protocol p)
00073 {
00074     if (socket < 0) {
00075         return false;
00076     }
00077     sreg<uint8_t>(socket, Sn_MR, p);
00078     return true;
00079 }
00080 
00081 bool WIZ820io::connect(int socket, const char * host, int port, int timeout_ms)
00082 {
00083     if (socket < 0) {
00084         return false;
00085     }
00086     sreg<uint8_t>(socket, Sn_MR, TCP);
00087     scmd(socket, OPEN);
00088     sreg_ip(socket, Sn_DIPR, host);
00089     sreg<uint16_t>(socket, Sn_DPORT, port);
00090     sreg<uint16_t>(socket, Sn_PORT, new_port());
00091     scmd(socket, CONNECT);
00092     Timer t;
00093     t.reset();
00094     t.start();
00095     while(!is_connected(socket)) {
00096         if (t.read_ms() > timeout_ms) {
00097             return false;
00098         }
00099     }
00100     return true;
00101 }
00102 
00103 bool WIZ820io::gethostbyname(const char* host, uint32_t* ip)
00104 {
00105     uint32_t addr = str_to_ip(host);
00106     char buf[17];
00107     snprintf(buf, sizeof(buf), "%d.%d.%d.%d", (addr>>24)&0xff, (addr>>16)&0xff, (addr>>8)&0xff, addr&0xff);
00108     if (strcmp(buf, host) == 0) {
00109         *ip = addr;
00110         return true;
00111     }
00112     DNSClient client;
00113     if(client.lookup(host)) {
00114         *ip = client.ip;
00115         return true;
00116     }
00117     return false;
00118 }
00119 
00120 bool WIZ820io::disconnect()
00121 {
00122     return true;
00123 }
00124 
00125 bool WIZ820io::is_connected(int socket)
00126 {
00127     if (sreg<uint8_t>(socket, Sn_SR) == SOCK_ESTABLISHED) {
00128         return true;
00129     }
00130     return false;
00131 }
00132 
00133 void WIZ820io::reset()
00134 {
00135     reset_pin = 1;
00136     reset_pin = 0;
00137     //wait_us(2); // 2us
00138     wait_us(500); // 500us (w5500)
00139     reset_pin = 1;
00140     //wait_ms(150); // 150ms
00141     wait_ms(300); // 300ms (w5500)
00142     
00143     //reg_wr<uint8_t>(MR, 1<<7);
00144     
00145 //#ifdef TARGET_LPC1114
00146 //    uint8_t mac[6] = {0x00,0x02,0xf7,0xf0,0x00,0x00};
00147 //#else
00148 //    uint8_t mac[6];
00149 //    mbed_mac_address((char*)mac);
00150 //#endif
00151 //    reg_wr_mac(SHAR, mac);
00152     
00153     // set RX and TX buffer size
00154     for (int socket = 0; socket < MAX_SOCK_NUM; socket++) {
00155         sreg<uint8_t>(socket, Sn_RXBUF_SIZE, 2);
00156         sreg<uint8_t>(socket, Sn_TXBUF_SIZE, 2);
00157     }
00158 }
00159 
00160 bool WIZ820io::close(int socket)
00161 {
00162     if (socket < 0) {
00163         return false;
00164     }
00165     // if not connected, return
00166     if (sreg<uint8_t>(socket, Sn_SR) == SOCK_CLOSED) {
00167         return true;
00168     }
00169     if (sreg<uint8_t>(socket, Sn_MR) == TCP) {
00170         scmd(socket, DISCON);
00171     }
00172     scmd(socket, CLOSE);
00173     sreg<uint8_t>(socket, Sn_IR, 0xff);
00174     return true;
00175 }
00176 
00177 int WIZ820io::wait_readable(int socket, int wait_time_ms, int req_size)
00178 {
00179     if (socket < 0) {
00180         return -1;
00181     }
00182     Timer t;
00183     t.reset();
00184     t.start();
00185     while(1) {
00186         int size = sreg<uint16_t>(socket, Sn_RX_RSR);
00187         if (size > req_size) {
00188             return size;
00189         }
00190         if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
00191             break;
00192         }
00193     }
00194     return -1;
00195 }
00196 
00197 int WIZ820io::wait_writeable(int socket, int wait_time_ms, int req_size)
00198 {
00199     if (socket < 0) {
00200         return -1;
00201     }
00202     Timer t;
00203     t.reset();
00204     t.start();
00205     while(1) {
00206         int size = sreg<uint16_t>(socket, Sn_TX_FSR);
00207         if (size > req_size) {
00208             return size;
00209         }
00210         if (wait_time_ms != (-1) && t.read_ms() > wait_time_ms) {
00211             break;
00212         }
00213     }
00214     return -1;
00215 }
00216 
00217 int WIZ820io::send(int socket, const char * str, int len)
00218 {
00219     if (socket < 0) {
00220         return -1;
00221     }
00222     uint16_t ptr = sreg<uint16_t>(socket, Sn_TX_WR);
00223     uint8_t cntl_byte = (0x14 + (socket << 5));
00224     spi_write(ptr, cntl_byte, (uint8_t*)str, len);
00225     sreg<uint16_t>(socket, Sn_TX_WR, ptr + len);
00226     scmd(socket, SEND);
00227     
00228     while ((sreg<uint8_t>(socket, Sn_IR) & INT_SEND_OK) != INT_SEND_OK) {
00229         if (sreg<uint8_t>(socket, Sn_SR) == CLOSED) {
00230             close(socket);
00231             return 0;
00232         }
00233     }
00234     sreg<uint8_t>(socket, Sn_IR, INT_SEND_OK);
00235 
00236     return len;
00237 }
00238 
00239 int WIZ820io::recv(int socket, char* buf, int len)
00240 {
00241     if (socket < 0) {
00242         return -1;
00243     }
00244     uint16_t ptr = sreg<uint16_t>(socket, Sn_RX_RD);
00245     uint8_t cntl_byte = (0x18 + (socket << 5));
00246     spi_read(ptr, cntl_byte, (uint8_t*)buf, len);
00247     sreg<uint16_t>(socket, Sn_RX_RD, ptr + len);
00248     scmd(socket, RECV);
00249     return len;
00250 }
00251 
00252 int WIZ820io::new_socket()
00253 {
00254     for(int s = 0; s < 8; s++) {
00255         if (sreg<uint8_t>(s, Sn_SR) == SOCK_CLOSED) {
00256             return s;
00257         }
00258     }
00259     return -1;
00260 }
00261 
00262 uint16_t WIZ820io::new_port()
00263 {
00264     uint16_t port = rand();
00265     port |= 49152;
00266     return port;
00267 }
00268 
00269 void WIZ820io::scmd(int socket, Command cmd)
00270 {
00271     sreg<uint8_t>(socket, Sn_CR, cmd);
00272     while(sreg<uint8_t>(socket, Sn_CR));
00273 }
00274 
00275 void WIZ820io::spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len)
00276 {
00277     cs = 0;
00278     spi->write(addr >> 8);
00279     spi->write(addr & 0xff);
00280     spi->write(cb);
00281     for(int i = 0; i < len; i++) {
00282         spi->write(buf[i]);
00283     }
00284     cs = 1;
00285 
00286 #if DBG_SPI
00287     debug("[SPI]W %04x(%02x %d)", addr, cb, len);
00288     for(int i = 0; i < len; i++) {
00289         debug(" %02x", buf[i]);
00290         if (i > 16) {
00291             debug(" ...");
00292             break;
00293         }
00294     }
00295     debug("\r\n");
00296 #endif    
00297 }
00298 
00299 void WIZ820io::spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len)
00300 {
00301     cs = 0;
00302     spi->write(addr >> 8);
00303     spi->write(addr & 0xff);
00304     spi->write(cb);
00305     for(int i = 0; i < len; i++) {
00306         buf[i] = spi->write(0);
00307     }
00308     cs = 1;
00309 
00310 #if DBG_SPI
00311     debug("[SPI]R %04x(%02x %d)", addr, cb, len);
00312     for(int i = 0; i < len; i++) {
00313         debug(" %02x", buf[i]);
00314         if (i > 16) {
00315             debug(" ...");
00316             break;
00317         }
00318     }
00319     debug("\r\n");
00320     if ((addr&0xf0ff)==0x4026 || (addr&0xf0ff)==0x4003) {
00321         wait_ms(200);
00322     }
00323 #endif    
00324 }
00325 
00326 uint32_t str_to_ip(const char* str)
00327 {
00328     uint32_t ip = 0;
00329     char* p = (char*)str;
00330     for(int i = 0; i < 4; i++) {
00331         ip |= atoi(p);
00332         p = strchr(p, '.');
00333         if (p == NULL) {
00334             break;
00335         }
00336         ip <<= 8;
00337         p++;
00338     }
00339     return ip;
00340 }
00341 
00342 void printfBytes(char* str, uint8_t* buf, int len)
00343 {
00344     printf("%s %d:", str, len);
00345     for(int i = 0; i < len; i++) {
00346         printf(" %02x", buf[i]);
00347     }
00348     printf("\n");  
00349 }
00350 
00351 void printHex(uint8_t* buf, int len)
00352 {
00353     for(int i = 0; i < len; i++) {
00354         if ((i%16) == 0) {
00355             printf("%p", buf+i);
00356         }
00357         printf(" %02x", buf[i]);
00358         if ((i%16) == 15) {
00359             printf("\n");
00360         }
00361     }
00362     printf("\n");
00363 }
00364 
00365 void debug_hex(uint8_t* buf, int len)
00366 {
00367     for(int i = 0; i < len; i++) {
00368         if ((i%16) == 0) {
00369             debug("%p", buf+i);
00370         }
00371         debug(" %02x", buf[i]);
00372         if ((i%16) == 15) {
00373             debug("\n");
00374         }
00375     }
00376     debug("\n");
00377 }