cancel
Showing results for 
Search instead for 
Did you mean: 

How to call timer interrupt using registers? [NO HAL DRIVERS]

RShre.2
Associate III

Hi, i am trying to call timer interrupt handler using registers and the following is my code, but the problem is that the function is being not called, I see no output in the oscilloscope or live expression. First my DAC value is still 0, the counter is running though. I also tried to use toggle breakpoint to see where the problem lies and I think it's at this point // Clear the interrupt flag

    TIM2->SR &= ~TIM_SR_UIF;

but i am not sure. Could anyone confirm if what i am doing is correct/wrong?

Btw, i am using stm32-g071rb.

int main(){

/* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_USART2_UART_Init();

 MX_DAC1_Init();

 MX_TIM2_Init();

 /* USER CODE BEGIN 2 */

// TIM2->DIER |= TIM_DIER_UIE;     // Enable the update (overflow) interrupt

 NVIC_SetPriority(TIM2_IRQn, 0);  // Set the interrupt priority (if needed)}

 NVIC_EnableIRQ(TIM2_IRQn);     // Enable the timer interrupt in the NVIC

 TIM2->CR1 |= TIM_CR1_CEN; // Start the timer

}

void TIM2_IRQHandler(void)

{

 /* USER CODE BEGIN TIM2_IRQn 0 */

if (TIM2->SR & TIM_SR_UIF)

  {

    // Timer interrupt flag is set, do something here

    DAC1->DHR12L2 = 1;

    HAL_Delay(100);

//

    // Clear the interrupt flag

    TIM2->SR &= ~TIM_SR_UIF;

  }

 /* USER CODE END TIM2_IRQn 0 */

 HAL_TIM_IRQHandler(&htim2);

 /* USER CODE BEGIN TIM2_IRQn 1 */

 /* USER CODE END TIM2_IRQn 1 */

}

1 ACCEPTED SOLUTION

Accepted Solutions
gbm
Lead III

The first problem is the use of HAL_Delay in the ISR. This call wiiil never return, as it is waiting for a lower priority interrupt to occur while called from a higher priority interrupt. This will not work.

Other problems: you cllear the interrupt flag incorrectly, using &= instead of a simple assignment =. You don't need to call HAL_TIM_IRQHandler() from your routine, it doesn't make sense.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

View solution in original post

7 REPLIES 7

Read out and check/post TIM2 registers content.

Generic interrupt not fire checklist here.

JW

Piranha
Chief II
JMala.3
Associate III

Your code looks good, Here is my code (for the STM32L010) that works perfectly. Th only comment I have on your code is why did you comment out the line // TIM2->DIER |= TIM_DIER_UIE; This is needed to set the Timer to interrupt. This may have been set already if you checked the interrupt checkbox in the HAL Timeer setup dialog

void SysTick_Init(void)
{
#ifdef BARE_METAL
    SET_AND_WAIT(RCC->APB2ENR, RCC_APB2ENR_TIM21EN);   /* Enable TIM21 Clock */
 
    TIM21->PSC = 3;              /* divide by 4 prescaler */
    TIM21->ARR = 5242;           /* set the countdown value */
#endif
 
    TIM21->DIER |= TIM_DIER_UIE; /* Enable the interrupt */
    TIM21->CR1  |= TIM_CR1_CEN;  /* Enable the timer */
    NVIC_EnableIRQ( TIM21_IRQn );
}
void TIM21_IRQHandler(void)
{
    if (TIM21->SR & TIM_SR_UIF)
    {
       SysTick_Milliseconds += TICK_PERIOD_IN_MS;
    }
    TIM21->SR = 0;  /* Clear all interrupts */
}

You have a race condition here. You should only clear the flag if UIF is set. Otherwise you may clear it without it being handled.

If you feel a post has answered your question, please click "Accept as Solution".

Yes, the tim2 is checked mark in the CubeMx as a global interrupt already that's why I commented that part in the code.

 But I see when the timer interrupt is enabled, my CNT is always at 0. Why is that so?

PS: Nevermind, it worked now.

gbm
Lead III

The first problem is the use of HAL_Delay in the ISR. This call wiiil never return, as it is waiting for a lower priority interrupt to occur while called from a higher priority interrupt. This will not work.

Other problems: you cllear the interrupt flag incorrectly, using &= instead of a simple assignment =. You don't need to call HAL_TIM_IRQHandler() from your routine, it doesn't make sense.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
RShre.2
Associate III

I just turned off the interrupt in cubemx and called it manually in the code so now the  HAL_TIM_IRQHandler() is not there anymore. Now, my value is changing. Thanks :)