Skip to content

Commit 655f729

Browse files
committed
ESP8266 platform support
1 parent aca69da commit 655f729

4 files changed

Lines changed: 151 additions & 2 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ devices (e.g. Dallas/Maxim thermometers).
4242
* Arduino megaAVR (recent Microchip AVR architecture).
4343
* Platform class: `OneWireNg_ArduinoMegaAVR`.
4444
* **Not tested**.
45+
* Arduino ESP8266.
46+
* Platform class: `OneWireNg_ArduinoESP8266`.
47+
* **Not tested**.
4548

4649
NOTE: Expect more platforms support in the future. **I'm inviting all developers**,
4750
eager to help me with porting and testing the library for new platforms.

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ sentence=Arduino 1-wire service library
66
paragraph=An alternative for the classic Arduino OneWire library providing: (1) fixed handling of the 1-wire bus low-level activities related to its open-drain characteristic, (2) new I/O interface with enhanced parasite powering support, (3) improved overall software architecture.
77
category=Communication
88
url=https://github.com/pstolarz/OneWireNg
9-
architectures=avr,megaavr
9+
architectures=avr,megaavr,esp8266

src/OneWireNg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ uint8_t OneWireNg::crc8(const void *in, size_t len)
130130
#elif (CONFIG_CRC8_TAB == CRC8_TAB_16LH)
131131
static const uint8_t CRCTAB_STORAGE CRC8_16L[] = {
132132
0x00, 0x5e, 0xbc, 0xe2, 0x61, 0x3f, 0xdd, 0x83,
133-
0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41,
133+
0xc2, 0x9c, 0x7e, 0x20, 0xa3, 0xfd, 0x1f, 0x41
134134
};
135135
static const uint8_t CRCTAB_STORAGE CRC8_16H[] = {
136136
0x00, 0x9d, 0x23, 0xbe, 0x46, 0xdb, 0x65, 0xf8,

src/OneWireNg_ArduinoESP8266.h

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
* Copyright (c) 2019 Piotr Stolarz
3+
* OneWireNg: Ardiono 1-wire service library
4+
*
5+
* Distributed under the 2-clause BSD License (the License)
6+
* see accompanying file LICENSE for details.
7+
*
8+
* This software is distributed WITHOUT ANY WARRANTY; without even the
9+
* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10+
* See the License for more information.
11+
*/
12+
13+
#ifndef __OWNG_ARDUINO_ESP8266__
14+
#define __OWNG_ARDUINO_ESP8266__
15+
16+
#include "Arduino.h"
17+
#include "OneWireNg_BitBang.h"
18+
19+
#define __WRITE_GPIO(pin, st) \
20+
if (pin < 16) { \
21+
if (st) GPOS = (1 << pin); else GPOC = (1 << pin); \
22+
} else \
23+
if (pin == 16) { \
24+
if (st) GP16O |= 1; else GP16O &= ~1; \
25+
}
26+
27+
#define __GPIO_AS_INPUT(pin) \
28+
if (pin < 16) { \
29+
GPEC = (1 << pin); \
30+
} else \
31+
if (pin == 16) { \
32+
GP16E &= ~1; \
33+
}
34+
35+
#define __GPIO_AS_OUTPUT(pin) \
36+
if (pin < 16) { \
37+
GPES = (1 << pin); \
38+
} else \
39+
if (pin == 16) { \
40+
GP16E |= 1; \
41+
}
42+
43+
/**
44+
* Arduino ESP8266 platform GPIO specific implementation.
45+
*/
46+
class OneWireNg_ArduinoESP8266: public OneWireNg_BitBang
47+
{
48+
public:
49+
/**
50+
* OneWireNg 1-wire service for Arduino ESP8266 platform.
51+
*
52+
* Bus powering is supported via switching its GPIO to the high state.
53+
* In this case the GPIO servers as a voltage source for connected slaves
54+
* working in parasite powering configuration.
55+
*
56+
* @param pin Arduino GPIO pin number used for bit-banging 1-wire bus.
57+
* @param pullUp If @c true configure internal pull-up resistor for the bus.
58+
*/
59+
OneWireNg_ArduinoESP8266(unsigned pin, bool pullUp):
60+
OneWireNg_BitBang(false)
61+
{
62+
_dtaPin = pin;
63+
initDtaGpio(pullUp);
64+
}
65+
66+
/**
67+
* OneWireNg 1-wire service for Arduino ESP8266 platform.
68+
*
69+
* Bus powering is supported via a switching transistor providing
70+
* the power to the bus and controlled by a dedicated GPIO (see @sa
71+
* OneWireNg_BitBang::setupPwrCtrlGpio()). In this configuration the
72+
* service mimics the open-drain type of output. The approach may be
73+
* feasible if the GPIO is unable to provide sufficient power for
74+
* connected slaves working in parasite powering configuration.
75+
*
76+
* @param pin Arduino GPIO pin number used for bit-banging 1-wire bus.
77+
* @param pwrCtrlPin Arduino GPIO pin number controlling the switching
78+
* transistor.
79+
* @param pullUp If @c true configure internal pull-up resistor for the bus.
80+
*/
81+
OneWireNg_ArduinoESP8266(unsigned pin, unsigned pwrCtrlPin, bool pullUp):
82+
OneWireNg_BitBang(true)
83+
{
84+
_dtaPin = pin;
85+
_pwrCtrlPin = pwrCtrlPin;
86+
87+
initDtaGpio(pullUp);
88+
initPwrCtrlGpio();
89+
}
90+
91+
protected:
92+
virtual int readGpioIn(GpioType gpio)
93+
{
94+
UNUSED(gpio);
95+
return (_dtaPin < 16 ? GPIP(_dtaPin) :
96+
_dtaPin == 16 ? (GP16I & 0x01) : 1);
97+
}
98+
99+
virtual void writeGpioOut(GpioType gpio, int state)
100+
{
101+
if (gpio == GPIO_DTA) {
102+
__WRITE_GPIO(_dtaPin, state);
103+
} else {
104+
__WRITE_GPIO(_pwrCtrlPin, state);
105+
}
106+
}
107+
108+
virtual void setGpioAsInput(GpioType gpio)
109+
{
110+
UNUSED(gpio);
111+
__GPIO_AS_INPUT(_dtaPin);
112+
}
113+
114+
virtual void setGpioAsOutput(GpioType gpio, int state)
115+
{
116+
if (gpio == GPIO_DTA) {
117+
__GPIO_AS_OUTPUT(_dtaPin);
118+
__WRITE_GPIO(_dtaPin, state);
119+
} else {
120+
__GPIO_AS_OUTPUT(_pwrCtrlPin);
121+
__WRITE_GPIO(_pwrCtrlPin, state);
122+
}
123+
}
124+
125+
void initDtaGpio(bool pullUp)
126+
{
127+
pinMode(_dtaPin, (pullUp ? INPUT_PULLUP : INPUT));
128+
setupDtaGpio();
129+
}
130+
131+
void initPwrCtrlGpio(void)
132+
{
133+
pinMode(_pwrCtrlPin, OUTPUT_OPEN_DRAIN);
134+
setupPwrCtrlGpio(true);
135+
}
136+
137+
private:
138+
unsigned _dtaPin;
139+
unsigned _pwrCtrlPin;
140+
};
141+
142+
#undef __GPIO_AS_OUTPUT
143+
#undef __GPIO_AS_INPUT
144+
#undef __WRITE_GPIO
145+
146+
#endif /* __OWNG_ARDUINO_ESP8266__ */

0 commit comments

Comments
 (0)