aboutsummaryrefslogtreecommitdiffstats
path: root/src/random.c
blob: 002b86297d8b672d5734e82bb30c6d578f2b0780 (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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
 * @file random.c
 * Provides true random number generation functionality
 *
 * Copyright (C) 2018 Clyne Sullivan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#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;
}