Skip to content

Commit 795ad18

Browse files
authored
Merge pull request #11294 from gschorcht/cpu/esp32/periph/conf/uart
boards/esp32: changes the approach for configurations of UART interfaces in board definitions
2 parents 11fc3b9 + 3cb08e9 commit 795ad18

3 files changed

Lines changed: 70 additions & 63 deletions

File tree

boards/common/esp32/include/periph_conf_common.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,35 @@ static const gpio_t dac_channels[] = DAC_GPIOS;
149149
* @name UART configuration
150150
*/
151151

152+
#ifndef UART0_TXD
153+
#define UART0_TXD (GPIO1) /**< TxD of UART_DEV(0) used on all ESP32 boards */
154+
#endif
155+
#ifndef UART0_RXD
156+
#define UART0_RXD (GPIO3) /**< RxD of UART_DEV(0) used on all ESP32 boards */
157+
#endif
158+
159+
/**
160+
* @brief Static array with configuration for declared I2C devices
161+
*/
162+
static const uart_conf_t uart_config[] = {
163+
{
164+
.txd = UART0_TXD,
165+
.rxd = UART0_RXD,
166+
},
167+
#if defined(UART1_TXD) && defined(UART1_RXD)
168+
{
169+
.txd = UART1_TXD,
170+
.rxd = UART1_RXD,
171+
},
172+
#endif
173+
#if defined(UART2_TXD) && defined(UART2_RXD)
174+
{
175+
.txd = UART2_TXD,
176+
.rxd = UART2_RXD,
177+
},
178+
#endif
179+
};
180+
152181
/**
153182
* @brief Number of UART interfaces
154183
*

cpu/esp32/include/periph_cpu.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -420,25 +420,33 @@ extern const unsigned spi_bus_num;
420420
* configuration and is always available. All ESP32 boards use it as standard
421421
* configuration for the console.
422422
*
423-
* UART_DEV(0).TXD GPIO1
424-
* UART_DEV(0).RXD GPIO3
423+
* UART_DEV(0).TXD GPIO1
424+
* UART_DEV(0).RXD GPIO3
425425
*
426426
* The pin configuration of UART_DEV(1) and UART_DEV(2) are defined in
427427
* board specific peripheral configuration by
428428
*
429-
* UARTn_TXD, the GPIO used as TxD signal, and
430-
* UARTn_RXD, the GPIO used as RxD signal,
429+
* - UARTn_TXD, the GPIO used as TxD signal, and
430+
* - UARTn_RXD, the GPIO used as RxD signal,
431431
*
432-
* where n can be 2 or 3. If they are not defined, the UART interface
432+
* where n can be 1 or 2. If they are not defined, the according UART interface
433433
* UART_DEV(n) is not used.
434434
*
435435
* UART_NUMOF is determined automatically from the board-specific peripheral
436-
* definitions of UARTn_TXD and UARTn_RXD.
436+
* definitions of UARTn_*.
437437
*
438438
* @{
439439
*/
440-
/** @} */
441440

441+
/**
442+
* @brief UART configuration structure type
443+
*/
444+
typedef struct {
445+
gpio_t txd; /**< GPIO used as TxD pin */
446+
gpio_t rxd; /**< GPIO used as RxD pin */
447+
} uart_conf_t;
448+
449+
/** @} */
442450

443451
#ifdef __cplusplus
444452
}

cpu/esp32/periph/uart.c

Lines changed: 26 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@
5050

5151
struct uart_hw_t {
5252
uart_dev_t* regs; /* pointer to register data struct of the UART device */
53-
uint8_t pin_txd; /* TxD pin */
54-
uint8_t pin_rxd; /* RxD pin */
53+
uint8_t mod; /* peripheral hardware module of the UART interface */
5554
bool used; /* indicates whether UART is used */
5655
uint32_t baudrate; /* used baudrate */
5756
uart_data_bits_t data; /* used data bits */
@@ -67,8 +66,7 @@ struct uart_hw_t {
6766
static struct uart_hw_t _uarts[] = {
6867
{
6968
.regs = &UART0,
70-
.pin_txd = GPIO1,
71-
.pin_rxd = GPIO3,
69+
.mod = PERIPH_UART0_MODULE,
7270
.used = false,
7371
.baudrate = STDIO_UART_BAUDRATE,
7472
.data = UART_DATA_BITS_8,
@@ -78,10 +76,9 @@ static struct uart_hw_t _uarts[] = {
7876
.signal_rxd = U0RXD_IN_IDX,
7977
.int_src = ETS_UART0_INTR_SOURCE
8078
},
81-
#if defined(UART1_TXD) && defined(UART1_RXD)
82-
{ .regs = &UART1,
83-
.pin_txd = UART1_TXD,
84-
.pin_rxd = UART1_RXD,
79+
{
80+
.regs = &UART1,
81+
.mod = PERIPH_UART1_MODULE,
8582
.used = false,
8683
.baudrate = STDIO_UART_BAUDRATE,
8784
.data = UART_DATA_BITS_8,
@@ -91,11 +88,9 @@ static struct uart_hw_t _uarts[] = {
9188
.signal_rxd = U1RXD_IN_IDX,
9289
.int_src = ETS_UART1_INTR_SOURCE
9390
},
94-
#endif
95-
#if defined(UART2_TXD) && defined(UART2_RXD)
96-
{ .regs = &UART2,
97-
.pin_txd = UART2_TXD,
98-
.pin_rxd = UART2_RXD,
91+
{
92+
.regs = &UART2,
93+
.mod = PERIPH_UART2_MODULE,
9994
.used = false,
10095
.baudrate = STDIO_UART_BAUDRATE,
10196
.data = UART_DATA_BITS_8,
@@ -105,7 +100,6 @@ static struct uart_hw_t _uarts[] = {
105100
.signal_rxd = U2RXD_IN_IDX,
106101
.int_src = ETS_UART2_INTR_SOURCE
107102
}
108-
#endif
109103
};
110104

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

133127
/* reset the pins when they were already used as UART pins */
134-
if (gpio_get_pin_usage(_uarts[uart].pin_txd) == _UART) {
135-
gpio_set_pin_usage(_uarts[uart].pin_txd, _GPIO);
128+
if (gpio_get_pin_usage(uart_config[uart].txd) == _UART) {
129+
gpio_set_pin_usage(uart_config[uart].txd, _GPIO);
136130
}
137-
if (gpio_get_pin_usage(_uarts[uart].pin_rxd) == _UART) {
138-
gpio_set_pin_usage(_uarts[uart].pin_rxd, _GPIO);
131+
if (gpio_get_pin_usage(uart_config[uart].rxd) == _UART) {
132+
gpio_set_pin_usage(uart_config[uart].rxd, _GPIO);
139133
}
140134

141135
/* try to initialize the pins as GPIOs first */
142-
if (gpio_init (_uarts[uart].pin_rxd, GPIO_IN) ||
143-
gpio_init (_uarts[uart].pin_txd, GPIO_OUT)) {
136+
if (gpio_init (uart_config[uart].rxd, GPIO_IN) ||
137+
gpio_init (uart_config[uart].txd, GPIO_OUT)) {
144138
return -1;
145139
}
146140

147141
/* store the usage type in GPIO table */
148-
gpio_set_pin_usage(_uarts[uart].pin_txd, _UART);
149-
gpio_set_pin_usage(_uarts[uart].pin_rxd, _UART);
142+
gpio_set_pin_usage(uart_config[uart].txd, _UART);
143+
gpio_set_pin_usage(uart_config[uart].rxd, _UART);
150144

151145
/* connect TxD pin to the TxD output signal through the GPIO matrix */
152-
GPIO.func_out_sel_cfg[_uarts[uart].pin_txd].func_sel = _uarts[uart].signal_txd;
153-
146+
GPIO.func_out_sel_cfg[uart_config[uart].txd].func_sel = _uarts[uart].signal_txd;
154147
/* connect RxD input signal to the RxD pin through the GPIO matrix */
155148
GPIO.func_in_sel_cfg[_uarts[uart].signal_rxd].sig_in_sel = 1;
156149
GPIO.func_in_sel_cfg[_uarts[uart].signal_rxd].sig_in_inv = 0;
157-
GPIO.func_in_sel_cfg[_uarts[uart].signal_rxd].func_sel = _uarts[uart].pin_rxd;
150+
GPIO.func_in_sel_cfg[_uarts[uart].signal_rxd].func_sel = uart_config[uart].rxd;
158151
}
159152
_uarts[uart].baudrate = baudrate;
160153

@@ -187,40 +180,17 @@ void uart_write(uart_t uart, const uint8_t *data, size_t len)
187180

188181
void uart_poweron (uart_t uart)
189182
{
190-
switch (uart) {
191-
#if UART_NUMOF
192-
case 0: periph_module_enable(PERIPH_UART0_MODULE);
193-
_uart_config(uart);
194-
break;
195-
#endif
196-
#if UART_NUMOF > 1
197-
case 1: periph_module_enable(PERIPH_UART1_MODULE);
198-
_uart_config(uart);
199-
break;
200-
#endif
201-
#if UART_NUMOF > 2
202-
case 2: periph_module_enable(PERIPH_UART2_MODULE);
203-
_uart_config(uart);
204-
break;
205-
#endif
206-
default: break;
207-
}
183+
CHECK_PARAM (uart < UART_NUMOF);
184+
185+
periph_module_enable(_uarts[uart].mod);
186+
_uart_config(uart);
208187
}
209188

210189
void uart_poweroff (uart_t uart)
211190
{
212-
switch (uart) {
213-
#if UART_NUMOF
214-
case 0: periph_module_disable(PERIPH_UART0_MODULE); break;
215-
#endif
216-
#if UART_NUMOF > 1
217-
case 1: periph_module_disable(PERIPH_UART1_MODULE); break;
218-
#endif
219-
#if UART_NUMOF > 2
220-
case 2: periph_module_disable(PERIPH_UART2_MODULE); break;
221-
#endif
222-
default: break;
223-
}
191+
CHECK_PARAM (uart < UART_NUMOF);
192+
193+
periph_module_disable(_uarts[uart].mod);
224194
}
225195

226196
/* systemwide UART initializations */
@@ -236,7 +206,7 @@ void uart_print_config(void)
236206
{
237207
for (unsigned uart = 0; uart < UART_NUMOF; uart++) {
238208
ets_printf("\tUART_DEV(%d)\ttxd=%d rxd=%d\n", uart,
239-
_uarts[uart].pin_txd, _uarts[uart].pin_rxd);
209+
uart_config[uart].txd, uart_config[uart].rxd);
240210
}
241211
}
242212

0 commit comments

Comments
 (0)