lm4f/uart: Implement uart_get_baudrate

This commit is contained in:
ALTracer
2024-08-17 12:22:37 +03:00
committed by Rachel Mant
parent 754dac7686
commit 9059ec1a42
2 changed files with 20 additions and 0 deletions

View File

@@ -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);

View File

@@ -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
*