This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Random number without softdevice

Hi,

I am working with nRF51 for my application. I do not use softdevices. I need to have a random variable in my code. I have read Reference Manual and I have found that random numbers can be made by RNG. Some registers are described in documentation for the RNG, but nothing more. I do not know how to use them in order to give a random value to my variable in main. Can anybody help me with that?

Parents
  • The RNG is probably one of the most simple peripherals to run on the nRF5s. Here's the way we did it (bare metal) to generate a random number.

    From the functional description in the nRF51 Reference Manual

    The RNG is started by triggering the START task and stopped by triggering the STOP task. When started, new random numbers are generated continuously and written to the VALUE register when ready. A VALRDY event is generated for every new random number that is written to the VALUE register. This means that after a VALRDY event is generated the CPU has the time until the next VALRDY event to read out the random number from the VALUE register before it is overwritten by a new random number

    Basically, start the RNG, wait for the VALRDY, stop the RNG, and read out the random number. It's really, really simple.

    uint8_t GenerateRandomNumber()
    {
       NRF_RNG_VALRDY = 0;
       NRF_RNG->TASKS_START = 1;
       while(NRF_RNG->VALRDY == 0){}
       NRF_RNG->TASKS_STOP = 1;
       return NRF_RNG->VALUE;
    }
    
  • Thanks for posting. I think that may come in handy, albeit I could just use the driver in my case ;-)

Reply Children
No Data
Related