Skip to main content
RShre.2
Associate III
June 12, 2023
Solved

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

  • June 12, 2023
  • 7 replies
  • 4832 views

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 */

}

This topic has been closed for replies.
Best answer by gbm

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.

7 replies

waclawek.jan
Super User
June 12, 2023

Read out and check/post TIM2 registers content.

Generic interrupt not fire checklist here.

JW

Piranha
Principal III
June 12, 2023
JMala.3
Associate III
June 12, 2023

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 */
}

TDK
June 12, 2023

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""."
gbm
gbmBest answer
Super User
June 13, 2023

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
RShre.2Author
Associate III
June 13, 2023

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 :)