3#include <zephyr/drivers/gpio.h>
4#include <zephyr/sys/util.h>
10static const char *
const TAG =
"zephyr";
12static gpio_flags_t flags_to_mode(
gpio::Flags flags,
bool inverted,
bool value) {
19 if (value != inverted) {
20 ret |= GPIO_OUTPUT_INIT_HIGH;
22 ret |= GPIO_OUTPUT_INIT_LOW;
29 ret |= GPIO_PULL_DOWN;
32 ret |= GPIO_OPEN_DRAIN;
44 return inverted ? GPIO_INT_EDGE_FALLING : GPIO_INT_EDGE_RISING;
46 return inverted ? GPIO_INT_EDGE_RISING : GPIO_INT_EDGE_FALLING;
48 return GPIO_INT_EDGE_BOTH;
50 return inverted ? GPIO_INT_LEVEL_HIGH : GPIO_INT_LEVEL_LOW;
52 return inverted ? GPIO_INT_LEVEL_LOW : GPIO_INT_LEVEL_HIGH;
54 return inverted ? GPIO_INT_EDGE_FALLING : GPIO_INT_EDGE_RISING;
59static void gpio_interrupt_handler(
const device * , gpio_callback *cb,
uint32_t ) {
60 auto *interrupt = CONTAINER_OF(cb, ZephyrGPIOInterrupt, callback);
61 if (interrupt->func !=
nullptr) {
62 interrupt->func(interrupt->arg);
74 auto *arg =
new ISRPinArg{};
75 arg->gpio = this->
gpio_;
76 arg->pin = this->
pin_;
83 if (!device_is_ready(this->
gpio_)) {
84 ESP_LOGE(TAG,
"Cannot attach interrupt: GPIO device not ready");
99 ESP_LOGE(TAG,
"gpio_add_callback failed for pin %u: %d", this->
pin_, ret);
103 ret = gpio_pin_interrupt_configure(this->
gpio_, port_pin, interrupt_type_to_flags(
type, this->
inverted_));
105 ESP_LOGE(TAG,
"gpio_pin_interrupt_configure failed for pin %u: %d", this->
pin_, ret);
110 ESP_LOGD(TAG,
"Interrupt attached to pin %u (type=%d)", this->
pin_, (
int) type);
114 if (!device_is_ready(this->
gpio_)) {
115 ESP_LOGE(TAG,
"gpio %u is not ready.", this->
pin_);
122 if (
nullptr == this->
gpio_) {
128 ESP_LOGE(TAG,
"gpio %u cannot be configured %d.", this->
pin_, ret);
137 if (
nullptr == this->
gpio_) {
147 if (
nullptr == this->
gpio_) {
154 if (this->
gpio_ ==
nullptr) {
159 gpio_pin_interrupt_configure(this->
gpio_, port_pin, GPIO_INT_DISABLE);
169 auto *arg = (zephyr::ISRPinArg *) this->
arg_;
170 if (arg ==
nullptr || arg->gpio ==
nullptr) {
173 return bool(gpio_pin_get(arg->gpio, arg->pin % arg->gpio_size) != arg->inverted);
177 auto *arg = (zephyr::ISRPinArg *) this->
arg_;
178 if (arg ==
nullptr || arg->gpio ==
nullptr) {
181 gpio_pin_set(arg->gpio, arg->pin % arg->gpio_size, value != arg->inverted ? 1 : 0);
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
void digital_write(bool value)
void digital_write(bool value) override
void attach_interrupt(void(*func)(void *), void *arg, gpio::InterruptType type) const override
const char * pin_name_prefix_
bool digital_read() override
size_t dump_summary(char *buffer, size_t len) const override
ISRInternalGPIOPin to_isr() const override
ZephyrGPIOInterrupt interrupt_
void detach_interrupt() const override
void pin_mode(gpio::Flags flags) override
struct gpio_callback callback