aboutsummaryrefslogtreecommitdiff
path: root/stm-uart.c
diff options
context:
space:
mode:
Diffstat (limited to 'stm-uart.c')
-rw-r--r--stm-uart.c70
1 files changed, 56 insertions, 14 deletions
diff --git a/stm-uart.c b/stm-uart.c
index ed872cf..538fe6c 100644
--- a/stm-uart.c
+++ b/stm-uart.c
@@ -40,43 +40,79 @@
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
+#define DEFAULT_UART STM_UART_USER
+
+
+inline UART_HandleTypeDef *_which_uart(enum stm_uart_port port)
+{
+ if (port == STM_UART_USER) {
+ return &huart2;
+ } else if (port == STM_UART_MGMT) {
+ return &huart1;
+ }
+
+ return NULL;
+}
+
/* send a single character */
HAL_StatusTypeDef uart_send_char(uint8_t ch)
{
- return HAL_UART_Transmit(&huart2, &ch, 1, 0x1);
+ return uart_send_char2(DEFAULT_UART, ch);
}
HAL_StatusTypeDef uart_send_char2(enum stm_uart_port port, uint8_t ch)
{
- if (port == STM_UART_USER) {
- return HAL_UART_Transmit(&huart2, &ch, 1, 0x1);
- } else if (port == STM_UART_MGMT) {
- return HAL_UART_Transmit(&huart1, &ch, 1, 0x1);
- }
+ UART_HandleTypeDef *uart = _which_uart(port);
+
+ if (uart)
+ return HAL_UART_Transmit(uart, &ch, 1, 0x1);
+
return HAL_ERROR;
}
/* receive a single character */
HAL_StatusTypeDef uart_recv_char(uint8_t *cp)
{
- return HAL_UART_Receive(&huart2, cp, 1, HAL_MAX_DELAY);
+ return uart_recv_char2(DEFAULT_UART, cp, HAL_MAX_DELAY);
}
/* receive a single character */
HAL_StatusTypeDef uart_recv_char2(enum stm_uart_port port, uint8_t *cp, uint32_t timeout)
{
- if (port == STM_UART_USER) {
- return HAL_UART_Receive(&huart2, cp, 1, timeout);
- } else if (port == STM_UART_MGMT) {
- return HAL_UART_Receive(&huart1, cp, 1, 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 HAL_UART_Transmit(&huart2, (uint8_t *) s, strlen(s), 0x1);
+ return uart_send_string2(DEFAULT_UART, 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;
+}
+
+/* send raw bytes */
+HAL_StatusTypeDef uart_send_bytes(enum stm_uart_port port, uint8_t *buf, size_t len)
+{
+ UART_HandleTypeDef *uart = _which_uart(port);
+
+ if (uart)
+ return HAL_UART_Transmit(uart, (uint8_t *) buf, (uint32_t) len, 0x1);
+
+ return HAL_ERROR;
}
/* Generalized routine to send binary, decimal, and hex integers.
@@ -84,9 +120,15 @@ HAL_StatusTypeDef uart_send_string(char *s)
*/
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(enum stm_uart_port port, uint32_t num, uint8_t digits, uint8_t radix)
+{
#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);
@@ -110,5 +152,5 @@ HAL_StatusTypeDef uart_send_number(uint32_t num, uint8_t digits, uint8_t radix)
/* number is larger than the specified number of digits */
digits = buf + BUFSIZE - where;
- return HAL_UART_Transmit(&huart2, (uint8_t *) where, digits, 0x1);
+ return HAL_UART_Transmit(uart, (uint8_t *) where, digits, 0x1);
}