diff --git a/cores/esp32/esp32-hal-periman.c b/cores/esp32/esp32-hal-periman.c index 6ef3c3d984a..ceb5e35efe2 100644 --- a/cores/esp32/esp32-hal-periman.c +++ b/cores/esp32/esp32-hal-periman.c @@ -16,7 +16,7 @@ typedef struct ATTR_PACKED { int8_t bus_channel; } peripheral_pin_item_t; -static peripheral_bus_deinit_cb_t deinit_functions[ESP32_BUS_TYPE_MAX]; +static peripheral_bus_deinit_cb_t deinit_functions[ESP32_BUS_TYPE_MAX] = { NULL }; static peripheral_pin_item_t pins[SOC_GPIO_PIN_COUNT]; #define GPIO_NOT_VALID(p) ((p >= SOC_GPIO_PIN_COUNT) || ((SOC_GPIO_VALID_GPIO_MASK & (1ULL << p)) == 0)) @@ -236,6 +236,14 @@ bool perimanSetBusDeinit(peripheral_bus_type_t type, peripheral_bus_deinit_cb_t return true; } +peripheral_bus_deinit_cb_t perimanGetBusDeinit(peripheral_bus_type_t type) { + if (type >= ESP32_BUS_TYPE_MAX || type == ESP32_BUS_TYPE_INIT) { + log_e("Invalid type: %s (%u)", perimanGetTypeName(type), (unsigned int)type); + return NULL; + } + return deinit_functions[type]; +} + bool perimanPinIsValid(uint8_t pin) { return !(GPIO_NOT_VALID(pin)); } diff --git a/cores/esp32/esp32-hal-periman.h b/cores/esp32/esp32-hal-periman.h index 217d62b8741..36f12581822 100644 --- a/cores/esp32/esp32-hal-periman.h +++ b/cores/esp32/esp32-hal-periman.h @@ -134,6 +134,10 @@ int8_t perimanGetPinBusChannel(uint8_t pin); // Sets the peripheral destructor callback. Used to destroy bus when pin is assigned another function bool perimanSetBusDeinit(peripheral_bus_type_t type, peripheral_bus_deinit_cb_t cb); +// Get the peripheral destructor callback. It allows changing/restoring the peripheral pin function detaching, if necessary +// retrns NULL is none is set +peripheral_bus_deinit_cb_t perimanGetBusDeinit(peripheral_bus_type_t type); + // Check if given pin is a valid GPIO number bool perimanPinIsValid(uint8_t pin);