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

sd_rand_application_vector_get() crashes system

My code:


uint32_t err_code;
uint8_t randomBytes[6];

err_code = sd_rand_application_vector_get(randomBytes, sizeof(randomBytes));

When this function is called my system goes off in the weeds. Anyone else have this problem?

=]

  • Try something like this:

    ```   
        uint8_t randomBytes[6] = {0,0,0,0,0,0};
        uint8_t bytes_available = 0;
                
        err_code = sd_rand_application_bytes_available_get(&bytes_available);
        APP_ERROR_CHECK(err_code);
        bytes_available = (bytes_available > sizeof(randomBytes) ? sizeof(randomBytes) : bytes_available);
               
        if (bytes_available)
        {
            err_code = sd_rand_application_vector_get(randomBytes, bytes_available);        
            APP_ERROR_CHECK(err_code);
            randomBytes[0] = randomBytes[0];        // Debugging purposes.
        }
    
    ```
    

    Note that if you debug and halt on a breakpoint while the softdevice is active, it will get a timing error internally and assert (remember, you halt the MCU, not the peripherals).

    *edit: formatting, and do remember to check error codes :-)

    Best regards Håkon

  • Hey, that works! I'm a little confused why your code works and calling the vector function does not - seems like a softdevice API implementation bug to me. =]

    And yes I have found halting the MCU to upset the softdevice. Is there a way to halt the peripherals upon a breakpoint using debugger settings?

    =]

  • My guess is you were asking for more bytes than were available in the buffer. The code Håkon provided makes sure you never ask for more bytes than what is available.

  • @Torbjorn: I can see how that might be but when I call sd_rand_application_vector_get() by itself it should return a value to err_code. The function does not return which is confusing, do you know why this is? Must I always call sd_rand_application_bytes_available_get() before sd_rand_application_vector_get()?

    Power usage is a major component of our design, so any way to reduce execution time is desired.

    =]