/
esphal.cpp
317 lines (295 loc) · 8.41 KB
/
esphal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#include "esphome/core/esphal.h"
#include "esphome/core/helpers.h"
#include "esphome/core/defines.h"
#include "esphome/core/log.h"
#ifdef ARDUINO_ARCH_ESP8266
extern "C" {
typedef struct { // NOLINT
void *interruptInfo; // NOLINT
void *functionInfo; // NOLINT
} ArgStructure;
void ICACHE_RAM_ATTR __attachInterruptArg(uint8_t pin, void (*)(void *), void *fp, // NOLINT
int mode);
void ICACHE_RAM_ATTR __detachInterrupt(uint8_t pin); // NOLINT
};
#endif
namespace esphome {
static const char *TAG = "esphal";
GPIOPin::GPIOPin(uint8_t pin, uint8_t mode, bool inverted)
: pin_(pin),
mode_(mode),
inverted_(inverted),
#ifdef ARDUINO_ARCH_ESP8266
gpio_read_(pin < 16 ? &GPI : &GP16I),
gpio_mask_(pin < 16 ? (1UL << pin) : 1)
#endif
#ifdef ARDUINO_ARCH_ESP32
gpio_set_(pin < 32 ? &GPIO.out_w1ts : &GPIO.out1_w1ts.val),
gpio_clear_(pin < 32 ? &GPIO.out_w1tc : &GPIO.out1_w1tc.val),
gpio_read_(pin < 32 ? &GPIO.in : &GPIO.in1.val),
gpio_mask_(pin < 32 ? (1UL << pin) : (1UL << (pin - 32)))
#endif
{
}
const char *GPIOPin::get_pin_mode_name() const {
const char *mode_s;
switch (this->mode_) {
case INPUT:
mode_s = "INPUT";
break;
case OUTPUT:
mode_s = "OUTPUT";
break;
case INPUT_PULLUP:
mode_s = "INPUT_PULLUP";
break;
case OUTPUT_OPEN_DRAIN:
mode_s = "OUTPUT_OPEN_DRAIN";
break;
case SPECIAL:
mode_s = "SPECIAL";
break;
case FUNCTION_1:
mode_s = "FUNCTION_1";
break;
case FUNCTION_2:
mode_s = "FUNCTION_2";
break;
case FUNCTION_3:
mode_s = "FUNCTION_3";
break;
case FUNCTION_4:
mode_s = "FUNCTION_4";
break;
#ifdef ARDUINO_ARCH_ESP32
case PULLUP:
mode_s = "PULLUP";
break;
case PULLDOWN:
mode_s = "PULLDOWN";
break;
case INPUT_PULLDOWN:
mode_s = "INPUT_PULLDOWN";
break;
case OPEN_DRAIN:
mode_s = "OPEN_DRAIN";
break;
case FUNCTION_5:
mode_s = "FUNCTION_5";
break;
case FUNCTION_6:
mode_s = "FUNCTION_6";
break;
case ANALOG:
mode_s = "ANALOG";
break;
#endif
#ifdef ARDUINO_ARCH_ESP8266
case FUNCTION_0:
mode_s = "FUNCTION_0";
break;
case WAKEUP_PULLUP:
mode_s = "WAKEUP_PULLUP";
break;
case WAKEUP_PULLDOWN:
mode_s = "WAKEUP_PULLDOWN";
break;
case INPUT_PULLDOWN_16:
mode_s = "INPUT_PULLDOWN_16";
break;
#endif
default:
mode_s = "UNKNOWN";
break;
}
return mode_s;
}
unsigned char GPIOPin::get_pin() const { return this->pin_; }
unsigned char GPIOPin::get_mode() const { return this->mode_; }
bool GPIOPin::is_inverted() const { return this->inverted_; }
void GPIOPin::setup() { this->pin_mode(this->mode_); }
bool ICACHE_RAM_ATTR HOT GPIOPin::digital_read() {
return bool((*this->gpio_read_) & this->gpio_mask_) != this->inverted_;
}
bool ICACHE_RAM_ATTR HOT ISRInternalGPIOPin::digital_read() {
return bool((*this->gpio_read_) & this->gpio_mask_) != this->inverted_;
}
void ICACHE_RAM_ATTR HOT GPIOPin::digital_write(bool value) {
#ifdef ARDUINO_ARCH_ESP8266
if (this->pin_ != 16) {
if (value != this->inverted_) {
GPOS = this->gpio_mask_;
} else {
GPOC = this->gpio_mask_;
}
} else {
if (value != this->inverted_) {
GP16O |= 1;
} else {
GP16O &= ~1;
}
}
#endif
#ifdef ARDUINO_ARCH_ESP32
if (value != this->inverted_) {
(*this->gpio_set_) = this->gpio_mask_;
} else {
(*this->gpio_clear_) = this->gpio_mask_;
}
#endif
}
void ICACHE_RAM_ATTR HOT ISRInternalGPIOPin::digital_write(bool value) {
#ifdef ARDUINO_ARCH_ESP8266
if (this->pin_ != 16) {
if (value != this->inverted_) {
GPOS = this->gpio_mask_;
} else {
GPOC = this->gpio_mask_;
}
} else {
if (value != this->inverted_) {
GP16O |= 1;
} else {
GP16O &= ~1;
}
}
#endif
#ifdef ARDUINO_ARCH_ESP32
if (value != this->inverted_) {
(*this->gpio_set_) = this->gpio_mask_;
} else {
(*this->gpio_clear_) = this->gpio_mask_;
}
#endif
}
ISRInternalGPIOPin::ISRInternalGPIOPin(uint8_t pin,
#ifdef ARDUINO_ARCH_ESP32
volatile uint32_t *gpio_clear, volatile uint32_t *gpio_set,
#endif
volatile uint32_t *gpio_read, uint32_t gpio_mask, bool inverted)
: pin_(pin),
inverted_(inverted),
gpio_read_(gpio_read),
gpio_mask_(gpio_mask)
#ifdef ARDUINO_ARCH_ESP32
,
gpio_clear_(gpio_clear),
gpio_set_(gpio_set)
#endif
{
}
void ICACHE_RAM_ATTR ISRInternalGPIOPin::clear_interrupt() {
#ifdef ARDUINO_ARCH_ESP8266
GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, this->gpio_mask_);
#endif
#ifdef ARDUINO_ARCH_ESP32
if (this->pin_ < 32) {
GPIO.status_w1tc = this->gpio_mask_;
} else {
GPIO.status1_w1tc.intr_st = this->gpio_mask_;
}
#endif
}
void ICACHE_RAM_ATTR HOT GPIOPin::pin_mode(uint8_t mode) {
#ifdef ARDUINO_ARCH_ESP8266
if (this->pin_ == 16 && mode == INPUT_PULLUP) {
// pullups are not available on GPIO16, manually override with
// input mode.
pinMode(16, INPUT);
return;
}
#endif
pinMode(this->pin_, mode);
}
#ifdef ARDUINO_ARCH_ESP8266
struct ESPHomeInterruptFuncInfo {
void (*func)(void *);
void *arg;
};
void ICACHE_RAM_ATTR interrupt_handler(void *arg) {
ArgStructure *as = static_cast<ArgStructure *>(arg);
auto *info = static_cast<ESPHomeInterruptFuncInfo *>(as->functionInfo);
info->func(info->arg);
}
#endif
void GPIOPin::detach_interrupt() const { this->detach_interrupt_(); }
void GPIOPin::detach_interrupt_() const {
#ifdef ARDUINO_ARCH_ESP8266
__detachInterrupt(get_pin());
#endif
#ifdef ARDUINO_ARCH_ESP32
detachInterrupt(get_pin());
#endif
}
void GPIOPin::attach_interrupt_(void (*func)(void *), void *arg, int mode) const {
if (this->inverted_) {
if (mode == RISING) {
mode = FALLING;
} else if (mode == FALLING) {
mode = RISING;
}
}
#ifdef ARDUINO_ARCH_ESP8266
ArgStructure *as = new ArgStructure;
as->interruptInfo = nullptr;
as->functionInfo = new ESPHomeInterruptFuncInfo{
.func = func,
.arg = arg,
};
__attachInterruptArg(this->pin_, interrupt_handler, as, mode);
#endif
#ifdef ARDUINO_ARCH_ESP32
// work around issue https://github.com/espressif/arduino-esp32/pull/1776 in arduino core
// yet again proves how horrible code is there :( - how could that have been accepted...
auto *attach = reinterpret_cast<void (*)(uint8_t, void (*)(void *), void *, int)>(attachInterruptArg);
attach(this->pin_, func, arg, mode);
#endif
}
ISRInternalGPIOPin *GPIOPin::to_isr() const {
return new ISRInternalGPIOPin(this->pin_,
#ifdef ARDUINO_ARCH_ESP32
this->gpio_clear_, this->gpio_set_,
#endif
this->gpio_read_, this->gpio_mask_, this->inverted_);
}
void force_link_symbols() {
#ifdef ARDUINO_ARCH_ESP8266
// Tasmota uses magic bytes in the binary to check if an OTA firmware is compatible
// with their settings - ESPHome uses a different settings system (that can also survive
// erases). So set magic bytes indicating all tasmota versions are supported.
// This only adds 12 bytes of binary size, which is an acceptable price to pay for easier support
// for Tasmota.
// https://github.com/arendst/Tasmota/blob/b05301b1497942167a015a6113b7f424e42942cd/tasmota/settings.ino#L346-L380
// https://github.com/arendst/Tasmota/blob/b05301b1497942167a015a6113b7f424e42942cd/tasmota/i18n.h#L652-L654
const static uint32_t TASMOTA_MAGIC_BYTES[] PROGMEM = {0x5AA55AA5, 0xFFFFFFFF, 0xA55AA55A};
// Force link symbol by using a volatile integer (GCC attribute used does not work because of LTO)
volatile int x = 0;
x = TASMOTA_MAGIC_BYTES[x];
#endif
}
} // namespace esphome
#ifdef ARDUINO_ESP8266_RELEASE_2_3_0
// Fix 2.3.0 std missing memchr
extern "C" {
void *memchr(const void *s, int c, size_t n) {
if (n == 0)
return nullptr;
const uint8_t *p = reinterpret_cast<const uint8_t *>(s);
do {
if (*p++ == c)
return const_cast<void *>(reinterpret_cast<const void *>(p - 1));
} while (--n != 0);
return nullptr;
}
};
#endif
#ifdef ARDUINO_ARCH_ESP8266
extern "C" {
extern void resetPins() { // NOLINT
// Added in framework 2.7.0
// usually this sets up all pins to be in INPUT mode
// however, not strictly needed as we set up the pins properly
// ourselves and this causes pins to toggle during reboot.
}
}
#endif