9#include "driver/gpio.h"
10#include "driver/rtc_io.h"
11#include "hal/gpio_hal.h"
12#include "soc/soc_caps.h"
13#include "soc/gpio_periph.h"
16#if (SOC_RTCIO_PIN_COUNT > 0)
17#include "hal/rtc_io_hal.h"
21#if defined(USE_ESP32_VARIANT_ESP32) || defined(USE_ESP32_VARIANT_ESP32H2) || defined(USE_ESP32_VARIANT_ESP32H21)
22#define RTCIO_NEED_MAPPING 1
25#if defined(USE_ESP32_VARIANT_ESP32) || (SOC_RTCIO_PIN_COUNT > 0 && defined(USE_GPIO_HOLD))
26#define ISR_PIN_USE_RTC 1
34static const gpio_hal_context_t GPIO_HAL = {.dev = GPIO_HAL_GET_HW(GPIO_PORT_0)};
41 return GPIO_MODE_INPUT;
43 return GPIO_MODE_OUTPUT;
45 return GPIO_MODE_OUTPUT_OD;
47 return GPIO_MODE_INPUT_OUTPUT_OD;
49 return GPIO_MODE_INPUT_OUTPUT;
51 return GPIO_MODE_DISABLE;
63#ifdef RTCIO_NEED_MAPPING
71static inline void refresh_hold(
const ESP32InternalGPIOPin &pin) {
76static inline void IRAM_ATTR isr_refresh_hold(
const ISRPinArg *arg) {
79#if defined(ISR_PIN_USE_RTC) && defined(SOC_RTCIO_HOLD_SUPPORTED)
81#ifdef RTCIO_NEED_MAPPING
82 rtcio_hal_hold_disable(arg->rtc_pin);
83 rtcio_hal_hold_enable(arg->rtc_pin);
85 rtcio_hal_hold_disable(arg->pin);
86 rtcio_hal_hold_enable(arg->pin);
91 gpio_hal_hold_dis(&GPIO_HAL, arg->pin);
92 gpio_hal_hold_en(&GPIO_HAL, arg->pin);
97 auto *arg =
new ISRPinArg{};
104#ifdef ISR_PIN_USE_RTC
105 arg->use_rtc = rtc_gpio_is_valid_gpio(this->
get_pin_num());
106#ifdef RTCIO_NEED_MAPPING
108 arg->rtc_pin = rtc_io_number_get(this->
get_pin_num());
115 gpio_int_type_t idf_type = GPIO_INTR_ANYEDGE;
124 idf_type = GPIO_INTR_ANYEDGE;
136 auto res = gpio_install_isr_service(ESP_INTR_FLAG_LEVEL3);
138 ESP_LOGE(TAG,
"attach_interrupt(): call to gpio_install_isr_service() failed, error code: %d", res);
143 gpio_isr_handler_add(this->
get_pin_num(), func, arg);
167 gpio_config_t conf{};
168 conf.pin_bit_mask = 1ULL <<
static_cast<uint32_t>(this->
pin_);
169 conf.mode = flags_to_mode(this->
flags_);
172 conf.intr_type = GPIO_INTR_DISABLE;
174#ifdef CONFIG_FREERTOS_USE_TICKLESS_IDLE
183 if (GPIO_IS_VALID_OUTPUT_GPIO(this->
get_pin_num())) {
198 gpio_pull_mode_t pull_mode = GPIO_FLOATING;
200 pull_mode = GPIO_PULLUP_PULLDOWN;
202 pull_mode = GPIO_PULLUP_ONLY;
204 pull_mode = GPIO_PULLDOWN_ONLY;
206 gpio_set_pull_mode(this->
get_pin_num(), pull_mode);
225using namespace esp32;
230 auto *arg =
reinterpret_cast<ISRPinArg *
>(this->
arg_);
231 return bool(gpio_hal_get_level(&GPIO_HAL, arg->pin)) != arg->inverted;
235 auto *arg =
reinterpret_cast<ISRPinArg *
>(this->
arg_);
236 gpio_hal_set_level(&GPIO_HAL, arg->pin,
value != arg->inverted);
238 isr_refresh_hold(arg);
247 auto *arg =
reinterpret_cast<ISRPinArg *
>(
arg_);
251 gpio_hal_output_enable(&GPIO_HAL, arg->pin);
253 gpio_hal_od_enable(&GPIO_HAL, arg->pin);
255 gpio_hal_output_disable(&GPIO_HAL, arg->pin);
260 gpio_hal_input_enable(&GPIO_HAL, arg->pin);
261#ifdef USE_ESP32_VARIANT_ESP32
265 rtcio_hal_pullup_enable(arg->rtc_pin);
267 rtcio_hal_pullup_disable(arg->rtc_pin);
270 rtcio_hal_pulldown_enable(arg->rtc_pin);
272 rtcio_hal_pulldown_disable(arg->rtc_pin);
278 gpio_hal_pullup_en(&GPIO_HAL, arg->pin);
280 gpio_hal_pullup_dis(&GPIO_HAL, arg->pin);
283 gpio_hal_pulldown_en(&GPIO_HAL, arg->pin);
285 gpio_hal_pulldown_dis(&GPIO_HAL, arg->pin);
289 gpio_hal_input_disable(&GPIO_HAL, arg->pin);
293 isr_refresh_hold(arg);
Copy of GPIOPin that is safe to use from ISRs (with no virtual functions)
void digital_write(bool value)
void pin_mode(gpio::Flags flags)
gpio_num_t get_pin_num() const
struct esphome::esp32::ESP32InternalGPIOPin::PinFlags pin_flags_
gpio_drive_cap_t get_drive_strength() const
void set_hold(bool hold) const
Apply or release the pad hold. Does nothing unless the pin is configured with hold_state.
void detach_interrupt() const
bool digital_read() override
void pin_mode(gpio::Flags flags) override
void attach_interrupt_(void(*func)(void *), void *arg, gpio::InterruptType type) const
void digital_write(bool value) override
static bool isr_service_installed
ISRInternalGPIOPin to_isr() const
size_t dump_summary(char *buffer, size_t len) const override
ESPHOME_LOG_TAG(TAG, CRASH_LOG_TAG)