2020-04-17 07:16 AM
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.
Solved! Go to Solution.
2020-04-17 07:58 AM
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.
2020-04-17 07:58 AM
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.
2020-04-17 01:50 PM
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
2020-04-27 11:37 PM
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? :)
2020-04-27 11:53 PM
When implementing a delay loop, you don't want certain optimizations to take place.
2020-04-28 12:22 AM
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.