Skip to main content
SSaiy.1
Associate III
Feb 13, 2023 1:04 PM UTC
Solved

STM32 L412 Timer interrupt not working

I am using STM32L412 microcontroller and doing baremetal programming for timer interrupt. I am using Timer 6 and base clock of 4MHZ is used as HCLK. However my interrupt is not firing below mentioning my code. count variable is also not updating

int main(void)

{

LedConfig();

StartTimer6();

  /* Loop forever */

while(1)

{

count=1;

}

}

void LedConfig(void)

{

/*Enable Clock for Port B. */

RCC->AHB2ENR |= (1U<<1);

/*Configure GPIOB_13 as general purpose output*/

GPIOB->MODER |= (1U<<26);

GPIOB->MODER &= ~(1U<<27);

}

void StartTimer6(void)

{

RCC->APB1ENR1 |= (1U<<4);

TIM6->PSC = 1000;

TIM6->ARR = 4000;

TIM6->EGR |= (1U<<0);

TIM6->DIER |= (1U<<0);

TIM6->CR1 |= (1U<<0);

NVIC_EnableIRQ(TIM6_IRQn);

}

void TIM6_IRQHandler(void)

{

if(TIM6->SR & (1U<<0))

{

GPIOB->ODR ^= (1U<<13);

TIM6->SR &= ~(1U<<0);

}

}

    This topic has been closed for replies.
    SSaiy.1
    SSaiy.1Best answer
    Associate III
    Feb 14, 2023 10:41 AM UTC

    Thanks @KnarfB as you said IRQ handler was different the actual one is TIM6_DACUNDER_IRQHandler. After I changed the handler name the code started working

    14 replies

    KnarfB
    Super User
    Feb 13, 2023 1:06 PM UTC

    no code, no description, no answer...

    SSaiy.1
    SSaiy.1Author
    Associate III
    Feb 13, 2023 1:08 PM UTC

    @KnarfB​ Sorry, I was entering the details.

    KnarfB
    Super User
    Feb 13, 2023 1:19 PM UTC

    And CNT register updating?

    Ref.man,. talks about delay after enabling peripheral clocks, maybe

    RCC->APB1ENR1 |= RCC_APB1ENR1_TIM6EN; // set clock enable flag
     volatile uint32_t dummy = RCC->APB1ENR1; // read back 
     (void)dummy; // avoid "unused" warning

    hth

    KnarfB

    SSaiy.1
    SSaiy.1Author
    Associate III
    Feb 13, 2023 1:23 PM UTC

    @KnarfB​ yes CNT is updating.

    KnarfB
    Super User
    Feb 13, 2023 1:32 PM UTC

    Any other init code before? Works "as is" on my Nucleo-STM32L432KC (LED on PB3) when started from first line in main()

    hth

    KnarfB

    SSaiy.1
    SSaiy.1Author
    Associate III
    Feb 13, 2023 1:36 PM UTC

    Nothing except the code I updated here.

    SSaiy.1
    SSaiy.1Author
    Associate III
    Feb 14, 2023 6:26 AM UTC

    When I tried with Timer 2 this code is working as expected, LED blinks at 1 second interval. Is the Timer 6 of STM32L412 has any additional configuration to made in order to work like this.

    This is what I get when I debug.

    0693W00000YAfEFQA1.png

    KnarfB
    Super User
    Feb 14, 2023 7:24 AM UTC

    This happens when the specific IRQ handler is not implemented. See the interrupt vector table in the startup code for the proper names. Guess: TIM6_DAC_IRQHandler?

    hth

    KnarfB

    waclawek.jan
    Super User
    Feb 14, 2023 10:39 AM UTC

    > See the interrupt vector table in the startup code for the proper names.

    +1

    I wonder where startup_stm32l412rbtx.s is from - not from https://github.com/STMicroelectronics/STM32CubeL4/tree/master/Drivers/CMSIS/Device/ST/STM32L4xx/Source/Templates

    General "interrupt does not fire" troubleshooting steps here.

    JW

    SSaiy.1
    SSaiy.1Author
    Associate III
    Feb 14, 2023 11:21 AM UTC

    when I tried the debug mode startup_stm32l412rbtx.s was autoloading because of the error, it was in the debug folder under projectname.elf -[arm/le]. I got the Handler name from this file.

    0693W00000YAgnaQAD.png

    KnarfB
    Super User
    Feb 14, 2023 11:58 AM UTC

    What toolchain / version are you using?

    KnarfB

    SSaiy.1
    SSaiy.1AuthorBest answer
    Associate III
    Feb 14, 2023 10:41 AM UTC

    Thanks @KnarfB as you said IRQ handler was different the actual one is TIM6_DACUNDER_IRQHandler. After I changed the handler name the code started working

    KnarfB
    Super User
    Feb 14, 2023 10:48 AM UTC

    Glad it works. To add some confusion: stm32l412xx.h (ARM CMSIS) and others contains a line

    /* Aliases for __IRQHandler */
    #define TIM6_IRQHandler TIM6_DAC_IRQHandler

    which covers that error up. But you don't seem to use that.

    KnarfB