Skip to content

Commit

Permalink
lm4f/uart: Implement uart_get_baudrate
Browse files Browse the repository at this point in the history
  • Loading branch information
ALTracer committed Aug 19, 2024
1 parent e8f0040 commit 9015d5a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/libopencm3/lm4f/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions lib/lm4f/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down

0 comments on commit 9015d5a

Please sign in to comment.