Understanding ARM Cortex-M Linker Script and Startup Code: A Practical Guide??

Understanding ARM Cortex-M Linker Script and Startup Code: A Practical Guide??

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:

  1. The structure and role of the linker script.
  2. What startup code does.
  3. A hands-on example using the STM32F429 Discovery Board.
  4. Testing with a basic blinky?? application.


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:

  • .text holds the executable code and is stored in Flash.
  • .data contains initialized variables, copied from Flash to SRAM at runtime.
  • .bss holds uninitialized variables and is zeroed out in SRAM during startup.
  • start_of_data, end_of_data, start_of_bss, end_of_bss, and end_of_text are symbols used by the linker to mark the beginning and end of various sections in memory. These symbols are used in the startup code to manage memory initialization.

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 ??

  1. The Cortex M3 Processor
  2. STM32CubeF4


要查看或添加评论,请登录

Pravin Raghul的更多文章

社区洞察

其他会员也浏览了