danieleff / STM32GENERIC

Generic implementation of Arduino for STM32 boards
https://danieleff.github.io/STM32GENERIC/
116 stars 88 forks source link

STM32F7 TRNG #31

Closed jlswbs closed 6 years ago

jlswbs commented 7 years ago

STM32F7 TRNG not working rand() or random() produce same sequence after reset

danieleff commented 6 years ago

rand() http://www.cplusplus.com/reference/cstdlib/rand/ is a pseudorandom number generator, and will produce the same sequence by design.

There is no Arduino API for true random numbers, but the STM32 API is pretty short:

//Initialize once:
__HAL_RCC_RNG_CLK_ENABLE();
RNG_HandleTypeDef handle = {0};
handle.Instance = RNG;
HAL_RNG_Init(&handle);

//Get:
uint32_t random = HAL_RNG_GetRandomNumber(&handle )
jlswbs commented 6 years ago

Many thanks code working great.