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"
20#ifndef SOC_GPIO_SUPPORT_RTC_INDEPENDENT
21#define SOC_GPIO_SUPPORT_RTC_INDEPENDENT 0
29static const gpio_hal_context_t GPIO_HAL = {.dev = GPIO_HAL_GET_HW(GPIO_PORT_0)};
36 return GPIO_MODE_INPUT;
38 return GPIO_MODE_OUTPUT;
40 return GPIO_MODE_OUTPUT_OD;
42 return GPIO_MODE_INPUT_OUTPUT_OD;
44 return GPIO_MODE_INPUT_OUTPUT;
46 return GPIO_MODE_DISABLE;
56#if defined(USE_ESP32_VARIANT_ESP32)
64static inline void refresh_hold(
const ESP32InternalGPIOPin &pin) {
69static inline void IRAM_ATTR isr_refresh_hold(
const ISRPinArg *arg) {
72#if defined(USE_ESP32_VARIANT_ESP32)
74 rtcio_hal_hold_disable(arg->rtc_pin);
75 rtcio_hal_hold_enable(arg->rtc_pin);
78#elif defined(USE_ESP32_VARIANT_ESP32S2) || defined(USE_ESP32_VARIANT_ESP32S3)
80 if (arg->pin < SOC_RTCIO_PIN_COUNT) {
81 rtcio_hal_hold_disable(arg->pin);
82 rtcio_hal_hold_enable(arg->pin);
86 gpio_hal_hold_dis(&GPIO_HAL, arg->pin);
87 gpio_hal_hold_en(&GPIO_HAL, arg->pin);
92 auto *arg =
new ISRPinArg{};
99#if defined(USE_ESP32_VARIANT_ESP32)
100 arg->use_rtc = rtc_gpio_is_valid_gpio(this->
get_pin_num());
102 arg->rtc_pin = rtc_io_number_get(this->
get_pin_num());
108 gpio_int_type_t idf_type = GPIO_INTR_ANYEDGE;
117 idf_type = GPIO_INTR_ANYEDGE;
129 auto res = gpio_install_isr_service(ESP_INTR_FLAG_LEVEL3);
131 ESP_LOGE(TAG,
"attach_interrupt(): call to gpio_install_isr_service() failed, error code: %d", res);
136 gpio_isr_handler_add(this->
get_pin_num(), func, arg);
160 gpio_config_t conf{};
161 conf.pin_bit_mask = 1ULL <<
static_cast<uint32_t>(this->
pin_);
162 conf.mode = flags_to_mode(this->
flags_);
165 conf.intr_type = GPIO_INTR_DISABLE;
167#ifdef CONFIG_FREERTOS_USE_TICKLESS_IDLE
176 if (GPIO_IS_VALID_OUTPUT_GPIO(this->
get_pin_num())) {
191 gpio_pull_mode_t pull_mode = GPIO_FLOATING;
193 pull_mode = GPIO_PULLUP_PULLDOWN;
195 pull_mode = GPIO_PULLUP_ONLY;
197 pull_mode = GPIO_PULLDOWN_ONLY;
199 gpio_set_pull_mode(this->
get_pin_num(), pull_mode);
218using namespace esp32;
223 auto *arg =
reinterpret_cast<ISRPinArg *
>(this->
arg_);
224 return bool(gpio_hal_get_level(&GPIO_HAL, arg->pin)) != arg->inverted;
228 auto *arg =
reinterpret_cast<ISRPinArg *
>(this->
arg_);
229 gpio_hal_set_level(&GPIO_HAL, arg->pin, value != arg->inverted);
231 isr_refresh_hold(arg);
240 auto *arg =
reinterpret_cast<ISRPinArg *
>(
arg_);
244 gpio_hal_output_enable(&GPIO_HAL, arg->pin);
246 gpio_hal_od_enable(&GPIO_HAL, arg->pin);
248 gpio_hal_output_disable(&GPIO_HAL, arg->pin);
253 gpio_hal_input_enable(&GPIO_HAL, arg->pin);
254#if defined(USE_ESP32_VARIANT_ESP32)
257 rtcio_hal_pullup_enable(arg->rtc_pin);
259 rtcio_hal_pullup_disable(arg->rtc_pin);
262 rtcio_hal_pulldown_enable(arg->rtc_pin);
264 rtcio_hal_pulldown_disable(arg->rtc_pin);
270 gpio_hal_pullup_en(&GPIO_HAL, arg->pin);
272 gpio_hal_pullup_dis(&GPIO_HAL, arg->pin);
275 gpio_hal_pulldown_en(&GPIO_HAL, arg->pin);
277 gpio_hal_pulldown_dis(&GPIO_HAL, arg->pin);
281 gpio_hal_input_disable(&GPIO_HAL, arg->pin);
285 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)