Unverified Commit 0d0d27fb authored by Jan Procházka's avatar Jan Procházka Committed by GitHub

Extend bus types and structure for Peripheral Manager (#8888)

* Extended bus types for peripheral manager

* add extra_type to peripheral manager

* Fix function call

* Fix check for no extra_type

* Add extra_type for SD and ETH CS pin

* added bus_num and bus_channel to pin structure

* update printPerimanInfo with new fields

* replaced setting bus to INIT with DetachPin call

* add defines for extra attributes

* use new required parameters in perimanSetPinBus

* function rename

* removed duplicate define

* Update print format

---------
Co-authored-by: default avatarRodrigo Garcia <rodrigo.garcia@espressif.com>
parent 600f3591
......@@ -39,6 +39,7 @@
#include "stdlib_noniso.h"
#include "binary.h"
#include "extra_attr.h"
#define PI 3.1415926535897932384626433832795
#define HALF_PI 1.5707963267948966192313216916398
......
......@@ -181,8 +181,8 @@ bool HWCDC::deinit(void * busptr)
running = true;
// Setting USB D+ D- pins
bool retCode = true;
retCode &= perimanSetPinBus(USB_DM_GPIO_NUM, ESP32_BUS_TYPE_INIT, NULL);
retCode &= perimanSetPinBus(USB_DP_GPIO_NUM, ESP32_BUS_TYPE_INIT, NULL);
retCode &= perimanClearPinBus(USB_DM_GPIO_NUM);
retCode &= perimanClearPinBus(USB_DP_GPIO_NUM);
if (retCode) {
// Force the host to re-enumerate (BUS_RESET)
pinMode(USB_DM_GPIO_NUM, OUTPUT_OPEN_DRAIN);
......@@ -220,10 +220,10 @@ void HWCDC::begin(unsigned long baud)
end();
return;
}
if (perimanSetBusDeinit(ESP32_BUS_TYPE_USB, HWCDC::deinit)) {
if (perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DM, HWCDC::deinit) && perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DP, HWCDC::deinit)) {
// Setting USB D+ D- pins
perimanSetPinBus(USB_DM_GPIO_NUM, ESP32_BUS_TYPE_USB, (void *) this);
perimanSetPinBus(USB_DP_GPIO_NUM, ESP32_BUS_TYPE_USB, (void *) this);
perimanSetPinBus(USB_DM_GPIO_NUM, ESP32_BUS_TYPE_USB_DM, (void *) this, -1, -1);
perimanSetPinBus(USB_DP_GPIO_NUM, ESP32_BUS_TYPE_USB_DP, (void *) this, -1, -1);
} else {
log_e("Serial JTAG Pins can't be set into Peripheral Manager.");
}
......
......@@ -242,6 +242,8 @@ static void printBoardInfo(void){
static void printPerimanInfo(void){
chip_report_printf("GPIO Info:\n");
chip_report_printf("------------------------------------------\n");
chip_report_printf(" GPIO : BUS_TYPE[bus/unit][chan]\n");
chip_report_printf(" -------------------------------------- \n");
for(uint8_t i = 0; i < SOC_GPIO_PIN_COUNT; i++){
if(!perimanPinIsValid(i)){
continue;//invalid pin
......@@ -250,8 +252,23 @@ static void printPerimanInfo(void){
if(type == ESP32_BUS_TYPE_INIT){
continue;//unused pin
}
chip_report_printf(" %17u : ", i);
chip_report_printf("%s\n", perimanGetTypeName(type));
const char* extra_type = perimanGetPinBusExtraType(i);
chip_report_printf(" %4u : ", i);
if(extra_type){
chip_report_printf("%s", extra_type);
}
else {
chip_report_printf("%s", perimanGetTypeName(type));
}
int8_t bus_number = perimanGetPinBusNum(i);
if (bus_number != -1){
chip_report_printf("[%u]", bus_number);
}
int8_t bus_channel = perimanGetPinBusChannel(i);
if (bus_channel != -1){
chip_report_printf("[%u]", bus_channel);
}
chip_report_printf("\n");
}
}
......
......@@ -221,7 +221,7 @@ esp_err_t __analogInit(uint8_t pin, adc_channel_t channel, adc_unit_t adc_unit){
}
}
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_ADC_ONESHOT, (void *)(pin+1))){
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_ADC_ONESHOT, (void *)(pin+1), adc_unit, channel)){
adcDetachBus((void *)(pin+1));
return err;
}
......@@ -396,7 +396,7 @@ static bool adcContinuousDetachBus(void * adc_unit_number){
int io_pin;
adc_oneshot_channel_to_io(adc_unit, channel, &io_pin);
if(perimanGetPinBusType(io_pin) == ESP32_BUS_TYPE_ADC_CONT){
if(!perimanSetPinBus(io_pin, ESP32_BUS_TYPE_INIT, NULL)){
if(!perimanClearPinBus(io_pin)){
return false;
}
}
......@@ -497,7 +497,7 @@ bool analogContinuous(uint8_t pins[], size_t pins_count, uint32_t conversions_pe
//Set periman deinit function and reset all pins to init state.
perimanSetBusDeinit(ESP32_BUS_TYPE_ADC_CONT, adcContinuousDetachBus);
for(int j = 0; j < pins_count; j++){
if(!perimanSetPinBus(pins[j], ESP32_BUS_TYPE_INIT, NULL)){
if(!perimanClearPinBus(pins[j])){
return false;
}
}
......@@ -571,7 +571,7 @@ bool analogContinuous(uint8_t pins[], size_t pins_count, uint32_t conversions_pe
}
for(int k = 0; k < pins_count; k++){
if(!perimanSetPinBus(pins[k], ESP32_BUS_TYPE_ADC_CONT, (void *)(adc_unit+1))){
if(!perimanSetPinBus(pins[k], ESP32_BUS_TYPE_ADC_CONT, (void *)(adc_unit+1), adc_unit, channel[k])){
log_e("perimanSetPinBus to ADC Continuous failed!");
adcContinuousDetachBus((void *)(adc_unit+1));
return false;
......
......@@ -32,7 +32,7 @@ bool __dacWrite(uint8_t pin, uint8_t value)
dac_oneshot_handle_t bus = (dac_oneshot_handle_t)perimanGetPinBus(pin, ESP32_BUS_TYPE_DAC_ONESHOT);
if(bus == NULL){
perimanSetBusDeinit(ESP32_BUS_TYPE_DAC_ONESHOT, dacDetachBus);
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL)){
if(!perimanClearPinBus(pin)){
return false;
}
dac_channel_t channel = (pin == DAC_CHAN0_GPIO_NUM)?DAC_CHAN_0:DAC_CHAN_1;
......@@ -44,7 +44,7 @@ bool __dacWrite(uint8_t pin, uint8_t value)
log_e("dac_oneshot_new_channel failed with error: %d", err);
return false;
}
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_DAC_ONESHOT, (void *)bus)){
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_DAC_ONESHOT, (void *)bus, -1, channel)){
dacDetachBus((void *)bus);
return false;
}
......@@ -67,7 +67,7 @@ bool __dacDisable(uint8_t pin)
void * bus = perimanGetPinBus(pin, ESP32_BUS_TYPE_DAC_ONESHOT);
if(bus != NULL){
// will call dacDetachBus
return perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL);
return perimanClearPinBus(pin);
} else {
log_e("pin %u is not attached to DAC", pin);
}
......
......@@ -110,7 +110,7 @@ extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode)
if(perimanGetPinBus(pin, ESP32_BUS_TYPE_GPIO) == NULL){
perimanSetBusDeinit(ESP32_BUS_TYPE_GPIO, gpioDetachBus);
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL)){
if(!perimanClearPinBus(pin)){
log_e("Deinit of previous bus failed");
return;
}
......@@ -144,7 +144,7 @@ extern void ARDUINO_ISR_ATTR __pinMode(uint8_t pin, uint8_t mode)
return;
}
if(perimanGetPinBus(pin, ESP32_BUS_TYPE_GPIO) == NULL){
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_GPIO, (void *)(pin+1))){
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_GPIO, (void *)(pin+1), -1, -1)){
//gpioDetachBus((void *)(pin+1));
return;
}
......
......@@ -235,8 +235,10 @@ esp_err_t i2cSlaveInit(uint8_t num, int sda, int scl, uint16_t slaveID, uint32_t
frequency = 1000000;
}
perimanSetBusDeinit(ESP32_BUS_TYPE_I2C_SLAVE, i2cSlaveDetachBus);
if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_INIT, NULL) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_INIT, NULL)){
perimanSetBusDeinit(ESP32_BUS_TYPE_I2C_SLAVE_SDA, i2cSlaveDetachBus);
perimanSetBusDeinit(ESP32_BUS_TYPE_I2C_SLAVE_SCL, i2cSlaveDetachBus);
if(!perimanClearPinBus(sda) || !perimanClearPinBus(scl)){
return false;
}
......@@ -353,7 +355,7 @@ esp_err_t i2cSlaveInit(uint8_t num, int sda, int scl, uint16_t slaveID, uint32_t
i2c_ll_slave_enable_rx_it(i2c->dev);
i2c_ll_set_stretch(i2c->dev, 0x3FF);
i2c_ll_update(i2c->dev);
if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_SLAVE, (void *)(i2c->num+1)) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_SLAVE, (void *)(i2c->num+1))){
if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_SLAVE_SDA, (void *)(i2c->num+1), i2c->num, -1) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_SLAVE_SCL, (void *)(i2c->num+1), i2c->num, -1)){
i2cSlaveDetachBus((void *)(i2c->num+1));
ret = ESP_FAIL;
}
......@@ -383,8 +385,8 @@ esp_err_t i2cSlaveDeinit(uint8_t num){
int scl = i2c->scl;
int sda = i2c->sda;
i2c_slave_free_resources(i2c);
perimanSetPinBus(scl, ESP32_BUS_TYPE_INIT, NULL);
perimanSetPinBus(sda, ESP32_BUS_TYPE_INIT, NULL);
perimanClearPinBus(scl);
perimanClearPinBus(sda);
I2C_SLAVE_MUTEX_UNLOCK();
return ESP_OK;
}
......
......@@ -98,8 +98,10 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency){
frequency = 1000000UL;
}
perimanSetBusDeinit(ESP32_BUS_TYPE_I2C_MASTER, i2cDetachBus);
if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_INIT, NULL) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_INIT, NULL)){
perimanSetBusDeinit(ESP32_BUS_TYPE_I2C_MASTER_SDA, i2cDetachBus);
perimanSetBusDeinit(ESP32_BUS_TYPE_I2C_MASTER_SCL, i2cDetachBus);
if(!perimanClearPinBus(sda) || !perimanClearPinBus(scl)){
return false;
}
......@@ -128,7 +130,7 @@ esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency){
bus[i2c_num].sda = sda;
//Clock Stretching Timeout: 20b:esp32, 5b:esp32-c3, 24b:esp32-s2
i2c_set_timeout((i2c_port_t)i2c_num, I2C_LL_MAX_TIMEOUT);
if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_MASTER, (void *)(i2c_num+1)) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_MASTER, (void *)(i2c_num+1))){
if(!perimanSetPinBus(sda, ESP32_BUS_TYPE_I2C_MASTER_SDA, (void *)(i2c_num+1), i2c_num, -1) || !perimanSetPinBus(scl, ESP32_BUS_TYPE_I2C_MASTER_SCL, (void *)(i2c_num+1), i2c_num, -1)){
i2cDetachBus((void *)(i2c_num+1));
return false;
}
......@@ -159,8 +161,8 @@ esp_err_t i2cDeinit(uint8_t i2c_num){
err = i2c_driver_delete((i2c_port_t)i2c_num);
if(err == ESP_OK){
bus[i2c_num].initialized = false;
perimanSetPinBus(bus[i2c_num].scl, ESP32_BUS_TYPE_INIT, NULL);
perimanSetPinBus(bus[i2c_num].sda, ESP32_BUS_TYPE_INIT, NULL);
perimanClearPinBus(bus[i2c_num].scl);
perimanClearPinBus(bus[i2c_num].sda);
bus[i2c_num].scl = -1;
bus[i2c_num].sda = -1;
}
......
......@@ -67,7 +67,7 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution)
perimanSetBusDeinit(ESP32_BUS_TYPE_LEDC, ledcDetachBus);
ledc_channel_handle_t *bus = (ledc_channel_handle_t*)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC);
if(bus != NULL && !perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL)){
if(bus != NULL && !perimanClearPinBus(pin)){
return false;
}
......@@ -110,7 +110,7 @@ bool ledcAttach(uint8_t pin, uint32_t freq, uint8_t resolution)
#endif
ledc_handle.used_channels |= 1UL << channel;
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_LEDC, (void *)handle)){
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_LEDC, (void *)handle, group, channel)){
ledcDetachBus((void *)handle);
return false;
}
......@@ -216,7 +216,7 @@ bool ledcDetach(uint8_t pin)
ledc_channel_handle_t *bus = (ledc_channel_handle_t*)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC);
if(bus != NULL){
// will call ledcDetachBus
return perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL);
return perimanClearPinBus(pin);
} else {
log_e("pin %u is not attached to LEDC", pin);
}
......@@ -360,7 +360,7 @@ void analogWrite(uint8_t pin, int value) {
// Use ledc hardware for internal pins
if (pin < SOC_GPIO_PIN_COUNT) {
ledc_channel_handle_t *bus = (ledc_channel_handle_t*)perimanGetPinBus(pin, ESP32_BUS_TYPE_LEDC);
if(bus == NULL && perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL)){
if(bus == NULL && perimanClearPinBus(pin)){
if(ledcAttach(pin, analog_frequency, analog_resolution) == 0){
log_e("analogWrite setup failed (freq = %u, resolution = %u). Try setting different resolution or frequency");
return;
......
......@@ -8,9 +8,12 @@
#include "esp32-hal-periman.h"
#include "esp_bit_defs.h"
typedef struct {
typedef struct ATTR_PACKED {
peripheral_bus_type_t type;
const char* extra_type;
void * bus;
int8_t bus_num;
int8_t bus_channel;
} peripheral_pin_item_t;
static peripheral_bus_deinit_cb_t deinit_functions[ESP32_BUS_TYPE_MAX];
......@@ -46,32 +49,67 @@ const char* perimanGetTypeName(peripheral_bus_type_t type) {
case ESP32_BUS_TYPE_RMT_RX: return "RMT_RX";
#endif
#if SOC_I2S_SUPPORTED
case ESP32_BUS_TYPE_I2S_STD: return "I2S_STD";
case ESP32_BUS_TYPE_I2S_TDM: return "I2S_TDM";
case ESP32_BUS_TYPE_I2S_PDM_TX: return "I2S_PDM_TX";
case ESP32_BUS_TYPE_I2S_PDM_RX: return "I2S_PDM_RX";
case ESP32_BUS_TYPE_I2S_STD_MCLK: return "I2S_STD_MCLK";
case ESP32_BUS_TYPE_I2S_STD_BCLK: return "I2S_STD_BCLK";
case ESP32_BUS_TYPE_I2S_STD_WS: return "I2S_STD_WS";
case ESP32_BUS_TYPE_I2S_STD_DOUT: return "I2S_STD_DOUT";
case ESP32_BUS_TYPE_I2S_STD_DIN: return "I2S_STD_DIN";
case ESP32_BUS_TYPE_I2S_TDM_MCLK: return "I2S_TDM_MCLK";
case ESP32_BUS_TYPE_I2S_TDM_BCLK: return "I2S_TDM_BCLK";
case ESP32_BUS_TYPE_I2S_TDM_WS: return "I2S_TDM_WS";
case ESP32_BUS_TYPE_I2S_TDM_DOUT: return "I2S_TDM_DOUT";
case ESP32_BUS_TYPE_I2S_TDM_DIN: return "I2S_TDM_DIN";
case ESP32_BUS_TYPE_I2S_PDM_TX_CLK: return "I2S_PDM_TX_CLK";
case ESP32_BUS_TYPE_I2S_PDM_TX_DOUT0: return "I2S_PDM_TX_DOUT0";
case ESP32_BUS_TYPE_I2S_PDM_TX_DOUT1: return "I2S_PDM_TX_DOUT1";
case ESP32_BUS_TYPE_I2S_PDM_RX_CLK: return "I2S_PDM_RX_CLK";
case ESP32_BUS_TYPE_I2S_PDM_RX_DIN0: return "I2S_PDM_RX_DIN0";
case ESP32_BUS_TYPE_I2S_PDM_RX_DIN1: return "I2S_PDM_RX_DIN1";
case ESP32_BUS_TYPE_I2S_PDM_RX_DIN2: return "I2S_PDM_RX_DIN2";
case ESP32_BUS_TYPE_I2S_PDM_RX_DIN3: return "I2S_PDM_RX_DIN3";
#endif
#if SOC_I2C_SUPPORTED
case ESP32_BUS_TYPE_I2C_MASTER: return "I2C_MASTER";
case ESP32_BUS_TYPE_I2C_SLAVE: return "I2C_SLAVE";
case ESP32_BUS_TYPE_I2C_MASTER_SDA: return "I2C_MASTER_SDA";
case ESP32_BUS_TYPE_I2C_MASTER_SCL: return "I2C_MASTER_SCL";
case ESP32_BUS_TYPE_I2C_SLAVE_SDA: return "I2C_SLAVE_SDA";
case ESP32_BUS_TYPE_I2C_SLAVE_SCL: return "I2C_SLAVE_SCL";
#endif
#if SOC_GPSPI_SUPPORTED
case ESP32_BUS_TYPE_SPI_MASTER: return "SPI_MASTER";
case ESP32_BUS_TYPE_SPI_MASTER_SCK: return "SPI_MASTER_SCK";
case ESP32_BUS_TYPE_SPI_MASTER_MISO: return "SPI_MASTER_MISO";
case ESP32_BUS_TYPE_SPI_MASTER_MOSI: return "SPI_MASTER_MOSI";
case ESP32_BUS_TYPE_SPI_MASTER_CS: return "SPI_MASTER_CS";
#endif
#if SOC_SDMMC_HOST_SUPPORTED
case ESP32_BUS_TYPE_SDMMC: return "SDMMC";
case ESP32_BUS_TYPE_SDMMC_CLK: return "SDMMC_CLK";
case ESP32_BUS_TYPE_SDMMC_CMD: return "SDMMC_CMD";
case ESP32_BUS_TYPE_SDMMC_D0: return "SDMMC_D0";
case ESP32_BUS_TYPE_SDMMC_D1: return "SDMMC_D1";
case ESP32_BUS_TYPE_SDMMC_D2: return "SDMMC_D2";
case ESP32_BUS_TYPE_SDMMC_D3: return "SDMMC_D3";
#endif
#if SOC_TOUCH_SENSOR_SUPPORTED
case ESP32_BUS_TYPE_TOUCH: return "TOUCH";
#endif
#if SOC_USB_SERIAL_JTAG_SUPPORTED || SOC_USB_OTG_SUPPORTED
case ESP32_BUS_TYPE_USB: return "USB";
case ESP32_BUS_TYPE_USB_DM: return "USB_DM";
case ESP32_BUS_TYPE_USB_DP: return "USB_DP";
#endif
#if SOC_GPSPI_SUPPORTED
case ESP32_BUS_TYPE_ETHERNET_SPI: return "ETHERNET_SPI";
#endif
#if CONFIG_ETH_USE_ESP32_EMAC
case ESP32_BUS_TYPE_ETHERNET_RMII: return "ETHERNET_RMII";
case ESP32_BUS_TYPE_ETHERNET_CLK: return "ETHERNET_CLK";
case ESP32_BUS_TYPE_ETHERNET_MCD: return "ETHERNET_MCD";
case ESP32_BUS_TYPE_ETHERNET_MDIO: return "ETHERNET_MDIO";
case ESP32_BUS_TYPE_ETHERNET_PWR: return "ETHERNET_PWR";
#endif
default: return "UNKNOWN";
}
}
bool perimanSetPinBus(uint8_t pin, peripheral_bus_type_t type, void * bus){
bool perimanSetPinBus(uint8_t pin, peripheral_bus_type_t type, void * bus, int8_t bus_num, int8_t bus_channel){
peripheral_bus_type_t otype = ESP32_BUS_TYPE_INIT;
void * obus = NULL;
if(GPIO_NOT_VALID(pin)){
......@@ -110,10 +148,27 @@ bool perimanSetPinBus(uint8_t pin, peripheral_bus_type_t type, void * bus){
}
pins[pin].type = type;
pins[pin].bus = bus;
pins[pin].bus_num = bus_num;
pins[pin].bus_channel = bus_channel;
pins[pin].extra_type = NULL;
log_v("Pin %u successfully set to type %s (%u) with bus %p", pin, perimanGetTypeName(type), (unsigned int)type, bus);
return true;
}
bool perimanSetPinBusExtraType(uint8_t pin, const char* extra_type){
if(GPIO_NOT_VALID(pin)){
log_e("Invalid pin: %u", pin);
return false;
}
if (pins[pin].type == ESP32_BUS_TYPE_INIT) {
log_e("Can't set extra type for Bus INIT Type (pin %u)", pin);
return false;
}
pins[pin].extra_type = extra_type;
log_v("Successfully set extra_type %s for pin %u", extra_type, pin);
return true;
}
void * perimanGetPinBus(uint8_t pin, peripheral_bus_type_t type){
if(GPIO_NOT_VALID(pin)){
log_e("Invalid pin: %u", pin);
......@@ -137,6 +192,30 @@ peripheral_bus_type_t perimanGetPinBusType(uint8_t pin){
return pins[pin].type;
}
const char* perimanGetPinBusExtraType(uint8_t pin){
if(GPIO_NOT_VALID(pin)){
log_e("Invalid pin: %u", pin);
return NULL;
}
return pins[pin].extra_type;
}
int8_t perimanGetPinBusNum(uint8_t pin){
if(GPIO_NOT_VALID(pin)){
log_e("Invalid pin: %u", pin);
return -1;
}
return pins[pin].bus_num;
}
int8_t perimanGetPinBusChannel(uint8_t pin){
if(GPIO_NOT_VALID(pin)){
log_e("Invalid pin: %u", pin);
return -1;
}
return pins[pin].bus_channel;
}
bool perimanSetBusDeinit(peripheral_bus_type_t type, peripheral_bus_deinit_cb_t cb){
if(type >= ESP32_BUS_TYPE_MAX || type == ESP32_BUS_TYPE_INIT){
log_e("Invalid type: %s (%u)", perimanGetTypeName(type), (unsigned int)type);
......
......@@ -14,6 +14,8 @@ extern "C"
#include <stdbool.h>
#include <stddef.h>
#define perimanClearPinBus(p) perimanSetPinBus(p, ESP32_BUS_TYPE_INIT, NULL, -1, -1)
typedef enum {
ESP32_BUS_TYPE_INIT, // IO has not been attached to a bus yet
ESP32_BUS_TYPE_GPIO, // IO is used as GPIO
......@@ -41,29 +43,64 @@ typedef enum {
ESP32_BUS_TYPE_RMT_RX, // IO is used as RMT input
#endif
#if SOC_I2S_SUPPORTED
ESP32_BUS_TYPE_I2S_STD, // IO is used as I2S STD pin
ESP32_BUS_TYPE_I2S_TDM, // IO is used as I2S TDM pin
ESP32_BUS_TYPE_I2S_PDM_TX, // IO is used as I2S PDM pin
ESP32_BUS_TYPE_I2S_PDM_RX, // IO is used as I2S PDM pin
ESP32_BUS_TYPE_I2S_STD_MCLK, // IO is used as I2S STD MCLK pin
ESP32_BUS_TYPE_I2S_STD_BCLK, // IO is used as I2S STD BCLK pin
ESP32_BUS_TYPE_I2S_STD_WS, // IO is used as I2S STD WS pin
ESP32_BUS_TYPE_I2S_STD_DOUT, // IO is used as I2S STD DOUT pin
ESP32_BUS_TYPE_I2S_STD_DIN, // IO is used as I2S STD DIN pin
ESP32_BUS_TYPE_I2S_TDM_MCLK, // IO is used as I2S TDM MCLK pin
ESP32_BUS_TYPE_I2S_TDM_BCLK, // IO is used as I2S TDM BCLK pin
ESP32_BUS_TYPE_I2S_TDM_WS, // IO is used as I2S TDM WS pin
ESP32_BUS_TYPE_I2S_TDM_DOUT, // IO is used as I2S TDM DOUT pin
ESP32_BUS_TYPE_I2S_TDM_DIN, // IO is used as I2S TDM DIN pin
ESP32_BUS_TYPE_I2S_PDM_TX_CLK, // IO is used as I2S PDM CLK pin
ESP32_BUS_TYPE_I2S_PDM_TX_DOUT0,// IO is used as I2S PDM DOUT0 pin
ESP32_BUS_TYPE_I2S_PDM_TX_DOUT1,// IO is used as I2S PDM DOUT1 pin
ESP32_BUS_TYPE_I2S_PDM_RX_CLK, // IO is used as I2S PDM CLK pin
ESP32_BUS_TYPE_I2S_PDM_RX_DIN0, // IO is used as I2S PDM DIN0 pin
ESP32_BUS_TYPE_I2S_PDM_RX_DIN1, // IO is used as I2S PDM DIN1 pin
ESP32_BUS_TYPE_I2S_PDM_RX_DIN2, // IO is used as I2S PDM DIN2 pin
ESP32_BUS_TYPE_I2S_PDM_RX_DIN3, // IO is used as I2S PDM DIN3 pin
#endif
#if SOC_I2C_SUPPORTED
ESP32_BUS_TYPE_I2C_MASTER, // IO is used as I2C master pin
ESP32_BUS_TYPE_I2C_SLAVE, // IO is used as I2C slave pin
ESP32_BUS_TYPE_I2C_MASTER_SDA, // IO is used as I2C master SDA pin
ESP32_BUS_TYPE_I2C_MASTER_SCL, // IO is used as I2C master SCL pin
ESP32_BUS_TYPE_I2C_SLAVE_SDA, // IO is used as I2C slave SDA pin
ESP32_BUS_TYPE_I2C_SLAVE_SCL, // IO is used as I2C slave SCL pin
#endif
#if SOC_GPSPI_SUPPORTED
ESP32_BUS_TYPE_SPI_MASTER, // IO is used as SPI master pin
ESP32_BUS_TYPE_SPI_MASTER_SCK, // IO is used as SPI master SCK pin
ESP32_BUS_TYPE_SPI_MASTER_MISO, // IO is used as SPI master MISO pin
ESP32_BUS_TYPE_SPI_MASTER_MOSI, // IO is used as SPI master MOSI pin
ESP32_BUS_TYPE_SPI_MASTER_CS, // IO is used as SPI master CS pin
#endif
#if SOC_SDMMC_HOST_SUPPORTED
ESP32_BUS_TYPE_SDMMC, // IO is used as SDMMC pin
ESP32_BUS_TYPE_SDMMC_CLK, // IO is used as SDMMC CLK pin
ESP32_BUS_TYPE_SDMMC_CMD, // IO is used as SDMMC CMD pin
ESP32_BUS_TYPE_SDMMC_D0, // IO is used as SDMMC D0 pin
ESP32_BUS_TYPE_SDMMC_D1, // IO is used as SDMMC D1 pin
ESP32_BUS_TYPE_SDMMC_D2, // IO is used as SDMMC D2 pin
ESP32_BUS_TYPE_SDMMC_D3, // IO is used as SDMMC D3 pin
#endif
#if SOC_TOUCH_SENSOR_SUPPORTED
ESP32_BUS_TYPE_TOUCH, // IO is used as TOUCH pin
#endif
#if SOC_USB_SERIAL_JTAG_SUPPORTED || SOC_USB_OTG_SUPPORTED
ESP32_BUS_TYPE_USB, // IO is used as USB pin
ESP32_BUS_TYPE_USB_DM, // IO is used as USB DM (+) pin
ESP32_BUS_TYPE_USB_DP, // IO is used as USB DP (-) pin
#endif
#if SOC_GPSPI_SUPPORTED
ESP32_BUS_TYPE_ETHERNET, // IO is used as ETHERNET-RMII pin
ESP32_BUS_TYPE_ETHERNET_SPI, // IO is used as ETHERNET SPI pin
#endif
#if CONFIG_ETH_USE_ESP32_EMAC
ESP32_BUS_TYPE_ETHERNET_RMII, // IO is used as ETHERNET RMII pin
ESP32_BUS_TYPE_ETHERNET_CLK, // IO is used as ETHERNET CLK pin
ESP32_BUS_TYPE_ETHERNET_MCD, // IO is used as ETHERNET MCD pin
ESP32_BUS_TYPE_ETHERNET_MDIO, // IO is used as ETHERNET MDIO pin
ESP32_BUS_TYPE_ETHERNET_PWR, // IO is used as ETHERNET PWR pin
#endif
ESP32_BUS_TYPE_MAX
} peripheral_bus_type_t;
......@@ -72,8 +109,8 @@ typedef bool (*peripheral_bus_deinit_cb_t)(void * bus);
const char* perimanGetTypeName(peripheral_bus_type_t type);
// Sets the bus type and bus handle for given pin.
bool perimanSetPinBus(uint8_t pin, peripheral_bus_type_t type, void * bus);
// Sets the bus type, bus handle, bus number and bus channel for given pin.
bool perimanSetPinBus(uint8_t pin, peripheral_bus_type_t type, void * bus, int8_t bus_num, int8_t bus_channel);
// Returns handle of the bus for the given pin if type of bus matches. NULL otherwise
void * perimanGetPinBus(uint8_t pin, peripheral_bus_type_t type);
......@@ -81,12 +118,24 @@ void * perimanGetPinBus(uint8_t pin, peripheral_bus_type_t type);
// Returns the type of the bus for the given pin if attached. ESP32_BUS_TYPE_MAX otherwise
peripheral_bus_type_t perimanGetPinBusType(uint8_t pin);
// Returns the bus number or unit of the bus for the given pin if set. -1 otherwise
int8_t perimanGetPinBusNum(uint8_t pin);
// Returns the bus channel of the bus for the given pin if set. -1 otherwise
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);
// Check if given pin is a valid GPIO number
bool perimanPinIsValid(uint8_t pin);
// Sets the extra type for non Init bus. Used to customise pin bus name which can be printed by printPerimanInfo().
bool perimanSetPinBusExtraType(uint8_t pin, const char* extra_type);
// Returns the extra type of the bus for given pin if set. NULL otherwise
const char* perimanGetPinBusExtraType(uint8_t pin);
#ifdef __cplusplus
}
#endif
......@@ -270,7 +270,7 @@ bool rmtDeinit(int pin)
log_v("Deiniting RMT GPIO %d", pin);
if (_rmtGetBus(pin, __FUNCTION__) != NULL) {
// release all allocated data
return perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL);
return perimanClearPinBus(pin);
}
log_e("GPIO %d - No RMT channel associated.", pin);
return false;
......@@ -468,7 +468,7 @@ bool rmtInit(int pin, rmt_ch_dir_t channel_direction, rmt_reserve_memsize_t mem_
}
// Try to dettach any (Tx|Rx|Whatever) previous bus or just keep it as not attached
if (!perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL)) {
if (!perimanClearPinBus(pin)) {
log_w("GPIO %d - Can't detach previous peripheral.", pin);
return false;
}
......@@ -574,7 +574,7 @@ bool rmtInit(int pin, rmt_ch_dir_t channel_direction, rmt_reserve_memsize_t mem_
// Finally, allocate Peripheral Manager RMT bus and associate it to its GPIO
peripheral_bus_type_t pinBusType =
channel_direction == RMT_TX_MODE ? ESP32_BUS_TYPE_RMT_TX : ESP32_BUS_TYPE_RMT_RX;
if (!perimanSetPinBus(pin, pinBusType, (void *) bus)) {
if (!perimanSetPinBus(pin, pinBusType, (void *) bus, -1, -1)) {
log_e("Can't allocate the GPIO %d in the Peripheral Manager.", pin);
goto Err;
}
......
......@@ -28,7 +28,7 @@ bool sigmaDeltaAttach(uint8_t pin, uint32_t freq) //freq 1220-312500
{
perimanSetBusDeinit(ESP32_BUS_TYPE_SIGMADELTA, sigmaDeltaDetachBus);
sdm_channel_handle_t bus = (sdm_channel_handle_t)perimanGetPinBus(pin, ESP32_BUS_TYPE_SIGMADELTA);
if(bus != NULL && !perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL)){
if(bus != NULL && !perimanClearPinBus(pin)){
return false;
}
bus = NULL;
......@@ -52,7 +52,7 @@ bool sigmaDeltaAttach(uint8_t pin, uint32_t freq) //freq 1220-312500
log_e("sdm_channel_enable failed with error: %d", err);
return false;
}
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_SIGMADELTA, (void *)bus)){
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_SIGMADELTA, (void *)bus, -1, -1)){
sigmaDeltaDetachBus((void *)bus);
return false;
}
......@@ -81,7 +81,7 @@ bool sigmaDeltaDetach(uint8_t pin)
void * bus = perimanGetPinBus(pin, ESP32_BUS_TYPE_SIGMADELTA);
if(bus != NULL){
// will call sigmaDeltaDetachBus
return perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL);
return perimanClearPinBus(pin);
} else {
log_e("pin %u is not attached to SigmaDelta", pin);
}
......
......@@ -212,14 +212,14 @@ bool spiAttachSCK(spi_t * spi, int8_t sck)
if(!spi || sck < 0) {
return false;
}
void * bus = perimanGetPinBus(sck, ESP32_BUS_TYPE_SPI_MASTER);
if(bus != NULL && !perimanSetPinBus(sck, ESP32_BUS_TYPE_INIT, NULL)){
void * bus = perimanGetPinBus(sck, ESP32_BUS_TYPE_SPI_MASTER_SCK);
if(bus != NULL && !perimanClearPinBus(sck)){
return false;
}
pinMode(sck, OUTPUT);
pinMatrixOutAttach(sck, SPI_CLK_IDX(spi->num), false, false);
spi->sck = sck;
if(!perimanSetPinBus(sck, ESP32_BUS_TYPE_SPI_MASTER, (void *)(spi->num+1))){
if(!perimanSetPinBus(sck, ESP32_BUS_TYPE_SPI_MASTER_SCK, (void *)(spi->num+1), spi->num, -1)){
spiDetachBus((void *)(spi->num+1));
log_e("Failed to set pin bus to SPI for pin %d", sck);
return false;
......@@ -232,8 +232,8 @@ bool spiAttachMISO(spi_t * spi, int8_t miso)
if(!spi || miso < 0) {
return false;
}
void * bus = perimanGetPinBus(miso, ESP32_BUS_TYPE_SPI_MASTER);
if(bus != NULL && !perimanSetPinBus(miso, ESP32_BUS_TYPE_INIT, NULL)){
void * bus = perimanGetPinBus(miso, ESP32_BUS_TYPE_SPI_MASTER_MISO);
if(bus != NULL && !perimanClearPinBus(miso)){
return false;
}
SPI_MUTEX_LOCK();
......@@ -241,7 +241,7 @@ bool spiAttachMISO(spi_t * spi, int8_t miso)
pinMatrixInAttach(miso, SPI_MISO_IDX(spi->num), false);
spi->miso = miso;
SPI_MUTEX_UNLOCK();
if(!perimanSetPinBus(miso, ESP32_BUS_TYPE_SPI_MASTER, (void *)(spi->num+1))){
if(!perimanSetPinBus(miso, ESP32_BUS_TYPE_SPI_MASTER_MISO, (void *)(spi->num+1), spi->num, -1)){
spiDetachBus((void *)(spi->num+1));
log_e("Failed to set pin bus to SPI for pin %d", miso);
return false;
......@@ -254,14 +254,14 @@ bool spiAttachMOSI(spi_t * spi, int8_t mosi)
if(!spi || mosi < 0) {
return false;
}
void * bus = perimanGetPinBus(mosi, ESP32_BUS_TYPE_SPI_MASTER);
if(bus != NULL && !perimanSetPinBus(mosi, ESP32_BUS_TYPE_INIT, NULL)){
void * bus = perimanGetPinBus(mosi, ESP32_BUS_TYPE_SPI_MASTER_MOSI);
if(bus != NULL && !perimanClearPinBus(mosi)){
return false;
}
pinMode(mosi, OUTPUT);
pinMatrixOutAttach(mosi, SPI_MOSI_IDX(spi->num), false, false);
spi->mosi = mosi;
if(!perimanSetPinBus(mosi, ESP32_BUS_TYPE_SPI_MASTER, (void *)(spi->num+1))){
if(!perimanSetPinBus(mosi, ESP32_BUS_TYPE_SPI_MASTER_MOSI, (void *)(spi->num+1), spi->num, -1)){
spiDetachBus((void *)(spi->num+1));
log_e("Failed to set pin bus to SPI for pin %d", mosi);
return false;
......@@ -276,7 +276,7 @@ bool spiDetachSCK(spi_t * spi, int8_t sck)
}
pinMatrixOutDetach(sck, false, false);
spi->sck = -1;
perimanSetPinBus(sck, ESP32_BUS_TYPE_INIT, NULL);
perimanClearPinBus(sck);
return true;
}
......@@ -287,7 +287,7 @@ bool spiDetachMISO(spi_t * spi, int8_t miso)
}
pinMatrixInDetach(SPI_MISO_IDX(spi->num), false, false);
spi->miso = -1;
perimanSetPinBus(miso, ESP32_BUS_TYPE_INIT, NULL);
perimanClearPinBus(miso);
return true;
}
......@@ -298,7 +298,7 @@ bool spiDetachMOSI(spi_t * spi, int8_t mosi)
}
pinMatrixOutDetach(mosi, false, false);
spi->mosi = -1;
perimanSetPinBus(mosi, ESP32_BUS_TYPE_INIT, NULL);
perimanClearPinBus(mosi);
return true;
}
......@@ -307,15 +307,15 @@ bool spiAttachSS(spi_t * spi, uint8_t cs_num, int8_t ss)
if(!spi || ss < 0 || cs_num > 2) {
return false;
}
void * bus = perimanGetPinBus(ss, ESP32_BUS_TYPE_SPI_MASTER);
if(bus != NULL && !perimanSetPinBus(ss, ESP32_BUS_TYPE_INIT, NULL)){
void * bus = perimanGetPinBus(ss, ESP32_BUS_TYPE_SPI_MASTER_CS);
if(bus != NULL && !perimanClearPinBus(ss)){
return false;
}
pinMode(ss, OUTPUT);
pinMatrixOutAttach(ss, SPI_SS_IDX(spi->num, cs_num), false, false);
spiEnableSSPins(spi, (1 << cs_num));
spi->ss = ss;
if(!perimanSetPinBus(ss, ESP32_BUS_TYPE_SPI_MASTER, (void *)(spi->num+1))){
if(!perimanSetPinBus(ss, ESP32_BUS_TYPE_SPI_MASTER_CS, (void *)(spi->num+1), spi->num, -1)){
spiDetachBus((void *)(spi->num+1));
log_e("Failed to set pin bus to SPI for pin %d", ss);
return false;
......@@ -330,7 +330,7 @@ bool spiDetachSS(spi_t * spi, int8_t ss)
}
pinMatrixOutDetach(ss, false, false);
spi->ss = -1;
perimanSetPinBus(ss, ESP32_BUS_TYPE_INIT, NULL);
perimanClearPinBus(ss);
return true;
}
......@@ -578,7 +578,11 @@ spi_t * spiStartBus(uint8_t spi_num, uint32_t clockDiv, uint8_t dataMode, uint8_
return NULL;
}
perimanSetBusDeinit(ESP32_BUS_TYPE_SPI_MASTER, spiDetachBus);
perimanSetBusDeinit(ESP32_BUS_TYPE_SPI_MASTER_SCK, spiDetachBus);
perimanSetBusDeinit(ESP32_BUS_TYPE_SPI_MASTER_MISO, spiDetachBus);
perimanSetBusDeinit(ESP32_BUS_TYPE_SPI_MASTER_MOSI, spiDetachBus);
perimanSetBusDeinit(ESP32_BUS_TYPE_SPI_MASTER_CS, spiDetachBus);
spi_t * spi = &_spi_bus_array[spi_num];
#if !CONFIG_DISABLE_HAL_LOCKS
......
......@@ -88,10 +88,10 @@ static void configure_pins(usb_hal_context_t *usb)
if (!usb->use_external_phy) {
gpio_set_drive_capability(USBPHY_DM_NUM, GPIO_DRIVE_CAP_3);
gpio_set_drive_capability(USBPHY_DP_NUM, GPIO_DRIVE_CAP_3);
if (perimanSetBusDeinit(ESP32_BUS_TYPE_USB, usb_otg_deinit)) {
if (perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DM, usb_otg_deinit) && perimanSetBusDeinit(ESP32_BUS_TYPE_USB_DP, usb_otg_deinit)){
// Bus Pointer is not used anyway - once the USB GPIOs are assigned, they can't be detached
perimanSetPinBus(USBPHY_DM_NUM, ESP32_BUS_TYPE_USB, (void *) usb);
perimanSetPinBus(USBPHY_DP_NUM, ESP32_BUS_TYPE_USB, (void *) usb);
perimanSetPinBus(USBPHY_DM_NUM, ESP32_BUS_TYPE_USB_DM, (void *) usb, -1, -1);
perimanSetPinBus(USBPHY_DP_NUM, ESP32_BUS_TYPE_USB_DP, (void *) usb, -1, -1);
} else {
log_e("USB OTG Pins can't be set into Peripheral Manager.");
}
......
......@@ -215,13 +215,13 @@ static touch_value_t __touchRead(uint8_t pin)
if(perimanGetPinBus(pin, ESP32_BUS_TYPE_TOUCH) == NULL){
perimanSetBusDeinit(ESP32_BUS_TYPE_TOUCH, touchDetachBus);
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_INIT, NULL)){
if(!perimanClearPinBus(pin)){
return 0;
}
__touchInit();
__touchChannelInit(pad);
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_TOUCH, (void *)(pin+1))){
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_TOUCH, (void *)(pin+1), -1, pad)){
touchDetachBus((void *)(pin+1));
return 0;
}
......@@ -312,7 +312,7 @@ void touchSleepWakeUpEnable(uint8_t pin, touch_value_t threshold)
perimanSetBusDeinit(ESP32_BUS_TYPE_TOUCH, touchDetachBus);
__touchInit();
__touchChannelInit(pad);
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_TOUCH, (void *)(pin+1))){
if(!perimanSetPinBus(pin, ESP32_BUS_TYPE_TOUCH, (void *)(pin+1), -1, pad)){
log_e("Failed to set bus to Peripheral manager");
touchDetachBus((void *)(pin+1));
return;
......
......@@ -99,7 +99,7 @@ static bool _uartDetachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[rxPin], PIN_FUNC_GPIO);
esp_rom_gpio_connect_in_signal(GPIO_FUNC_IN_LOW, UART_PERIPH_SIGNAL(uart_num, SOC_UART_RX_PIN_IDX), false);
uart->_rxPin = -1; // -1 means unassigned/detached
if (!perimanSetPinBus(rxPin, ESP32_BUS_TYPE_INIT, NULL)) {
if (!perimanClearPinBus(rxPin)) {
retCode = false;
log_e("UART%d failed to detach RX pin %d", uart_num, rxPin);
}
......@@ -108,7 +108,7 @@ static bool _uartDetachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[txPin], PIN_FUNC_GPIO);
esp_rom_gpio_connect_out_signal(txPin, SIG_GPIO_OUT_IDX, false, false);
uart->_txPin = -1; // -1 means unassigned/detached
if (!perimanSetPinBus(txPin, ESP32_BUS_TYPE_INIT, NULL)) {
if (!perimanClearPinBus(txPin)) {
retCode = false;
log_e("UART%d failed to detach TX pin %d", uart_num, txPin);
}
......@@ -117,7 +117,7 @@ static bool _uartDetachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[ctsPin], PIN_FUNC_GPIO);
esp_rom_gpio_connect_in_signal(GPIO_FUNC_IN_LOW, UART_PERIPH_SIGNAL(uart_num, SOC_UART_CTS_PIN_IDX), false);
uart->_ctsPin = -1; // -1 means unassigned/detached
if (!perimanSetPinBus(ctsPin, ESP32_BUS_TYPE_INIT, NULL)) {
if (!perimanClearPinBus(ctsPin)) {
retCode = false;
log_e("UART%d failed to detach CTS pin %d", uart_num, ctsPin);
}
......@@ -126,7 +126,7 @@ static bool _uartDetachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[rtsPin], PIN_FUNC_GPIO);
esp_rom_gpio_connect_out_signal(rtsPin, SIG_GPIO_OUT_IDX, false, false);
uart->_rtsPin = -1; // -1 means unassigned/detached
if (!perimanSetPinBus(rtsPin, ESP32_BUS_TYPE_INIT, NULL)) {
if (!perimanClearPinBus(rtsPin)) {
retCode = false;
log_e("UART%d failed to detach RTS pin %d", uart_num, rtsPin);
}
......@@ -185,11 +185,11 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
bool retCode = true;
if (rxPin >= 0) {
// forces a clean detaching from a previous peripheral
if (perimanGetPinBusType(rxPin) != ESP32_BUS_TYPE_INIT) perimanSetPinBus(rxPin, ESP32_BUS_TYPE_INIT, NULL);
if (perimanGetPinBusType(rxPin) != ESP32_BUS_TYPE_INIT) perimanClearPinBus(rxPin);
// connect RX Pad
bool ret = ESP_OK == uart_set_pin(uart->num, UART_PIN_NO_CHANGE, rxPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
if (ret) {
ret &= perimanSetPinBus(rxPin, ESP32_BUS_TYPE_UART_RX, (void *)uart);
ret &= perimanSetPinBus(rxPin, ESP32_BUS_TYPE_UART_RX, (void *)uart, uart_num, -1);
if (ret) uart->_rxPin = rxPin;
}
if (!ret) {
......@@ -199,11 +199,11 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
}
if (txPin >= 0) {
// forces a clean detaching from a previous peripheral
if (perimanGetPinBusType(txPin) != ESP32_BUS_TYPE_INIT) perimanSetPinBus(txPin, ESP32_BUS_TYPE_INIT, NULL);
if (perimanGetPinBusType(txPin) != ESP32_BUS_TYPE_INIT) perimanClearPinBus(txPin);
// connect TX Pad
bool ret = ESP_OK == uart_set_pin(uart->num, txPin, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE);
if (ret) {
ret &= perimanSetPinBus(txPin, ESP32_BUS_TYPE_UART_TX, (void *)uart);
ret &= perimanSetPinBus(txPin, ESP32_BUS_TYPE_UART_TX, (void *)uart, uart_num, -1);
if (ret) uart->_txPin = txPin;
}
if (!ret) {
......@@ -213,11 +213,11 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
}
if (ctsPin >= 0) {
// forces a clean detaching from a previous peripheral
if (perimanGetPinBusType(ctsPin) != ESP32_BUS_TYPE_INIT) perimanSetPinBus(ctsPin, ESP32_BUS_TYPE_INIT, NULL);
if (perimanGetPinBusType(ctsPin) != ESP32_BUS_TYPE_INIT) perimanClearPinBus(ctsPin);
// connect CTS Pad
bool ret = ESP_OK == uart_set_pin(uart->num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, ctsPin);
if (ret) {
ret &= perimanSetPinBus(ctsPin, ESP32_BUS_TYPE_UART_CTS, (void *)uart);
ret &= perimanSetPinBus(ctsPin, ESP32_BUS_TYPE_UART_CTS, (void *)uart, uart_num, -1);
if (ret) uart->_ctsPin = ctsPin;
}
if (!ret) {
......@@ -227,11 +227,11 @@ static bool _uartAttachPins(uint8_t uart_num, int8_t rxPin, int8_t txPin, int8_t
}
if (rtsPin >= 0) {
// forces a clean detaching from a previous peripheral
if (perimanGetPinBusType(rtsPin) != ESP32_BUS_TYPE_INIT) perimanSetPinBus(rtsPin, ESP32_BUS_TYPE_INIT, NULL);
if (perimanGetPinBusType(rtsPin) != ESP32_BUS_TYPE_INIT) perimanClearPinBus(rtsPin);
// connect RTS Pad
bool ret = ESP_OK == uart_set_pin(uart->num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, rtsPin, UART_PIN_NO_CHANGE);
if (ret) {
ret &= perimanSetPinBus(rtsPin, ESP32_BUS_TYPE_UART_RTS, (void *)uart);
ret &= perimanSetPinBus(rtsPin, ESP32_BUS_TYPE_UART_RTS, (void *)uart, uart_num, -1);
if (ret) uart->_rtsPin = rtsPin;
}
if (!ret) {
......
#define ATTR_PACKED __attribute__((__packed__))
#define ATTR_ALIGNED(x) __attribute__((__aligned__(x)))
#define ATTR_SECTION(x) __attribute__((__section__(x)))
This diff is collapsed.
This diff is collapsed.
......@@ -13,6 +13,8 @@
// limitations under the License.
#include "sd_diskio.h"
#include "esp_system.h"
#include "esp32-hal-periman.h"
extern "C" {
#include "ff.h"
#include "diskio.h"
......@@ -748,6 +750,7 @@ uint8_t sdcard_init(uint8_t cs, SPIClass * spi, int hz)
pinMode(card->ssPin, OUTPUT);
digitalWrite(card->ssPin, HIGH);
perimanSetPinBusExtraType(card->ssPin, "SD_SS");
s_cards[pdrv] = card;
......
......@@ -107,8 +107,14 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount
if(_card) {
return true;
}
perimanSetBusDeinit(ESP32_BUS_TYPE_SDMMC, SDMMCFS::sdmmcDetachBus);
perimanSetBusDeinit(ESP32_BUS_TYPE_SDMMC_CLK, SDMMCFS::sdmmcDetachBus);
perimanSetBusDeinit(ESP32_BUS_TYPE_SDMMC_CMD, SDMMCFS::sdmmcDetachBus);
perimanSetBusDeinit(ESP32_BUS_TYPE_SDMMC_D0, SDMMCFS::sdmmcDetachBus);
if(!mode1bit) {
perimanSetBusDeinit(ESP32_BUS_TYPE_SDMMC_D1, SDMMCFS::sdmmcDetachBus);
perimanSetBusDeinit(ESP32_BUS_TYPE_SDMMC_D2, SDMMCFS::sdmmcDetachBus);
perimanSetBusDeinit(ESP32_BUS_TYPE_SDMMC_D3, SDMMCFS::sdmmcDetachBus);
}
//mount
sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
#ifdef SOC_SDMMC_USE_GPIO_MATRIX
......@@ -129,13 +135,13 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount
slot_config.width = 4;
#endif // SOC_SDMMC_USE_GPIO_MATRIX
if(!perimanSetPinBus(_pin_cmd, ESP32_BUS_TYPE_INIT, NULL)){ return false; }
if(!perimanSetPinBus(_pin_clk, ESP32_BUS_TYPE_INIT, NULL)){ return false; }
if(!perimanSetPinBus(_pin_d0, ESP32_BUS_TYPE_INIT, NULL)){ return false; }
if(!perimanClearPinBus(_pin_cmd)){ return false; }
if(!perimanClearPinBus(_pin_clk)){ return false; }
if(!perimanClearPinBus(_pin_d0)){ return false; }
if(!mode1bit) {
if(!perimanSetPinBus(_pin_d1, ESP32_BUS_TYPE_INIT, NULL)){ return false; }
if(!perimanSetPinBus(_pin_d2, ESP32_BUS_TYPE_INIT, NULL)){ return false; }
if(!perimanSetPinBus(_pin_d3, ESP32_BUS_TYPE_INIT, NULL)){ return false; }
if(!perimanClearPinBus(_pin_d1)){ return false; }
if(!perimanClearPinBus(_pin_d2)){ return false; }
if(!perimanClearPinBus(_pin_d3)){ return false; }
}
sdmmc_host_t host = SDMMC_HOST_DEFAULT();
......@@ -174,13 +180,13 @@ bool SDMMCFS::begin(const char * mountpoint, bool mode1bit, bool format_if_mount
}
_impl->mountpoint(mountpoint);
if(!perimanSetPinBus(_pin_cmd, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; }
if(!perimanSetPinBus(_pin_clk, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; }
if(!perimanSetPinBus(_pin_d0, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; }
if(!perimanSetPinBus(_pin_cmd, ESP32_BUS_TYPE_SDMMC_CMD, (void *)(this), -1, -1)){ goto err; }
if(!perimanSetPinBus(_pin_clk, ESP32_BUS_TYPE_SDMMC_CLK, (void *)(this), -1, -1)){ goto err; }
if(!perimanSetPinBus(_pin_d0, ESP32_BUS_TYPE_SDMMC_D0, (void *)(this), -1, -1)){ goto err; }
if(!mode1bit) {
if(!perimanSetPinBus(_pin_d1, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; }
if(!perimanSetPinBus(_pin_d2, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; }
if(!perimanSetPinBus(_pin_d3, ESP32_BUS_TYPE_SDMMC, (void *)(this))){ goto err; }
if(!perimanSetPinBus(_pin_d1, ESP32_BUS_TYPE_SDMMC_D1, (void *)(this), -1, -1)){ goto err; }
if(!perimanSetPinBus(_pin_d2, ESP32_BUS_TYPE_SDMMC_D2, (void *)(this), -1, -1)){ goto err; }
if(!perimanSetPinBus(_pin_d3, ESP32_BUS_TYPE_SDMMC_D3, (void *)(this), -1, -1)){ goto err; }
}
return true;
......@@ -196,13 +202,13 @@ void SDMMCFS::end()
esp_vfs_fat_sdcard_unmount(_impl->mountpoint(), _card);
_impl->mountpoint(NULL);
_card = NULL;
perimanSetPinBus(_pin_cmd, ESP32_BUS_TYPE_INIT, NULL);
perimanSetPinBus(_pin_clk, ESP32_BUS_TYPE_INIT, NULL);
perimanSetPinBus(_pin_d0, ESP32_BUS_TYPE_INIT, NULL);
perimanClearPinBus(_pin_cmd);
perimanClearPinBus(_pin_clk);
perimanClearPinBus(_pin_d0);
if(!_mode1bit) {
perimanSetPinBus(_pin_d1, ESP32_BUS_TYPE_INIT, NULL);
perimanSetPinBus(_pin_d2, ESP32_BUS_TYPE_INIT, NULL);
perimanSetPinBus(_pin_d3, ESP32_BUS_TYPE_INIT, NULL);
perimanClearPinBus(_pin_d1);
perimanClearPinBus(_pin_d2);
perimanClearPinBus(_pin_d3);
}
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment