Processor has TRNG (datasheet chapter 43), but I don't see any API yet, and I presume it's not incorporated in random/rand.
Has anyone with a board tried it? There is a Trng struct defined in arduino-1.5/hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/include/component/component_trng.h, though I couldn't figure out how to reference it in sketch, so I copied and hacked it to get the following little sketch to compile, but I don't have a board yet to know if this works ...
// DUE's TRNG 32 bits every 84 ticks
//#include <component/component_trng.h>
#include "component_trng.h"
Trng *trng;
void setup() {
trng = (Trng *) 0x400BC000;
trng->TRNG_CR = TRNG_CR_KEY(0x524e47) | TRNG_CR_ENABLE;
Serial.begin(9600);
}
void loop() {
unsigned int rng;
while (! (trng->TRNG_ISR & TRNG_ISR_DATRDY)); //wait til data ready
rng = trng->TRNG_ODATA;
Serial.println(rng,HEX);
delay(3000);
}
(almost) all the devices inside SAM3X are clocked through a Clock Controller called PMC. Basically you need to start clocking each device before starting using it.
This is done throught the function:
pmc_enable_periph_clk(ID_TRNG);
another fact is that the structure Trng that you instatiate here:
trng = (Trng *) 0x400BC000;
is already available from CMSIS as TRNG. In general i see that registers structures are named using camel case (struct Trng) while actual instances of such structures are capital case (TRNG for example but some has numbering if there are many like TWI0 / TWI1).
cmaglie:
...
is already available from CMSIS as TRNG. In general i see that registers structures are named using camel case (struct Trng) while actual instances of such structures are capital case (TRNG for example but some has numbering if there are many like TWI0 / TWI1).
You should find some documentation already inside arduino distribution here:
hardware/arduino/sam/system/CMSIS/CMSIS/
maybe a google search for "CMSIS documentation" would do the job.
But in the end... I don't hide you that i gathered much information looking directly to the header files
In this case we have only one used bit in the register, so its easy, but sometime we have registers with dozen used bits, and i use 0xFFFFFFFF to avoid an infinite chain of OR'ed constants.
In this case, maybe, we can also avoid to disable the interrupts at all (since the default is disabled and we never turn it on).
C