Hello.
Any possibility to read output pin state for ESP32-S3 and ESP-IDF (master branch)?
If it is still relevant and for those, who are still searching. There is a way to read gpio state, when it in output mode.
It would look like this:
Code: Select all
#include "driver/gpio.h"
#include "hal/gpio_hal.h"
bool get_out_gp_level_hal(gpio_num_t gpio_num) {
gpio_dev_t *hw = GPIO_HAL_GET_HW(GPIO_PORT_0);
if (gpio_num < 32) {
return (hw->out >> gpio_num) & 0x1;
} else {
return (hw->out1.data >> (gpio_num - 32)) & 0x1;
}
}
Be aware: how yout obtain gpio hw pointer is hardware dependent.
I tested that only on ESP32-S3, esp-idf v5.4.
Personally, I wouldn't use this approach unless there's a pressing need. To track pin states, you can create your own bit-field.
Something like this:
Code: Select all
typedef struct valve_pins_states {
uint8_t valve1_pin_state : 1;
uint8_t valve2_pin_state : 1;
uint8_t reserved : 6;
} valve_pins_states;
static valve_pins_states valve_ps;
#define P_VALVE1 GPIO_NUM_38
inline static void close_valve1(void) {
gpio_set_level(P_VALVE1, 0);
valve_ps.valve1_pin_state = 0;
}
inline static void open_valve1(void) {
gpio_set_level(P_VALVE1, 1);
valve_ps.valve1_pin_state = 1;
}
inline static bool is_valve1_open(void) {
return valve_ps.valve1_pin_state;
}
#define P_VALVE2 GPIO_NUM_37
inline static void close_valve2(void) {
gpio_set_level(P_VALVE2, 0);
valve_ps.valve2_pin_state = 0;
}
inline static void open_valve2(void) {
gpio_set_level(P_VALVE2, 1);
valve_ps.valve2_pin_state = 1;
}
inline static bool is_valve2_open(void) {
return valve_ps.valve2_pin_state;
}