Skip to main content
Be Bitwise

Bare-Metal Programming

Microcontroller Programming · Embedded Systems & Firmware track

6 min read

In this module

No OS, No Problem

Bare-metal programming means code running directly on the silicon with nothing underneath. No kernel, no scheduler, no standard library unless you ship one yourself, no runtime environment whatsoever. You own everything from the very first instruction after reset to the main application loop.

Startup Code

When power hits an ARM Cortex-M MCU, the hardware reads two words from the very start of flash: the initial stack pointer at address 0x00000000, and the reset handler address at 0x00000004. The CPU loads the stack pointer first, then jumps to the reset handler.

The reset handler — your startup code — has to wake the system up before main() can run:

c
// Minimal startup code for ARM Cortex-M
void Reset_Handler(void) {
// 1. Copy initialized data from flash to RAM (.data section)
extern uint32_t _sidata, _sdata, _edata;
uint32_t *src = &_sidata; // Source: flash
uint32_t *dst = &_sdata; // Destination: RAM
while (dst < &_edata) *dst++ = *src++;
// 2. Zero the .bss section (uninitialized globals)
extern uint32_t _sbss, _ebss;
dst = &_sbss;
while (dst < &_ebss) *dst++ = 0;
// 3. Call any C++ constructors (if using C++)
// __libc_init_array();
// 4. Call main
main();
// 5. If main returns, loop forever
while(1);
}

The symbols _sidata, _sdata, _edata, _sbss, and _ebss are defined by the linker script.

Why Copy .data?

Global variables with initial values (like int count = 42;) are stored in flash at compile time. But the program expects to read and write them in RAM. The startup code copies these initial values from their flash location to their RAM location. The .bss section (uninitialised globals) just needs to be zeroed - no copy from flash needed.

Linker Scripts

The linker script tells the linker where everything goes in the MCU's memory map:

text
/* Linker script for STM32F411 */
MEMORY
{
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 128K
}
SECTIONS
{
.isr_vector : { KEEP(*(.isr_vector)) } > FLASH /* KEEP: nothing references it, so --gc-sections would drop it */
.text : { *(.text*) } > FLASH
.rodata : { *(.rodata*) } > FLASH
_sidata = LOADADDR(.data);
.data : {
_sdata = .;
*(.data*)
_edata = .;
} > RAM AT> FLASH
.bss : {
_sbss = .;
*(.bss*)
_ebss = .;
} > RAM
/* Stack at the end of RAM */
_estack = ORIGIN(RAM) + LENGTH(RAM);
}

The > RAM AT> FLASH directive translates to: allocate the .data section in RAM (the VMA, or virtual memory address), but stash its initial contents in FLASH (the LMA, load memory address). Your startup code copies from LMA to VMA at boot.

The vector table is linked at 0x08000000, yet a Cortex-M core fetches its initial stack pointer and reset vector from address 0. On the STM32F4 that works because, with the BOOT pins in their default state, the chip aliases main flash at 0x00000000, so the core reads the same table at either address. Other boot-pin settings map system memory (the ROM bootloader) or SRAM there instead.

Here's that linker script drawn as a memory map, with the startup code's two loops as arrows:

STM32F411 memory map from the linker script above

MMIO Register Access

Peripherals are driven through Memory-Mapped I/O registers. Each register sits at a specific address listed in the MCU's reference manual. The volatile keyword does the heavy lifting here, stopping the compiler from quietly optimising away reads and writes to hardware registers:

c
// Define a pointer to the GPIO port B output register
#define GPIOB_ODR (*(volatile uint32_t *)0x40020414)
// Set pin 7 high (turn on LED)
GPIOB_ODR |= (1 << 7);
// Set pin 7 low (turn off LED)
GPIOB_ODR &= ~(1 << 7);
// Toggle pin 7
GPIOB_ODR ^= (1 << 7);

In practice, MCU vendors provide header files with all register definitions as structs:

c
// Using vendor-provided CMSIS headers
#include "stm32f4xx.h"
// Enable GPIO port B clock
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
// Configure pin 7 as output
GPIOB->MODER |= (1 << 14); // Set mode bits for pin 7
// Set pin 7 high
GPIOB->ODR |= (1 << 7);

Writing to Hardware Registers Directly

Every peripheral operation is a sequence of register writes. To send a byte over UART:

c
// Clocks: GPIOA and USART2 (USART2 hangs off the APB1 bus)
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
// Hand PA2 (TX) and PA3 (RX) to the USART: alternate function 7
GPIOA->MODER |= (2 << 4) | (2 << 6);
GPIOA->AFR[0] |= (7 << 8) | (7 << 12);
// Baud rate comes from the APB1 clock (PCLK1), not the core clock.
// 16 MHz is the reset default (HSI, no prescaling); recompute if you change clocks.
uint32_t pclk1 = 16000000;
USART2->BRR = pclk1 / 115200; // 16x oversampling: BRR = PCLK1 / baud
USART2->CR1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE; // Enable TX, RX, UART
// Send a byte
while (!(USART2->SR & USART_SR_TXE)); // Wait until TX buffer empty
USART2->DR = 'H'; // Write byte to data register
// Receive a byte
while (!(USART2->SR & USART_SR_RXNE)); // Wait until RX buffer has data
char c = USART2->DR; // Read byte from data register

Every line pokes hardware directly. No abstraction layer, no driver framework, no error handling beyond what you write yourself. That's bare-metal in a nutshell.

Read-Modify-Write Hazards

When you write GPIOB->ODR |= (1 << 7), the CPU reads the entire register, ORs in the new bit, and writes the entire register back. If an interrupt fires between the read and the write and modifies the same register, the interrupt's change is lost. STM32 GPIO ports provide an atomic bit set/reset register (BSRR) to avoid this race condition: one write sets or clears only the bits you name.