aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--projects/board-test/uart-test.c48
-rw-r--r--stm-uart.c70
-rw-r--r--stm-uart.h10
3 files changed, 87 insertions, 41 deletions
diff --git a/projects/board-test/uart-test.c b/projects/board-test/uart-test.c
index 6f022f4..eb6108a 100644
--- a/projects/board-test/uart-test.c
+++ b/projects/board-test/uart-test.c
@@ -16,36 +16,34 @@
int
main()
{
- uint8_t tx = 'A';
- uint8_t rx = 0;
- uint8_t upper = 0;
+ uint8_t crlf[] = "\r\n";
+ uint8_t tx = 'A';
+ uint8_t rx = 0;
+ uint8_t upper = 0;
- stm_init();
+ stm_init();
- while (1)
- {
- led_toggle(LED_GREEN);
+ while (1) {
+ led_toggle(LED_GREEN);
- uart_send_char2(STM_UART_USER, tx + upper);
- uart_send_char2(STM_UART_MGMT, tx + upper);
- DELAY();
+ uart_send_char2(STM_UART_USER, tx + upper);
+ uart_send_char2(STM_UART_MGMT, tx + upper);
+ DELAY();
- if (uart_recv_char2(STM_UART_USER, &rx, 0) == HAL_OK ||
- uart_recv_char2(STM_UART_MGMT, &rx, 0) == HAL_OK) {
- led_toggle(LED_YELLOW);
- if (rx == '\r') {
- upper = upper == 0 ? ('a' - 'A'):0;
+ if (uart_recv_char2(STM_UART_USER, &rx, 0) == HAL_OK ||
+ uart_recv_char2(STM_UART_MGMT, &rx, 0) == HAL_OK) {
+ led_toggle(LED_YELLOW);
+ if (rx == '\r') {
+ upper = upper == 0 ? ('a' - 'A'):0;
+ }
}
- }
- if (tx++ == 'Z') {
- /* linefeed after each alphabet */
- uart_send_char2(STM_UART_USER, '\r');
- uart_send_char2(STM_UART_USER, '\n');
- uart_send_char2(STM_UART_MGMT, '\r');
- uart_send_char2(STM_UART_MGMT, '\n');
- tx = 'A';
- led_toggle(LED_BLUE);
+ if (tx++ == 'Z') {
+ /* linefeed after each alphabet */
+ uart_send_string2(STM_UART_USER, crlf);
+ uart_send_string2(STM_UART_MGMT, crlf);
+ tx = 'A';
+ led_toggle(LED_BLUE);
+ }
}
- }
}
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);
}
diff --git a/stm-uart.h b/stm-uart.h
index 1155179..e934068 100644
--- a/stm-uart.h
+++ b/stm-uart.h
@@ -51,11 +51,17 @@ extern UART_HandleTypeDef huart2;
extern HAL_StatusTypeDef uart_send_char(uint8_t ch);
extern HAL_StatusTypeDef uart_recv_char(uint8_t *cp);
+extern HAL_StatusTypeDef uart_send_string(char *s);
+extern HAL_StatusTypeDef uart_send_number(uint32_t num, uint8_t digits, uint8_t radix);
+
extern HAL_StatusTypeDef uart_send_char2(enum stm_uart_port port, uint8_t ch);
extern HAL_StatusTypeDef uart_recv_char2(enum stm_uart_port port, uint8_t *cp, uint32_t timeout);
-extern HAL_StatusTypeDef uart_send_string(char *s);
-extern HAL_StatusTypeDef uart_send_number(uint32_t num, uint8_t digits, uint8_t radix);
+extern HAL_StatusTypeDef uart_send_string2(enum stm_uart_port port, const char *s);
+extern HAL_StatusTypeDef uart_send_number2(enum stm_uart_port port, uint32_t num, uint8_t digits, uint8_t radix);
+
+extern HAL_StatusTypeDef uart_send_bytes(enum stm_uart_port port, uint8_t *buf, size_t len);
+
#define uart_send_binary(num, bits) uart_send_number(num, bits, 2)
#define uart_send_integer(num, digits) uart_send_number(num, digits, 10)
#define uart_send_hex(num, digits) uart_send_number(num, digits, 16)