cancel
Showing results for 
Search instead for 
Did you mean: 

TIM1 update interrupt not firing.

mlickess
Associate II
Posted on September 11, 2015 at 13:11

Hello Everybody.

I am having trouble with the output compare function of an STM32F401RE. 

I am testing the OC function to generate a waveform by toggling an output at the compare point and the overflow point.

The counter is running and comparing correctly, but I cannot get the interrupt to fire for the update event when the counter overflows.

I have a break point in the interrupt routines, but the update event (for overflow) never occours.

The flag is being set in TIM1_SR bit 0 (UIF: Update interrupt flag), but the interrupt is not called.

My code (generated through STM32CubeMX - this is the first time I have used this program, so may be missing something with the new abstration layer!) is below.

I hope someone can help, than kyou in advance!

int main(void)

{

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init(); /* interrupt enables */

  /* Configure the system clock */

  SystemClock_Config();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_TIM1_Init();

 

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1){}

}

/**

* @brief This function handles TIM1 update interrupt and TIM10 global interrupt. - timer overflow

*/

void TIM1_UP_TIM10_IRQHandler(void)

{

  HAL_TIM_IRQHandler(&htim1); /* clears flags etc... */

}

/**

* @brief This function handles TIM1 capture compare interrupt.

*/

void TIM1_CC_IRQHandler(void)

{

  HAL_TIM_IRQHandler(&htim1); /* clears flags etc... */

}

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

  RCC_OscInitTypeDef RCC_OscInitStruct;

  RCC_ClkInitTypeDef RCC_ClkInitStruct;

  __PWR_CLK_ENABLE();

  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

  RCC_OscInitStruct.HSIState = RCC_HSI_ON;

  RCC_OscInitStruct.HSICalibrationValue = 16;

  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

  RCC_OscInitStruct.PLL.PLLM = 16;

  RCC_OscInitStruct.PLL.PLLN = 336;

  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;

  RCC_OscInitStruct.PLL.PLLQ = 7;

  HAL_RCC_OscConfig(&RCC_OscInitStruct);

  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;

  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2);

  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

}

/* TIM1 init function */

void MX_TIM1_Init(void)

{

  TIM_MasterConfigTypeDef sMasterConfig;

  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;

  TIM_OC_InitTypeDef sConfigOC;

__TIM1_CLK_ENABLE();

TIM1->DIER = 0x0002;

/* initialise basic timer operation */

  htim1.Instance = TIM1;

  htim1.Init.Prescaler = 0;

  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim1.Init.Period = 0x0fff;

  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  htim1.Init.RepetitionCounter = 0;

  HAL_TIM_OC_Init(&htim1);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);

  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;

  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;

  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;

  sBreakDeadTimeConfig.DeadTime = 0;

  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;

  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;

  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;

  HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig);

/* configure the timer to run with output compare */

  sConfigOC.OCMode = TIM_OCMODE_TIMING;

//sConfigOC.OCMode = TIM_OCMODE_INACTIVE;

  sConfigOC.Pulse = 0x00ff;

  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;

  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;

  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;

  HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1);

/* start the output compare module */

HAL_TIM_Base_Start(&htim1);

HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_1);

}

void HAL_MspInit(void)

{

  HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);

  /* System interrupt init*/

  /* SysTick_IRQn interrupt configuration */

  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

}

void HAL_TIM_OC_MspInit(TIM_HandleTypeDef* htim_oc)

{

  if(htim_oc->Instance==TIM1)

  {

    /* Peripheral clock enable */

    //__TIM1_CLK_ENABLE();                                  DOES NOT WORK HERE! moved to MX_TIM1_Init()

    /* Peripheral interrupt init*/

    HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0, 0);

    HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);

    HAL_NVIC_SetPriority(TIM1_CC_IRQn, 1, 0);

    HAL_NVIC_EnableIRQ(TIM1_CC_IRQn);

  }

}
1 REPLY 1
mlickess
Associate II
Posted on September 14, 2015 at 13:24

Solved.

The cube was initialising, but not enabling the interrupts.

I manually added 

TIM1->DIER = 0x0003;