From 8f746c1bba69550b966e3a0edd6ccc9c9877db55 Mon Sep 17 00:00:00 2001 From: Fredrik Thulin Date: Mon, 5 Sep 2016 09:45:13 +0200 Subject: add bit-for-bit testing of FMC address and data bus --- projects/cli-test/test-fmc.c | 100 +++++++++++++++++++++++++++---------------- 1 file changed, 64 insertions(+), 36 deletions(-) diff --git a/projects/cli-test/test-fmc.c b/projects/cli-test/test-fmc.c index ea9afef..b393dac 100644 --- a/projects/cli-test/test-fmc.c +++ b/projects/cli-test/test-fmc.c @@ -76,9 +76,27 @@ volatile uint32_t data_diff = 0; volatile uint32_t addr_diff = 0; +static int _write_then_read(struct cli_def *cli, uint32_t addr, uint32_t *write_buf, uint32_t *read_buf) +{ + int ok; + + ok = fmc_write_32(addr, write_buf); + if (ok != 0) { + cli_print(cli, "FMC write failed: 0x%x", ok); + return 0; + } + ok = fmc_read_32(0, read_buf); + if (ok != 0) { + cli_print(cli, "FMC read failed: 0x%x", ok); + return 0; + } + + return 1; +} + int test_fpga_data_bus(struct cli_def *cli, uint32_t test_rounds) { - int c, ok; + int i, c; uint32_t rnd, buf; HAL_StatusTypeDef hal_result; @@ -96,19 +114,8 @@ int test_fpga_data_bus(struct cli_def *cli, uint32_t test_rounds) break; } - /* write value to fpga at address 0 */ - ok = fmc_write_32(0, &rnd); - if (ok != 0) { - cli_print(cli, "FMC write failed: 0x%x", ok); - break; - } - - /* read value from fpga */ - ok = fmc_read_32(0, &buf); - if (ok != 0) { - cli_print(cli, "FMC read failed: 0x%x", ok); - break; - } + /* write value to fpga at address 0 and then read it back from the test register */ + if (! _write_then_read(cli, 0, &rnd, &buf)) break; /* compare (abort testing in case of error) */ data_diff = buf ^ rnd; @@ -124,16 +131,31 @@ int test_fpga_data_bus(struct cli_def *cli, uint32_t test_rounds) if (! data_diff) { cli_print(cli, "Sample of data bus test data: expected 0x%lx got 0x%lx", rnd, buf); + } else { + uint32_t data; + cli_print(cli, "\nFMC data bus per-bit analysis:"); + for (i = 0; i < 31; i++) { + data = 1 << i; + + if (! _write_then_read(cli, 0, &data, &buf)) break; + + if (data == buf) { + cli_print(cli, "Data 0x%08lx (FMC_D%02i) - OK", data, i + 1); + } else { + cli_print(cli, "Data 0x%08lx (FMC_D%02i) - FAIL (read 0x%08lx)", data, i + 1, buf); + } + } } + /* return number of successful tests */ return c; } int test_fpga_address_bus(struct cli_def *cli, uint32_t test_rounds) { - int c, ok; - uint32_t rnd, buf; + int i, c; + uint32_t addr, buf, dummy = 1; HAL_StatusTypeDef hal_result; /* initialize stm32 rng */ @@ -144,31 +166,22 @@ int test_fpga_address_bus(struct cli_def *cli, uint32_t test_rounds) for (c = 0; c < test_rounds; c++) { addr_diff = 0; /* try to generate "random" number */ - hal_result = HAL_RNG_GenerateRandomNumber(&rng_inst, &rnd); + hal_result = HAL_RNG_GenerateRandomNumber(&rng_inst, &addr); if (hal_result != HAL_OK) break; /* there are 26 physicaly connected address lines on the alpha, but "only" 24 usable for now (the top two ones are used by FMC to choose bank, and we only have one bank set up currently) */ - rnd &= 0x3fffffc; + addr &= 0x3fffffc; /* don't test zero addresses (fpga will store data, not address) */ - if (rnd == 0) continue; + if (addr == 0) continue; - /* write dummy value to fpga at some non-zero address */ - ok = fmc_write_32(rnd, &buf); - if (ok != 0) { - cli_print(cli, "FMC write failed: 0x%x", ok); - break; - } - - /* read value from fpga */ - ok = fmc_read_32(0, &buf); - if (ok != 0) { - cli_print(cli, "FMC read failed: 0x%x", ok); - break; - } + /* write dummy value to fpga at some non-zero address and then read from the + test register to see what address the FPGA thought we wrote to + */ + if (! _write_then_read(cli, addr, &dummy, &buf)) break; /* fpga receives address of 32-bit word, while we need byte address here to compare @@ -176,9 +189,9 @@ int test_fpga_address_bus(struct cli_def *cli, uint32_t test_rounds) buf <<= 2; /* compare (abort testing in case of error) */ - addr_diff = buf ^ rnd; + addr_diff = buf ^ addr; if (addr_diff) { - cli_print(cli, "Address bus FAIL: expected 0x%lx got 0x%lx", rnd, buf); + cli_print(cli, "Address bus FAIL: expected 0x%lx got 0x%lx", addr, buf); uart_send_string2(STM_UART_MGMT, (char *) "Binary diff: "); uart_send_number2(STM_UART_MGMT, addr_diff, 32, 2); uart_send_string2(STM_UART_MGMT, "\r\n"); @@ -188,10 +201,25 @@ int test_fpga_address_bus(struct cli_def *cli, uint32_t test_rounds) } if (! addr_diff) { - cli_print(cli, "Sample of address bus test data: expected 0x%lx got 0x%lx", rnd, buf); + cli_print(cli, "Sample of address bus test data: expected 0x%lx got 0x%lx", addr, buf); + } else { + cli_print(cli, "\nFMC address bus per-bit analysis:"); + for (i = 0; i < 23; i++) { + uint32_t shifted_addr; + addr = 1 << i; + + shifted_addr = addr << 2; + + if (! _write_then_read(cli, shifted_addr, &dummy, &buf)) break; + + if (addr == buf) { + cli_print(cli, "Address 0x%08lx (FMC_A%02i) - OK", addr, i + 1); + } else { + cli_print(cli, "Address 0x%08lx (FMC_A%02i) - FAIL (read 0x%08lx)", addr, i + 1, buf); + } + } } /* return number of successful tests */ return c; } - -- cgit v1.2.3 From d172acba926b72c57c47697bd640c51c0fcb038d Mon Sep 17 00:00:00 2001 From: Paul Selkirk Date: Tue, 20 Sep 2016 09:10:08 -0400 Subject: Still some problem with uart receive under heavy load, so change to a 2-byte receive buffer with half-complete callbacks, and raise the dma priority. --- .../TARGET_CRYPTECH_ALPHA/stm32f4xx_hal_msp.c | 2 +- .../TARGET_CRYPTECH_ALPHA/stm32f4xx_it.c | 42 +++++++++++++++++++--- .../TARGET_CRYPTECH_ALPHA/stm32f4xx_it_rtos.c | 42 +++++++++++++++++++--- projects/hsm/hsm.c | 25 +++++++------ 4 files changed, 89 insertions(+), 22 deletions(-) diff --git a/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_hal_msp.c b/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_hal_msp.c index 7eeb6df..609c596 100644 --- a/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_hal_msp.c +++ b/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_hal_msp.c @@ -182,7 +182,7 @@ void HAL_UART_MspInit(UART_HandleTypeDef* huart) hdma->Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE; hdma->Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; hdma->Init.Mode = DMA_CIRCULAR; - hdma->Init.Priority = DMA_PRIORITY_LOW; + hdma->Init.Priority = DMA_PRIORITY_HIGH; hdma->Init.FIFOMode = DMA_FIFOMODE_DISABLE; /* hdma->Init.FIFOThreshold = DMA_FIFO_THRESHOLD_HALFFULL; diff --git a/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_it.c b/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_it.c index 7b8e97f..f69e790 100644 --- a/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_it.c +++ b/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_it.c @@ -189,6 +189,12 @@ void USART2_IRQHandler(void) HAL_UART_IRQHandler(&huart_user); } +/** + * @brief Rx Transfer completed callbacks. + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. + * @retval None + */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { extern void HAL_UART1_RxCpltCallback(UART_HandleTypeDef *huart); @@ -201,23 +207,49 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) HAL_UART2_RxCpltCallback(huart); } +__weak void HAL_UART1_RxCpltCallback(UART_HandleTypeDef *huart) +{ + /* NOTE: This function Should not be modified, when the callback is needed, + the HAL_UART1_RxCpltCallback could be implemented in the user file + */ +} + +__weak void HAL_UART2_RxCpltCallback(UART_HandleTypeDef *huart) +{ + /* NOTE: This function Should not be modified, when the callback is needed, + the HAL_UART2_RxCpltCallback could be implemented in the user file + */ +} + /** - * @brief Rx Transfer completed callbacks. + * @brief Rx Half Transfer completed callbacks. * @param huart: pointer to a UART_HandleTypeDef structure that contains * the configuration information for the specified UART module. * @retval None */ -__weak void HAL_UART1_RxCpltCallback(UART_HandleTypeDef *huart) +void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) +{ + extern void HAL_UART1_RxHalfCpltCallback(UART_HandleTypeDef *huart); + extern void HAL_UART2_RxHalfCpltCallback(UART_HandleTypeDef *huart); + + if (huart->Instance == USART1) + HAL_UART1_RxHalfCpltCallback(huart); + + else if (huart->Instance == USART2) + HAL_UART2_RxHalfCpltCallback(huart); +} + +__weak void HAL_UART1_RxHalfCpltCallback(UART_HandleTypeDef *huart) { /* NOTE: This function Should not be modified, when the callback is needed, - the HAL_UART_RxCpltCallback could be implemented in the user file + the HAL_UART1_RxHalfCpltCallback could be implemented in the user file */ } -__weak void HAL_UART2_RxCpltCallback(UART_HandleTypeDef *huart) +__weak void HAL_UART2_RxHalfCpltCallback(UART_HandleTypeDef *huart) { /* NOTE: This function Should not be modified, when the callback is needed, - the HAL_UART_TxCpltCallback could be implemented in the user file + the HAL_UART2_RxHalfCpltCallback could be implemented in the user file */ } diff --git a/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_it_rtos.c b/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_it_rtos.c index 1118db2..0b829b3 100644 --- a/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_it_rtos.c +++ b/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F4/TARGET_CRYPTECH_ALPHA/stm32f4xx_it_rtos.c @@ -193,6 +193,12 @@ void USART2_IRQHandler(void) HAL_UART_IRQHandler(&huart_user); } +/** + * @brief Rx Transfer completed callbacks. + * @param huart: pointer to a UART_HandleTypeDef structure that contains + * the configuration information for the specified UART module. + * @retval None + */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { extern void HAL_UART1_RxCpltCallback(UART_HandleTypeDef *huart); @@ -205,23 +211,49 @@ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) HAL_UART2_RxCpltCallback(huart); } +__weak void HAL_UART1_RxCpltCallback(UART_HandleTypeDef *huart) +{ + /* NOTE: This function Should not be modified, when the callback is needed, + the HAL_UART1_RxCpltCallback could be implemented in the user file + */ +} + +__weak void HAL_UART2_RxCpltCallback(UART_HandleTypeDef *huart) +{ + /* NOTE: This function Should not be modified, when the callback is needed, + the HAL_UART2_RxCpltCallback could be implemented in the user file + */ +} + /** - * @brief Rx Transfer completed callbacks. + * @brief Rx Half Transfer completed callbacks. * @param huart: pointer to a UART_HandleTypeDef structure that contains * the configuration information for the specified UART module. * @retval None */ -__weak void HAL_UART1_RxCpltCallback(UART_HandleTypeDef *huart) +void HAL_UART_RxHalfCpltCallback(UART_HandleTypeDef *huart) +{ + extern void HAL_UART1_RxHalfCpltCallback(UART_HandleTypeDef *huart); + extern void HAL_UART2_RxHalfCpltCallback(UART_HandleTypeDef *huart); + + if (huart->Instance == USART1) + HAL_UART1_RxHalfCpltCallback(huart); + + else if (huart->Instance == USART2) + HAL_UART2_RxHalfCpltCallback(huart); +} + +__weak void HAL_UART1_RxHalfCpltCallback(UART_HandleTypeDef *huart) { /* NOTE: This function Should not be modified, when the callback is needed, - the HAL_UART_RxCpltCallback could be implemented in the user file + the HAL_UART1_RxHalfCpltCallback could be implemented in the user file */ } -__weak void HAL_UART2_RxCpltCallback(UART_HandleTypeDef *huart) +__weak void HAL_UART2_RxHalfCpltCallback(UART_HandleTypeDef *huart) { /* NOTE: This function Should not be modified, when the callback is needed, - the HAL_UART_TxCpltCallback could be implemented in the user file + the HAL_UART2_RxHalfCpltCallback could be implemented in the user file */ } diff --git a/projects/hsm/hsm.c b/projects/hsm/hsm.c index 862e718..f2f1b12 100644 --- a/projects/hsm/hsm.c +++ b/projects/hsm/hsm.c @@ -103,7 +103,7 @@ osMutexId uart_mutex; osMutexDef(uart_mutex); #endif -static volatile uint8_t uart_rx; /* current character received from UART */ +static uint8_t uart_rx[2]; /* current character received from UART */ /* Callback for HAL_UART_Receive_DMA(). * With multiple worker threads, we can't do a blocking receive, because @@ -113,7 +113,7 @@ static volatile uint8_t uart_rx; /* current character received from UART */ * Even with only one worker thread, context-switching to the CLI thread * causes HAL_UART_Receive to miss input. */ -void HAL_UART2_RxCpltCallback(UART_HandleTypeDef *huart) +static void RxCallback(uint8_t c) { /* current RPC input buffer */ static rpc_buffer_t *ibuf = NULL; @@ -125,7 +125,7 @@ void HAL_UART2_RxCpltCallback(UART_HandleTypeDef *huart) ibuf->len = 0; } - if (hal_slip_recv_char(ibuf->buf, &ibuf->len, sizeof(ibuf->buf), &complete) != LIBHAL_OK) + if (hal_slip_process_char(c, ibuf->buf, &ibuf->len, sizeof(ibuf->buf), &complete) != LIBHAL_OK) Error_Handler(); if (complete) { @@ -135,17 +135,20 @@ void HAL_UART2_RxCpltCallback(UART_HandleTypeDef *huart) } } -void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) +void HAL_UART2_RxHalfCpltCallback(UART_HandleTypeDef *huart) { - /* I dunno, just trap it for now */ - Error_Handler(); + RxCallback(uart_rx[0]); } -hal_error_t hal_serial_recv_char(uint8_t *cp) +void HAL_UART2_RxCpltCallback(UART_HandleTypeDef *huart) { - /* return the character from HAL_UART_Receive_DMA */ - *cp = uart_rx; - return LIBHAL_OK; + RxCallback(uart_rx[1]); +} + +void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) +{ + /* I dunno, just trap it for now */ + Error_Handler(); } hal_error_t hal_serial_send_char(uint8_t c) @@ -269,7 +272,7 @@ int main() } /* Start the UART receiver. */ - if (HAL_UART_Receive_DMA(&huart_user, (uint8_t *)&uart_rx, 1) != CMSIS_HAL_OK) + if (HAL_UART_Receive_DMA(&huart_user, uart_rx, 2) != CMSIS_HAL_OK) Error_Handler(); /* Launch other threads (csprng warm-up thread?) -- cgit v1.2.3