drivers: uart: esp32: use DEVICE_DT_INST_DEFINE()

Current uart driver implementation is incompleted regarding the
usage of DT_INST_FOREACH_STATUS_OKAY. If uart0 and uart2 are selected,
build breaks due to peripheral number ordering, which would be
0 and 1 in this case. This fix PR fix this by re-working the macros
and setting proper uart peripheral instances in DTSI, required for signal
routing configuration.

Signed-off-by: Sylvio Alves <sylvio.alves@espressif.com>
This commit is contained in:
Sylvio Alves 2022-01-07 16:50:56 -03:00 committed by Anas Nashif
commit c409a4656f
6 changed files with 31 additions and 18 deletions

View file

@ -52,7 +52,6 @@
struct uart_esp32_pin {
const char *gpio_name;
int signal;
int pin;
};
@ -66,6 +65,7 @@ struct uart_esp32_config {
const clock_control_subsys_t clock_subsys;
uint8_t uart_num;
int irq_source;
};
@ -217,7 +217,8 @@ static int uart_esp32_configure_pins(const struct device *dev, const struct uart
break;
gpio_pin_set(tx_dev, cfg->tx.pin, 1);
gpio_pin_configure(tx_dev, cfg->tx.pin, GPIO_OUTPUT);
esp_rom_gpio_matrix_out(cfg->tx.pin, cfg->tx.signal, 0, 0);
esp_rom_gpio_matrix_out(cfg->tx.pin,
uart_periph_signal[cfg->uart_num].tx_sig, 0, 0);
/* RX pin config */
const struct device *rx_dev = device_get_binding(cfg->rx.gpio_name);
@ -225,7 +226,7 @@ static int uart_esp32_configure_pins(const struct device *dev, const struct uart
if (!rx_dev)
break;
gpio_pin_configure(rx_dev, cfg->rx.pin, GPIO_PULL_UP | GPIO_INPUT);
esp_rom_gpio_matrix_in(cfg->rx.pin, cfg->rx.signal, 0);
esp_rom_gpio_matrix_in(cfg->rx.pin, uart_periph_signal[cfg->uart_num].rx_sig, 0);
if (uart->flow_ctrl == UART_CFG_FLOW_CTRL_RTS_CTS) {
if (cfg->rts.gpio_name == NULL || cfg->cts.gpio_name == NULL)
@ -237,7 +238,8 @@ static int uart_esp32_configure_pins(const struct device *dev, const struct uart
if (!cts_dev)
break;
gpio_pin_configure(cts_dev, cfg->cts.pin, GPIO_PULL_UP | GPIO_INPUT);
esp_rom_gpio_matrix_in(cfg->cts.pin, cfg->cts.signal, 0);
esp_rom_gpio_matrix_in(cfg->cts.pin,
uart_periph_signal[cfg->uart_num].cts_sig, 0);
/* RTS pin config */
const struct device *rts_dev = device_get_binding(cfg->rts.gpio_name);
@ -245,7 +247,8 @@ static int uart_esp32_configure_pins(const struct device *dev, const struct uart
if (!rts_dev)
break;
gpio_pin_configure(rts_dev, cfg->rts.pin, GPIO_OUTPUT);
esp_rom_gpio_matrix_out(cfg->rts.pin, cfg->rts.signal, 0, 0);
esp_rom_gpio_matrix_out(cfg->rts.pin,
uart_periph_signal[cfg->uart_num].rts_sig, 0, 0);
}
return 0;
@ -537,30 +540,27 @@ static const DRAM_ATTR struct uart_driver_api uart_esp32_api = {
#define ESP32_UART_INIT(idx) \
static const DRAM_ATTR struct uart_esp32_config uart_esp32_cfg_port_##idx = { \
.clock_dev = DEVICE_DT_GET(DT_CLOCKS_CTLR(DT_NODELABEL(uart##idx))), \
.uart_num = DT_INST_PROP(idx, peripheral), \
.clock_dev = DEVICE_DT_GET(DT_INST_CLOCKS_CTLR(idx)), \
.tx = { \
.signal = U##idx##TXD_OUT_IDX, \
.pin = DT_INST_PROP(idx, tx_pin), \
.gpio_name = DT_UART_ESP32_GPIO_NAME(idx, tx_pin), \
}, \
.rx = { \
.signal = U##idx##RXD_IN_IDX, \
.pin = DT_INST_PROP(idx, rx_pin), \
.gpio_name = DT_UART_ESP32_GPIO_NAME(idx, rx_pin), \
}, \
IF_ENABLED(DT_PROP(DT_NODELABEL(uart##idx), hw_flow_control), ( \
IF_ENABLED(DT_NODE_HAS_PROP(idx, hw_flow_control), ( \
.rts = { \
.signal = U##idx##RTS_OUT_IDX, \
.pin = DT_INST_PROP(idx, rts_pin), \
.gpio_name = DT_UART_ESP32_GPIO_NAME(idx, rts_pin), \
}, \
.cts = { \
.signal = U##idx##CTS_IN_IDX, \
.pin = DT_INST_PROP(idx, cts_pin), \
.gpio_name = DT_UART_ESP32_GPIO_NAME(idx, cts_pin), \
},)) \
.clock_subsys = (clock_control_subsys_t)DT_CLOCKS_CELL(DT_NODELABEL(uart##idx), offset), \
.irq_source = DT_IRQN(DT_NODELABEL(uart##idx)) \
.clock_subsys = (clock_control_subsys_t)DT_INST_CLOCKS_CELL(idx, offset), \
.irq_source = DT_INST_IRQN(idx) \
}; \
\
static struct uart_esp32_data uart_esp32_data_##idx = { \
@ -569,9 +569,9 @@ static struct uart_esp32_data uart_esp32_data_##idx = { \
.parity = UART_CFG_PARITY_NONE, \
.stop_bits = UART_CFG_STOP_BITS_1, \
.data_bits = UART_CFG_DATA_BITS_8, \
.flow_ctrl = IS_ENABLED( \
DT_PROP(DT_NODELABEL(uart##idx), hw_flow_control)) ?\
UART_CFG_FLOW_CTRL_RTS_CTS : UART_CFG_FLOW_CTRL_NONE \
.flow_ctrl = \
COND_CODE_1(DT_NODE_HAS_PROP(idx, hw_flow_control), \
(UART_CFG_FLOW_CTRL_RTS_CTS), (UART_CFG_FLOW_CTRL_NONE)) \
}, \
.hal = { \
.dev = \
@ -579,7 +579,7 @@ static struct uart_esp32_data uart_esp32_data_##idx = { \
}, \
}; \
\
DEVICE_DT_DEFINE(DT_NODELABEL(uart##idx), \
DEVICE_DT_INST_DEFINE(idx, \
&uart_esp32_init, \
NULL, \
&uart_esp32_data_##idx, \