Skip to content

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

Open
csanchezdll wants to merge 1 commit intoapache:masterfrom
csanchezdll:add_rng_to_stm32h5
Open

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

Conversation

@csanchezdll
Copy link
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
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
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
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.

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

Signed-off-by: Carlos Sanchez <carlossanchez@geotab.com>
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