{"id":2263,"date":"2024-01-11T07:53:35","date_gmt":"2024-01-11T07:53:35","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=2263"},"modified":"2024-01-11T07:53:38","modified_gmt":"2024-01-11T07:53:38","slug":"working-with-stm32-and-random-number-generator","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=2263","title":{"rendered":"Working with STM32 and Random Number Generator"},"content":{"rendered":"\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"819\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/random_numbers-1024x819.jpg\" alt=\"\" class=\"wp-image-2264\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/random_numbers-1024x819.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/random_numbers-300x240.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/random_numbers-768x614.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/random_numbers-750x600.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/random_numbers-400x320.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/random_numbers-250x200.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/random_numbers.jpg 1110w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure><\/div>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall take a look at what is random number and how the STM32 uses hardware to generate true random numbers and develop the code for that.<\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is random number.<\/li><li>How STM32 generate random numbers.<\/li><li>Features of the RNG peripheral of STM32.<\/li><li>Developing the driver.<\/li><li>Results.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. What is Random Number:<\/h2>\n\n\n\n<p>A random number generator is a device or algorithm that can produce numbers that are unpredictable and have no discernible pattern. Random numbers are useful for many purposes, such as cryptography, gaming, simulation, statistics, and more. There are two main types of random number generators: true and pseudo-random. True random number generators use physical phenomena, such as atmospheric noise, to produce randomness. Pseudo-random number generators use mathematical formulas to create sequences of numbers that appear random, but are actually deterministic. \u00b9\u00b2\u00b3<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. How STM32 Generate Random Numbers:<\/h2>\n\n\n\n<p>The hardware random number generator (RNG) in STM32 microcontrollers typically exploits the inherent unpredictability in physical processes. It often relies on the thermal noise present in semiconductor devices.<\/p>\n\n\n\n<p>Here&#8217;s a simplified explanation:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>Thermal Noise:<\/strong> Microcontrollers naturally exhibit thermal noise due to temperature fluctuations at the microscopic level within the silicon.<\/li><li><strong>Analog-to-Digital Conversion (ADC):<\/strong> The RNG circuit samples this thermal noise using an Analog-to-Digital Converter (ADC). The continuous and unpredictable nature of thermal noise provides a source of randomness.<\/li><li><strong>Bit Extraction:<\/strong> The digitized noise is then processed to extract random bits. Various techniques, like Von Neumann extractor or more advanced algorithms, may be employed to ensure randomness and eliminate bias.<\/li><li><strong>Output:<\/strong> The generated random bits are then made available for use by software.<\/li><\/ol>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Features of the RNG Peripheral of STM32:<\/h2>\n\n\n\n<p>RNG main features<\/p>\n\n\n\n<p>\u2022 It delivers 32-bit random numbers, produced by an analog generator<\/p>\n\n\n\n<p>\u2022 40 periods of the RNG_CLK clock signal between two consecutive random numbers<\/p>\n\n\n\n<p>\u2022 Monitoring of the RNG entropy to flag abnormal behavior (generation of stable values, or of a stable sequence of values)<\/p>\n\n\n\n<p>\u2022 It can be disabled to reduce power consumption<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong><em>Note: Not all STM32F4 has the RNG peripheral, please refer to the datasheet of the desired MCU to see if the MCU supports RNG or not.<\/em><\/strong><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">4. Developing the Driver:<\/h2>\n\n\n\n<p>Before we develop the driver, first we need to configure the PLL to generate 48MHz needed for the 48MHz peripherals like SDIO and RNG as mentioned in the reference manual:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"202\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.34.53\u202fAM-1024x202.png\" alt=\"\" class=\"wp-image-2265\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.34.53\u202fAM-1024x202.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.34.53\u202fAM-300x59.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.34.53\u202fAM-768x151.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.34.53\u202fAM-1536x303.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.34.53\u202fAM-2048x403.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.34.53\u202fAM-1150x227.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.34.53\u202fAM-750x148.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.34.53\u202fAM-400x79.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.34.53\u202fAM-250x49.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Since we need to keep the core frequency at 16MHz using internal oscillator, we need to configure the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>PLL P.<\/li><li>PLL N.<\/li><li>PLL M.<\/li><li>PLL Q.<\/li><\/ul>\n\n\n\n<p><\/p>\n\n\n\n<p>From CubeMX clock configuration, we can find that PLL has the following configuration:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"384\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.38.39\u202fAM-1024x384.png\" alt=\"\" class=\"wp-image-2267\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.38.39\u202fAM-1024x384.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.38.39\u202fAM-300x113.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.38.39\u202fAM-768x288.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.38.39\u202fAM-1536x576.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.38.39\u202fAM-2048x768.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.38.39\u202fAM-1150x431.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.38.39\u202fAM-750x281.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.38.39\u202fAM-400x150.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.38.39\u202fAM-250x94.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<p>Hence, the following values:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>PLL M =16<\/li><li>PLL N = 192<\/li><li>PLL P  =2<\/li><li>PLL Q =4 <\/li><\/ul>\n\n\n\n<p>In main.c, we start by including the following two header files:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;stm32f4xx.h&quot;\n#include &quot;stdint.h&quot;<\/pre><\/div>\n\n\n\n<p>Next declare uint32_t variable to hold the generated random values:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint32_t random_number;<\/pre><\/div>\n\n\n\n<p>Next, define the PLL values:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#define PLL_M      16\n#define PLL_N      192\n#define PLL_P      2\n#define PLL_Q      4<\/pre><\/div>\n\n\n\n<p>In main function.<\/p>\n\n\n\n<p>Configure the PLL with the required parameters:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">    RCC-&gt;PLLCFGR = (PLL_M&lt;&lt;RCC_PLLCFGR_PLLM_Pos) | (PLL_N &lt;&lt; RCC_PLLCFGR_PLLN_Pos) | (((PLL_P &gt;&gt; 1) -1) &lt;&lt; RCC_PLLCFGR_PLLP_Pos) | \/\/set PLL_M,PLL_N,PLL_P\n             (PLL_Q &lt;&lt; RCC_PLLCFGR_PLLQ_Pos);<\/pre><\/div>\n\n\n\n<p>Enable the PLL and wait until it is enabled:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">    RCC-&gt;CR |= RCC_CR_PLLON; \/\/turn on the PLL\n\n\n    while((RCC-&gt;CR &amp; RCC_CR_PLLRDY) == 0); \/\/wait until PLL is active<\/pre><\/div>\n\n\n\n<p>Then we need to enable clock access to RNG peripheral:<\/p>\n\n\n\n<p>From the datasheet of STM32F407:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"384\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.43.54\u202fAM-1024x384.png\" alt=\"\" class=\"wp-image-2268\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.43.54\u202fAM-1024x384.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.43.54\u202fAM-300x113.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.43.54\u202fAM-768x288.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.43.54\u202fAM-1536x576.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.43.54\u202fAM-2048x768.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.43.54\u202fAM-1150x431.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.43.54\u202fAM-750x281.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.43.54\u202fAM-400x150.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.43.54\u202fAM-250x94.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>We can see that RNG is connected to AHB2 bus.<\/p>\n\n\n\n<p>Hence, we can enable clock it as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"384\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.46.02\u202fAM-1024x384.png\" alt=\"\" class=\"wp-image-2269\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.46.02\u202fAM-1024x384.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.46.02\u202fAM-300x113.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.46.02\u202fAM-768x288.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.46.02\u202fAM-1536x576.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.46.02\u202fAM-2048x768.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.46.02\u202fAM-1150x431.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.46.02\u202fAM-750x281.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.46.02\u202fAM-400x150.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.46.02\u202fAM-250x94.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">RCC-&gt;AHB2ENR|=RCC_AHB2ENR_RNGEN;<\/pre><\/div>\n\n\n\n<p>Next, enable the RNG:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"611\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.47.08\u202fAM-1024x611.png\" alt=\"\" class=\"wp-image-2270\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.47.08\u202fAM-1024x611.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.47.08\u202fAM-300x179.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.47.08\u202fAM-768x458.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.47.08\u202fAM-1536x916.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.47.08\u202fAM-2048x1221.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.47.08\u202fAM-1150x686.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.47.08\u202fAM-750x447.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.47.08\u202fAM-400x239.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.47.08\u202fAM-250x149.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">RNG-&gt;CR|=RNG_CR_RNGEN;<\/pre><\/div>\n\n\n\n<p>In while loop:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Wait until the new data is read.<\/li><li>Read the data.<\/li><\/ul>\n\n\n\n<p>To check if there is new data:<\/p>\n\n\n\n<p>Read DRDY bit in Status Register of the RNG:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"304\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.48.35\u202fAM-1024x304.png\" alt=\"\" class=\"wp-image-2271\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.48.35\u202fAM-1024x304.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.48.35\u202fAM-300x89.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.48.35\u202fAM-768x228.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.48.35\u202fAM-1536x455.png 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.48.35\u202fAM-2048x607.png 2048w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.48.35\u202fAM-1150x341.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.48.35\u202fAM-750x222.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.48.35\u202fAM-400x119.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/Screenshot-2024-01-11-at-10.48.35\u202fAM-250x74.png 250w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Store the new value into the variable:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">random_number=RNG-&gt;DR;<\/pre><\/div>\n\n\n\n<p>Hence, the entire code as following:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csrc&quot;,&quot;theme&quot;:&quot;dracula&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">#include &quot;stm32f4xx.h&quot;\n#include &quot;stdint.h&quot;\n\nuint32_t random_number;\n\n#define PLL_M      16\n#define PLL_N      192\n#define PLL_P      2\n#define PLL_Q      4\n\n\n\n\nint main(void)\n{\n\n    RCC-&gt;PLLCFGR = (PLL_M&lt;&lt;RCC_PLLCFGR_PLLM_Pos) | (PLL_N &lt;&lt; RCC_PLLCFGR_PLLN_Pos) | (((PLL_P &gt;&gt; 1) -1) &lt;&lt; RCC_PLLCFGR_PLLP_Pos) | \/\/set PLL_M,PLL_N,PLL_P\n             (PLL_Q &lt;&lt; RCC_PLLCFGR_PLLQ_Pos);\n\n    RCC-&gt;CR |= RCC_CR_PLLON; \/\/turn on the PLL\n\n\n    while((RCC-&gt;CR &amp; RCC_CR_PLLRDY) == 0); \/\/wait until PLL is active\n\n\n\tRCC-&gt;AHB2ENR|=RCC_AHB2ENR_RNGEN;\n\n\n\tRNG-&gt;CR|=RNG_CR_RNGEN;\n\n\n\n\twhile(1)\n\t{\n\t\twhile((RNG-&gt;SR &amp; RNG_SR_DRDY)==0);\n\t\trandom_number=RNG-&gt;DR;\n\t}\n\n}<\/pre><\/div>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">5. Results:<\/h2>\n\n\n\n<p>Open new debug session and add random_number to live expression window and you should get the following:<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"638\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/01\/ezgif-5-8f02b76a5e.gif\" alt=\"\" class=\"wp-image-2272\" \/><\/figure><\/div>\n\n\n\n<p>Feel free to use interrupt to get the data in the background.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude42<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we shall take a look at what is random number and how the STM32 uses hardware to generate true random numbers and develop the code for that. In this guide, we shall cover the following: What is random number. How STM32 generate random numbers. Features of the RNG peripheral of STM32. Developing [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,11,12],"tags":[],"class_list":["post-2263","post","type-post","status-publish","format-standard","hentry","category-embedded-systems","category-peripheral-drivers","category-stm32"],"_links":{"self":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2263"}],"collection":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2263"}],"version-history":[{"count":2,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2263\/revisions"}],"predecessor-version":[{"id":2273,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/2263\/revisions\/2273"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}