Solving STM32 Startup Crashes: Fixing Linker and Vector Table Mismatches

In this post, we explore common causes of startup crashes in STM32 simulations in proteus and provide practical solutions. You’ll learn how to identify and fix issues such as linker script mismatches, redundant startup code, and vector table misalignment, ensuring your firmware builds and runs correctly from the very first instruction.

STM32F103C8 LED blink

The Debugging Story

1. The Setup

The project was built for an STM32F103C8 (Cortex-M3) microcontroller in Proteus. The goal was a simple LED blink, but the simulation crashed immediately at 0.0025s with:

  • Error: [INTERRUPT] Stack error while saving exception context. Access to invalid address 0x4B11B060.

2. Root Cause Analysis

  • Redundant Startup Code: The project included both crt.c and vtable.c, both containing initialization logic (Reset_Handler vs ResetHandler). This caused a conflict in the C runtime initialization.
  • Linker/Vector Table Mismatch: The vector table in vtable.c was tagged with section(".vectors"), while the linker script (STM32F103XB_FLASH.ld) expected .isr_vector. The linker ignored the table, leaving the processor with no valid reset vector.

3. The Debugging Journey

  • Phase 1: Cleanup: We removed crt.c to eliminate the conflicting initialization logic.
  • Phase 2: The Secondary Error: After removing crt.c, we encountered a new compilation error: ../vtable.c:112:1: error: expected identifier or '(' before '{' token This happened because, while attempting to fix the section attribute, the gVectors[] array definition was accidentally deleted, leaving an orphan brace { that the compiler could not parse.
  • Phase 3: Final Alignment: We corrected the syntax in vtable.c by properly declaring the gVectors[] array and updating the attribute to __attribute__ ((section(".isr_vector"), used)).
  • Phase 4: Clean Build: A "Clean Project" and "Build Project" were performed to force the linker to correctly map the memory and place the vector table at the start of the FLASH.

4. The Result

With the startup code unified, the section names aligned, and the syntax corrected, the STM32F103C8 successfully initialized its stack pointer and entered the main() function, allowing the LED to blink as intended.


Code



#include 

int main(void)
{
    // Enable GPIOC clock
    RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;

    // Configure PC13 as output, push-pull, 2MHz
    // PC13 is in CRH register (bits 20-23)
    GPIOC->CRH &= ~(0xF << 20);
    GPIOC->CRH |=  (0x2 << 20);

    while (1) {
        // Toggle PC13
        GPIOC->ODR ^= (1 << 13);
        
        // Simple delay
        for (volatile int i = 0; i < 100000; i++);
    }
}



Key Takeaway for your readers:

"When debugging ARM startup crashes, remember that removing redundant startup files is only half the battle. Always check your C-syntax after manual edits and ensure your linker script's section names are an exact match for your source code's section attributes."


See also:

Post a Comment

Previous Post Next Post