cpu/nrf5x_common: map hwrng to SoC library if SoftDevice is present

This commit is contained in:
PeterKietzmann 2019-05-21 12:01:42 +02:00
parent e8650f5b9a
commit 7ee9905fa6

View File

@ -24,12 +24,20 @@
#include "cpu.h" #include "cpu.h"
#include "periph/hwrng.h" #include "periph/hwrng.h"
#ifndef MODULE_NORDIC_SOFTDEVICE_BLE
#include "assert.h"
#endif
void hwrng_init(void) void hwrng_init(void)
{ {
/* nothing to do here */ /* 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) void hwrng_read(void *buf, unsigned int num)
{ {
unsigned int count = 0; unsigned int count = 0;
@ -62,3 +70,24 @@ void hwrng_read(void *buf, unsigned int num)
NRF_RNG->POWER = 0; NRF_RNG->POWER = 0;
#endif #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 */