Pages: [1]   Go Down
Author Topic: random number generator (TRNG) API ?  (Read 2588 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 9
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
// 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);
}
Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 70
Posts: 2171
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Forum Administrator
Milano, Italy
Offline Offline
Sr. Member
*****
Karma: 23
Posts: 292
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Code:
pmc_enable_periph_clk(ID_TRNG);

another fact is that the structure Trng that you instatiate here:

Code:
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).


Logged

C.

Offline Offline
Jr. Member
**
Karma: 9
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

...
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
Logged

Forum Administrator
Milano, Italy
Offline Offline
Sr. Member
*****
Karma: 23
Posts: 292
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-wink
Logged

C.

Germany
Offline Offline
Full Member
***
Karma: 10
Posts: 230
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
        TRNG->TRNG_IDR = 0xFFFFFFFF;
}

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

Offline Offline
Jr. Member
**
Karma: 9
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
        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
Code:
     TRNG->TRNG_IDR = TRNG_IDR_DATRDY; // disable interrupts
Logged

Forum Administrator
Milano, Italy
Offline Offline
Sr. Member
*****
Karma: 23
Posts: 292
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

C.

Pages: [1]   Go Up
Jump to: