diff --git a/include/libopencm3/lm4f/uart.h b/include/libopencm3/lm4f/uart.h index 920e26be..26f43400 100644 --- a/include/libopencm3/lm4f/uart.h +++ b/include/libopencm3/lm4f/uart.h @@ -443,6 +443,7 @@ enum uart_fifo_tx_trigger_level { BEGIN_DECLS void uart_set_baudrate(uint32_t uart, uint32_t baud); +uint32_t uart_get_baudrate(uint32_t uart); void uart_set_databits(uint32_t uart, uint8_t databits); uint8_t uart_get_databits(uint32_t uart); void uart_set_stopbits(uint32_t uart, uint8_t stopbits); diff --git a/lib/lm4f/uart.c b/lib/lm4f/uart.c index 2835dc7a..eb9fa4fc 100644 --- a/lib/lm4f/uart.c +++ b/lib/lm4f/uart.c @@ -130,6 +130,25 @@ void uart_set_baudrate(uint32_t uart, uint32_t baud) UART_FBRD(uart) = div % 64; } +/** + * \brief Get UART baudrate + * + * @param[in] uart UART block register address base @ref uart_reg_base + * @return Baud rate in bits per second (bps) + */ +uint32_t uart_get_baudrate(uint32_t uart) +{ + /* Are we running off the internal clock or system clock? */ + const uint32_t clock = UART_CC(uart) == UART_CC_CS_PIOSC ? 16000000U : rcc_get_system_clock_frequency(); + + /* Read back divisor parts. Baudrate = clock/16 / (ibrd+fbrd/64). */ + const uint16_t ibrd = UART_IBRD(uart); + const uint16_t fbrd = UART_FBRD(uart); + uint32_t div = ibrd * 64U + fbrd; + /* Recalculate the actual baudrate. Note that 4 comes from 1/(16/64). */ + return 4U * clock / div; +} + /** * \brief Set UART databits *