uart: improve setup, blocking transmission, and flushing

This commit is contained in:
King Kévin 2020-06-09 00:59:19 +02:00
parent 5ab9402644
commit 28604a43fd
1 changed files with 3 additions and 2 deletions

View File

@ -52,6 +52,7 @@ void uart_setup(void)
/* enable UART I/O peripheral */
rcc_periph_clock_enable(RCC_USART_PORT(UART_ID)); // enable clock for UART port peripheral
rcc_periph_clock_enable(RCC_USART(UART_ID)); // enable clock for UART peripheral
rcc_periph_reset_pulse(RST_USART(UART_ID)); // reset peripheral
rcc_periph_clock_enable(RCC_AFIO); // enable pin alternate function (UART)
gpio_set_mode(USART_TX_PORT(UART_ID), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, USART_TX_PIN(UART_ID)); // setup GPIO pin UART transmit
gpio_set_mode(USART_RX_PORT(UART_ID), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, USART_RX_PIN(UART_ID)); // setup GPIO pin UART receive
@ -76,8 +77,7 @@ void uart_setup(void)
void uart_putchar_blocking(char c)
{
uart_flush(); // empty buffer first
usart_send_blocking(USART(UART_ID), c); // send character
usart_send_blocking(USART(UART_ID), c); // send character (this already wait for the buffer to be empty)
}
void uart_flush(void)
@ -86,6 +86,7 @@ void uart_flush(void)
__WFI(); // sleep until interrupt
}
usart_wait_send_ready(USART(UART_ID)); // wait until transmit register is empty (transmission might not be complete)
while(!usart_get_flag(USART(UART_ID), USART_SR_TC)); // wait for transmission to be complete
}
void uart_putchar_nonblocking(char c)