STM32F103C8T6 ARM STM32 Minimum System Development Board

September 13, 2020

libmaple -- address alignment

I first mis-diagnosed this as an address alignment issue. I have a byte containing an enum, and when I try to write a value to it, I get a fault. This byte is at an odd address, but that is not the crucual issue at all.

Writing to this address causes a "hard fault" (vector 3). I had this vector pointing, along with virtually every other vector, to a "bogus" routine that just put the processor into a spin loop. So I didn't know what was happening other than things stopped running.

This problem is happening with the i2c code I pulled out of libmaple. So the question arises, as to how this setup works in the libmaple code. I have verified that this enum is also at an odd address in the libmaple code -- so some compiler option is not yielding a neatly aligned structure. There is an alignment bit in the SCB (system control block?). Namely the CCR at address 0xE000ED14, but this register is zero, so the bit to allow unaligned addresses is not set. All very mysterious.

We can read the byte, but writing it causes the exception. And this is the clue! The problem is that my addresses are in flash, not in ram! Therefore it only makes sense that I can read but not write. This is obvious from the addresses once you look at them:

My code (fails):  0800161B
libmaple (works): 2000066F
So, the fix? Work on the linker script (lds file), and while I am at it, add the code to zero BSS.
Feedback? Questions? Drop me a line!

Tom's Computer Info / [email protected]