Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 496ffbd

Browse files
authored
Merge pull request #895 from pennam/trng
Enable (t)rng support for STM32H7 and NANO RP2040
2 parents 508d85e + 447062f commit 496ffbd

File tree

2 files changed

+758
-0
lines changed

2 files changed

+758
-0
lines changed

‎cores/arduino/WMath.cpp‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,36 @@
2323

2424
extern "C" {
2525
#include "stdlib.h"
26+
#include "hal/trng_api.h"
2627
}
28+
#if defined(ARDUINO_NANO_RP2040_CONNECT) || \
29+
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
2750

2851
void 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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /