Custom TIM3 IRQ Handler

I’m using CLI and photon_043. I need to write a custom IRQ handler for TIM3 (and other timers in the future). I see TIM3_irq() in stm32f2xx/core_hal_stm32f2xx.c, and how tone_hal.c uses HAL_TIM3_Handler. I’m trying to do the same thing from my code and I get:

undefined reference to `HAL_TIM3_Handler'

Simplified code:

#include "application.h"

extern "C" {
    extern void (*HAL_TIM3_Handler)(void);

    void timer3customhandler(void) {
    }

    void setupTimer3Handler(void) {
        HAL_TIM3_Handler = timer3customhandler;
    }
}

void setup() {
    setupTimer3Handler();
}

void loop() {

}

How can I get the ISR to call my handler function?

I’d prefer to replace the HAL’s IRQ handlers with my own custom function for efficiency, would it be possible to declare the IRQ handler functions as weak so a sketch or library can override them if needed?

would it be possible to declare the IRQ handler functions as weak

Nevermind, I realized the way you are separating the modules from the application that won’t work.

Are the interrupt vectors in RAM so they can be modified, or in flash? (Only interested in the Photon)

@Pixelmatix, I had the same challenge with the SparkIntervalTimer library. In the upcoming firmware release (IDE, CLI, DEV) there is an API call to hook into system ISR jump table. If you compile locally, I believe the “latest” branch has the updates. The new function is attachSystemInterrupt(). :smiley:

2 Likes

Thanks for pointing that out! I see it in photon_043 too. I’ll check out your library for a working example.

Adding this code to my sketch (which had already set up TIM3 and enabled interrupts) worked for me:

void TIM3_callback() { }
attachSystemInterrupt(SysInterrupt_TIM3_IRQ, TIM3_callback);
2 Likes

Hi @Pixelmatix

can you post your code, I’m trying use the attachsysteminterrupt function also. Can you post the code you used? Do we have to use the NVIC or that configured under attachsysteminterrupt, I couldn’t find anything about that. Thanks!

Sorry for the delay, I was traveling without my computer.

I used code (modified code from stm32f2xx_tim.c) to initialize the timer and setup the NVIC.

    NVIC_InitTypeDef NVIC_InitStructure;

    attachSystemInterrupt(SysInterrupt_TIM3_IRQ, TIM3_callback);

    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_Init(&NVIC_InitStructure);

Thanks, I was able to figure it out but really appreciate you got back.

Thanks guys for this. It’s helped me to get a handler working for TIM7. I was missing the NVIC configuration.

Thanks everyone for helping out on this thread!