Skip to main content
Associate II
February 8, 2024
Solved

TIM6 and TIM7 identical, the interrupt is working for TIM6 and not for TIM7

  • February 8, 2024
  • 14 replies
  • 4718 views

Hi,

I added TIM7 to application and configured it the same way TIM6 was.

I need to use two timers for my app, code contains all the same setups for TIM6 and TIM7, except for the timer expiration counters, and I observe that the callback 

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)  

never enters in the conditional block it contains:

if ( htim == &htim7 )

{

/* code block */

}

the callback above is invoked when htim == &tim6.

The timers setup are:tim6.jpgtim7.jpg

Please help, I double checked all I could without finding a solution

 

This topic has been closed for replies.
Best answer by mƎALLEm

You called:

 

HAL_TIM_Base_Start(&htim7);

 

instead of:

 

HAL_TIM_Base_Start_IT(&htim7);

 

So add "_IT"

14 replies

mƎALLEm
ST Technical Moderator
February 8, 2024

Hello,

And if you disable TIM6 and keep TIM7, what is the behavior? TIM7 still not working?

Did you check TIM7 counter? is it really counting?

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Beppe101Author
Associate II
February 8, 2024

Hi,

I disabled TIM6 as suggested, the callback above is only invoked with ( htim == &htim3 ).

mƎALLEm
ST Technical Moderator
February 8, 2024

@Beppe101 wrote:

Hi,

I disabled TIM6 as suggested, the callback above is only invoked with ( htim == &htim3 ).


Didn't understand. You are using TIM6 and TIM7. What TIM3 is doing here?

How many timers are you using in the callback?

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Beppe101Author
Associate II
February 8, 2024

tim3.jpgHi,

the app works correctly with timers TIM3, TIM6, TIM14 together. I add TIM7 and TIM7 doesn't work as expected no matter if TIM6 is present or not. TIM14 used for PWM generation does not fire callback above. TIM3 has 2 channels for input capture and it fires the callback that does nothing with TIM3.

mƎALLEm
ST Technical Moderator
February 8, 2024

Could you please keep only TIM6 and TIM7 firing the interrupts just for the test?

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Beppe101Author
Associate II
February 8, 2024

I keep only TIM6 and TIM7 and then the callback never gets called, attempted with both 'global interrupt' settings on and off.

the main.c is

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file : main.c
 * @brief : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2023 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "crc.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/**************************************
 * Includes *
 **************************************/

#include <stdio.h>
#include <string.h>

#include "Core_interface.h"
#include "AlertsManager_interface.h"
#include "AntennaGainControl_interface.h"
#include "SerialBusManager_interface.h"

#ifdef DEBUG
#include "SerialConsoleManager_interface.h"
#endif /* DEBUG */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */


/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/**************************************
 * Global variables *
 **************************************/

/* ADC1 converted values */
uint32_t adc1Values[ADC1_NUMBER_OF_CHANNELS];

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */


/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */


/* USER CODE END 0 */

/**
 * @brief The application entry point.
 * @retval int
 */
int main(void)
{
 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */
 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_DMA_Init();
 MX_USART2_UART_Init();
 MX_ADC1_Init();
 MX_USART1_UART_Init();
 MX_CRC_Init();
 MX_TIM7_Init();
 MX_TIM6_Init();
 /* USER CODE BEGIN 2 */


 HAL_UART_MspInit(&huart1);
 HAL_UART_MspInit(&huart2);

 HAL_TIM_Base_Start_IT(&htim6);
// HAL_TIM_Base_Start_IT(&htim3);
 // HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);
 // HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);

// HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1);
// HAL_TIM_Base_Start(&htim14);
 HAL_TIM_Base_Start(&htim7);

 __HAL_RCC_CRC_CLK_ENABLE();

 /* USER CODE END 2 */

 /* Infinite loop */
 /* USER CODE BEGIN WHILE */

	TIM7->CNT = 0;
	TIM7->ARR = 500;

	TIM6->CNT = 0;
	TIM6->ARR = 900;

 while (1)
 {

 }

 /* USER CODE END WHILE */

 /* USER CODE BEGIN 3 */

 /* USER CODE END 3 */
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 /** Configure the main internal regulator output voltage
 */
 HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);

 /** Initializes the RCC Oscillators according to the specified parameters
 * in the RCC_OscInitTypeDef structure.
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
 RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
 RCC_OscInitStruct.PLL.PLLN = 8;
 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
 RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }

 /** Initializes the CPU, AHB and APB buses clocks
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 |RCC_CLOCKTYPE_PCLK1;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
 {
 Error_Handler();
 }
}

/* USER CODE BEGIN 4 */



/* USER CODE END 4 */

/**
 * @brief This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
 /* USER CODE BEGIN Error_Handler_Debug */
	/* User can add his own implementation to report the HAL error return state */
	__disable_irq();
	while (1) {
	}
 /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
 * @brief Reports the name of the source file and the source line number
 * where the assert_param error has occurred.
 * @PAram file: pointer to the source file name
 * @PAram line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
 /* USER CODE BEGIN 6 */
	/* User can add his own implementation to report the file name and line number,
 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */

 

TDK
February 8, 2024

Are you starting TIM7?

Not really providing the full picture of the problem. Consider attaching your main.c at the least.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Beppe101Author
Associate II
February 8, 2024

Here is main.c

/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file : main.c
 * @brief : Main program body
 ******************************************************************************
 * @attention
 *
 * Copyright (c) 2023 STMicroelectronics.
 * All rights reserved.
 *
 * This software is licensed under terms that can be found in the LICENSE file
 * in the root directory of this software component.
 * If no LICENSE file comes with this software, it is provided AS-IS.
 *
 ******************************************************************************
 */
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "crc.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/**************************************
 * Includes *
 **************************************/

#include <stdio.h>
#include <string.h>

#include "Core_interface.h"
#include "AlertsManager_interface.h"
#include "AntennaGainControl_interface.h"
#include "SerialBusManager_interface.h"

#ifdef DEBUG
#include "SerialConsoleManager_interface.h"
#endif /* DEBUG */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */


/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/**************************************
 * Global variables *
 **************************************/

/* ADC1 converted values */
uint32_t adc1Values[ADC1_NUMBER_OF_CHANNELS];

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */


/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */


/* USER CODE END 0 */

/**
 * @brief The application entry point.
 * @retval int
 */
int main(void)
{
 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
 HAL_Init();

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */
 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */
 MX_GPIO_Init();
 MX_DMA_Init();
 MX_USART2_UART_Init();
 MX_ADC1_Init();
 MX_TIM14_Init();
 MX_USART1_UART_Init();
 MX_CRC_Init();
 MX_TIM3_Init();
 MX_TIM1_Init();
 MX_TIM7_Init();
 /* USER CODE BEGIN 2 */


 HAL_UART_MspInit(&huart1);
 HAL_UART_MspInit(&huart2);

// HAL_TIM_Base_Start_IT(&htim6);
 HAL_TIM_Base_Start_IT(&htim3);
 HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_1);
 HAL_TIM_IC_Start_IT(&htim3, TIM_CHANNEL_2);

 HAL_TIM_PWM_Start(&htim14, TIM_CHANNEL_1);
 HAL_TIM_Base_Start(&htim14);
 HAL_TIM_Base_Start(&htim7);

 __HAL_RCC_CRC_CLK_ENABLE();

 /* USER CODE END 2 */

 /* Infinite loop */
 /* USER CODE BEGIN WHILE */

	TIM7->CNT = 0;
	TIM7->ARR = 500;


 while (1)
 {

 }

 /* USER CODE END WHILE */

 /* USER CODE BEGIN 3 */

 /* USER CODE END 3 */
}

/**
 * @brief System Clock Configuration
 * @retval None
 */
void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 /** Configure the main internal regulator output voltage
 */
 HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);

 /** Initializes the RCC Oscillators according to the specified parameters
 * in the RCC_OscInitTypeDef structure.
 */
 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
 RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV1;
 RCC_OscInitStruct.PLL.PLLN = 8;
 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
 RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
 {
 Error_Handler();
 }

 /** Initializes the CPU, AHB and APB buses clocks
 */
 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
 |RCC_CLOCKTYPE_PCLK1;
 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
 {
 Error_Handler();
 }
}

/* USER CODE BEGIN 4 */



/* USER CODE END 4 */

/**
 * @brief This function is executed in case of error occurrence.
 * @retval None
 */
void Error_Handler(void)
{
 /* USER CODE BEGIN Error_Handler_Debug */
	/* User can add his own implementation to report the HAL error return state */
	__disable_irq();
	while (1) {
	}
 /* USER CODE END Error_Handler_Debug */
}

#ifdef USE_FULL_ASSERT
/**
 * @brief Reports the name of the source file and the source line number
 * where the assert_param error has occurred.
 * @PAram file: pointer to the source file name
 * @PAram line: assert_param error line source number
 * @retval None
 */
void assert_failed(uint8_t *file, uint32_t line)
{
 /* USER CODE BEGIN 6 */
	/* User can add his own implementation to report the file name and line number,
 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
 /* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
mƎALLEm
mƎALLEmBest answer
ST Technical Moderator
February 8, 2024

You called:

 

HAL_TIM_Base_Start(&htim7);

 

instead of:

 

HAL_TIM_Base_Start_IT(&htim7);

 

So add "_IT"

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Pavel A.
February 8, 2024

Have you added the TIM7 ISR handler in your  stm.....it.c file?

Tesla DeLorean
Guru
February 8, 2024

The interrupt needs to be enable at the TIM, and NVIC, there needs to be an IRQ Handler, and that needs to call into the HAL.

A bunch of ducks need to line up, but it's NOT THAT HARD to check list the items in the chain, and check each via a peripheral view in the debugger, or printing/decoding the registers out of a serial port console.

In most Cube cases the main.c is next to worthless for the critical details, as it's incredibly superficial. You'll need to look in the IT and MSP source files.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
TDK
February 8, 2024

> HAL_TIM_Base_Start_IT(&htim6);
> HAL_TIM_Base_Start(&htim7);

Is it really so hard to see what is different here?

 

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