libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
RNG peripheral API

This library supports "version 1" of the random number generator peripheral (RNG) in the STM32 series of ARM Cortex Microcontrollers by ST Microelectronics. More...

Collaboration diagram for RNG peripheral API:

Macros

#define RNG_CR   MMIO32(RNG_BASE + 0x00)
 
#define RNG_SR   MMIO32(RNG_BASE + 0x04)
 
#define RNG_DR   MMIO32(RNG_BASE + 0x08)
 
#define RNG_CR_RNGEN   (1 << 2)
 
#define RNG_CR_IE   (1 << 3)
 
#define RNG_SR_DRDY   (1 << 0)
 
#define RNG_SR_CECS   (1 << 1)
 
#define RNG_SR_SECS   (1 << 2)
 
#define RNG_SR_CEIS   (1 << 5)
 
#define RNG_SR_SEIS   (1 << 6)
 

Functions

void rng_enable (void)
 Enable the Random Number Generator peripheral. More...
 
void rng_disable (void)
 Disable the Random Number Generator peripheral. More...
 
void rng_interrupt_enable (void)
 Enable the Random Number Generator error interrupt. More...
 
void rng_interrupt_disable (void)
 Disable the Random Number Generator error interrupt. More...
 
bool rng_get_random (uint32_t *rand_nr)
 Randomizes a number (non-blocking). More...
 
uint32_t rng_get_random_blocking (void)
 Get a random number and block until it works. More...
 

Detailed Description

This library supports "version 1" of the random number generator peripheral (RNG) in the STM32 series of ARM Cortex Microcontrollers by ST Microelectronics.

This is a common peripheral available on multiple devices in the family.

LGPL License Terms libopencm3 License

Macro Definition Documentation

◆ RNG_CR

#define RNG_CR   MMIO32(RNG_BASE + 0x00)

Definition at line 39 of file rng_common_v1.h.

◆ RNG_CR_IE

#define RNG_CR_IE   (1 << 3)

Definition at line 53 of file rng_common_v1.h.

◆ RNG_CR_RNGEN

#define RNG_CR_RNGEN   (1 << 2)

Definition at line 50 of file rng_common_v1.h.

◆ RNG_DR

#define RNG_DR   MMIO32(RNG_BASE + 0x08)

Definition at line 45 of file rng_common_v1.h.

◆ RNG_SR

#define RNG_SR   MMIO32(RNG_BASE + 0x04)

Definition at line 42 of file rng_common_v1.h.

◆ RNG_SR_CECS

#define RNG_SR_CECS   (1 << 1)

Definition at line 61 of file rng_common_v1.h.

◆ RNG_SR_CEIS

#define RNG_SR_CEIS   (1 << 5)

Definition at line 67 of file rng_common_v1.h.

◆ RNG_SR_DRDY

#define RNG_SR_DRDY   (1 << 0)

Definition at line 58 of file rng_common_v1.h.

◆ RNG_SR_SECS

#define RNG_SR_SECS   (1 << 2)

Definition at line 64 of file rng_common_v1.h.

◆ RNG_SR_SEIS

#define RNG_SR_SEIS   (1 << 6)

Definition at line 70 of file rng_common_v1.h.

Function Documentation

◆ rng_disable()

void rng_disable ( void  )

Disable the Random Number Generator peripheral.

Definition at line 35 of file rng_common_v1.c.

References RNG_CR.

◆ rng_enable()

void rng_enable ( void  )

Enable the Random Number Generator peripheral.

Definition at line 42 of file rng_common_v1.c.

References RNG_CR, and RNG_CR_RNGEN.

◆ rng_get_random()

bool rng_get_random ( uint32_t *  rand_nr)

Randomizes a number (non-blocking).

Can fail if a clock error or seed error is detected. Consult the Reference Manual, but "try again", potentially after resetting the peripheral

Parameters
rand_nrpointer to a uint32_t that will be randomized.
Returns
true on success, pointer is only written to on success
See also
rng_get_random_blocking

Definition at line 68 of file rng_common_v1.c.

References RNG_DR, RNG_SR, RNG_SR_CECS, RNG_SR_DRDY, and RNG_SR_SECS.

Referenced by rng_get_random_blocking().

Here is the caller graph for this function:

◆ rng_get_random_blocking()

uint32_t rng_get_random_blocking ( void  )

Get a random number and block until it works.

Unless you have a clock problem, this should always return "promptly" If you have a clock problem, you will wait here forever! Check device RM for clock requirements (usually fRNGCLK > fHCLK/16 or fRNGCLK > fHCLK/32

Returns
a random 32bit number

Definition at line 95 of file rng_common_v1.c.

References RNG_CR, RNG_CR_RNGEN, RNG_DR, rng_get_random(), RNG_SR, RNG_SR_CEIS, and RNG_SR_SEIS.

Here is the call graph for this function:

◆ rng_interrupt_disable()

void rng_interrupt_disable ( void  )

Disable the Random Number Generator error interrupt.

Definition at line 56 of file rng_common_v1.c.

References RNG_CR.

◆ rng_interrupt_enable()

void rng_interrupt_enable ( void  )

Enable the Random Number Generator error interrupt.

Definition at line 49 of file rng_common_v1.c.

References RNG_CR, and RNG_CR_IE.