cancel
Showing results for 
Search instead for 
Did you mean: 

How to give delay when working gpio toggel?

LACTIC
Associate II

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.

1 ACCEPTED SOLUTION

Accepted Solutions
berendi
Principal

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.

View solution in original post

5 REPLIES 5
berendi
Principal

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

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
 

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? :)

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

Loading and calculation of these variables is an overhead, which has to be minimized. But, if the order of these matters, then start should most likely be loaded first. Thought, as multiplication and subtraction takes constant time, the order doesn't matter as long as the compensation constant is appropriate.