cancel
Showing results for 
Search instead for 
Did you mean: 

Setup TIM8 for Output Compare interrupts is not working

areify
Associate II

Hi everyone.

I'm trying to setup the timer TIM8 in order to generate interrupts, using compare registers for this purpose. My intention is to use all the available

channels. I'm testing with a first stop of 150 ticks (150us), but it never stops. The function I'm using for initializing and starting TIM8 is next:

int timerOpen(void)
{  
   uint32_t apb2freq;
   uint16_t tmp;
   
   //TIM1 working under APB2
   apb2freq = SystemCoreClock;
   tmp = APBPrescTable[((RCC->CFGR & RCC_CFGR_PPRE2) >> 8)];
   if (tmp > 0) tmp--;
   apb2freq >>= tmp;  
   
   //TIMx defined as TIM8
   timer_handle.Instance = TIMx;
	
   //Prescaler calculation: 1 MHz desired
   uint32_t PrescalerValue = (uint32_t)(apb2freq / 1000000) - 1;
	
   //Period bigger than compare register
   timer_handle.Init.Period            = 100000 - 1;
   timer_handle.Init.Prescaler         = PrescalerValue;
   timer_handle.Init.ClockDivision     = 0;
   timer_handle.Init.CounterMode       = TIM_COUNTERMODE_UP;
#ifndef STM32L1
   timer_handle.Init.RepetitionCounter = 0;
#endif
   
   // Interrupt after 150 * 1 us = 150 us
   timer_handle.Instance->CCR1 = 150;
   //timer_handle.Instance->DIER |= 0x02;		//Enable interrupt
   
   
   //Init TIM8
   HAL_TIM_OC_MspInit(&timer_handle);
	
   //Init low level resources
   TIMx_CLK_ENABLE();
   
   //Interrupt priorities. TIMx_IRQn defined as
   HAL_NVIC_SetPriority(TIMx_IRQn, 0, 0);
        
   //TIM8 interrupt enabling. 
   HAL_NVIC_EnableIRQ(TIMx_IRQn);
	
   //Enabling GPIOA timer
   __HAL_RCC_GPIOA_CLK_ENABLE();
		
   //Init timer and handler
   if(HAL_TIM_OC_Init(&timer_handle) != HAL_OK)
   {
      //Error while timer init
      return -1;
   }	
	
   //Start the timer
   if (HAL_TIM_OC_Start_IT(&timer_handle,TIM_CHANNEL_1) != HAL_OK)
   {
     //Error while timer init
      return -2;
   }
 
   return 0;
}

I call the ISR function as next:

void TIM1_IRQHandler(void)
{
  HAL_TIM_IRQHandler(&timer_us_timhandle_);
}

How can I get an stop until 150us? I'm getting a little lost with HAL libraries...

And another question (bonus track)... Is this possible to do the same with the other timers (TIM2, TIM3, TIM4...)?

Thank you in advance.

5 REPLIES 5
Uwe Bonnes
Principal III

Start your debugger, set a breakpoint after TIM8 has beem set up and decode the TIM8 registers with the reference manual.

> I'm trying to setup the timer TIM8

> void TIM1_IRQHandler(void)

I don't understand your code, and I don't use Cube. You may need to set all the init struct fields.

In fact, it would be better to start with TIM2..TIM5 as they are simpler than TIM1 and TIM8.

JW

Sorry, I was checking first with TIM1 and I forgot. However, it's not working anyway.

I'm not sure about which structs I'd need to init. That's my main problem with this (it's my first time with ARM and HAL).

> In fact, it would be better to start with TIM2..TIM5 as they are simpler than TIM1 and TIM8.

Sorry, did not see this part. Can I do this with those timers? That was my question... As I'm reading, it's only possible to work at Output Compare Mode and CCR registers with TIM1 and TIM8.

As Uwe said above, read out the timer registers content in debugger and check if they match your expectations. If so, observe if the required falg in TIMx_SR gets set and check if the respective enable bit in TIMx_DIER is set. Then check if the respective interrupt is enabled in NVIC, and check in disassembly view, if the interrupt service routine (ISR) address is present in the vector table (usually at the beginning of the FLASH).

JW

Perfect! ISR was not on vector table because I was overriding TIM1_IRQHandler instead of TIM1_CC_IRQHandler.

Now it's stopping but it does every 34.4 ms (due to period, which overflows to this value). I don't know exactly why is stopping at period time instead of doing at CCR time.