Timer Input Capture interrupt handler

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Timer Input Capture interrupt handler

Post by hazelnusse »

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:

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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Timer Input Capture interrupt handler

Post by Giovanni »

An easy and safe way is the following:

1) Read the SR register into a variable.
2) Write in the SR the same variable negated this clears all interrupts that were set at the time of SR read.
3) Serve all bits in the variable, no need to clear anything.

I am going to update all drivers using TIMx to do exactly this, the current method is not as good.

chSysLockFromIsr()/chSysUnLockFromIsr() must be used if you call I-Class functions OR if you need mutual exclusion with higher priority interrupts.

Giovanni
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: Timer Input Capture interrupt handler

Post by hazelnusse »

Should step 2) be:

Code: Select all

uint16_t sr = STM32_TIM4->SR;
STM32_TIM4 = ~sr;  // should this be '&=' instead of '='  ?

All the interrupt bits in the TIMX->SR are rc_w0, which means:
read/clear (rc_w0): Software can read as well as clear this bit by writing 0. Writing ‘1’ has no effect on the bit value.

If I understand this correctly, this means &= is not needed, as you suggest. Just want to make sure.

Luke
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Timer Input Capture interrupt handler

Post by Giovanni »

You understood it correctly.

Giovanni
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: Timer Input Capture interrupt handler

Post by hazelnusse »

I finished implementing the interrupt handler and have tested it out and it seems to work as expected. I need to test with all three input lines but at least currently it works with one input just fine. In case anybody is interested, the code is here:

https://github.com/hazelnusse/robot.bicycle/blob/master/Interrupts.cpp

When I enable the interrupt, I do

Code: Select all

  nvicEnableVector(TIM4_IRQn, CORTEX_PRIORITY_MASK(7));


I haven't directly enabled any other interrupts at this point, although I think some are enabled because I'm using the Serial shell and the MMC/SD card along with FatFS and am pretty sure both of those are interrupt driven. I may have two other interrupts for EXTI lines, and they would be lower priority than this interrupt (so higher number). I sort of just guessed that 7 would be a reasonable priority, but am not sure. How should I go about deciding the priority of this interrupt vector?

Thanks,
Luke
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Timer Input Capture interrupt handler

Post by Giovanni »

It must be between 2 (highest) and 15 (lowest). Levels 0 and 1 are reserved for very low latency interrupts that cannot interact with the kernel directly.

The priority level depends on your design, you have to decide if higher priority interrupts can interfere with your application. Latency and jitter are the keywords. May be this article can help:

http://www.chibios.org/dokuwiki/doku.ph ... les:jitter

Giovanni
hazelnusse
Posts: 77
Joined: Thu May 24, 2012 8:01 am

Re: Timer Input Capture interrupt handler

Post by hazelnusse »

Giovanni,
Thanks again for the thoughtful responses. I just wanted to report back that my interrupt handle works exactly how I would like it to, and because I only need the most recent number of counts between rising edges on my input signals, I didn't need to use a queue or a mailbox. In case anybody ever needs to measure angular velocity from an optical encoder, they can have a look at how I implemented it [0].

Luke

[0] -- https://github.com/hazelnusse/robot.bicycle/blob/master/Interrupts.cpp
Post Reply