ESPHome 2025.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 <zephyr/drivers/gpio.h>
4#include "esphome/core/log.h"
5
6namespace esphome {
7namespace zephyr {
8
9static const char *const TAG = "zephyr";
10
11static gpio_flags_t flags_to_mode(gpio::Flags flags, bool inverted, bool value) {
12 gpio_flags_t ret = 0;
13 if (flags & gpio::FLAG_INPUT) {
14 ret |= GPIO_INPUT;
15 }
17 ret |= GPIO_OUTPUT;
18 if (value != inverted) {
19 ret |= GPIO_OUTPUT_INIT_HIGH;
20 } else {
21 ret |= GPIO_OUTPUT_INIT_LOW;
22 }
23 }
25 ret |= GPIO_PULL_UP;
26 }
28 ret |= GPIO_PULL_DOWN;
29 }
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 auto ret = gpio_pin_configure(this->gpio_, this->pin_ % 32, flags_to_mode(flags, this->inverted_, this->value_));
83 if (ret != 0) {
84 ESP_LOGE(TAG, "gpio %u cannot be configured %d.", this->pin_, ret);
85 }
86}
87
88std::string ZephyrGPIOPin::dump_summary() const {
89 char buffer[32];
90 snprintf(buffer, sizeof(buffer), "GPIO%u, P%u.%u", this->pin_, this->pin_ / 32, this->pin_ % 32);
91 return buffer;
92}
93
95 if (nullptr == this->gpio_) {
96 return false;
97 }
98 return bool(gpio_pin_get(this->gpio_, this->pin_ % 32) != this->inverted_);
99}
100
102 // make sure that value is not ignored since it can be inverted e.g. on switch side
103 // that way init state should be correct
104 this->value_ = value;
105 if (nullptr == this->gpio_) {
106 return;
107 }
108 gpio_pin_set(this->gpio_, this->pin_ % 32, value != this->inverted_ ? 1 : 0);
109}
111 // TODO
112}
113
114} // namespace zephyr
115
116bool IRAM_ATTR ISRInternalGPIOPin::digital_read() {
117 // TODO
118 return false;
119}
120
121} // namespace esphome
122
123#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:101
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:94
std::string dump_summary() const override
Definition gpio.cpp:88
ISRInternalGPIOPin to_isr() const override
Definition gpio.cpp:41
void detach_interrupt() const override
Definition gpio.cpp:110
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:78
uint16_t type
uint16_t flags
@ 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