0
0
Cnc-programmingHow-ToBeginner · 4 min read

How to Write a Startup File for ARM Cortex-M Microcontrollers

To write a startup file for ARM Cortex-M, define the interrupt vector table with initial stack pointer and reset handler, then implement the Reset_Handler to initialize memory and call main(). The file is usually written in assembly or C and must set up system state before your program runs.
📐

Syntax

The startup file typically includes the vector table at the beginning, which lists the initial stack pointer and addresses of interrupt handlers. It defines the Reset_Handler function that runs on reset. The file also declares default handlers for interrupts and sets up memory initialization.

  • .word: Defines 32-bit values in the vector table.
  • Reset_Handler:: Label for the reset routine.
  • ldr: Loads addresses or values.
  • b: Branches to a function or label.
armasm
  .section .isr_vector,"a",%progbits
  .word _estack             /* Initial Stack Pointer */
  .word Reset_Handler       /* Reset Handler */
  .word NMI_Handler         /* NMI Handler */
  .word HardFault_Handler   /* Hard Fault Handler */

Reset_Handler:
  /* Initialize data and bss sections here */
  bl main                  /* Call main program */
  b .                      /* Loop forever if main returns */

NMI_Handler:
  b .                      /* Infinite loop */

HardFault_Handler:
  b .                      /* Infinite loop */
💻

Example

This example shows a minimal startup file in ARM assembly for a Cortex-M microcontroller. It sets the initial stack pointer, defines the reset handler to call main(), and provides default infinite loops for other interrupts.

armasm
  .syntax unified
  .cpu cortex-m3
  .thumb

  .section .isr_vector,"a",%progbits
  .word _estack             /* Initial Stack Pointer */
  .word Reset_Handler       /* Reset Handler */
  .word NMI_Handler         /* NMI Handler */
  .word HardFault_Handler   /* Hard Fault Handler */

  .section .text
  .global Reset_Handler
  .global NMI_Handler
  .global HardFault_Handler
  .extern main

Reset_Handler:
  /* Normally initialize .data and .bss here */
  bl main                  /* Call main program */
  b .                      /* Loop forever if main returns */

NMI_Handler:
  b .                      /* Infinite loop */

HardFault_Handler:
  b .                      /* Infinite loop */
⚠️

Common Pitfalls

Common mistakes when writing startup files include:

  • Not setting the initial stack pointer correctly, causing crashes on reset.
  • Failing to initialize .data and .bss memory sections, leading to unpredictable behavior.
  • Forgetting to declare Reset_Handler as global, so the linker cannot find it.
  • Not providing default handlers for interrupts, which can cause the system to jump to invalid memory.

Always ensure the vector table is placed at the correct memory address (usually start of flash) and that the startup file matches your microcontroller's memory layout.

armasm
/* Wrong: Missing initial stack pointer and Reset_Handler not global */
  .section .isr_vector,"a",%progbits
  .word Reset_Handler

Reset_Handler:
  bl main
  b .

/* Right: Include initial stack pointer and global Reset_Handler */
  .section .isr_vector,"a",%progbits
  .word _estack
  .word Reset_Handler

  .global Reset_Handler
Reset_Handler:
  bl main
  b .
📊

Quick Reference

Startup file essentials for ARM Cortex-M:

  • Vector Table: First entry is initial stack pointer, second is reset handler address.
  • Reset_Handler: Initializes memory and calls main().
  • Default Handlers: Provide infinite loops for unexpected interrupts.
  • Memory Sections: Initialize .data and clear .bss before main.
  • Placement: Vector table must be at address 0x0 or as per MCU memory map.

Key Takeaways

The startup file must define the vector table with the initial stack pointer and Reset_Handler.
Reset_Handler initializes memory and calls main(), setting up the system before your program runs.
Always provide default interrupt handlers to avoid undefined behavior on unexpected interrupts.
Place the vector table at the correct memory address as required by your Cortex-M microcontroller.
Initialize .data and clear .bss sections in Reset_Handler to ensure proper program operation.