Hard Fault after rewriting part of the flash
Hello there ST Community,
I'm currently working in a way to update a device using a USB stick. Instead of implementing a normal bootloader, I decided to try a different approach: have the updater (and all its needed libraries) installed in a fixed region of the flash, while the rest of the program is installed in a modifiable part of the flash. During update, my updater erases the modifiable part of the flash, reads the USB stick and rewrite the flash with the new program.
This part is working well, and the program is perfectly capable of rewriting the flash. The problem arises after the MCU is reset. During the initialization it suddenly triggers a hard fault (more details below) - this is after some functions are completed.
The difference between the initial program and the new one is simply the addition of a couple of functions. When I use the same old program as the new one (which basically means reprogramming the flash) the process works fine and no hard fault is generated. The new program also works fine when tested separately.
In the hard fault my CSFR register has the bits BFARVALID and PRECISERR set. My BFAR register has a random number that does not fall under the valid address range. The problem is, when I investigate the instruction in the address pointed by the PC register (by debugging the new program separately), it is simply a branch instruction in a random function. Basically, debugging the hard fault didn't point me to the problem.
Also, I created a second new program with different functions added. This time, the instruction that causes the error is a Load Register Byte (which makes more sense but still doesn't help).
I'm wondering if the problem is my poor approach to the uploader problem. Is there a better solution? Does anybody have an idea about what might be causing these problems?
Thank you in advance.
