Skip to main content
KSing.4
Associate
November 6, 2021
Question

Getting Usage_FaultHandler exception when reading int32 value from the un-aligned void* ptr. Example: Here data pointer address is 0x20008911, which is unaligned by 4 byte. rpm = ((float) * (const s32 *)_data) / 256.0F;

  • November 6, 2021
  • 5 replies
  • 1720 views

We are porting applications from STM32F1 to STM32F427 MCU. Currently, we are not explicitly configuring SCB->CCR unaligned trap, based on ARM M4 manual, LDR, VLDR always triggers the fault if the address is not aligned properly.

Do I require to use memcpy while converting the void* to int32? Are there any other solutions to avoid these realignment exceptions?

This topic has been closed for replies.

5 replies

waclawek.jan
Super User
November 6, 2021

> 0x20008911, which is not unaligned by 4 byte

"not unaligned" is double-negation; "0x20008911 is not aligned" or "0x20008911 is unaligned".

LDR won't trap, VLDR will. Look at disassembly of how this line was compiled - the optimizer may ignore the casts.

Try (float)*(volatile s32*)_data.

JW

KSing.4
KSing.4Author
Associate
November 6, 2021

Thanks for your response, yes with volatile machine instructions changed to LDR.

TDK
November 6, 2021

> Are there any other solutions to avoid these realignment exceptions?

Clear the UNALIGN_TRP bit in SCB->CCR. The Cortex-M4 can handle unaligned access, but I believe it takes a few more cycles.

SCB->CCR &= ~SCB_CCR_UNALIGN_TRP_Msk;

 Edit: probably due to the floating point instruction which must be aligned.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Tesla DeLorean
Guru
November 6, 2021

Might want to consider how you're managing unaligned memory structures, guessing from a file or external device, etc.

LDRD,STRD are also intolerant of misalignment across the Cortex-M parts.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Pavel A.
November 7, 2021

In cmsis_compiler.h are defined macros for unaligned access: __UNALIGNED_UINT32_READ etc.

> Try (float)*(volatile s32*)_data.

This will convert an int value to float ((

C++ has reinterpet_cast but in C it is ugly.