Showing results for 
Search instead for 
Did you mean: 

Wrong MPU setting from MX wizard, HardFault on unaligned transactions, STM32U5 (Core-M33)


I have bumped in a problem when using USBX on STM32U5A5 controller. Application generated HardFault somewhere inside USBX driver. After some digging I have found the cause of problem was unaligned write to RAM when driver was creating USB device descriptors. Particularly strh instruction (store half word) on unaligned address.

The cause was wrongly configured MPU (memory protection unit). In my project I added backup ram region to linker script via MX / Tools / Memory management tool. The linker script is updated only if both switches are set to on:

* Apply Application Regions Settings to Peripherals --> adds MPU settings to device configuration

* Apply Application Regions Settings to Linker Files --> updates memory regions in linker script xxx_FLASH.ld

Note: If you switch both switches off the configuration will not return back, MPU will not be disabled.

The problem is that MX wizard generates MPU settings for RAM regions with attributes set to DEVICE, not to Memory. The region with DEVICE attribute will generate HardFault (if not handled separately) on unaligned transactions.

MPU in MX wizard.pngMPU in Debugger.png

DEVICE region attribute is 0000xxxx, Normal memory can not have attribute with zeros in upper 4 bits.

If you edit MPU MAIRx registers in debugger or if you modify initialization code in main.c the problem will dissapear.

The remaining question is: Is it possible to force the MX wizard to generate valid (non DEVICE) attribute for memory regions?