Skip to content

Portenta C33 UNO R4 minima UNO R4 WiFi allow usage of TRNG #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions cores/arduino/WMath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,47 @@

extern "C" {
#include <stdlib.h>
#include "common_data.h"
}

#if defined (ARDUINO_PORTENTA_C33)
#define SCE_TRNG_SUPPORT 1
static long trng()
{
uint32_t value[4];
if (R_SCE_Open(&sce_ctrl, &sce_cfg) != FSP_SUCCESS)
return -1;
R_SCE_RandomNumberGenerate(value);
R_SCE_Close(&sce_ctrl);
return (long)value[0] >= 0 ? value[0] : -value[0];
}
#endif

#if defined(ARDUINO_UNOR4_WIFI) || defined(ARDUINO_UNOR4_MINIMA)
#define SCE_TRNG_SUPPORT 1
extern "C" {
fsp_err_t HW_SCE_McuSpecificInit(void);
fsp_err_t HW_SCE_RNG_Read(uint32_t * OutData_Text);
}
static long trng()
{
uint32_t value[4];
if (HW_SCE_McuSpecificInit() != FSP_SUCCESS)
return -1;
HW_SCE_RNG_Read(value);
return (long)value[0] >= 0 ? value[0] : -value[0];
}
#endif

#if (SCE_TRNG_SUPPORT == 1)
static bool useTRNG = true;
#endif

void randomSeed(unsigned long seed)
{
#if (SCE_TRNG_SUPPORT == 1)
useTRNG = false;
#endif
if (seed != 0) {
srand(seed);
}
Expand All @@ -37,6 +74,11 @@ long random(long howbig)
if (howbig == 0) {
return 0;
}
#if (SCE_TRNG_SUPPORT == 1)
if (useTRNG == true) {
return trng() % howbig;
}
#endif
return rand() % howbig;
}

Expand All @@ -48,3 +90,5 @@ long random(long howsmall, long howbig)
long diff = howbig - howsmall;
return random(diff) + howsmall;
}

#undef SCE_TRNG_SUPPORT
Loading