cancel
Showing results for 
Search instead for 
Did you mean: 

Measuring a time between two events with SysTick on STM32F4?

Bogdan
Senior
Posted on October 23, 2014 at 20:45

Hello all, i want to build a function like current_time = time_now - last_time,  more exactly i wanna  get the time from two of the same event (reading some mems sensors)

I'ved google`it a bit and found that most use the Systick timer built in the ARM M4 core,  but  here i have a question since my experience with systick is very small

Logicaly speaking this means that i must have a timer that runs all the time in paralel with my software, some kind of RTO clock?

But the systick does not gennerate interrupt when it overflows ?

Supose i want to increment at every us, thus getting the timestamp in microseconds, and my concern is wouldn`t this generate a interrupt every us? Halting my code?

Some advices would be apriciated...

Thank You

3 REPLIES 3
Posted on October 23, 2014 at 20:53

The SysTick is a 24-bit count, you get the ''SysTick'' interrupt when it reloads.

Suggest you use

https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/Flat.aspx?RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/compensating%20latencies%20on%20STM32F4%20interrupts&FolderCTID=0x01200200770978C69A1141439FE559EB459D7580009C4E14902C3CDE46A77F0FFD06506F5B&cu...

for very fine tick measurement (ie 168 MHz to 6 seconds), or a 32-bit TIMx (ie 84 MHz to 1 seconds)

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 23, 2014 at 20:57

For 1us granularity suggest you prescale a 32-bit TIMx to 1MHz, you'd have a wrap time in the order of 4295 seconds (1.19 hours)

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Bogdan
Senior
Posted on October 23, 2014 at 22:12 Thank you clive, tryied using timer2 as a plain time base, like bellow My core is running at 168MHZ , APB1 prescaler 4 So loading a value of 0xFFFFFFFF wich means 4294967295, this results in how many microseconds the timer counts before reset? That means (4294967295/1000000)/3600 would give 1.19 hours ? It's sufficient for me (won`t be flying the quadcopter that much :))) )

TIM_InitStruct.TIM_Prescaler = ((SystemCoreClock / 1000000) / 2) - 1;
TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_InitStruct.TIM_Period = 0xFFFFFFFF;
TIM_InitStruct.TIM_ClockDivision = TIM_CKD_DIV1; 
TIM_InitStruct.TIM_RepetitionCounter = 0; 
TIM_TimeBaseInit(TIM2, &TIM_InitStruct);
TIM_Cmd(TIM2, ENABLE);

and for reading the actual ''uptime'' since last reset i just used uint32_t TIM_GetCounter(TIM_TypeDef* TIMx) from the stm32f4xx_tim.c library