- 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.

pull/5523/head
Dave Wu 2017-10-03 14:21:37 +11:00 committed by adbridge
parent af8b5d916f
commit aa6872523e
3 changed files with 33 additions and 19 deletions

View File

@ -99,7 +99,7 @@ void analogin_init(analogin_t *obj, PinName pin)
obj->hDevice = hDevice; obj->hDevice = hDevice;
/* Power up ADC */ /* Power up ADC */
adi_adc_PowerUp (hDevice, true); adi_adc_PowerUp(hDevice, true);
/* Set ADC reference */ /* Set ADC reference */
adi_adc_SetVrefSource(hDevice, ADI_ADC_VREF_SRC_INT_2_50_V); 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 */ /* Start calibration */
adi_adc_StartCalibration (hDevice); adi_adc_StartCalibration(hDevice);
/* Wait until calibration is done */ /* Wait until calibration is done */
while (!bCalibrationDone) { while (!bCalibrationDone) {
@ -198,7 +198,7 @@ static uint32_t adi_pin2channel(PinName pin)
break; break;
} }
return((uint32_t) activech); return ((uint32_t)activech);
} }

View File

@ -44,8 +44,8 @@
#include "cmsis.h" #include "cmsis.h"
#include "PeripheralNames.h" #include "PeripheralNames.h"
#include "PinNames.h" #include "PinNames.h"
//#include "target_config.h"
#include "gpio_object.h" #include "gpio_object.h"
#include "adi_rng.h"
#include "adi_i2c.h" #include "adi_i2c.h"
#include "adi_spi.h" #include "adi_spi.h"
@ -69,7 +69,7 @@ struct serial_s {
}; };
struct trng_s { struct trng_s {
uint8_t dummy; ADI_RNG_HANDLE RNGhDevice;
}; };
#define BUILD_I2C_MI_DYNAMIC #define BUILD_I2C_MI_DYNAMIC

View File

@ -52,44 +52,58 @@
#define TRNG_CNT_VAL 4095 #define TRNG_CNT_VAL 4095
#define TRNG_PRESCALER 2 #define TRNG_PRESCALER 2
static ADI_RNG_HANDLE RNGhDevice; /* Memory to handle CRC Device */ /* Data buffers for Random numbers */
static uint32_t RngDevMem[(ADI_RNG_MEMORY_SIZE + 3)/4]; /* Data buffers for Random numbers */ static uint32_t RngDevMem[(ADI_RNG_MEMORY_SIZE + 3)/4];
void trng_init(trng_t *obj) void trng_init(trng_t *obj)
{ {
(void)obj; ADI_RNG_HANDLE RNGhDevice;
// Open the device
adi_rng_Open(0,RngDevMem,sizeof(RngDevMem),&RNGhDevice); adi_rng_Open(0,RngDevMem,sizeof(RngDevMem),&RNGhDevice);
// Set sample length for the H/W RN accumulator // Set sample length for the H/W RN accumulator
adi_rng_SetSampleLen(RNGhDevice, TRNG_PRESCALER, TRNG_CNT_VAL); 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); adi_rng_Enable(RNGhDevice, true);
// Save device handle
obj->RNGhDevice = RNGhDevice;
} }
void trng_free(trng_t *obj) void trng_free(trng_t *obj)
{ {
(void)obj; ADI_RNG_HANDLE RNGhDevice = obj->RNGhDevice;
adi_rng_Enable(RNGhDevice, false); adi_rng_Enable(RNGhDevice, false);
adi_rng_Close(RNGhDevice); adi_rng_Close(RNGhDevice);
} }
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length) 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; bool bRNGRdy;
uint32_t nRandomNum, i; uint32_t nRandomNum, i;
for (i = 0; i < length; ) { for (i = 0; i < length; i++) {
// wait for the RNG ready to give a random number // Loop until the device has data to be read
do {
adi_rng_GetRdyStatus(RNGhDevice, &bRNGRdy); adi_rng_GetRdyStatus(RNGhDevice, &bRNGRdy);
if (bRNGRdy) { } while (!bRNGRdy);
// read the random number
// Read the RNG
adi_rng_GetRngData(RNGhDevice, &nRandomNum); adi_rng_GetRngData(RNGhDevice, &nRandomNum);
output[i++] = (uint8_t) nRandomNum;
} // Save the output
output[i] = (uint8_t)nRandomNum;
} }
*output_length = length; *output_length = length;
return 0; return 0;
} }