STM32 HRTIM Prescaler Value

Hello,

can I somehow define the prescaling factor for the STM32 HRTIM unit? In the “Advanced PWM Generation on STM32” document it is said that the value is set during initialization process of the MCU, but can I somehow define it beforehand? Or is it always set to the highest resolution possible with a given PWM frequency? In STMCubeIDE you can set this value manually.

I need the exact value for calculations of my compensator.

Thank you.

Hello Florian,

It is not possible to define the prescale factor for the STM32 HRTIM unit using the block from the target library. As you surmised correctly, we automatically choose the prescale factor such that we can achieve the highest resolution for the desired frequency, with some margin to allow for frequency variation.

The only way to manually specify the prescale factor while using the STM32 TSP is to generate code into an STM32 Cube IDE project and subsequently modify the code before programming the MCU from there. To find the corresponding lines of code, search for the functions LL_HRTIM_TIM_SetPrescaler() and LL_HRTIM_TIM_SetPeriod().

1 Like

Edit: I’d like to comment that the table below isn’t correct for all scenarios as it depends if the frequency is fixed vs. variable, synchronization settings (self synchronized vs synchronized with the master) and the count mode (sawtooth vs. symmetrical).

Hello Florian, I was putting together an analysis for another presentation and thought you mind find it insightful. Below is a table showing the frequencies at which the prescale value changes from one threshold to the next. This for the STM32G4x at a System Clock of 170 MHz. Below the table is some pseudo-code to describing the calculations for the Fx and Gx chips if your System Clock is different.

Interpreting the table, if the Carrier Frequency parameter entered into the component mask exceeds ~93 kHz the prescaler value is 1. If the Carrier Frequency is ~47-93 kHz then the prescalar is 2 and so on.

Prescale
Ratio
scaledHrtimClk
(MHz)
Min. Freq.
@ Prescale
(Hz)
PWM
Resolution
(pS)
1 5440 93407 184
2 2720 46703 368
4 1360 23352 735
8 680 11676 1471
16 340 5838 2941
32 170 2919 5882
64 85 1459 11765
128 42.5 730 23529
sysClk = 170e6; % user defined
prescale = 1;
maxPeriod = 58240; % 16bit register + operational range. Fixed in Coder.
hrtimClk = sysClk*32;
scaledHrtimClk = hrtimClk/prescale;
pwmResolution = 1/scaledHrtimClk;
minFreq=(scaledHrtimClk/maxPeriod);

You’ll notice this is similar to Table 5 in the HRTIM cookbook, but the max period is 2^16. There is a bit of extra margin added into the PLECS Coder to allow for the frequency to go ~10% lower than the value entered in the mask.

1 Like