What Runs Before main()? Understanding the Reset Sequence

When you click “Run” in your IDE, your processor doesn’t instantly jump to the first line of your main() function. It actually does a lot in the background to prepare to run your C code.

Understanding this hidden sequence that occurs between power and code execution (the reset sequence) helps you debug boot failures and optimize power consumption. In this guide, you will learn what happens during the reset sequence on the Texas Instruments CC2340 series and Nordic Semiconductor 54 L series development boards.

The Vector Table and Reset Vector

Every ARM Cortex-M processor, including those on your TI CC2340R5 and Nordic nRF54L15, follows a rigid hardware reset sequence. When the board gets power or a reset signal, the processor looks for the Vector Table in memory.

The Vector Table provides two key pieces of information:

  1. The initial Stack Pointer address
  2. The Reset Vector

The Reset Vector is the memory address of the first instruction the processor executes after a reset. After the processor finds the Vector Table, it loads the Reset Vector into the Program Counter to begin running the Reset Handler, which prepares your hardware to run your application code starting at main().

The Reset Handler

The Reset Handler prepares the RAM. Your variables need to be in the correct state, or else your code will fail. This process involves two tasks:

  • Data Initialization: The Reset Handler copies the initial values of global variables and static variables from storage to RAM.
  • BSS Clearing: The Reset Handler sets all uninitialized global variables to zero. If you have ever noticed that your global variable starts at zero, you can blame your Reset Handler.

Hardware-Specific Initialization

While the Reset Handler does the same RAM setup on both the TI CC2340R5 and Nordic nRF54L15 boards, it does a different additional hardware setup for each board type.

TI: Trim and Bootloader

On the TI CC2340R5, the Reset Handler runs a ROM-based bootloader first. This bootloader verifies the integrity of your firmware image and also applies trim settings, which are calibration data for the oscillators and voltage regulators.

Nordic: Zephyr and SystemInit

On the Nordic nRF54L15, the Reset Handler executes SystemInit(). SystemInit() configures the Instruction Cache, enables the Floating Point Unit, and starts the clock tree.

TI CC2340R5 Example

For the TI CC2340R5 LaunchPad, the startup file defines the reset sequence. Below is an example of this sequence in your firmware.

//*****************************************************************************
//
//! This is the code that gets called when the processor first starts execution
//! following a reset event. Only the absolutely necessary set is performed,
//! after which the application supplied entry() routine is called. Any fancy
//! actions (such as making decisions based on the reset cause register, and
//! resetting the bits in that register) are left solely in the hands of the
//! application.
//
//*****************************************************************************
void resetISR(void)
{
    IntDisableMaster();

    //
    // Final trim of device
    //
    SetupTrimDevice();

    //
    // Jump to the C Initialization Routine.
    //
    __asm(" .global _c_int00\n"
          " bl      _c_int00");

    //
    // If we ever return signal Error
    //
    faultISR();
}

An easy way to test that your reset sequence runs successfully is to flash any firmware that produces a clear signal, like an LED loop or printf log. If you see the signal, you know your reset sequence completed successfully.

Common Issues

If your board fails to boot, the problem is most likely in the reset sequence. Common problems include:

  • Invalid Stack Pointer: The CPU crashes before the first line of code.
  • Watchdog Resets: If the reset sequence takes too long, a watchdog might trigger.
  • Clock Failures: If the crystal fails to start, the CPU hangs.

Wrapping Up

The journey from power-on to main() is a coordinated effort between your processor and the Reset Handler. The Reset Handler sets up the stack, initializes RAM, and configures clocks. By the time your first line of firmware application code runs, the heavy lifting of the reset sequence is done.

Once you understand the reset sequence, you can troubleshoot GPIO configurations and other power-up issues. Knowing what runs before your code ensures you are not debugging blindly when your board refuses to start.


Ready to connect your devices anywhere on Earth? Get started with Hubble Network for free →