ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
gpio.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ZEPHYR
5#include <zephyr/device.h>
6#include <zephyr/drivers/gpio.h>
7namespace esphome::zephyr {
8
9// Bundles the Zephyr gpio_callback together with the ESPHome ISR function and
10// argument. Keeping them in one POD struct lets the static handler recover the
11// owning data straight from the callback pointer via CONTAINER_OF, so no global
12// pin->instance lookup table is needed.
14 struct gpio_callback callback;
15 void (*func)(void *){nullptr};
16 void *arg{nullptr};
17};
18
19class ZephyrGPIOPin final : public GPIOPin {
20 public:
21 ZephyrGPIOPin(const device *gpio, int gpio_size, const char *pin_name_prefix) {
22 this->gpio_ = gpio;
23 this->gpio_size_ = gpio_size;
24 this->pin_name_prefix_ = pin_name_prefix;
25 }
26 void set_pin(uint8_t pin) { this->pin_ = pin; }
27 void set_inverted(bool inverted) { this->inverted_ = inverted; }
29
30 void setup() override;
31 void pin_mode(gpio::Flags flags) override;
32 bool digital_read() override;
33 void digital_write(bool value) override;
34 size_t dump_summary(char *buffer, size_t len) const override;
35 void detach_interrupt() const;
36 template<typename T> void attach_interrupt(void (*func)(T *), T *arg, gpio::InterruptType type) const {
37 this->attach_interrupt_(reinterpret_cast<void (*)(void *)>(func), arg, type);
38 }
40 uint8_t get_pin() const { return this->pin_; }
41 bool is_inverted() const { return this->inverted_; }
42 bool is_internal() override { return true; }
43 gpio::Flags get_flags() const override { return flags_; }
44
45 protected:
46 void attach_interrupt_(void (*func)(void *), void *arg, gpio::InterruptType type) const;
47 const device *gpio_{nullptr};
48 const char *pin_name_prefix_{nullptr};
50 uint8_t pin_;
51 uint8_t gpio_size_{};
52 bool inverted_{};
53 bool value_{false};
54
55 // attach_interrupt_()/detach_interrupt() are const, so their interrupt state is mutable.
57};
58
59} // namespace esphome::zephyr
60
61#endif // USE_ZEPHYR
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio_pin.h:98
const device * gpio_
Definition gpio.h:47
void digital_write(bool value) override
Definition gpio.cpp:144
void detach_interrupt() const
Definition gpio.cpp:154
ZephyrGPIOPin(const device *gpio, int gpio_size, const char *pin_name_prefix)
Definition gpio.h:21
const char * pin_name_prefix_
Definition gpio.h:48
void set_flags(gpio::Flags flags)
Definition gpio.h:28
uint8_t get_pin() const
Definition gpio.h:40
bool digital_read() override
Definition gpio.cpp:137
bool is_internal() override
Definition gpio.h:42
bool is_inverted() const
Definition gpio.h:41
size_t dump_summary(char *buffer, size_t len) const override
Definition gpio.cpp:133
void attach_interrupt_(void(*func)(void *), void *arg, gpio::InterruptType type) const
Definition gpio.cpp:83
void attach_interrupt(void(*func)(T *), T *arg, gpio::InterruptType type) const
Definition gpio.h:36
gpio::Flags get_flags() const override
Definition gpio.h:43
void set_pin(uint8_t pin)
Definition gpio.h:26
ZephyrGPIOInterrupt interrupt_
Definition gpio.h:56
ISRInternalGPIOPin to_isr() const
Definition gpio.cpp:74
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:122
void set_inverted(bool inverted)
Definition gpio.h:27
uint16_t flags
uint8_t type
uint32_t buffer
Definition gif.cpp:50
const void size_t len
Definition hal.h:68
struct gpio_callback callback
Definition gpio.h:14