2323
2424extern " C" {
2525 #include < stdlib.h>
26+ #include " common_data.h"
2627}
2728
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+
2862void randomSeed (unsigned long seed)
2963{
64+ #if (SCE_TRNG_SUPPORT == 1)
65+ useTRNG = false ;
66+ #endif
3067 if (seed != 0 ) {
3168 srand (seed);
3269 }
@@ -37,6 +74,11 @@ long random(long howbig)
3774 if (howbig == 0 ) {
3875 return 0 ;
3976 }
77+ #if (SCE_TRNG_SUPPORT == 1)
78+ if (useTRNG == true ) {
79+ return trng () % howbig;
80+ }
81+ #endif
4082 return rand () % howbig;
4183}
4284
@@ -48,3 +90,5 @@ long random(long howsmall, long howbig)
4890 long diff = howbig - howsmall;
4991 return random (diff) + howsmall;
5092}
93+
94+ #undef SCE_TRNG_SUPPORT
0 commit comments