WIZ820io(W5200) network interface, EthernetInterface compatible.
Dependents: Seeed_Ethernet_Shield_V2_HelloWorld Seeed_Ethernet_Shield Cayenne-WIZ820ioInterface Seeed_Ethernet_Shield
Fork of WiflyInterface by
WIZ820io.h
00001 /* Copyright (C) 2012 mbed.org, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 * 00018 */ 00019 00020 #pragma once 00021 00022 #include "mbed.h" 00023 #include "mbed_debug.h" 00024 00025 #define TEST_ASSERT(A) while(!(A)){debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);}; 00026 00027 #define DEFAULT_WAIT_RESP_TIMEOUT 500 00028 00029 enum Protocol { 00030 CLOSED = 0, 00031 TCP = 1, 00032 UDP = 2, 00033 }; 00034 00035 enum Command { 00036 OPEN = 0x01, 00037 LISTEN = 0x02, 00038 CONNECT = 0x04, 00039 DISCON = 0x08, 00040 CLOSE = 0x10, 00041 SEND = 0x20, 00042 SEND_MAC = 0x21, 00043 SEND_KEEP = 0x22, 00044 RECV = 0x40, 00045 00046 }; 00047 00048 enum Status { 00049 SOCK_CLOSED = 0x00, 00050 SOCK_INIT = 0x13, 00051 SOCK_LISTEN = 0x14, 00052 SOCK_SYNSENT = 0x15, 00053 SOCK_ESTABLISHED = 0x17, 00054 SOCK_CLOSE_WAIT = 0x1c, 00055 SOCK_UDP = 0x22, 00056 }; 00057 00058 #define MAX_SOCK_NUM 8 00059 00060 #define MR 0x0000 00061 #define GAR 0x0001 00062 #define SUBR 0x0005 00063 #define SHAR 0x0009 00064 #define SIPR 0x000f 00065 #define PHYSTATUS 0x0035 00066 // socket 00067 #define Sn_MR 0x4000 00068 #define Sn_CR 0x4001 00069 #define Sn_SR 0x4003 00070 #define Sn_PORT 0x4004 00071 #define Sn_DIPR 0x400c 00072 #define Sn_DPORT 0x4010 00073 #define Sn_TX_FSR 0x4020 00074 #define Sn_TX_WR 0x4024 00075 #define Sn_RX_RSR 0x4026 00076 #define Sn_RX_RD 0x4028 00077 00078 class WIZ820io { 00079 public: 00080 /* 00081 * Constructor 00082 * 00083 * @param tx mbed pin to use for tx line of Serial interface 00084 * @param rx mbed pin to use for rx line of Serial interface 00085 * @param reset reset pin of the WIZ820io module 00086 */ 00087 WIZ820io(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset); 00088 WIZ820io(SPI* spi, PinName cs, PinName reset); 00089 00090 /* 00091 * Connect the WIZ820io module to the ssid contained in the constructor. 00092 * 00093 * @return true if connected, false otherwise 00094 */ 00095 bool join(); 00096 00097 /* 00098 * Disconnect the WIZ820io module from the access point 00099 * 00100 * @ returns true if successful 00101 */ 00102 bool disconnect(); 00103 00104 /* 00105 * Open a tcp connection with the specified host on the specified port 00106 * 00107 * @param host host (can be either an ip address or a name. If a name is provided, a dns request will be established) 00108 * @param port port 00109 * @ returns true if successful 00110 */ 00111 bool connect(int socket, const char * host, int port, int timeout_ms = 10*1000); 00112 00113 /* 00114 * Set the protocol (UDP or TCP) 00115 * 00116 * @param p protocol 00117 * @ returns true if successful 00118 */ 00119 bool setProtocol(int socket, Protocol p); 00120 00121 /* 00122 * Reset the WIZ820io module 00123 */ 00124 void reset(); 00125 00126 00127 int wait_readable(int socket, int wait_time_ms, int req_size = 0); 00128 00129 int wait_writeable(int socket, int wait_time_ms, int req_size = 0); 00130 00131 /* 00132 * Check if a tcp link is active 00133 * 00134 * @returns true if successful 00135 */ 00136 bool is_connected(int socket); 00137 00138 /* 00139 * Close a tcp connection 00140 * 00141 * @ returns true if successful 00142 */ 00143 bool close(int socket); 00144 00145 /* 00146 * @param str string to be sent 00147 * @param len string length 00148 */ 00149 int send(int socket, const char * str, int len); 00150 00151 int recv(int socket, char* buf, int len); 00152 00153 /* 00154 * Return true if the module is using dhcp 00155 * 00156 * @returns true if the module is using dhcp 00157 */ 00158 bool isDHCP() { 00159 return dhcp; 00160 } 00161 00162 bool gethostbyname(const char* host, uint32_t* ip); 00163 00164 static WIZ820io * getInstance() { 00165 return inst; 00166 }; 00167 00168 int new_socket(); 00169 uint16_t new_port(); 00170 void scmd(int socket, Command cmd); 00171 00172 template<typename T> 00173 void sreg(int socket, uint16_t addr, T data) { 00174 reg_wr<T>(addr+0x100*socket, data); 00175 } 00176 00177 template<typename T> 00178 T sreg(int socket, uint16_t addr) { 00179 return reg_rd<T>(addr+0x100*socket); 00180 } 00181 00182 template<typename T> 00183 void reg_wr(uint16_t addr, T data) { 00184 uint8_t buf[sizeof(T)]; 00185 *reinterpret_cast<T*>(buf) = data; 00186 for(int i = 0; i < sizeof(buf)/2; i++) { // Little Endian to Big Endian 00187 uint8_t t = buf[i]; 00188 buf[i] = buf[sizeof(buf)-1-i]; 00189 buf[sizeof(buf)-1-i] = t; 00190 } 00191 spi_write(addr, buf, sizeof(buf)); 00192 } 00193 00194 template<typename T> 00195 T reg_rd(uint16_t addr) { 00196 uint8_t buf[sizeof(T)]; 00197 spi_read(addr, buf, sizeof(buf)); 00198 for(int i = 0; i < sizeof(buf)/2; i++) { // Big Endian to Little Endian 00199 uint8_t t = buf[i]; 00200 buf[i] = buf[sizeof(buf)-1-i]; 00201 buf[sizeof(buf)-1-i] = t; 00202 } 00203 return *reinterpret_cast<T*>(buf); 00204 } 00205 00206 void reg_rd_mac(uint16_t addr, uint8_t* data) { 00207 spi_read(addr, data, 6); 00208 } 00209 00210 void reg_wr_ip(uint16_t addr, const char* ip) { 00211 uint8_t buf[4]; 00212 char* p = (char*)ip; 00213 for(int i = 0; i < 4; i++) { 00214 buf[i] = atoi(p); 00215 p = strchr(p, '.'); 00216 if (p == NULL) { 00217 break; 00218 } 00219 p++; 00220 } 00221 spi_write(addr, buf, sizeof(buf)); 00222 } 00223 00224 void sreg_ip(int socket, uint16_t addr, const char* ip) { 00225 reg_wr_ip(addr+0x100*socket, ip); 00226 } 00227 00228 protected: 00229 uint32_t ip; 00230 uint32_t netmask; 00231 uint32_t gateway; 00232 uint32_t dnsaddr; 00233 bool dhcp; 00234 00235 static WIZ820io* inst; 00236 00237 void reg_wr_mac(uint16_t addr, uint8_t* data) { 00238 spi_write(addr, data, 6); 00239 } 00240 00241 void spi_write(uint16_t addr, const uint8_t *buf, uint16_t len); 00242 void spi_read(uint16_t addr, uint8_t *buf, uint16_t len); 00243 SPI* spi; 00244 DigitalOut cs; 00245 DigitalOut reset_pin; 00246 }; 00247 00248 // WIZ820io.cpp 00249 extern uint32_t str_to_ip(const char* str); 00250 extern void printfBytes(char* str, uint8_t* buf, int len); 00251 extern void printHex(uint8_t* buf, int len); 00252 extern void debug_hex(uint8_t* buf, int len);
Generated on Thu Jul 14 2022 11:02:04 by 1.7.2