Skip to content

Commit

Permalink
stm32_common/periph/uart: move uart_set_baudrate to periph_cpu_common
Browse files Browse the repository at this point in the history
  • Loading branch information
fjmolinas committed Jan 23, 2018
1 parent 1a2b0e5 commit b04e11a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 24 deletions.
17 changes: 17 additions & 0 deletions cpu/stm32_common/include/periph_cpu_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,23 @@ uint32_t periph_apb_clk(uint8_t bus);
*/
uint32_t periph_timer_clk(uint8_t bus);

/**
* @brief Recalculate Baudrate for UART device
*
* The UART device will be initialized with the following configuration:
* - 8 data bits
* - no parity
* - 1 stop bit
* - baudrate as given
*
* @param[in] uart UART device to initialize
* @param[in] baudrate desired baudrate in baud/s
*
* @return UART_OK on success
* @return UART_NOBAUD on inapplicable baudrate
*/
int uart_set_baudrate(unsigned int uart, uint32_t baudrate);

/**
* @brief Enable the given peripheral clock
*
Expand Down
16 changes: 9 additions & 7 deletions cpu/stm32_common/periph/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
#include "periph/uart.h"
#include "periph/gpio.h"

#define RXENABLE (USART_CR1_RE | USART_CR1_RXNEIE)
#define RXENABLE (USART_CR1_RE | USART_CR1_RXNEIE)

#define UART_8X_OVERSAMPLING 8
#define UART_16X_OVERSAMPLING 16
/**
* @brief Allocate memory to store the callback functions
*/
Expand Down Expand Up @@ -63,18 +65,18 @@ int uart_set_baudrate(uart_t uart, uint32_t baudrate) {
#ifdef USART_CR1_OVER8
if (uart_clk < 16) {
dev(uart)->CR1 |= USART_CR1_OVER8;
mantissa = (uint16_t)(uart_clk / 8);
fraction = (uint8_t)(uart_clk - (mantissa * 8));
mantissa = (uint16_t)(uart_clk / UART_8X_OVERSAMPLING);
fraction = (uint8_t)(uart_clk - (mantissa * UART_8X_OVERSAMPLING));
dev(uart)->BRR = ((mantissa & 0x0fff) << 4) | (fraction & 0x07);
} else {
dev(uart)->CR1 &= ~USART_CR1_OVER8;
mantissa = (uint16_t)(uart_clk / 16);
fraction = (uint8_t)(uart_clk - (mantissa * 16));
mantissa = (uint16_t)(uart_clk / UART_16X_OVERSAMPLING);
fraction = (uint8_t)(uart_clk - (mantissa * UART_16X_OVERSAMPLING));
dev(uart)->BRR = ((mantissa & 0x0fff) << 4) | (fraction & 0x0f);
}
#else
mantissa = (uint16_t)(uart_clk / 16);
fraction = (uint8_t)(uart_clk - (mantissa * 16));
mantissa = (uint16_t)(uart_clk / UART_16X_OVERSAMPLING);
fraction = (uint8_t)(uart_clk - (mantissa * UART_16X_OVERSAMPLING));
dev(uart)->BRR = ((mantissa & 0x0fff) << 4) | (fraction & 0x0f);
#endif

Expand Down
17 changes: 0 additions & 17 deletions drivers/include/periph/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,23 +134,6 @@ enum {
*/
int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg);

/**
* @brief Recalculate Baudrate for UART device
*
* The UART device will be initialized with the following configuration:
* - 8 data bits
* - no parity
* - 1 stop bit
* - baudrate as given
*
* @param[in] uart UART device to initialize
* @param[in] baudrate desired baudrate in baud/s
*
* @return UART_OK on success
* @return UART_NOBAUD on inapplicable baudrate
*/
int uart_set_baudrate(uart_t uart, uint32_t baudrate);

/**
* @brief Write data from the given buffer to the specified UART device
*
Expand Down

0 comments on commit b04e11a

Please sign in to comment.