I have an application that I am porting from the Keil IDE to build with the GNU toolchain due to license issues. I have successfully be able to set up, build, flash and run the
I'll expand a bit on what led me here, and how I use the insight from @Mike to correct it.
I had a project running fine on a demo project in Eclipse SW4STM32, but with sources and headers scattered all over the place so I wanted to have a more "compact" project easier to customize and use as a base for minor modifications (and easier to follow in Git).
I created an empty AC6 project targetting the same board. It generated the HAL drivers, the startup_stm32.s
and LinkerScript.ld
. I then copied all of the .c
and corresponding .h
from the original project to my new project (which was a pain in itself because they were scattered in BSP, CMSIS, Components, Middlewares, etc. directories). Everything compiled and seemed to work, until I started modifying a bit.
In the debugger, it seemed all function calls were working until the while(1)
main loop where I ended up in the Default_Handler
defined in the startup_stm32.s
, seemingly from WWDG_IRQHandler
. That was, in fact, the default IRQ handler for not-user-defined handlers (WWDG_IRQHandler
being the first one declared, it was reported as such by gdb, as indicated by @D Krüger).
I started looking at compiler and linker options or linker script, without much luck, until I realized the only file I didn't check was the startup_stm32.s
, which was indeed different.
I blindly copy-pasted it and voilà!
The explanation I could give is that the STM32 is calling IRQ handlers defined in the startup_stm32.s
when interrupt occur, all of them initially pointing to Default_Handler()
(later overriden by the linker). So if a .c
file you copied defines a handler with a slightly different name (but consistent with its own startup_xxx.s
), you'll end up with the Default_Handler()
being called (which is an infinite loop) instead of the one you defined. And things go wrong.
See https://www.freertos.org/Debugging-Hard-Faults-On-Cortex-M-Microcontrollers.html for more information.
N.B. I'm not happy to blindly copy-paste without fully understanding, but time constraints and milestones usually push you to territories you're not happy to explore...