5050
5151struct 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 {
6766static 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
188181void 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
210189void 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