28,
description_<"An ESP-IDF GPIO API wrapper as a message-based `sygaldry` component">
30 #define gpio_function(c_name, esp_idf_func, ... )\
31 static auto c_name() noexcept\
33 auto ret = esp_idf_func(__VA_ARGS__);\
34 ESP_ERROR_CHECK_WITHOUT_ABORT(ret);\
38 gpio_function(remove_interrupt_handler, gpio_isr_handler_remove, pin_number);
39 static auto interrupt_handler(
void (*handler)(
void*),
void* args)
noexcept
41 auto ret = gpio_isr_handler_add(pin_number, handler, args);
42 ESP_ERROR_CHECK_WITHOUT_ABORT(ret);
45 static void uninstall_isr_service()
noexcept
47 gpio_uninstall_isr_service();
51 static auto install_isr_service(
int intr_alloc_flags)
noexcept
53 auto ret = gpio_install_isr_service(intr_alloc_flags);
54 ESP_ERROR_CHECK_WITHOUT_ABORT(ret);
59 gpio_function(enable_interrupt, gpio_intr_enable, pin_number);
60 gpio_function(disable_interrupt, gpio_intr_disable, pin_number);
62 gpio_function(reset, gpio_reset_pin, pin_number);
63 gpio_function(rising_edge, gpio_set_intr_type, pin_number, GPIO_INTR_POSEDGE);
64 gpio_function(falling_edge, gpio_set_intr_type, pin_number, GPIO_INTR_NEGEDGE);
65 gpio_function(any_edge, gpio_set_intr_type, pin_number, GPIO_INTR_ANYEDGE);
66 gpio_function(low_level, gpio_set_intr_type, pin_number, GPIO_INTR_LOW_LEVEL);
67 gpio_function(high_level, gpio_set_intr_type, pin_number, GPIO_INTR_HIGH_LEVEL);
68 gpio_function(high, gpio_set_level, pin_number, 1);
69 gpio_function(low, gpio_set_level, pin_number, 0);
71 gpio_function(disable_pin, gpio_set_direction, pin_number, GPIO_MODE_DISABLE);
72 gpio_function(input_mode, gpio_set_direction, pin_number, GPIO_MODE_INPUT);
73 gpio_function(output_mode, gpio_set_direction, pin_number, GPIO_MODE_OUTPUT);
74 gpio_function(output_od_mode, gpio_set_direction, pin_number, GPIO_MODE_OUTPUT_OD);
75 gpio_function(input_output_mode, gpio_set_direction, pin_number, GPIO_MODE_INPUT_OUTPUT);
76 gpio_function(input_output_od_mode, gpio_set_direction, pin_number, GPIO_MODE_INPUT_OUTPUT_OD);
79 gpio_function(enable_pullup, gpio_set_pull_mode, pin_number, GPIO_PULLUP_ONLY);
80 gpio_function(enable_pulldown, gpio_set_pull_mode, pin_number, GPIO_PULLDOWN_ONLY);
81 gpio_function(enable_pullup_and_pulldown, gpio_set_pull_mode, pin_number, GPIO_PULLUP_PULLDOWN);
82 gpio_function(disable_pullup_and_pulldown, gpio_set_pull_mode, pin_number, GPIO_FLOATING);
83 gpio_function(disable_pullup, gpio_pullup_dis, pin_number);
84 gpio_function(disable_pulldown, gpio_pulldown_dis, pin_number);
86 gpio_function(rising_edge_wakeup, gpio_wakeup_enable, pin_number, GPIO_INTR_POSEDGE);
87 gpio_function(falling_edge_wakeup, gpio_wakeup_enable, pin_number, GPIO_INTR_NEGEDGE);
88 gpio_function(any_edge_wakeup, gpio_wakeup_enable, pin_number, GPIO_INTR_ANYEDGE);
89 gpio_function(low_level_wakeup, gpio_wakeup_enable, pin_number, GPIO_INTR_LOW_LEVEL);
90 gpio_function(high_level_wakeup, gpio_wakeup_enable, pin_number, GPIO_INTR_HIGH_LEVEL);
91 gpio_function(disable_wakeup, gpio_wakeup_disable, pin_number);
92 gpio_function(wakeup_high, gpio_wakeup_enable, pin_number, GPIO_INTR_HIGH_LEVEL);
93 gpio_function(wakeup_low, gpio_wakeup_enable, pin_number, GPIO_INTR_LOW_LEVEL);
95 gpio_function(set_drive_weakest, gpio_set_drive_capability, pin_number, GPIO_DRIVE_CAP_0);
96 gpio_function(set_drive_weak, gpio_set_drive_capability, pin_number, GPIO_DRIVE_CAP_1);
97 gpio_function(set_drive_medium, gpio_set_drive_capability, pin_number, GPIO_DRIVE_CAP_2);
98 gpio_function(set_drive_strong, gpio_set_drive_capability, pin_number, GPIO_DRIVE_CAP_DEFAULT);
99 gpio_function(set_drive_strongest, gpio_set_drive_capability, pin_number, GPIO_DRIVE_CAP_3);
102 static auto level()
noexcept {
103 return gpio_get_level(pin_number);
106 static auto drive_capability()
noexcept {
107 auto ret = GPIO_DRIVE_CAP_DEFAULT;
108 gpio_get_drive_capability(pin_number, &ret);
114 static_assert(GPIO_NUM_0 <= pin_number && pin_number <= GPIO_NUM_39,
115 "pin number invalid");
118 static_assert(pin_number != GPIO_NUM_0,
"GPIO0 is an important strapping pin"
119 "used during boot to determine SPI boot (pulled up, default) or"
120 "download boot (pulled down). It should not be used for GPIO");
121 static_assert(pin_number != GPIO_NUM_1,
"GPIO1 is UART TXD, used for"
122 "programming, and should not be used for GPIO");
123 static_assert(pin_number != GPIO_NUM_2,
"GPIO2 is an important strapping pin"
124 "that must be pulled down during boot to initiate firmware download."
125 "It should not be used for GPIO");
126 static_assert(pin_number != GPIO_NUM_3,
"GPIO3 is UART_RXD, used for"
127 "programming, and should not be used for GPIO");
128 static_assert(!(GPIO_NUM_6 <= pin_number && pin_number <= GPIO_NUM_11)
129 && pin_number != GPIO_NUM_16 && pin_number != GPIO_NUM_17,
130 "GPIO6-11, 16, and 17 are used by SPI flash memory and shoult not be"
132 static_assert(pin_number != GPIO_NUM_20 && !(GPIO_NUM_28 <= pin_number && pin_number <= GPIO_NUM_32),
133 "GPIO20, and 28-32 likely don't exist, and can't be used for GPIO");