Skip to main content
LACTIC
Associate III
April 17, 2020
Solved

How to give delay when working gpio toggel?

  • April 17, 2020
  • 2 replies
  • 1316 views

Hello ,

I am using Standard Peripheral Library with STM32F3VCT6 mcu. I want give delay without using timer of it means want give delay by using RCC clock. Is there any thing by which it can be happen.

This topic has been closed for replies.
Best answer by berendi

RCC won't help you at counting elapsed clock cycles. There are a plenty of different timers on the STM32F3 series, depending on the exact model.

Basic timers, general purpose timers, advanced timer, a high-resolution timer, SysTick timer, DWT cycle counter, or the RTC for longer intervals. Pick one.

2 replies

berendi
berendiBest answer
Principal
April 17, 2020

RCC won't help you at counting elapsed clock cycles. There are a plenty of different timers on the STM32F3 series, depending on the exact model.

Basic timers, general purpose timers, advanced timer, a high-resolution timer, SysTick timer, DWT cycle counter, or the RTC for longer intervals. Pick one.

Nikita91
Lead II
April 17, 2020

If you want a delay of few microseconds you can use the cycle couter. Example:

bspTsMhzDiv is the MCU sysclk frequeny in MHz (168 if the sysclk is 168MHz)

#pragma GCC push_options
#pragma GCC optimize ("O3")
 
void bspDelayUs (uint32_t us)
{
	volatile uint32_t cycles = (bspTsMhzDiv * us) - 10 ;	// Remove some cycles for instructions
	volatile uint32_t start = DWT->CYCCNT ;
 
	do
	{
	} while ((DWT->CYCCNT - start) < cycles) ;
}
 
#pragma GCC pop_options
 

Piranha
Principal III
April 28, 2020

The cycles and start variables doesn't need to be volatile, which can potentially limit some optimization. And what's the point of using do-while in place where simple while does the same? :)

berendi
Principal
April 28, 2020

When implementing a delay loop, you don't want certain optimizations to take place.