Skip to content

Instantly share code, notes, and snippets.

@wandrson
Created April 5, 2014 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wandrson/9995991 to your computer and use it in GitHub Desktop.
Save wandrson/9995991 to your computer and use it in GitHub Desktop.
A short Arduino Due sketch that streams 32-bit random numbers using the TRNG component on the ARM processor. Developed to test the validity of the entropy produced as part of updating my Arduino Entropy library to make use of this source, if used on a Due chip.
// DUE's TRNG 32 bits every 84 ticks
#include <sam.h>
#include <sam3xa/include/component/component_trng.h>
uint32_t test_hwrng()
{
static bool enabled = false;
if (!enabled) {
pmc_enable_periph_clk(ID_TRNG);
TRNG->TRNG_IDR = 0xFFFFFFFF;
TRNG->TRNG_CR = TRNG_CR_KEY(0x524e47) | TRNG_CR_ENABLE;
enabled = true;
}
while (! (TRNG->TRNG_ISR & TRNG_ISR_DATRDY))
;
return TRNG->TRNG_ODATA;
}
void setup() {
Serial.begin(9600); // I use 9600 baud to maintain consistency for my Entropy library testing procedures
randomSeed(test_hwrng()); // Just to show how the test_hwrng() function could be used for seeding PRNG
}
void loop() {
Serial.println(test_hwrng());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment