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 W5200.cpp Source File

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