How to use RP2350 hardware TRNG? #2643
-
The RP2350 is stated to include a hardware-based True Random Number Generator (TRNG), but I couldn't find an example specific to its use. Most examples available are based on Ring Oscillators (ROSC), which often produce lower-quality randomness compared to a true TRNG and are less reliable than even Arduino's random() function. Is there a way to utilize the RP2350's TRNG through the Arduino-Pico library? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The TRNG is supported through Pico-SDK's pico_rand module, which we do include in our precompiled libpico build configuration. The module uses the TRNG output to feed a xoroshiro128 generator, along a lot of other sources of randomness, e.g., the ROSC, the system time, the random initial values of SRAM or the board's unique ID. There is also Arduino wrapper library for this module: arduino-pico/cores/rp2040/RP2040Support.h Lines 463 to 465 in e16c459 That means that #include <Arduino.h>
void setup() {
Serial.begin(115200);
}
void loop() {
uint32_t random_val = rp2040.hwrand32();
Serial.println("Random value: 0x" + String(random_val, HEX));
delay(1000);
} We can call also the functions of the pico_rand module directly. You can also print out which exact sources were used. #include <Arduino.h>
#include <pico/rand.h>
void setup() {
Serial.begin(115200);
}
String get_used_rng_sources() {
String used_srcs = "";
#ifdef PICO_RAND_ENTROPY_SRC_TIME
used_srcs += "time,";
#endif
#if PICO_RAND_ENTROPY_SRC_ROSC
used_srcs += "rosc,";
#endif
#if PICO_RAND_ENTROPY_SRC_TRNG
used_srcs += "trng,";
#endif
#if PICO_RAND_ENTROPY_SRC_BUS_PERF_COUNTER
used_srcs += "perf_count,";
#endif
return used_srcs;
}
void loop() {
uint32_t random_val = get_rand_32();
Serial.println("RNG sources: " + get_used_rng_sources() + ". Random value: 0x" + String(random_val, HEX));
delay(1000);
} Prints on a RP2350 based board:
|
Beta Was this translation helpful? Give feedback.
Note that if you want to get the TRNG output directly, you can also do that.