diff options
author | Fredrik Thulin <fredrik@thulin.net> | 2015-06-28 16:30:08 +0200 |
---|---|---|
committer | Fredrik Thulin <fredrik@thulin.net> | 2015-06-28 16:30:08 +0200 |
commit | c4678339908e413cbc6751cf863267807acafc85 (patch) | |
tree | 3692460e9e5b390f542a53b768599f1a012267dc /src/entropy | |
parent | f6818cce54b99eecfc0d67437095d5adb955ee2c (diff) |
Enable dual USART output functionality.
In other words, enable the USART connected to the serial port on
the Raspberry Pi GPIO header.
Sending a newline to either USART directs the generated entropy
to that USART.
Diffstat (limited to 'src/entropy')
-rw-r--r-- | src/entropy/main.c | 40 | ||||
-rw-r--r-- | src/entropy/stm32f4xx_hal_msp.c | 18 | ||||
-rw-r--r-- | src/entropy/stm32f4xx_it.c | 16 | ||||
-rw-r--r-- | src/entropy/stm_init.c | 34 | ||||
-rw-r--r-- | src/entropy/stm_init.h | 1 |
5 files changed, 89 insertions, 20 deletions
diff --git a/src/entropy/main.c b/src/entropy/main.c index e29e00c..f7663f5 100644 --- a/src/entropy/main.c +++ b/src/entropy/main.c @@ -42,6 +42,7 @@ extern DMA_HandleTypeDef hdma_tim; +UART_HandleTypeDef *huart; __IO ITStatus UartReady = RESET; static union { @@ -79,6 +80,7 @@ inline uint32_t get_one_bit(void) __attribute__((__always_inline__)); volatile uint32_t *restart_DMA(void); inline volatile uint32_t *get_DMA_read_buf(void); inline uint32_t safe_get_counter(volatile uint32_t *dmabuf, const uint32_t dmabuf_idx); +void check_uart_rx(UART_HandleTypeDef *this); /* static void Error_Handler(void); */ @@ -86,7 +88,7 @@ inline uint32_t safe_get_counter(volatile uint32_t *dmabuf, const uint32_t dmabu int main() { - uint32_t count = 0, send_bytes, idx = 255, send_sync_bytes_in = 0, i; + uint32_t count = 0, send_bytes, idx = 255, send_sync_bytes_in = 0, i, timeout; uint32_t mode = MODE_ENTROPY; /* Initialize buffers */ @@ -101,6 +103,8 @@ main() restart_DMA(); restart_DMA(); + huart = &huart1; + /* Toggle GREEN LED to show we've initialized */ { for (i = 0; i < 10; i++) { @@ -144,7 +148,8 @@ main() } /* Send buf on UART (non blocking interrupt driven send). */ - if (HAL_UART_Transmit_IT(&huart1, (uint8_t *) buf.rnd + idx, (uint16_t) send_bytes) == HAL_OK) { + UartReady = RESET; + if (HAL_UART_Transmit_IT(huart, (uint8_t *) buf.rnd + idx, (uint16_t) send_bytes) == HAL_OK) { if (mode == MODE_ENTROPY) { /* Flip-flop idx between the value 0 and the value BYTES_PER_CHUNK. @@ -155,15 +160,21 @@ main() get_entropy32(send_bytes / 4, idx / 4); } - while (UartReady != SET) { ; } - UartReady = RESET; - } else { - /* Turn on RED LED for one second */ + timeout = 0xffff; + while (UartReady != SET && timeout) { timeout--; } + } + + if (UartReady != SET) { + /* Failed to send, turn on RED LED for one second */ HAL_GPIO_WritePin(LED_PORT, LED_RED, GPIO_PIN_SET); HAL_Delay(1000); HAL_GPIO_WritePin(LED_PORT, LED_RED, GPIO_PIN_RESET); } + /* Check for UART change request */ + check_uart_rx(&huart1); + check_uart_rx(&huart2); + count++; } @@ -347,12 +358,27 @@ inline uint32_t safe_get_counter(volatile uint32_t *dmabuf, const uint32_t dmabu /* UART transmit complete callback */ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UH) { - if (UH->Instance == USART1) { + if ((UH->Instance == USART1 && huart->Instance == USART1) || + (UH->Instance == USART2 && huart->Instance == USART2)) { /* Signal UART transmit complete to the code in the main loop. */ UartReady = SET; } } +/* + * If a newline is received on UART1 or UART2, redirect output to that UART. + */ +void check_uart_rx(UART_HandleTypeDef *this) { + uint8_t rx = 0; + if (HAL_UART_Receive(this, &rx, 1, 0) == HAL_OK) { + if (rx == '\n') { + huart = this; + /* Signal UART transmit complete to the code in the main loop. */ + UartReady = SET; + } + } +} + /** * @brief This function is executed in case of error occurrence. * @param None diff --git a/src/entropy/stm32f4xx_hal_msp.c b/src/entropy/stm32f4xx_hal_msp.c index fc27e04..536a78c 100644 --- a/src/entropy/stm32f4xx_hal_msp.c +++ b/src/entropy/stm32f4xx_hal_msp.c @@ -58,7 +58,7 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart) */ GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; - GPIO_InitStruct.Pull = GPIO_PULLUP; + GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; GPIO_InitStruct.Alternate = GPIO_AF7_USART1; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); @@ -66,6 +66,19 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart) /* NVIC for interrupt mode */ HAL_NVIC_SetPriority(USART1_IRQn, 0, 1); HAL_NVIC_EnableIRQ(USART1_IRQn); + } else if (huart->Instance == USART2) { + /* Peripheral clock enable */ + __USART2_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3; + GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; + GPIO_InitStruct.Pull = GPIO_NOPULL; + GPIO_InitStruct.Speed = GPIO_SPEED_FAST; + GPIO_InitStruct.Alternate = GPIO_AF7_USART2; + HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + /* NVIC for interrupt mode */ + HAL_NVIC_SetPriority(USART2_IRQn, 0, 1); + HAL_NVIC_EnableIRQ(USART2_IRQn); } } @@ -80,6 +93,9 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart) PA10 ------> USART1_RX */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10); + } else if (huart->Instance == USART2) { + __USART2_CLK_DISABLE(); + HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3); } } diff --git a/src/entropy/stm32f4xx_it.c b/src/entropy/stm32f4xx_it.c index 231b4fc..d60a229 100644 --- a/src/entropy/stm32f4xx_it.c +++ b/src/entropy/stm32f4xx_it.c @@ -41,8 +41,6 @@ #include "stm32f4xx_it.h" #include "stm_init.h" -extern TIM_HandleTypeDef htim2; - /** @addtogroup STM32F4xx_HAL_Examples * @{ */ @@ -171,8 +169,7 @@ void SysTick_Handler(void) * @brief This function handles UART interrupt request. * @param None * @retval None - * @Note This function is redefined in "main.h" and related to DMA stream - * used for USART data transmission + * @Note HAL_UART_IRQHandler will call HAL_UART_TxCpltCallback in main.c. */ void USART1_IRQHandler(void) { @@ -180,6 +177,17 @@ void USART1_IRQHandler(void) } /** + * @brief This function handles UART interrupt request. + * @param None + * @retval None + * @Note HAL_UART_IRQHandler will call HAL_UART_TxCpltCallback in main.c. + */ +void USART2_IRQHandler(void) +{ + HAL_UART_IRQHandler(&huart2); +} + +/** * @brief This function handles DMA1 Stream 6 interrupt request. * @param None * @retval None diff --git a/src/entropy/stm_init.c b/src/entropy/stm_init.c index 4bf35b5..5f69760 100644 --- a/src/entropy/stm_init.c +++ b/src/entropy/stm_init.c @@ -38,9 +38,6 @@ /* Includes ------------------------------------------------------------------*/ #include "stm_init.h" -//#include "stm32f4xx_hal.h" -//#include "stm32f4xx_hal_rcc.h" - /** @addtogroup STM32F4xx_HAL_Examples * @{ @@ -54,6 +51,7 @@ /* Private define ------------------------------------------------------------*/ #define UART1_BAUD_RATE 460800 +#define UART2_BAUD_RATE 115200 /* Private macro -------------------------------------------------------------*/ @@ -62,6 +60,7 @@ static GPIO_InitTypeDef GPIO_InitStruct; TIM_HandleTypeDef htim2; UART_HandleTypeDef huart1; +UART_HandleTypeDef huart2; DMA_HandleTypeDef hdma_tim; /* Private function prototypes -----------------------------------------------*/ @@ -70,6 +69,7 @@ static void Error_Handler(void); static void MX_GPIO_Init(void); static void MX_TIM2_Init(uint32_t *counters_buf, uint16_t counters); static void MX_USART1_UART_Init(void); +static void MX_USART2_UART_Init(void); /* Private functions ---------------------------------------------------------*/ /** @@ -103,6 +103,7 @@ void stm_init(uint32_t *buf0, uint16_t counters) MX_GPIO_Init(); MX_TIM2_Init(buf0, counters); MX_USART1_UART_Init(); + MX_USART2_UART_Init(); } @@ -153,7 +154,7 @@ static void SystemClock_Config(void) RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV8; RCC_OscInitStruct.PLL.PLLQ = 7; - if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { + if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } @@ -163,7 +164,7 @@ static void SystemClock_Config(void) RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; /* AHB prescaler */ RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; /* APB1 prescaler /1 gives 42 MHz APB1 */ RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; /* APB2 prescaler /1 gives 42 MHz APB2 */ - if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { + if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK) { Error_Handler(); } @@ -251,13 +252,12 @@ void MX_TIM2_Init(uint32_t *counters_buf, uint16_t counters) */ void MX_USART1_UART_Init(void) { - huart1.Instance = USART1; huart1.Init.BaudRate = UART1_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; /* Only Transmit */ + huart1.Init.Mode = UART_MODE_TX_RX; huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; @@ -267,6 +267,24 @@ void MX_USART1_UART_Init(void) } } +/* USART2 init function */ +void MX_USART2_UART_Init(void) +{ + huart2.Instance = USART2; + huart2.Init.BaudRate = UART2_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(); + } +} + /** * @brief This function is executed in case of error occurrence. @@ -276,7 +294,7 @@ void MX_USART1_UART_Init(void) static void Error_Handler(void) { HAL_GPIO_WritePin(LED_PORT, LED_RED, GPIO_PIN_SET); - while(1) { ; } + while(1) { ; } } #ifdef USE_FULL_ASSERT diff --git a/src/entropy/stm_init.h b/src/entropy/stm_init.h index 6a5de19..d58c254 100644 --- a/src/entropy/stm_init.h +++ b/src/entropy/stm_init.h @@ -10,6 +10,7 @@ #define LED_BLUE GPIO_PIN_15 extern UART_HandleTypeDef huart1; +extern UART_HandleTypeDef huart2; extern DMA_HandleTypeDef hdma_tim; extern void stm_init(uint32_t *buf0, uint16_t counters); |