Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 37 ยท Microcontroller Architecture

Clocks, Reset and Boot

Learn how a microcontroller gets its clock (RC, crystal, PLL), what resets it (power-on, brown-out, watchdog), and what happens between reset and main().

Two questions decide whether a microcontroller does anything useful: how fast is it running? and what does it do the instant after power or reset? The first is about the clock system, the second about reset and boot. Many beginner problems (an LED that blinks eight times too slowly, a program that does not start, a chip that resets every few seconds) trace back to these two topics.

This lesson explains oscillators and the PLL with the arithmetic you need to configure them, lists the kinds of reset, and follows the boot sequence on a Cortex-M step by step, including a small simulation of it.

Where the clock comes from

Everything in a microcontroller is synchronous: the CPU, buses and peripherals advance on clock edges. The clock is produced by one of several sources:

Source Accuracy Start time Power Notes
Internal RC oscillator (HSI / MSI) 1-2 %, drifts with temperature microseconds low the default after reset; fine for LEDs and UART at low baud rates
External crystal (HSE, 4-26 MHz) 10-50 ppm ~1 ms medium accurate; needed for USB, CAN, radios
External oscillator module 1-20 ppm fast higher a ready-made clock fed to a pin
32.768 kHz watch crystal (LSE) 20 ppm ~100s of ms very low real-time clock and low-power timing
Internal low-speed RC (LSI) poor fast very low watchdog
PLL (phase-locked loop) follows its source ~100 us to lock medium multiplies a slow clock up to the CPU speed

A crystal is accurate because quartz vibrates at a very stable frequency; a typical 25 ppm crystal drifts about two seconds per day. An internal RC oscillator is a simple circuit whose frequency depends on the chip and temperature, so UART communication at 115200 baud may need calibration or fail outright.

The PLL

CPUs run at tens or hundreds of MHz, but crystals are only a few MHz. A phase-locked loop multiplies the frequency: the input is divided by M, multiplied by N, then divided by P to give the system clock:

f_sys = f_in / M * N / P

For example, with an 8 MHz crystal on a typical STM32F4: 8 MHz / 8 * 336 / 2 = 168 MHz. The values must keep the intermediate frequencies inside allowed ranges (for instance the PLL's VCO must stay between 100 and 432 MHz), and as the system clock goes up, flash needs more wait states and the power regulator may need a higher voltage scale. Vendor tools (STM32CubeMX, the Pico SDK's pico_clocks) calculate this for you, but you should understand it.

C PLL calculator: find settings for a target clock

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Clock tree and gating

The system clock is split into branches: the CPU (HCLK), the buses (APB1 and APB2 on STM32) and individual peripherals. Each peripheral clock can be gated (switched off) to save power, and by default most are off after reset. This is why you must enable a peripheral's clock before touching its registers: writing to an unclocked peripheral silently does nothing or reads zero. Frequencies on each bus also determine timing: a UART's baud rate generator divides its bus clock, so changing the system clock changes every baud rate unless you reconfigure them.

Reset: why the chip restarts

A reset forces the CPU and most peripherals to a known state and begins execution from the reset vector. The common reset sources:

Source Trigger Typical use
Power-on reset (POR) supply rises from 0 clean start
Brown-out reset (BOR) supply sags below a threshold prevent erratic behavior and flash corruption
External reset pin (NRST) pin pulled low reset button, programmer
Watchdog reset watchdog timer not "kicked" in time recover from a hang
Software reset firmware requests it (NVIC_SystemReset) after a firmware update, on fatal error
Debug or low-power exit reset debugger or deep standby wake special cases

After reset the chip records the cause in a reset status register (STM32: RCC_CSR). A good firmware reads it at boot and logs it, because "why did we restart?" is one of the most useful questions in the field. A watchdog reset every 12 hours is a bug; a brown-out reset when the motor starts is a power supply problem.

A watchdog timer (WDT) is a counter that counts down and resets the chip when it reaches zero. Healthy firmware periodically "kicks" it (reloads the counter). If the program hangs in an infinite loop, nobody kicks it and the chip reboots itself. You enable it only after the system is working; "kick it from a timer interrupt" is a classic mistake that defeats its purpose.

What happens between reset and main()

On a Cortex-M the first words of flash (or the vector table's location) are special:

sequenceDiagram
    participant HW as Hardware
    participant VT as Vector table
    participant RS as Reset_Handler (startup code)
    participant M as main()
    HW->>VT: Read word 0 -> initial stack pointer
    HW->>VT: Read word 1 -> address of Reset_Handler
    HW->>RS: Jump to Reset_Handler
    RS->>RS: Copy .data from flash to RAM
    RS->>RS: Zero .bss
    RS->>RS: SystemInit (clocks, FPU, vector table offset)
    RS->>RS: Run C library / C++ constructors
    RS->>M: Call main()
  1. The hardware loads the initial stack pointer from address 0 of the vector table.
  2. It loads the reset vector (the address of Reset_Handler) from address 4 and jumps there.
  3. Reset_Handler, usually written by the vendor in assembly or C, copies .data from flash into RAM, zeroes .bss, calls SystemInit() to set up clocks, runs the C library initialization and static constructors, then calls main().

This is why a global variable like int counter = 5; has the right value when main starts even though no code in main assigns it: the startup code did the copy. Chapter 4 builds this yourself in Startup Code, Linker Sections and the Stack.

On some chips the very first code that runs is a boot ROM, and boot pins (BOOT0 on STM32, the BOOTSEL button on a Pico) choose whether it starts your flash program or a built-in loader that accepts a new firmware.

Simulating a boot sequence

The simulation below mimics the boot: a vector table with a stack pointer and reset handler, a "flash" image holding initial values for .data, a RAM that starts as garbage, and a handler that fixes it before calling main.

C Reset_Handler: copy .data, zero .bss, call main

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

A watchdog in miniature

Here is the watchdog idea in a few lines. Each pass of the main loop kicks the dog; when a simulated hang stops the kicks, the counter runs out and the "chip" resets.

C Watchdog: kick it or be reset

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A program expects a 1 second delay but the LED takes 8 seconds. What is a likely cause?

    After reset many chips use a slow internal oscillator; if the code assumes the PLL speed, all delays stretch.
  2. 2. With an 8 MHz input, M=8, N=336, P=2, what is the PLL output?

    8/8 = 1 MHz, times 336 = 336 MHz VCO, divided by 2 = 168 MHz.
  3. 3. What does the startup code do before main()?

    The C runtime requires initialized and zeroed globals; the Reset_Handler prepares them.
  4. 4. Why is it a mistake to kick the watchdog from a timer interrupt?

    The watchdog should prove that the main program flow is alive. An interrupt-based kick hides a hung main loop.
  5. 5. Where does the Cortex-M get its initial stack pointer?

    Word 0 of the vector table holds the initial SP, and word 1 holds the reset handler address.

Summary

  • The clock can come from an internal RC oscillator (default, inexact), a crystal (accurate) or a PLL that multiplies a slow source: f_sys = f_in / M * N / P.
  • Peripheral clocks are gated; enable them before use. Bus clocks decide baud rates and timer periods.
  • Resets come from power-on, brown-out, the reset pin, the watchdog and software; read the reset-cause register at boot.
  • On Cortex-M the hardware loads SP and the reset vector from the vector table; the startup code copies .data, zeroes .bss, initializes clocks and calls main().
  • A watchdog recovers from hangs only if it is kicked from the main flow of control.

Next: Fixed-Width Types and Integer Pitfalls