[Cryptech-Commits] [user/ft/stm32-dev-bridge] 02/03: Implement UART outputing of results

git at cryptech.is git at cryptech.is
Thu Sep 10 08:52:44 UTC 2015


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

fredrik at thulin.net pushed a commit to branch master
in repository user/ft/stm32-dev-bridge.

commit b5f7b6b8ed23eccc15415776b6d2fa05e10b9c7d
Author: Fredrik Thulin <fredrik at thulin.net>
Date:   Thu Sep 10 10:47:08 2015 +0200

    Implement UART outputing of results
---
 src/fmc-test/Makefile                     |  2 +-
 src/fmc-test/include/stm-uart.h           | 16 ++++++
 src/fmc-test/include/stm32f4xx_hal_conf.h |  2 +-
 src/fmc-test/src/main.c                   | 87 +++++++++++++++++++++++--------
 src/fmc-test/src/stm-uart.c               | 83 +++++++++++++++++++++++++++++
 src/fmc-test/src/stm32f4xx_hal_msp.c      | 36 +++++++++++++
 6 files changed, 203 insertions(+), 23 deletions(-)

diff --git a/src/fmc-test/Makefile b/src/fmc-test/Makefile
index 0cd5524..1de0221 100644
--- a/src/fmc-test/Makefile
+++ b/src/fmc-test/Makefile
@@ -1,5 +1,5 @@
 # put your *.o targets here, make should handle the rest!
-SRCS = main.c stm32f4xx_hal_msp.c stm32f4xx_it.c stm-fmc.c
+SRCS = main.c stm32f4xx_hal_msp.c stm32f4xx_it.c stm-fmc.c stm-uart.c
 #main.c stm_init.c stm32f4xx_it.c stm32f4xx_hal_msp.c
 
 # all the files will be generated with this name
diff --git a/src/fmc-test/include/stm-uart.h b/src/fmc-test/include/stm-uart.h
new file mode 100644
index 0000000..c106d76
--- /dev/null
+++ b/src/fmc-test/include/stm-uart.h
@@ -0,0 +1,16 @@
+#ifndef __STM32_DEV_BRIDGE_UART_H
+#define __STM32_DEV_BRIDGE_UART_H
+
+#include "stm32f4xx_hal.h"
+
+#define USART2_BAUD_RATE	115200
+
+extern void MX_USART2_UART_Init(void);
+
+extern void uart_send_binary(uint32_t num, uint8_t bits);
+extern void uart_send_string(char *s);
+extern void uart_send_integer(uint32_t data, uint32_t mag);
+
+extern UART_HandleTypeDef huart2;
+
+#endif /* __STM32_DEV_BRIDGE_UART_H */
diff --git a/src/fmc-test/include/stm32f4xx_hal_conf.h b/src/fmc-test/include/stm32f4xx_hal_conf.h
index f1c9cb1..fd13d9e 100644
--- a/src/fmc-test/include/stm32f4xx_hal_conf.h
+++ b/src/fmc-test/include/stm32f4xx_hal_conf.h
@@ -73,7 +73,7 @@
 //#define HAL_SD_MODULE_ENABLED
 //#define HAL_SPI_MODULE_ENABLED
 //#define HAL_TIM_MODULE_ENABLED
-//#define HAL_UART_MODULE_ENABLED
+#define HAL_UART_MODULE_ENABLED
 //#define HAL_USART_MODULE_ENABLED
 //#define HAL_IRDA_MODULE_ENABLED
 //#define HAL_SMARTCARD_MODULE_ENABLED
diff --git a/src/fmc-test/src/main.c b/src/fmc-test/src/main.c
index 38165b8..7275aac 100644
--- a/src/fmc-test/src/main.c
+++ b/src/fmc-test/src/main.c
@@ -8,7 +8,7 @@
 //------------------------------------------------------------------------------
 #include "stm32f4xx_hal.h"
 #include "stm-fmc.h"
-
+#include "stm-uart.h"
 
 //------------------------------------------------------------------------------
 // Defines
@@ -70,6 +70,11 @@ int main(void)
   // initialize gpio
   MX_GPIO_Init();
 
+  // initialize UART for debug output
+  MX_USART2_UART_Init();
+
+  uart_send_string("Keep calm for Novena boot...\r\n");
+
   // Blink blue LED for six seconds to not upset the Novena at boot.
   led_on(GPIO_PIN_LED_BLUE);
   for (i = 0; i < 12; i++) {
@@ -90,7 +95,7 @@ int main(void)
   led_off(GPIO_PIN_LED_BLUE);
 
   // vars
-  volatile int data_test_ok = 0, addr_test_ok = 0, successful_runs = 0;
+  volatile int data_test_ok = 0, addr_test_ok = 0, successful_runs = 0, failed_runs = 0, sleep = 0;
 
   // main loop (test, until an error is detected)
   while (1)
@@ -100,25 +105,32 @@ int main(void)
       // test address bus
       addr_test_ok = test_fpga_address_bus();
 
-      // check for errors (abort testing in case of error)
-      if (data_test_ok < TEST_NUM_ROUNDS) break;
-
-      // check for errors (abort testing in case of error)
-      if (addr_test_ok < TEST_NUM_ROUNDS) break;
-
-      // toggle yellow led to indicate, that we are alive
-      led_toggle(GPIO_PIN_LED_YELLOW);
-      successful_runs++;
-    }
-
-  // error handler
-  while (1)
-    {
-      // turn on red led, turn off other leds
-      led_on(GPIO_PIN_LED_RED);
-      led_off(GPIO_PIN_LED_GREEN);
-      led_off(GPIO_PIN_LED_YELLOW);
-      led_off(GPIO_PIN_LED_BLUE);
+      uart_send_string("Data: ");
+      uart_send_integer(data_test_ok, 100000);
+      uart_send_string(", addr: ");
+      uart_send_integer(addr_test_ok, 100000);
+      uart_send_string("\r\n");
+
+      if (data_test_ok == TEST_NUM_ROUNDS &&
+	  addr_test_ok == TEST_NUM_ROUNDS) {
+	// toggle yellow led to indicate, that we are alive
+	led_toggle(GPIO_PIN_LED_YELLOW);
+
+	successful_runs++;
+	sleep = 100;
+      } else {
+	led_on(GPIO_PIN_LED_RED);
+	failed_runs++;
+	sleep = 2000;
+      }
+
+      uart_send_string("Success ");
+      uart_send_integer(successful_runs, 0);
+      uart_send_string(", fail ");
+      uart_send_integer(failed_runs, 0);
+      uart_send_string("\r\n\r\n");
+
+      HAL_Delay(sleep);
     }
 
   // should never reach this line
@@ -154,6 +166,15 @@ int test_fpga_data_bus(void)
 	{
 	  data_diff = buf;
 	  data_diff ^= rnd;
+
+	  uart_send_string("Data bus fail: expected ");
+	  uart_send_binary(rnd, 32);
+	  uart_send_string(", got ");
+	  uart_send_binary(buf, 32);
+	  uart_send_string(", diff ");
+	  uart_send_binary(data_diff, 32);
+	  uart_send_string("\r\n");
+
 	  break;
 	}
     }
@@ -202,6 +223,15 @@ int test_fpga_address_bus(void)
 	{
 	  addr_diff = buf;
 	  addr_diff ^= rnd;
+
+	  uart_send_string("Addr bus fail: expected ");
+	  uart_send_binary(rnd, 32);
+	  uart_send_string(", got ");
+	  uart_send_binary(buf, 32);
+	  uart_send_string(", diff ");
+	  uart_send_binary(addr_diff, 32);
+	  uart_send_string("\r\n");
+
 	  break;
 	}
     }
@@ -275,6 +305,21 @@ void MX_GPIO_Init(void)
 }
 
 
+
+/**
+ * @brief  This function is executed in case of error occurrence.
+ * @param  None
+ * @retval None
+ */
+void Error_Handler(void)
+{
+  volatile int i = 0;
+  led_on(GPIO_PIN_LED_RED);
+  while (1) {
+    i++; /* To not get optimized away/obscured */
+  }
+}
+
 //------------------------------------------------------------------------------
 #ifdef USE_FULL_ASSERT
 //------------------------------------------------------------------------------
diff --git a/src/fmc-test/src/stm-uart.c b/src/fmc-test/src/stm-uart.c
new file mode 100644
index 0000000..723845e
--- /dev/null
+++ b/src/fmc-test/src/stm-uart.c
@@ -0,0 +1,83 @@
+/* Includes ------------------------------------------------------------------*/
+#include "stm32f4xx_hal.h"
+#include "stm-uart.h"
+
+#include <string.h>
+
+UART_HandleTypeDef huart2;
+
+extern void Error_Handler();
+
+
+/* Private variables ---------------------------------------------------------*/
+
+/* Private function prototypes -----------------------------------------------*/
+
+/* USART2 init function */
+void MX_USART2_UART_Init(void)
+{
+  huart2.Instance = USART2;
+  huart2.Init.BaudRate = USART2_BAUD_RATE;
+  huart2.Init.WordLength = UART_WORDLENGTH_8B;
+  huart2.Init.StopBits = UART_STOPBITS_1;
+  huart2.Init.Parity = UART_PARITY_NONE;
+  huart2.Init.Mode = UART_MODE_TX_RX;
+  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
+  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
+
+  if (HAL_UART_Init(&huart2) != HAL_OK) {
+    /* Initialization Error */
+    Error_Handler();
+  }
+}
+
+void uart_send_binary(uint32_t num, uint8_t bits)
+{
+  uint32_t i;
+  unsigned char ch;
+
+  bits--;  /* bits 4 should give i = 1000, not 10000 */
+
+  i = 1 << bits;
+  while (i) {
+    ch = '0';
+    if (num & i) {
+      ch = '1';
+    }
+
+    HAL_UART_Transmit(&huart2, (uint8_t *) &ch, 1, 0x1);
+    i = i >> 1;
+  }
+}
+
+void uart_send_string(char *s)
+{
+  HAL_UART_Transmit(&huart2, (uint8_t *) s, strlen(s), 0x1);
+}
+
+void uart_send_integer(uint32_t data, uint32_t mag) {
+  uint32_t i, t;
+  unsigned char ch;
+
+  if (! mag) {
+    /* Find magnitude */
+    if (data < 10) {
+      ch = '0' + data;
+      HAL_UART_Transmit(&huart2, (uint8_t *) &ch, 1, 0x1);
+      return;
+    }
+
+    for (mag = 10; mag < data; mag = i) {
+      i = mag * 10;
+      if (i > data || i < mag)
+        break;
+    }
+  }
+  /* mag is now 10 if data is 45, and 1000 if data is 1009 */
+  for (i = mag; i; i /= 10) {
+    t = (data / i);
+    ch = '0' + t;
+    HAL_UART_Transmit(&huart2, (uint8_t *) &ch, 1, 0x1);
+    data -= (t * i);
+  }
+}
diff --git a/src/fmc-test/src/stm32f4xx_hal_msp.c b/src/fmc-test/src/stm32f4xx_hal_msp.c
index f932c45..ee2cb7e 100644
--- a/src/fmc-test/src/stm32f4xx_hal_msp.c
+++ b/src/fmc-test/src/stm32f4xx_hal_msp.c
@@ -103,5 +103,41 @@ void HAL_SRAM_MspDeInit(SRAM_HandleTypeDef* hsram)
 {
 }
 
+void HAL_UART_MspInit(UART_HandleTypeDef* huart)
+{
+  GPIO_InitTypeDef GPIO_InitStruct;
+
+  if (huart->Instance == USART2) {
+    /* Peripheral clock enable */
+    __USART2_CLK_ENABLE();
+    __GPIOA_CLK_ENABLE();
+
+    /**USART2 GPIO Configuration
+       PA2     ------> USART2_TX
+       PA3     ------> USART2_RX
+    */
+    GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
+    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+    GPIO_InitStruct.Pull = GPIO_PULLUP;
+    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
+    GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
+    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+  }
+
+}
+
+void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
+{
+  if (huart->Instance == USART2) {
+    /* Peripheral clock disable */
+    __USART2_CLK_DISABLE();
+
+    /**USART2 GPIO Configuration
+       PA2     ------> USART2_TX
+       PA3     ------> USART2_RX
+    */
+    HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3);
+  }
+}
 
 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/



More information about the Commits mailing list