← BlogBlog

The low-speed drive without an FPU

The last post got sensorless FOC down to standstill on motor 3 with high-frequency injection (HFI). That drive runs on the F302, a Cortex-M4 with a hardware floating-point unit, and every part of it uses f32. This post covers one question about it: would the same low-speed drive run on a Cortex-M0+, such as an STM32G0, which has no FPU?

The short answer is yes. The HFI drive was rewritten in integer arithmetic, run on motor 3 in place of the float version with the same results, and cycle-counted as M0+ code. The worst case is about 1 900 cycles per control tick with the code in RAM and under 3 700 with the code in flash, against a budget of 6 400 at 64 MHz and 10 kHz.

The M0+ is not new to the project. At MS3 a G0B1 board ran the telemetry protocol, to show that the link did not depend on the bigger chip. It has not run a motor.

Why it is not just a recompile

The control core already builds for thumbv6m, the M0+ target, and CI checks that it does. But on an M0+ every f32 add, multiply and divide becomes a call into a software routine, tens of cycles each, and an FOC tick has hundreds of them. The M0+ also has no long multiply: its MULS instruction keeps only the low 32 bits of a 32 × 32 product, so anything that needs a 64-bit intermediate turns into another library call.

So the integer version had two rules: no floating point in the per-tick code, and no product that needs more than 32 bits.

What was built

mmc_core::fixq implements the same algorithms as the float current loop, the HFI tracker and the HFI start, in integers:

Piece Integer form
Currents, voltages, duties Q15: a signed 16-bit fraction of a base value
Angle u32, with 2³² = one electrical turn, so it wraps for free
sin / cos 257-entry Q15 table with linear interpolation, within 4 LSB
Gains x·m >> s with m in [2¹⁴, 2¹⁵) and inputs clamped to ±2¹⁵, so every product stays under 2³⁰
PI integrators Q30, 15 bits below the output’s resolution, so small errors still accumulate
Current loop as in the float Foc: decoupling feed-forward, voltage circle limit by integer square root
HFI tracker the same ++−− carrier, difference demodulation and PLL as before; one 32-bit division per tick for the normalisation
Start and speed loop lock, polarity pulses (compared without dividing), speed loop with the HFI gains and the cross-saturation correction
Modulation min-max (equivalent to SVPWM), one division per tick for 1/Vbus

The gain representation keeps the 32-bit rule. A gain such as 0.0037 cannot be a Q15 number without losing most of its digits, so each gain is stored as a mantissa m and a shift s. Because the mantissa is held between 2¹⁴ and 2¹⁵ and the input is clamped to 16 bits, every product stays under 2³⁰. The unit tests check this across nine decades of gain.

The parameters still arrive as f32 from the parameter table. They are converted once, when the drive starts, and only the per-tick step has to be integer. On the F302 this runs as a cargo feature, fixq: the board converts the ADC readings to Q15 on the way in and the duties back on the way out. A board without an FPU would hand over ADC counts and take timer compare values directly.

On the bench

Before the bench, the integer code was checked in two steps. Seven unit tests compare each piece with its float version, including the integer tracker converging on the same salient rotor from the same currents. Then all 22 simulator tests passed with the fixq feature on, including the HFI start.

On motor 3 (F302 board, 18 V) the integer drive ran with the same parameters as the float runs at the end of the last post: HFI speed gains 8×, cross-saturation correction 0.44, and 6 ms × 8 polarity pulses.

target, rad/s el +5 −5 +10 −10 +20 −20 +50 −50
float: true speed 5.2 −5.0 10.0 −10.1 20.0 −20.1 50.0 −50.1
integer: true speed 5.0 −5.0 9.9 −10.0 20.0 −20.1 50.0 −49.8
float: stick-slip p-p, ° el 318 222 283 213 287 220 184 167
integer: stick-slip p-p, ° el 281 206 273 206 270 203 183 206

Both zero crossings tested, 50 → −50 and 100 → −100 rad/s el, went through on the integer drive as they did on the float one. The angle error during the 100 → −100 reversal was +24 ± 17° on the integer drive against +25 ± 18° on the float drive.

A reversal at 50 rad/s el, float and integer

Real capture · motor 3 · HFI · 50 → −50 rad/s el
-5 0 5 10 15 20 25 30 35 40 0 s 2 s 4 s 6 s 8 s 10 s estimated rotor angle, electrical turns float drive (fw 25) integer drive (fixq)

The drive's own angle estimate, unwrapped, in electrical turns. Each run starts at 1 s with the lock and polarity pulses, runs at +50 rad/s el, and is commanded to −50 near 6 s. The two drives follow the same path; the small offset between the lines comes from where each start sequence left the estimate.

Starts: 17 of 17 reached their target with the polarity right and no overcurrent, with a peak d current of 0.81 A. Three of the batched starts counted 10–12 hall edges during the polarity pulses, which in the last post would have meant the rotor moved. Here the rotor had stopped on a hall boundary and the alternating pulses toggled the sensor between two states without turning it.

On this motor, the integer and float drives could not be told apart.

The cycle count

The F302 can time the integer step directly: 1 508 cycles in the worst case and about 1 400 on average, holding 20 rad/s el. That is a Cortex-M4 with a single-cycle multiply and a hardware divide, so it is a lower bound for an M0+, not an estimate of one.

There was no G0 board on the bench, so the M0+ numbers come from an emulator. A small crate, mmc-fixq-bench, builds the integer step for thumbv6m. A Python script runs the resulting binary in the Unicorn CPU emulator, in closed loop with a salient R-L motor model, and charges each instruction its Cortex-M0+ cycle count. The flash case adds one wait state per 32-bit instruction fetch, per literal load and per taken branch. That is deliberately pessimistic, because the G0’s prefetch buffer hides some of it.

Integer HFI step: cycles per control tick

Worst case · F302 measured · M0+ emulated
integer HFI step, CPU cycles per control tick (worst case) 0 2000 4000 6000 F302 (M4F, 72 MHz), measured 1508 · 21% budget 7200 M0+ emulated, code in RAM 1881 · 29% budget 6400 M0+ emulated, 2 wait states 3617 · 57% budget 6400

Bars are the worst case over a closed-loop run; the outlined box is one control period (7 200 cycles at 72 MHz / 10 kHz on the F302, 6 400 at 64 MHz / 10 kHz on a G0). Means were 1 400, 1 597 and 3 057 cycles.

At 64 MHz and a 10 kHz control rate, the step uses 29% of the period with the code in RAM and at most 57% from flash. At 32 MHz the flash case would not fit; from RAM it would, at 59%. The step is 2.5 KB of code plus the 514-byte sine table.

A CI check now disassembles the M0+ build and fails if the step calls any floating-point or 64-bit helper. Today it calls only the two 32-bit division routines.

On the F302 itself, the full control interrupt with the integer step measured 5 643 of its 7 200 cycles, because it still runs the float telemetry, the flux observer and the hall code around it. Since the F302 build now leaves out six-step and the online estimator, the fixq firmware fits in flash at the normal optimisation level, with 7 KB to spare.

Found while porting

Porting the modulation turned up a difference in the float code. When HFI adds its carrier, the float path rebuilt the duties from the voltage before dead-time compensation. So dead-time compensation had been off whenever HFI ran, in every HFI result so far, including the ones in the last post. The integer path was written to match, so the comparison above is like for like.

This was fixed in the next session. With compensation on under HFI, the stick-slip at ±50 rad/s el dropped from about 210° to about 153° el, and the other speeds were unchanged.

What an M0+ drive still needs

  • The flux observer in integer. The integer path stays on HFI. It has no observer and no handover to one, so it covers the low-speed range only.
  • A G0 board. It needs TIM1 with dead time and a break input, external shunt amplifiers, and one ADC reading the phases one after another. That sequential sampling is acceptable for HFI, because the time skew between phases is constant and the demodulation takes differences of consecutive samples.
  • Real silicon numbers. The bench crate is ready to run on the G0B1 board with SysTick timing, which would replace the emulated column with a measured one.

The code is in the repository: crates/mmc-core/src/fixq.rs, the bench crate crates/mmc-fixq-bench, the emulator script tools/m0_cycles.py, and the write-up docs/FIXQ.md.