Skip to content

arch/arm/src/stm32h5: add support for HW RNG.#18549

Merged
xiaoxiang781216 merged 1 commit intoapache:masterfrom
csanchezdll:add_rng_to_stm32h5
Mar 19, 2026
Merged

arch/arm/src/stm32h5: add support for HW RNG.#18549
xiaoxiang781216 merged 1 commit intoapache:masterfrom
csanchezdll:add_rng_to_stm32h5

Conversation

@csanchezdll
Copy link
Copy Markdown
Contributor

@csanchezdll csanchezdll commented Mar 17, 2026

Driver copied from stm32f7, which includes CEIS/SEIS clearing per reference manual.

Summary

STM32H5 MCUs have a random number generation. According to the datasheets, the whole family has it, so there is no need
to make the change conditional on specific part number as other families (STM32H7, for example, do).

There are two slightly different implementations of the RNG driver. One is used in plain stm32, stm32h7,and stm32f0l0g0: when error flags SEIS/CEIS are set it just reads DR again. The other is used in stm32f7 and stm32l4: it clears those flags and disables/re-enables RNG on failure. This second one is the correct procedure according to the manual, so I have made stm32h5 one used that one (copies from stm32f7).

Impact

This change will affect all STM32H5 platforms, making /dev/random appear and /dev/urandom support available for selection using Kconfig.

Testing

Host:

$ uname -a
Linux 40f6844c7dd8 6.8.0-101-generic #101~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Wed Feb 11 13:19:54 UTC  x86_64 x86_64 x86_64 GNU/Linux

$ arm-none-eabi-gcc --version
arm-none-eabi-gcc (GNU Arm Embedded Toolchain 10-2020-q4-major) 10.2.1 20201103 (release)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Board is NUCLEO-H563ZI

Tested using nsh to get random numbers from RNG.
Build:

nuttx$ tools/configure.sh -l nucleo-h563zi:nsh
  Copy files
  Select CONFIG_HOST_LINUX=y
  Refreshing...
CP: arch/dummy/Kconfig to /home/carlossanchez/src/nuttx/arch/dummy/dummy_kconfig
CP: boards/dummy/Kconfig to /home/carlossanchez/src/nuttx/boards/dummy/dummy_kconfig
LN: platform/board to /home/carlossanchez/src/nuttx-apps/platform/dummy
LN: include/arch to arch/arm/include
LN: include/arch/board to /home/carlossanchez/src/nuttx/boards/arm/stm32h5/nucleo-h563zi/include
LN: drivers/platform to /home/carlossanchez/src/nuttx/drivers/dummy
LN: include/arch/chip to /home/carlossanchez/src/nuttx/arch/arm/include/stm32h5
LN: arch/arm/src/chip to /home/carlossanchez/src/nuttx/arch/arm/src/stm32h5
LN: arch/arm/src/board to /home/carlossanchez/src/nuttx/boards/arm/stm32h5/nucleo-h563zi/src
#
# configuration written to .config
#
nuttx$ make menuconfig
[System Type -> STM32H5 Peripheral Selection -> Enable RNG]
nuttx$ make

NSH prompt:

ABCG
stm32_rng_initialize: Initializing RNG

NuttShell (NSH) NuttX-12.12.0
nsh> hexdump /dev/random count=4
/dev/random at 00000000:
0000: f6 12 50 83                                     ..P.
nsh> hexdump /dev/random count=4
/dev/random at 00000000:
0000: 7e ac bb a7                                     ~...

@github-actions github-actions bot added Arch: arm Issues related to ARM (32-bit) architecture Size: M The size of the change in this PR is medium labels Mar 17, 2026
@simbit18
Copy link
Copy Markdown
Contributor

Hi @csanchezdll, please align CMake with Make

add stm32_rng.c

https://github.com/csanchezdll/incubator-nuttx/blob/7742cff29e3ceff166aeedb1e00c6310177a37b8/arch/arm/src/stm32h5/CMakeLists.txt#L40

@csanchezdll
Copy link
Copy Markdown
Contributor Author

RNG requires HSI48, so I did not want to make it unconditional (some scenarios might prefer that clock disabled). Also I had to modify slightly the logic that enables that clock in RCC initialization.
PR is now ready.

@simbit18
Copy link
Copy Markdown
Contributor

Hi @csanchezdll, please fix

../nuttx/tools/checkpatch.sh -c -u -m -g  b9c639f604b5247a7527fc4143b88cd9d905a332..HEAD
Used config files:
    1: .codespellrc
Error: /home/runner/work/nuttx/nuttx/nuttx/arch/arm/src/stm32h5/stm32h5xx_rcc.c:1205:1: error: Blank line contains whitespace
Error: /home/runner/work/nuttx/nuttx/nuttx/arch/arm/src/stm32h5/stm32h5xx_rcc.c:1214:1: error: Blank line contains whitespace
Some checks failed. For contributing guidelines, see:
  https://github.com/apache/nuttx/blob/master/CONTRIBUTING.md
Error: Process completed with exit code 1.

@csanchezdll
Copy link
Copy Markdown
Contributor Author

It seems the checks failed for external reasons:

2026-03-18T17:08:37.0961786Z ====================================================================================
2026-03-18T17:08:37.0985532Z Cmake in present: nrf52832-dk/sdc_nimble,CONFIG_ARM_TOOLCHAIN_GNU_EABI
2026-03-18T17:08:37.0986163Z Configuration/Tool: nrf52832-dk/sdc_nimble,CONFIG_ARM_TOOLCHAIN_GNU_EABI
2026-03-18T17:08:37.1082281Z 2026-03-18 17:08:37
2026-03-18T17:08:37.1082912Z ------------------------------------------------------------------------------------
2026-03-18T17:08:37.1083565Z   Cleaning...
2026-03-18T17:08:37.1083912Z   Configuring...
2026-03-18T17:08:40.6514312Z   Select HOST_LINUX=y
2026-03-18T17:08:42.4686084Z    TOOLS_DIR path is "/github/workspace/sources/nuttx"
2026-03-18T17:08:42.4686690Z    HOST = Linux
2026-03-18T17:09:12.8302758Z CMake Error at /github/workspace/sources/nuttx/build/_deps/sdk-nrfxlib-subbuild/sdk-nrfxlib-populate-prefix/src/sdk-nrfxlib-populate-stamp/download-sdk-nrfxlib-populate.cmake:170 (message):
2026-03-18T17:09:12.8305413Z   Each download failed!
2026-03-18T17:09:12.8305662Z 
2026-03-18T17:09:12.8306251Z     error: downloading 'https://github.com/nrfconnect/sdk-nrfxlib/archive/v2.3.0.tar.gz' failed
2026-03-18T17:09:12.8307418Z           status_code: 28
2026-03-18T17:09:12.8307887Z           status_string: "Timeout was reached"

I have checked other runs of the same build job Linux (arm-05) on other PRs and it runs well, so I guess this is a transient error.
I am re-pushing to force a re-run (maybe admins can trigger a run on this job only, but I can not).

Driver copied from stm32f7, which includes CEIS/SEIS clearing per reference manual.

Signed-off-by: Carlos Sanchez <carlossanchez@geotab.com>
@simbit18
Copy link
Copy Markdown
Contributor

Hi @csanchezdll, I restarted the failed jobs

@csanchezdll
Copy link
Copy Markdown
Contributor Author

Sorry @simbit18 I read it just after re-pushing. No way I can stop it now. I will be more patient next time, knowing that can be done.

@simbit18
Copy link
Copy Markdown
Contributor

@csanchezdll Don't worry. You just have to wait three hours now and be a little more patient!!! :)

@xiaoxiang781216 xiaoxiang781216 merged commit ff29b04 into apache:master Mar 19, 2026
26 checks passed
@csanchezdll csanchezdll deleted the add_rng_to_stm32h5 branch March 19, 2026 14:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Board: arm Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants