diff --git a/boards/nucleo-l1/include/periph_conf.h b/boards/nucleo-l1/include/periph_conf.h index fa0b85f63b5fcf243edaca30f189605ac52c7bd7..aa18c55360f3a4a3ae294edcc0b32716faf4ff68 100644 --- a/boards/nucleo-l1/include/periph_conf.h +++ b/boards/nucleo-l1/include/periph_conf.h @@ -72,11 +72,9 @@ static const timer_conf_t timer_config[] = { #define UART_0_ISR isr_usart2 #define UART_0_BUS_FREQ 32000000 /* UART 0 pin configuration */ -#define UART_0_PORT GPIOA -#define UART_0_PORT_CLKEN() (RCC->AHBENR |= RCC_AHBENR_GPIOAEN) -#define UART_0_RX_PIN 3 -#define UART_0_TX_PIN 2 -#define UART_0_AF 7 +#define UART_0_RX_PIN GPIO(PORT_A, 3) +#define UART_0_TX_PIN GPIO(PORT_A, 2) +#define UART_0_AF GPIO_AF7 /** * @brief GPIO configuration diff --git a/cpu/stm32l1/include/periph_cpu.h b/cpu/stm32l1/include/periph_cpu.h index 133f7164609a430e1f6084adbe8aa8a1d8ca895c..a5507a76e1ddc4f89f9491d61b5bbf0d106332be 100644 --- a/cpu/stm32l1/include/periph_cpu.h +++ b/cpu/stm32l1/include/periph_cpu.h @@ -80,6 +80,16 @@ typedef enum { GPIO_AF14 /**< use alternate function 14 */ } gpio_af_t; +/** + * @brief Configure the alternate function for the given pin + * + * @note This is meant for internal use in STM32L1 peripheral drivers only + * + * @param[in] pin pin to configure + * @param[in] af alternate function to use + */ +void gpio_init_af(gpio_t pin, gpio_af_t af); + /** * @brief Timer configuration data structure */ diff --git a/cpu/stm32l1/periph/uart.c b/cpu/stm32l1/periph/uart.c index 6c5ed4c0669fb05340638ed2b123ee9137276bea..a73c6253d5c25b77feadaf739b0b01c3e8f92419 100644 --- a/cpu/stm32l1/periph/uart.c +++ b/cpu/stm32l1/periph/uart.c @@ -23,6 +23,7 @@ #include "thread.h" #include "periph_conf.h" #include "periph/uart.h" +#include "periph/gpio.h" /* guard file in case no UART device was specified */ #if UART_NUMOF @@ -94,11 +95,10 @@ int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, uart_tx_cb_t t int uart_init_blocking(uart_t uart, uint32_t baudrate) { USART_TypeDef *dev = 0; - GPIO_TypeDef *port = 0; - uint32_t tx_pin = 0; - uint32_t rx_pin = 0; - uint8_t af = 0; - uint32_t clk = 0; + gpio_t tx_pin = 0; + gpio_t rx_pin = 0; + gpio_af_t af = 0; + float clk = 0; uint16_t mantissa; uint8_t fraction; @@ -106,37 +106,31 @@ int uart_init_blocking(uart_t uart, uint32_t baudrate) #if UART_0_EN case UART_0: dev = UART_0_DEV; - port = UART_0_PORT; clk = UART_0_CLK; tx_pin = UART_0_TX_PIN; rx_pin = UART_0_RX_PIN; af = UART_0_AF; UART_0_CLKEN(); - UART_0_PORT_CLKEN(); break; #endif #if UART_1_EN case UART_1: dev = UART_1_DEV; - port = UART_1_PORT; clk = UART_1_CLK; tx_pin = UART_1_TX_PIN; rx_pin = UART_1_RX_PIN; af = UART_1_AF; UART_1_CLKEN(); - UART_1_PORT_CLKEN(); break; #endif #if UART_2_EN case UART_2: dev = UART_2_DEV; - port = UART_2_PORT; clk = UART_2_CLK; tx_pin = UART_2_TX_PIN; rx_pin = UART_2_RX_PIN; af = UART_2_AF; UART_2_CLKEN(); - UART_2_PORT_CLKEN(); break; #endif default: @@ -144,25 +138,10 @@ int uart_init_blocking(uart_t uart, uint32_t baudrate) } /* uart_configure RX and TX pins, set pin to use alternative function mode */ - port->MODER &= ~(3 << (rx_pin * 2) | 3 << (tx_pin * 2)); - port->MODER |= 2 << (rx_pin * 2) | 2 << (tx_pin * 2); - /* and assign alternative function */ - if (rx_pin < 8) { - port->AFR[0] &= ~(0xf << (rx_pin * 4)); - port->AFR[0] |= af << (rx_pin * 4); - } - else { - port->AFR[1] &= ~(0xf << ((rx_pin - 8) * 4)); - port->AFR[1] |= af << ((rx_pin - 8) * 4); - } - if (tx_pin < 8) { - port->AFR[0] &= ~(0xf << (tx_pin * 4)); - port->AFR[0] |= af << (tx_pin * 4); - } - else { - port->AFR[1] &= ~(0xf << ((tx_pin - 8) * 4)); - port->AFR[1] |= af << ((tx_pin - 8) * 4); - } + gpio_init(tx_pin, GPIO_DIR_OUT, GPIO_NOPULL); + gpio_init_af(tx_pin, af); + gpio_init(rx_pin, GPIO_DIR_IN, GPIO_NOPULL); + gpio_init_af(rx_pin, af); /* uart_configure UART to mode 8N1 with given baudrate */ clk /= baudrate;