cancel
Showing results for 
Search instead for 
Did you mean: 

Timer Commutation Event Callback not firing STM32F429

DNG_Gabbi
Associate III

EDIT* for those seeking a solution, JW in the replies below pointed out that the DIER register for TIM1 never enabled the interrupt bit for the commutation event and the mistake was that I used 

HAL_TIMEx_ConfigCommutEvent(&htim1, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);

Instead of 

HAL_TIMEx_ConfigCommutEvent_IT(&htim1, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);

---------------------------------------------------------------------------------------------------------------------------------------------------------

Dear whomever may read this,

I've been working with PWM on TIM1 with the NUCLEO-F429ZI, I have three channels and their complementary channels active. This is an iterative test project, where I am bring function to life one step at a time. The following is the configuration for TIM1 (Note channel 2, 2N and 3, 3N have the same configuration as 1 and 1N).

TIM1 Channel ModesTIM1 Channel ModesTIM1 ParametersTIM1 Parameters

After being happy with the PWM timer, I enabled the commutation interrupt

TIM1 InterruptsTIM1 Interrupts

In the beginning I also had the update interrupt and that was working and firing. Then in my loop I was simply trying to fire a commutation generation event about once every second (just rough test). The if statement runs but the "void TIM1_TRG_COM_TIM11_IRQHandler(void)" routine never runs for some reason.

I then took the event generation out of the loop and did it once in the beginning made a test to see if the commutation flag is being set (it is), but it's never serviced and then cleared. So the LED kept toggling. It can be manually reset.

Here is the current version of my main, in the middle of trying out some tests. The code is temporary test code after trying to debug. But from all I can see I have configured the timer to accept the software events, I thought "void TIM1_TRG_COM_TIM11_IRQHandler(void)" would handle that. Perhaps I am wrong there.

 

 

 

 

 

 

int main(void)
{
  HAL_Init();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM1_Init();
  MX_ADC1_Init();
  MX_USART3_UART_Init();

  /* Initialize interrupts */
  MX_NVIC_Init();

  HAL_TIM_Base_MspInit(&htim1);
  HAL_TIMEx_ConfigCommutEvent(&htim1, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);

  // TODO: This is needed for the update event
  HAL_TIM_Base_Start_IT(&htim1);

  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
  HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);

  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
  HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);

  HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
  HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);

  HAL_ADC_Start(&hadc1);

  uint16_t adc_val = 0;
  float duty_cycle = 0.5f;
  uint16_t period_pwm = __HAL_TIM_GET_AUTORELOAD(&htim1);
  uint32_t last_event = HAL_GetTick();
  const uint32_t tick_between = 1000;

  HAL_TIM_GenerateEvent(&htim1, TIM_EVENTSOURCE_COM);

  while (1)
  {
	  HAL_ADC_PollForConversion(&hadc1, 1000);
	  adc_val = HAL_ADC_GetValue(&hadc1);

	  duty_cycle = adc_val / 4095.0f;

	  __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1,  	  duty_cycle * period_pwm);
	  __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2,  0.5 * duty_cycle * period_pwm);
	  __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0.25 * duty_cycle * period_pwm);

	  if ((HAL_GetTick() - last_event) > tick_between)
	  {

		  if (__HAL_TIM_GET_FLAG(&htim1, TIM_FLAG_COM))
		  {
			  HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
			  __HAL_TIM_CLEAR_FLAG(&htim1, TIM_FLAG_COM);
		  }
		  last_event = HAL_GetTick();
	  }
  }

  HAL_ADC_Stop(&hadc1);
}

 

 

 

 

 

 

 I can send further information and answer question if anyone has any idea what is happening, I'm no HAL expert and this is a step in my learning journey. I'd appreciate the help.

1 ACCEPTED SOLUTION

Accepted Solutions

In your timer screenshots, TIMx_DIER=0x1, i.e. only UIE is set, not the commutation interrupt enable bit.

I don't use Cube/HAL.

JW

View solution in original post

8 REPLIES 8
DNG_Gabbi
Associate III

I should perhaps add some of the following functions

static void MX_NVIC_Init(void)
{
  /* TIM1_TRG_COM_TIM11_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(TIM1_TRG_COM_TIM11_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(TIM1_TRG_COM_TIM11_IRQn);
}

Enabling the function that I thought would handle the Event Generation for TIM_EVENTSOURCE_COM

 

void HAL_TIMEx_CommutCallback(TIM_HandleTypeDef *htim)
{
	char msg[30] = "Made it to the callback\r\n";
	HAL_UART_Transmit(&huart3, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
}

Function meant originally to handle the commutation callback

 

void TIM1_TRG_COM_TIM11_IRQHandler(void)
{
  /* USER CODE BEGIN TIM1_TRG_COM_TIM11_IRQn 0 */
	char msg[30] = "Made it COM IRQ Handler\r\n";
	HAL_UART_Transmit(&huart3, (uint8_t*)msg, strlen(msg), HAL_MAX_DELAY);
  /* USER CODE END TIM1_TRG_COM_TIM11_IRQn 0 */
  HAL_TIM_IRQHandler(&htim1);
  /* USER CODE BEGIN TIM1_TRG_COM_TIM11_IRQn 1 */

  /* USER CODE END TIM1_TRG_COM_TIM11_IRQn 1 */
}

 

I have verified UART working, I've also had LED toggling in there instead to no luck.

Cube/HAL is open source, so you can debug it as your own code.

If you've verified by reading the TIM registers that the commutation event happened (the respectrive flag in TIMx_SR is set) and the commutation interrupt is enabled (respective flag in TIMx_DIER), then the processor should fire the respective interrupt (as you've said, it should be TIM1_TRG_COM_TIM11_IRQHandler, but that depends on how exactly the vector table, usually in startup code, is constructed). So place a breakpoint there, and if the interrupt does fire and stops on that breakpoint, walk through Cube's code to see, why does it not vector to the your callback. If the interrupt does not fire, follow steps from here.

JW

Dear JW, 

Thanks for the reply, I had tried some debugging yesterday. But I'm relatively new to HAL and interrupts especially, I've been trying to get a better hang of it and thought I understood the process more clearly. 

I will detail here my debugging process, I would appreciate if someone more knowledgeable could point me to mistakes and next steps if any are available.

First thing I did was to also enable the update event for TIM1 to see if I could get any interrupt going and found for that that I needed the following line in the start. (Before I had only started the PWM channels).

HAL_TIM_Base_Start_IT(&htim1);

This managed to fire the following interrupt.

void TIM1_UP_TIM10_IRQHandler(void)

 

I've also checked the TIM1->SR and the COMIF bits, before and after event generation.

Before Event GenerationBefore Event Generation

After Event GenerationAfter Event Generation

After The Clear FlagAfter The Clear Flag

Satisfied that the COM interrupt flag is set and I can manually clear it I changed the main loop back to 

 HAL_TIM_GenerateEvent(&htim1, TIM_EVENTSOURCE_COM);

  while (1)
  {
	  HAL_ADC_PollForConversion(&hadc1, 1000);
	  adc_val = HAL_ADC_GetValue(&hadc1);

	  duty_cycle = adc_val / 4095.0f;

	  // Much better way to reconfigure the pulse duration
	  __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1,  	  duty_cycle * period_pwm);
	  __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2,  0.5 * duty_cycle * period_pwm);
	  __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_3, 0.25 * duty_cycle * period_pwm);

	  if ((HAL_GetTick() - last_event) > tick_between)
	  {
		  HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
		  last_event = HAL_GetTick();
	  }

    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }

 So I generate the event and then go into the loop, the u

void TIM1_TRG_COM_TIM11_IRQHandler(void)

pdate event fires but never the 

void TIM1_TRG_COM_TIM11_IRQHandler(void)

Perhaps then I'm configuring that interrupt wrong. In main before the look, AFTER MX_TIM1_Init(); I have

  /* Initialize interrupts */
  MX_NVIC_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIMEx_ConfigCommutEvent(&htim1, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);

Where MX_NVIC_Init() is as follows

static void MX_NVIC_Init(void)
{
  /* TIM1_TRG_COM_TIM11_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(TIM1_TRG_COM_TIM11_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(TIM1_TRG_COM_TIM11_IRQn);
}

 

As I have said I'm not expert on HAL and I can have troubles, from what I know I think I'm doing things right. But I'd be very happy to learn what I'm doing wrong :)

p.s. I put a break point on the interrupt hander for TIM1_TRG_COM_TIM11_IRQHandler, it never runs.

I'd like to add the contents of the NVIC registers, I've been trying to read through RM0090 to find the ISRx registers but have not been able to locate that. Thus I have a hard time knowing whether the interrupt enable lines are actually configuring things correctly. I attach here a picture of the registers, If anyone knows what page in RM0090 it is or in some other document I'd appreciate that.

NVIC ISERxNVIC ISERx

I also attach here the vector table in the start up, line 50 below has the interrupt in question.

  .section  .isr_vector,"a",%progbits
  .type  g_pfnVectors, %object
   
g_pfnVectors:
  .word  _estack
  .word  Reset_Handler

  .word  NMI_Handler
  .word  HardFault_Handler
  .word  MemManage_Handler
  .word  BusFault_Handler
  .word  UsageFault_Handler
  .word  0
  .word  0
  .word  0
  .word  0
  .word  SVC_Handler
  .word  DebugMon_Handler
  .word  0
  .word  PendSV_Handler
  .word  SysTick_Handler
  
  /* External Interrupts */
  .word     WWDG_IRQHandler                   /* Window WatchDog              */                                        
  .word     PVD_IRQHandler                    /* PVD through EXTI Line detection */                        
  .word     TAMP_STAMP_IRQHandler             /* Tamper and TimeStamps through the EXTI line */            
  .word     RTC_WKUP_IRQHandler               /* RTC Wakeup through the EXTI line */                      
  .word     FLASH_IRQHandler                  /* FLASH                        */                                          
  .word     RCC_IRQHandler                    /* RCC                          */                                            
  .word     EXTI0_IRQHandler                  /* EXTI Line0                   */                        
  .word     EXTI1_IRQHandler                  /* EXTI Line1                   */                          
  .word     EXTI2_IRQHandler                  /* EXTI Line2                   */                          
  .word     EXTI3_IRQHandler                  /* EXTI Line3                   */                          
  .word     EXTI4_IRQHandler                  /* EXTI Line4                   */                          
  .word     DMA1_Stream0_IRQHandler           /* DMA1 Stream 0                */                  
  .word     DMA1_Stream1_IRQHandler           /* DMA1 Stream 1                */                   
  .word     DMA1_Stream2_IRQHandler           /* DMA1 Stream 2                */                   
  .word     DMA1_Stream3_IRQHandler           /* DMA1 Stream 3                */                   
  .word     DMA1_Stream4_IRQHandler           /* DMA1 Stream 4                */                   
  .word     DMA1_Stream5_IRQHandler           /* DMA1 Stream 5                */                   
  .word     DMA1_Stream6_IRQHandler           /* DMA1 Stream 6                */                   
  .word     ADC_IRQHandler                    /* ADC1, ADC2 and ADC3s         */                   
  .word     CAN1_TX_IRQHandler                /* CAN1 TX                      */                         
  .word     CAN1_RX0_IRQHandler               /* CAN1 RX0                     */                          
  .word     CAN1_RX1_IRQHandler               /* CAN1 RX1                     */                          
  .word     CAN1_SCE_IRQHandler               /* CAN1 SCE                     */                          
  .word     EXTI9_5_IRQHandler                /* External Line[9:5]s          */                          
  .word     TIM1_BRK_TIM9_IRQHandler          /* TIM1 Break and TIM9          */         
  .word     TIM1_UP_TIM10_IRQHandler          /* TIM1 Update and TIM10        */         
  .word     TIM1_TRG_COM_TIM11_IRQHandler     /* TIM1 Trigger and Commutation and TIM11 */
  .word     TIM1_CC_IRQHandler                /* TIM1 Capture Compare         */                          
  .word     TIM2_IRQHandler                   /* TIM2                         */                   
  .word     TIM3_IRQHandler                   /* TIM3                         */                   
  .word     TIM4_IRQHandler                   /* TIM4                         */                   
  .word     I2C1_EV_IRQHandler                /* I2C1 Event                   */                          
  .word     I2C1_ER_IRQHandler                /* I2C1 Error                   */                          
  .word     I2C2_EV_IRQHandler                /* I2C2 Event                   */                          
  .word     I2C2_ER_IRQHandler                /* I2C2 Error                   */                            
  .word     SPI1_IRQHandler                   /* SPI1                         */                   
  .word     SPI2_IRQHandler                   /* SPI2                         */                   
  .word     USART1_IRQHandler                 /* USART1                       */                   
  .word     USART2_IRQHandler                 /* USART2                       */                   
  .word     USART3_IRQHandler                 /* USART3                       */                   
  .word     EXTI15_10_IRQHandler              /* External Line[15:10]s        */                          
  .word     RTC_Alarm_IRQHandler              /* RTC Alarm (A and B) through EXTI Line */                 
  .word     OTG_FS_WKUP_IRQHandler            /* USB OTG FS Wakeup through EXTI line */                       
  .word     TIM8_BRK_TIM12_IRQHandler         /* TIM8 Break and TIM12         */         
  .word     TIM8_UP_TIM13_IRQHandler          /* TIM8 Update and TIM13        */         
  .word     TIM8_TRG_COM_TIM14_IRQHandler     /* TIM8 Trigger and Commutation and TIM14 */
  .word     TIM8_CC_IRQHandler                /* TIM8 Capture Compare         */                          
  .word     DMA1_Stream7_IRQHandler           /* DMA1 Stream7                 */                          
  .word     FMC_IRQHandler                    /* FMC                         */                   
  .word     SDIO_IRQHandler                   /* SDIO                         */                   
  .word     TIM5_IRQHandler                   /* TIM5                         */                   
  .word     SPI3_IRQHandler                   /* SPI3                         */                   
  .word     UART4_IRQHandler                  /* UART4                        */                   
  .word     UART5_IRQHandler                  /* UART5                        */                   
  .word     TIM6_DAC_IRQHandler               /* TIM6 and DAC1&2 underrun errors */                   
  .word     TIM7_IRQHandler                   /* TIM7                         */
  .word     DMA2_Stream0_IRQHandler           /* DMA2 Stream 0                */                   
  .word     DMA2_Stream1_IRQHandler           /* DMA2 Stream 1                */                   
  .word     DMA2_Stream2_IRQHandler           /* DMA2 Stream 2                */                   
  .word     DMA2_Stream3_IRQHandler           /* DMA2 Stream 3                */                   
  .word     DMA2_Stream4_IRQHandler           /* DMA2 Stream 4                */                   
  .word     ETH_IRQHandler                    /* Ethernet                     */                   
  .word     ETH_WKUP_IRQHandler               /* Ethernet Wakeup through EXTI line */                     
  .word     CAN2_TX_IRQHandler                /* CAN2 TX                      */                          
  .word     CAN2_RX0_IRQHandler               /* CAN2 RX0                     */                          
  .word     CAN2_RX1_IRQHandler               /* CAN2 RX1                     */                          
  .word     CAN2_SCE_IRQHandler               /* CAN2 SCE                     */                          
  .word     OTG_FS_IRQHandler                 /* USB OTG FS                   */                   
  .word     DMA2_Stream5_IRQHandler           /* DMA2 Stream 5                */                   
  .word     DMA2_Stream6_IRQHandler           /* DMA2 Stream 6                */                   
  .word     DMA2_Stream7_IRQHandler           /* DMA2 Stream 7                */                   
  .word     USART6_IRQHandler                 /* USART6                       */                    
  .word     I2C3_EV_IRQHandler                /* I2C3 event                   */                          
  .word     I2C3_ER_IRQHandler                /* I2C3 error                   */                          
  .word     OTG_HS_EP1_OUT_IRQHandler         /* USB OTG HS End Point 1 Out   */                   
  .word     OTG_HS_EP1_IN_IRQHandler          /* USB OTG HS End Point 1 In    */                   
  .word     OTG_HS_WKUP_IRQHandler            /* USB OTG HS Wakeup through EXTI */                         
  .word     OTG_HS_IRQHandler                 /* USB OTG HS                   */                   
  .word     DCMI_IRQHandler                   /* DCMI                         */                   
  .word     0                                 /* Reserved                     */                   
  .word     HASH_RNG_IRQHandler               /* Hash and Rng                 */
  .word     FPU_IRQHandler                    /* FPU                          */
  .word     UART7_IRQHandler                  /* UART7                        */      
  .word     UART8_IRQHandler                  /* UART8                        */
  .word     SPI4_IRQHandler                   /* SPI4                         */
  .word     SPI5_IRQHandler                   /* SPI5 						  */
  .word     SPI6_IRQHandler                   /* SPI6						  */
  .word     SAI1_IRQHandler                   /* SAI1						  */
  .word     LTDC_IRQHandler                   /* LTDC_IRQHandler			  */
  .word     LTDC_ER_IRQHandler                /* LTDC_ER_IRQHandler			  */
  .word     DMA2D_IRQHandler                  /* DMA2D                        */
  

  .size  g_pfnVectors, .-g_pfnVectors

 

I've looked even deeper into it, turns out this IRQn is 26, defined in IRQn_Type enum.

  TIM1_TRG_COM_TIM11_IRQn     = 26,     /*!< TIM1 Trigger and Commutation Interrupt and TIM11 global interrupt */

 

The HAL_NVIC_EnableIRQ, calls NVIC_EnableIRQ with that number, which is a macro for

__STATIC_INLINE void __NVIC_EnableIRQ(IRQn_Type IRQn)
{
  if ((int32_t)(IRQn) >= 0)
  {
    __COMPILER_BARRIER();
    NVIC->ISER[(((uint32_t)IRQn) >> 5UL)] = (uint32_t)(1UL << (((uint32_t)IRQn) & 0x1FUL));
    __COMPILER_BARRIER();
  }
}

Then here if I am not mistaking, we divide the number by 2^5 = 32, to find which ISERx register 32 bit words this number belongs to 26/32 = 0 so ISER0. Then we extract the lower 5 bits of 26 which is 26. And put one into bit 27 (if counting from bit 0).

That then corresponds to the following highlighted bit

ISER0 bit 27 setISER0 bit 27 set

I don't know how to see if there is some mismatch in the NVIC controller or something for why this is not firing. I hope I have provided enough information for anyone that would be able to help me :)

In your timer screenshots, TIMx_DIER=0x1, i.e. only UIE is set, not the commutation interrupt enable bit.

I don't use Cube/HAL.

JW

Thank you very much JW, this was absolutely that problem. I would have thought that either it would be enabled through CubeIDE or through the

 

HAL_TIMEx_ConfigCommutEvent(&htim1, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);

 

I was unaware of this register (DIER) DMA/Interrupt Enable Register. For anyone else that might stumble on a similar problem adding the second line below, enables the com interrupt for TIM1.

 

HAL_TIMEx_ConfigCommutEvent(&htim1, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE);
htim1.Instance->DIER |= TIM_DIER_COMIE; // THIS LINE!

 

 Again thank you very kindly for the assistance JW, I was losing my hair :grinning_face_with_sweat:

But that has me wondering if there is some HAL function call missing, or if there is something wrong in the IDE pipeline. Because the UIE bit is set but not this one considering similar IDE setup.