ESP8266
w5100.h
1 /*
2  * Copyright (c) 2010 by Arduino LLC. All rights reserved.
3  *
4  * This file is free software; you can redistribute it and/or modify
5  * it under the terms of either the GNU General Public License version 2
6  * or the GNU Lesser General Public License version 2.1, both as
7  * published by the Free Software Foundation.
8  */
9 
10 #ifndef W5100_H_INCLUDED
11 #define W5100_H_INCLUDED
12 
13 #include <SPI.h>
14 
15 #define SPI_CS 10
16 
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)
21 #else
22 #define SPI_ETHERNET_SETTINGS SPI_CS,SPISettings(4000000, MSBFIRST, SPI_MODE0)
23 #endif
24 
25 #define MAX_SOCK_NUM 4
26 
27 typedef uint8_t SOCKET;
28 
29 #define IDM_OR 0x8000
30 #define IDM_AR0 0x8001
31 #define IDM_AR1 0x8002
32 #define IDM_DR 0x8003
33 /*
34 class MR {
35 public:
36  static const uint8_t RST = 0x80;
37  static const uint8_t PB = 0x10;
38  static const uint8_t PPPOE = 0x08;
39  static const uint8_t LB = 0x04;
40  static const uint8_t AI = 0x02;
41  static const uint8_t IND = 0x01;
42 };
43 */
44 /*
45 class IR {
46 public:
47  static const uint8_t CONFLICT = 0x80;
48  static const uint8_t UNREACH = 0x40;
49  static const uint8_t PPPoE = 0x20;
50  static const uint8_t SOCK0 = 0x01;
51  static const uint8_t SOCK1 = 0x02;
52  static const uint8_t SOCK2 = 0x04;
53  static const uint8_t SOCK3 = 0x08;
54  static inline uint8_t SOCK(SOCKET ch) { return (0x01 << ch); };
55 };
56 */
57 
58 class SnMR {
59 public:
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;
64  static const uint8_t MACRAW = 0x04;
65  static const uint8_t PPPOE = 0x05;
66  static const uint8_t ND = 0x20;
67  static const uint8_t MULTI = 0x80;
68 };
69 
70 enum SockCMD {
71  Sock_OPEN = 0x01,
72  Sock_LISTEN = 0x02,
73  Sock_CONNECT = 0x04,
74  Sock_DISCON = 0x08,
75  Sock_CLOSE = 0x10,
76  Sock_SEND = 0x20,
77  Sock_SEND_MAC = 0x21,
78  Sock_SEND_KEEP = 0x22,
79  Sock_RECV = 0x40
80 };
81 
82 /*class SnCmd {
83 public:
84  static const uint8_t OPEN = 0x01;
85  static const uint8_t LISTEN = 0x02;
86  static const uint8_t CONNECT = 0x04;
87  static const uint8_t DISCON = 0x08;
88  static const uint8_t CLOSE = 0x10;
89  static const uint8_t SEND = 0x20;
90  static const uint8_t SEND_MAC = 0x21;
91  static const uint8_t SEND_KEEP = 0x22;
92  static const uint8_t RECV = 0x40;
93 };
94 */
95 
96 class SnIR {
97 public:
98  static const uint8_t SEND_OK = 0x10;
99  static const uint8_t TIMEOUT = 0x08;
100  static const uint8_t RECV = 0x04;
101  static const uint8_t DISCON = 0x02;
102  static const uint8_t CON = 0x01;
103 };
104 
105 class SnSR {
106 public:
107  static const uint8_t CLOSED = 0x00;
108  static const uint8_t INIT = 0x13;
109  static const uint8_t LISTEN = 0x14;
110  static const uint8_t SYNSENT = 0x15;
111  static const uint8_t SYNRECV = 0x16;
112  static const uint8_t ESTABLISHED = 0x17;
113  static const uint8_t FIN_WAIT = 0x18;
114  static const uint8_t CLOSING = 0x1A;
115  static const uint8_t TIME_WAIT = 0x1B;
116  static const uint8_t CLOSE_WAIT = 0x1C;
117  static const uint8_t LAST_ACK = 0x1D;
118  static const uint8_t UDP = 0x22;
119  static const uint8_t IPRAW = 0x32;
120  static const uint8_t MACRAW = 0x42;
121  static const uint8_t PPPOE = 0x5F;
122 };
123 
124 class IPPROTO {
125 public:
126  static const uint8_t IP = 0;
127  static const uint8_t ICMP = 1;
128  static const uint8_t IGMP = 2;
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;
136 };
137 
138 class W5100Class {
139 
140 public:
141  void init();
142 
150  void read_data(SOCKET s, volatile uint16_t src, volatile uint8_t * dst, uint16_t len);
151 
158  void send_data_processing(SOCKET s, const uint8_t *data, uint16_t len);
169 // FIXME Update documentation
170  void send_data_processing_offset(SOCKET s, uint16_t data_offset, const uint8_t *data, uint16_t len);
171 
179  void recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek = 0);
180 
181  inline void setGatewayIp(uint8_t *_addr);
182  inline void getGatewayIp(uint8_t *_addr);
183 
184  inline void setSubnetMask(uint8_t *_addr);
185  inline void getSubnetMask(uint8_t *_addr);
186 
187  inline void setMACAddress(uint8_t * addr);
188  inline void getMACAddress(uint8_t * addr);
189 
190  inline void setIPAddress(uint8_t * addr);
191  inline void getIPAddress(uint8_t * addr);
192 
193  inline void setRetransmissionTime(uint16_t timeout);
194  inline void setRetransmissionCount(uint8_t _retry);
195 
196  void execCmdSn(SOCKET s, SockCMD _cmd);
197 
198  uint16_t getTXFreeSize(SOCKET s);
199  uint16_t getRXReceivedSize(SOCKET s);
200 
201 
202  // W5100 Registers
203  // ---------------
204 private:
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);
209 
210 #define __GP_REGISTER8(name, address) \
211  static inline void write##name(uint8_t _data) { \
212  write(address, _data); \
213  } \
214  static inline uint8_t read##name() { \
215  return read(address); \
216  }
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); \
221  } \
222  static uint16_t read##name() { \
223  uint16_t res = read(address); \
224  res = (res << 8) + read(address + 1); \
225  return res; \
226  }
227 #define __GP_REGISTER_N(name, address, size) \
228  static uint16_t write##name(uint8_t *_buff) { \
229  return write(address, _buff, size); \
230  } \
231  static uint16_t read##name(uint8_t *_buff) { \
232  return read(address, _buff, size); \
233  }
234 
235 public:
236  __GP_REGISTER8 (MR, 0x0000); // Mode
237  __GP_REGISTER_N(GAR, 0x0001, 4); // Gateway IP address
238  __GP_REGISTER_N(SUBR, 0x0005, 4); // Subnet mask address
239  __GP_REGISTER_N(SHAR, 0x0009, 6); // Source MAC address
240  __GP_REGISTER_N(SIPR, 0x000F, 4); // Source IP address
241  __GP_REGISTER8 (IR, 0x0015); // Interrupt
242  __GP_REGISTER8 (IMR, 0x0016); // Interrupt Mask
243  __GP_REGISTER16(RTR, 0x0017); // Timeout address
244  __GP_REGISTER8 (RCR, 0x0019); // Retry count
245  __GP_REGISTER8 (RMSR, 0x001A); // Receive memory size
246  __GP_REGISTER8 (TMSR, 0x001B); // Transmit memory size
247  __GP_REGISTER8 (PATR, 0x001C); // Authentication type address in PPPoE mode
248  __GP_REGISTER8 (PTIMER, 0x0028); // PPP LCP Request Timer
249  __GP_REGISTER8 (PMAGIC, 0x0029); // PPP LCP Magic Number
250  __GP_REGISTER_N(UIPR, 0x002A, 4); // Unreachable IP address in UDP mode
251  __GP_REGISTER16(UPORT, 0x002E); // Unreachable Port address in UDP mode
252 
253 #undef __GP_REGISTER8
254 #undef __GP_REGISTER16
255 #undef __GP_REGISTER_N
256 
257  // W5100 Socket registers
258  // ----------------------
259 private:
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);
264 
265  static const uint16_t CH_BASE = 0x0400;
266  static const uint16_t CH_SIZE = 0x0100;
267 
268 #define __SOCKET_REGISTER8(name, address) \
269  static inline void write##name(SOCKET _s, uint8_t _data) { \
270  writeSn(_s, address, _data); \
271  } \
272  static inline uint8_t read##name(SOCKET _s) { \
273  return readSn(_s, address); \
274  }
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); \
279  } \
280  static uint16_t read##name(SOCKET _s) { \
281  uint16_t res = readSn(_s, address); \
282  uint16_t res2 = readSn(_s,address + 1); \
283  res = res << 8; \
284  res2 = res2 & 0xFF; \
285  res = res | res2; \
286  return res; \
287  }
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); \
291  } \
292  static uint16_t read##name(SOCKET _s, uint8_t *_buff) { \
293  return readSn(_s, address, _buff, size); \
294  }
295 
296 public:
297  __SOCKET_REGISTER8(SnMR, 0x0000) // Mode
298  __SOCKET_REGISTER8(SnCR, 0x0001) // Command
299  __SOCKET_REGISTER8(SnIR, 0x0002) // Interrupt
300  __SOCKET_REGISTER8(SnSR, 0x0003) // Status
301  __SOCKET_REGISTER16(SnPORT, 0x0004) // Source Port
302  __SOCKET_REGISTER_N(SnDHAR, 0x0006, 6) // Destination Hardw Addr
303  __SOCKET_REGISTER_N(SnDIPR, 0x000C, 4) // Destination IP Addr
304  __SOCKET_REGISTER16(SnDPORT, 0x0010) // Destination Port
305  __SOCKET_REGISTER16(SnMSSR, 0x0012) // Max Segment Size
306  __SOCKET_REGISTER8(SnPROTO, 0x0014) // Protocol in IP RAW Mode
307  __SOCKET_REGISTER8(SnTOS, 0x0015) // IP TOS
308  __SOCKET_REGISTER8(SnTTL, 0x0016) // IP TTL
309  __SOCKET_REGISTER16(SnTX_FSR, 0x0020) // TX Free Size
310  __SOCKET_REGISTER16(SnTX_RD, 0x0022) // TX Read Pointer
311  __SOCKET_REGISTER16(SnTX_WR, 0x0024) // TX Write Pointer
312  __SOCKET_REGISTER16(SnRX_RSR, 0x0026) // RX Free Size
313  __SOCKET_REGISTER16(SnRX_RD, 0x0028) // RX Read Pointer
314  __SOCKET_REGISTER16(SnRX_WR, 0x002A) // RX Write Pointer (supported?)
315 
316 #undef __SOCKET_REGISTER8
317 #undef __SOCKET_REGISTER16
318 #undef __SOCKET_REGISTER_N
319 
320 
321 private:
322  static const uint8_t RST = 7; // Reset BIT
323 
324  static const int SOCKETS = 4;
325  static const uint16_t SMASK = 0x07FF; // Tx buffer MASK
326  static const uint16_t RMASK = 0x07FF; // Rx buffer MASK
327 public:
328  static const uint16_t SSIZE = 2048; // Max Tx buffer size
329 private:
330  static const uint16_t RSIZE = 2048; // Max Rx buffer size
331  uint16_t SBASE[SOCKETS]; // Tx buffer base address
332  uint16_t RBASE[SOCKETS]; // Rx buffer base address
333 
334 private:
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); };
348 #else
349  inline static void initSS() { DDRB |= _BV(2); };
350  inline static void setSS() { PORTB &= ~_BV(2); };
351  inline static void resetSS() { PORTB |= _BV(2); };
352 #endif
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
358 };
359 
360 extern W5100Class W5100;
361 
362 uint8_t W5100Class::readSn(SOCKET _s, uint16_t _addr) {
363  return read(CH_BASE + _s * CH_SIZE + _addr);
364 }
365 
366 uint8_t W5100Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t _data) {
367  return write(CH_BASE + _s * CH_SIZE + _addr, _data);
368 }
369 
370 uint16_t W5100Class::readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) {
371  return read(CH_BASE + _s * CH_SIZE + _addr, _buf, _len);
372 }
373 
374 uint16_t W5100Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) {
375  return write(CH_BASE + _s * CH_SIZE + _addr, _buf, _len);
376 }
377 
378 void W5100Class::getGatewayIp(uint8_t *_addr) {
379  readGAR(_addr);
380 }
381 
382 void W5100Class::setGatewayIp(uint8_t *_addr) {
383  writeGAR(_addr);
384 }
385 
386 void W5100Class::getSubnetMask(uint8_t *_addr) {
387  readSUBR(_addr);
388 }
389 
390 void W5100Class::setSubnetMask(uint8_t *_addr) {
391  writeSUBR(_addr);
392 }
393 
394 void W5100Class::getMACAddress(uint8_t *_addr) {
395  readSHAR(_addr);
396 }
397 
398 void W5100Class::setMACAddress(uint8_t *_addr) {
399  writeSHAR(_addr);
400 }
401 
402 void W5100Class::getIPAddress(uint8_t *_addr) {
403  readSIPR(_addr);
404 }
405 
406 void W5100Class::setIPAddress(uint8_t *_addr) {
407  writeSIPR(_addr);
408 }
409 
410 void W5100Class::setRetransmissionTime(uint16_t _timeout) {
411  writeRTR(_timeout);
412 }
413 
415  writeRCR(_retry);
416 }
417 
418 #endif
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
Definition: w5100.h:124
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
Definition: w5100.h:138
Definition: w5100.h:105
static const uint16_t CH_SIZE
Definition: w5100.h:266
static const uint8_t LISTEN
Definition: w5100.h:109
Definition: w5100.h:58
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
Definition: w5100.h:96
void getGatewayIp(uint8_t *_addr)
Definition: w5100.h:378
Definition: Udp.h:41
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