STM32
STM32 is STMicroelectronics' family of 32-bit microcontrollers built on Arm Cortex-M cores. It is one of the most widely used MCU families in the world, found in industrial controllers, drones, medical devices, consumer electronics, motor drives, and countless hobby projects. The family spans tiny, inexpensive parts for simple control tasks, ultra-low-power chips for battery devices, high-performance parts running hundreds of megahertz with graphics and Ethernet, wireless variants with Bluetooth or LoRa radios, and even microcontrollers with neural processing units.
That breadth comes with a consistent ecosystem: shared tools for pin and clock configuration, common driver libraries, inexpensive development boards with built-in debuggers, and support from RTOSes, Rust, and other toolchains.
TL;DR
- STM32 MCUs use Arm Cortex-M cores, from Cortex-M0+ to Cortex-M7 and Cortex-M33 with TrustZone.
- Choose a family by performance, power, peripherals, memory, wireless, security, and price.
- STM32CubeMX configures pins, clocks, and peripherals and generates initialization code.
- HAL drivers are portable and high-level; LL drivers are thin and efficient.
- Debug over SWD with ST-LINK; analyze crashes with fault status registers.
- Use low-power modes (Sleep, Stop, Standby) for battery devices, and read-out protection and secure boot for production.
Quick Example
An interrupt-driven UART command handler that sleeps between events, written in the user-code sections of a CubeMX-generated project (huart2 and the LED pin definitions are generated):
Flashing and verifying from the command line:
Core Concepts
Families
Within a family, part numbers encode package, pin count, flash size, and temperature range. Development boards — Nucleo and Discovery — include an ST-LINK debugger, so a USB cable is all that is needed to start.
Configuration and Drivers
STM32CubeMX (standalone or inside STM32CubeIDE) is a graphical tool for choosing pin functions, configuring the clock tree, enabling peripherals, DMA, and interrupts, and generating initialization code for IDE projects, Makefiles, or CMake. Code generation preserves user code between USER CODE BEGIN and USER CODE END markers — anything outside them is overwritten on regeneration.
Many projects mix them: HAL for complex peripherals such as USB, LL or direct register access for timing-critical paths.
Toolchains
Peripherals, Interrupts, and DMA
STM32 parts include GPIO, timers (PWM, input capture, encoders), ADCs and DACs, UART, SPI, I2C, USB, CAN/FDCAN, Ethernet, SDMMC, and cryptographic accelerators, depending on the family. The NVIC prioritizes interrupts; DMA moves data between peripherals and memory without CPU involvement — essential for high-rate ADC sampling, UART streams, and SPI displays. Keep interrupt handlers short and defer work to the main loop or RTOS tasks. See Microcontrollers.
Low-Power Modes
Measure current with a power analyzer; floating pins, enabled debug interfaces, and forgotten peripherals often dominate consumption.
Debugging and Fault Analysis
- SWD with ST-LINK supports breakpoints, watchpoints, and live variable views.
- SWO/ITM or a UART provides printf-style tracing without halting the core.
- HardFault analysis: read the Configurable Fault Status Register (CFSR), the faulting address registers, and the stacked program counter to find the crashing instruction.
- Watchdogs (IWDG, WWDG) recover from hangs in the field; log the reset cause at startup.
RTOS and Firmware Architecture
Simple devices use a superloop with interrupts; complex ones use an RTOS — FreeRTOS (often via CMSIS-RTOS v2), Eclipse ThreadX (integrated into STM32Cube for many families), or Zephyr. An RTOS adds tasks, queues, and timers, plus overhead and new failure modes such as stack overflows and priority inversion. See RTOS.
Security and Production
- Read-out protection (RDP) prevents reading flash through the debugger; the highest level permanently disables debugging.
- TrustZone (L5, U5, H5, and others) separates secure and non-secure firmware.
- Secure boot and firmware update: ST's secure firmware solutions or TF-M verify signed images before running them. See Firmware OTA Updates.
- Unique device IDs and crypto accelerators support device identity and secure communication. See IoT Security.
Best Practices
Choose Parts With Headroom
Pick a part with spare flash, RAM, and pins in a family with larger pin-compatible variants, so features and fixes still fit later.
Keep Generated and Hand-Written Code Separate
Write application code in your own files and keep CubeMX user-code sections minimal, so regeneration never destroys work.
Use DMA for Streaming Peripherals
Interrupt-per-byte designs fail at higher data rates; DMA with half- and full-transfer callbacks keeps up reliably.
Protect Production Devices
Enable read-out protection, signed firmware updates, and watchdogs before shipping, and plan how field units recover from failed updates.
Common Mistakes
Editing Outside User Code Sections
Blocking Calls Inside Interrupts
Shipping With Debug Access Open
FAQ
What is STM32?
A family of 32-bit microcontrollers from STMicroelectronics based on Arm Cortex-M cores, ranging from low-cost and ultra-low-power parts to high-performance and wireless devices.
Which STM32 should a beginner start with?
A Nucleo board with a mainstream part, such as an STM32G4 or STM32F4, is a common choice: inexpensive, well documented, with an on-board debugger and plenty of community examples.
What is the difference between HAL and LL drivers?
HAL drivers offer a portable, high-level API with callbacks and state management. LL drivers are thin functions that map closely to registers, producing smaller and faster code at the cost of portability.
Can I program STM32 in Rust?
Yes. Rust HAL crates and the Embassy framework support many STM32 families, using the same debug probes and flashing tools.
How do I reduce power consumption on STM32?
Use Stop or Standby modes when idle, disable unused peripherals and clocks, configure unused pins as analog, lower clock frequencies where possible, and measure actual current to find leaks.
Related Topics
- Microcontrollers — MCU fundamentals
- Embedded C — The most common STM32 language
- Embedded Rust — Rust on Cortex-M
- RTOS — FreeRTOS, ThreadX, and Zephyr
- Firmware OTA Updates — Updating devices in the field
- IoT Security — Securing connected devices
- Arduino — Prototyping boards and the Arduino ecosystem