Skip to content

Commit 5394a28

Browse files
committed
ESP32 platform support
1 parent 7297c96 commit 5394a28

5 files changed

Lines changed: 168 additions & 3 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ devices (e.g. Dallas/Maxim thermometers).
4545
* Arduino ESP8266.
4646
* Platform class: `OneWireNg_ArduinoESP8266`.
4747
* **Not tested**.
48+
* Arduino ESP32.
49+
* Platform class: `OneWireNg_ArduinoESP32`.
50+
* **Not tested**.
4851

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

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"type": "git",
1414
"url": "https://github.com/pstolarz/OneWireNg"
1515
},
16-
"version": "0.1.0",
16+
"version": "0.2.0",
1717
"homepage": "https://github.com/pstolarz/OneWireNg",
1818
"frameworks": "Arduino",
1919
"examples": [

library.properties

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=OneWireNg
2-
version=0.1.0
2+
version=0.2.0
33
author=Piotr Stolarz
44
maintainer=Piotr Stolarz <pstolarz@o2.pl>
55
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,esp8266
9+
architectures=avr,megaavr,esp8266,esp32

src/OneWireNg_ArduinoESP32.h

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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_ESP32__
14+
#define __OWNG_ARDUINO_ESP32__
15+
16+
#include <assert.h>
17+
#include "Arduino.h"
18+
#include "OneWireNg_BitBang.h"
19+
20+
#define __READ_GPIO(pin) \
21+
(pin < 32 ? ((GPIO.in >> pin) & 0x01) : \
22+
pin < 40 ? ((GPIO.in1.val >> (pin - 32)) & 0x01) : 1)
23+
24+
#define __WRITE_GPIO(pin, st) \
25+
if (pin < 32) { \
26+
if (st) GPIO.out_w1ts = ((uint32_t)1 << pin); \
27+
else GPIO.out_w1tc = ((uint32_t)1 << pin); \
28+
} else \
29+
if (pin < 34) { \
30+
if (st) GPIO.out1_w1ts.val = ((uint32_t)1 << (pin - 32)); \
31+
else GPIO.out1_w1tc.val = ((uint32_t)1 << (pin - 32)); \
32+
}
33+
34+
#define __GPIO_AS_INPUT(pin) \
35+
if(pin < 32) { \
36+
GPIO.enable_w1tc = ((uint32_t)1 << pin); \
37+
} else \
38+
if (pin < 40) { \
39+
GPIO.enable1_w1tc.val = ((uint32_t)1 << (pin - 32)); \
40+
}
41+
42+
#define __GPIO_AS_OUTPUT(pin) \
43+
if (pin < 32) { \
44+
GPIO.enable_w1ts = ((uint32_t)1 << pin); \
45+
} else \
46+
if (pin < 34) { \
47+
GPIO.enable1_w1ts.val = ((uint32_t)1 << (pin - 32)); \
48+
}
49+
50+
/**
51+
* Arduino ESP32 platform GPIO specific implementation.
52+
*/
53+
class OneWireNg_ArduinoESP32: public OneWireNg_BitBang
54+
{
55+
public:
56+
/**
57+
* OneWireNg 1-wire service for Arduino ESP32 platform.
58+
*
59+
* Bus powering is supported via switching its GPIO to the high state.
60+
* In this case the GPIO servers as a voltage source for connected slaves
61+
* working in parasite powering configuration.
62+
*
63+
* @param pin Arduino GPIO pin number used for bit-banging 1-wire bus.
64+
* @param pullUp If @c true configure internal pull-up resistor for the bus.
65+
*/
66+
OneWireNg_ArduinoESP32(unsigned pin, bool pullUp):
67+
OneWireNg_BitBang(false)
68+
{
69+
/* pins above 33 can only be inputs */
70+
assert(pin < 34);
71+
72+
_dtaPin = pin;
73+
initDtaGpio(pullUp);
74+
}
75+
76+
/**
77+
* OneWireNg 1-wire service for Arduino ESP32 platform.
78+
*
79+
* Bus powering is supported via a switching transistor providing
80+
* the power to the bus and controlled by a dedicated GPIO (see @sa
81+
* OneWireNg_BitBang::setupPwrCtrlGpio()). In this configuration the
82+
* service mimics the open-drain type of output. The approach may be
83+
* feasible if the GPIO is unable to provide sufficient power for
84+
* connected slaves working in parasite powering configuration.
85+
*
86+
* @param pin Arduino GPIO pin number used for bit-banging 1-wire bus.
87+
* @param pwrCtrlPin Arduino GPIO pin number controlling the switching
88+
* transistor.
89+
* @param pullUp If @c true configure internal pull-up resistor for the bus.
90+
*/
91+
OneWireNg_ArduinoESP32(unsigned pin, unsigned pwrCtrlPin, bool pullUp):
92+
OneWireNg_BitBang(true)
93+
{
94+
/* pins above 33 can only be inputs */
95+
assert(pin < 40 && pwrCtrlPin < 34);
96+
97+
_dtaPin = pin;
98+
_pwrCtrlPin = pwrCtrlPin;
99+
100+
initDtaGpio(pullUp);
101+
initPwrCtrlGpio();
102+
}
103+
104+
protected:
105+
virtual int readGpioIn(GpioType gpio)
106+
{
107+
UNUSED(gpio);
108+
return __READ_GPIO(_dtaPin);
109+
}
110+
111+
virtual void writeGpioOut(GpioType gpio, int state)
112+
{
113+
if (gpio == GPIO_DTA) {
114+
__WRITE_GPIO(_dtaPin, state);
115+
} else {
116+
__WRITE_GPIO(_pwrCtrlPin, state);
117+
}
118+
}
119+
120+
virtual void setGpioAsInput(GpioType gpio)
121+
{
122+
UNUSED(gpio);
123+
__GPIO_AS_INPUT(_dtaPin);
124+
}
125+
126+
virtual void setGpioAsOutput(GpioType gpio, int state)
127+
{
128+
if (gpio == GPIO_DTA) {
129+
__GPIO_AS_OUTPUT(_dtaPin);
130+
__WRITE_GPIO(_dtaPin, state);
131+
} else {
132+
__GPIO_AS_OUTPUT(_pwrCtrlPin);
133+
__WRITE_GPIO(_pwrCtrlPin, state);
134+
}
135+
}
136+
137+
void initDtaGpio(bool pullUp)
138+
{
139+
pinMode(_dtaPin, INPUT | (pullUp ? PULLUP : 0));
140+
setupDtaGpio();
141+
}
142+
143+
void initPwrCtrlGpio(void)
144+
{
145+
pinMode(_pwrCtrlPin, OUTPUT);
146+
setupPwrCtrlGpio(true);
147+
}
148+
149+
private:
150+
unsigned _dtaPin;
151+
unsigned _pwrCtrlPin;
152+
};
153+
154+
#undef __GPIO_AS_OUTPUT
155+
#undef __GPIO_AS_INPUT
156+
#undef __WRITE_GPIO
157+
#undef __READ_GPIO
158+
159+
#endif /* __OWNG_ARDUINO_ESP32__ */

src/OneWireNg_CurrentPlatform.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
#elif defined(ARDUINO_ARCH_ESP8266)
2727
# include "OneWireNg_ArduinoESP8266.h"
2828
# define OneWireNg_CurrentPlatform OneWireNg_ArduinoESP8266
29+
#elif defined(ARDUINO_ARCH_ESP32)
30+
# include "OneWireNg_ArduinoESP32.h"
31+
# define OneWireNg_CurrentPlatform OneWireNg_ArduinoESP32
2932
#else
3033
# define OneWireNg_CurrentPlatform
3134
# warning "Can't detect platform. Use proper class for the platform you are compiling for!"

0 commit comments

Comments
 (0)