random number generator (TRNG) API ?

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);
}

I can't answer your question, but thanks for pointing me to the system register definitions. Hadn't looked in the Devices folder of CMSIS.

mantoui, thanks i tried and it works... after a little tweak:

uint32_t trueRandom()
{
    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;
}

(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).

Excellent!!

Is there documentation for CMSIS ??

thanks

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 :wink:

cmaglie:
mantoui, thanks i tried and it works... after a little tweak:

        TRNG->TRNG_IDR = 0xFFFFFFFF;

}

Why the hole 32bit if I didn't read the spec wrong the first bit would be enough!?

Markus_L811:

cmaglie:
mantoui, thanks i tried and it works... after a little tweak:

        TRNG->TRNG_IDR = 0xFFFFFFFF;

}

Why the hole 32bit if I didn't read the spec wrong the first bit would be enough!?

defines let you do something like

     TRNG->TRNG_IDR = TRNG_IDR_DATRDY; // disable interrupts

Yup, you can do that way.

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