ESPHome 2026.1.4
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 if (!device_is_ready(this->gpio_)) {
54 ESP_LOGE(TAG, "gpio %u is not ready.", this->pin_);
55 return;
56 }
57 this->pin_mode(this->flags_);
58}
59
61 if (nullptr == this->gpio_) {
62 return;
63 }
64 auto ret = gpio_pin_configure(this->gpio_, this->pin_ % this->gpio_size_,
65 flags_to_mode(flags, this->inverted_, this->value_));
66 if (ret != 0) {
67 ESP_LOGE(TAG, "gpio %u cannot be configured %d.", this->pin_, ret);
68 }
69}
70
71size_t ZephyrGPIOPin::dump_summary(char *buffer, size_t len) const {
72 return snprintf(buffer, len, "GPIO%u, %s%u", this->pin_, this->pin_name_prefix_, this->pin_ % this->gpio_size_);
73}
74
76 if (nullptr == this->gpio_) {
77 return false;
78 }
79 return bool(gpio_pin_get(this->gpio_, this->pin_ % this->gpio_size_) != this->inverted_);
80}
81
83 // make sure that value is not ignored since it can be inverted e.g. on switch side
84 // that way init state should be correct
85 this->value_ = value;
86 if (nullptr == this->gpio_) {
87 return;
88 }
89 gpio_pin_set(this->gpio_, this->pin_ % this->gpio_size_, value != this->inverted_ ? 1 : 0);
90}
92 // TODO
93}
94
95} // namespace zephyr
96
97bool IRAM_ATTR ISRInternalGPIOPin::digital_read() {
98 // TODO
99 return false;
100}
101
102} // namespace esphome
103
104#endif
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio.h:92
const device * gpio_
Definition gpio.h:33
void digital_write(bool value) override
Definition gpio.cpp:82
void attach_interrupt(void(*func)(void *), void *arg, gpio::InterruptType type) const override
Definition gpio.cpp:48
const char * pin_name_prefix_
Definition gpio.h:34
void setup() override
Definition gpio.cpp:52
bool digital_read() override
Definition gpio.cpp:75
size_t dump_summary(char *buffer, size_t len) const override
Definition gpio.cpp:71
ISRInternalGPIOPin to_isr() const override
Definition gpio.cpp:41
void detach_interrupt() const override
Definition gpio.cpp:91
void pin_mode(gpio::Flags flags) override
Definition gpio.cpp:60
uint16_t type
uint16_t flags
@ FLAG_OUTPUT
Definition gpio.h:28
@ FLAG_OPEN_DRAIN
Definition gpio.h:29
@ FLAG_PULLUP
Definition gpio.h:30
@ FLAG_INPUT
Definition gpio.h:27
@ FLAG_PULLDOWN
Definition gpio.h:31
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
std::string size_t len
Definition helpers.h:595