File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change 2323
2424extern " 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+
2851void 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
You can’t perform that action at this time.
0 commit comments