Update of W5500 Interface for mbed-os

Dependents:   PwrCond_mbed5

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers W5500.h Source File

W5500.h

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