From c942171a9955c9f2c9ee6362c492a06fe6e37e53 Mon Sep 17 00:00:00 2001 From: Dave Wu Date: Tue, 3 Oct 2017 14:21:37 +1100 Subject: [PATCH] - Fixed an issue where the TRNG device is read even when it is not ready; - Added a configuration call in trng_init to make sure the TRNG buffering mode is disabled, so only 8-bit bytes are returned; - Moved the TRNG device handle into the trng_t structure; - Fixed some formatting errors in the adc driver. --- .../TARGET_ADUCM4050/api/analogin_api.c | 6 +-- .../TARGET_ADUCM4050/api/objects.h | 4 +- .../TARGET_ADUCM4050/api/trng_api.c | 42 ++++++++++++------- 3 files changed, 33 insertions(+), 19 deletions(-) diff --git a/targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/analogin_api.c b/targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/analogin_api.c index b9ef021c19..06413a81d6 100755 --- a/targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/analogin_api.c +++ b/targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/analogin_api.c @@ -99,7 +99,7 @@ void analogin_init(analogin_t *obj, PinName pin) obj->hDevice = hDevice; /* Power up ADC */ - adi_adc_PowerUp (hDevice, true); + adi_adc_PowerUp(hDevice, true); /* Set ADC reference */ adi_adc_SetVrefSource(hDevice, ADI_ADC_VREF_SRC_INT_2_50_V); @@ -113,7 +113,7 @@ void analogin_init(analogin_t *obj, PinName pin) } /* Start calibration */ - adi_adc_StartCalibration (hDevice); + adi_adc_StartCalibration(hDevice); /* Wait until calibration is done */ while (!bCalibrationDone) { @@ -198,7 +198,7 @@ static uint32_t adi_pin2channel(PinName pin) break; } - return((uint32_t) activech); + return ((uint32_t)activech); } diff --git a/targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/objects.h b/targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/objects.h index 7b316f5c32..aeecf36d00 100755 --- a/targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/objects.h +++ b/targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/objects.h @@ -44,8 +44,8 @@ #include "cmsis.h" #include "PeripheralNames.h" #include "PinNames.h" -//#include "target_config.h" #include "gpio_object.h" +#include "adi_rng.h" #include "adi_i2c.h" #include "adi_spi.h" @@ -69,7 +69,7 @@ struct serial_s { }; struct trng_s { - uint8_t dummy; + ADI_RNG_HANDLE RNGhDevice; }; #define BUILD_I2C_MI_DYNAMIC diff --git a/targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/trng_api.c b/targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/trng_api.c index 4012775454..8106f44e59 100755 --- a/targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/trng_api.c +++ b/targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/trng_api.c @@ -52,44 +52,58 @@ #define TRNG_CNT_VAL 4095 #define TRNG_PRESCALER 2 -static ADI_RNG_HANDLE RNGhDevice; /* Memory to handle CRC Device */ -static uint32_t RngDevMem[(ADI_RNG_MEMORY_SIZE + 3)/4]; /* Data buffers for Random numbers */ +/* Data buffers for Random numbers */ +static uint32_t RngDevMem[(ADI_RNG_MEMORY_SIZE + 3)/4]; void trng_init(trng_t *obj) { - (void)obj; + ADI_RNG_HANDLE RNGhDevice; + + // Open the device adi_rng_Open(0,RngDevMem,sizeof(RngDevMem),&RNGhDevice); // Set sample length for the H/W RN accumulator adi_rng_SetSampleLen(RNGhDevice, TRNG_PRESCALER, TRNG_CNT_VAL); - // Enable the RNG + // Disable buffering - single byte generation only + adi_rng_EnableBuffering(RNGhDevice, false); + + // Enable the TRNG adi_rng_Enable(RNGhDevice, true); + + // Save device handle + obj->RNGhDevice = RNGhDevice; } void trng_free(trng_t *obj) { - (void)obj; + ADI_RNG_HANDLE RNGhDevice = obj->RNGhDevice; + adi_rng_Enable(RNGhDevice, false); adi_rng_Close(RNGhDevice); } int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) { - (void)obj; + ADI_RNG_HANDLE RNGhDevice = obj->RNGhDevice; bool bRNGRdy; uint32_t nRandomNum, i; - for (i = 0; i < length; ) { - // wait for the RNG ready to give a random number - adi_rng_GetRdyStatus(RNGhDevice, &bRNGRdy); - if (bRNGRdy) { - // read the random number - adi_rng_GetRngData(RNGhDevice, &nRandomNum); - output[i++] = (uint8_t) nRandomNum; - } + for (i = 0; i < length; i++) { + // Loop until the device has data to be read + do { + adi_rng_GetRdyStatus(RNGhDevice, &bRNGRdy); + } while (!bRNGRdy); + + // Read the RNG + adi_rng_GetRngData(RNGhDevice, &nRandomNum); + + // Save the output + output[i] = (uint8_t)nRandomNum; } + *output_length = length; + return 0; }