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 ETHERNET_SHIELD_SPI_CS 10
16 
17 #define MAX_SOCK_NUM 4
18 
19 typedef uint8_t SOCKET;
20 
21 #define IDM_OR  0x8000
22 #define IDM_AR0 0x8001
23 #define IDM_AR1 0x8002
24 #define IDM_DR  0x8003
25 /*
26 class MR {
27 public:
28   static const uint8_t RST   = 0x80;
29   static const uint8_t PB    = 0x10;
30   static const uint8_t PPPOE = 0x08;
31   static const uint8_t LB    = 0x04;
32   static const uint8_t AI    = 0x02;
33   static const uint8_t IND   = 0x01;
34 };
35 */
36 /*
37 class IR {
38 public:
39   static const uint8_t CONFLICT = 0x80;
40   static const uint8_t UNREACH  = 0x40;
41   static const uint8_t PPPoE    = 0x20;
42   static const uint8_t SOCK0    = 0x01;
43   static const uint8_t SOCK1    = 0x02;
44   static const uint8_t SOCK2    = 0x04;
45   static const uint8_t SOCK3    = 0x08;
46   static inline uint8_t SOCK(SOCKET ch) { return (0x01 << ch); };
47 };
48 */
49 
50 class SnMR {
51 public:
52   static const uint8_t CLOSE  = 0x00;
53   static const uint8_t TCP    = 0x01;
54   static const uint8_t UDP    = 0x02;
55   static const uint8_t IPRAW  = 0x03;
56   static const uint8_t MACRAW = 0x04;
57   static const uint8_t PPPOE  = 0x05;
58   static const uint8_t ND     = 0x20;
59   static const uint8_t MULTI  = 0x80;
60 };
61 
62 enum SockCMD {
63   Sock_OPEN      = 0x01,
64   Sock_LISTEN    = 0x02,
65   Sock_CONNECT   = 0x04,
66   Sock_DISCON    = 0x08,
67   Sock_CLOSE     = 0x10,
68   Sock_SEND      = 0x20,
69   Sock_SEND_MAC  = 0x21,
70   Sock_SEND_KEEP = 0x22,
71   Sock_RECV      = 0x40
72 };
73 
74 /*class SnCmd {
75 public:
76   static const uint8_t OPEN      = 0x01;
77   static const uint8_t LISTEN    = 0x02;
78   static const uint8_t CONNECT   = 0x04;
79   static const uint8_t DISCON    = 0x08;
80   static const uint8_t CLOSE     = 0x10;
81   static const uint8_t SEND      = 0x20;
82   static const uint8_t SEND_MAC  = 0x21;
83   static const uint8_t SEND_KEEP = 0x22;
84   static const uint8_t RECV      = 0x40;
85 };
86 */
87 
88 class SnIR {
89 public:
90   static const uint8_t SEND_OK = 0x10;
91   static const uint8_t TIMEOUT = 0x08;
92   static const uint8_t RECV    = 0x04;
93   static const uint8_t DISCON  = 0x02;
94   static const uint8_t CON     = 0x01;
95 };
96 
97 class SnSR {
98 public:
99   static const uint8_t CLOSED      = 0x00;
100   static const uint8_t INIT        = 0x13;
101   static const uint8_t LISTEN      = 0x14;
102   static const uint8_t SYNSENT     = 0x15;
103   static const uint8_t SYNRECV     = 0x16;
104   static const uint8_t ESTABLISHED = 0x17;
105   static const uint8_t FIN_WAIT    = 0x18;
106   static const uint8_t CLOSING     = 0x1A;
107   static const uint8_t TIME_WAIT   = 0x1B;
108   static const uint8_t CLOSE_WAIT  = 0x1C;
109   static const uint8_t LAST_ACK    = 0x1D;
110   static const uint8_t UDP         = 0x22;
111   static const uint8_t IPRAW       = 0x32;
112   static const uint8_t MACRAW      = 0x42;
113   static const uint8_t PPPOE       = 0x5F;
114 };
115 
116 class IPPROTO {
117 public:
118   static const uint8_t IP   = 0;
119   static const uint8_t ICMP = 1;
120   static const uint8_t IGMP = 2;
121   static const uint8_t GGP  = 3;
122   static const uint8_t TCP  = 6;
123   static const uint8_t PUP  = 12;
124   static const uint8_t UDP  = 17;
125   static const uint8_t IDP  = 22;
126   static const uint8_t ND   = 77;
127   static const uint8_t RAW  = 255;
128 };
129 
130 class W5100Class {
131 
132 public:
133   void init();
134 
135   /**
136    * @brief	This function is being used for copy the data form Receive buffer of the chip to application buffer.
137    *
138    * It calculate the actual physical address where one has to read
139    * the data from Receive buffer. Here also take care of the condition while it exceed
140    * the Rx memory uper-bound of socket.
141    */
142   void read_data(SOCKET s, volatile uint16_t src, volatile uint8_t * dst, uint16_t len);
143 
144   /**
145    * @brief	 This function is being called by send() and sendto() function also.
146    *
147    * This function read the Tx write pointer register and after copy the data in buffer update the Tx write pointer
148    * register. User should read upper byte first and lower byte later to get proper value.
149    */
150   void send_data_processing(SOCKET s, const uint8_t *data, uint16_t len);
151   /**
152    * @brief A copy of send_data_processing that uses the provided ptr for the
153    *        write offset.  Only needed for the "streaming" UDP API, where
154    *        a single UDP packet is built up over a number of calls to
155    *        send_data_processing_ptr, because TX_WR doesn't seem to get updated
156    *        correctly in those scenarios
157    * @param ptr value to use in place of TX_WR.  If 0, then the value is read
158    *        in from TX_WR
159    * @return New value for ptr, to be used in the next call
160    */
161 // FIXME Update documentation
162   void send_data_processing_offset(SOCKET s, uint16_t data_offset, const uint8_t *data, uint16_t len);
163 
164   /**
165    * @brief	This function is being called by recv() also.
166    *
167    * This function read the Rx read pointer register
168    * and after copy the data from receive buffer update the Rx write pointer register.
169    * User should read upper byte first and lower byte later to get proper value.
170    */
171   void recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek = 0);
172 
173   inline void setGatewayIp(uint8_t *_addr);
174   inline void getGatewayIp(uint8_t *_addr);
175 
176   inline void setSubnetMask(uint8_t *_addr);
177   inline void getSubnetMask(uint8_t *_addr);
178 
179   inline void setMACAddress(uint8_t * addr);
180   inline void getMACAddress(uint8_t * addr);
181 
182   inline void setIPAddress(uint8_t * addr);
183   inline void getIPAddress(uint8_t * addr);
184 
185   inline void setRetransmissionTime(uint16_t timeout);
186   inline void setRetransmissionCount(uint8_t _retry);
187 
188   void execCmdSn(SOCKET s, SockCMD _cmd);
189 
190   uint16_t getTXFreeSize(SOCKET s);
191   uint16_t getRXReceivedSize(SOCKET s);
192 
193 
194   // W5100 Registers
195   // ---------------
196 private:
197   static uint8_t write(uint16_t _addr, uint8_t _data);
198   static uint16_t write(uint16_t addr, const uint8_t *buf, uint16_t len);
199   static uint8_t read(uint16_t addr);
200   static uint16_t read(uint16_t addr, uint8_t *buf, uint16_t len);
201 
202 #define __GP_REGISTER8(name, address)             \
203   static inline void write##name(uint8_t _data) { \
204     write(address, _data);                        \
205   }                                               \
206   static inline uint8_t read##name() {            \
207     return read(address);                         \
208   }
209 #define __GP_REGISTER16(name, address)            \
210   static void write##name(uint16_t _data) {       \
211     write(address,   _data >> 8);                 \
212     write(address+1, _data & 0xFF);               \
213   }                                               \
214   static uint16_t read##name() {                  \
215     uint16_t res = read(address);                 \
216     res = (res << 8) + read(address + 1);         \
217     return res;                                   \
218   }
219 #define __GP_REGISTER_N(name, address, size)      \
220   static uint16_t write##name(uint8_t *_buff) {   \
221     return write(address, _buff, size);           \
222   }                                               \
223   static uint16_t read##name(uint8_t *_buff) {    \
224     return read(address, _buff, size);            \
225   }
226 
227 public:
228   __GP_REGISTER8 (MR,     0x0000);    // Mode
229   __GP_REGISTER_N(GAR,    0x0001, 4); // Gateway IP address
230   __GP_REGISTER_N(SUBR,   0x0005, 4); // Subnet mask address
231   __GP_REGISTER_N(SHAR,   0x0009, 6); // Source MAC address
232   __GP_REGISTER_N(SIPR,   0x000F, 4); // Source IP address
233   __GP_REGISTER8 (IR,     0x0015);    // Interrupt
234   __GP_REGISTER8 (IMR,    0x0016);    // Interrupt Mask
235   __GP_REGISTER16(RTR,    0x0017);    // Timeout address
236   __GP_REGISTER8 (RCR,    0x0019);    // Retry count
237   __GP_REGISTER8 (RMSR,   0x001A);    // Receive memory size
238   __GP_REGISTER8 (TMSR,   0x001B);    // Transmit memory size
239   __GP_REGISTER8 (PATR,   0x001C);    // Authentication type address in PPPoE mode
240   __GP_REGISTER8 (PTIMER, 0x0028);    // PPP LCP Request Timer
241   __GP_REGISTER8 (PMAGIC, 0x0029);    // PPP LCP Magic Number
242   __GP_REGISTER_N(UIPR,   0x002A, 4); // Unreachable IP address in UDP mode
243   __GP_REGISTER16(UPORT,  0x002E);    // Unreachable Port address in UDP mode
244 
245 #undef __GP_REGISTER8
246 #undef __GP_REGISTER16
247 #undef __GP_REGISTER_N
248 
249   // W5100 Socket registers
250   // ----------------------
251 private:
252   static inline uint8_t readSn(SOCKET _s, uint16_t _addr);
253   static inline uint8_t writeSn(SOCKET _s, uint16_t _addr, uint8_t _data);
254   static inline uint16_t readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len);
255   static inline uint16_t writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len);
256 
257   static const uint16_t CH_BASE = 0x0400;
258   static const uint16_t CH_SIZE = 0x0100;
259 
260 #define __SOCKET_REGISTER8(name, address)                    \
261   static inline void write##name(SOCKET _s, uint8_t _data) { \
262     writeSn(_s, address, _data);                             \
263   }                                                          \
264   static inline uint8_t read##name(SOCKET _s) {              \
265     return readSn(_s, address);                              \
266   }
267 #define __SOCKET_REGISTER16(name, address)                   \
268   static void write##name(SOCKET _s, uint16_t _data) {       \
269     writeSn(_s, address,   _data >> 8);                      \
270     writeSn(_s, address+1, _data & 0xFF);                    \
271   }                                                          \
272   static uint16_t read##name(SOCKET _s) {                    \
273     uint16_t res = readSn(_s, address);                      \
274     uint16_t res2 = readSn(_s,address + 1);                  \
275     res = res << 8;                                          \
276     res2 = res2 & 0xFF;                                      \
277     res = res | res2;                                        \
278     return res;                                              \
279   }
280 #define __SOCKET_REGISTER_N(name, address, size)             \
281   static uint16_t write##name(SOCKET _s, uint8_t *_buff) {   \
282     return writeSn(_s, address, _buff, size);                \
283   }                                                          \
284   static uint16_t read##name(SOCKET _s, uint8_t *_buff) {    \
285     return readSn(_s, address, _buff, size);                 \
286   }
287 
288 public:
289   __SOCKET_REGISTER8(SnMR,        0x0000)        // Mode
290   __SOCKET_REGISTER8(SnCR,        0x0001)        // Command
291   __SOCKET_REGISTER8(SnIR,        0x0002)        // Interrupt
292   __SOCKET_REGISTER8(SnSR,        0x0003)        // Status
293   __SOCKET_REGISTER16(SnPORT,     0x0004)        // Source Port
294   __SOCKET_REGISTER_N(SnDHAR,     0x0006, 6)     // Destination Hardw Addr
295   __SOCKET_REGISTER_N(SnDIPR,     0x000C, 4)     // Destination IP Addr
296   __SOCKET_REGISTER16(SnDPORT,    0x0010)        // Destination Port
297   __SOCKET_REGISTER16(SnMSSR,     0x0012)        // Max Segment Size
298   __SOCKET_REGISTER8(SnPROTO,     0x0014)        // Protocol in IP RAW Mode
299   __SOCKET_REGISTER8(SnTOS,       0x0015)        // IP TOS
300   __SOCKET_REGISTER8(SnTTL,       0x0016)        // IP TTL
301   __SOCKET_REGISTER16(SnTX_FSR,   0x0020)        // TX Free Size
302   __SOCKET_REGISTER16(SnTX_RD,    0x0022)        // TX Read Pointer
303   __SOCKET_REGISTER16(SnTX_WR,    0x0024)        // TX Write Pointer
304   __SOCKET_REGISTER16(SnRX_RSR,   0x0026)        // RX Free Size
305   __SOCKET_REGISTER16(SnRX_RD,    0x0028)        // RX Read Pointer
306   __SOCKET_REGISTER16(SnRX_WR,    0x002A)        // RX Write Pointer (supported?)
307 
308 #undef __SOCKET_REGISTER8
309 #undef __SOCKET_REGISTER16
310 #undef __SOCKET_REGISTER_N
311 
312 
313 private:
314   static const uint8_t  RST = 7; // Reset BIT
315 
316   static const int SOCKETS = 4;
317   static const uint16_t SMASK = 0x07FF; // Tx buffer MASK
318   static const uint16_t RMASK = 0x07FF; // Rx buffer MASK
319 public:
320   static const uint16_t SSIZE = 2048; // Max Tx buffer size
321 private:
322   static const uint16_t RSIZE = 2048; // Max Rx buffer size
323   uint16_t SBASE[SOCKETS]; // Tx buffer base address
324   uint16_t RBASE[SOCKETS]; // Rx buffer base address
325 
326 private:
327 #if !defined(SPI_HAS_EXTENDED_CS_PIN_HANDLING)
328   #define SPI_ETHERNET_SETTINGS SPISettings(4000000, MSBFIRST, SPI_MODE0)
329   #if defined(ARDUINO_ARCH_AVR)
330     #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
initSS()331       inline static void initSS()    { DDRB  |=  _BV(4); };
setSS()332       inline static void setSS()     { PORTB &= ~_BV(4); };
resetSS()333       inline static void resetSS()   { PORTB |=  _BV(4); };
334     #elif defined(__AVR_ATmega32U4__)
335       inline static void initSS()    { DDRB  |=  _BV(6); };
336       inline static void setSS()     { PORTB &= ~_BV(6); };
337       inline static void resetSS()   { PORTB |=  _BV(6); };
338     #elif defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__)
339       inline static void initSS()    { DDRB  |=  _BV(0); };
340       inline static void setSS()     { PORTB &= ~_BV(0); };
341       inline static void resetSS()   { PORTB |=  _BV(0); };
342     #else
343       inline static void initSS()    { DDRB  |=  _BV(2); };
344       inline static void setSS()     { PORTB &= ~_BV(2); };
345       inline static void resetSS()   { PORTB |=  _BV(2); };
346     #endif
347   #elif defined(__ARDUINO_ARC__)
348 	inline static void initSS() { pinMode(10, OUTPUT); };
349 	inline static void setSS() { digitalWrite(10, LOW); };
350 	inline static void resetSS() { digitalWrite(10, HIGH); };
351   #else
352     inline static void initSS() {
353       *portModeRegister(digitalPinToPort(ETHERNET_SHIELD_SPI_CS)) |= digitalPinToBitMask(ETHERNET_SHIELD_SPI_CS);
354     }
355     inline static void setSS()   {
356       *portOutputRegister(digitalPinToPort(ETHERNET_SHIELD_SPI_CS)) &= ~digitalPinToBitMask(ETHERNET_SHIELD_SPI_CS);
357     }
358     inline static void resetSS() {
359       *portOutputRegister(digitalPinToPort(ETHERNET_SHIELD_SPI_CS)) |= digitalPinToBitMask(ETHERNET_SHIELD_SPI_CS);
360     }
361   #endif
362 #else
363   #define SPI_ETHERNET_SETTINGS ETHERNET_SHIELD_SPI_CS,SPISettings(4000000, MSBFIRST, SPI_MODE0)
364   // initSS(), setSS(), resetSS() not needed with EXTENDED_CS_PIN_HANDLING
365 #endif
366 };
367 
368 extern W5100Class W5100;
369 
readSn(SOCKET _s,uint16_t _addr)370 uint8_t W5100Class::readSn(SOCKET _s, uint16_t _addr) {
371   return read(CH_BASE + _s * CH_SIZE + _addr);
372 }
373 
writeSn(SOCKET _s,uint16_t _addr,uint8_t _data)374 uint8_t W5100Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t _data) {
375   return write(CH_BASE + _s * CH_SIZE + _addr, _data);
376 }
377 
readSn(SOCKET _s,uint16_t _addr,uint8_t * _buf,uint16_t _len)378 uint16_t W5100Class::readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) {
379   return read(CH_BASE + _s * CH_SIZE + _addr, _buf, _len);
380 }
381 
writeSn(SOCKET _s,uint16_t _addr,uint8_t * _buf,uint16_t _len)382 uint16_t W5100Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) {
383   return write(CH_BASE + _s * CH_SIZE + _addr, _buf, _len);
384 }
385 
getGatewayIp(uint8_t * _addr)386 void W5100Class::getGatewayIp(uint8_t *_addr) {
387   readGAR(_addr);
388 }
389 
setGatewayIp(uint8_t * _addr)390 void W5100Class::setGatewayIp(uint8_t *_addr) {
391   writeGAR(_addr);
392 }
393 
getSubnetMask(uint8_t * _addr)394 void W5100Class::getSubnetMask(uint8_t *_addr) {
395   readSUBR(_addr);
396 }
397 
setSubnetMask(uint8_t * _addr)398 void W5100Class::setSubnetMask(uint8_t *_addr) {
399   writeSUBR(_addr);
400 }
401 
getMACAddress(uint8_t * _addr)402 void W5100Class::getMACAddress(uint8_t *_addr) {
403   readSHAR(_addr);
404 }
405 
setMACAddress(uint8_t * _addr)406 void W5100Class::setMACAddress(uint8_t *_addr) {
407   writeSHAR(_addr);
408 }
409 
getIPAddress(uint8_t * _addr)410 void W5100Class::getIPAddress(uint8_t *_addr) {
411   readSIPR(_addr);
412 }
413 
setIPAddress(uint8_t * _addr)414 void W5100Class::setIPAddress(uint8_t *_addr) {
415   writeSIPR(_addr);
416 }
417 
setRetransmissionTime(uint16_t _timeout)418 void W5100Class::setRetransmissionTime(uint16_t _timeout) {
419   writeRTR(_timeout);
420 }
421 
setRetransmissionCount(uint8_t _retry)422 void W5100Class::setRetransmissionCount(uint8_t _retry) {
423   writeRCR(_retry);
424 }
425 
426 #endif
427