Skip to main content
dejan
Associate II
July 3, 2020
Solved

STM32H7 hrtimer example(s)

  • July 3, 2020
  • 20 replies
  • 5452 views

Are there some working hrtimer examples for STM32H7 floating around?

This topic has been closed for replies.
Best answer by

There are a couple in the CubeH7 distribution.

20 replies

Best answer
July 4, 2020

There are a couple in the CubeH7 distribution.

dejan
dejanAuthor
Associate II
July 4, 2020

OK, I have found these and played around a bit and find an interesting discovery. According to the data sheet STM32H7 HRTIM clock is Sysclk (480MHz). However, there are reserved settings of clock prescaler in HRTIM_TIMXCR registers, which actually multiply HRTIM clk up to 6.4GHz ! I have tried it and it seems to be working. Perhaps there are some settings/modes where it does not work, so they cut it out from the datasheet?

July 4, 2020

How did you verify that the timer really works with 6.4GHz clock?

dejan
dejanAuthor
Associate II
July 4, 2020

I programed it to generate squarewave with complementary outputs and deadtime and simply varied value of prescaler parameter while observing outputs with an oscilloscope.

July 4, 2020

Prescaler effectively divides the clock rate. Did you try anything with prescaler=0?

dejan
dejanAuthor
Associate II
July 4, 2020

According to the datasheet, prescaler values below 5 (101) are reserved, but actually MULTIPLY hrtim clk by 2, 4, 8...

July 4, 2020

Ok. So what was the highest frequency you were able to generate (as seen on the scope)?

dejan
dejanAuthor
Associate II
July 6, 2020

My scope goes up to 100MHz, so I can only see squarewaves up to ~30MHz. However, time resolution matches 6.4GHz clock - that is, if I program prescaler to 0 and period register to 3200, I can see 2MHz output signal. Then I programmed the other channel with period of 3201 and now these two channels are not in-sync anymore, since second channel period is longer for 156pS.

dejan
dejanAuthor
Associate II
July 6, 2020

Actually, it's even better - I just realized that HRTIMSEL bit in CFGR was reset, so HRTIM was supplied with 200MHz clock. Now I set it to 1 (HRTIM base clock is now 400Mhz) and thing still works - calculated clock is now 12.8GHz !!!

July 6, 2020

That is interesting. Now the question is if the signal is clean and stable at those frequencies. I should be able to see till about 2GHz on my scope, i'll try to check it.

Do you use rev Y or V of the chip?

dejan
dejanAuthor
Associate II
July 6, 2020

Chip version register is 0x2003. Again - I am using relatively simple configuration, where I generate two signals in anti-phase with dead time, using single timer and two outputs. My Nucleo board does not have crystal mounted, so I use internal 64Mhz RC oscillator as a reference - therefore it's inherently not very stable, but nevertheless everything works as it should.

July 6, 2020

Can you attach the code used?

dejan
dejanAuthor
Associate II
July 6, 2020

Here's the code. This one uses TimerA and TimerB, where period registers are programmed with 3200 and 3201. CHA1 (PC6) and CH2B (PA8) are programmed to generate squarewave outputs. You should get 4.0Mhz on PC6 and 3.998750MHz on PA8.

Vincent Onde
ST Employee
July 10, 2020

​Hello,

There is no multiplier in this MCU, but I agree the timer behaves as if the clock was multiplied.

It is actually dithering between adjacent values. This is not visible with a 100MHz scope, but the minimum resolution you'll get is always 2.5ns (at 400MHz), whatever the prescaler. The interesting point you've noticed is that in average, the frequency resolution will be much finer. The average is over 32 periods, or  16 periods,... depending on the clock prescaler value. It applies also for duty cycle, to some extend: for some frequencies, the dithering will not work. This is one of the reasons why this operating mode is reserved in the spec.

Congratulations anyway for your findings and analysis!

Best regards,

Vincent

dejan
dejanAuthor
Associate II
July 10, 2020

Yes, I understand that there's no multiplier, but it's a pity ST did not explain in detail how that "multiplying" works - and that applies to other families that have HIRES timer.