Skip to content

Commit

Permalink
Add soft device rng portion to MicroBitDevice::seedRandom().
Browse files Browse the repository at this point in the history
Otherwise, calling this function when BLE is running results
in 0xBBC5EED as always being used as the seed.
  • Loading branch information
microbit-carlos committed Sep 13, 2024
1 parent bd446c4 commit e39ab00
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion source/MicroBitDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,19 @@ void MicroBitDevice::seedRandom()
{
uint32_t r = 0xBBC5EED;

if(!ble_running())
if (ble_running())
{
// If Bluetooth is enabled, we need to go through the Nordic software to safely do this.
uint8_t available_bytes = 0;
while (available_bytes < 4)
sd_rand_application_bytes_available_get(&available_bytes);

uint32_t random_value = 0;
uint32_t result = sd_rand_application_vector_get((uint8_t*)&random_value, sizeof(random_value));
if (result == NRF_SUCCESS)
r = random_value;
}
else
{
// Start the Random number generator. No need to leave it running... I hope. :-)
NRF_RNG->TASKS_START = 1;
Expand Down

0 comments on commit e39ab00

Please sign in to comment.