From c4678339908e413cbc6751cf863267807acafc85 Mon Sep 17 00:00:00 2001 From: Fredrik Thulin Date: Sun, 28 Jun 2015 16:30:08 +0200 Subject: 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. --- src/entropy/main.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'src/entropy/main.c') 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 -- cgit v1.2.3