From 410e6bb67fb5df5d0e4c962deac3e5562e5dc48f Mon Sep 17 00:00:00 2001 From: Paul Selkirk Date: Mon, 17 Apr 2017 16:12:25 -0400 Subject: Rewrite the wait-for-ready loop in uart_send_bytes() to actually work. --- stm-uart.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'stm-uart.c') diff --git a/stm-uart.c b/stm-uart.c index 05190fb..a670d76 100644 --- a/stm-uart.c +++ b/stm-uart.c @@ -105,14 +105,15 @@ HAL_StatusTypeDef uart_send_string2(stm_uart_port_t port, const char *s) /* send raw bytes */ HAL_StatusTypeDef uart_send_bytes(stm_uart_port_t port, uint8_t *buf, size_t len) { - uint32_t timeout = 100; UART_HandleTypeDef *uart = _which_uart(port); if (uart) { - while (HAL_UART_GetState(uart) != HAL_UART_STATE_READY && timeout--) { ; } - if (! timeout) return HAL_ERROR; - - return HAL_UART_Transmit(uart, (uint8_t *) buf, (uint32_t) len, 0x1); + for (int timeout = 0; timeout < 100; ++timeout) { + HAL_UART_StateTypeDef status = HAL_UART_GetState(uart); + if (status == HAL_UART_STATE_READY || + status == HAL_UART_STATE_BUSY_RX) + return HAL_UART_Transmit(uart, (uint8_t *) buf, (uint32_t) len, 0x1); + } } return HAL_ERROR; -- cgit v1.2.3