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