{"id":3402,"date":"2025-04-19T16:14:31","date_gmt":"2025-04-19T16:14:31","guid":{"rendered":"https:\/\/blog.embeddedexpert.io\/?p=3402"},"modified":"2025-04-19T16:14:34","modified_gmt":"2025-04-19T16:14:34","slug":"getting-started-with-stm32h5-arm-cortex-m33-random-number-generator","status":"publish","type":"post","link":"https:\/\/blog.embeddedexpert.io\/?p=3402","title":{"rendered":"Getting Started with STM32H5 ARM Cortex M33: Random Number Generator"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"683\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-19-2025-at-07_00_59-PM-1024x683.png\" alt=\"\" class=\"wp-image-3403\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-19-2025-at-07_00_59-PM-1024x683.png 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-19-2025-at-07_00_59-PM-300x200.png 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-19-2025-at-07_00_59-PM-768x512.png 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-19-2025-at-07_00_59-PM-1150x767.png 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-19-2025-at-07_00_59-PM-750x500.png 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-19-2025-at-07_00_59-PM-400x267.png 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-19-2025-at-07_00_59-PM-250x167.png 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/ChatGPT-Image-Apr-19-2025-at-07_00_59-PM.png 1536w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The STM32H5 series includes a hardware random number generator (RNG) that provides true random values, essential for cryptographic and security applications. This guide walks you through enabling and using the RNG peripheral in STM32H5 using STM32CubeIDE and HAL drivers.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>In this guide, we shall cover the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Introduction to random number generator<\/li>\n\n\n\n<li>STM32CubeIDE setup.<\/li>\n\n\n\n<li>Firmware Development.<\/li>\n\n\n\n<li>Results.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">1. Introduction to Random Number Generator:<\/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<h2 class=\"wp-block-heading\">1.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\u2019s a simplified explanation:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Thermal Noise:<\/strong>\u00a0Microcontrollers naturally exhibit thermal noise due to temperature fluctuations at the microscopic level within the silicon.<\/li>\n\n\n\n<li><strong>Analog-to-Digital Conversion (ADC):<\/strong>\u00a0The 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>\n\n\n\n<li><strong>Bit Extraction:<\/strong>\u00a0The 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>\n\n\n\n<li><strong>Output:<\/strong>\u00a0The generated random bits are then made available for use by software.<\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\">1.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><strong><em>Note: Not all STM32H5 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\">2. STM32CubeIDE Configuration:<\/h2>\n\n\n\n<p>We start off by creating new project with name of Internal RandomNumberGenerator. For how to create project using STM32CubeIDE, please refer to this guide\u00a0<a href=\"https:\/\/blog.embeddedexpert.io\/?p=2789\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"780\" height=\"1024\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_18-42-51-780x1024.jpg\" alt=\"\" class=\"wp-image-3404\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_18-42-51-780x1024.jpg 780w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_18-42-51-228x300.jpg 228w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_18-42-51-768x1008.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_18-42-51-750x985.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_18-42-51-400x525.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_18-42-51-250x328.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_18-42-51.jpg 984w\" sizes=\"(max-width: 780px) 100vw, 780px\" \/><\/figure>\n\n\n\n<p>Once the project has been created, STM32CubeMX window will appear.<\/p>\n\n\n\n<p>Head to Security, then RNG and enable RNG as following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"934\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_19-07-42-1024x934.jpg\" alt=\"\" class=\"wp-image-3405\" srcset=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_19-07-42-1024x934.jpg 1024w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_19-07-42-300x274.jpg 300w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_19-07-42-768x700.jpg 768w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_19-07-42-1536x1401.jpg 1536w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_19-07-42-1150x1049.jpg 1150w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_19-07-42-750x684.jpg 750w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_19-07-42-400x365.jpg 400w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_19-07-42-250x228.jpg 250w, https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/2025-04-19_19-07-42.jpg 1792w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Keep every as is. No need to modify any thing.<\/p>\n\n\n\n<p>Thats all for STM32CubeIDE. Save the project and this will generate the codes.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. Firmware Development:<\/h2>\n\n\n\n<p><\/p>\n\n\n\n<p>Once the project has been generated, main.c will be opened.<\/p>\n\n\n\n<p>In main.c in user PV begin(Private Variable), declare a variable of uint32_t to hold the random number 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;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\">uint32_t randomNumber;<\/pre><\/div>\n\n\n\n<p>In user code begin in while loop, state generating the random number 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;C&quot;,&quot;language&quot;:&quot;C&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;c&quot;}\"> HAL_RNG_GenerateRandomNumber(&amp;hrng,&amp;randomNumber);<\/pre><\/div>\n\n\n\n<p>The function HAL_RNG_GenerateRandomNumber takes the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Instant to random number generator which is hrng.<\/li>\n\n\n\n<li>Variable&#8217;s address to hold the generated random.<\/li>\n<\/ul>\n\n\n\n<p>That all for this guide.<\/p>\n\n\n\n<p>Save the project, build and run it on your board.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2024\/07\/2024-07-12_15-03-3.jpg\" alt=\"\" \/><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. Results:<\/h2>\n\n\n\n<p>Open debug session and add randomNumber variable to Live Expression and you should get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"800\" height=\"928\" src=\"https:\/\/blog.embeddedexpert.io\/wp-content\/uploads\/2025\/04\/ezgif-2ba73c2336688c.gif\" alt=\"\" class=\"wp-image-3406\" \/><\/figure>\n\n\n\n<p>Now, you can use the random number in your project.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Happy coding \ud83d\ude09<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The STM32H5 series includes a hardware random number generator (RNG) that provides true random values, essential for cryptographic and security applications. This guide walks you through enabling and using the RNG peripheral in STM32H5 using STM32CubeIDE and HAL drivers. In this guide, we shall cover the following: 1. Introduction to Random Number Generator: A random [&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-3402","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\/3402"}],"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=3402"}],"version-history":[{"count":1,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3402\/revisions"}],"predecessor-version":[{"id":3407,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=\/wp\/v2\/posts\/3402\/revisions\/3407"}],"wp:attachment":[{"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3402"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3402"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.embeddedexpert.io\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3402"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}