Skip to content

Commit 8aaca2f

Browse files
Use SPI transactions in Ethernet library
1 parent 53924e9 commit 8aaca2f

File tree

4 files changed

+80
-7
lines changed

4 files changed

+80
-7
lines changed

libraries/Ethernet/src/Ethernet.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@ int EthernetClass::begin(uint8_t *mac_address)
1616

1717
// Initialise the basic info
1818
W5100.init();
19+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
1920
W5100.setMACAddress(mac_address);
2021
W5100.setIPAddress(IPAddress(0,0,0,0).raw_address());
22+
SPI.endTransaction();
2123

2224
// Now try to get our config info from a DHCP server
2325
int ret = _dhcp->beginWithDHCP(mac_address);
2426
if(ret == 1)
2527
{
2628
// We've successfully found a DHCP server and got our configuration info, so set things
2729
// accordingly
30+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
2831
W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
2932
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
3033
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
34+
SPI.endTransaction();
3135
_dnsServerAddress = _dhcp->getDnsServerIp();
3236
}
3337

@@ -61,10 +65,12 @@ void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dn
6165
void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet)
6266
{
6367
W5100.init();
68+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
6469
W5100.setMACAddress(mac);
6570
W5100.setIPAddress(local_ip.raw_address());
6671
W5100.setGatewayIp(gateway.raw_address());
6772
W5100.setSubnetMask(subnet.raw_address());
73+
SPI.endTransaction();
6874
_dnsServerAddress = dns_server;
6975
}
7076

@@ -80,9 +86,11 @@ int EthernetClass::maintain(){
8086
case DHCP_CHECK_RENEW_OK:
8187
case DHCP_CHECK_REBIND_OK:
8288
//we might have got a new IP.
89+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
8390
W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
8491
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
8592
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
93+
SPI.endTransaction();
8694
_dnsServerAddress = _dhcp->getDnsServerIp();
8795
break;
8896
default:
@@ -96,21 +104,27 @@ int EthernetClass::maintain(){
96104
IPAddress EthernetClass::localIP()
97105
{
98106
IPAddress ret;
107+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
99108
W5100.getIPAddress(ret.raw_address());
109+
SPI.endTransaction();
100110
return ret;
101111
}
102112

103113
IPAddress EthernetClass::subnetMask()
104114
{
105115
IPAddress ret;
116+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
106117
W5100.getSubnetMask(ret.raw_address());
118+
SPI.endTransaction();
107119
return ret;
108120
}
109121

110122
IPAddress EthernetClass::gatewayIP()
111123
{
112124
IPAddress ret;
125+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
113126
W5100.getGatewayIp(ret.raw_address());
127+
SPI.endTransaction();
114128
return ret;
115129
}
116130

libraries/Ethernet/src/utility/socket.cpp

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
1212
if ((protocol == SnMR::TCP) || (protocol == SnMR::UDP) || (protocol == SnMR::IPRAW) || (protocol == SnMR::MACRAW) || (protocol == SnMR::PPPOE))
1313
{
1414
close(s);
15+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
1516
W5100.writeSnMR(s, protocol | flag);
1617
if (port != 0) {
1718
W5100.writeSnPORT(s, port);
@@ -22,7 +23,7 @@ uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
2223
}
2324

2425
W5100.execCmdSn(s, Sock_OPEN);
25-
26+
SPI.endTransaction();
2627
return 1;
2728
}
2829

@@ -32,7 +33,10 @@ uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
3233

3334
uint8_t socketStatus(SOCKET s)
3435
{
35-
return W5100.readSnSR(s);
36+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
37+
uint8_t status = W5100.readSnSR(s);
38+
SPI.endTransaction();
39+
return status;
3640
}
3741

3842

@@ -41,8 +45,10 @@ uint8_t socketStatus(SOCKET s)
4145
*/
4246
void close(SOCKET s)
4347
{
48+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
4449
W5100.execCmdSn(s, Sock_CLOSE);
4550
W5100.writeSnIR(s, 0xFF);
51+
SPI.endTransaction();
4652
}
4753

4854

@@ -52,9 +58,13 @@ void close(SOCKET s)
5258
*/
5359
uint8_t listen(SOCKET s)
5460
{
55-
if (W5100.readSnSR(s) != SnSR::INIT)
61+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
62+
if (W5100.readSnSR(s) != SnSR::INIT) {
63+
SPI.endTransaction();
5664
return 0;
65+
}
5766
W5100.execCmdSn(s, Sock_LISTEN);
67+
SPI.endTransaction();
5868
return 1;
5969
}
6070

@@ -76,9 +86,11 @@ uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port)
7686
return 0;
7787

7888
// set destination IP
89+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
7990
W5100.writeSnDIPR(s, addr);
8091
W5100.writeSnDPORT(s, port);
8192
W5100.execCmdSn(s, Sock_CONNECT);
93+
SPI.endTransaction();
8294

8395
return 1;
8496
}
@@ -91,7 +103,9 @@ uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port)
91103
*/
92104
void disconnect(SOCKET s)
93105
{
106+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
94107
W5100.execCmdSn(s, Sock_DISCON);
108+
SPI.endTransaction();
95109
}
96110

97111

@@ -113,17 +127,21 @@ uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
113127
// if freebuf is available, start.
114128
do
115129
{
130+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
116131
freesize = W5100.getTXFreeSize(s);
117132
status = W5100.readSnSR(s);
133+
SPI.endTransaction();
118134
if ((status != SnSR::ESTABLISHED) && (status != SnSR::CLOSE_WAIT))
119135
{
120136
ret = 0;
121137
break;
122138
}
139+
yield();
123140
}
124141
while (freesize < ret);
125142

126143
// copy data
144+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
127145
W5100.send_data_processing(s, (uint8_t *)buf, ret);
128146
W5100.execCmdSn(s, Sock_SEND);
129147

@@ -133,12 +151,17 @@ uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
133151
/* m2008.01 [bj] : reduce code */
134152
if ( W5100.readSnSR(s) == SnSR::CLOSED )
135153
{
154+
SPI.endTransaction();
136155
close(s);
137156
return 0;
138157
}
158+
SPI.endTransaction();
159+
yield();
160+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
139161
}
140162
/* +2008.01 bj */
141163
W5100.writeSnIR(s, SnIR::SEND_OK);
164+
SPI.endTransaction();
142165
return ret;
143166
}
144167

@@ -152,6 +175,7 @@ uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
152175
int16_t recv(SOCKET s, uint8_t *buf, int16_t len)
153176
{
154177
// Check how much data is available
178+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
155179
int16_t ret = W5100.getRXReceivedSize(s);
156180
if ( ret == 0 )
157181
{
@@ -178,13 +202,17 @@ int16_t recv(SOCKET s, uint8_t *buf, int16_t len)
178202
W5100.recv_data_processing(s, buf, ret);
179203
W5100.execCmdSn(s, Sock_RECV);
180204
}
205+
SPI.endTransaction();
181206
return ret;
182207
}
183208

184209

185210
int16_t recvAvailable(SOCKET s)
186211
{
187-
return W5100.getRXReceivedSize(s);
212+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
213+
int16_t ret = W5100.getRXReceivedSize(s);
214+
SPI.endTransaction();
215+
return ret;
188216
}
189217

190218

@@ -195,8 +223,9 @@ int16_t recvAvailable(SOCKET s)
195223
*/
196224
uint16_t peek(SOCKET s, uint8_t *buf)
197225
{
226+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
198227
W5100.recv_data_processing(s, buf, 1, 1);
199-
228+
SPI.endTransaction();
200229
return 1;
201230
}
202231

@@ -225,6 +254,7 @@ uint16_t sendto(SOCKET s, const uint8_t *buf, uint16_t len, uint8_t *addr, uint1
225254
}
226255
else
227256
{
257+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
228258
W5100.writeSnDIPR(s, addr);
229259
W5100.writeSnDPORT(s, port);
230260

@@ -239,12 +269,17 @@ uint16_t sendto(SOCKET s, const uint8_t *buf, uint16_t len, uint8_t *addr, uint1
239269
{
240270
/* +2008.01 [bj]: clear interrupt */
241271
W5100.writeSnIR(s, (SnIR::SEND_OK | SnIR::TIMEOUT)); /* clear SEND_OK & TIMEOUT */
272+
SPI.endTransaction();
242273
return 0;
243274
}
275+
SPI.endTransaction();
276+
yield();
277+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
244278
}
245279

246280
/* +2008.01 bj */
247281
W5100.writeSnIR(s, SnIR::SEND_OK);
282+
SPI.endTransaction();
248283
}
249284
return ret;
250285
}
@@ -264,6 +299,7 @@ uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t
264299

265300
if ( len > 0 )
266301
{
302+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
267303
ptr = W5100.readSnRX_RD(s);
268304
switch (W5100.readSnMR(s) & 0x07)
269305
{
@@ -318,6 +354,7 @@ uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t
318354
break;
319355
}
320356
W5100.execCmdSn(s, Sock_RECV);
357+
SPI.endTransaction();
321358
}
322359
return data_len;
323360
}
@@ -341,6 +378,7 @@ uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len)
341378
if (ret == 0)
342379
return 0;
343380

381+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
344382
W5100.send_data_processing(s, (uint8_t *)buf, ret);
345383
W5100.execCmdSn(s, Sock_SEND);
346384

@@ -350,18 +388,24 @@ uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len)
350388
{
351389
/* in case of igmp, if send fails, then socket closed */
352390
/* if you want change, remove this code. */
391+
SPI.endTransaction();
353392
close(s);
354393
return 0;
355394
}
395+
SPI.endTransaction();
396+
yield();
397+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
356398
}
357399

358400
W5100.writeSnIR(s, SnIR::SEND_OK);
401+
SPI.endTransaction();
359402
return ret;
360403
}
361404

362405
uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len)
363406
{
364407
uint16_t ret =0;
408+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
365409
if (len > W5100.getTXFreeSize(s))
366410
{
367411
ret = W5100.getTXFreeSize(s); // check size not to exceed MAX size.
@@ -371,6 +415,7 @@ uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len)
371415
ret = len;
372416
}
373417
W5100.send_data_processing_offset(s, offset, buf, ret);
418+
SPI.endTransaction();
374419
return ret;
375420
}
376421

@@ -386,14 +431,17 @@ int startUDP(SOCKET s, uint8_t* addr, uint16_t port)
386431
}
387432
else
388433
{
434+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
389435
W5100.writeSnDIPR(s, addr);
390436
W5100.writeSnDPORT(s, port);
437+
SPI.endTransaction();
391438
return 1;
392439
}
393440
}
394441

395442
int sendUDP(SOCKET s)
396443
{
444+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
397445
W5100.execCmdSn(s, Sock_SEND);
398446

399447
/* +2008.01 bj */
@@ -403,12 +451,17 @@ int sendUDP(SOCKET s)
403451
{
404452
/* +2008.01 [bj]: clear interrupt */
405453
W5100.writeSnIR(s, (SnIR::SEND_OK|SnIR::TIMEOUT));
454+
SPI.endTransaction();
406455
return 0;
407456
}
457+
SPI.endTransaction();
458+
yield();
459+
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
408460
}
409461

410462
/* +2008.01 bj */
411463
W5100.writeSnIR(s, SnIR::SEND_OK);
464+
SPI.endTransaction();
412465

413466
/* Sent ok */
414467
return 1;

libraries/Ethernet/src/utility/w5100.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
// W5100 controller instance
1616
W5100Class W5100;
1717

18-
#define SPI_CS 10
19-
2018
#define TX_RX_MAX_BUF_SIZE 2048
2119
#define TX_BUF 0x1100
2220
#define RX_BUF (TX_BUF + TX_RX_MAX_BUF_SIZE)

libraries/Ethernet/src/utility/w5100.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@
1212

1313
#include <SPI.h>
1414

15+
#define SPI_CS 10
16+
17+
#if defined(ARDUINO_ARCH_AVR)
18+
#define SPI_ETHERNET_SETTINGS SPISettings(4000000, MSBFIRST, SPI_MODE0)
19+
#else
20+
#define SPI_ETHERNET_SETTINGS SPI_CS,SPISettings(4000000, MSBFIRST, SPI_MODE0)
21+
#endif
22+
1523
#define MAX_SOCK_NUM 4
1624

1725
typedef uint8_t SOCKET;

0 commit comments

Comments
 (0)