Skip to content

arch/arm/stm32h5: Add MPU support - #20122

Open
darrylring wants to merge 3 commits into
apache:masterfrom
darrylring:feature/stm32h5-mpu
Open

arch/arm/stm32h5: Add MPU support#20122
darrylring wants to merge 3 commits into
apache:masterfrom
darrylring:feature/stm32h5-mpu

Conversation

@darrylring

Copy link
Copy Markdown
Contributor

Summary

This adds basic MPU support for the STM32H5 based on the initialization in the STM32U5 (same core).

The STM32H5 has a couple issues ("features") related to the OTP flash region:

  1. It cannot be accessed via cacheable reads. If ICACHE is enabled, reading OTP will cause a hard fault.
  2. Reading unwritten OTP flash causes flash double ECC errors, which triggers NMIs by default. This crashes NuttX currently. (This is out of scope for this PR.)

NOTE: This does not enable protected mode builds (CONFIG_BUILD_PROTECTED) at this time. I don't currently understand that portion of the codebase enough (yet) to attempt that.

Impact

No impact, since MPU support is currently not implemented.

Testing

I added code to the NUCLEO-H563ZI board to configure an MPU region over the OTP flash in the case that CONFIG_ARM_MPU and CONFIG_STM32_ICACHE are enabled. I have not added these to any defconfig, though.

Before, with CONFIG_STM32_ICACHE disabled:

nsh> mh 0x08FFF800 0xC
  0x8fff800 = 0x003e
  0x8fff802 = 0x0063
  0x8fff804 = 0x5111
  0x8fff806 = 0x3332
  0x8fff808 = 0x3236
  0x8fff80a = 0x3836

Before, with CONFIG_STM32_ICACHE enabled:

nsh> mh 0x08FFF800 0xC
dump_assert_info: Current Version: NuttX  13.0.1-RC1 3a8b290f71-dirty Sep 11 2026 16:26:21 arm
dump_assert_info: Assertion failed panic: at file: armv8-m/arm_hardfault.c:147 task: <noname> process: <noname> 0x8012db5
up_dump_register: R0: 00000000 R1: 080150bb R2: 00000000  R3: 08fff800
up_dump_register: R4: 08014ce5 R5: 08014a19 R6: 0801a3e7  FP: 00000000
up_dump_register: R8: 00000000 SB: 00000000 SL: 20003f18 R11: 00000000
up_dump_register: IP: 00000000 SP: 20004448 LR: 0801508d  PC: 080150d2
up_dump_register: xPSR: 81000000 BASEPRI: 00000000 CONTROL: 00000000
up_dump_register: EXC_RETURN: ffffffac
...

After, with CONFIG_ARM_MPU and CONFIG_STM32_ICACHE enabled:

nsh> mh 0x08FFF800 0xC
  0x8fff800 = 0x003e
  0x8fff802 = 0x0063
  0x8fff804 = 0x5111
  0x8fff806 = 0x3332
  0x8fff808 = 0x3236
  0x8fff80a = 0x3836

@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 Board: arm labels Sep 11, 2026
@github-actions

github-actions Bot commented Sep 11, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

arduino-mega2560

  • flash: .text +16 B (+0.0%, 65,128 B / 262,144 B, total: 25% used)

esp32-devkitc

  • ROM: .flash.text +12 B (+0.0%, 124,904 B / 4,194,272 B, total: 3% used)
  • irom0_0_seg: .flash.text +12 B (+0.0%, 89,108 B / 3,342,304 B, total: 3% used)

hifive1-revb

  • flash: .text -8 B (-0.0%, 83,724 B / 4,194,304 B, total: 2% used)

mirtoo

  • kseg0_progmem: .text +20 B (+0.0%, 67,772 B / 131,072 B, total: 52% used)

qemu-armv8a

  • Code: .text.nx_pthread_create -4 B, .text.nxsched_set_param +16 B, .text.spawn_execattrs +4 B, .text.work_qcancel +20 B (+0.0%, 336,944 B)

qemu-intel64

  • Code: .text +15 B (+0.0%, 8,659,813 B)

rx65n-rsk2mb

  • ROM: .text +16 B (+0.0%, 86,896 B / 2,097,152 B, total: 4% used)

s698pm-dkit

  • Code: .text +192 B (+0.1%, 367,680 B)

stm32-nucleo-f103rb

  • flash: .text +8 B (+0.0%, 34,556 B / 131,072 B, total: 26% used)

This adds MPU initialization code based on the STM32U5. Unlike the
STM32U5 code, though, this allows the MPU to be used outside of
PROTECTED build mode.

PROTECTED build mode is still not yet supported.

Assisted-by: Claude:claude-sonnet-5
Signed-off-by: Darryl Ring <darryl@bluerobotics.com>
If CONFIG_ARM_MPU and CONFIG_STM32_ICACHE are set, this will configure an
MPU region marking the OTP flash as non-cacheable. This prevents hard faults
when accessing the 4K OTP region from software.

Signed-off-by: Darryl Ring <darryl@bluerobotics.ca>
acassis
acassis previously approved these changes Sep 12, 2026
Comment thread arch/arm/src/stm32h5/stm32_mpuinit.c Outdated
#include "mpu.h"
#include "stm32_mpuinit.h"

#ifdef CONFIG_ARM_MPU

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@darrylring the Make.defs and CMakefile.txt already prevents the file to be compiled if CONFIG_ARM_MPU is not enabled. In the past Greg used to add this double check, but it is not necessary

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Fixed in 34f4927.

The compilation of stm32_mpuinit.c is guarded by CMakeLists.txt and
Make.defs, so this is unneccessary.

Signed-off-by: Darryl Ring <darryl@bluerobotics.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