0
0
Embedded Cprogramming~7 mins

Startup sequence and reset vector in Embedded C

Choose your learning style9 modes available
Introduction

The startup sequence prepares the microcontroller to run your program. The reset vector tells the processor where to start running code after a reset.

When you want to understand how your embedded program begins running after power on.
When debugging why your microcontroller does not start your main program.
When writing low-level code that runs before main(), like initializing hardware.
When customizing the reset behavior or adding your own startup code.
When learning how embedded systems boot up step-by-step.
Syntax
Embedded C
void Reset_Handler(void) {
    // Initialization code here
    main();
}

// Vector table example
void (* const vector_table[])(void) __attribute__((section(".isr_vector"))) = {
    Reset_Handler, // Reset vector
    // Other interrupt handlers
};

The reset vector is a pointer to the Reset_Handler function.

The startup sequence runs before main() and sets up memory and hardware.

Examples
This Reset_Handler clears uninitialized variables and copies initialized variables before calling main.
Embedded C
void Reset_Handler(void) {
    // Clear .bss section
    // Copy .data section from flash to RAM
    main();
}
The vector table is placed in a special memory section so the processor knows where to find interrupt handlers.
Embedded C
void (* const vector_table[])(void) __attribute__((section(".isr_vector"))) = {
    Reset_Handler, // Reset vector
    NMI_Handler,
    HardFault_Handler,
    // ... other handlers
};
Sample Program

This program simulates the startup sequence by copying initialized data and clearing uninitialized data before running main.

Embedded C
#include <stdint.h>

// Simulated memory sections
uint32_t data_init[] = {1, 2, 3};
uint32_t data[3];
uint32_t bss[3];

void main(void) {
    // Main program starts here
    for (int i = 0; i < 3; i++) {
        // Print data and bss values
        // In embedded, you might toggle LEDs or send data
    }
}

void Reset_Handler(void) {
    // Copy initialized data from flash to RAM
    for (int i = 0; i < 3; i++) {
        data[i] = data_init[i];
    }
    // Clear bss section
    for (int i = 0; i < 3; i++) {
        bss[i] = 0;
    }
    main();
}

// Vector table with reset vector
void (* const vector_table[])(void) __attribute__((section(".isr_vector"))) = {
    Reset_Handler
};

// Simulate reset by calling Reset_Handler
int main_wrapper() {
    Reset_Handler();
    return 0;
}
OutputSuccess
Important Notes

The reset vector is usually the first entry in the vector table.

The startup code runs before main() and prepares memory and hardware.

In real embedded systems, startup code is often written in assembly or provided by the compiler.

Summary

The reset vector tells the processor where to start after reset.

The startup sequence prepares memory and hardware before main() runs.

Understanding this helps you debug and customize embedded programs.