Skip to content

Commit

Permalink
modified Ethernet (w5100) library to run on the ESP8266
Browse files Browse the repository at this point in the history
  • Loading branch information
Links2004 committed Nov 5, 2015
1 parent 6735cad commit 2a01c2a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
2 changes: 2 additions & 0 deletions libraries/Ethernet/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ With the Arduino Ethernet Shield, this library allows an Arduino board to connec
For more information about this library please visit us at
http://www.arduino.cc/en/Reference/Ethernet

modified to run on the ESP8266

== License ==

Copyright (c) 2010 Arduino LLC. All right reserved.
Expand Down
8 changes: 7 additions & 1 deletion libraries/Ethernet/src/Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ uint8_t EthernetClass::_state[MAX_SOCK_NUM] = {
uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = {
0, 0, 0, 0 };

#ifdef ESP8266
static DhcpClass s_dhcp;
#endif

int EthernetClass::begin(uint8_t *mac_address)
{
#ifndef ESP8266
static DhcpClass s_dhcp;
#endif
_dhcp = &s_dhcp;


Expand Down Expand Up @@ -133,4 +139,4 @@ IPAddress EthernetClass::dnsServerIP()
return _dnsServerAddress;
}

EthernetClass Ethernet;
EthernetClass Ethernet;
12 changes: 6 additions & 6 deletions libraries/Ethernet/src/utility/w5100.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void W5100Class::init(void)
{
delay(300);

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

uint8_t W5100Class::write(uint16_t _addr, uint8_t _data)
{
#if defined(ARDUINO_ARCH_AVR)
#if defined(ARDUINO_ARCH_AVR) || defined(ESP8266)
setSS();
SPI.transfer(0xF0);
SPI.transfer(_addr >> 8);
Expand All @@ -156,7 +156,7 @@ uint16_t W5100Class::write(uint16_t _addr, const uint8_t *_buf, uint16_t _len)
{
for (uint16_t i=0; i<_len; i++)
{
#if defined(ARDUINO_ARCH_AVR)
#if defined(ARDUINO_ARCH_AVR) || defined(ESP8266)
setSS();
SPI.transfer(0xF0);
SPI.transfer(_addr >> 8);
Expand All @@ -177,7 +177,7 @@ uint16_t W5100Class::write(uint16_t _addr, const uint8_t *_buf, uint16_t _len)

uint8_t W5100Class::read(uint16_t _addr)
{
#if defined(ARDUINO_ARCH_AVR)
#if defined(ARDUINO_ARCH_AVR) || defined(ESP8266)
setSS();
SPI.transfer(0x0F);
SPI.transfer(_addr >> 8);
Expand All @@ -197,7 +197,7 @@ uint16_t W5100Class::read(uint16_t _addr, uint8_t *_buf, uint16_t _len)
{
for (uint16_t i=0; i<_len; i++)
{
#if defined(ARDUINO_ARCH_AVR)
#if defined(ARDUINO_ARCH_AVR) || defined(ESP8266)
setSS();
SPI.transfer(0x0F);
SPI.transfer(_addr >> 8);
Expand All @@ -222,4 +222,4 @@ void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd) {
// Wait for command to complete
while (readSnCR(s))
;
}
}
8 changes: 7 additions & 1 deletion libraries/Ethernet/src/utility/w5100.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#if defined(ARDUINO_ARCH_AVR)
#define SPI_ETHERNET_SETTINGS SPISettings(4000000, MSBFIRST, SPI_MODE0)
#elif defined(ESP8266)
#define SPI_ETHERNET_SETTINGS SPISettings(14000000, MSBFIRST, SPI_MODE0)
#else
#define SPI_ETHERNET_SETTINGS SPI_CS,SPISettings(4000000, MSBFIRST, SPI_MODE0)
#endif
Expand Down Expand Up @@ -348,6 +350,10 @@ class W5100Class {
inline static void setSS() { PORTB &= ~_BV(2); };
inline static void resetSS() { PORTB |= _BV(2); };
#endif
#elif defined(ESP8266)
inline static void initSS() { pinMode(SS, OUTPUT); };
inline static void setSS() { GPOC = digitalPinToBitMask(SS); };
inline static void resetSS() { GPOS = digitalPinToBitMask(SS); };
#endif // ARDUINO_ARCH_AVR
};

Expand Down Expand Up @@ -409,4 +415,4 @@ void W5100Class::setRetransmissionCount(uint8_t _retry) {
writeRCR(_retry);
}

#endif
#endif

0 comments on commit 2a01c2a

Please sign in to comment.