简体   繁体   中英

STM32F103 Timer2 not interrupting

Background

I'm relatively inexperienced with the STM32 series, so I'm sure that this is simple and I'm missing a setup somewhere.

I am trying to set up the timer to simply interrupt on an update event, which should be when the counter rolls over at the TIM2->ARR value.

  • I am currently setting a breakpoint inside the timer interrupt and it is simply not triggering
  • I have tried to use other timer modules
  • The counter is counting (I can observe through the debugger)
  • The registers are loaded appropriately based on the below code
  • The TIM2->SR UIF (update interrupt flag) is set when the counter rolls over

Code

void TIM_init(void){
    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

    TIM2->PSC = 1000;
    TIM2->ARR = 1000;
    TIM2->DIER = TIM_DIER_UIE;
    TIM2->EGR = TIM_EGR_UG;
    NVIC_EnableIRQ(TIM2_IRQn);

    DBGMCU->CR |= DBGMCU_CR_DBG_TIM2_STOP;
    TIM2->CR1 |= TIM_CR1_CEN;
}

void TIM2_IRQHandler(void){
    TIM2->SR &= ~TIM_SR_UIF;    // clear the interrupt flag
}

I also tried setting the priority grouping as follows, same results:

void TIM_init(void){
    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

    TIM2->PSC = 10;
    TIM2->ARR = 1000;
    TIM2->DIER = TIM_DIER_UIE;
    TIM2->EGR = TIM_EGR_UG;

    // Enable the Timer2 Interrupts
    uint32_t priorityGroup, priority;
    priorityGroup = NVIC_GetPriorityGrouping();
    priority = NVIC_EncodePriority(priorityGroup, 3, 6);
    NVIC_SetPriority(TIM2_IRQn, priority);

    NVIC_EnableIRQ(TIM2_IRQn);

    DBGMCU->CR |= DBGMCU_CR_DBG_TIM2_STOP;
    TIM2->CR1 = TIM_CR1_CEN;
}

The project also contains an assembly file startup_stm32f10x.s . An excerpt from that file:

__vector_table
        DCD     sfe(CSTACK)
        DCD     Reset_Handler             ; Reset Handler
        DCD     NMI_Handler               ; NMI Handler
        DCD     HardFault_Handler         ; Hard Fault Handler.....

which continues on, including the TIM2_IRQHandler . This indicates to me that there is a vector table there.

It might not be an exact solution but more of a suggestion untill someone gives a better solution for you.

Case 1 : If your task needs only few driver initialization like interrupt and you have enough time, then you can go for obvious reading of the whole initialization chapter, for your case interrupt chapter. Keep an eye for the following points.

  • Small foot notes in the chapter.
  • See the chapter related to clock for exception
  • See the chapter related to Power for exception

In some cases the datasheet writes initialization related information in the clock and power chapter which occasionally caused a desired driver not to function.

Case 2 : If you need several initialization and lots of development to be done in limited time then here is an alternative approach.

During my work with different kind of µc what I found is that it is better to leave the lower level initialization for the manufacturers, at least at beginning stages. Now a days most of the manufacturers have options where you can create a blank skeleton project online or with some kind of software. For your case it is STM32Cube initialization code generator .

The advantages being you can select your µc and related drivers and they will create a full skeleton project with all initialization and sometimes fill it up with example code for you to start. It reduces developer effort to go through the lower level initialization and directly go for development. You can also see their code to get the grasp of how the driver has been initialized and do relevant modification. Keep a close eye on the comments in their initialization code for understanding or any framework documentation that comes with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM