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.
w5200.h
00001 /* 00002 * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st> 00003 * 00004 * This file is free software; you can redistribute it and/or modify 00005 * it under the terms of either the GNU General Public License version 2 00006 * or the GNU Lesser General Public License version 2.1, both as 00007 * published by the Free Software Foundation. 00008 */ 00009 #include "mbed.h" 00010 #pragma once 00011 00012 #define MAX_SOCK_NUM 8 00013 00014 typedef uint8_t SOCKET; 00015 00016 class SnMR { 00017 public: 00018 static const uint8_t CLOSE = 0x00; 00019 static const uint8_t TCP = 0x01; 00020 static const uint8_t UDP = 0x02; 00021 static const uint8_t IPRAW = 0x03; 00022 static const uint8_t MACRAW = 0x04; 00023 static const uint8_t PPPOE = 0x05; 00024 static const uint8_t ND = 0x20; 00025 static const uint8_t MULTI = 0x80; 00026 }; 00027 00028 enum SockCMD { 00029 Sock_OPEN = 0x01, 00030 Sock_LISTEN = 0x02, 00031 Sock_CONNECT = 0x04, 00032 Sock_DISCON = 0x08, 00033 Sock_CLOSE = 0x10, 00034 Sock_SEND = 0x20, 00035 Sock_SEND_MAC = 0x21, 00036 Sock_SEND_KEEP = 0x22, 00037 Sock_RECV = 0x40 00038 }; 00039 00040 class SnIR { 00041 public: 00042 static const uint8_t SEND_OK = 0x10; 00043 static const uint8_t TIMEOUT = 0x08; 00044 static const uint8_t RECV = 0x04; 00045 static const uint8_t DISCON = 0x02; 00046 static const uint8_t CON = 0x01; 00047 }; 00048 00049 class SnSR { 00050 public: 00051 static const uint8_t CLOSED = 0x00; 00052 static const uint8_t INIT = 0x13; 00053 static const uint8_t LISTEN = 0x14; 00054 static const uint8_t SYNSENT = 0x15; 00055 static const uint8_t SYNRECV = 0x16; 00056 static const uint8_t ESTABLISHED = 0x17; 00057 static const uint8_t FIN_WAIT = 0x18; 00058 static const uint8_t CLOSING = 0x1A; 00059 static const uint8_t TIME_WAIT = 0x1B; 00060 static const uint8_t CLOSE_WAIT = 0x1C; 00061 static const uint8_t LAST_ACK = 0x1D; 00062 static const uint8_t UDP = 0x22; 00063 static const uint8_t IPRAW = 0x32; 00064 static const uint8_t MACRAW = 0x42; 00065 static const uint8_t PPPOE = 0x5F; 00066 }; 00067 00068 class IPPROTO { 00069 public: 00070 static const uint8_t IP = 0; 00071 static const uint8_t ICMP = 1; 00072 static const uint8_t IGMP = 2; 00073 static const uint8_t GGP = 3; 00074 static const uint8_t TCP = 6; 00075 static const uint8_t PUP = 12; 00076 static const uint8_t UDP = 17; 00077 static const uint8_t IDP = 22; 00078 static const uint8_t ND = 77; 00079 static const uint8_t RAW = 255; 00080 }; 00081 00082 class W5200Class { 00083 public: 00084 void init(); 00085 00086 /** 00087 * @brief This function is being used for copy the data form Receive buffer of the chip to application buffer. 00088 * 00089 * It calculate the actual physical address where one has to read 00090 * the data from Receive buffer. Here also take care of the condition while it exceed 00091 * the Rx memory uper-bound of socket. 00092 */ 00093 void read_data(SOCKET s, volatile uint8_t * src, volatile uint8_t * dst, uint16_t len); 00094 00095 /** 00096 * @brief This function is being called by send() and sendto() function also. 00097 * 00098 * This function read the Tx write pointer register and after copy the data in buffer update the Tx write pointer 00099 * register. User should read upper byte first and lower byte later to get proper value. 00100 */ 00101 void send_data_processing(SOCKET s, const uint8_t *data, uint16_t len); 00102 /** 00103 * @brief A copy of send_data_processing that uses the provided ptr for the 00104 * write offset. Only needed for the "streaming" UDP API, where 00105 * a single UDP packet is built up over a number of calls to 00106 * send_data_processing_ptr, because TX_WR doesn't seem to get updated 00107 * correctly in those scenarios 00108 * @param ptr value to use in place of TX_WR. If 0, then the value is read 00109 * in from TX_WR 00110 * @return New value for ptr, to be used in the next call 00111 */ 00112 // FIXME Update documentation 00113 void send_data_processing_offset(SOCKET s, uint16_t data_offset, const uint8_t *data, uint16_t len); 00114 00115 /** 00116 * @brief This function is being called by recv() also. 00117 * 00118 * This function read the Rx read pointer register 00119 * and after copy the data from receive buffer update the Rx write pointer register. 00120 * User should read upper byte first and lower byte later to get proper value. 00121 */ 00122 void recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek = 0); 00123 00124 inline void setGatewayIp(uint8_t *_addr); 00125 inline void getGatewayIp(uint8_t *_addr); 00126 00127 inline void setSubnetMask(uint8_t *_addr); 00128 inline void getSubnetMask(uint8_t *_addr); 00129 00130 inline void setMACAddress(uint8_t * addr); 00131 inline void getMACAddress(uint8_t * addr); 00132 00133 inline void setIPAddress(uint8_t * addr); 00134 inline void getIPAddress(uint8_t * addr); 00135 00136 inline void setRetransmissionTime(uint16_t timeout); 00137 inline void setRetransmissionCount(uint8_t _retry); 00138 00139 void execCmdSn(SOCKET s, SockCMD _cmd); 00140 00141 uint16_t getTXFreeSize(SOCKET s); 00142 uint16_t getRXReceivedSize(SOCKET s); 00143 00144 00145 // W5200 Registers 00146 // --------------- 00147 private: 00148 static uint8_t write(uint16_t _addr, uint8_t _data); 00149 static uint16_t write(uint16_t addr, const uint8_t *buf, uint16_t len); 00150 static uint8_t read(uint16_t addr); 00151 static uint16_t read(uint16_t addr, uint8_t *buf, uint16_t len); 00152 00153 #define __GP_REGISTER8(name, address) \ 00154 static inline void write##name(uint8_t _data) { \ 00155 write(address, _data); \ 00156 } \ 00157 static inline uint8_t read##name() { \ 00158 return read(address); \ 00159 } 00160 #define __GP_REGISTER16(name, address) \ 00161 static void write##name(uint16_t _data) { \ 00162 write(address, _data >> 8); \ 00163 write(address+1, _data & 0xFF); \ 00164 } \ 00165 static uint16_t read##name() { \ 00166 uint16_t res = read(address); \ 00167 res = (res << 8) + read(address + 1); \ 00168 return res; \ 00169 } 00170 #define __GP_REGISTER_N(name, address, size) \ 00171 static uint16_t write##name(uint8_t *_buff) { \ 00172 return write(address, _buff, size); \ 00173 } \ 00174 static uint16_t read##name(uint8_t *_buff) { \ 00175 return read(address, _buff, size); \ 00176 } 00177 00178 public: 00179 __GP_REGISTER8 (MR, 0x0000); // Mode 00180 __GP_REGISTER_N(GAR, 0x0001, 4); // Gateway IP address 00181 __GP_REGISTER_N(SUBR, 0x0005, 4); // Subnet mask address 00182 __GP_REGISTER_N(SHAR, 0x0009, 6); // Source MAC address 00183 __GP_REGISTER_N(SIPR, 0x000F, 4); // Source IP address 00184 __GP_REGISTER8 (IR, 0x0015); // Interrupt 00185 __GP_REGISTER8 (IMR, 0x0016); // Interrupt Mask 00186 __GP_REGISTER16(RTR, 0x0017); // Timeout address 00187 __GP_REGISTER8 (RCR, 0x0019); // Retry count 00188 __GP_REGISTER8 (RMSR, 0x001A); // Receive memory size 00189 __GP_REGISTER8 (TMSR, 0x001B); // Transmit memory size 00190 __GP_REGISTER8 (PATR, 0x001C); // Authentication type address in PPPoE mode 00191 __GP_REGISTER8 (VERSIONR,0x001f); // Chip version 00192 __GP_REGISTER8 (PTIMER, 0x0028); // PPP LCP Request Timer 00193 __GP_REGISTER8 (PMAGIC, 0x0029); // PPP LCP Magic Number 00194 __GP_REGISTER8 (PHYSTATUS,0x0035); // PHY Status 00195 00196 #undef __GP_REGISTER8 00197 #undef __GP_REGISTER16 00198 #undef __GP_REGISTER_N 00199 00200 // W5200 Socket registers 00201 // ---------------------- 00202 private: 00203 static inline uint8_t readSn(SOCKET _s, uint16_t _addr); 00204 static inline uint8_t writeSn(SOCKET _s, uint16_t _addr, uint8_t _data); 00205 static inline uint16_t readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len); 00206 static inline uint16_t writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len); 00207 00208 static const uint16_t CH_BASE = 0x4000; 00209 static const uint16_t CH_SIZE = 0x0100; 00210 00211 #define __SOCKET_REGISTER8(name, address) \ 00212 static inline void write##name(SOCKET _s, uint8_t _data) { \ 00213 writeSn(_s, address, _data); \ 00214 } \ 00215 static inline uint8_t read##name(SOCKET _s) { \ 00216 return readSn(_s, address); \ 00217 } 00218 #define __SOCKET_REGISTER16(name, address) \ 00219 static void write##name(SOCKET _s, uint16_t _data) { \ 00220 writeSn(_s, address, _data >> 8); \ 00221 writeSn(_s, address+1, _data & 0xFF); \ 00222 } \ 00223 static uint16_t read##name(SOCKET _s) { \ 00224 uint16_t res = readSn(_s, address); \ 00225 res = (res << 8) + readSn(_s, address + 1); \ 00226 return res; \ 00227 } 00228 #define __SOCKET_REGISTER_N(name, address, size) \ 00229 static uint16_t write##name(SOCKET _s, uint8_t *_buff) { \ 00230 return writeSn(_s, address, _buff, size); \ 00231 } \ 00232 static uint16_t read##name(SOCKET _s, uint8_t *_buff) { \ 00233 return readSn(_s, address, _buff, size); \ 00234 } 00235 00236 public: 00237 __SOCKET_REGISTER8(SnMR, 0x0000) // Mode 00238 __SOCKET_REGISTER8(SnCR, 0x0001) // Command 00239 __SOCKET_REGISTER8(SnIR, 0x0002) // Interrupt 00240 __SOCKET_REGISTER8(SnSR, 0x0003) // Status 00241 __SOCKET_REGISTER16(SnPORT, 0x0004) // Source Port 00242 __SOCKET_REGISTER_N(SnDHAR, 0x0006, 6) // Destination Hardw Addr 00243 __SOCKET_REGISTER_N(SnDIPR, 0x000C, 4) // Destination IP Addr 00244 __SOCKET_REGISTER16(SnDPORT, 0x0010) // Destination Port 00245 __SOCKET_REGISTER16(SnMSSR, 0x0012) // Max Segment Size 00246 __SOCKET_REGISTER8(SnPROTO, 0x0014) // Protocol in IP RAW Mode 00247 __SOCKET_REGISTER8(SnTOS, 0x0015) // IP TOS 00248 __SOCKET_REGISTER8(SnTTL, 0x0016) // IP TTL 00249 __SOCKET_REGISTER16(SnTX_FSR, 0x0020) // TX Free Size 00250 __SOCKET_REGISTER16(SnTX_RD, 0x0022) // TX Read Pointer 00251 __SOCKET_REGISTER16(SnTX_WR, 0x0024) // TX Write Pointer 00252 __SOCKET_REGISTER16(SnRX_RSR, 0x0026) // RX Free Size 00253 __SOCKET_REGISTER16(SnRX_RD, 0x0028) // RX Read Pointer 00254 __SOCKET_REGISTER16(SnRX_WR, 0x002A) // RX Write Pointer (supported?) 00255 00256 #undef __SOCKET_REGISTER8 00257 #undef __SOCKET_REGISTER16 00258 #undef __SOCKET_REGISTER_N 00259 00260 private: 00261 static const uint8_t RST = 7; // Reset BIT 00262 static const int SOCKETS = 8; 00263 00264 static const uint16_t SMASK = 0x07FF; // Tx buffer MASK 00265 static const uint16_t RMASK = 0x07FF; // Rx buffer MASK 00266 public: 00267 static const uint16_t SSIZE = 2048; // Max Tx buffer size 00268 private: 00269 static const uint16_t RSIZE = 2048; // Max Rx buffer size 00270 uint16_t SBASE[SOCKETS]; // Tx buffer base address 00271 uint16_t RBASE[SOCKETS]; // Rx buffer base address 00272 }; 00273 00274 extern W5200Class W5200; 00275 00276 uint8_t W5200Class::readSn(SOCKET _s, uint16_t _addr) { 00277 return read(CH_BASE + _s * CH_SIZE + _addr); 00278 } 00279 00280 uint8_t W5200Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t _data) { 00281 return write(CH_BASE + _s * CH_SIZE + _addr, _data); 00282 } 00283 00284 uint16_t W5200Class::readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) { 00285 return read(CH_BASE + _s * CH_SIZE + _addr, _buf, _len); 00286 } 00287 00288 uint16_t W5200Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) { 00289 return write(CH_BASE + _s * CH_SIZE + _addr, _buf, _len); 00290 } 00291 00292 void W5200Class::getGatewayIp(uint8_t *_addr) { 00293 readGAR(_addr); 00294 } 00295 00296 void W5200Class::setGatewayIp(uint8_t *_addr) { 00297 writeGAR(_addr); 00298 } 00299 00300 void W5200Class::getSubnetMask(uint8_t *_addr) { 00301 readSUBR(_addr); 00302 } 00303 00304 void W5200Class::setSubnetMask(uint8_t *_addr) { 00305 writeSUBR(_addr); 00306 } 00307 00308 void W5200Class::getMACAddress(uint8_t *_addr) { 00309 readSHAR(_addr); 00310 } 00311 00312 void W5200Class::setMACAddress(uint8_t *_addr) { 00313 writeSHAR(_addr); 00314 } 00315 00316 void W5200Class::getIPAddress(uint8_t *_addr) { 00317 readSIPR(_addr); 00318 } 00319 00320 void W5200Class::setIPAddress(uint8_t *_addr) { 00321 writeSIPR(_addr); 00322 } 00323 00324 void W5200Class::setRetransmissionTime(uint16_t _timeout) { 00325 writeRTR(_timeout); 00326 } 00327 00328 void W5200Class::setRetransmissionCount(uint8_t _retry) { 00329 writeRCR(_retry); 00330 }
Generated on Tue Jul 12 2022 13:04:15 by
1.7.2