Stm32f103 goes into hardfault when branching to main


#1

Hi everyone !
I am posting here because I am working on a small robot written in Rust with some friends.
The last few weeks we have been struggling with an error that we can not figure out… We are using a blue pill with an stm32f103, 20K of ram and 64K of flash.

We have a communication protocol that is implemented as a state machine, with some C in the middle to avoid duplication (the same code is reused in C++ for another part of the robot).
Here is the symptom :

  • If we comment this line everything work fines
  • If we leave it has is, the µ-controller goes into hardfault when we hit this line

This line is “just” matching on the value of a byte and the state of a state machine.

Stepping through the debugger shows this the instruction before crashing

Then we into hardfault, with a strange value for sp (0x1ffe3d50).
I have strictly no idea what could be the problem with this, if someone could help investigate this issue I would be really thankfull.

Thanks for you consideration and have a nice day !


#2

Check your data structure size. As far as I can tell from your code a FrameReader uses more than 64K (256 frames of 256 bytes plus overhead), which is much too large for your stack.


#3

Thanks, I should have checked that before posting…

Anyway it still is strange that it goes into hardfault upon jumping to main and not at initialization.


#4

That’s normal, the space on the stack is reserved when a function is entered, by subtracting a constant from the stack pointer. The next access to the stack then causes a fault.


#5

Thank you for explaining !
So the reason why it doesn’t crash without the step call is because the variable is optimized away ?


#6

Yes, I think so.