ESPHome 2025.9.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 <zephyr/drivers/gpio.h>
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace zephyr {
8
9static const char *const TAG = "zephyr";
10
11static int flags_to_mode(gpio::Flags flags, bool inverted, bool value) {
12 int ret = 0;
13 if (flags & gpio::FLAG_INPUT) {
14 ret |= GPIO_INPUT;
15 }
16 if (flags & gpio::FLAG_OUTPUT) {
17 ret |= GPIO_OUTPUT;
18 if (value != inverted) {
19 ret |= GPIO_OUTPUT_INIT_HIGH;
20 } else {
21 ret |= GPIO_OUTPUT_INIT_LOW;
22 }
23 }
24 if (flags & gpio::FLAG_PULLUP) {
25 ret |= GPIO_PULL_UP;
26 }
27 if (flags & gpio::FLAG_PULLDOWN) {
28 ret |= GPIO_PULL_DOWN;
29 }
30 if (flags & gpio::FLAG_OPEN_DRAIN) {
31 ret |= GPIO_OPEN_DRAIN;
32 }
33 return ret;
34}
35
36struct ISRPinArg {
37 uint8_t pin;
38 bool inverted;
39};
40
42 auto *arg = new ISRPinArg{}; // NOLINT(cppcoreguidelines-owning-memory)
43 arg->pin = this->pin_;
44 arg->inverted = this->inverted_;
45 return ISRInternalGPIOPin((void *) arg);
46}
47
48void ZephyrGPIOPin::attach_interrupt(void (*func)(void *), void *arg, gpio::InterruptType type) const {
49 // TODO
50}
51
53 const struct device *gpio = nullptr;
54 if (this->pin_ < 32) {
55#define GPIO0 DT_NODELABEL(gpio0)
56#if DT_NODE_HAS_STATUS(GPIO0, okay)
57 gpio = DEVICE_DT_GET(GPIO0);
58#else
59#error "gpio0 is disabled"
60#endif
61 } else {
62#define GPIO1 DT_NODELABEL(gpio1)
63#if DT_NODE_HAS_STATUS(GPIO1, okay)
64 gpio = DEVICE_DT_GET(GPIO1);
65#else
66#error "gpio1 is disabled"
67#endif
68 }
69 if (device_is_ready(gpio)) {
70 this->gpio_ = gpio;
71 } else {
72 ESP_LOGE(TAG, "gpio %u is not ready.", this->pin_);
73 return;
74 }
75 this->pin_mode(this->flags_);
76}
77
79 if (nullptr == this->gpio_) {
80 return;
81 }
82 gpio_pin_configure(this->gpio_, this->pin_ % 32, flags_to_mode(flags, this->inverted_, this->value_));
83}
84
85std::string ZephyrGPIOPin::dump_summary() const {
86 char buffer[32];
87 snprintf(buffer, sizeof(buffer), "GPIO%u, P%u.%u", this->pin_, this->pin_ / 32, this->pin_ % 32);
88 return buffer;
89}
90
92 if (nullptr == this->gpio_) {
93 return false;
94 }
95 return bool(gpio_pin_get(this->gpio_, this->pin_ % 32) != this->inverted_);
96}
97
99 // make sure that value is not ignored since it can be inverted e.g. on switch side
100 // that way init state should be correct
101 this->value_ = value;
102 if (nullptr == this->gpio_) {
103 return;
104 }
105 gpio_pin_set(this->gpio_, this->pin_ % 32, value != this->inverted_ ? 1 : 0);
106}
108 // TODO
109}
110
111} // namespace zephyr
112
113bool IRAM_ATTR ISRInternalGPIOPin::digital_read() {
114 // TODO
115 return false;
116}
117
118} // namespace esphome
119
120#endif
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio.h:73
const device * gpio_
Definition gpio.h:31
void digital_write(bool value) override
Definition gpio.cpp:98
void attach_interrupt(void(*func)(void *), void *arg, gpio::InterruptType type) const override
Definition gpio.cpp:48
void setup() override
Definition gpio.cpp:52
bool digital_read() override
Definition gpio.cpp:91
std::string dump_summary() const override
Definition gpio.cpp:85
ISRInternalGPIOPin to_isr() const override
Definition gpio.cpp:41
void detach_interrupt() const override
Definition gpio.cpp:107
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:78
uint8_t type
@ FLAG_OUTPUT
Definition gpio.h:19
@ FLAG_OPEN_DRAIN
Definition gpio.h:20
@ FLAG_PULLUP
Definition gpio.h:21
@ FLAG_INPUT
Definition gpio.h:18
@ FLAG_PULLDOWN
Definition gpio.h:22
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7