Skip to content

Commit 2a01c2a

Browse files
committed
modified Ethernet (w5100) library to run on the ESP8266
see esp8266#962
1 parent 6735cad commit 2a01c2a

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

libraries/Ethernet/README.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ With the Arduino Ethernet Shield, this library allows an Arduino board to connec
55
For more information about this library please visit us at
66
http://www.arduino.cc/en/Reference/Ethernet
77

8+
modified to run on the ESP8266
9+
810
== License ==
911

1012
Copyright (c) 2010 Arduino LLC. All right reserved.

libraries/Ethernet/src/Ethernet.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@ uint8_t EthernetClass::_state[MAX_SOCK_NUM] = {
88
uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = {
99
0, 0, 0, 0 };
1010

11+
#ifdef ESP8266
12+
static DhcpClass s_dhcp;
13+
#endif
14+
1115
int EthernetClass::begin(uint8_t *mac_address)
1216
{
17+
#ifndef ESP8266
1318
static DhcpClass s_dhcp;
19+
#endif
1420
_dhcp = &s_dhcp;
1521

1622

@@ -133,4 +139,4 @@ IPAddress EthernetClass::dnsServerIP()
133139
return _dnsServerAddress;
134140
}
135141

136-
EthernetClass Ethernet;
142+
EthernetClass Ethernet;

libraries/Ethernet/src/utility/w5100.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ void W5100Class::init(void)
2626
{
2727
delay(300);
2828

29-
#if defined(ARDUINO_ARCH_AVR)
29+
#if defined(ARDUINO_ARCH_AVR) || defined(ESP8266)
3030
SPI.begin();
3131
initSS();
3232
#else
@@ -136,7 +136,7 @@ void W5100Class::read_data(SOCKET s, volatile uint16_t src, volatile uint8_t *ds
136136

137137
uint8_t W5100Class::write(uint16_t _addr, uint8_t _data)
138138
{
139-
#if defined(ARDUINO_ARCH_AVR)
139+
#if defined(ARDUINO_ARCH_AVR) || defined(ESP8266)
140140
setSS();
141141
SPI.transfer(0xF0);
142142
SPI.transfer(_addr >> 8);
@@ -156,7 +156,7 @@ uint16_t W5100Class::write(uint16_t _addr, const uint8_t *_buf, uint16_t _len)
156156
{
157157
for (uint16_t i=0; i<_len; i++)
158158
{
159-
#if defined(ARDUINO_ARCH_AVR)
159+
#if defined(ARDUINO_ARCH_AVR) || defined(ESP8266)
160160
setSS();
161161
SPI.transfer(0xF0);
162162
SPI.transfer(_addr >> 8);
@@ -177,7 +177,7 @@ uint16_t W5100Class::write(uint16_t _addr, const uint8_t *_buf, uint16_t _len)
177177

178178
uint8_t W5100Class::read(uint16_t _addr)
179179
{
180-
#if defined(ARDUINO_ARCH_AVR)
180+
#if defined(ARDUINO_ARCH_AVR) || defined(ESP8266)
181181
setSS();
182182
SPI.transfer(0x0F);
183183
SPI.transfer(_addr >> 8);
@@ -197,7 +197,7 @@ uint16_t W5100Class::read(uint16_t _addr, uint8_t *_buf, uint16_t _len)
197197
{
198198
for (uint16_t i=0; i<_len; i++)
199199
{
200-
#if defined(ARDUINO_ARCH_AVR)
200+
#if defined(ARDUINO_ARCH_AVR) || defined(ESP8266)
201201
setSS();
202202
SPI.transfer(0x0F);
203203
SPI.transfer(_addr >> 8);
@@ -222,4 +222,4 @@ void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd) {
222222
// Wait for command to complete
223223
while (readSnCR(s))
224224
;
225-
}
225+
}

libraries/Ethernet/src/utility/w5100.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#if defined(ARDUINO_ARCH_AVR)
1818
#define SPI_ETHERNET_SETTINGS SPISettings(4000000, MSBFIRST, SPI_MODE0)
19+
#elif defined(ESP8266)
20+
#define SPI_ETHERNET_SETTINGS SPISettings(14000000, MSBFIRST, SPI_MODE0)
1921
#else
2022
#define SPI_ETHERNET_SETTINGS SPI_CS,SPISettings(4000000, MSBFIRST, SPI_MODE0)
2123
#endif
@@ -348,6 +350,10 @@ class W5100Class {
348350
inline static void setSS() { PORTB &= ~_BV(2); };
349351
inline static void resetSS() { PORTB |= _BV(2); };
350352
#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); };
351357
#endif // ARDUINO_ARCH_AVR
352358
};
353359

@@ -409,4 +415,4 @@ void W5100Class::setRetransmissionCount(uint8_t _retry) {
409415
writeRCR(_retry);
410416
}
411417

412-
#endif
418+
#endif

0 commit comments

Comments
 (0)