Skip to content

Commit e39ab00

Browse files
Add soft device rng portion to MicroBitDevice::seedRandom().
Otherwise, calling this function when BLE is running results in 0xBBC5EED as always being used as the seed.
1 parent bd446c4 commit e39ab00

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

source/MicroBitDevice.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,19 @@ void MicroBitDevice::seedRandom()
6868
{
6969
uint32_t r = 0xBBC5EED;
7070

71-
if(!ble_running())
71+
if (ble_running())
72+
{
73+
// If Bluetooth is enabled, we need to go through the Nordic software to safely do this.
74+
uint8_t available_bytes = 0;
75+
while (available_bytes < 4)
76+
sd_rand_application_bytes_available_get(&available_bytes);
77+
78+
uint32_t random_value = 0;
79+
uint32_t result = sd_rand_application_vector_get((uint8_t*)&random_value, sizeof(random_value));
80+
if (result == NRF_SUCCESS)
81+
r = random_value;
82+
}
83+
else
7284
{
7385
// Start the Random number generator. No need to leave it running... I hope. :-)
7486
NRF_RNG->TASKS_START = 1;

0 commit comments

Comments
 (0)