cancel
Showing results for 
Search instead for 
Did you mean: 

Timer Duration Count With Interrupt

dclark9
Associate II
Posted on September 10, 2012 at 12:26

A fairly basic thing to do which I am struggling to find any precise examples for.

I want to be able to setup a Comms timeout - specifically on an SPI connection whereby if  I don't get a response to a command I send within 5 seconds, I throw an error.

I am trying to do as much of my development as possible using interrupts, so my ideal way of implementing this would be to have a timer that I start, and which then generates an interrupt after 5 seconds if I don't get a response - and if I do get a response I just stop the timer.

Would this be the best way to implement this sort of functionality, and does anyone know of any examples/or can give me one to help me get this up and running?

Many thanks in advance for any responses,

Dave

#stm32-timer #stm32
5 REPLIES 5
emalund
Associate III
Posted on September 10, 2012 at 15:21

so my ideal way of implementing this would be to have a timer that I start, and which then generates an interrupt after 5 seconds if I don't get a response - and if I do get a response I just stop the timer.

 

 

Would this be the best way to implement this sort of functionality,

 

 

yes
Posted on September 10, 2012 at 16:35

With a timer and a 16-bit prescale, 16-bit time base you should be able to get close to a minute on a 72 MHz processor/timer clock.

A more common way on the STM32, Cortex parts, is to use the SysTick to create a nice periodic timer at say 10, 100 or 1000 Hz and have it decrement some expiration timers and call the appropriate routines. This might make more sense than committing a single hardware timer to each timeout.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
emalund
Associate III
Posted on September 10, 2012 at 17:21

A more common way on the STM32, Cortex parts, is to use the SysTick to create a nice periodic timer at say 10, 100 or 1000 Hz and have it decrement some expiration timers and call the appropriate routines. This might make more sense than committing a single hardware timer to each timeout.

 

agreed

Erik
dclark9
Associate II
Posted on September 11, 2012 at 13:47

Thanks for the replies - eventually went with Systick running at 100ms intervals with the ISR routine then calling duplicate signature functions in each of the code modules that might want to keep track of elapsed time by iterating through an array of pointers to the functions.

I'd be absolutely lost without the people on this forum!!

emalund
Associate III
Posted on September 11, 2012 at 15:17

with the ISR routine then calling duplicate signature functions in each of the code modules that might want to keep track

 

 may be OK, however I am a great proponent of KISS (Keep ISRs Short and Simple) thus my implementation is like this

typedef enum

{

 TIMER_FOR_X,

 TIMER_FOR_Y,

NO_OF_SYSTICK_TIMERS

}TIMERS;

char/int/... TimerStack[NO_OF_SYSTICK_TIMERS]

systick ISR:

for (i=0; i< NO_OF_SYSTICK_TIMERS; i++

{

if TimerStack[i]

{

TimerStack[i]--

using a timer in the main

to start set the desired time

to check

if TimerStack[mine] = 0

{

time expired

Erik

PS yes, this is not ''good code'' but you should get the idea