Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: IBMIoTClientEthernetExample_W5200
Fork of W5500Interface by
W5200.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 #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 // W5200 socket 00021 #define Sn_MR 0x4000 00022 #define Sn_CR 0x4001 00023 #define Sn_IR 0x4002 00024 #define Sn_SR 0x4003 00025 #define Sn_PORT 0x4004 00026 #define Sn_DIPR 0x400c 00027 #define Sn_DPORT 0x4010 00028 #define Sn_RXBUF_SIZE 0x401e 00029 #define Sn_TXBUF_SIZE 0x401f 00030 #define Sn_TX_FSR 0x4020 00031 #define Sn_TX_WR 0x4024 00032 #define Sn_RX_RSR 0x4026 00033 #define Sn_RX_RD 0x4028 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 * Set MAC Address 00048 * 00049 * @return true if connected, false otherwise 00050 */ 00051 bool setmac(); 00052 00053 /* 00054 * Constructor 00055 * 00056 * @param spi spi class 00057 * @param cs cs of the W5200 00058 * @param reset reset pin of the W5200 00059 */ 00060 WIZnet_Chip(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset); 00061 WIZnet_Chip(SPI* spi, PinName cs, PinName reset); 00062 00063 /* 00064 * Connect the W5200 module to the ssid contained in the constructor. 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 W5200 module from the access point 00079 * 00080 * @ returns true if successful 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 W5200 module 00103 */ 00104 void reset(); 00105 00106 00107 int wait_readable(int socket, int wait_time_ms, int req_size = 0); 00108 00109 int wait_writeable(int socket, int wait_time_ms, int req_size = 0); 00110 00111 /* 00112 * Check if a tcp link is active 00113 * 00114 * @returns true if successful 00115 */ 00116 bool is_connected(int socket); 00117 00118 /* 00119 * Close a tcp connection 00120 * 00121 * @ returns true if successful 00122 */ 00123 bool close(int socket); 00124 00125 /* 00126 * @param str string to be sent 00127 * @param len string length 00128 */ 00129 int send(int socket, const char * str, int len); 00130 00131 int recv(int socket, char* buf, int len); 00132 00133 /* 00134 * Return true if the module is using dhcp 00135 * 00136 * @returns true if the module is using dhcp 00137 */ 00138 bool isDHCP() { 00139 return dhcp; 00140 } 00141 00142 bool gethostbyname(const char* host, uint32_t* ip); 00143 00144 static WIZnet_Chip * getInstance() { 00145 return inst; 00146 }; 00147 00148 int new_socket(); 00149 uint16_t new_port(); 00150 void scmd(int socket, Command cmd); 00151 00152 template<typename T> 00153 void sreg(int socket, uint16_t addr, T data) { 00154 reg_wr<T>(addr+0x100*socket, data); 00155 } 00156 00157 template<typename T> 00158 T sreg(int socket, uint16_t addr) { 00159 return reg_rd<T>(addr+0x100*socket); 00160 } 00161 00162 template<typename T> 00163 void reg_wr(uint16_t addr, T data) { 00164 uint8_t buf[sizeof(T)]; 00165 *reinterpret_cast<T*>(buf) = data; 00166 for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian 00167 uint8_t t = buf[i]; 00168 buf[i] = buf[sizeof(buf)-1-i]; 00169 buf[sizeof(buf)-1-i] = t; 00170 } 00171 spi_write(addr, buf, sizeof(buf)); 00172 } 00173 00174 template<typename T> 00175 T reg_rd(uint16_t addr) { 00176 uint8_t buf[sizeof(T)]; 00177 spi_read(addr, buf, sizeof(buf)); 00178 for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian 00179 uint8_t t = buf[i]; 00180 buf[i] = buf[sizeof(buf)-1-i]; 00181 buf[sizeof(buf)-1-i] = t; 00182 } 00183 return *reinterpret_cast<T*>(buf); 00184 } 00185 00186 void reg_rd_mac(uint16_t addr, uint8_t* data) { 00187 spi_read(addr, data, 6); 00188 } 00189 00190 void reg_wr_ip(uint16_t addr, const char* ip) { 00191 uint8_t buf[4]; 00192 char* p = (char*)ip; 00193 for(int i = 0; i < 4; i++) { 00194 buf[i] = atoi(p); 00195 p = strchr(p, '.'); 00196 if (p == NULL) { 00197 break; 00198 } 00199 p++; 00200 } 00201 spi_write(addr, buf, sizeof(buf)); 00202 } 00203 00204 void sreg_ip(int socket, uint16_t addr, const char* ip) { 00205 reg_wr_ip(addr+0x100*socket, ip); 00206 } 00207 00208 protected: 00209 uint8_t mac[6]; 00210 uint32_t ip; 00211 uint32_t netmask; 00212 uint32_t gateway; 00213 uint32_t dnsaddr; 00214 bool dhcp; 00215 00216 static WIZnet_Chip* inst; 00217 00218 void reg_wr_mac(uint16_t addr, uint8_t* data) { 00219 spi_write(addr, data, 6); 00220 } 00221 00222 void spi_write(uint16_t addr, const uint8_t *buf, uint16_t len); 00223 void spi_read(uint16_t addr, uint8_t *buf, uint16_t len); 00224 SPI* spi; 00225 DigitalOut cs; 00226 DigitalOut reset_pin; 00227 }; 00228 00229 extern uint32_t str_to_ip(const char* str); 00230 extern void printfBytes(char* str, uint8_t* buf, int len); 00231 extern void printHex(uint8_t* buf, int len); 00232 extern void debug_hex(uint8_t* buf, int len); 00233
Generated on Sat Jul 16 2022 21:31:48 by
