0

I am getting a unaligned access hardfault on the ldr.w instruction with Thumb2 on ARM Cortex-M7. The microcontroller is an STM32H745xx and I am using STM32CubeIDE.

I am using the STM32 arm-eabi-none-gcc toolchain to compile the project, with the -mno-unaligned-access option.

instruction: ldr.w r3, [r3, #1288] ; 0x508

Any ideas on how this might be fixed?

FYI: I am very new to the embedded world.

ethorn
  • 1
  • What is in r3 before the instruction is executed? I suspect it is not aligned to 4 bytes. You are loading what is in the address pointed to by r3 + 1288. This needs to be 4 byte aligned. – Realtime Rik Jan 18 '22 at 12:03
  • Thanks for taking the time. r3 is 0xd737782e. The instruction before is: ldr r3, [r7, #32]. How can I make sure that the compiler does not make unaligned addresses? – ethorn Jan 18 '22 at 14:40
  • How did r3 get that value? Is this in an assembly function you wrote yourself or have you disassembled the output of compiling some C code? If it is C code, then you have probably written some invalid code that causes undefined behaviour. Edit the question to include more context of how you got here. – Tom V Jan 18 '22 at 18:04
  • The function causing the error is from a c library that I have compiled with the arm-eabi-none-gcc toolchain, with -mthumb and -mno-unaligned-access. The function is referencing a table structure and uses a mutex. – ethorn Jan 19 '22 at 10:33
  • `0xd737782e` does not look like a valid adress on a STM32H7, unless you have an external memory ? – Guillaume Petitjean Jan 19 '22 at 10:58
  • the Cortex M7 supports unaligned accesses anyway (except in some particular cases like external memories configured in device memory type) – Guillaume Petitjean Jan 19 '22 at 10:59
  • It is not the compilers job if you pack structures for example or do other bad things. It is the programmers job to avoid unaligned accesses. whether or not the target can disable the trap. You need to provide a minimal example of the code , the compile steps and the output. – old_timer Jan 19 '22 at 19:19
  • basically if you ask the compiler to do it, it will. please provide a complete minimal example. – old_timer Jan 19 '22 at 19:20

0 Answers0