What purpose does the start-up code has in an MCU?

Start-Up Code is the soul of an MCU. It is the first-ever code that runs on an MCU right after the Power On Reset (POR). The Start-Up code triggers the initializing of crucial parts of the MCU needed for booting and the subsequent target application launch.

The start-up code in embedded systems is typically lightweight and is written in assembly language, or a combination of C and assembly. When code is written in assembly language, the developer has greater control and can optimize for speed by carefully selecting specific instructions or instruction types.

NOTE : For more detailed overview of Boot Sequence click on this link

Boot Sequence

Start-Up Code performs several essential tasks to get the MCU ready to run the target application.

These Tasks include :

  1. Stack Pointer Initialization: Stack Pointer points to the memory location in RAM which will be used by the program to store the function call stacks and the local variables for each function call. Start-Up Code initializes the Stack Pointer with the known Stack location address (The top address of the stack).
  2. Data Section Initialization: Data Section in the memory layout contains the initialized global and static variables. Start-Up code initializes the data Section from the FLASH/ROM or any other location in the memory to the RAM or the memory where code execution will happen.
  3. Block Started by symbol (BSS) Initialization: BSS Section in the memory layout contains uninitialized global and static variables. The Start-Up code initializes the BSS Section with zero, to make sure all the variables get some known value.
  4. Vector Table Initialization: The vector table contains the function pointers of the interrupt callback functions. The Initialization Code for the Start-Up sets the Vector table location to the value stored in the vector table section of memory, allowing the designated interrupt callback to be executed each time an interrupt is triggered.
  5. Configuring the Clock and Debug Setting: Depending upon the application, the clock setting defers so Start-Up can take care of the default clock settings or even the complete clock settings and ensures the application code runs at the correct speed.The Start-Up code also sets the debug settings required for the MCU Platform e.g. enabling/disabling the JTAG/SWD for debugging, and enabling/disabling and configuring the UART for debug prints.

Startup Code

All the start-up code tasks execution is done by the reset handler function. After completing all start-up code tasks, the reset handler exits and transfers control to the main() function, beginning execution of the target application.