W5500 driver for mbed OS 5

Dependents:   http-webserver-example mbed-os-example-sockets

Fork of W5500Interface by Sergei G

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers W5500.h Source File

W5500.h

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