2012-09-10 03:26 AM
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 #stm322012-09-10 06:21 AM
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
2012-09-10 07:35 AM
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.2012-09-10 08:21 AM
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
2012-09-11 04:47 AM
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!!2012-09-11 06:17 AM
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