23#include <idfxx/intr_alloc>
25#include <esp_idf_version.h>
27#include <hal/gpio_types.h>
28#include <soc/gpio_num.h>
29#include <soc/soc_caps.h>
110#if SOC_GPIO_SUPPORT_PIN_HYS_FILTER
113#if SOC_GPIO_SUPPORT_PIN_HYS_CTRL_BY_EFUSE
121 class unique_isr_handle;
184 if (
this != &other) {
227#if SOC_GPIO_SUPPORT_PIN_HYS_FILTER
244#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
279 [[
nodiscard]]
constexpr int num()
const {
return static_cast<int>(_num); }
294#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
348#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(6, 0, 0)
452#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
480#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
535#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
638#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
668#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
714#if !SOC_GPIO_SUPPORT_HOLD_SINGLE_IO_IN_DSLP
746#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
772 static constexpr bool _is_valid_gpio_num(
int num) {
775 static constexpr bool _is_valid_output_gpio_num(
int num) {
778 static constexpr bool _is_valid_digital_io_gpio_num(
int num) {
804template<
typename...
Gpios>
808#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
830template<
typename...
Gpios>
836#if SOC_GPIO_SUPPORT_FORCE_HOLD
863struct gpio_constant {
864 static_assert(N == GPIO_NUM_NC || gpio::_is_valid_gpio_num(N),
"Invalid GPIO number");
865 static constexpr gpio value{
static_cast<gpio_num_t
>(N)};
879#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 0))
882#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 1))
885#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 2))
888#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 3))
891#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 4))
894#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 5))
897#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 6))
900#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 7))
903#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 8))
906#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 9))
909#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 10))
912#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 11))
915#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 12))
918#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 13))
921#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 14))
924#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 15))
927#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 16))
930#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 17))
933#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 18))
936#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 19))
939#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 20))
942#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 21))
945#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 22))
948#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 23))
951#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 24))
954#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 25))
957#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 26))
960#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 27))
963#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 28))
966#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 29))
969#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 30))
972#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 31))
975#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 32))
978#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 33))
981#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 34))
984#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 35))
987#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 36))
990#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 37))
993#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 38))
996#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 39))
999#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 40))
1002#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 41))
1005#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 42))
1008#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 43))
1011#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 44))
1014#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 45))
1017#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 46))
1020#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 47))
1023#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 48))
1037 if (!
g.is_connected()) {
1040 return "GPIO_" + std::to_string(
g.num());
1069#include "sdkconfig.h"
1070#ifdef CONFIG_IDFXX_STD_FORMAT
1076struct formatter<
idfxx::gpio> {
1077 constexpr auto parse(format_parse_context& ctx) {
return ctx.begin(); }
1079 template<
typename FormatContext>
1080 auto format(
idfxx::gpio g, FormatContext& ctx)
const {
1082 return std::copy(s.begin(), s.end(), ctx.out());
1086struct formatter<
idfxx::gpio::level> {
1087 constexpr auto parse(format_parse_context& ctx) {
return ctx.begin(); }
1089 template<
typename FormatContext>
1092 return std::copy(s.begin(), s.end(), ctx.out());
Handle to a registered ISR handler.
RAII handle for ISR registration that removes the handler on destruction.
unique_isr_handle & operator=(unique_isr_handle &&other) noexcept
Move assignment operator.
unique_isr_handle() noexcept
Constructs an empty unique_isr_handle.
unique_isr_handle(unique_isr_handle &&other) noexcept
Move constructor.
~unique_isr_handle()
Destructor.
unique_isr_handle & operator=(const unique_isr_handle &)=delete
unique_isr_handle(const unique_isr_handle &)=delete
unique_isr_handle(isr_handle handle) noexcept
Constructs from an isr_handle, taking ownership.
isr_handle release() noexcept
Releases ownership of the handle without removing the ISR.
static result< gpio > make(int num)
Creates a validated GPIO pin.
static void uninstall_isr_service()
Uninstalls the GPIO ISR service, freeing related resources.
void set_drive_capability(enum drive_cap strength)
Sets the gpio drive capability.
result< void > try_pulldown_disable()
Disables the internal pull-down resistor.
void intr_enable()
Enables interrupts for this gpio.
result< void > try_sleep_set_direction(enum mode mode)
Sets GPIO direction at sleep.
result< void > try_wakeup_enable(enum intr_type intr_type)
Enables GPIO wake-up from light sleep.
result< void > try_set_pull_mode(enum pull_mode mode)
Sets the pull resistor mode.
result< void > try_wakeup_disable()
Disables GPIO wake-up.
static result< void > try_install_isr_service(idfxx::intr_levels levels=intr_level_lowmed, idfxx::flags< intr_flag > flags={})
Installs the GPIO ISR service for per-gpio interrupt handlers.
static result< void > try_install_isr_service(idfxx::flags< intr_flag > flags)
Installs the GPIO ISR service with default priority levels.
void pulldown_enable()
Enables the internal pull-down resistor.
constexpr bool is_digital_io_pin_capable() const
Returns true if this gpio is a valid digital I/O pin.
void input_enable()
Enables input on this gpio.
static void deep_sleep_hold_disable()
Disables hold for all digital GPIOs during deep sleep.
void sleep_set_direction(enum mode mode)
Sets GPIO direction at sleep.
void output_disable()
Disables output on this gpio.
constexpr int num() const
Returns the underlying GPIO pin number.
void set_intr_type(enum intr_type intr_type)
Sets the interrupt trigger type.
result< drive_cap > try_get_drive_capability() const
Gets the current drive capability.
result< void > try_set_direction(enum mode mode)
Sets the GPIO direction mode.
void sleep_sel_disable()
Disables SLP_SEL to change GPIO status automatically in light sleep.
result< void > try_pullup_enable()
Enables the internal pull-up resistor.
gpio & operator=(const gpio &)=default
enum level get_level() const
Reads the current input level.
void wakeup_disable()
Disables GPIO wake-up.
friend struct gpio_constant
result< void > try_isr_handler_remove(isr_handle handle)
Removes a specific ISR handler.
result< void > try_set_drive_capability(enum drive_cap strength)
Sets the gpio drive capability.
void set_pull_mode(enum pull_mode mode)
Sets the pull resistor mode.
void pulldown_disable()
Disables the internal pull-down resistor.
void od_enable()
Enables open-drain mode on this gpio.
static constexpr gpio max()
Returns a GPIO for the highest valid pin number.
gpio & operator=(gpio &&)=default
void sleep_set_pull_mode(enum pull_mode pull)
Sets pull resistor mode at sleep.
static constexpr gpio nc()
Returns a GPIO representing "not connected".
result< void > try_hold_disable()
Disables gpio hold function.
void output_enable()
Enables output on this gpio.
void set_level(enum level level)
Sets the output level.
void toggle_level()
Toggles the output level.
void reset()
Resets the gpio to default state.
@ disable
Disable input and output.
@ input_output_od
Input and output with open-drain mode.
@ input_output
Input and output.
@ output_od
Output only with open-drain mode.
isr_handle isr_handler_add(std::move_only_function< void() const > handler)
Adds an ISR handler for this gpio.
constexpr gpio_num_t idf_num() const
Returns the underlying ESP-IDF GPIO number.
gpio(const gpio &)=default
constexpr gpio()
Constructs a GPIO representing "not connected".
pull_mode
Pull resistor configuration.
@ pullup_pulldown
Pin pull up + pull down.
result< void > try_hold_enable()
Enables gpio hold function.
static void install_isr_service(idfxx::intr_levels levels=intr_level_lowmed, idfxx::flags< intr_flag > flags={})
Installs the GPIO ISR service for per-gpio interrupt handlers.
static void install_isr_service(idfxx::flags< intr_flag > flags)
Installs the GPIO ISR service with default priority levels.
void od_disable()
Disables open-drain mode on this gpio.
void intr_disable()
Disables interrupts for this gpio.
level
GPIO output/input level.
void hold_enable()
Enables gpio hold function.
gpio(int num)
Constructs a validated GPIO pin.
result< isr_handle > try_isr_handler_add(std::move_only_function< void() const > handler)
Adds an ISR handler for this gpio.
void isr_handler_remove_all()
Removes all ISR handlers for this gpio.
constexpr bool operator==(const gpio &) const noexcept=default
Compares two GPIO pins for equality.
constexpr bool is_output_capable() const
Returns true if this gpio supports output mode.
isr_handle isr_handler_add(void(*fn)(void *), void *arg)
Adds an ISR handler for this gpio.
state get_state() const
Returns the current configuration state of this GPIO.
drive_cap
Pin drive capability (output strength).
@ cap_0
Weak drive capability.
@ cap_default
Medium drive capability.
@ cap_1
Stronger drive capability.
@ cap_2
Medium drive capability.
@ cap_3
Strongest drive capability.
constexpr bool is_connected() const
Returns true if this is a valid GPIO pin.
void hold_disable()
Disables gpio hold function.
result< void > try_pullup_disable()
Disables the internal pull-up resistor.
result< isr_handle > try_isr_handler_add(void(*fn)(void *), void *arg)
Adds an ISR handler for this gpio.
void set_direction(enum mode mode)
Sets the GPIO direction mode.
void wakeup_enable(enum intr_type intr_type)
Enables GPIO wake-up from light sleep.
result< void > try_pulldown_enable()
Enables the internal pull-down resistor.
void sleep_sel_enable()
Enables SLP_SEL to change GPIO status automatically in light sleep.
enum drive_cap get_drive_capability() const
Gets the current drive capability.
hys_ctrl_mode
Hysteresis control mode.
@ soft_enable
Pin input hysteresis enable by software.
@ soft_disable
Pin input hysteresis disable by software.
void pullup_enable()
Enables the internal pull-up resistor.
void pullup_disable()
Disables the internal pull-up resistor.
intr_type
Interrupt trigger type.
@ disable
Disable GPIO interrupt.
@ low_level
Input low level trigger.
@ high_level
Input high level trigger.
@ anyedge
Both rising and falling edge.
static void deep_sleep_hold_enable()
Enables hold for all digital GPIOs during deep sleep.
void isr_handler_remove(isr_handle handle)
Removes a specific ISR handler.
@ efuse
Reset due to efuse error.
std::string to_string(core_id c)
Returns a string representation of a CPU core identifier.
void configure_gpios(const gpio::config &cfg, std::vector< gpio > gpios)
Configures multiple GPIOs with the same settings.
result< void > try_configure_gpios(const gpio::config &cfg, std::vector< gpio > gpios)
Configures multiple GPIOs with the same settings.
constexpr gpio::level operator~(gpio::level l) noexcept
Inverts a GPIO level.
T unwrap(result< T > result)
Throws a std::system_error if the result is an error.
std::expected< T, std::error_code > result
result type wrapping a value or error code.
constexpr intr_levels intr_level_lowmed
Low and medium priority levels (1-3). These can be handled in C / C++.
Configuration parameters for idfxx::gpio_config.
Current configuration state of a GPIO pin.
bool input_enabled
Input is enabled.
bool output_enabled
Output is enabled.
bool pulldown_enabled
Internal pull-down is enabled.
enum drive_cap drive_strength
Drive strength.
bool open_drain
Open-drain mode is enabled.
bool pullup_enabled
Internal pull-up is enabled.