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.h Source File

W5500.h

00001 #pragma once
00002 
00003 #include "mbed.h"
00004 #include "mbed_debug.h"
00005 
00006 #define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
00007 
00008 #define DEFAULT_WAIT_RESP_TIMEOUT 500
00009 
00010 
00011 #define MAX_SOCK_NUM 8
00012 
00013 #define MR        0x0000
00014 #define GAR       0x0001
00015 #define SUBR      0x0005
00016 #define SHAR      0x0009
00017 #define SIPR      0x000f
00018 #define PHYCFGR   0x002e
00019 
00020 // W5500 socket register
00021 #define Sn_MR         0x0000
00022 #define Sn_CR         0x0001
00023 #define Sn_IR         0x0002
00024 #define Sn_SR         0x0003
00025 #define Sn_PORT       0x0004
00026 #define Sn_DIPR       0x000c
00027 #define Sn_DPORT      0x0010
00028 #define Sn_RXBUF_SIZE 0x001e
00029 #define Sn_TXBUF_SIZE 0x001f
00030 #define Sn_TX_FSR     0x0020
00031 #define Sn_TX_WR      0x0024
00032 #define Sn_RX_RSR     0x0026
00033 #define Sn_RX_RD      0x0028
00034 
00035 class WIZnet_Chip {
00036 public:
00037 
00038     enum Protocol   { CLOSED = 0, TCP = 1, UDP = 2,};
00039     enum Command    { OPEN = 0x01, LISTEN = 0x02, CONNECT = 0x04, DISCON = 0x08, CLOSE = 0x10, SEND = 0x20, \
00040                         SEND_MAC = 0x21, SEND_KEEP = 0x22, RECV = 0x40, };
00041     enum Interrupt  { INT_CON = 0x01, INT_DISCON = 0x02, INT_RECV = 0x04, INT_TIMEOUT = 0x08, INT_SEND_OK = 0x10,};
00042 
00043     enum Status     { SOCK_CLOSED = 0x00, SOCK_INIT = 0x13, SOCK_LISTEN = 0x14, SOCK_SYNSENT = 0x15, SOCK_ESTABLISHED = 0x17, \
00044                          SOCK_CLOSE_WAIT = 0x1c, SOCK_UDP   = 0x22, };
00045 
00046     /*
00047     * Constructor
00048     *
00049     * @param spi spi class
00050     * @param cs cs of the W5500
00051     * @param reset reset pin of the W5500
00052     */
00053     WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset);
00054     WIZnet_Chip(SPI* spi, PinName cs, PinName reset);
00055 
00056     /*
00057     * Set MAC Address to W5500
00058     *
00059     * @return true if connected, false otherwise
00060     */ 
00061     bool setmac();
00062 
00063     /*
00064     * Set Network Informations (SrcIP, Netmask, Gataway)
00065     *
00066     * @return true if connected, false otherwise
00067     */ 
00068     bool setip();
00069 
00070     /*
00071     * Get Link Status
00072     *
00073     * @return true if Link up, false Link down
00074     */
00075     bool linkstatus();
00076 
00077     /*
00078     * Disconnect the connection
00079     *
00080     * @ returns true 
00081     */
00082     bool disconnect();
00083 
00084     /*
00085     * Open a tcp connection with the specified host on the specified port
00086     *
00087     * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established)
00088     * @param port port
00089     * @ returns true if successful
00090     */
00091     bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000);
00092 
00093     /*
00094     * Set the protocol (UDP or TCP)
00095     *
00096     * @param p protocol
00097     * @ returns true if successful
00098     */
00099     bool setProtocol(int socket, Protocol p);
00100 
00101     /*
00102     * Reset the W5500
00103     */
00104     void reset();
00105    
00106     int wait_readable(int socket, int wait_time_ms, int req_size = 0);
00107 
00108     int wait_writeable(int socket, int wait_time_ms, int req_size = 0);
00109 
00110     /*
00111     * Check if a tcp link is active
00112     *
00113     * @returns true if successful
00114     */
00115     bool is_connected(int socket);
00116 
00117     /*
00118     * Close a tcp connection
00119     *
00120     * @ returns true if successful
00121     */
00122     bool close(int socket);
00123 
00124     /*
00125     * @param str string to be sent
00126     * @param len string length
00127     */
00128     int send(int socket, const char * str, int len);
00129 
00130     int recv(int socket, char* buf, int len);
00131 
00132     /*
00133     * Return true if the module is using dhcp
00134     *
00135     * @returns true if the module is using dhcp
00136     */
00137     bool isDHCP() {
00138         return dhcp;
00139     }
00140 
00141     bool gethostbyname(const char* host, uint32_t* ip);
00142 
00143     static WIZnet_Chip * getInstance() {
00144         return inst;
00145     };
00146 
00147     int new_socket();
00148     uint16_t new_port();
00149     void scmd(int socket, Command cmd);
00150 
00151     template<typename T>
00152     void sreg(int socket, uint16_t addr, T data) {
00153         reg_wr<T>(addr, (0x0C + (socket << 5)), data);
00154     }
00155 
00156     template<typename T>
00157     T sreg(int socket, uint16_t addr) {
00158         return reg_rd<T>(addr, (0x08 + (socket << 5)));
00159     }
00160 
00161     template<typename T>
00162     void reg_wr(uint16_t addr, T data) {
00163         return reg_wr(addr, 0x04, data);
00164     }
00165     
00166     template<typename T>
00167     void reg_wr(uint16_t addr, uint8_t cb, T data) {
00168         uint8_t buf[sizeof(T)];
00169         *reinterpret_cast<T*>(buf) = data;
00170         for(int i = 0; i < sizeof(buf)/2; i++) { //  Little Endian to Big Endian
00171             uint8_t t = buf[i];
00172             buf[i] = buf[sizeof(buf)-1-i];
00173             buf[sizeof(buf)-1-i] = t;
00174         }
00175         spi_write(addr, cb, buf, sizeof(buf));
00176     }
00177 
00178     template<typename T>
00179     T reg_rd(uint16_t addr) {
00180         return reg_rd<T>(addr, 0x00);
00181     }
00182 
00183     template<typename T>
00184     T reg_rd(uint16_t addr, uint8_t cb) {
00185         uint8_t buf[sizeof(T)];
00186         spi_read(addr, cb, buf, sizeof(buf));
00187         for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian
00188             uint8_t t = buf[i];
00189             buf[i] = buf[sizeof(buf)-1-i];
00190             buf[sizeof(buf)-1-i] = t;
00191         }
00192         return *reinterpret_cast<T*>(buf);
00193     }
00194 
00195     void reg_rd_mac(uint16_t addr, uint8_t* data) {
00196         spi_read(addr, 0x00, data, 6);
00197     }
00198 
00199     void reg_wr_ip(uint16_t addr, uint8_t cb, const char* ip) {
00200         uint8_t buf[4];
00201         char* p = (char*)ip;
00202         for(int i = 0; i < 4; i++) {
00203             buf[i] = atoi(p);
00204             p = strchr(p, '.');
00205             if (p == NULL) {
00206                 break;
00207             }
00208             p++;
00209         }
00210         spi_write(addr, cb, buf, sizeof(buf));
00211     }
00212 
00213     void sreg_ip(int socket, uint16_t addr, const char* ip) {
00214         reg_wr_ip(addr, (0x0C + (socket << 5)), ip);
00215     }
00216 
00217 protected:
00218     uint8_t mac[6];
00219     uint32_t ip;
00220     uint32_t netmask;
00221     uint32_t gateway;
00222     uint32_t dnsaddr;
00223     bool dhcp;
00224 
00225     static WIZnet_Chip* inst;
00226 
00227     void reg_wr_mac(uint16_t addr, uint8_t* data) {
00228         spi_write(addr, 0x04, data, 6);
00229     }
00230 
00231     void spi_write(uint16_t addr, uint8_t cb, const uint8_t *buf, uint16_t len);
00232     void spi_read(uint16_t addr, uint8_t cb, uint8_t *buf, uint16_t len);
00233     SPI* spi;
00234     DigitalOut cs;
00235     DigitalOut reset_pin;
00236 };
00237 
00238 extern uint32_t str_to_ip(const char* str);
00239 extern void printfBytes(char* str, uint8_t* buf, int len);
00240 extern void printHex(uint8_t* buf, int len);
00241 extern void debug_hex(uint8_t* buf, int len);