W5500 from SeeedStudio on NUCLEO-L476RG
Dependents: coap-example Borsch coap-example
Fork of W5500Interface by
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 * Check if status of socket is closed. 00152 * 00153 * Used by automatically open socket (bind to local address). 00154 * 00155 * @ returns true if socket is closed. 00156 */ 00157 bool is_closed(int socket); 00158 00159 /* 00160 * @param str string to be sent 00161 * @param len string length 00162 */ 00163 int send(int socket, const char * str, int len); 00164 00165 int recv(int socket, char* buf, int len); 00166 00167 /* 00168 * Return true if the module is using dhcp 00169 * 00170 * @returns true if the module is using dhcp 00171 */ 00172 bool isDHCP() { 00173 return dhcp; 00174 } 00175 00176 bool gethostbyname(const char* host, uint32_t* ip); 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 protected: 00253 uint8_t mac[6]; 00254 uint32_t ip; 00255 uint32_t netmask; 00256 uint32_t gateway; 00257 uint32_t dnsaddr; 00258 bool dhcp; 00259 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 }; 00272 00273 extern uint32_t str_to_ip(const char* str); 00274 extern void printfBytes(char* str, uint8_t* buf, int len); 00275 extern void printHex(uint8_t* buf, int len); 00276 extern void debug_hex(uint8_t* buf, int len);
Generated on Fri Jul 15 2022 13:32:25 by 1.7.2