aboutsummaryrefslogtreecommitdiff
path: root/stm-uart.c
diff options
context:
space:
mode:
Diffstat (limited to 'stm-uart.c')
-rw-r--r--stm-uart.c105
1 files changed, 29 insertions, 76 deletions
diff --git a/stm-uart.c b/stm-uart.c
index 59089fe..f64c90c 100644
--- a/stm-uart.c
+++ b/stm-uart.c
@@ -43,7 +43,7 @@ UART_HandleTypeDef huart_user; /* USART2 */
DMA_HandleTypeDef hdma_usart_mgmt_rx;
DMA_HandleTypeDef hdma_usart_user_rx;
-static stm_uart_port_t default_uart = STM_UART_USER;
+UART_HandleTypeDef* default_uart = STM_UART_MGMT;
#ifdef HAL_DMA_MODULE_ENABLED
/**
@@ -118,100 +118,52 @@ void uart_init(void)
MX_USART2_UART_Init();
}
-void uart_set_default(stm_uart_port_t port)
+void uart_set_default(UART_HandleTypeDef *uart)
{
- if (port == STM_UART_USER || port == STM_UART_MGMT)
- default_uart = port;
-}
-
-static inline UART_HandleTypeDef *_which_uart(stm_uart_port_t port)
-{
- if (port == STM_UART_USER) {
- return &huart_user;
- } else if (port == STM_UART_MGMT) {
- return &huart_mgmt;
- }
-
- return NULL;
+ default_uart = uart;
}
/* send a single character */
-HAL_StatusTypeDef uart_send_char(uint8_t ch)
-{
- return uart_send_char2(default_uart, ch);
-}
-
-HAL_StatusTypeDef uart_send_char2(stm_uart_port_t port, uint8_t ch)
+HAL_StatusTypeDef uart_send_char2(UART_HandleTypeDef *uart, uint8_t ch)
{
- return uart_send_bytes(port, &ch, 1);
+ return uart_send_bytes2(uart, &ch, 1);
}
/* receive a single character */
-HAL_StatusTypeDef uart_recv_char(uint8_t *cp)
+HAL_StatusTypeDef uart_recv_char2(UART_HandleTypeDef *uart, uint8_t *cp, uint32_t timeout)
{
- return uart_recv_char2(default_uart, cp, HAL_MAX_DELAY);
-}
-
-/* receive a single character */
-HAL_StatusTypeDef uart_recv_char2(stm_uart_port_t port, uint8_t *cp, uint32_t timeout)
-{
- UART_HandleTypeDef *uart = _which_uart(port);
-
- if (uart)
- return HAL_UART_Receive(uart, cp, 1, timeout);
-
- return HAL_ERROR;
-}
-
-/* send a string */
-HAL_StatusTypeDef uart_send_string(char *s)
-{
- return uart_send_string2(default_uart, s);
+ return HAL_UART_Receive(uart, cp, 1, timeout);
}
/* send a string */
-HAL_StatusTypeDef uart_send_string2(stm_uart_port_t port, const char *s)
+HAL_StatusTypeDef uart_send_string2(UART_HandleTypeDef *uart, const char *s)
{
- return uart_send_bytes(port, (uint8_t *) s, strlen(s));
+ return uart_send_bytes2(uart, (uint8_t *) s, strlen(s));
}
/* send raw bytes */
-HAL_StatusTypeDef uart_send_bytes(stm_uart_port_t port, uint8_t *buf, size_t len)
+HAL_StatusTypeDef uart_send_bytes2(UART_HandleTypeDef *uart, uint8_t *buf, size_t len)
{
- UART_HandleTypeDef *uart = _which_uart(port);
-
- if (uart) {
- 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);
- }
+ 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;
+ return HAL_TIMEOUT;
}
/* receive raw bytes */
-HAL_StatusTypeDef uart_receive_bytes(stm_uart_port_t port, uint8_t *buf, size_t len, uint32_t timeout)
+HAL_StatusTypeDef uart_receive_bytes2(UART_HandleTypeDef *uart, uint8_t *buf, size_t len, uint32_t timeout)
{
- UART_HandleTypeDef *uart = _which_uart(port);
-
- if (uart)
- return HAL_UART_Receive(uart, (uint8_t *) buf, (uint32_t) len, timeout);
-
- return HAL_ERROR;
+ return HAL_UART_Receive(uart, (uint8_t *) buf, (uint32_t) len, timeout);
}
/* Generalized routine to send binary, decimal, and hex integers.
* This code is adapted from Chris Giese's printf.c
*/
-HAL_StatusTypeDef uart_send_number(uint32_t num, uint8_t digits, uint8_t radix)
-{
- return uart_send_number2(default_uart, num, digits, radix);
-}
-
-HAL_StatusTypeDef uart_send_number2(stm_uart_port_t port, uint32_t num, uint8_t digits, uint8_t radix)
+HAL_StatusTypeDef uart_send_number2(UART_HandleTypeDef *uart, uint32_t num, uint8_t digits, uint8_t radix)
{
#define BUFSIZE 32
char buf[BUFSIZE];
@@ -239,29 +191,30 @@ HAL_StatusTypeDef uart_send_number2(stm_uart_port_t port, uint32_t num, uint8_t
/* number is larger than the specified number of digits */
digits = buf + BUFSIZE - where;
- return uart_send_bytes(port, (uint8_t *) where, digits);
+ return uart_send_bytes2(uart, (uint8_t *) where, digits);
}
-HAL_StatusTypeDef uart_send_hexdump(stm_uart_port_t port, const uint8_t *buf,
- const uint8_t start_offset, const uint8_t end_offset)
+HAL_StatusTypeDef uart_send_hexdump2(UART_HandleTypeDef *uart, const uint8_t *buf,
+ const uint8_t start_offset, const uint8_t end_offset)
{
uint32_t i;
- uart_send_string2(port, "00 -- ");
+ uart_send_number2(uart, start_offset, 2, 16);
+ uart_send_string2(uart, " -- ");
for (i = start_offset; i <= end_offset; i++) {
if (i && (! (i % 16))) {
- uart_send_string2(port, "\r\n");
+ uart_send_string2(uart, "\r\n");
if (i != end_offset) {
/* Output new offset unless the last byte is reached */
- uart_send_number2(port, i, 2, 16);
- uart_send_string2(port, " -- ");
+ uart_send_number2(uart, i, 2, 16);
+ uart_send_string2(uart, " -- ");
}
}
- uart_send_number2(port, *(buf + i), 2, 16);
- uart_send_string2(port, " ");
+ uart_send_number2(uart, *(buf + i), 2, 16);
+ uart_send_string2(uart, " ");
}
return HAL_OK;