File tree 1 file changed +33
-0
lines changed
1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 23
23
24
24
extern " C" {
25
25
#include " stdlib.h"
26
+ #include " hal/trng_api.h"
26
27
}
27
28
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
+
28
51
void randomSeed (unsigned long seed)
29
52
{
53
+ #if (MBED_TRNG_SUPPORT == 1)
54
+ useTRNG = false ;
55
+ #endif
30
56
if (seed != 0 ) {
31
57
srandom (seed);
32
58
}
@@ -37,6 +63,11 @@ long random(long howbig)
37
63
if (howbig == 0 ) {
38
64
return 0 ;
39
65
}
66
+ #if (MBED_TRNG_SUPPORT == 1)
67
+ if (useTRNG == true ) {
68
+ return trng () % howbig;
69
+ }
70
+ #endif
40
71
return random () % howbig;
41
72
}
42
73
@@ -48,3 +79,5 @@ long random(long howsmall, long howbig)
48
79
long diff = howbig - howsmall;
49
80
return random (diff) + howsmall;
50
81
}
82
+
83
+ #undef MBED_TRNG_SUPPORT
You can’t perform that action at this time.
0 commit comments