23#include <idfxx/intr_alloc>
26#include <hal/gpio_types.h>
27#include <soc/gpio_num.h>
28#include <soc/soc_caps.h>
109#if SOC_GPIO_SUPPORT_PIN_HYS_FILTER
112#if SOC_GPIO_SUPPORT_PIN_HYS_CTRL_BY_EFUSE
120 class unique_isr_handle;
183 if (
this != &other) {
226#if SOC_GPIO_SUPPORT_PIN_HYS_FILTER
243#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
278 [[
nodiscard]]
constexpr int num()
const {
return static_cast<int>(_num); }
293#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
405#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
433#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
488#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
591#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
621#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
688#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
714 static constexpr bool _is_valid_gpio_num(
int num) {
717 static constexpr bool _is_valid_output_gpio_num(
int num) {
720 static constexpr bool _is_valid_digital_io_gpio_num(
int num) {
746template<
typename...
Gpios>
750#ifdef CONFIG_COMPILER_CXX_EXCEPTIONS
772template<
typename...
Gpios>
783struct gpio_constant {
784 static_assert(N == GPIO_NUM_NC || gpio::_is_valid_gpio_num(N),
"Invalid GPIO number");
785 static constexpr gpio value{
static_cast<gpio_num_t
>(N)};
799#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 0))
802#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 1))
805#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 2))
808#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 3))
811#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 4))
814#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 5))
817#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 6))
820#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 7))
823#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 8))
826#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 9))
829#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 10))
832#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 11))
835#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 12))
838#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 13))
841#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 14))
844#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 15))
847#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 16))
850#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 17))
853#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 18))
856#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 19))
859#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 20))
862#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 21))
865#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 22))
868#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 23))
871#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 24))
874#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 25))
877#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 26))
880#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 27))
883#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 28))
886#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 29))
889#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 30))
892#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 31))
895#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 32))
898#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 33))
901#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 34))
904#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 35))
907#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 36))
910#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 37))
913#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 38))
916#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 39))
919#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 40))
922#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 41))
925#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 42))
928#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 43))
931#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 44))
934#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 45))
937#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 46))
940#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 47))
943#if (SOC_GPIO_VALID_GPIO_MASK & (1ULL << 48))
957 if (!
g.is_connected()) {
960 return "GPIO_" + std::to_string(
g.num());
989#include "sdkconfig.h"
990#ifdef CONFIG_IDFXX_STD_FORMAT
996struct formatter<
idfxx::gpio> {
997 constexpr auto parse(format_parse_context& ctx) {
return ctx.begin(); }
999 template<
typename FormatContext>
1000 auto format(
idfxx::gpio g, FormatContext& ctx)
const {
1002 return std::copy(s.begin(), s.end(), ctx.out());
1006struct formatter<
idfxx::gpio::level> {
1007 constexpr auto parse(format_parse_context& ctx) {
return ctx.begin(); }
1009 template<
typename FormatContext>
1012 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.
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.
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 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 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.
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 flags< E > operator~(E a) noexcept
Computes the bitwise complement of an enum value.
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.