ESPHome 2026.12.0-dev
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/hal.h"
5#include "esphome/core/log.h"
6
7namespace esphome::esp8266 {
8
9ESPHOME_LOG_TAG(TAG, "esp8266");
10
11static int flags_to_mode(gpio::Flags flags, uint8_t pin) {
12 flags =
15 return OUTPUT;
16 }
17 if (flags == gpio::FLAG_INPUT) {
18 return INPUT;
19 }
21 if (pin == 16) {
22 // GPIO16 doesn't have a pullup, so pinMode would fail.
23 // However, sometimes this method is called with pullup mode anyway
24 // for example from dallas one_wire. For those cases convert this
25 // to a INPUT mode.
26 return INPUT;
27 }
28 return INPUT_PULLUP;
29 }
31 return INPUT_PULLDOWN_16;
32 }
34 return OUTPUT_OPEN_DRAIN;
35 }
36 return INPUT;
37}
38
39// Slot of each pin's GPF register (0x804 + 4 * slot), so ISR code does not read the core's PROGMEM table.
40static constexpr uint32_t GPF_SLOTS_0_7 = 0x76fe4d5c;
41static constexpr uint32_t GPF_SLOTS_8_15 = 0x3210ba98;
42static inline volatile uint32_t &IRAM_ATTR isr_gpf(uint8_t num) {
43 const uint32_t slots = num < 8 ? GPF_SLOTS_0_7 : GPF_SLOTS_8_15;
44 return ESP8266_REG(0x804 + 4 * ((slots >> (4 * (num & 7))) & 0xF));
45}
46
48
49void ESP8266GPIOPin::attach_interrupt_(void (*func)(void *), void *arg, gpio::InterruptType type) const {
50 uint8_t arduino_mode = 0;
51 switch (type) {
53 arduino_mode = inverted_ ? FALLING : RISING;
54 break;
56 arduino_mode = inverted_ ? RISING : FALLING;
57 break;
59 arduino_mode = CHANGE;
60 break;
62 arduino_mode = inverted_ ? ONHIGH : ONLOW;
63 break;
65 arduino_mode = inverted_ ? ONLOW : ONHIGH;
66 break;
67 }
68
69 attachInterruptArg(pin_, func, arg, arduino_mode);
70}
72 pinMode(pin_, flags_to_mode(flags, pin_)); // NOLINT
73}
74
75size_t ESP8266GPIOPin::dump_summary(char *buffer, size_t len) const {
76 return buf_append_printf(buffer, len, 0, "GPIO%u", this->pin_);
77}
78
80 return bool(digitalRead(pin_)) != inverted_; // NOLINT
81}
83 digitalWrite(pin_, value != inverted_ ? 1 : 0); // NOLINT
84}
85void ESP8266GPIOPin::detach_interrupt() const { detachInterrupt(pin_); }
86
87} // namespace esphome::esp8266
88
89namespace esphome {
90
92using esp8266::isr_gpf;
93
94bool IRAM_ATTR ISRInternalGPIOPin::digital_read() {
95 auto *pin = static_cast<ESP8266GPIOPin *>(this->arg_);
96 const uint8_t num = pin->get_pin();
97 return bool(num < 16 ? GPI & (1 << num) : GP16I & 1) != pin->is_inverted();
98}
99
100void IRAM_ATTR ISRInternalGPIOPin::digital_write(bool value) {
101 auto *pin = static_cast<ESP8266GPIOPin *>(this->arg_);
102 const uint8_t num = pin->get_pin();
103 if (num < 16) {
104 if (value != pin->is_inverted()) {
105 GPOS = 1 << num;
106 } else {
107 GPOC = 1 << num;
108 }
109 } else {
110 GP16O = value != pin->is_inverted() ? GP16O | 1 : GP16O & ~1;
111 }
112}
113
115 auto *pin = static_cast<ESP8266GPIOPin *>(this->arg_);
116 // NOLINTNEXTLINE(clang-analyzer-core.FixedAddressDereference) -- GPIO_REG_WRITE is MMIO at a fixed address
117 GPIO_REG_WRITE(GPIO_STATUS_W1TC_ADDRESS, 1UL << pin->get_pin());
118}
119
121 const uint8_t num = static_cast<ESP8266GPIOPin *>(this->arg_)->get_pin();
122 if (num < 16) {
123 if (flags & gpio::FLAG_OUTPUT) {
124 GPES = 1 << num;
126 GPC(num) = GPC(num) | (1 << GPCD);
127 } else {
128 GPC(num) &= ~(1 << GPCD);
129 }
130 } else if (flags & gpio::FLAG_INPUT) {
131 GPEC = 1 << num;
132 }
133 if (flags & gpio::FLAG_PULLUP) {
134 isr_gpf(num) = isr_gpf(num) | (1 << GPFPU);
135 GPC(num) = GPC(num) | (1 << GPCD);
136 } else {
137 isr_gpf(num) = isr_gpf(num) & ~(1 << GPFPU);
138 }
139 } else {
140 GP16E = flags & gpio::FLAG_OUTPUT ? GP16E | 1 : GP16E & ~1;
141 GPF16 = flags & gpio::FLAG_PULLDOWN ? GPF16 | (1 << GP16FPD) : GPF16 & ~(1 << GP16FPD);
142 }
143}
144
145} // namespace esphome
146
147#endif // USE_ESP8266
ISR-safe handle to an internal pin (no virtual calls); every handle points at the pin itself and shar...
Definition gpio_pin.h:126
void digital_write(bool value)
Definition gpio.cpp:211
void pin_mode(gpio::Flags flags)
Definition gpio.cpp:223
friend class esp8266::ESP8266GPIOPin
Definition gpio_pin.h:130
ISRInternalGPIOPin to_isr() const
Definition gpio.cpp:47
void attach_interrupt_(void(*func)(void *), void *arg, gpio::InterruptType type) const
Definition gpio.cpp:49
bool digital_read() override
Definition gpio.cpp:79
void digital_write(bool value) override
Definition gpio.cpp:82
uint8_t get_pin() const
Definition gpio.h:26
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:71
size_t dump_summary(char *buffer, size_t len) const override
Definition gpio.cpp:75
uint16_t flags
uint8_t type
uint32_t buffer
Definition gif.cpp:50
ESPHOME_LOG_TAG(TAG, "esp8266")
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
@ INTERRUPT_RISING_EDGE
Definition gpio_pin.h:52
@ INTERRUPT_HIGH_LEVEL
Definition gpio_pin.h:56
@ INTERRUPT_LOW_LEVEL
Definition gpio_pin.h:55
@ INTERRUPT_ANY_EDGE
Definition gpio_pin.h:54
uint32_t value
Definition hal.cpp:59
const void size_t len
Definition hal.h:68
static void uint32_t