ESPHome  2022.9.1
gpio_arduino.cpp
Go to the documentation of this file.
1 #ifdef USE_ESP32_FRAMEWORK_ARDUINO
2 
3 #include "gpio_arduino.h"
4 #include "esphome/core/log.h"
5 #include <esp32-hal-gpio.h>
6 
7 namespace esphome {
8 namespace esp32 {
9 
10 static const char *const TAG = "esp32";
11 
12 static int IRAM_ATTR flags_to_mode(gpio::Flags flags) {
13  if (flags == gpio::FLAG_INPUT) {
14  return INPUT;
15  } else if (flags == gpio::FLAG_OUTPUT) {
16  return OUTPUT;
17  } else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLUP)) {
18  return INPUT_PULLUP;
19  } else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLDOWN)) {
20  return INPUT_PULLDOWN;
21  } else if (flags == (gpio::FLAG_OUTPUT | gpio::FLAG_OPEN_DRAIN)) {
22  return OUTPUT_OPEN_DRAIN;
23  } else {
24  return 0;
25  }
26 }
27 
28 struct ISRPinArg {
29  uint8_t pin;
30  bool inverted;
31 };
32 
34  auto *arg = new ISRPinArg{}; // NOLINT(cppcoreguidelines-owning-memory)
35  arg->pin = pin_;
36  arg->inverted = inverted_;
37  return ISRInternalGPIOPin((void *) arg);
38 }
39 
40 void ArduinoInternalGPIOPin::attach_interrupt(void (*func)(void *), void *arg, gpio::InterruptType type) const {
41  uint8_t arduino_mode = DISABLED;
42  switch (type) {
44  arduino_mode = inverted_ ? FALLING : RISING;
45  break;
47  arduino_mode = inverted_ ? RISING : FALLING;
48  break;
50  arduino_mode = CHANGE;
51  break;
53  arduino_mode = inverted_ ? ONHIGH : ONLOW;
54  break;
56  arduino_mode = inverted_ ? ONLOW : ONHIGH;
57  break;
58  }
59 
60  attachInterruptArg(pin_, func, arg, arduino_mode);
61 }
62 
64  pinMode(pin_, flags_to_mode(flags)); // NOLINT
65 }
66 
68  char buffer[32];
69  snprintf(buffer, sizeof(buffer), "GPIO%u", pin_);
70  return buffer;
71 }
72 
74  return bool(digitalRead(pin_)) != inverted_; // NOLINT
75 }
77  digitalWrite(pin_, value != inverted_ ? 1 : 0); // NOLINT
78 }
80  detachInterrupt(pin_); // NOLINT
81 }
82 
83 } // namespace esp32
84 
85 using namespace esp32;
86 
88  auto *arg = reinterpret_cast<ISRPinArg *>(arg_);
89  return bool(digitalRead(arg->pin)) != arg->inverted; // NOLINT
90 }
91 void IRAM_ATTR ISRInternalGPIOPin::digital_write(bool value) {
92  auto *arg = reinterpret_cast<ISRPinArg *>(arg_);
93  digitalWrite(arg->pin, value != arg->inverted ? 1 : 0); // NOLINT
94 }
96  auto *arg = reinterpret_cast<ISRPinArg *>(arg_);
97 #ifdef CONFIG_IDF_TARGET_ESP32C3
98  GPIO.status_w1tc.val = 1UL << arg->pin;
99 #else
100  if (arg->pin < 32) {
101  GPIO.status_w1tc = 1UL << arg->pin;
102  } else {
103  GPIO.status1_w1tc.intr_st = 1UL << (arg->pin - 32);
104  }
105 #endif
106 }
108  auto *arg = reinterpret_cast<ISRPinArg *>(arg_);
109  pinMode(arg->pin, flags_to_mode(flags)); // NOLINT
110 }
111 
112 } // namespace esphome
113 
114 #endif // USE_ESP32_FRAMEWORK_ARDUINO
void pin_mode(gpio::Flags flags) override
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition: gpio.h:66
ISRInternalGPIOPin to_isr() const override
std::string dump_summary() const override
void pin_mode(gpio::Flags flags)
uint8_t type
const uint32_t flags
Definition: stm32flash.h:85
void attach_interrupt(void(*func)(void *), void *arg, gpio::InterruptType type) const override
InterruptType
Definition: gpio.h:40
Definition: a4988.cpp:4
void detach_interrupt() const override
void digital_write(bool value) override
void digital_write(bool value)