Timer Input Capture interrupt handler
Posted: Thu Aug 09, 2012 8:41 pm
I'm using TIM4 on a STM32F107 to time the rising edges of three different signals. I've configured the timer and enabled the three input capture interrupts (rising edges on IC1, IC2, IC3) and the update event (timer overflow/underflow). I am now implementing the interrupt handler. Since my interrupt handler can be called for 4 different reasons (IC1, IC2, IC3 rising edges, or a timer overflow), I'm a bit uncertain whether I should only process 1 interrupt each time the handler is called, or if I should process more than one. By 'process', I mean clear the interrupt flag in TIM4 status register and save data (timer values latched in CCR1,CCR2,CCR3) and or handle the overflow. If I knew that each time my handler was called that there would only be 1 interrupt flag set in the TIM4 status register, it would be obvious that I only process that one. But what if more than one flag is set in the SR? Do I process all of them, or do I only process one and then let the handler get called again? I think that I probably should process all, but maybe this is not true?
Here is what I have so far:
Also, SampleAndControl::timers is a class static data, so should I be protecting these parts with something like chSysLockFromIsr()/chSysUnLockFromIsr()? Any thoughts and/or comments would be greatly appreciated.
Luke
Here is what I have so far:
Code: Select all
// There are 4 ways this interrupt handler gets called.
// 1) Rising edge on IC1 ( Rear wheel encoder A)
// 2) Rising edge on IC2 (Front wheel encoder A)
// 3) Risign edge on IC3 ( Steer encoder A)
// 4) Rising edge on UE (Timer overflow)
CH_IRQ_HANDLER(VectorB8)
{
CH_IRQ_PROLOGUE();
uint32_t sr = STM32_TIM4->SR; // save TIM4 status register
uint32_t dir = GPIOD->IDR; // save GPIOD register; encoder B state
if (sr & 0x0002) { // IC1
STM32_TIM4->SR = sr & ~0x0002; // clear CC1IF
SampleAndControl::timers.CCR[0] = STM32_TIM4->CCR[0];
SampleAndControl::timers.dir[0] = dir & (1 << 12);
} // should this be else if so only one interrupt flag is processed?
if (sr & 0x0004) { // IC2
STM32_TIM4->SR = sr & ~0x0004; // clear CC2IF
SampleAndControl::timers.CCR[1] = STM32_TIM4->CCR[1];
SampleAndControl::timers.dir[1] = dir & (1 << 13);
} // should this be else if so only one interrupt flag is processed?
if (sr & 0x0008) { // IC3
STM32_TIM4->SR = sr & ~0x0008; // clear CC3IF
SampleAndControl::timers.CCR[2] = STM32_TIM4->CCR[2];
SampleAndControl::timers.dir[2] = dir & (1 << 14);
} // should this be else if so only one interrupt flag is processed?
if (sr & 0x0001) { // UE (overflow/underflow)
STM32_TIM4->SR = sr & ~0x0001; // clear CC3IF
// Need to implement code to handle timer overflows
}
CH_IRQ_EPILOGUE();
}
Also, SampleAndControl::timers is a class static data, so should I be protecting these parts with something like chSysLockFromIsr()/chSysUnLockFromIsr()? Any thoughts and/or comments would be greatly appreciated.
Luke