1
votes

I've tried to write a short program using the stm32 timer interrupt on the stm32f100rbt6 on a STM32VLDiscovery board.

This is the code I have so far (main.c):

static inline void initGPIO() {
    RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
    GPIO_InitTypeDef init;
    init.GPIO_Mode = GPIO_Mode_Out_PP;
    init.GPIO_Speed = GPIO_Speed_2MHz;
    init.GPIO_Pin = 0xFFFF;
    GPIO_Init(GPIOC, &init);
}

static inline void initTimer3() {
    NVIC_EnableIRQ(TIM3_IRQn);
    RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
    TIM3->SR = 0;
    TIM3->ARR = 0xFFFF;
    TIM3->PSC = 0xFFFF;
    TIM3->DIER = TIM_DIER_UIE;
    TIM3->CR1 |= TIM_CR1_CEN;
    //same problem when I enable the IRQ here
}

int main(void) {
    SCB->CCR |= SCB_CCR_STKALIGN;
    SystemInit();

    initGPIO();

    GPIOC->ODR = 0xFFFF;

    initTimer3();

    for (;;) {
        GPIOC->BSRR = ~(TIM3->CNT >> 8) & 0x7F;
        GPIOC->BRR = (TIM3->CNT >> 8) & 0x7F;
    }
}

void TIM3_IRQHandler() {
    TIM3->SR = 0;
    //GPIOC->ODR = 0xCC;
}

void HardFault_Handler() {
    GPIOC->BRR = 0x100;
    while (1)
        ;
}

It should call the IRQHandler everytime the timer overflows. When I don't enable the NVIC, then the program runs normally, just writing a part of the timer value to GPIOC. But as soon as I enable the NVIC, the program HardFaults, as soon as the initTimer3 function is called.

What I've tried so far:

  • Moving the NVIC_EnableIRQ line somewhere else in the function

    • Still get the same problem
  • Adding a delay before calling the initTimer3 function

    • Everything before the initTimer3 function gets executed correctly, as soon as the init function is called, the program hardfaults.
  • Using a Different Timer

    • I already tried using TIM7 and TIM6

Update

Setting the STKALIGN bit does not change anything either. The HardFault happens as soon as the Interrupt is activated.

2
Should you not be enabling the interrupt AFTER configuring the timer registers? Does the timer need to be powered up? - Martin James
I already tried enabling the interrupt after the timer configuration, also tried enabling it just before writing CEN bit to high. - Alexander Daum

2 Answers

1
votes

Another common problem:

The interrupt routine name must much this one in the startup files. For example in the code generated by the Cube MX the vector table is defined in the startup_stm32f103xb.s file. The timer interrupts routines are:

  .word TIM1_BRK_IRQHandler
  .word TIM1_UP_IRQHandler
  .word TIM1_TRG_COM_IRQHandler
  .word TIM1_CC_IRQHandler
  .word TIM2_IRQHandler
  .word TIM3_IRQHandler
  .word TIM4_IRQHandler

Check in your files what names are used in the vector table.

-1
votes

You have the HF probably because the stack when invoking the interrupt routine is not aligned . F1xx family has hardware stack alignment off by default and startup or user app has to enable it. Alternatively you can force a proper alignment by using the proper attribute.

enter image description here