23
23
24
24
extern " C" {
25
25
#include < stdlib.h>
26
+ #include " common_data.h"
26
27
}
27
28
29
+ #if defined (ARDUINO_PORTENTA_C33)
30
+ #define SCE_TRNG_SUPPORT 1
31
+ static long trng ()
32
+ {
33
+ uint32_t value[4 ];
34
+ if (R_SCE_Open (&sce_ctrl, &sce_cfg) != FSP_SUCCESS)
35
+ return -1 ;
36
+ R_SCE_RandomNumberGenerate (value);
37
+ R_SCE_Close (&sce_ctrl);
38
+ return (long )value[0 ] >= 0 ? value[0 ] : -value[0 ];
39
+ }
40
+ #endif
41
+
42
+ #if defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_UNOR4_MINIMA)
43
+ #define SCE_TRNG_SUPPORT 1
44
+ extern " C" {
45
+ fsp_err_t HW_SCE_McuSpecificInit (void );
46
+ fsp_err_t HW_SCE_RNG_Read (uint32_t * OutData_Text);
47
+ }
48
+ static long trng ()
49
+ {
50
+ uint32_t value[4 ];
51
+ if (HW_SCE_McuSpecificInit () != FSP_SUCCESS)
52
+ return -1 ;
53
+ HW_SCE_RNG_Read (value);
54
+ return (long )value[0 ] >= 0 ? value[0 ] : -value[0 ];
55
+ }
56
+ #endif
57
+
58
+ #if (SCE_TRNG_SUPPORT == 1)
59
+ static bool useTRNG = true ;
60
+ #endif
61
+
28
62
void randomSeed (unsigned long seed)
29
63
{
64
+ #if (SCE_TRNG_SUPPORT == 1)
65
+ useTRNG = false ;
66
+ #endif
30
67
if (seed != 0 ) {
31
68
srand (seed);
32
69
}
@@ -37,6 +74,11 @@ long random(long howbig)
37
74
if (howbig == 0 ) {
38
75
return 0 ;
39
76
}
77
+ #if (SCE_TRNG_SUPPORT == 1)
78
+ if (useTRNG == true ) {
79
+ return trng () % howbig;
80
+ }
81
+ #endif
40
82
return rand () % howbig;
41
83
}
42
84
@@ -48,3 +90,5 @@ long random(long howsmall, long howbig)
48
90
long diff = howbig - howsmall;
49
91
return random (diff) + howsmall;
50
92
}
93
+
94
+ #undef SCE_TRNG_SUPPORT
0 commit comments