libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
rng_common_v1.c
Go to the documentation of this file.
1/** @addtogroup rng_file RNG peripheral API
2 * @ingroup peripheral_apis
3 *
4 * This library supports "version 1" of the random number generator
5 * peripheral (RNG) in the STM32 series of ARM Cortex Microcontrollers
6 * by ST Microelectronics. This is a common peripheral available on multiple
7 * devices in the family.
8 *
9 * LGPL License Terms @ref lgpl_license
10 */
11
12/*
13 * This file is part of the libopencm3 project.
14 *
15 * This library is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License as published by
17 * the Free Software Foundation, either version 3 of the License, or
18 * (at your option) any later version.
19 *
20 * This library is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Lesser General Public License for more details.
24 *
25 * You should have received a copy of the GNU Lesser General Public License
26 * along with this library. If not, see <http://www.gnu.org/licenses/>.
27 */
28
30
31/**@{*/
32
33/** Disable the Random Number Generator peripheral.
34*/
35void rng_disable(void)
36{
37 RNG_CR &= ~RNG_CR_RNGEN;
38}
39
40/** Enable the Random Number Generator peripheral.
41*/
42void rng_enable(void)
43{
45}
46
47/** Enable the Random Number Generator error interrupt.
48*/
50{
52}
53
54/** Disable the Random Number Generator error interrupt.
55*/
57{
58 RNG_CR &= ~RNG_CR_IE;
59}
60
61/** Randomizes a number (non-blocking).
62 * Can fail if a clock error or seed error is detected. Consult the Reference
63 * Manual, but "try again", potentially after resetting the peripheral
64 * @param rand_nr pointer to a uint32_t that will be randomized.
65 * @returns true on success, pointer is only written to on success
66 * @sa rng_get_random_blocking
67 */
68bool rng_get_random(uint32_t *rand_nr)
69{
70 /* Check for errors */
71 if (RNG_SR & (RNG_SR_CECS | RNG_SR_SECS)) {
72 return false;
73 }
74
75 /* data ready */
76 if (!(RNG_SR & RNG_SR_DRDY)) {
77 return false;
78 }
79
80 *rand_nr = RNG_DR;
81
82 return true;
83}
84
85
86/**
87 * Get a random number and block until it works.
88 * Unless you have a clock problem, this should always return "promptly"
89 * If you have a clock problem, you will wait here forever!
90 * Check device RM for clock requirements (usually fRNGCLK > fHCLK/16 or
91 * fRNGCLK > fHCLK/32
92
93 * @returns a random 32bit number
94 */
96{
97 uint32_t rv;
98 bool done;
99 do {
100
101 if (RNG_SR & RNG_SR_SEIS) {
102 RNG_SR = RNG_SR & ~RNG_SR_SEIS;
103 for (int i = 12; i != 0; i--) {
104 rv = RNG_DR;
105 }
106 RNG_CR &= ~RNG_CR_RNGEN;
108 }
109
110 if (RNG_SR & RNG_SR_CEIS) {
111 RNG_SR = RNG_SR & ~RNG_SR_CEIS;
112 }
113
114 done = rng_get_random(&rv);
115 } while (!done);
116
117 return rv;
118}
119
120
121/**@}*/
#define RNG_SR
Definition: rng_common_v1.h:42
#define RNG_CR_IE
Definition: rng_common_v1.h:53
void rng_enable(void)
Enable the Random Number Generator peripheral.
Definition: rng_common_v1.c:42
#define RNG_SR_CEIS
Definition: rng_common_v1.h:67
void rng_disable(void)
Disable the Random Number Generator peripheral.
Definition: rng_common_v1.c:35
#define RNG_SR_CECS
Definition: rng_common_v1.h:61
#define RNG_SR_DRDY
Definition: rng_common_v1.h:58
#define RNG_SR_SECS
Definition: rng_common_v1.h:64
#define RNG_CR_RNGEN
Definition: rng_common_v1.h:50
void rng_interrupt_enable(void)
Enable the Random Number Generator error interrupt.
Definition: rng_common_v1.c:49
bool rng_get_random(uint32_t *rand_nr)
Randomizes a number (non-blocking).
Definition: rng_common_v1.c:68
#define RNG_CR
Definition: rng_common_v1.h:39
#define RNG_SR_SEIS
Definition: rng_common_v1.h:70
uint32_t rng_get_random_blocking(void)
Get a random number and block until it works.
Definition: rng_common_v1.c:95
void rng_interrupt_disable(void)
Disable the Random Number Generator error interrupt.
Definition: rng_common_v1.c:56
#define RNG_DR
Definition: rng_common_v1.h:45