I was using STM32F4 and i had created a microsecond delay function using DWT register. And now, i'm using STM32F030F4P6. And this MCU has not DWT register. So, i tried to create another delay function.
void delay_ticks(uint32_t ticks)
{
SysTick->LOAD = ticks;
SysTick->VAL = 0;
SysTick->CTRL = SysTick_CTRL_ENABLE_Msk;
// COUNTFLAG is a bit that is set to 1 when counter reaches 0.
// It's automatically cleared when read.
while ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) == 0);
SysTick->CTRL = 0;
}
static inline void delay_ms(uint32_t ms)
{
delay_ticks((ms * 8000)); // number of ms*8000000 can overflow so i use 8000 instead of (ms * 8000000) / 1000)
}
when i try to set delay to 1 second, i get 8 seconds. And when i try to set delay to 500 miliseconds i get 4 seconds. There is always a 1:8 ratio
note: the VAL register is automatically loaded with the value of the LOAD register when the timer is enabled.
EDIT: I added these lines of code to choose HCLK as a source and now it works. (HCLK/8 is default value. so i need this configuration):
SysTick->CTRL &= ~SYSTICK_CLKSOURCE_HCLK_DIV8;
SysTick->CTRL |= SYSTICK_CLKSOURCE_HCLK;