mirror of https://github.com/ARMmbed/mbed-os.git
- 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/5144/head
parent
7cf9bf5c3d
commit
c942171a99
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue