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

How to use sd_rand_application ?? or use Random number with NRF51 ?

I've tried to use stdlib's random generate function.

and as usally all of the basic C language's random generate functions does, I've used srand with Seed based random. (And also, used with time(null) argument like this.)

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
srand(time(null);
rand();

and when I coded like this, and when I launch compile, it keeps failure with

._build\nrf51422_xxac.axf: Error: L6218E: Undefined symbol time (referred from main.o). (Why on earth this error occurs??? I've double checked with my time.h header file exists correctly!!)

So I've just give up using rand function and find for Random function.

Seems to be there are two random functions.

RNG function seems to be implement total Random Function just like Linux /dev/random does. (And It needs for NRF51 core's compute power. -But actually I don't need Random that seriously.-)

So I thought there is an alternative solution for random application, and it seems to be softdevice function called sd_rand_application_vector_get.

But Actually I can't find any appropriate document for this function. Is there any document for this function's usage? There are only defines for this function, and no any other explanation about this.

Does this function needs for NRF_RNG_Driver enabled? do I have to include nrf_drv_rng.c to use this function?

Can Anyone help me with usage of this function? Is sd_rand_application has to be initialized to use?

(*ps.

    uint8_t num_rand_bytes_available;
err_code = sd_rand_application_bytes_available_get(&num_rand_bytes_available);
APP_ERROR_CHECK(err_code);
uint8_t rand_number;
if (num_rand_bytes_available > 0)
{
    err_code = sd_rand_application_vector_get(&rand_number, 1);
    APP_ERROR_CHECK(err_code);
}

I've used this code but, It doesn't generate any random number even if I turned the device off and on, or reset button. The first random number fixed and doesn't change at all)

Parents
  • Hi,

    Q1: But Actually I can't find any appropriate document for this function. Is there any document for this function's usage?

    A1: You can find the API documentation here.


    Q2: Does this function needs for NRF_RNG_Driver enabled? do I have to include nrf_drv_rng.c to use this function?

    A2: This is a SoftDevice API function, you don’t need to enable NRF_RNG_Driver. And you don’t need to include nrf_drv_rng.c. You only need to have enabled the SoftDevice.


    Your code looks correct, but if you call the function very close to the time you enable the SoftDevice, the SoftDevice hasn't had enough time to fill the buffer of random numbers. It takes some time after the SoftDevice is enabled to the buffer with random numbers is filled. Wait a couple of milliseconds, and call sd_rand_application_bytes_available_get(..) again until you have available bytes in the pool.

  • Thanks! really, But It would be really helpful if API Documentation's explanation gets little bit more specific.

    Such as It would need some time to full random numbers in buffers, so User might to wait little bit before calling sd_rand_application_bytes_available_get(...).

Reply Children
No Data
Related