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 | |
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')
-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 | ||||
-rw-r--r-- | src/uart-test/main.c | 25 | ||||
-rw-r--r-- | src/uart-test/stm32f4xx_hal_msp.c | 20 | ||||
-rw-r--r-- | src/uart-test/stm32f4xx_it.c | 25 | ||||
-rw-r--r-- | src/uart-test/stm_init.c | 50 | ||||
-rw-r--r-- | src/uart-test/stm_init.h | 1 |
10 files changed, 164 insertions, 66 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); diff --git a/src/uart-test/main.c b/src/uart-test/main.c index 77c17f4..55ec6fb 100644 --- a/src/uart-test/main.c +++ b/src/uart-test/main.c @@ -9,24 +9,45 @@ #define DELAY() HAL_Delay(250) +UART_HandleTypeDef *huart; + +/* + * 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') { + HAL_GPIO_TogglePin(LED_PORT, LED_GREEN); + + huart = this; + } + } +} + int main() { uint8_t c = 'a'; - uint32_t i = 0; stm_init(); + huart = &huart1; + while (1) { HAL_GPIO_TogglePin(LED_PORT, LED_YELLOW); - HAL_UART_Transmit(&huart1, (uint8_t *) &c, 1, 0xff); + HAL_UART_Transmit(huart, (uint8_t *) &c, 1, 0xff); DELAY(); if (c++ == 'z') { c = 'a'; HAL_GPIO_TogglePin(LED_PORT, LED_BLUE); } + + /* Check for UART change request */ + check_uart_rx(&huart1); + check_uart_rx(&huart2); } } diff --git a/src/uart-test/stm32f4xx_hal_msp.c b/src/uart-test/stm32f4xx_hal_msp.c index 7cee9ce..4416195 100644 --- a/src/uart-test/stm32f4xx_hal_msp.c +++ b/src/uart-test/stm32f4xx_hal_msp.c @@ -5,6 +5,8 @@ * * PA9: USART1_TX * PA10: USART1_RX + * PA2: USART2_TX + * PA3: USART2_RX */ #include "stm32f4xx_hal.h" @@ -19,14 +21,19 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart) __USART1_CLK_ENABLE(); 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); - - /* 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); } } @@ -37,6 +44,9 @@ void HAL_UART_MspDeInit(UART_HandleTypeDef* huart) if (huart->Instance == USART1) { __USART1_CLK_DISABLE(); 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/uart-test/stm32f4xx_it.c b/src/uart-test/stm32f4xx_it.c index 4e5bd89..73e7735 100644 --- a/src/uart-test/stm32f4xx_it.c +++ b/src/uart-test/stm32f4xx_it.c @@ -53,10 +53,6 @@ /* Private define ------------------------------------------------------------*/ /* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ - -/* UART handler declared in "stm_init.h" file */ -//extern UART_HandleTypeDef huart2; - /* Private function prototypes -----------------------------------------------*/ /* Private functions ---------------------------------------------------------*/ @@ -170,27 +166,6 @@ 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 - */ -void USART1_IRQHandler(void) -{ - HAL_UART_IRQHandler(&huart1); -} - -/** - * @brief This function handles PPP interrupt request. - * @param None - * @retval None - */ -/*void PPP_IRQHandler(void) -{ -}*/ - -/** * @} */ diff --git a/src/uart-test/stm_init.c b/src/uart-test/stm_init.c index ad39080..384c4f8 100644 --- a/src/uart-test/stm_init.c +++ b/src/uart-test/stm_init.c @@ -40,11 +40,13 @@ #include "stm_init.h" UART_HandleTypeDef huart1; +UART_HandleTypeDef huart2; /* Private typedef -----------------------------------------------------------*/ /* Private define ------------------------------------------------------------*/ #define UART1_BAUD_RATE 460800 +#define UART2_BAUD_RATE 115200 /* Private macro -------------------------------------------------------------*/ @@ -57,6 +59,7 @@ static void SystemClock_Config(void); static void Error_Handler(void); static void MX_GPIO_Init(void); static void MX_USART1_UART_Init(void); +static void MX_USART2_UART_Init(void); /* Private functions ---------------------------------------------------------*/ /** @@ -89,6 +92,7 @@ void stm_init(void) /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART1_UART_Init(); + MX_USART2_UART_Init(); } @@ -138,8 +142,7 @@ static void SystemClock_Config(void) RCC_OscInitStruct.PLL.PLLN = 336; 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(); } @@ -149,8 +152,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(); } @@ -179,11 +181,11 @@ void MX_GPIO_Init(void) __GPIOC_CLK_ENABLE(); /*Configure LED GPIO pins PB12==red, PB13==yellow, PB14==green, PB15==blue */ - GPIO_InitStruct.Pin = GPIO_PIN_12 | GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15; + GPIO_InitStruct.Pin = LED_RED | LED_YELLOW | LED_GREEN | LED_BLUE; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; - HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); + HAL_GPIO_Init(LED_PORT, &GPIO_InitStruct); /*Configure GPIO pin : PC9 (RCC_MCO_2) */ GPIO_InitStruct.Pin = GPIO_PIN_9; @@ -209,22 +211,42 @@ void MX_GPIO_Init(void) 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_RX; - huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; + 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_RX; + huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart1.Init.OverSampling = UART_OVERSAMPLING_16; - if(HAL_UART_Init(&huart1) != HAL_OK) { + if (HAL_UART_Init(&huart1) != HAL_OK) { /* Initialization Error */ Error_Handler(); } } +/* 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. * @param None diff --git a/src/uart-test/stm_init.h b/src/uart-test/stm_init.h index e1cdfb8..838ca77 100644 --- a/src/uart-test/stm_init.h +++ b/src/uart-test/stm_init.h @@ -10,6 +10,7 @@ #define LED_BLUE GPIO_PIN_15 extern UART_HandleTypeDef huart1; +extern UART_HandleTypeDef huart2; extern void stm_init(void); |