High resolution timer on STM32, tricks maybe

Aug 30, 2025 Last reply: 10 months ago 27 Replies

Hi



I am working on an application where I need pico second timing in a power stage.



My go to controller, the STM32G071 only have 128MHz timer clock, and I am running the stage at 250kHz, leaving 0.2%/8ns resolution on the duty cycle.



I need more. I could use the STM32F334, which has picosecond timer, using 144MHz timer clock followed by 217ps delay lines. But. it's 3 USD in quantity.



Any other options that are low cost? (I guess FPGA's can do it, but none comes close to much less than 3 USD)



A 20-ns RC ramp driven by a separate CMOS buffer, with a level detector made from a spare DAC channel and a FIN1002 line receiver.

We do that in our TDR and sampler offerings—with two FIN1002s, the relative jitter is way down in the single-digit picoseconds.

Cheers

Phil Hobbs

A spare on-chip comparator channel might be good enough, depending on the requirements.

Cheers

Phil Hobbs

We use FIN1101 as a 1 ns comparator. It's great. If the data sheet said "comparator" it would cost five times as much.

LVDS's do have a lot if deliberate input offset, but that calibrates out.

Why picoseconds in a power stage?

Can you dither it? We're doing that in a class-D amp to get around some near-the-rail nonlinearities.

PID control loops will often dither themselves.

That STM has DACs and one of them could be used to improve PWM resolution if you really need to.

If you are using mark-to-space to adjust output power, you can find you need that to get fine control.

Dithering does work, but it works best if you are dithering between two levels that are very close together.

But you don't want the output to move around much

Really? The nice thing about PWM is that it's pretty efficient and you don't have to cope with much waste heat.

At Cambridge Instruments, around 1988, we used the middle 1.25nsec of a

2.50nsec ramp. The clock ran at 800MHz and the logic was Gigabit Logic's GaAs parts, and we used broad-band transistors to make the ramp. It got auto-calibrated ever few minutes but that didn't take long, and we were ping-ponging between two parallel timers which over-lapped. The project didn't make it to volume production, but we had several working prototypes running well enough to demonstrate for about six months.

ECLinPS was almost as fast a few years later, and Motorola could make it with a decent yield, which Gigabit never really mastered.

stm32g474 also hrtimer, afair ~180ps resolution. it is something like $3 in singles at jlcpcb, so I can't imagine they are anywhere hear $3 in volume

Yeah, I was looking at Western prices:

formatting link
Volume price at LCSC is 2USD:

formatting link

Nice idea, it could even be done with a second PWM channel, to avoid the need for a DAC. In any case it needs a good stable supply for the buffer.

The micro I have has a 20ns comparator, but that's much too slow for this addition

The FIN1002 is 20 cents at LCSC

Did a quick simulation:

formatting link

The transfer function from the DAC to the PWM is logaritmic, so like you say need to do calibration, or alternatively switch to a current source feeding into a capacitor. However, a current source with 10ns compliance is not that easy, easier with the RC

I could possibly do dithering. This is for a halfbridge stage feeding current into a battery. The battery voltage is solid, so need high resolution of the output PWM to get good accuracy on the charging current.

Could add a LC filter with larger output capacitors to bring the cutoff frequency down, allowing for dithering, but trying to reduce cost, thus minimum capacitor size that the ripple current allows for.

Noise in the system will probably result in some dithering anyway, coming from quantization of the current measurement.

Since the battery voltage is solid, I have turned down the integration gain of the compensator, and then running an outer current loop to regulate the PWM.

Yes. I did a bunch (sold tens of thousands) of electric meters that used a single-slope 7-bit ADC to get infinite current sensor resolution. Just added a triangle to the current transformer signal, which smeared out a few LSBs.

0.2% isn't enough resolution for a battery?

If the loop is PID, the output must average exact. It will dither itself as needed.

Or, add a few LSBs of gaussian or pseudorandom noise to the PWM, and that will smear the quantization and make things linear to below an LSB.

Over the top, just for fun, delta-sigma the LSB to get, say, 32 bit resolution.

All those are pretty much equivalent. Batteries aren't very wideband devices.

The input voltage to the buck is 48V, so the resolution at 0.2% equates to 0.1V discrete steps. The internal resistance of the battery is 4mohm, resulting in current resolution of 25A.

I need of course to add lead resistances, and my current sense of 1mohm, but it's still way above what I need. I would expect to be able to control the current in 100mA steps.

One side effect of this, is how the controller is actually started. If the setpoint (duty cycle times input voltage) is different than the battery voltage, a large current will flow.

For example, the BQ25756 charge controller:

formatting link
Looks like they are doing a current mode loop, so cycle per cycle peak current control, so they do not apply/guess a duty cycle, they ramp the reference up very slowly in the current loop, to avoid setting a duty cycle.

Do you need this resolution per cycle? AFAICS battery act like a big capacitor, so will average the current. Changing time from cycle to cycle you can make average much more accurate than any single cycle. For example in 1 ms interval you can get 0.00078% accuracy, which with data above leads to 100mA resolution. I think John is saying the same in different words.

There's resistance and no doubt inductance in series with the battery, which essentially lowpass filter the loop. So current averages over many PWM cycles.

I wonder what the time domain response of the battery is. I think that ions move slowly.

Yes, it all helps, so the dithering might just be the way forward.

Some information on the model of a battery cell:

formatting link
I guess it's time to bring out the Bode 100 and do a sweep.

Some hints here, at page 9:

formatting link
DC = 100mohm

1kHz = 80mohm 1MHz = 1.7ohm

Equivalent parallel impedance at 1kHz is 0.4ohm, capacitance = 400uF

A lot higher than I expected. You could run the power stage inductor directly into the battery without a parallel capacitor.

But, there's a lot of inductance also, at 1MHz it's dominant, about

0.3uH. So at 250kHz, the battery looks like an inductor, not a capacitor. So the inductor will help to reduce the current at 250kHz.

The in an STM32G4 has optional dithering build in, afaiu it encodes 4 extra bit into a 16 cycle pattern of longer and shorter PWM cycles

or you could combine it, make a delta-sigma with, say 8 bit output, and stick that 8 bit into a pwm

First-order loop; easy to stabilize.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required