|
| 1 | +/* |
| 2 | + * SPDX-FileCopyrightText: 2019-2023 Espressif Systems (Shanghai) CO LTD |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include "esp32-hal-log.h" |
| 8 | +#include "esp32-hal-periman.h" |
| 9 | +#include "soc/soc_caps.h" |
| 10 | + |
| 11 | +typedef struct { |
| 12 | + peripheral_bus_type_t type; |
| 13 | + void * bus; |
| 14 | +} peripheral_pin_item_t; |
| 15 | + |
| 16 | +static peripheral_bus_deinit_cb_t deinit_functions[ESP32_BUS_TYPE_MAX]; |
| 17 | +static peripheral_pin_item_t pins[SOC_GPIO_PIN_COUNT]; |
| 18 | + |
| 19 | +bool perimanSetPinBus(uint8_t pin, peripheral_bus_type_t type, void * bus){ |
| 20 | + peripheral_bus_type_t otype = ESP32_BUS_TYPE_INIT; |
| 21 | + void * obus = NULL; |
| 22 | + if(pin >= SOC_GPIO_PIN_COUNT){ |
| 23 | + log_e("Invalid pin: %u", pin); |
| 24 | + return false; |
| 25 | + } |
| 26 | + if(type >= ESP32_BUS_TYPE_MAX){ |
| 27 | + log_e("Invalid type: %u", (unsigned int)type); |
| 28 | + return false; |
| 29 | + } |
| 30 | + if(type > ESP32_BUS_TYPE_GPIO && bus == NULL){ |
| 31 | + log_e("Bus is NULL"); |
| 32 | + return false; |
| 33 | + } |
| 34 | + otype = pins[pin].type; |
| 35 | + obus = pins[pin].bus; |
| 36 | + if(type == otype && bus == obus){ |
| 37 | + log_i("Bus already set"); |
| 38 | + return true; |
| 39 | + } |
| 40 | + if(obus != NULL){ |
| 41 | + if(deinit_functions[otype] == NULL){ |
| 42 | + log_e("Bus does not have deinit function set"); |
| 43 | + return false; |
| 44 | + } |
| 45 | + if(!deinit_functions[otype](obus)){ |
| 46 | + log_e("Previous bus failed to deinit"); |
| 47 | + return false; |
| 48 | + } |
| 49 | + } |
| 50 | + pins[pin].type = type; |
| 51 | + pins[pin].bus = bus; |
| 52 | + return true; |
| 53 | +} |
| 54 | + |
| 55 | +void * perimanGetPinBus(uint8_t pin, peripheral_bus_type_t type){ |
| 56 | + if(pin >= SOC_GPIO_PIN_COUNT){ |
| 57 | + log_e("Invalid pin: %u", pin); |
| 58 | + return NULL; |
| 59 | + } |
| 60 | + if(type >= ESP32_BUS_TYPE_MAX){ |
| 61 | + log_e("Invalid type: %u", (unsigned int)type); |
| 62 | + return NULL; |
| 63 | + } |
| 64 | + if(pins[pin].type == type){ |
| 65 | + return pins[pin].bus; |
| 66 | + } |
| 67 | + return NULL; |
| 68 | +} |
| 69 | + |
| 70 | +peripheral_bus_type_t perimanGetPinBusType(uint8_t pin){ |
| 71 | + if(pin >= SOC_GPIO_PIN_COUNT){ |
| 72 | + log_e("Invalid pin: %u", pin); |
| 73 | + return ESP32_BUS_TYPE_MAX; |
| 74 | + } |
| 75 | + return pins[pin].type; |
| 76 | +} |
| 77 | + |
| 78 | +bool perimanSetBusDeinit(peripheral_bus_type_t type, peripheral_bus_deinit_cb_t cb){ |
| 79 | + if(type >= ESP32_BUS_TYPE_MAX){ |
| 80 | + log_e("Invalid type: %u", (unsigned int)type); |
| 81 | + return false; |
| 82 | + } |
| 83 | + if(cb == NULL){ |
| 84 | + log_e("Callback is NULL"); |
| 85 | + return false; |
| 86 | + } |
| 87 | + deinit_functions[type] = cb; |
| 88 | + return true; |
| 89 | +} |
| 90 | + |
| 91 | + |
0 commit comments