Skip to content

Commit c942171

Browse files
committed
- 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.
1 parent 7cf9bf5 commit c942171

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/analogin_api.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void analogin_init(analogin_t *obj, PinName pin)
9999
obj->hDevice = hDevice;
100100

101101
/* Power up ADC */
102-
adi_adc_PowerUp (hDevice, true);
102+
adi_adc_PowerUp(hDevice, true);
103103

104104
/* Set ADC reference */
105105
adi_adc_SetVrefSource(hDevice, ADI_ADC_VREF_SRC_INT_2_50_V);
@@ -113,7 +113,7 @@ void analogin_init(analogin_t *obj, PinName pin)
113113
}
114114

115115
/* Start calibration */
116-
adi_adc_StartCalibration (hDevice);
116+
adi_adc_StartCalibration(hDevice);
117117

118118
/* Wait until calibration is done */
119119
while (!bCalibrationDone) {
@@ -198,7 +198,7 @@ static uint32_t adi_pin2channel(PinName pin)
198198
break;
199199
}
200200

201-
return((uint32_t) activech);
201+
return ((uint32_t)activech);
202202
}
203203

204204

targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/objects.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
#include "cmsis.h"
4545
#include "PeripheralNames.h"
4646
#include "PinNames.h"
47-
//#include "target_config.h"
4847
#include "gpio_object.h"
48+
#include "adi_rng.h"
4949

5050
#include "adi_i2c.h"
5151
#include "adi_spi.h"
@@ -69,7 +69,7 @@ struct serial_s {
6969
};
7070

7171
struct trng_s {
72-
uint8_t dummy;
72+
ADI_RNG_HANDLE RNGhDevice;
7373
};
7474

7575
#define BUILD_I2C_MI_DYNAMIC

targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api/trng_api.c

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,44 +52,58 @@
5252
#define TRNG_CNT_VAL 4095
5353
#define TRNG_PRESCALER 2
5454

55-
static ADI_RNG_HANDLE RNGhDevice; /* Memory to handle CRC Device */
56-
static uint32_t RngDevMem[(ADI_RNG_MEMORY_SIZE + 3)/4]; /* Data buffers for Random numbers */
55+
/* Data buffers for Random numbers */
56+
static uint32_t RngDevMem[(ADI_RNG_MEMORY_SIZE + 3)/4];
5757

5858
void trng_init(trng_t *obj)
5959
{
60-
(void)obj;
60+
ADI_RNG_HANDLE RNGhDevice;
61+
62+
// Open the device
6163
adi_rng_Open(0,RngDevMem,sizeof(RngDevMem),&RNGhDevice);
6264

6365
// Set sample length for the H/W RN accumulator
6466
adi_rng_SetSampleLen(RNGhDevice, TRNG_PRESCALER, TRNG_CNT_VAL);
6567

66-
// Enable the RNG
68+
// Disable buffering - single byte generation only
69+
adi_rng_EnableBuffering(RNGhDevice, false);
70+
71+
// Enable the TRNG
6772
adi_rng_Enable(RNGhDevice, true);
73+
74+
// Save device handle
75+
obj->RNGhDevice = RNGhDevice;
6876
}
6977

7078
void trng_free(trng_t *obj)
7179
{
72-
(void)obj;
80+
ADI_RNG_HANDLE RNGhDevice = obj->RNGhDevice;
81+
7382
adi_rng_Enable(RNGhDevice, false);
7483
adi_rng_Close(RNGhDevice);
7584
}
7685

7786
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
7887
{
79-
(void)obj;
88+
ADI_RNG_HANDLE RNGhDevice = obj->RNGhDevice;
8089
bool bRNGRdy;
8190
uint32_t nRandomNum, i;
8291

83-
for (i = 0; i < length; ) {
84-
// wait for the RNG ready to give a random number
85-
adi_rng_GetRdyStatus(RNGhDevice, &bRNGRdy);
86-
if (bRNGRdy) {
87-
// read the random number
88-
adi_rng_GetRngData(RNGhDevice, &nRandomNum);
89-
output[i++] = (uint8_t) nRandomNum;
90-
}
92+
for (i = 0; i < length; i++) {
93+
// Loop until the device has data to be read
94+
do {
95+
adi_rng_GetRdyStatus(RNGhDevice, &bRNGRdy);
96+
} while (!bRNGRdy);
97+
98+
// Read the RNG
99+
adi_rng_GetRngData(RNGhDevice, &nRandomNum);
100+
101+
// Save the output
102+
output[i] = (uint8_t)nRandomNum;
91103
}
104+
92105
*output_length = length;
106+
93107
return 0;
94108
}
95109

0 commit comments

Comments
 (0)