timerinterruptstm32f1

Problem in STM32F103C8 with Timer Interrupt


I am newbie at STM32 and I have a question about the interrupt. I have wrote below code for STM32F103C8T6 MCU for timer interrupt, but the code in while(1) dosn't run when I active the interrupt with __enable_irq() command. It seams the __enable_irq() have a infinite loop and program dosn't leave it. Please help me what is my problem. Thank you.

#include "stm32f10x.h"

#define TIME2EN 0

void configure_pc13(void);

void configure_timer(void);

void configure_clock(void);

volatile uint64_t t_counter = 0;

int main(void){ 
    __disable_irq();
    
    configure_clock();
    
    configure_pc13();
    
    configure_timer();
    
    NVIC_EnableIRQ(TIM2_IRQn);
    
    __enable_irq();
    
    while(1){
        if(t_counter >= 40){
            GPIOC->ODR ^= (0x1 << 13);
            t_counter = 0;
        }
    }
}

void TIM2_IRQHandler(void){
    t_counter++;
    TIM2->SR &= ~TIM_SR_UIF;
}

void configure_clock(void){
    RCC->CR &= ~( (0x1UL << 0) | (0x1UL << 16) );
    
    RCC->CR |= (0x1 << 16);
    
    while( !( ( RCC->CR >> 17 ) & 1 ) );
}

void configure_pc13(void){
    RCC->APB2ENR |= (1 << 4);
    
    GPIOC->CRH &= ~(0xFUL << 20);
    
    GPIOC->CRH |= (0x1 << 20);
    
    GPIOC->ODR |= (0x1 << 13);
}

void configure_timer(void){
    RCC->APB1ENR |= (0x1 << TIME2EN);
    
    TIM2->PSC = 1;
    TIM2->ARR = 8;
    
    TIM2->CR1 = 1;
    
    TIM2->DIER |= 1;
}

I have used the __disable_irq() inside the TIM2_IRQHandler function but the t_counter variable dosn't count correctly.


Solution

  • As you've set ARR to 8 and PSC to 1, there's an interrupt every (1+1)*(8+1)=18 machine cycles. The minimum interrupt entry/exit is 12+12 cycles, and that does not include any instructions executed in the interrupt service routine (ISR).

    In other words, your program executes the ISR over and over and nothing else executes.

    You simply can't have that interrupt rate.