FirmwareSTM32H743
STM32H7 Field-Oriented Motor Controller with CAN-FD Telemetry
20 kHz closed-loop BLDC FOC firmware on STM32H743 with zero-copy DMA ADC sampling and deterministic CAN-FD telemetry.
Hardware & Firmware Specifications
- Target Platform
- STM32H743
- Protocols & Interfaces
- CAN-FD · SPI · SWD
- Control Loop Rate
- 20 kHz (50 µs budget)
- CPU Utilization
- 31% @ 480 MHz
- Telemetry Bus
- CAN-FD @ 5 Mbps
- Encoder Interface
- 14-bit Magnetic SPI
#C++20#Bare-Metal#DMA#FOC#CMake
Problem & Requirements
Brushless actuator nodes in robotic joints require deterministic current-loop timing without jitter, while streaming high-rate torque, velocity, and fault telemetry back to a central host over a shared bus.
Key constraints:
- Hard real-time current loop executing every
50 µs(20 kHz) synchronized to center-aligned PWM timers. - Zero CPU polling on phase current ADCs and magnetic angle encoder reads.
- Fail-safe hardware/firmware interlocks for over-current, bus-off, and watchdog recovery.
Firmware Architecture
The control path runs entirely inside a single high-priority timer/ADC DMA completion interrupt, decoupling deterministic math from lower-priority telemetry and command parsing.
TIM1center-aligned PWM triggersADC1/ADC2dual-simultaneous phase current conversions at the zero-vector midpoint.DMAtransfers current samples directly intoDTCMRAM (zero wait states, no cache coherency overhead).SPI1reads the 14-bit magnetic encoder in parallel before the Clark/Park transform and PI current regulators update PWM compare registers.
extern "C" void ADC1_2_IRQHandler() {
const auto i_abc = current_sense.read_dma_dtcm();
const float theta_elec = encoder.read_electrical_angle();
const auto dq = foc::clark_park(i_abc, theta_elec);
const auto v_dq = pid_dq.step(target_iq, dq);
const auto duty = foc::svpwm(v_dq, theta_elec, bus_voltage_v);
pwm_stage.write_compare_registers(duty);
}
Bench Verification
- Timing Jitter: Measured
< 180 nsloop execution jitter on a 200 MHz oscilloscope via a GPIO toggle pin during full 5 Mbps CAN-FD bus load. - Fault Recovery: Verified hardware
TIM1_BKINtrip latches outputs off in< 400 nsduring simulated phase-short testing.