ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
gpio.cpp
Go to the documentation of this file.
1#ifdef USE_ZEPHYR
2#include "gpio.h"
3#include "esphome/core/hal.h"
4#include <zephyr/drivers/gpio.h>
5#include <zephyr/sys/util.h>
6#include "esphome/core/log.h"
7
8namespace esphome {
9namespace zephyr {
10
11ESPHOME_LOG_TAG(TAG, "zephyr");
12
13// Without a GPIO_OUTPUT_INIT_* bit, Zephyr leaves the output level as it is.
14static gpio_flags_t flags_to_mode(gpio::Flags flags) {
15 gpio_flags_t ret = 0;
16 if (flags & gpio::FLAG_INPUT) {
17 ret |= GPIO_INPUT;
18 }
20 ret |= GPIO_OUTPUT;
21 }
23 ret |= GPIO_PULL_UP;
24 }
26 ret |= GPIO_PULL_DOWN;
27 }
29 ret |= GPIO_OPEN_DRAIN;
30 }
31 return ret;
32}
33
34// ESPHome's InterruptType is expressed in logical levels, but the pin is configured active-high in Zephyr (inversion is
35// applied in software by digital_read()/digital_write(), see the `!= inverted_` convention below). So when the pin is
36// inverted we must swap the physical edge/level the interrupt arms on: a logical rising edge is a physical falling
37// edge, etc. GPIO_INT_EDGE_BOTH is symmetric and needs no swap.
38static gpio_flags_t interrupt_type_to_flags(gpio::InterruptType type, bool inverted) {
39 switch (type) {
41 return inverted ? GPIO_INT_EDGE_FALLING : GPIO_INT_EDGE_RISING;
43 return inverted ? GPIO_INT_EDGE_RISING : GPIO_INT_EDGE_FALLING;
45 return GPIO_INT_EDGE_BOTH;
47 return inverted ? GPIO_INT_LEVEL_HIGH : GPIO_INT_LEVEL_LOW;
49 return inverted ? GPIO_INT_LEVEL_LOW : GPIO_INT_LEVEL_HIGH;
50 }
51 return inverted ? GPIO_INT_EDGE_FALLING : GPIO_INT_EDGE_RISING;
52}
53
54// Zephyr calls this with a pointer to the gpio_callback the interrupt fired on.
55// Recover the owning ZephyrGPIOInterrupt and dispatch to the ESPHome ISR.
56static void gpio_interrupt_handler(const device * /*dev*/, gpio_callback *cb, uint32_t /*pins*/) {
57 auto *interrupt = CONTAINER_OF(cb, ZephyrGPIOInterrupt, callback);
58 if (interrupt->func != nullptr) {
59 interrupt->func(interrupt->arg);
60 }
61}
62
63struct ISRPinArg {
64 const device *gpio;
65 uint8_t pin;
66 uint8_t gpio_size;
67 bool inverted;
68};
69
71 auto *arg = new ISRPinArg{}; // NOLINT(cppcoreguidelines-owning-memory)
72 arg->gpio = this->gpio_;
73 arg->pin = this->pin_;
74 arg->gpio_size = this->gpio_size_;
75 arg->inverted = this->inverted_;
76 return ISRInternalGPIOPin((void *) arg);
77}
78
79void ZephyrGPIOPin::attach_interrupt_(void (*func)(void *), void *arg, gpio::InterruptType type) const {
80 if (!device_is_ready(this->gpio_)) {
81 ESP_LOGE(TAG, "Cannot attach interrupt: GPIO device not ready");
82 return;
83 }
84
85 // Drop any interrupt previously attached to this pin before re-registering.
86 this->detach_interrupt();
87
88 this->interrupt_.func = func;
89 this->interrupt_.arg = arg;
90
91 uint8_t port_pin = this->pin_ % this->gpio_size_;
92 gpio_init_callback(&this->interrupt_.callback, gpio_interrupt_handler, BIT(port_pin));
93
94 int ret = gpio_add_callback(this->gpio_, &this->interrupt_.callback);
95 if (ret != 0) {
96 ESP_LOGE(TAG, "gpio_add_callback failed for pin %u: %d", this->pin_, ret);
97 return;
98 }
99
100 ret = gpio_pin_interrupt_configure(this->gpio_, port_pin, interrupt_type_to_flags(type, this->inverted_));
101 if (ret != 0) {
102 ESP_LOGE(TAG, "gpio_pin_interrupt_configure failed for pin %u: %d", this->pin_, ret);
103 gpio_remove_callback(this->gpio_, &this->interrupt_.callback);
104 return;
105 }
106
107 ESP_LOGD(TAG, "Interrupt attached to pin %u (type=%d)", this->pin_, (int) type);
108}
109
111 if (!device_is_ready(this->gpio_)) {
112 ESP_LOGE(TAG, "gpio %u is not ready.", this->pin_);
113 return;
114 }
115 this->pin_mode(this->flags_);
116}
117
119 if (nullptr == this->gpio_) {
120 return;
121 }
122 gpio_flags_t mode = flags_to_mode(flags);
123 if (flags & gpio::FLAG_OUTPUT) {
124 mode |= this->value_ != this->inverted_ ? GPIO_OUTPUT_INIT_HIGH : GPIO_OUTPUT_INIT_LOW;
125 }
126 auto ret = gpio_pin_configure(this->gpio_, this->pin_ % this->gpio_size_, mode);
127 if (ret != 0) {
128 ESP_LOGE(TAG, "gpio %u cannot be configured %d.", this->pin_, ret);
129 }
130}
131
132size_t ZephyrGPIOPin::dump_summary(char *buffer, size_t len) const {
133 return snprintf(buffer, len, "GPIO%u, %s%u", this->pin_, this->pin_name_prefix_, this->pin_ % this->gpio_size_);
134}
135
137 if (nullptr == this->gpio_) {
138 return false;
139 }
140 return bool(gpio_pin_get(this->gpio_, this->pin_ % this->gpio_size_) != this->inverted_);
141}
142
144 // make sure that value is not ignored since it can be inverted e.g. on switch side
145 // that way init state should be correct
146 this->value_ = value;
147 if (nullptr == this->gpio_) {
148 return;
149 }
150 gpio_pin_set(this->gpio_, this->pin_ % this->gpio_size_, value != this->inverted_ ? 1 : 0);
151}
152
154 if (this->gpio_ == nullptr) {
155 return;
156 }
157
158 uint8_t port_pin = this->pin_ % this->gpio_size_;
159 gpio_pin_interrupt_configure(this->gpio_, port_pin, GPIO_INT_DISABLE);
160 gpio_remove_callback(this->gpio_, &this->interrupt_.callback);
161
162 this->interrupt_.func = nullptr;
163 this->interrupt_.arg = nullptr;
164}
165
166} // namespace zephyr
167
168bool IRAM_ATTR ISRInternalGPIOPin::digital_read() {
169 auto *arg = (zephyr::ISRPinArg *) this->arg_;
170 if (arg == nullptr || arg->gpio == nullptr) {
171 return false;
172 }
173 return bool(gpio_pin_get(arg->gpio, arg->pin % arg->gpio_size) != arg->inverted);
174}
175
176void IRAM_ATTR ISRInternalGPIOPin::digital_write(bool value) {
177 auto *arg = (zephyr::ISRPinArg *) this->arg_;
178 if (arg == nullptr || arg->gpio == nullptr) {
179 return;
180 }
181 gpio_pin_set(arg->gpio, arg->pin % arg->gpio_size, value != arg->inverted ? 1 : 0);
182}
183
185 auto *arg = (zephyr::ISRPinArg *) this->arg_;
186 if (arg == nullptr || arg->gpio == nullptr) {
187 return;
188 }
189 gpio_pin_configure(arg->gpio, arg->pin % arg->gpio_size, zephyr::flags_to_mode(flags));
190}
191
192} // namespace esphome
193
194#endif
BedjetMode mode
BedJet operating mode.
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio_pin.h:98
void digital_write(bool value)
Definition gpio.cpp:234
void pin_mode(gpio::Flags flags)
Definition gpio.cpp:246
const device * gpio_
Definition gpio.h:47
void digital_write(bool value) override
Definition gpio.cpp:143
void detach_interrupt() const
Definition gpio.cpp:153
const char * pin_name_prefix_
Definition gpio.h:48
bool digital_read() override
Definition gpio.cpp:136
size_t dump_summary(char *buffer, size_t len) const override
Definition gpio.cpp:132
void attach_interrupt_(void(*func)(void *), void *arg, gpio::InterruptType type) const
Definition gpio.cpp:79
ZephyrGPIOInterrupt interrupt_
Definition gpio.h:56
ISRInternalGPIOPin to_isr() const
Definition gpio.cpp:70
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:118
uint16_t flags
uint8_t type
uint32_t buffer
Definition gif.cpp:50
int ret
@ 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
ESPHOME_LOG_TAG(TAG, "zephyr")
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
static void uint32_t
struct gpio_callback callback
Definition gpio.h:14