Page 1 of 1
TIM interrupt handler issue
Posted: Sun Dec 30, 2012 9:17 pm
by nosense
Hello I am trying to do some specific edge capturing on CH1 of TIM2 on STM32F103C8. The problem is that when I enable interrupt (nvicEnableVector(TIM2_IRQn, 12);) execution of program ends in "port_halt" with message SV#8 - misplaced CH_IRQ_PROLOGUE(). All subsystems (ICU, GPT,...) are disabled in halconf.h. I am using ChibiOS 2.4.3.
Code: Select all
#include "ch.h"
#include "hal.h"
#include "chdebug.h"
#include "chprintf.h"
#define RC_PWM_FREQ 50
volatile uint16_t val;
CH_IRQ_HANDLER(TIM2_IRQHandler) {
CH_IRQ_PROLOGUE();
chSysLockFromIsr();
val = TIM2->CCR1; //read value to clear IRQ flag
chSysUnlockFromIsr();
CH_IRQ_EPILOGUE();
}
void initRCin(void){
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
TIM2->PSC = STM32_PCLK1*2 / ((1<<16) * RC_PWM_FREQ); //50Hz frequency
TIM2->CCMR1 |= TIM_CCMR1_CC1S_0; //channel 1 = input on T1
TIM2->DIER |= TIM_DIER_CC1IE; //enable capture/compare interrupt
TIM2->CCER |= TIM_CCER_CC1E; //Capture/Compare output enable
nvicEnableVector(TIM2_IRQn, 12);
TIM2->CR1 |= TIM_CR1_CEN; //counter enable
}
int main(void) {
halInit();
chSysInit();
initRCin();
while (TRUE) {
chThdSleepMilliseconds(100);
}
}
Re: TIM interrupt handler issue
Posted: Sun Dec 30, 2012 10:30 pm
by Giovanni
Hi,
I don't see obvious errors in that code. It is what most drivers do. Check the stack frames after stopping in port_halt.
Giovanni
Re: TIM interrupt handler issue
Posted: Mon Dec 31, 2012 4:33 pm
by nosense
Ok little update: I discovered that program doesn't halt immediately, but after several interrupt requests. When connect the input pin to ground (= no edges to capture and no interrupts) program works. When I connect signal back to input pin the program halts within few seconds.
I found that port_halt is caused by (dbg_lock_cnt==1) in dbg_check_enter_isr function called from CH_IRQ_PROLOGUE macro.
Free stack indicator in chibios debug plugin shows 900 bytes for main and 20 bytes for idle thread when in endless port_halt loop.
I tried also original chconf.h, halconf.h and mcuconf.h from STM32F103 demo and the result was the same port_halt.
Everything works fine when I strip interrupt handler to
Code: Select all
CH_IRQ_HANDLER(TIM2_IRQHandler) {
val = TIM2->CCR1; //read value to clear IRQ flag
}
Though I don't think this is a good idea as the macros may actually do something useful and not only cause problems.
Re: TIM interrupt handler issue
Posted: Mon Dec 31, 2012 5:05 pm
by Giovanni
Actually you can remove the macros if you don't need to wakeup a thead from the ISR but you are just masking the real problem. That error is not supposed to happen.
Giovanni
Re: TIM interrupt handler issue
Posted: Mon Dec 31, 2012 5:26 pm
by nosense
Another interesting fact: When I remove line that clears interrupt flag (so the interrupt is fired again immediately) I get stack overflow when macros are in handler. When I remove the macros the program is cycling in interrupt handler but without stack overflow.
I have erased edge capturing and stripped code a little bit to:
Code: Select all
#include "ch.h"
#include "hal.h"
volatile uint16_t val;
CH_IRQ_HANDLER(TIM2_IRQHandler) {
CH_IRQ_PROLOGUE();
chSysLockFromIsr();
val++;
TIM2->SR &= ~TIM_SR_UIF; //clear overflow flag
chSysUnlockFromIsr();
CH_IRQ_EPILOGUE();
}
void initRCin(void){
RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
TIM2->DIER |= TIM_DIER_UIE; //enable overflow interrupt
nvicEnableVector(TIM2_IRQn, 12);
TIM2->CR1 |= TIM_CR1_CEN; //counter enable
}
int main(void) {
halInit();
chSysInit();
initRCin();
while (TRUE) {
chThdSleepMilliseconds(100);
}
}
Now the interrupt is generated on TIM2 overflow. Can someone please test this code and confirm problematic behavior in different environment?
Re: TIM interrupt handler issue
Posted: Mon Dec 31, 2012 7:09 pm
by Giovanni
Hi,
Your problem is here:
nvicEnableVector(TIM2_IRQn, 12);
That function takes the priority mask as parameter, not the priority level. It should be:
nvicEnableVector(TIM2_IRQn, CORTEX_PRIORITY_MASK(12));
In practice your interrupt had priority zero which can preempt the kernel.
Giovanni
Re: TIM interrupt handler issue
Posted: Tue Jan 01, 2013 8:51 pm
by nosense
Damn stupid mistake. I would never seek for error here. Thank you very much. The program seem to work now.