Skip to content

Commit 9e0e9c8

Browse files
committed
Added a check for the STUCK bit before reading the RNG data register to ensure there are no hardware faults.
1 parent 2eff4c7 commit 9e0e9c8

File tree

1 file changed

+22
-10
lines changed
  • targets/TARGET_Analog_Devices/TARGET_ADUCM4X50/TARGET_ADUCM4050/api

1 file changed

+22
-10
lines changed

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#include <stdlib.h>
4444
#include <adi_rng.h>
4545
#include <adi_pwr.h>
46+
#include "adi_rng_def.h"
4647
#include "cmsis.h"
4748
#include "trng_api.h"
4849

@@ -86,37 +87,48 @@ void trng_free(trng_t *obj)
8687
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
8788
{
8889
ADI_RNG_HANDLE RNGhDevice = obj->RNGhDevice;
89-
bool bRNGRdy;
90-
uint32_t nRandomNum, i;
90+
bool bRNGRdy, bStuck;
91+
uint32_t i;
92+
volatile uint32_t nRandomNum;
9193
ADI_RNG_RESULT result;
94+
ADI_RNG_DEV_TYPE *pDevice = (ADI_RNG_DEV_TYPE*)RNGhDevice;
9295

9396
for (i = 0; i < length; i++) {
9497
// Loop until the device has data to be read
9598
do {
9699
result = adi_rng_GetRdyStatus(RNGhDevice, &bRNGRdy);
97-
if (result != ADI_RNG_SUCCESS)
98-
{
100+
if (result != ADI_RNG_SUCCESS) {
99101
return -1;
100102
}
101103
} while (!bRNGRdy);
102104

105+
// Check the STUCK bit to make sure the oscillator output isn't stuck
106+
result = adi_rng_GetStuckStatus(RNGhDevice, &bStuck);
107+
108+
// If the stuck bit is set, this means there may be a problem with RNG hardware,
109+
// exit with an error
110+
if ( (result != ADI_RNG_SUCCESS) || ((result == ADI_RNG_SUCCESS) && (bStuck)) ) {
111+
// Clear the STUCK bit by writing a 1 to it
112+
pDevice->pRNG->STAT |= BITM_RNG_STAT_STUCK;
113+
return -1;
114+
}
115+
103116
// Read the RNG
104-
result = adi_rng_GetRngData(RNGhDevice, &nRandomNum);
117+
result = adi_rng_GetRngData(RNGhDevice, (uint32_t*)(&nRandomNum));
105118

106-
if (result != ADI_RNG_SUCCESS)
107-
{
119+
if (result != ADI_RNG_SUCCESS) {
108120
return -1;
109121
}
110122

111123
// Save the output
112124
output[i] = (uint8_t)(nRandomNum & 0xFF);
113-
114-
// Clear the nRandomNum variable for security purposes
115-
nRandomNum = 0;
116125
}
117126

118127
*output_length = length;
119128

129+
// Clear nRandomNum on the stack before exiting
130+
nRandomNum = 0;
131+
120132
return 0;
121133
}
122134

0 commit comments

Comments
 (0)