Skip to content

Commit

Permalink
cpu/esp32: UART configuration approach changed
Browse files Browse the repository at this point in the history
UART devices are now configured using static array in header files instead of static variables in implementation to be able to define UART_NUMOF using the size of the array instead of a variable.
  • Loading branch information
gschorcht committed May 2, 2019
1 parent 9eae68f commit ad4fbd0
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 50 deletions.
22 changes: 15 additions & 7 deletions cpu/esp32/include/periph_cpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -426,25 +426,33 @@ extern const unsigned spi_bus_num;
* configuration and is always available. All ESP32 boards use it as standard
* configuration for the console.
*
* UART_DEV(0).TXD GPIO1
* UART_DEV(0).RXD GPIO3
* UART_DEV(0).TXD GPIO1
* UART_DEV(0).RXD GPIO3
*
* The pin configuration of UART_DEV(1) and UART_DEV(2) are defined in
* board specific peripheral configuration by
*
* UARTn_TXD, the GPIO used as TxD signal, and
* UARTn_RXD, the GPIO used as RxD signal,
* - UARTn_TXD, the GPIO used as TxD signal, and
* - UARTn_RXD, the GPIO used as RxD signal,
*
* where n can be 2 or 3. If they are not defined, the UART interface
* where n can be 1 or 2. If they are not defined, the according UART interface
* UART_DEV(n) is not used.
*
* UART_NUMOF is determined automatically from the board-specific peripheral
* definitions of UARTn_TXD and UARTn_RXD.
* definitions of UARTn_*.
*
* @{
*/
/** @} */

/**
* @brief UART configuration structure type
*/
typedef struct {
gpio_t txd; /**< GPIO used as TxD pin */
gpio_t rxd; /**< GPIO used as RxD pin */
} uart_conf_t;

/** @} */

#ifdef __cplusplus
}
Expand Down
74 changes: 31 additions & 43 deletions cpu/esp32/periph/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ static struct uart_hw_t _uarts[] = {
.data = UART_DATA_BITS_8,
.stop = UART_STOP_BITS_1,
.parity = UART_PARITY_NONE,
.mod = PERIPH_UART0_MODULE,
.signal_txd = U0TXD_OUT_IDX,
.signal_rxd = U0RXD_IN_IDX,
.baudrate = STDIO_UART_BAUDRATE,
.used = false,
.int_src = ETS_UART0_INTR_SOURCE
},
#if defined(UART1_TXD) && defined(UART1_RXD)
Expand All @@ -87,8 +90,13 @@ static struct uart_hw_t _uarts[] = {
.data = UART_DATA_BITS_8,
.stop = UART_STOP_BITS_1,
.parity = UART_PARITY_NONE,
{
.regs = &UART1,
.mod = PERIPH_UART1_MODULE,
.signal_txd = U1TXD_OUT_IDX,
.signal_rxd = U1RXD_IN_IDX,
.baudrate = STDIO_UART_BAUDRATE,
.used = false,
.int_src = ETS_UART1_INTR_SOURCE
},
#endif
Expand All @@ -101,11 +109,15 @@ static struct uart_hw_t _uarts[] = {
.data = UART_DATA_BITS_8,
.stop = UART_STOP_BITS_1,
.parity = UART_PARITY_NONE,
{
.regs = &UART2,
.mod = PERIPH_UART2_MODULE,
.signal_txd = U2TXD_OUT_IDX,
.signal_rxd = U2RXD_IN_IDX,
.baudrate = STDIO_UART_BAUDRATE,
.used = false,
.int_src = ETS_UART2_INTR_SOURCE
}
#endif
};

/* declaration of external functions */
Expand All @@ -131,30 +143,29 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
if (uart == UART_DEV(1) || uart == UART_DEV(2)) {

/* reset the pins when they were already used as UART pins */
if (gpio_get_pin_usage(_uarts[uart].pin_txd) == _UART) {
gpio_set_pin_usage(_uarts[uart].pin_txd, _GPIO);
if (gpio_get_pin_usage(uart_config[uart].txd) == _UART) {
gpio_set_pin_usage(uart_config[uart].txd, _GPIO);
}
if (gpio_get_pin_usage(_uarts[uart].pin_rxd) == _UART) {
gpio_set_pin_usage(_uarts[uart].pin_rxd, _GPIO);
if (gpio_get_pin_usage(uart_config[uart].rxd) == _UART) {
gpio_set_pin_usage(uart_config[uart].rxd, _GPIO);
}

/* try to initialize the pins as GPIOs first */
if (gpio_init (_uarts[uart].pin_rxd, GPIO_IN) ||
gpio_init (_uarts[uart].pin_txd, GPIO_OUT)) {
if (gpio_init (uart_config[uart].rxd, GPIO_IN) ||
gpio_init (uart_config[uart].txd, GPIO_OUT)) {
return -1;
}

/* store the usage type in GPIO table */
gpio_set_pin_usage(_uarts[uart].pin_txd, _UART);
gpio_set_pin_usage(_uarts[uart].pin_rxd, _UART);
gpio_set_pin_usage(uart_config[uart].txd, _UART);
gpio_set_pin_usage(uart_config[uart].rxd, _UART);

/* connect TxD pin to the TxD output signal through the GPIO matrix */
GPIO.func_out_sel_cfg[_uarts[uart].pin_txd].func_sel = _uarts[uart].signal_txd;

GPIO.func_out_sel_cfg[uart_config[uart].txd].func_sel = _uarts[uart].signal_txd;
/* connect RxD input signal to the RxD pin through the GPIO matrix */
GPIO.func_in_sel_cfg[_uarts[uart].signal_rxd].sig_in_sel = 1;
GPIO.func_in_sel_cfg[_uarts[uart].signal_rxd].sig_in_inv = 0;
GPIO.func_in_sel_cfg[_uarts[uart].signal_rxd].func_sel = _uarts[uart].pin_rxd;
GPIO.func_in_sel_cfg[_uarts[uart].signal_rxd].func_sel = uart_config[uart].rxd;
}
_uarts[uart].baudrate = baudrate;

Expand Down Expand Up @@ -187,40 +198,17 @@ void uart_write(uart_t uart, const uint8_t *data, size_t len)

void uart_poweron (uart_t uart)
{
switch (uart) {
#if UART_NUMOF
case 0: periph_module_enable(PERIPH_UART0_MODULE);
_uart_config(uart);
break;
#endif
#if UART_NUMOF > 1
case 1: periph_module_enable(PERIPH_UART1_MODULE);
_uart_config(uart);
break;
#endif
#if UART_NUMOF > 2
case 2: periph_module_enable(PERIPH_UART2_MODULE);
_uart_config(uart);
break;
#endif
default: break;
}
CHECK_PARAM (uart < UART_NUMOF);

periph_module_enable(_uarts[uart].mod);
_uart_config(uart);
}

void uart_poweroff (uart_t uart)
{
switch (uart) {
#if UART_NUMOF
case 0: periph_module_disable(PERIPH_UART0_MODULE); break;
#endif
#if UART_NUMOF > 1
case 1: periph_module_disable(PERIPH_UART1_MODULE); break;
#endif
#if UART_NUMOF > 2
case 2: periph_module_disable(PERIPH_UART2_MODULE); break;
#endif
default: break;
}
CHECK_PARAM (uart < UART_NUMOF);

periph_module_disable(_uarts[uart].mod);
}

/* systemwide UART initializations */
Expand All @@ -236,7 +224,7 @@ void uart_print_config(void)
{
for (unsigned uart = 0; uart < UART_NUMOF; uart++) {
ets_printf("\tUART_DEV(%d)\ttxd=%d rxd=%d\n", uart,
_uarts[uart].pin_txd, _uarts[uart].pin_rxd);
uart_config[uart].txd, uart_config[uart].rxd);
}
}

Expand Down

0 comments on commit ad4fbd0

Please sign in to comment.