ESPHome 2026.8.0-dev
Loading...
Searching...
No Matches
gpio.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ZEPHYR
4#include "esphome/core/hal.h"
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 InternalGPIOPin {
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 override;
36 ISRInternalGPIOPin to_isr() const override;
37 uint8_t get_pin() const override { return this->pin_; }
38 bool is_inverted() const override { return this->inverted_; }
39 gpio::Flags get_flags() const override { return flags_; }
40
41 protected:
42 void attach_interrupt(void (*func)(void *), void *arg, gpio::InterruptType type) const override;
43 const device *gpio_{nullptr};
44 const char *pin_name_prefix_{nullptr};
46 uint8_t pin_;
47 uint8_t gpio_size_{};
48 bool inverted_{};
49 bool value_{false};
50
51 // attach_interrupt()/detach_interrupt() are const (matching the base class), so
52 // the interrupt state they manage has to be mutable.
54};
55
56} // namespace esphome::zephyr
57
58#endif // USE_ZEPHYR
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio.h:85
const device * gpio_
Definition gpio.h:43
void digital_write(bool value) override
Definition gpio.cpp:143
void attach_interrupt(void(*func)(void *), void *arg, gpio::InterruptType type) const override
Definition gpio.cpp:82
ZephyrGPIOPin(const device *gpio, int gpio_size, const char *pin_name_prefix)
Definition gpio.h:21
const char * pin_name_prefix_
Definition gpio.h:44
void set_flags(gpio::Flags flags)
Definition gpio.h:28
bool digital_read() override
Definition gpio.cpp:136
bool is_inverted() const override
Definition gpio.h:38
uint8_t get_pin() const override
Definition gpio.h:37
size_t dump_summary(char *buffer, size_t len) const override
Definition gpio.cpp:132
ISRInternalGPIOPin to_isr() const override
Definition gpio.cpp:73
gpio::Flags get_flags() const override
Definition gpio.h:39
void set_pin(uint8_t pin)
Definition gpio.h:26
ZephyrGPIOInterrupt interrupt_
Definition gpio.h:53
void detach_interrupt() const override
Definition gpio.cpp:153
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:121
void set_inverted(bool inverted)
Definition gpio.h:27
uint16_t type
uint16_t flags
const void size_t len
Definition hal.h:64
struct gpio_callback callback
Definition gpio.h:14