diff --git a/cpu/nrf5x_common/periph/hwrng.c b/cpu/nrf5x_common/periph/hwrng.c index 5bdc6b8346e6..5510c080be20 100644 --- a/cpu/nrf5x_common/periph/hwrng.c +++ b/cpu/nrf5x_common/periph/hwrng.c @@ -24,12 +24,20 @@ #include "cpu.h" #include "periph/hwrng.h" +#ifndef MODULE_NORDIC_SOFTDEVICE_BLE +#include "assert.h" +#endif void hwrng_init(void) { /* nothing to do here */ } +/* + * The hardware peripheral is used by the SoftDevice. When the SoftDevice is + * enabled, it shall only be accessed through the SoftDevice API + */ +#ifndef MODULE_NORDIC_SOFTDEVICE_BLE void hwrng_read(void *buf, unsigned int num) { unsigned int count = 0; @@ -62,3 +70,24 @@ void hwrng_read(void *buf, unsigned int num) NRF_RNG->POWER = 0; #endif } + +#else + +void hwrng_read(void *buf, unsigned int num) +{ + uint32_t ret; + uint8_t avail; + + assert(num <= 0xff); + + /* this is not the most efficient, but this way we can assure that there are + * enough bytes of random data available */ + do { + sd_rand_application_bytes_available_get(&avail); + } while (avail < (uint8_t)num); + + ret = sd_rand_application_vector_get((uint8_t *)buf, (uint8_t)num); + assert(ret == NRF_SUCCESS); + (void)ret; +} +#endif /* MODULE_NORDIC_SOFTDEVICE_BLE */