ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
gpio_arduino.cpp
Go to the documentation of this file.
1#ifdef USE_LIBRETINY
2
3#include "gpio_arduino.h"
4#include "esphome/core/hal.h"
5#include "esphome/core/log.h"
6
7// clang-tidy cannot parse the Realtek SDK headers behind wiring_data.h
8#if defined(USE_BK72XX) || (defined(USE_RTL87XX) && !defined(CLANG_TIDY))
9#include <wiring_data.h>
10#endif
11
12namespace esphome::libretiny {
13
14ESPHOME_LOG_TAG(TAG, "libretiny.gpio");
15
16static int IRAM_ATTR flags_to_mode(gpio::Flags flags) {
17 flags =
19 if (flags == gpio::FLAG_INPUT) {
20 return INPUT;
21 } else if (flags == gpio::FLAG_OUTPUT) {
22 return OUTPUT;
23 } else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLUP)) {
24 return INPUT_PULLUP;
25 } else if (flags == (gpio::FLAG_INPUT | gpio::FLAG_PULLDOWN)) {
26 return INPUT_PULLDOWN;
28 return OUTPUT_OPEN_DRAIN;
29 } else {
30 return 0;
31 }
32}
33
34// The ISR copy is the pin itself, so both share the stored mode and level. Pins are never freed.
36 return ISRInternalGPIOPin(
37 const_cast<ArduinoInternalGPIOPin *>(this)); // NOLINT(cppcoreguidelines-pro-type-const-cast)
38}
39
40#if defined(USE_LIBRETINY_VARIANT_RTL8710B) && !defined(CLANG_TIDY)
41// RTL8710B GPIO interrupts on one edge only and LibreTiny maps CHANGE to rising, so flip the edge on every interrupt.
42// Mirrors LibreTiny 1.14.0 wiring_irq.c (PinData is the SDK handler id); drop once libretiny-eu/libretiny#155 is fixed.
43// Re-reads the level after each write; a pin still bouncing after four tries is corrected at its next edge.
44static void IRAM_ATTR rtl8710b_arm_next_edge(gpio_irq_t *irq) {
45 uint32_t level = GPIO_ReadDataBit(irq->pin);
46 for (int i = 0; i < 4; i++) {
47 // gpio_irq_set() without its flash wrapper, so the handler runs from RAM and mask ROM only
48 GPIO_INTMode(irq->pin, ENABLE, GPIO_INT_Trigger_EDGE,
49 level ? GPIO_INT_POLARITY_ACTIVE_LOW : GPIO_INT_POLARITY_ACTIVE_HIGH, GPIO_INT_DEBOUNCE_ENABLE);
50 const uint32_t now = GPIO_ReadDataBit(irq->pin);
51 if (now == level)
52 return;
53 level = now;
54 }
55}
56
57static void IRAM_ATTR rtl8710b_change_handler(uint32_t id, gpio_irq_event event) {
58 auto *data = reinterpret_cast<PinData *>(id); // NOLINT(performance-no-int-to-ptr)
59 // A later attach with another mode keeps this handler
60 if (data->irqMode == CHANGE)
61 rtl8710b_arm_next_edge(data->irq);
62 if (data->irqHandler != nullptr)
63 reinterpret_cast<void (*)(void *)>(data->irqHandler)(data->irqParam);
64}
65#endif
66
67void ArduinoInternalGPIOPin::attach_interrupt_(void (*func)(void *), void *arg, gpio::InterruptType type) const {
68 PinStatus arduino_mode = (PinStatus) 255;
69 switch (type) {
71 arduino_mode = inverted_ ? FALLING : RISING;
72 break;
74 arduino_mode = inverted_ ? RISING : FALLING;
75 break;
77 arduino_mode = CHANGE;
78 break;
80 arduino_mode = inverted_ ? HIGH : LOW;
81 break;
83 arduino_mode = inverted_ ? LOW : HIGH;
84 break;
85 }
86
87 attachInterruptParam(pin_, func, arduino_mode, arg);
88#if defined(USE_LIBRETINY_VARIANT_RTL8710B) && !defined(CLANG_TIDY)
89 if (arduino_mode == CHANGE) {
90 PinInfo *info = pinInfo(this->pin_);
91 PinData *data = info != nullptr ? info->data : nullptr;
92 if (data != nullptr && data->irq != nullptr) {
93 // The interrupt is already live: keep it from arming an edge between the level read and the write below
94 const uint32_t primask = __get_PRIMASK();
95 __disable_irq();
96 GPIO_UserRegIrq(data->irq->pin, reinterpret_cast<void *>(rtl8710b_change_handler), data);
97 rtl8710b_arm_next_edge(data->irq);
98 __set_PRIMASK(primask);
99 }
100 }
101#endif
102}
103
104#ifdef USE_BK72XX
105// BDK GPIO config register (REG_GPIO_0_CONFIG, GCFG_OUTPUT_BIT): 0 enables the output, bit 1 is the output level
106static constexpr uintptr_t BK_GPIO_CFG_BASE = 0x00802800;
107static constexpr uint32_t BK_GPIO_OUTPUT_BIT = 1 << 1;
108
109// LibreTiny's switch to output always starts the pin LOW. Enable the output at its level in one store, then update
110// LibreTiny's pin state the way its own switch does (cores/beken-72xx/arduino/src/wiring_digital.c).
111static bool bk_enter_output(uint8_t pin, bool level) {
112 PinInfo *info = pinInfo(pin);
113 if (info == nullptr || info->gpio > 31)
114 return false;
115 PinData *data = pinData(info);
116 if (pinEnabled(info, PIN_GPIO) && data->gpioMode == OUTPUT)
117 return false;
118 pinRemoveMode(info, PIN_PWM);
119 auto *cfg =
120 reinterpret_cast<volatile uint32_t *>(BK_GPIO_CFG_BASE + info->gpio * 4); // NOLINT(performance-no-int-to-ptr)
121 *cfg = level ? BK_GPIO_OUTPUT_BIT : 0;
122 pinEnable(info, PIN_GPIO);
123 data->gpioMode = OUTPUT;
124 return true;
125}
126#endif
127
128#if defined(USE_RTL87XX) && !defined(CLANG_TIDY)
129// The switch to output keeps the HAL data register, so write the level to it first
130static void rtl_preset_level(uint8_t pin, bool level) {
131 PinInfo *info = pinInfo(pin);
132 if (info == nullptr)
133 return;
134 if (info->data == nullptr || info->data->gpio == nullptr)
135 pinMode(pin, INPUT); // NOLINT - LibreTiny creates the gpio_t on first use
136 if (info->data != nullptr && info->data->gpio != nullptr)
137 gpio_write(info->data->gpio, level);
138}
139#endif
140
142 const int mode = flags_to_mode(flags);
143 this->output_ = flags & gpio::FLAG_OUTPUT;
144#ifdef USE_BK72XX
145 if (mode == OUTPUT && bk_enter_output(this->pin_, this->level_))
146 return;
147#endif
148#if defined(USE_RTL87XX) && !defined(CLANG_TIDY)
149 // The preset avoids a LOW blip when going HIGH, but a LOW only takes after the switch (measured on a BW15),
150 // so the write below is still needed
151 if (mode == OUTPUT)
152 rtl_preset_level(this->pin_, this->level_);
153#endif
154 pinMode(this->pin_, mode); // NOLINT
155 if (this->output_)
156 digitalWrite(this->pin_, this->level_); // NOLINT
157}
158
160 return snprintf(buffer, len, "%u", this->pin_);
161}
162
164#if defined(USE_LIBRETINY_VARIANT_RTL8720C) && !defined(CLANG_TIDY)
165 // The Arduino read switches the pin back to GPIO and frees its interrupt, so read through the interrupt instead.
166 // Remove once LibreTiny's Arduino pin read handles this. The IRQ bit is only set once data->irq exists.
167 PinInfo *info = pinInfo(this->pin_);
168 if (info != nullptr && (info->enabled & PIN_IRQ))
169 return bool(gpio_irq_read(info->data->irq)) ^ this->inverted_;
170#endif
171 return bool(digitalRead(this->pin_)) ^ this->inverted_; // NOLINT
172}
174 this->level_ = value ^ this->inverted_;
175 if (this->output_)
176 digitalWrite(this->pin_, this->level_); // NOLINT
177}
179 detachInterrupt(pin_); // NOLINT
180}
181
182} // namespace esphome::libretiny
183
184namespace esphome {
185
186using namespace libretiny;
187
188bool IRAM_ATTR ISRInternalGPIOPin::digital_read() {
189 return reinterpret_cast<ArduinoInternalGPIOPin *>(this->arg_)->digital_read();
190}
191void IRAM_ATTR ISRInternalGPIOPin::digital_write(bool value) {
192 reinterpret_cast<ArduinoInternalGPIOPin *>(this->arg_)->digital_write(value);
193}
195 detachInterrupt(reinterpret_cast<ArduinoInternalGPIOPin *>(this->arg_)->get_pin());
196}
198 reinterpret_cast<ArduinoInternalGPIOPin *>(this->arg_)->pin_mode(flags);
199}
200
201} // namespace esphome
202
203#endif // USE_LIBRETINY
BedjetMode mode
BedJet operating mode.
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
Definition gpio_pin.h:98
void digital_write(bool value)
Definition gpio.cpp:234
void pin_mode(gpio::Flags flags)
Definition gpio.cpp:246
void pin_mode(gpio::Flags flags) override
void attach_interrupt_(void(*func)(void *), void *arg, gpio::InterruptType type) const
size_t dump_summary(char *buffer, size_t len) const override
uint16_t flags
uint16_t id
uint8_t type
uint32_t buffer
Definition gif.cpp:50
@ INTERRUPT_FALLING_EDGE
Definition gpio_pin.h:53
@ INTERRUPT_RISING_EDGE
Definition gpio_pin.h:52
@ INTERRUPT_HIGH_LEVEL
Definition gpio_pin.h:56
@ INTERRUPT_LOW_LEVEL
Definition gpio_pin.h:55
@ INTERRUPT_ANY_EDGE
Definition gpio_pin.h:54
ESPHOME_LOG_TAG(TAG, "libretiny.crash")
uint32_t value
Definition hal.cpp:41
const void size_t len
Definition hal.h:68
static void uint32_t