ESPHome 2025.5.0
Loading...
Searching...
No Matches
gpio.cpp
Go to the documentation of this file.
1#ifdef USE_ESP8266
2
3#include "gpio.h"
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace esp8266 {
8
9static const char *const TAG = "esp8266";
10
11static int flags_to_mode(gpio::Flags flags, uint8_t pin) {
12 if (flags == gpio::FLAG_INPUT) { // NOLINT(bugprone-branch-clone)
13 return INPUT;
14 } else if (flags == gpio::FLAG_OUTPUT) {
15 return OUTPUT;
16 } else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLUP)) {
17 if (pin == 16) {
18 // GPIO16 doesn't have a pullup, so pinMode would fail.
19 // However, sometimes this method is called with pullup mode anyway
20 // for example from dallas one_wire. For those cases convert this
21 // to a INPUT mode.
22 return INPUT;
23 }
24 return INPUT_PULLUP;
25 } else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLDOWN)) {
26 return INPUT_PULLDOWN_16;
27 } else if (flags == (gpio::FLAG_OUTPUT | gpio::FLAG_OPEN_DRAIN)) {
28 return OUTPUT_OPEN_DRAIN;
29 } else {
30 return 0;
31 }
32}
33
34struct ISRPinArg {
35 uint8_t pin;
36 bool inverted;
37 volatile uint32_t *in_reg;
38 volatile uint32_t *out_set_reg;
39 volatile uint32_t *out_clr_reg;
40 volatile uint32_t *mode_set_reg;
41 volatile uint32_t *mode_clr_reg;
42 volatile uint32_t *func_reg;
43 uint32_t mask;
44};
45
47 auto *arg = new ISRPinArg{}; // NOLINT(cppcoreguidelines-owning-memory)
48 arg->pin = this->pin_;
49 arg->inverted = this->inverted_;
50 if (this->pin_ < 16) {
51 arg->in_reg = &GPI;
52 arg->out_set_reg = &GPOS;
53 arg->out_clr_reg = &GPOC;
54 arg->mode_set_reg = &GPES;
55 arg->mode_clr_reg = &GPEC;
56 arg->func_reg = &GPF(this->pin_);
57 arg->mask = 1 << this->pin_;
58 } else {
59 arg->in_reg = &GP16I;
60 arg->out_set_reg = &GP16O;
61 arg->out_clr_reg = nullptr;
62 arg->mode_set_reg = &GP16E;
63 arg->mode_clr_reg = nullptr;
64 arg->func_reg = &GPF16;
65 arg->mask = 1;
66 }
67 return ISRInternalGPIOPin((void *) arg);
68}
69
70void ESP8266GPIOPin::attach_interrupt(void (*func)(void *), void *arg, gpio::InterruptType type) const {
71 uint8_t arduino_mode = 0;
72 switch (type) {
74 arduino_mode = inverted_ ? FALLING : RISING;
75 break;
77 arduino_mode = inverted_ ? RISING : FALLING;
78 break;
80 arduino_mode = CHANGE;
81 break;
83 arduino_mode = inverted_ ? ONHIGH : ONLOW;
84 break;
86 arduino_mode = inverted_ ? ONLOW : ONHIGH;
87 break;
88 }
89
90 attachInterruptArg(pin_, func, arg, arduino_mode);
91}
93 pinMode(pin_, flags_to_mode(flags, pin_)); // NOLINT
94}
95
96std::string ESP8266GPIOPin::dump_summary() const {
97 char buffer[32];
98 snprintf(buffer, sizeof(buffer), "GPIO%u", pin_);
99 return buffer;
100}
101
103 return bool(digitalRead(pin_)) != inverted_; // NOLINT
104}
106 digitalWrite(pin_, value != inverted_ ? 1 : 0); // NOLINT
107}
108void ESP8266GPIOPin::detach_interrupt() const { detachInterrupt(pin_); }
109
110} // namespace esp8266
111
112using namespace esp8266;
113
114bool IRAM_ATTR ISRInternalGPIOPin::digital_read() {
115 auto *arg = reinterpret_cast<ISRPinArg *>(this->arg_);
116 return bool(*arg->in_reg & arg->mask) != arg->inverted;
117}
118
119void IRAM_ATTR ISRInternalGPIOPin::digital_write(bool value) {
120 auto *arg = reinterpret_cast<ISRPinArg *>(arg_);
121 if (arg->pin < 16) {
122 if (value != arg->inverted) {
123 *arg->out_set_reg = arg->mask;
124 } else {
125 *arg->out_clr_reg = arg->mask;
126 }
127 } else {
128 if (value != arg->inverted) {
129 *arg->out_set_reg |= 1;
130 } else {
131 *arg->out_set_reg &= ~1;
132 }
133 }
134}
135
137 auto *arg = reinterpret_cast<ISRPinArg *>(arg_);
138 GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, 1UL << arg->pin);
139}
140
141void IRAM_ATTR ISRInternalGPIOPin::pin_mode(gpio::Flags flags) {
142 auto *arg = reinterpret_cast<ISRPinArg *>(this->arg_);
143 if (arg->pin < 16) {
144 if (flags & gpio::FLAG_OUTPUT) {
145 *arg->mode_set_reg = arg->mask;
146 } else {
147 *arg->mode_clr_reg = arg->mask;
148 }
149 if (flags & gpio::FLAG_PULLUP) {
150 *arg->func_reg |= 1 << GPFPU;
151 } else {
152 *arg->func_reg &= ~(1 << GPFPU);
153 }
154 } else {
155 if (flags & gpio::FLAG_OUTPUT) {
156 *arg->mode_set_reg |= 1;
157 } else {
158 *arg->mode_set_reg &= ~1;
159 }
160 if (flags & gpio::FLAG_PULLDOWN) {
161 *arg->func_reg |= 1 << GP16FPD;
162 } else {
163 *arg->func_reg &= ~(1 << GP16FPD);
164 }
165 }
166}
167
168} // namespace esphome
169
170#endif // USE_ESP8266
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio.h:73
void digital_write(bool value)
Definition gpio.cpp:147
void pin_mode(gpio::Flags flags)
Definition gpio.cpp:156
bool digital_read() override
Definition gpio.cpp:102
ISRInternalGPIOPin to_isr() const override
Definition gpio.cpp:46
void digital_write(bool value) override
Definition gpio.cpp:105
void attach_interrupt(void(*func)(void *), void *arg, gpio::InterruptType type) const override
Definition gpio.cpp:70
std::string dump_summary() const override
Definition gpio.cpp:96
void detach_interrupt() const override
Definition gpio.cpp:108
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:92
uint8_t type
@ INTERRUPT_FALLING_EDGE
Definition gpio.h:42
@ INTERRUPT_RISING_EDGE
Definition gpio.h:41
@ INTERRUPT_HIGH_LEVEL
Definition gpio.h:45
@ INTERRUPT_LOW_LEVEL
Definition gpio.h:44
@ INTERRUPT_ANY_EDGE
Definition gpio.h:43
@ FLAG_OUTPUT
Definition gpio.h:19
@ FLAG_OPEN_DRAIN
Definition gpio.h:20
@ FLAG_PULLUP
Definition gpio.h:21
@ FLAG_INPUT
Definition gpio.h:18
@ FLAG_PULLDOWN
Definition gpio.h:22
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7