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.
