HAL Library 18- RNG for STM32Fxxx
STM32Fxxx devices (in most cases) have True Random Number Generator (or RNG). This peripheral can provide 32-bits random number. I made a small library, that you can enable and use it very quickly which is compatible with HAL based drivers.
RNG on STM32Fxxx is based on analog circuitry. It makes analog noise and that noise 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 STM32Fxxx devices.
Library
Features
- True random number generator
- Supports 32-bit numbers
Dependencies
- HAL
- TM
- STM32Fxxx HAL
- defines.h
Functions and enumerations
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 |
/** * @defgroup TM_RNG_Functions * @brief Library Functions * @{ */ /** * @brief Initializes RNG peripheral and enables clock * @param None * @retval None */ void TM_RNG_Init(void); /** * @brief De initializes RNG peripheral and disables clock * @param None * @retval None */ void TM_RNG_DeInit(void); /** * @brief Gets 32-bit random number * @param None * @retval 32-bit random number */ uint32_t TM_RNG_Get(void); /** * @} */ |
Example
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 48 49 50 51 52 53 54 55 56 57 58 |
/** * Keil project example 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 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx/STM32F7xx Keil packs are requred with HAL driver support * @stdperiph STM32F4xx/STM32F7xx HAL drivers required */ /* Include core modules */ #include "stm32fxxx_hal.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32_disco.h" #include "tm_stm32_delay.h" #include "tm_stm32_rng.h" #include "tm_stm32_usart.h" /* Random value */ uint32_t randval; /* Format string */ char buff[100]; int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init delay */ TM_DELAY_Init(); /* Init USART6, TX: PC6, 921600 bauds */ TM_USART_Init(USART6, TM_USART_PinsPack_1, 921600); /* Init Random Number generator */ TM_RNG_Init(); while (1) { /* Get 32-bit random value */ randval = TM_RNG_Get(); /* Format string */ sprintf(buff, "Value: 0x%08X\n", randval); /* Send via USART */ TM_USART_Puts(USART6, buff); /* Delay */ Delayms(500); } } |
Project is available on Github, download all libraries below.
STM32 libraries based on STM32Fxxx HAL drivers.
Recent comments