Library 22- True random number generator for STM32F4
STM32F4 devices (in most cases) have True Random Number Generator (or RNG). This peripheral can provide 32bit random number. I made a small library, that you can enable and use it very quickly.
RNG on STM32F4 is based on analog circuitry. It makes analog noise and that noice is connected to linear shift register. Analog circuitry is designed from ring oscillators whose outputs are XORed. For RNG circuitry is also separate LFSR clock and is independent from System clock. This allows RNG to be independent from system clock between different STM32F4 devices.
RNG Library
Features
- True random number generator
- Supports 32-bit numbers
Dependencies
- CMSIS
- STM32F4xx
- STM32F4xx RCC
- STM32F4xx RNG
- TM
- defines.h
- defines.h
How to use it
After you set up your files, include it into your project and use function to initialize RNG peripheral:
1 2 3 4 5 6 |
/** * Initialize RNG * * */ extern void TM_RNG_Init(void); |
After you set it up, you are ready to use. When you need your number, just call function below:
1 2 3 4 5 6 |
/** * Get 32bit random number * * Returns 32bit random number */ extern uint32_t TM_RNG_Get(void); |
Example
Bottom example produces this
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 45 46 47 |
/** * Keil project for Random Number Generator * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @packs STM32F4xx Keil packs version 2.2.0 or greater required * @stdperiph STM32F4xx Standard peripheral drivers version 1.4.0 or greater required */ /* Include core modules */ #include "stm32f4xx.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32f4_delay.h" #include "tm_stm32f4_usart.h" #include "tm_stm32f4_rng.h" #include <stdio.h> int main(void) { char buffer[20]; /* Initialize system */ SystemInit(); /* Initialize delay */ TM_DELAY_Init(); /* Initialize USART1, TX: PB6 */ TM_USART_Init(USART1, TM_USART_PinsPack_2, 115200); /* Initialize random number generator */ TM_RNG_Init(); while (1) { /* Get and format random number */ sprintf(buffer, "Number: %u\n", TM_RNG_Get()); /* Put to USART */ TM_USART_Puts(USART1, buffer); /* Delay 1 second */ Delayms(1000); } } |
Project available on my Github account, download library below.
True random number generator for STM32F4xx devices
Recent comments