Understanding ARM Cortex-M Linker Script and Startup Code: A Practical Guide??
Pravin Raghul
Embedded Software Engineer | Cortex-M | Embedded Linux & Kernel Drivers | C, C++, Python
Introduction ??
ARM Cortex-M processors follow a specific sequence during boot-up. Both the linker script and the startup code plays a critical component. The linker script guides the linker during compilation, specifying how memory is allocated, which sections to include, and the address of the first instruction. The startup code initializes the system and prepares it for running the application by setting up memory and calling the main() function.
In this article, I'll walk you through how I implemented a linker script, startup code, and tested them on the STM32F429 Discovery Board with a simple blinky application.
This guide will cover:
The Linker Script: Defining Memory Layout??
The linker script is a vital component during compilation. It defines the memory regions such as Flash and SRAM, specifies how different sections (like `.text`, `.data`, and `.bss`) are placed in memory, and sets the starting address of the program.
In the provided linker script, the memory layout is defined as follows:
Here, FLASH is where the code and read-only data will be stored, while SRAM is where variables and runtime data reside. The addresses and sizes are indeed specific to the STM32F429 processor.
The linker script then defines how sections are allocated:
You can check the complete linker script on linker.ld
Startup Code: The Boot Process??
The startup code executes immediately after reset, performing essential initialization tasks before calling the main application. This includes setting up the stack, copying initialized data to SRAM, and clearing the `.bss` section.
The provided startup code includes a vector table and a reset handler:
The vector table defines the initial stack pointer and the address of the reset_handler function. For minimal boot-up, these two elements are required, while other system exceptions are not handled.
领英推荐
The reset handler performs memory initialization and jumps to main():
This code handles copying initialized data from Flash to SRAM, zeroing out the `.bss` section, and finally calling the `main()` function to start the application.
The full implementation of the startup code is available on startup.c.
Testing with a Blinky??Application
To test the startup code and linker script, I used a simple blinky application that toggles the LEDs on the STM32F429 Discovery Board. This test confirmed that the memory was properly initialized and the system could successfully execute the main() function.
The blinky application source code is available on blinky.c.
Conclusion: ??
This article covered the key roles of ARM Cortex-M linker scripts and startup code. The linker script organizes memory during compilation, while the startup code handles system initialization and application execution. Testing on the STM32F429 Discovery Board with a blinky application.
If you're interested in experimenting with the full source code, you can access the project on cortex-m-startup. Feel free to try it out on your hardware and customize it for your needs!
Future Works ??
Next, I will be building projects on the same Cortex-M startup code by developing drivers for the STM32 microcontroller. I'll focus on creating drivers for peripherals like GPIO, UART, and timers ???.
References ??