10 #ifndef W5100_H_INCLUDED
11 #define W5100_H_INCLUDED
17 #if defined(ARDUINO_ARCH_AVR)
18 #define SPI_ETHERNET_SETTINGS SPISettings(4000000, MSBFIRST, SPI_MODE0)
19 #elif defined(ESP8266)
20 #define SPI_ETHERNET_SETTINGS SPISettings(14000000, MSBFIRST, SPI_MODE0)
22 #define SPI_ETHERNET_SETTINGS SPI_CS,SPISettings(4000000, MSBFIRST, SPI_MODE0)
25 #define MAX_SOCK_NUM 4
27 typedef uint8_t SOCKET;
30 #define IDM_AR0 0x8001
31 #define IDM_AR1 0x8002
60 static const uint8_t
CLOSE = 0x00;
61 static const uint8_t
TCP = 0x01;
62 static const uint8_t
UDP = 0x02;
63 static const uint8_t
IPRAW = 0x03;
65 static const uint8_t
PPPOE = 0x05;
66 static const uint8_t
ND = 0x20;
67 static const uint8_t
MULTI = 0x80;
78 Sock_SEND_KEEP = 0x22,
100 static const uint8_t
RECV = 0x04;
102 static const uint8_t
CON = 0x01;
108 static const uint8_t
INIT = 0x13;
118 static const uint8_t
UDP = 0x22;
126 static const uint8_t
IP = 0;
129 static const uint8_t
GGP = 3;
130 static const uint8_t
TCP = 6;
131 static const uint8_t
PUP = 12;
132 static const uint8_t
UDP = 17;
133 static const uint8_t
IDP = 22;
134 static const uint8_t
ND = 77;
135 static const uint8_t
RAW = 255;
150 void read_data(SOCKET s,
volatile uint16_t src,
volatile uint8_t * dst, uint16_t len);
205 static uint8_t
write(uint16_t _addr, uint8_t _data);
206 static uint16_t
write(uint16_t addr,
const uint8_t *buf, uint16_t len);
207 static uint8_t
read(uint16_t addr);
208 static uint16_t
read(uint16_t addr, uint8_t *buf, uint16_t len);
210 #define __GP_REGISTER8(name, address) \
211 static inline void write##name(uint8_t _data) { \
212 write(address, _data); \
214 static inline uint8_t read##name() { \
215 return read(address); \
217 #define __GP_REGISTER16(name, address) \
218 static void write##name(uint16_t _data) { \
219 write(address, _data >> 8); \
220 write(address+1, _data & 0xFF); \
222 static uint16_t read##name() { \
223 uint16_t res = read(address); \
224 res = (res << 8) + read(address + 1); \
227 #define __GP_REGISTER_N(name, address, size) \
228 static uint16_t write##name(uint8_t *_buff) { \
229 return write(address, _buff, size); \
231 static uint16_t read##name(uint8_t *_buff) { \
232 return read(address, _buff, size); \
236 __GP_REGISTER8 (MR, 0x0000);
237 __GP_REGISTER_N(GAR, 0x0001, 4);
238 __GP_REGISTER_N(SUBR, 0x0005, 4);
239 __GP_REGISTER_N(SHAR, 0x0009, 6);
240 __GP_REGISTER_N(SIPR, 0x000F, 4);
241 __GP_REGISTER8 (IR, 0x0015);
242 __GP_REGISTER8 (IMR, 0x0016);
243 __GP_REGISTER16(RTR, 0x0017);
244 __GP_REGISTER8 (RCR, 0x0019);
245 __GP_REGISTER8 (RMSR, 0x001A);
246 __GP_REGISTER8 (TMSR, 0x001B);
247 __GP_REGISTER8 (PATR, 0x001C);
248 __GP_REGISTER8 (PTIMER, 0x0028);
249 __GP_REGISTER8 (PMAGIC, 0x0029);
250 __GP_REGISTER_N(UIPR, 0x002A, 4);
251 __GP_REGISTER16(UPORT, 0x002E);
253 #undef __GP_REGISTER8
254 #undef __GP_REGISTER16
255 #undef __GP_REGISTER_N
260 static inline uint8_t
readSn(SOCKET _s, uint16_t _addr);
261 static inline uint8_t
writeSn(SOCKET _s, uint16_t _addr, uint8_t _data);
262 static inline uint16_t
readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len);
263 static inline uint16_t
writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len);
268 #define __SOCKET_REGISTER8(name, address) \
269 static inline void write##name(SOCKET _s, uint8_t _data) { \
270 writeSn(_s, address, _data); \
272 static inline uint8_t read##name(SOCKET _s) { \
273 return readSn(_s, address); \
275 #define __SOCKET_REGISTER16(name, address) \
276 static void write##name(SOCKET _s, uint16_t _data) { \
277 writeSn(_s, address, _data >> 8); \
278 writeSn(_s, address+1, _data & 0xFF); \
280 static uint16_t read##name(SOCKET _s) { \
281 uint16_t res = readSn(_s, address); \
282 uint16_t res2 = readSn(_s,address + 1); \
284 res2 = res2 & 0xFF; \
288 #define __SOCKET_REGISTER_N(name, address, size) \
289 static uint16_t write##name(SOCKET _s, uint8_t *_buff) { \
290 return writeSn(_s, address, _buff, size); \
292 static uint16_t read##name(SOCKET _s, uint8_t *_buff) { \
293 return readSn(_s, address, _buff, size); \
297 __SOCKET_REGISTER8(
SnMR, 0x0000)
298 __SOCKET_REGISTER8(SnCR, 0x0001)
299 __SOCKET_REGISTER8(
SnIR, 0x0002)
300 __SOCKET_REGISTER8(
SnSR, 0x0003)
301 __SOCKET_REGISTER16(SnPORT, 0x0004)
302 __SOCKET_REGISTER_N(SnDHAR, 0x0006, 6)
303 __SOCKET_REGISTER_N(SnDIPR, 0x000C, 4)
304 __SOCKET_REGISTER16(SnDPORT, 0x0010)
305 __SOCKET_REGISTER16(SnMSSR, 0x0012)
306 __SOCKET_REGISTER8(SnPROTO, 0x0014)
307 __SOCKET_REGISTER8(SnTOS, 0x0015)
308 __SOCKET_REGISTER8(SnTTL, 0x0016)
309 __SOCKET_REGISTER16(SnTX_FSR, 0x0020)
310 __SOCKET_REGISTER16(SnTX_RD, 0x0022)
311 __SOCKET_REGISTER16(SnTX_WR, 0x0024)
312 __SOCKET_REGISTER16(SnRX_RSR, 0x0026)
313 __SOCKET_REGISTER16(SnRX_RD, 0x0028)
314 __SOCKET_REGISTER16(SnRX_WR, 0x002A)
316 #undef __SOCKET_REGISTER8
317 #undef __SOCKET_REGISTER16
318 #undef __SOCKET_REGISTER_N
322 static const uint8_t
RST = 7;
325 static const uint16_t
SMASK = 0x07FF;
326 static const uint16_t
RMASK = 0x07FF;
335 #if defined(ARDUINO_ARCH_AVR)
336 #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
337 inline static void initSS() { DDRB |= _BV(4); };
338 inline static void setSS() { PORTB &= ~_BV(4); };
339 inline static void resetSS() { PORTB |= _BV(4); };
340 #elif defined(__AVR_ATmega32U4__)
341 inline static void initSS() { DDRB |= _BV(6); };
342 inline static void setSS() { PORTB &= ~_BV(6); };
343 inline static void resetSS() { PORTB |= _BV(6); };
344 #elif defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__)
345 inline static void initSS() { DDRB |= _BV(0); };
346 inline static void setSS() { PORTB &= ~_BV(0); };
347 inline static void resetSS() { PORTB |= _BV(0); };
349 inline static void initSS() { DDRB |= _BV(2); };
350 inline static void setSS() { PORTB &= ~_BV(2); };
351 inline static void resetSS() { PORTB |= _BV(2); };
353 #elif defined(ESP8266)
354 inline static void initSS() { pinMode(SS, OUTPUT); };
355 inline static void setSS() { GPOC = digitalPinToBitMask(SS); };
356 inline static void resetSS() { GPOS = digitalPinToBitMask(SS); };
357 #endif // ARDUINO_ARCH_AVR
363 return read(CH_BASE + _s * CH_SIZE + _addr);
367 return write(CH_BASE + _s * CH_SIZE + _addr, _data);
371 return read(CH_BASE + _s * CH_SIZE + _addr, _buf, _len);
375 return write(CH_BASE + _s * CH_SIZE + _addr, _buf, _len);
static uint16_t readSHAR(uint8_t *_buff)
Definition: w5100.h:239
static const uint8_t ND
Definition: w5100.h:134
static const uint16_t CH_BASE
Definition: w5100.h:265
static const uint8_t CLOSE
Definition: w5100.h:60
static const uint8_t PPPOE
Definition: w5100.h:121
void setRetransmissionTime(uint16_t timeout)
Definition: w5100.h:410
static const uint8_t IDP
Definition: w5100.h:133
void setIPAddress(uint8_t *addr)
Definition: w5100.h:406
static const int SOCKETS
Definition: w5100.h:324
static const uint8_t PPPOE
Definition: w5100.h:65
uint16_t RBASE[SOCKETS]
Definition: w5100.h:332
static const uint8_t MULTI
Definition: w5100.h:67
uint16_t getTXFreeSize(SOCKET s)
Definition: w5100.cpp:50
static const uint8_t IPRAW
Definition: w5100.h:119
static const uint8_t TIME_WAIT
Definition: w5100.h:115
static const uint8_t IP
Definition: w5100.h:126
static uint16_t writeSUBR(uint8_t *_buff)
Definition: w5100.h:238
static uint8_t read(uint16_t addr)
Definition: w5100.cpp:178
void execCmdSn(SOCKET s, SockCMD _cmd)
Definition: w5100.cpp:219
static const uint16_t SSIZE
Definition: w5100.h:328
static const uint8_t LAST_ACK
Definition: w5100.h:117
static const uint8_t GGP
Definition: w5100.h:129
void read_data(SOCKET s, volatile uint16_t src, volatile uint8_t *dst, uint16_t len)
This function is being used for copy the data form Receive buffer of the chip to application buffer...
Definition: w5100.cpp:116
static void writeRCR(uint8_t _data)
Definition: w5100.h:244
static const uint16_t SMASK
Definition: w5100.h:325
static const uint8_t PUP
Definition: w5100.h:131
static const uint16_t RSIZE
Definition: w5100.h:330
void recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek=0)
This function is being called by recv() also.
Definition: w5100.cpp:104
uint16_t SBASE[SOCKETS]
Definition: w5100.h:331
static const uint8_t TIMEOUT
Definition: w5100.h:99
void getMACAddress(uint8_t *addr)
Definition: w5100.h:394
static const uint8_t RECV
Definition: w5100.h:100
static uint16_t writeGAR(uint8_t *_buff)
Definition: w5100.h:237
static const uint8_t SYNRECV
Definition: w5100.h:111
static uint8_t readSn(SOCKET _s, uint16_t _addr)
Definition: w5100.h:362
void setSubnetMask(uint8_t *_addr)
Definition: w5100.h:390
static const uint16_t RMASK
Definition: w5100.h:326
static const uint8_t DISCON
Definition: w5100.h:101
static const uint8_t ND
Definition: w5100.h:66
static const uint8_t CLOSE_WAIT
Definition: w5100.h:116
static const uint16_t CH_SIZE
Definition: w5100.h:266
static const uint8_t LISTEN
Definition: w5100.h:109
static const uint8_t CLOSED
Definition: w5100.h:107
static void setSS()
Definition: w5100.h:355
static const uint8_t CON
Definition: w5100.h:102
static const uint8_t FIN_WAIT
Definition: w5100.h:113
static uint16_t readSUBR(uint8_t *_buff)
Definition: w5100.h:238
void getSubnetMask(uint8_t *_addr)
Definition: w5100.h:386
static const uint8_t MACRAW
Definition: w5100.h:120
static const uint8_t SYNSENT
Definition: w5100.h:110
static const uint8_t SEND_OK
Definition: w5100.h:98
uint16_t getRXReceivedSize(SOCKET s)
Definition: w5100.cpp:62
void getGatewayIp(uint8_t *_addr)
Definition: w5100.h:378
static const uint8_t RAW
Definition: w5100.h:135
void init()
Definition: w5100.cpp:25
static const uint8_t RST
Definition: w5100.h:322
static const uint8_t ESTABLISHED
Definition: w5100.h:112
void setRetransmissionCount(uint8_t _retry)
Definition: w5100.h:414
static const uint8_t IPRAW
Definition: w5100.h:63
void setMACAddress(uint8_t *addr)
Definition: w5100.h:398
static void writeRTR(uint16_t _data)
Definition: w5100.h:243
void setGatewayIp(uint8_t *_addr)
Definition: w5100.h:382
static void resetSS()
Definition: w5100.h:356
static const uint8_t IGMP
Definition: w5100.h:128
static uint16_t writeSHAR(uint8_t *_buff)
Definition: w5100.h:239
void send_data_processing(SOCKET s, const uint8_t *data, uint16_t len)
This function is being called by send() and sendto() function also.
Definition: w5100.cpp:75
static const uint8_t TCP
Definition: w5100.h:130
static uint16_t writeSIPR(uint8_t *_buff)
Definition: w5100.h:240
static const uint8_t INIT
Definition: w5100.h:108
static const uint8_t TCP
Definition: w5100.h:61
static const uint8_t ICMP
Definition: w5100.h:127
static uint16_t readSIPR(uint8_t *_buff)
Definition: w5100.h:240
static void initSS()
Definition: w5100.h:354
static const uint8_t MACRAW
Definition: w5100.h:64
static uint8_t writeSn(SOCKET _s, uint16_t _addr, uint8_t _data)
Definition: w5100.h:366
void getIPAddress(uint8_t *addr)
Definition: w5100.h:402
void send_data_processing_offset(SOCKET s, uint16_t data_offset, const uint8_t *data, uint16_t len)
A copy of send_data_processing that uses the provided ptr for the write offset. Only needed for the "...
Definition: w5100.cpp:81
static uint16_t readGAR(uint8_t *_buff)
Definition: w5100.h:237
static const uint8_t CLOSING
Definition: w5100.h:114
static uint8_t write(uint16_t _addr, uint8_t _data)
Definition: w5100.cpp:137