gadget-securevault/random.c
Daniele Lacamera dee9335db4 Initial import
2019-11-02 12:59:53 +01:00

34 lines
773 B
C

/*
* (c) danielinux 2019
* GPLv.2
*
* See LICENSE for details
*/
#include "system.h"
#include <stdint.h>
uint32_t random_uint32(void)
{
int i;
uint32_t rand;
/* enable RNG peripheral clock */
AHB2_CLOCK_ER |= RNG_AHB2_CLOCK_ER;
/* enable RNG interrupt, set IE bit in RNG->CR register */
RNG_CR |= RNG_CR_IE;
/* enable RNG, set RNGEN bit in RNG->CR. Activates RNG,
* RNG_LFSR, and error detector */
RNG_CR |= RNG_CR_RNGEN;
/* verify no errors, make sure SEIS and CEIS bits are 0
* in RNG->SR register */
if (RNG_SR & (RNG_SR_SECS | RNG_SR_CECS))
return 0;
/* wait until RNG number is ready */
while ((RNG_SR & RNG_SR_DRDY) == 0)
;
/* get value */
rand = RNG_DR;
return rand;
}