ESPHome 2026.12.0-dev
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
64
65void ZephyrGPIOPin::attach_interrupt_(void (*func)(void *), void *arg, gpio::InterruptType type) const {
66 if (!device_is_ready(this->gpio_)) {
67 ESP_LOGE(TAG, "Cannot attach interrupt: GPIO device not ready");
68 return;
69 }
70
71 // Drop any interrupt previously attached to this pin before re-registering.
72 this->detach_interrupt();
73
74 this->interrupt_.func = func;
75 this->interrupt_.arg = arg;
76
77 uint8_t port_pin = this->pin_ % this->gpio_size_;
78 gpio_init_callback(&this->interrupt_.callback, gpio_interrupt_handler, BIT(port_pin));
79
80 int ret = gpio_add_callback(this->gpio_, &this->interrupt_.callback);
81 if (ret != 0) {
82 ESP_LOGE(TAG, "gpio_add_callback failed for pin %u: %d", this->pin_, ret);
83 return;
84 }
85
86 ret = gpio_pin_interrupt_configure(this->gpio_, port_pin, interrupt_type_to_flags(type, this->inverted_));
87 if (ret != 0) {
88 ESP_LOGE(TAG, "gpio_pin_interrupt_configure failed for pin %u: %d", this->pin_, ret);
89 gpio_remove_callback(this->gpio_, &this->interrupt_.callback);
90 return;
91 }
92
93 ESP_LOGD(TAG, "Interrupt attached to pin %u (type=%d)", this->pin_, (int) type);
94}
95
97 if (!device_is_ready(this->gpio_)) {
98 ESP_LOGE(TAG, "gpio %u is not ready.", this->pin_);
99 return;
100 }
101 this->pin_mode(this->flags_);
102}
103
105 if (nullptr == this->gpio_) {
106 return;
107 }
108 gpio_flags_t mode = flags_to_mode(flags);
109 if (flags & gpio::FLAG_OUTPUT) {
110 mode |= this->value_ != this->inverted_ ? GPIO_OUTPUT_INIT_HIGH : GPIO_OUTPUT_INIT_LOW;
111 }
112 auto ret = gpio_pin_configure(this->gpio_, this->pin_ % this->gpio_size_, mode);
113 if (ret != 0) {
114 ESP_LOGE(TAG, "gpio %u cannot be configured %d.", this->pin_, ret);
115 }
116}
117
118size_t ZephyrGPIOPin::dump_summary(char *buffer, size_t len) const {
119 return snprintf(buffer, len, "GPIO%u, %s%u", this->pin_, this->pin_name_prefix_, this->pin_ % this->gpio_size_);
120}
121
123 if (nullptr == this->gpio_) {
124 return false;
125 }
126 return bool(gpio_pin_get(this->gpio_, this->pin_ % this->gpio_size_) != this->inverted_);
127}
128
130 // make sure that value is not ignored since it can be inverted e.g. on switch side
131 // that way init state should be correct
132 this->value_ = value;
133 if (nullptr == this->gpio_) {
134 return;
135 }
136 gpio_pin_set(this->gpio_, this->pin_ % this->gpio_size_, value != this->inverted_ ? 1 : 0);
137}
138
140 if (this->gpio_ == nullptr) {
141 return;
142 }
143
144 uint8_t port_pin = this->pin_ % this->gpio_size_;
145 gpio_pin_interrupt_configure(this->gpio_, port_pin, GPIO_INT_DISABLE);
146 gpio_remove_callback(this->gpio_, &this->interrupt_.callback);
147
148 this->interrupt_.func = nullptr;
149 this->interrupt_.arg = nullptr;
150}
151
152} // namespace zephyr
153
154bool IRAM_ATTR ISRInternalGPIOPin::digital_read() {
155 auto *pin = static_cast<zephyr::ZephyrGPIOPin *>(this->arg_);
156 if (pin == nullptr || pin->gpio_ == nullptr)
157 return false;
158 return bool(gpio_pin_get(pin->gpio_, pin->pin_ % pin->gpio_size_) != pin->inverted_);
159}
160
161void IRAM_ATTR ISRInternalGPIOPin::digital_write(bool value) {
162 auto *pin = static_cast<zephyr::ZephyrGPIOPin *>(this->arg_);
163 if (pin == nullptr || pin->gpio_ == nullptr)
164 return;
165 gpio_pin_set(pin->gpio_, pin->pin_ % pin->gpio_size_, value != pin->inverted_ ? 1 : 0);
166}
167
169 auto *pin = static_cast<zephyr::ZephyrGPIOPin *>(this->arg_);
170 if (pin == nullptr || pin->gpio_ == nullptr)
171 return;
172 gpio_pin_configure(pin->gpio_, pin->pin_ % pin->gpio_size_, zephyr::flags_to_mode(flags));
173}
174
175} // namespace esphome
176
177#endif
BedjetMode mode
BedJet operating mode.
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
const device * gpio_
Definition gpio.h:50
void digital_write(bool value) override
Definition gpio.cpp:129
void detach_interrupt() const
Definition gpio.cpp:139
const char * pin_name_prefix_
Definition gpio.h:51
void setup() override
Definition gpio.cpp:96
friend class esphome::ISRInternalGPIOPin
Definition gpio.h:21
bool digital_read() override
Definition gpio.cpp:122
size_t dump_summary(char *buffer, size_t len) const override
Definition gpio.cpp:118
void attach_interrupt_(void(*func)(void *), void *arg, gpio::InterruptType type) const
Definition gpio.cpp:65
ZephyrGPIOInterrupt interrupt_
Definition gpio.h:59
ISRInternalGPIOPin to_isr() const
Definition gpio.cpp:63
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:104
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:59
const void size_t len
Definition hal.h:68
static void uint32_t
struct gpio_callback callback
Definition gpio.h:14