aboutsummaryrefslogtreecommitdiffstats
path: root/src/random.c
blob: 9c1e0d1321e79b07396fd581e3c63d73aa2f9c21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <random.h>

#include <stm32l476xx.h>
#include <clock.h>

void random_init(void)
{
	// setup and enable RNG clock
	RCC->CCIPR &= ~(RCC_CCIPR_CLK48SEL);
	RCC->CCIPR |= RCC_CCIPR_CLK48SEL_1;
	RCC->AHB2ENR |= RCC_AHB2ENR_RNGEN;
	RNG->CR |= RNG_CR_RNGEN;
}

uint32_t random_get(void)
{
	if (RNG->SR & (RNG_SR_SEIS | RNG_SR_CEIS))
		return 0;

	while (!(RNG->SR & RNG_SR_DRDY))
		delay(1);

	return RNG->DR;
}