we never really needed real random numbers. The reason is that we use
rand() only in our radio stack to repeat the transmission.
There are more solutions where to get random data based on your requirements.
MCU random generator
The MCU has this peripheral, but we haven’t support for it in SDK. But you can still use STM32 HAL library which is not used now but it is still included in the SDK.
Create global structure in the top of
application.c for RNG
application_init() enable clock and peripheral and read value.
rnd.Instance = RNG;
application_task() loop I have this. Note that RNG needs to enable PLL at least during number generation.
int ret = HAL_RNG_GenerateRandomNumber(&rnd, &rng);
bc_log_debug("APP: ret %d, %lu", ret, rng);
And here are random values
0.50 <D> APP: ret 0, 1868780179
1.00 <D> APP: ret 0, 1768378975
1.50 <D> APP: ret 0, 1807353431
2.00 <D> APP: ret 0, 67176906
The ATSHA204 cryptochip and RNG
This would be the best solution, but you need to write some nonce value and then lock the chip EEPROM forever if I understood it right. Since we are not using this chip yet we did not decided to lock it so the generator should return just some fixed data 0xFFFF0000 according to datasheet. http://ww1.microchip.com/downloads/en/devicedoc/Atmel-8740-CryptoAuth-ATSHA204-Datasheet.pdf
If you or someone needs real radnomness, this would be the best solution.
I tried now the ADC and I’m not convinced that these data are “noisy” enough. But you can try it on your module and maybe there will be difference.
bc_adc_init(); to the
application_init() and then this is my task:
bc_log_debug("APP: %f, %d", voltage, res);
This is result, not too much randomness in it
0.50 <D> APP: 1.227206, 8320
1.00 <D> APP: 1.227206, 3952
1.50 <D> APP: 1.227206, 3888
2.00 <D> APP: 1.227206, 3936
2.50 <D> APP: 1.227206, 3936
3.00 <D> APP: 1.227206, 3936
3.50 <D> APP: 1.227206, 3952
4.00 <D> APP: 1.227206, 3808
4.50 <D> APP: 1.227206, 3904
5.00 <D> APP: 1.227206, 3936
5.50 <D> APP: 1.227206, 3920
6.00 <D> APP: 1.227206, 3904
6.50 <D> APP: 1.227206, 3920
MCU has also internal temperature sensor but we haven’t support for it in SDK. It is connected to the ADC
Combine somehow all three axes (XORing?) and then hash it and use the number. In the accelerometer there is also internal temperature sensor which could be used, but this has no support in SDK and you need to set or read some I2C registers inside.
TMP122 temperature sensor
This sensor is on every Core Module and could be used to seed randomness.