Skip to content

Commit 5dbb365

Browse files
committed
Enable trng support for STM32H7 boards
1 parent 0108ded commit 5dbb365

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Diff for: cores/arduino/WMath.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,36 @@
2323

2424
extern "C" {
2525
#include "stdlib.h"
26+
#include "hal/trng_api.h"
2627
}
2728

29+
#if defined(ARDUINO_PORTENTA_H7_M7) || \
30+
defined(ARDUINO_NICLA_VISION) || \
31+
defined(ARDUINO_OPTA) || \
32+
defined(ARDUINO_GIGA)
33+
#define MBED_TRNG_SUPPORT 1
34+
static long trng()
35+
{
36+
trng_t trng_obj;
37+
trng_init(&trng_obj);
38+
long value;
39+
size_t olen;
40+
if (trng_get_bytes(&trng_obj, (uint8_t*)&value, sizeof(value), &olen) != 0)
41+
return -1;
42+
trng_free(&trng_obj);
43+
return value >= 0 ? value : -value;
44+
}
45+
#endif
46+
47+
#if (MBED_TRNG_SUPPORT == 1)
48+
static bool useTRNG = true;
49+
#endif
50+
2851
void randomSeed(unsigned long seed)
2952
{
53+
#if (MBED_TRNG_SUPPORT == 1)
54+
useTRNG = false;
55+
#endif
3056
if (seed != 0) {
3157
srandom(seed);
3258
}
@@ -37,6 +63,11 @@ long random(long howbig)
3763
if (howbig == 0) {
3864
return 0;
3965
}
66+
#if (MBED_TRNG_SUPPORT == 1)
67+
if (useTRNG == true) {
68+
return trng() % howbig;
69+
}
70+
#endif
4071
return random() % howbig;
4172
}
4273

@@ -48,3 +79,5 @@ long random(long howsmall, long howbig)
4879
long diff = howbig - howsmall;
4980
return random(diff) + howsmall;
5081
}
82+
83+
#undef MBED_TRNG_SUPPORT

0 commit comments

Comments
 (0)