Skip to content

About

Bare-metal C on STM32 Nucleo-F401RE (ARM Cortex-M4): register-level GPIO, no HAL or CMSIS.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

STM32 Bare-Metal C: LED Blink (Nucleo-F401RE)

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.

What it does

  1. Enables the clock to GPIO port A.
  2. Configures pin PA5 (LD2) as an output.
  3. Toggles PA5 on and off in an infinite loop, with a simple delay so the blink is visible.

The register-level part

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_MODER sets PA5 to output: PA5 uses bits 11:10, and 01 means output mode.
  • GPIOA_ODR bit 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.

Build and flash

  • Toolchain: STM32CubeIDE (arm-none-eabi-gcc).
  • Flash: over the Nucleo's onboard ST-Link (Run in CubeIDE).

Hardware

  • Board: ST Nucleo-F401RE (STM32F401RE, ARM Cortex-M4).
  • LED: onboard LD2, wired to PA5.

Companion project

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.

About

Bare-metal C on STM32 Nucleo-F401RE (ARM Cortex-M4): register-level GPIO, no HAL or CMSIS.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages