Try Embedded

Nucleo-F401RE: blink an LED

A first bare-metal program on an STM32: blink LD2 and an LED on D7 by writing GPIOA's registers, timed by SysTick.

Basic · Beginner

In a real product: Device heartbeat LED (Consumer electronics): A blinking LED shows the firmware is alive.

The Nucleo-F401RE carries an STM32F401RE: an Arm Cortex-M4F with 512 KB of flash and 96 KB of RAM. Its headers are laid out like an Uno's, but every pin is really a port pin with a name such as PA5 or PC13, and here you drive them straight through the chip's registers, with no Arduino core and no HAL.

This program blinks the board's green LED, LD2 on PA5, and an LED on D7 (PA8) the other way round, so one is always lit.

What you'll learn

  • Turning on a GPIO port's clock in RCC->AHB1ENR: a peripheral with its clock off ignores every write
  • Making a pin an output with two bits of GPIOx->MODER, and driving it with GPIOx->BSRR
  • Counting milliseconds with SysTick and its interrupt, the Cortex-M's own timer

How it works

After reset the chip runs from its 16 MHz internal oscillator (HSI), and SystemCoreClock says so. main() first sets GPIOAEN so port A gets a clock, then writes 01 (output) into the MODER bits of pins 5 and 8.

BSRR sets a pin with its low 16 bits and clears one with its high 16 bits, in a single write. Unlike ODR ^= ..., nothing can sneak in between a read and a write, so an interrupt that changes another pin of the port is never undone.

SysTick counts down from LOAD at the core clock. With LOAD = SystemCoreClock / 1000 - 1 it reaches zero every millisecond, and SysTick_Handler adds one to ticks. delay_ms() waits until enough ticks have passed.

An STM32 pin drives 3.3 V, so with a red LED's 2 V drop the 220 Ω resistor gives (3.3 V − 2 V) / 220 Ω ≈ 6 mA.

Try this

  • Open the register viewer and watch bits 5 and 8 of GPIOA's ODR take turns.
  • Comment out the RCC->AHB1ENR line and run it: nothing lights, and the simulator tells you why.
  • Make LD2 flash twice quickly, then pause, like a heartbeat.

main.c

// Bare-metal C on the Nucleo-F401RE: no Arduino core and no HAL. CMSIS names every register.
// Blink LD2 (PA5) and an LED on D7 (PA8) in turn, every half second.
#include <stdio.h>
#include "stm32f4xx.h"

static volatile uint32_t ticks;   // milliseconds since SysTick started

void SysTick_Handler(void) {
  ticks++;
}

static void delay_ms(uint32_t ms) {
  uint32_t start = ticks;
  while ((uint32_t)(ticks - start) < ms) {}
}

// printf sends each character here: USART2 on PA2, which the ST-LINK carries to the Serial Monitor.
int __io_putchar(int ch) {
  while (!(USART2->SR & USART_SR_TXE)) {}
  USART2->DR = ch;
  return ch;
}

int main(void) {
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
  RCC->APB1ENR |= RCC_APB1ENR_USART2EN;
  // PA5 and PA8 outputs (01), PA2 alternate function (10).
  GPIOA->MODER = (GPIOA->MODER & ~(GPIO_MODER_MODER5 | GPIO_MODER_MODER8 | GPIO_MODER_MODER2))
               | GPIO_MODER_MODER5_0 | GPIO_MODER_MODER8_0 | GPIO_MODER_MODER2_1;
  GPIOA->AFR[0] = (GPIOA->AFR[0] & ~(0xFu << 8)) | (7u << 8);   // PA2: AF7, USART2 TX
  USART2->BRR = SystemCoreClock / 115200;                       // 16 MHz HSI after reset
  USART2->CR1 = USART_CR1_TE | USART_CR1_UE;

  SysTick->LOAD = SystemCoreClock / 1000 - 1;   // 1 ms
  SysTick->VAL = 0;
  SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk;

  printf("Blinking LD2 and D7\r\n");
  while (1) {
    GPIOA->BSRR = GPIO_BSRR_BS5 | GPIO_BSRR_BR8;   // LD2 on, D7 off
    delay_ms(500);
    GPIOA->BSRR = GPIO_BSRR_BR5 | GPIO_BSRR_BS8;   // LD2 off, D7 on
    delay_ms(500);
  }
}

Lessons behind it