Skip to content

Commit f00f1f1

Browse files
implement SPIClass
1 parent e452805 commit f00f1f1

File tree

4 files changed

+112
-56
lines changed

4 files changed

+112
-56
lines changed

src/Ethernet.cpp

+66-28
Original file line numberDiff line numberDiff line change
@@ -23,31 +23,69 @@
2323
#include "utility/w5100.h"
2424
#include "Dhcp.h"
2525

26+
#ifdef CONFIG_IDF_TARGET_ESP32
27+
#ifdef ETH_USE_HSPI
28+
SPIClass spiETH = SPIClass(HSPI);
29+
#elif defined(ETH_USE_FSPI)
30+
SPIClass spiETH = SPIClass(FSPI);
31+
#else // use default VSPI port
32+
SPIClass spiETH = SPIClass(VSPI);
33+
#endif
34+
#else
35+
#ifdef ETH_USE_HSPI
36+
SPIClass spiETH = SPIClass(HSPI);
37+
#elif defined(ETH_USE_FSPI)
38+
SPIClass spiETH = SPIClass(FSPI);
39+
#else // use FSPI port
40+
#ifdef ARDUINO_ARCH_SAMD
41+
SPIClassSAMD spiETH = SPI;
42+
#else
43+
SPIClass spiETH = SPI;
44+
#endif
45+
#endif
46+
#endif
47+
2648
IPAddress EthernetClass::_dnsServerAddress;
2749
DhcpClass* EthernetClass::_dhcp = NULL;
2850

51+
/***************************************************************************************
52+
** Function name: getSPIinstance
53+
** Description: Get the instance of the SPI class
54+
***************************************************************************************/
55+
#ifdef ARDUINO_ARCH_SAMD
56+
SPIClassSAMD& EthernetClass::getSPIinstance(void)
57+
{
58+
return spiETH;
59+
}
60+
#else
61+
SPIClass& EthernetClass::getSPIinstance(void)
62+
{
63+
return spiETH;
64+
}
65+
#endif
66+
2967
int EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout)
3068
{
3169
static DhcpClass s_dhcp;
3270
_dhcp = &s_dhcp;
3371

3472
// Initialise the basic info
3573
if (W5100.init() == 0) return 0;
36-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
74+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
3775
W5100.setMACAddress(mac);
3876
W5100.setIPAddress(IPAddress(0,0,0,0).raw_address());
39-
SPI.endTransaction();
77+
spiETH.endTransaction();
4078

4179
// Now try to get our config info from a DHCP server
4280
int ret = _dhcp->beginWithDHCP(mac, timeout, responseTimeout);
4381
if (ret == 1) {
4482
// We've successfully found a DHCP server and got our configuration
4583
// info, so set things accordingly
46-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
84+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
4785
W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
4886
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
4987
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
50-
SPI.endTransaction();
88+
spiETH.endTransaction();
5189
_dnsServerAddress = _dhcp->getDnsServerIp();
5290
socketPortRand(micros());
5391
}
@@ -81,7 +119,7 @@ void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress g
81119
void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet)
82120
{
83121
if (W5100.init() == 0) return;
84-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
122+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
85123
W5100.setMACAddress(mac);
86124
#ifdef ESP8266
87125
W5100.setIPAddress(&ip[0]);
@@ -96,7 +134,7 @@ void EthernetClass::begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress g
96134
W5100.setGatewayIp(gateway._address);
97135
W5100.setSubnetMask(subnet._address);
98136
#endif
99-
SPI.endTransaction();
137+
spiETH.endTransaction();
100138
_dnsServerAddress = dns;
101139
}
102140

@@ -138,11 +176,11 @@ int EthernetClass::maintain()
138176
case DHCP_CHECK_RENEW_OK:
139177
case DHCP_CHECK_REBIND_OK:
140178
//we might have got a new IP.
141-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
179+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
142180
W5100.setIPAddress(_dhcp->getLocalIp().raw_address());
143181
W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address());
144182
W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address());
145-
SPI.endTransaction();
183+
spiETH.endTransaction();
146184
_dnsServerAddress = _dhcp->getDnsServerIp();
147185
break;
148186
default:
@@ -156,82 +194,82 @@ int EthernetClass::maintain()
156194

157195
void EthernetClass::MACAddress(uint8_t *mac_address)
158196
{
159-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
197+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
160198
W5100.getMACAddress(mac_address);
161-
SPI.endTransaction();
199+
spiETH.endTransaction();
162200
}
163201

164202
IPAddress EthernetClass::localIP()
165203
{
166204
IPAddress ret;
167-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
205+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
168206
W5100.getIPAddress(ret.raw_address());
169-
SPI.endTransaction();
207+
spiETH.endTransaction();
170208
return ret;
171209
}
172210

173211
IPAddress EthernetClass::subnetMask()
174212
{
175213
IPAddress ret;
176-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
214+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
177215
W5100.getSubnetMask(ret.raw_address());
178-
SPI.endTransaction();
216+
spiETH.endTransaction();
179217
return ret;
180218
}
181219

182220
IPAddress EthernetClass::gatewayIP()
183221
{
184222
IPAddress ret;
185-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
223+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
186224
W5100.getGatewayIp(ret.raw_address());
187-
SPI.endTransaction();
225+
spiETH.endTransaction();
188226
return ret;
189227
}
190228

191229
void EthernetClass::setMACAddress(const uint8_t *mac_address)
192230
{
193-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
231+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
194232
W5100.setMACAddress(mac_address);
195-
SPI.endTransaction();
233+
spiETH.endTransaction();
196234
}
197235

198236
void EthernetClass::setLocalIP(const IPAddress local_ip)
199237
{
200-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
238+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
201239
IPAddress ip = local_ip;
202240
W5100.setIPAddress(ip.raw_address());
203-
SPI.endTransaction();
241+
spiETH.endTransaction();
204242
}
205243

206244
void EthernetClass::setSubnetMask(const IPAddress subnet)
207245
{
208-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
246+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
209247
IPAddress ip = subnet;
210248
W5100.setSubnetMask(ip.raw_address());
211-
SPI.endTransaction();
249+
spiETH.endTransaction();
212250
}
213251

214252
void EthernetClass::setGatewayIP(const IPAddress gateway)
215253
{
216-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
254+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
217255
IPAddress ip = gateway;
218256
W5100.setGatewayIp(ip.raw_address());
219-
SPI.endTransaction();
257+
spiETH.endTransaction();
220258
}
221259

222260
void EthernetClass::setRetransmissionTimeout(uint16_t milliseconds)
223261
{
224262
if (milliseconds > 6553) milliseconds = 6553;
225-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
263+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
226264
W5100.setRetransmissionTime(milliseconds * 10);
227-
SPI.endTransaction();
265+
spiETH.endTransaction();
228266
}
229267

230268
void EthernetClass::setRetransmissionCount(uint8_t num)
231269
{
232-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
270+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
233271
W5100.setRetransmissionCount(num);
234-
SPI.endTransaction();
272+
spiETH.endTransaction();
235273
}
236274

237275

src/Ethernet.h

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949

5050

5151
#include <Arduino.h>
52+
#include <SPI.h>
5253
#include "Client.h"
5354
#include "Server.h"
5455
#include "Udp.h"
@@ -108,6 +109,13 @@ class EthernetClass {
108109
friend class EthernetClient;
109110
friend class EthernetServer;
110111
friend class EthernetUDP;
112+
113+
#ifdef ARDUINO_ARCH_SAMD
114+
static SPIClassSAMD& getSPIinstance(void); // Get SPI class handle
115+
#else
116+
static SPIClass& getSPIinstance(void); // Get SPI class handle
117+
#endif
118+
111119
private:
112120
// Opens a socket(TCP or UDP or IP_RAW mode)
113121
static uint8_t socketBegin(uint8_t protocol, uint16_t port);

src/utility/w5100.cpp

+32-28
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,14 @@ uint8_t W5100Class::init(void)
101101
delay(560);
102102
//Serial.println("w5100 init");
103103

104-
SPI.begin();
104+
#if defined(ETH_SCLK) && defined(ETH_MISO) && defined(ETH_MOSI) && defined(ETHERNET_CS_PIN)
105+
spiETH.begin(ETH_SCLK, ETH_MISO, ETH_MOSI, ETHERNET_CS_PIN);
106+
#else
107+
spiETH.begin();
108+
#endif
105109
initSS();
106110
resetSS();
107-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
111+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
108112

109113
// Attempt W5200 detection first, because W5200 does not properly
110114
// reset its SPI state when CS goes high (inactive). Communication
@@ -189,10 +193,10 @@ uint8_t W5100Class::init(void)
189193
} else {
190194
//Serial.println("no chip :-(");
191195
chip = 0;
192-
SPI.endTransaction();
196+
spiETH.endTransaction();
193197
return 0; // no known chip is responding :-(
194198
}
195-
SPI.endTransaction();
199+
spiETH.endTransaction();
196200
initialized = true;
197201
return 1; // successful init
198202
}
@@ -276,15 +280,15 @@ W5100Linkstatus W5100Class::getLinkStatus()
276280
if (!init()) return UNKNOWN;
277281
switch (chip) {
278282
case 52:
279-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
283+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
280284
phystatus = readPSTATUS_W5200();
281-
SPI.endTransaction();
285+
spiETH.endTransaction();
282286
if (phystatus & 0x20) return LINK_ON;
283287
return LINK_OFF;
284288
case 55:
285-
SPI.beginTransaction(SPI_ETHERNET_SETTINGS);
289+
spiETH.beginTransaction(SPI_ETHERNET_SETTINGS);
286290
phystatus = readPHYCFGR_W5500();
287-
SPI.endTransaction();
291+
spiETH.endTransaction();
288292
if (phystatus & 0x01) return LINK_ON;
289293
return LINK_OFF;
290294
default:
@@ -299,11 +303,11 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len)
299303
if (chip == 51) {
300304
for (uint16_t i=0; i<len; i++) {
301305
setSS();
302-
SPI.transfer(0xF0);
303-
SPI.transfer(addr >> 8);
304-
SPI.transfer(addr & 0xFF);
306+
spiETH.transfer(0xF0);
307+
spiETH.transfer(addr >> 8);
308+
spiETH.transfer(addr & 0xFF);
305309
addr++;
306-
SPI.transfer(buf[i]);
310+
spiETH.transfer(buf[i]);
307311
resetSS();
308312
}
309313
} else if (chip == 52) {
@@ -312,13 +316,13 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len)
312316
cmd[1] = addr & 0xFF;
313317
cmd[2] = ((len >> 8) & 0x7F) | 0x80;
314318
cmd[3] = len & 0xFF;
315-
SPI.transfer(cmd, 4);
319+
spiETH.transfer(cmd, 4);
316320
#ifdef SPI_HAS_TRANSFER_BUF
317-
SPI.transfer(buf, NULL, len);
321+
spiETH.transfer(buf, NULL, len);
318322
#else
319323
// TODO: copy 8 bytes at a time to cmd[] and block transfer
320324
for (uint16_t i=0; i < len; i++) {
321-
SPI.transfer(buf[i]);
325+
spiETH.transfer(buf[i]);
322326
}
323327
#endif
324328
resetSS();
@@ -366,15 +370,15 @@ uint16_t W5100Class::write(uint16_t addr, const uint8_t *buf, uint16_t len)
366370
for (uint8_t i=0; i < len; i++) {
367371
cmd[i + 3] = buf[i];
368372
}
369-
SPI.transfer(cmd, len + 3);
373+
spiETH.transfer(cmd, len + 3);
370374
} else {
371-
SPI.transfer(cmd, 3);
375+
spiETH.transfer(cmd, 3);
372376
#ifdef SPI_HAS_TRANSFER_BUF
373-
SPI.transfer(buf, NULL, len);
377+
spiETH.transfer(buf, NULL, len);
374378
#else
375379
// TODO: copy 8 bytes at a time to cmd[] and block transfer
376380
for (uint16_t i=0; i < len; i++) {
377-
SPI.transfer(buf[i]);
381+
spiETH.transfer(buf[i]);
378382
}
379383
#endif
380384
}
@@ -391,17 +395,17 @@ uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len)
391395
for (uint16_t i=0; i < len; i++) {
392396
setSS();
393397
#if 1
394-
SPI.transfer(0x0F);
395-
SPI.transfer(addr >> 8);
396-
SPI.transfer(addr & 0xFF);
398+
spiETH.transfer(0x0F);
399+
spiETH.transfer(addr >> 8);
400+
spiETH.transfer(addr & 0xFF);
397401
addr++;
398-
buf[i] = SPI.transfer(0);
402+
buf[i] = spiETH.transfer(0);
399403
#else
400404
cmd[0] = 0x0F;
401405
cmd[1] = addr >> 8;
402406
cmd[2] = addr & 0xFF;
403407
cmd[3] = 0;
404-
SPI.transfer(cmd, 4); // TODO: why doesn't this work?
408+
spiETH.transfer(cmd, 4); // TODO: why doesn't this work?
405409
buf[i] = cmd[3];
406410
addr++;
407411
#endif
@@ -413,9 +417,9 @@ uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len)
413417
cmd[1] = addr & 0xFF;
414418
cmd[2] = (len >> 8) & 0x7F;
415419
cmd[3] = len & 0xFF;
416-
SPI.transfer(cmd, 4);
420+
spiETH.transfer(cmd, 4);
417421
memset(buf, 0, len);
418-
SPI.transfer(buf, len);
422+
spiETH.transfer(buf, len);
419423
resetSS();
420424
} else { // chip == 55
421425
setSS();
@@ -457,9 +461,9 @@ uint16_t W5100Class::read(uint16_t addr, uint8_t *buf, uint16_t len)
457461
cmd[2] = ((addr >> 6) & 0xE0) | 0x18; // 2K buffers
458462
#endif
459463
}
460-
SPI.transfer(cmd, 3);
464+
spiETH.transfer(cmd, 3);
461465
memset(buf, 0, len);
462-
SPI.transfer(buf, len);
466+
spiETH.transfer(buf, len);
463467
resetSS();
464468
}
465469
return len;

src/utility/w5100.h

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
#include <Arduino.h>
1818
#include <SPI.h>
1919

20+
#ifdef ARDUINO_ARCH_SAMD
21+
extern SPIClassSAMD spiETH;
22+
#else
23+
extern SPIClass spiETH;
24+
#endif
25+
2026
// Safe for all chips
2127
#define SPI_ETHERNET_SETTINGS SPISettings(14000000, MSBFIRST, SPI_MODE0)
2228

0 commit comments

Comments
 (0)