Skip to main content
SShid
Associate III
July 20, 2020
Question

Timer for generating 1us tick

  • July 20, 2020
  • 3 replies
  • 5696 views

I'm using STM32F108RG on 48MHz clock configuration.

I need to generate a 1 micrososeconds timer tick on it, for driving an output pin interfaced with a 3rd party device. Using PWM is not an option available.

I tried configuring TIM3 with following parameters:

Clock: Internal (48MHz)

Prescaler: 47 (48 - 1)

Counter Mode: Up

Counter Period (Auto-Reload Register -16 bit): 1

Internal Clock Division: No Division

Auto-reload preload: Disabled

With these settings, the closest I can reach is 6.8us.

I was able to generate 10us with the above settings with Counter Period set to 10,

but it doesn't seem to go below 6.8us.

I also tried TIM1 for the same purpose, but got the same observations.

Is that a limitation of the device, or do I need to perform any additional steps?

This topic has been closed for replies.

3 replies

Tesla DeLorean
Guru
July 20, 2020

Don't interrupt at these speeds you will saturate the processor.

Have a TIM count at 1 MHz, and spin on TIM->CNT advancing a required amount.

Consider also DWT_CYCCNT​

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
SShid
SShidAuthor
Associate III
July 20, 2020

Thanks for the response, clive1

I'm a complete noob on STM32 platform (otherwise wouldn't have had posted such queries)

So what exactly do you mean by "spin on TIM->CNT"?

Also, what is 'DWT_CYCCNT'?

I gave it a search in the HAL library thinking that it's some sort of peripheral register,

but couldn't find it.

Apologies for asking for more help on this.

Tesla DeLorean
Guru
July 20, 2020

If terms are unfamiliar stick them into Google or the search here, should be preexisting examples and postings.

With TIM3, prescale 47, period 0xFFFF (maximal)

void delay_us(uint16_t us)

{

uint16_t start = TIM3->CNT;

while((TIM3->CNT - start) < us) {};

}

For finer granularity, let the TIM clock faster, use a 32-bit TIM where available

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
TDK
Super User
July 20, 2020

You could use SPI with a 1MHz clock to output an arbitrary signal. Calling an interrupt at 1MHz isn't going to pan out well. It'll take more than 48 ticks for the ISR to complete.

"If you feel a post has answered your question, please click ""Accept as Solution""."
SShid
SShidAuthor
Associate III
July 20, 2020

That would have been a great idea.

Thanks for the response, TDK; but I don't think I have enough SPI peripherals left on my application.

Tesla DeLorean
Guru
July 20, 2020

With hard software timing loops you're going to run out of bandwidth for your application pretty quickly

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
S.Ma
Principal
July 20, 2020

interrupts at 10kHz or higher is the symptom of danger. use hw assist as much as possible. if you have a spare timer pin channel or spi mosi, short it with the one you want to send a pulse pattern by dma if needed. no spare pin? new board design?

Tesla DeLorean
Guru
July 20, 2020

Indeed modulating patterns with fixed timing requirements can be done with TIM+DMA+GPIO more effectively than using timing loops eating all processor resources.

Consider also logic or CPLDs for hardware orientated problems.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..