STM32 Firmware Development (HAL, LL, FreeRTOS)
STM32 firmware from part selection to field updates: HAL, LL, FreeRTOS, DMA, CAN and low power
STM32 firmware development is writing the embedded software for ST's Arm Cortex-M microcontrollers: drivers, real-time scheduling, communication buses, power management and field updates. Electuni writes STM32 firmware for product teams building motor controllers, low-power sensor nodes and industrial devices, choosing the STM32 family and driver layer to suit each product.
Design → Prototype → Production
Key Facts: STM32 Firmware Development
- Families
- G4, L0/L4/U5, F4/H7
- Drivers
- HAL, LL, Register Level
- RTOS
- FreeRTOS or Bare Metal
- Buses
- CAN/FDCAN, SPI, I2C, UART
- Low Power
- Stop/Standby with LoRa
- Updates
- Bootloader + Field Updates
What You Get
- STM32 family and part recommendation tied to the product's peripherals and power budget
- Firmware architecture: driver layer choice, task or loop structure and interrupt priorities
- Peripheral drivers using HAL, LL or register-level code where timing requires it
- Bootloader and field update method suited to the product's interfaces
- Fault handling with logged HardFault context and watchdog recovery
- Bring-up and test notes for each peripheral on the target hardware
- Source code in Git with build instructions and a CubeMX configuration file
How We Approach STM32 Firmware Development
STM32 is a large family, and choosing the right series saves work later. The STM32G4 is a common choice for motor control and digital power: it pairs fast ADCs, comparators and op-amps with CORDIC and FMAC math accelerators, and some G4 parts add the HRTIM high-resolution timer. The L0, L4 and U5 series are built for low power, with Stop, Standby and Shutdown modes that suit battery and energy-harvesting nodes. The F4 and H7 series cover heavier processing, from DSP work to graphics. The part is chosen from the peripherals, memory and power budget the product actually needs.
The driver layer is a deliberate choice too. ST's HAL is portable and quick to bring up, and STM32CubeMX generates clock trees and pin configuration that avoid many early mistakes. The LL drivers are thinner and closer to the registers, and are used where interrupt latency or code size matters. Register-level code is kept for the few paths with hard timing. Firmware runs on FreeRTOS when the product has several concurrent activities, such as a radio stack, a sensor pipeline and a user interface. DMA moves ADC samples, SPI frames and UART streams without loading the CPU, and CAN or FDCAN handles robust links between boards.
Field behaviour is designed in from the start. Low-power products are built around the radio's duty cycle: for a LoRa node with an SX1262, the MCU spends most of its life in Stop mode with the RTC running, wakes on a timer or the radio's interrupt, and goes back to sleep as quickly as possible. A bootloader lets the product be updated over UART, USB, CAN or the air, with image checks before the new firmware runs. HardFaults are captured with their fault registers and stacked program counter, so a crash in the field becomes a line of code rather than a mystery.
Technical Constraints and Trade-offs
- HAL drivers add overhead in interrupt paths, so time-critical loops such as current control often use LL or register-level code for those paths only.
- On STM32H7 and other cores with a data cache, DMA buffers need cache maintenance or non-cacheable memory regions, or the CPU and DMA see different data.
- Low-power modes switch off clocks and some peripherals. Every peripheral, GPIO and debug setting must be checked in Stop or Standby, because one pull-up or enabled clock can dominate the sleep current.
- For motor control, ADC sampling must be triggered by the PWM timer at the right point in the switching period. Software-triggered sampling captures switching noise.
- Cortex-M0+ parts such as the STM32L0 lack the configurable fault status registers of M3/M4/M7 cores, so HardFault analysis there relies more on the stacked registers.
- Read-out protection level 2 permanently disables debug access, so it is applied only after the update and test process no longer needs SWD.
Fabrication and Assembly Handoff
- Git repository with full history, toolchain version and build instructions
- STM32CubeMX project file recording clock tree, pin and peripheral configuration
- Memory map and linker script showing bootloader, application and data regions
- Update procedure for the bootloader, including image format and any signing steps
- Option byte and read-out protection settings for production programming
- Programming instructions for the production line using STM32CubeProgrammer
From a real project
LoRa sensor node with an STM32L052 and SX1262
A battery-and-solar powered node that monitors electric fence lines outdoors, with no mains power and no network, and must report line condition over long range for years without a site visit. It pairs an STM32L052 ultra-low-power microcontroller, chosen for its sleep current, with an SX1262 LoRa transceiver, a 2S LiFePO4 pack with MPPT solar charging, and a sensing front-end for 12-15 kV line pulses. The radio duty cycle has to fit what the solar harvest actually delivers.
Read the project on our PCB division's site (opens in a new tab)How an Electuni Engagement Works
Step 1
Discovery
Requirements analysis and feasibility study.
Step 2
Architecture
System design, component selection, and risk assessment.
Step 3
Design
Schematic, layout, firmware, and mechanical CAD.
Step 4
Review
DFM/DFT checks and design review with stakeholders.
Step 5
Prototype
Board fabrication, assembly, and firmware bring-up.
Step 6
Handoff
Production files, BOM, and full documentation package.
Tools & Technologies
- STM32CubeMX
- STM32CubeIDE
- FreeRTOS
- STM32CubeProgrammer
- ST-LINK / SWD
- Git
STM32 Firmware Development FAQ
Should STM32 firmware use HAL or LL drivers?
Often both. HAL is quick to bring up and easy for another engineer to maintain, so it suits configuration and non-critical paths. LL drivers are thinner and faster, so they suit interrupt handlers, high-rate DMA and control loops. Mixing them per peripheral is supported by ST and is usually more practical than committing the whole project to one layer.
Which STM32 is best for motor control?
The STM32G4 series is a frequent choice for FOC and digital power. It combines advanced timers with dead-time insertion, fast ADCs with injected conversions, comparators for overcurrent protection and CORDIC and FMAC accelerators, and some G4 parts add HRTIM. The final part depends on motor count, current sensing method and the communication interfaces the controller needs.
How do you debug a HardFault on an STM32?
A fault handler saves the stacked registers, including the program counter and link register, plus the fault status and address registers on cores that have them. That shows which instruction failed and why, such as a bad pointer, stack overflow or unaligned access. The data can be logged to retained RAM or flash and read back after a watchdog reset.
How are STM32 products updated in the field?
Either through ST's built-in system bootloader, which supports interfaces such as UART and USB on many parts, or through a custom bootloader in the first flash sectors. A custom bootloader can accept images over CAN, a radio link or external flash, check integrity and signature, and keep a fallback image where flash allows.
Can an STM32 run a LoRa node for years on a battery?
Low-power series such as the STM32L0 and STM32U5 are designed for it, but battery life is set by the whole system: transmit frequency and power, sensor wake time, regulator quiescent current and leakage. The firmware keeps the MCU in Stop mode between events, and the actual sleep and transmit currents are measured on hardware rather than taken from datasheets.
When is CAN or CAN FD the right bus for a product?
When several boards or modules must exchange messages reliably over cable in an electrically noisy environment, such as vehicles, machines or battery systems. CAN arbitrates and checks errors in hardware. CAN FD, supported by the FDCAN peripheral on STM32G4, H7 and other series, allows larger payloads and a faster data phase.