Skip to content

STM32 - A Minimal Hello-World Program

This simplest blinking led project goes through documents of microcontroller, the target hardware, and a guide to learn how microcontroller bootups.

Objectives


Functional features:

  • Turn on the board and boot the MCU up
  • Repeatedly turn on and turn off an on-board LED attached to a GPIO

Non-functional features:

  • Understand main steps to boot up an MCU
  • Use Bare-metal programming, without any IDE, library
  • The compiled firmware is as small as possible

Hardware


The target board is Nucleo-64 F411RE board:

  • STM32F411RET6U:
    • Arm® 32-bit Cortex®-M4 CPU with FPU, upto 100 MHz
    • 512 Kbytes of flash memory
    • 128 Kbytes of SRAM
    • Debug mode:
      • Serial wire debug (SWD) & JTAG interfaces
      • Cortex®-M4 Embedded Trace Macrocell™
    • 1 User LED
  • 1 Reset Push-Button and 1 User Push-Button
  • On-board ST-LINK/V2-1 debugger and programmer with SWD connector and USB re-enumeration capability (Virtual COM port, Mass storage, Debug port)

Documents


Nucleo-64 Board User Manual UM1724 STM32 Nucleo-64 boards (MB1136):

  • Board features: MCU, Debugger, on-board Peripherals
  • Hardware settings: Layout, Configuration, Connectors, Jumpers
  • Power supply

Board Schematic MB1136-DEFAULT-C05 Board schematic:

  • Components and Wiring

MCU Datasheet DS10314 - STM32F411RE MCU:

  • Device overview with block diagrams
  • Peripherals and features
  • Memory map and Memory range
  • Pin-out and pin description
  • Electrical characteristics

Reference Manual RM0383 - STM32F411xC/E MCUs:

  • System Architecture with bus, peripherals, and connections
  • Memory map and boundary addresses
  • Boot configuration, vector table relocation
  • Peripheral with details features, descriptions, and structure
  • Registers and bit-fields for all accessible registers
  • Code examples using CMSIS header files

Programming Manual PM0214 - STM32 Cortex®-M4 MCUs and MPUs:

  • Processor Mode and Stack Model
  • Memory Model and Memory Management
  • Exception model, the Interrupt Service Routines, Fault Handling
  • Power Management, Sleep Mode, Wake Up
  • The Instruction Set
  • CMSIS intrinsic functions
  • Core Peripherals:
    • Memory Protection Unit (MPU)
    • Nested vectored interrupt controller (NVIC)
    • System control block (SCB)
    • SysTick timer (STK)

Application Notes

There many Application Note documents provided by ST. Each document present the usage, design, and advice for a specific application or feature.

For example:

Development Tools


List of tools on the host PC for compiling, debugging the target MCU:

ARM Compiler


Install Cross-Compiler ARM GNU Toolchain:

sudo apt install gcc-arm-none-eabi

For the latest version, download the toolchain from Arm GNU Toolchain.

This will install:

  • gcc-arm-none-eabi: Bare metal C and C++ compiler for embedded ARM chips
  • binutils-arm-none-eabi: Programs are used to manipulate binary and object files that may have been created for ARM architecture
  • libnewlib-arm-none-eabi: C library and math library intended for use on embedded ARM chips
  • libstdc++-arm-none-eabi-newlib: C++ library for embedded ARM chips using newlib as standard C library

Install ST-Link package:

sudo apt install stlink-tools

For the latest version, download the package from stlink-org/stlink.

The STLink toolset includes:

  • st-info - a chip information tool
  • st-flash - a flash manipulation tool
  • st-trace - a logging tool to record information on execution
  • st-util - a GDB server
  • stlink-lib - a communication library

Connect the board and probe the on-board ST-Link debugger:

st-info --probe
Found 1 stlink programmers
version: V2J39S27
serial: 0669FF343633464257251222
flash: 524288 (pagesize: 16384)
sram: 131072
chipid: 0x431
dev-type: STM32F411xC_xE