A blink of the onboard LED (LD2) on an STM32 Nucleo-F401RE, written in bare-metal C at the register level. No HAL, no CMSIS. Every peripheral register is addressed directly by its memory address, so nothing is hidden behind a library.
I built this to understand an ARM Cortex-M part all the way down to the memory-mapped registers, instead of through an abstraction layer.
- Enables the clock to GPIO port A.
- Configures pin PA5 (LD2) as an output.
- Toggles PA5 on and off in an infinite loop, with a simple delay so the blink is visible.
The whole program touches three registers, defined by hand from the STM32F401 reference manual (RM0368):
#define RCC_AHB1ENR (*(volatile uint32_t *)0x40023830) // clock enable for AHB1 peripherals
#define GPIOA_MODER (*(volatile uint32_t *)0x40020000) // pin mode for port A (input/output/...)
#define GPIOA_ODR (*(volatile uint32_t *)0x40020014) // output data (drives the pins high/low)RCC_AHB1ENR |= (1 << 0)turns on GPIOA's clock (bit 0). A peripheral does nothing until its clock runs.GPIOA_MODERsets PA5 to output: PA5 uses bits 11:10, and01means output mode.GPIOA_ODRbit 5 drives PA5 high (LED on) or low (LED off).
The volatile keyword matters here: it stops the compiler from optimizing away reads and writes to hardware registers, and from deleting the empty delay loop.
- Toolchain: STM32CubeIDE (arm-none-eabi-gcc).
- Flash: over the Nucleo's onboard ST-Link (Run in CubeIDE).
- Board: ST Nucleo-F401RE (STM32F401RE, ARM Cortex-M4).
- LED: onboard LD2, wired to PA5.
I also brought up this same board in async Rust (embassy): tinyml-air-monitor. Same STM32, two very different approaches. Raw register writes in C here, a high-level async HAL in Rust there.