17. Random number generator (RNG)
The RNG is a random number generator based on a continuous analog noise that provides a 16-bit value to the host when read.
17.1 Features
- • AHB slave peripheral
- • Deliver 16-bit random number produced by an analog generator
- • Minimum period of 1.25 μs (corresponding to 20 RNGCLK cycles) between two consecutive random number. This is automatically managed by a pulling spacer counter that adds wait-state on the AHB bus when reading occurs too closely to the previous one.
- • Monitoring of the entropy of the RNG to flag abnormal behavior (generation of stable values or stable sequence of values)
- • 2 clock domains:
- – RNGCLK: 16 MHz for the normalization and shifter (specific serial to 16-bit parallel conversion)
- – HCLK: AHB clock (16 MHz, 32 MHz or 64 MHz) for the AHB interface
- • Can be disabled to reduce power consumption:
Power consumption and RNG:
The internal free-running oscillators are quite power consuming. It is possible to stop them when the RNG is not used.
- • After a PORESETn, the internal free-running oscillators are stopped by default to limit consumption.
- • When the RNG clock tree is enabled through the RCC after a PORESETn, the oscillators are automatically restarted.
- • If the SW gate the RNG clock tree through the RCC, it still not stop the internal oscillator. The SW has to first set the RNG_CR[2] = RNG_DIS to stop them.
- • On the other side, the SW has to restart them by clearing the RNG_DIS bit once the RNG clock tree is re-enabled.
17.2 RNG registers map
Refer to Section 2.2.2: Memory map and register boundary addresses for the register boundary addresses.
Note: Despite RNG registers are addressed through AHB, only 32-bit accesses are allowed. Any 8-bit or 16-bit access generate an AHB error leading to a hard fault on Cortex-M0+.
Table 45. RNG register list
| Address offset | Name | RW(1) | Reset | Description |
|---|---|---|---|---|
| 0x00 | RNG_CR | RW | 0x00000000 | RNG Configuration register. See Table 46: RNG_CR register description on page 335 |
| 0x04 | RNG_SR | RO | 0x00000000 | RNG Status register. See Table 47: RNG_SR register description on page 336 |
| 0x08 | RNG_VAL | RO | 0x00000000 | RNG 16-bit random value. See Table 48: RNG_VAL register description on page 336 |
1. These acronyms have the following meaning:
RW = Read and Write
RO = Read Only
17.3 RNG registers description
RNG_CR
This register configures the RNG.
Table 46. RNG_CR register description
| Address: RNG_BASE + 0x00 | ||||
|---|---|---|---|---|
| Bit | Field name | Reset | R/W | Description |
| 1:0 | RESERVED | 2'h0 | RW | RESERVED |
| 2 | RNG_DIS | 1'h0 | RW | RNG Disable bit. This bit enables or disables the random number generator. – 0: RNG is enable (default) – 1: RNG is disabled. The internal free-running oscillators are put in power-down mode and the RNG clock is stopped at the input of the block. |
| 3 | TST_CLK | 1'h0 | RW | RNG Test Clock bit. Writing this bit with 1b starts the logic that detects the presence of the RNG_CLK. Then wait (with a timeout of at least four RNGCLK cycles) for REVCLK = 1b in RNG_SR register. If REVCLK = 0b after timeout elapsed, it means that RNGCLK is not present and reading RNG_VAL register triggers an AHB error response. For security reason, software should check before reading random values that the RNGCLK is present. This bit is auto-cleared and always read as 0. |
| 31:4 | RESERVED | 28'h0000000 | R | RESERVED |
RNG_SR
This register provides status flags of the RNG.
Table 47. RNG_SR register description
| Address: RNG_BASE + 0x04 | ||||
|---|---|---|---|---|
| Bit | Field name | Reset | R/W | Description |
| 0 | RNGRDY | 1'h0 | R | New Random Value Ready. – 0: the RNG_VAL register value is not yet valid. If performing a read access to RNG_VAL, the host is put on hold (by wait-states insertion on the AHB bus) until a random value is available. – 1: the RNG_VAL register contains a valid random number. This bit remains at 0b when the RNG is disabled (RNGDIS bit = 1b in RNG_CR) |
| 1 | REVCLK | 1'h0 | R | RNGCLK Clock Reveal bit. A write with 1b to bit TSTCLK in RNG_CR resets this bit. If the RNGCLK is present, this bit is 1b after four RNGCLK cycles after the end of the write to RNG_CR. If REVCLK = 0b after this period, it means the RNGCLK is not present and reading RNG_VAL triggers a AHB error response. |
| 2 | FAULT | 1'h0 | RC_W1 | Fault Reveal bit. This bit is set by hardware when a faulty sequence of bits occurs. The faulty sequences are: – sequence of more than 32 consecutive bits of same value (0b or 1b) – sequence of more than 16 consecutive alternation of 0b and 1b (010101...01b). Writing this bit with 1b clear it. Writing with 0b has no effect. |
| 31:3 | RESERVED | 29'h00000000 | R | RESERVED |
RNG_VAL
This register delivers a 16-bit random value when read. After being read, this register delivers a new random value only after 20 cycles of RNGCLK. If the host performs a new read before the period has elapsed, the RNG inserts a wait-state on the AHB bus.
Table 48. RNG_VAL register description
| Address: RNG_BASE + 0x08 | ||||
|---|---|---|---|---|
| Bit | Field name | Reset | R/W | Description |
| 15:0 | RANDOM_VALUE | 16'h---- | R | Random Value. |
| 31:16 | RESERVED | 16'h0000 | R | RESERVED |