diff options
author | Fredrik Thulin <fredrik@thulin.net> | 2016-06-01 21:03:05 +0200 |
---|---|---|
committer | Fredrik Thulin <fredrik@thulin.net> | 2016-06-01 21:03:05 +0200 |
commit | 9915d1ba46e30990ea149c7a09d1d2ed0d13a331 (patch) | |
tree | 160f184c1dfc8548de8c6293a5d16376c65ab6ec /stm-uart.c | |
parent | 2d08b2038a5cf14a92d8ca3e10fb3421e73e5b74 (diff) |
Implement circular buffer UART RX using interrupts.
Diffstat (limited to 'stm-uart.c')
-rw-r--r-- | stm-uart.c | 24 |
1 files changed, 9 insertions, 15 deletions
@@ -62,12 +62,7 @@ HAL_StatusTypeDef uart_send_char(uint8_t ch) HAL_StatusTypeDef uart_send_char2(enum stm_uart_port port, uint8_t ch) { - UART_HandleTypeDef *uart = _which_uart(port); - - if (uart) - return HAL_UART_Transmit(uart, &ch, 1, 0x1); - - return HAL_ERROR; + return uart_send_bytes(port, &ch, 1); } /* receive a single character */ @@ -96,21 +91,21 @@ HAL_StatusTypeDef uart_send_string(char *s) /* send a string */ HAL_StatusTypeDef uart_send_string2(enum stm_uart_port port, const char *s) { - UART_HandleTypeDef *uart = _which_uart(port); - - if (uart) - return HAL_UART_Transmit(uart, (uint8_t *) s, strlen(s), 0x1); - - return HAL_ERROR; + return uart_send_bytes(port, (uint8_t *) s, strlen(s)); } /* send raw bytes */ HAL_StatusTypeDef uart_send_bytes(enum stm_uart_port port, uint8_t *buf, size_t len) { + uint32_t timeout = 100; UART_HandleTypeDef *uart = _which_uart(port); - if (uart) + 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); + } return HAL_ERROR; } @@ -139,7 +134,6 @@ HAL_StatusTypeDef uart_send_number2(enum stm_uart_port port, uint32_t num, uint8 #define BUFSIZE 32 char buf[BUFSIZE]; char *where = buf + BUFSIZE; - UART_HandleTypeDef *uart = _which_uart(port); /* initialize buf so we can add leading 0 by adjusting the pointer */ memset(buf, '0', BUFSIZE); @@ -163,7 +157,7 @@ HAL_StatusTypeDef uart_send_number2(enum stm_uart_port port, uint32_t num, uint8 /* number is larger than the specified number of digits */ digits = buf + BUFSIZE - where; - return HAL_UART_Transmit(uart, (uint8_t *) where, digits, 0x1); + return uart_send_bytes(port, (uint8_t *) where, digits); } HAL_StatusTypeDef uart_send_hexdump(enum stm_uart_port port, const uint8_t *buf, |