[Cryptech-Commits] [sw/stm32] 02/12: Test both UARTs, and also test receiving data.

git at cryptech.is git at cryptech.is
Mon May 16 09:43:15 UTC 2016


This is an automated email from the git hooks/post-receive script.

fredrik at thulin.net pushed a commit to branch ft-alpha_board_bringup
in repository sw/stm32.

commit 8a492acf7b856f0ab13a531a77ba9e64097ba007
Author: Fredrik Thulin <fredrik at thulin.net>
AuthorDate: Thu May 12 19:46:38 2016 +0200

    Test both UARTs, and also test receiving data.
---
 projects/board-test/uart-test.c | 33 +++++++++++++++++++++++++--------
 stm-init.c                      | 19 +++++++++++++++++++
 stm-uart.c                      | 22 ++++++++++++++++++++++
 stm-uart.h                      | 11 +++++++++++
 4 files changed, 77 insertions(+), 8 deletions(-)

diff --git a/projects/board-test/uart-test.c b/projects/board-test/uart-test.c
index f32fde7..6f022f4 100644
--- a/projects/board-test/uart-test.c
+++ b/projects/board-test/uart-test.c
@@ -1,8 +1,9 @@
 /*
  * Test code that just sends the letters 'a' to 'z' over and
- * over again using USART2.
+ * over again to both the USER and MGMT UARTs. If a CR is received,
+ * it will toggle upper/lower case of the letters being sent.
  *
- * Toggles the BLUE LED slowly and the RED LED for every
+ * Toggles the BLUE LED slowly and the GREEN LED for every
  * character sent.
  */
 #include "stm32f4xx_hal.h"
@@ -15,20 +16,36 @@
 int
 main()
 {
-  uint8_t c = 'a';
+  uint8_t tx = 'A';
+  uint8_t rx = 0;
+  uint8_t upper = 0;
 
   stm_init();
 
   while (1)
   {
-    HAL_GPIO_TogglePin(LED_PORT, LED_GREEN);
+    led_toggle(LED_GREEN);
 
-    uart_send_char(c);
+    uart_send_char2(STM_UART_USER, tx + upper);
+    uart_send_char2(STM_UART_MGMT, tx + upper);
     DELAY();
 
-    if (c++ == 'z') {
-      c = 'a';
-      HAL_GPIO_TogglePin(LED_PORT, LED_BLUE);
+    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);
     }
   }
 }
diff --git a/stm-init.c b/stm-init.c
index f6b3aa1..051a755 100644
--- a/stm-init.c
+++ b/stm-init.c
@@ -52,6 +52,7 @@
 static void MX_GPIO_Init(void);
 #endif
 #ifdef HAL_UART_MODULE_ENABLED
+static void MX_USART1_UART_Init(void);
 static void MX_USART2_UART_Init(void);
 #endif
 
@@ -70,11 +71,29 @@ void stm_init(void)
   MX_GPIO_Init();
 #endif
 #ifdef HAL_UART_MODULE_ENABLED
+  MX_USART1_UART_Init();
   MX_USART2_UART_Init();
 #endif
 }
 
 #ifdef HAL_UART_MODULE_ENABLED
+/* USART1 init function */
+static void MX_USART1_UART_Init(void)
+{
+  huart1.Instance = USART1;
+  huart1.Init.BaudRate = USART1_BAUD_RATE;
+  huart1.Init.WordLength = UART_WORDLENGTH_8B;
+  huart1.Init.StopBits = UART_STOPBITS_1;
+  huart1.Init.Parity = UART_PARITY_NONE;
+  huart1.Init.Mode = UART_MODE_TX_RX;
+  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
+  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
+
+  if (HAL_UART_Init(&huart1) != HAL_OK) {
+    /* Initialization Error */
+    Error_Handler();
+  }
+}
 /* USART2 init function */
 static void MX_USART2_UART_Init(void)
 {
diff --git a/stm-uart.c b/stm-uart.c
index 0ecbedc..ed872cf 100644
--- a/stm-uart.c
+++ b/stm-uart.c
@@ -37,6 +37,7 @@
 
 #include <string.h>
 
+UART_HandleTypeDef huart1;
 UART_HandleTypeDef huart2;
 
 /* send a single character */
@@ -45,12 +46,33 @@ HAL_StatusTypeDef uart_send_char(uint8_t ch)
     return HAL_UART_Transmit(&huart2, &ch, 1, 0x1);
 }
 
+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);
+    }
+    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);
 }
 
+/* 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);
+    }
+    return HAL_ERROR;
+}
+
 /* send a string */
 HAL_StatusTypeDef uart_send_string(char *s)
 {
diff --git a/stm-uart.h b/stm-uart.h
index caf87b3..1155179 100644
--- a/stm-uart.h
+++ b/stm-uart.h
@@ -37,12 +37,23 @@
 
 #include "stm32f4xx_hal.h"
 
+#define USART1_BAUD_RATE	115200
 #define USART2_BAUD_RATE	115200
 
+enum stm_uart_port {
+  STM_UART_USER,
+  STM_UART_MGMT
+};
+
+extern UART_HandleTypeDef huart1;
 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_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);
 #define uart_send_binary(num, bits)    uart_send_number(num, bits, 2)



More information about the Commits mailing list