Custom Interrupt on P1/Photon [SOLVED]

Hi all

I’m trying to generate a timer based interrupt from Timer 3, with not much luck.
I release there is a library written for this, however I’m trying to do it without the abstraction as I need to run both the interrupt and PWM off the same timer (timer 3).

I have got PWM to work successfully off the timer so I thought it wouldn’t be a lot more work to get the interrupt up and running…

The following code I’ve stripped the PWM part from to focus just on the interrupt.

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; 
NVIC_InitTypeDef        NVIC_InitStructure; 

uint16_t                period;
uint16_t                pulse;

/* Compute the period value to generate a clock freq of 1KHz */
period = (uint16_t)(SystemCoreClock / 2000)-1; //SystemCoreClock = 120MHz

/* Compute the CCR1 value to generate a PWM signal with 50% duty cycle */
pulse = (uint16_t) ((period-1)/(100.0/(100-dutyCycle)));

/* ISR configuration */
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);

/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

/* Timer Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = period;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

/* TIM3 Counter Enable */
TIM_Cmd(TIM3, ENABLE);

I’m using the following as my interrupt handler

void TIM3_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM3,TIM_IT_Update) != RESET)
  {
    TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
    //Interrupt code here
  }
}

Anybody have any ideas?

I can confirm the timer is running, I’ve created an if loop to toggle a pin depending on whether the timer value is greater than or less than 30000 - It works.
Just can’t seem to get the ISR to work. Is there any chance something is being overridden by the Particle wiring libraries?

After going through @peekay123’s IntervalTimerLibrary I see that I needed to use:

attachSystemInterrupt(SysInterrupt_TIM3_Update, Wiring_TIM3_Interrupt_Handler_override) ; 

And also change my handler to:

void Wiring_TIM3_Interrupt_Handler_override()
{
   if (TIM_GetITStatus(TIM3,TIM_IT_Update) != RESET)
   {
      TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
      //Interrupt code here
   } 
}

In case anyone else has the same problem, I don’t really understand what goes on behind the scenes but I suspect one of the Particle wiring modules had overridden my TIM3_IRQHandler.

Could be handy if this is added to the docs as currently it only shows how to set up external interrupts, unless I missed something :slight_smile:

@G65434_2, glad you got it working! There is absolutely a need for documentation on hooking into peripheral interrupts in the STM32F205. This is on my radar. :smile:

1 Like

Sounds good @peekay123, thanks for all your work!