cancel
Showing results for 
Search instead for 
Did you mean: 

Timers wont fire interrupt but normal gpio interrupt works

MSadf.1
Associate II

I have configured a timer to trigger on a rising edge of my signal so that I can measure the period of my signal. I tried the basic gpio interrupts and it worked fine, but when I tried it with the timer interrupt, the callback was never called, as if the timer didn't react to the rising edges of my signal. How can I solve this problem ? You can find my code and the ioc file attached.

Thank you for your time

#include "main.h"
#include "string.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
 
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define START 0
#define END 1
#define CLOCK_FREQUENCY 550000000
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
#if defined ( __ICCARM__ ) /*!< IAR Compiler */
#pragma location=0x30000000
ETH_DMADescTypeDef  DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
#pragma location=0x30000200
ETH_DMADescTypeDef  DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
 
#elif defined ( __CC_ARM )  /* MDK ARM Compiler */
 
__attribute__((at(0x30000000))) ETH_DMADescTypeDef  DMARxDscrTab[ETH_RX_DESC_CNT]; /* Ethernet Rx DMA Descriptors */
__attribute__((at(0x30000200))) ETH_DMADescTypeDef  DMATxDscrTab[ETH_TX_DESC_CNT]; /* Ethernet Tx DMA Descriptors */
 
#elif defined ( __GNUC__ ) /* GNU Compiler */
ETH_DMADescTypeDef DMARxDscrTab[ETH_RX_DESC_CNT] __attribute__((section(".RxDecripSection"))); /* Ethernet Rx DMA Descriptors */
ETH_DMADescTypeDef DMATxDscrTab[ETH_TX_DESC_CNT] __attribute__((section(".TxDecripSection")));   /* Ethernet Tx DMA Descriptors */
 
#endif
 
ETH_TxPacketConfig TxConfig;
 
ADC_HandleTypeDef hadc1;
DMA_HandleTypeDef hdma_adc1;
 
ETH_HandleTypeDef heth;
 
TIM_HandleTypeDef htim2;
 
UART_HandleTypeDef huart3;
 
/* USER CODE BEGIN PV */
uint32_t frequency; //
uint32_t period;
uint32_t time_first_edge; // time capture at the first rising edge
uint32_t time_second_edge; // time capture at the second rising edge
uint8_t edge; // if edge == 0, first edge, if edge == 1, second edge
uint32_t overflow_cnt;
char msg[35];
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART3_UART_Init(void);
static void MX_USB_OTG_HS_USB_Init(void);
static void MX_ADC1_Init(void);
static void MX_ETH_Init(void);
static void MX_TIM2_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
 
	char test[] = "transmitted";
	HAL_UART_Transmit(&huart3, (uint8_t*)test, strlen(test), 1);
	HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_1);
 
}
 
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef* htim)
{
	char test[] = "transmitted";
	HAL_UART_Transmit(&huart3, (uint8_t*)test, strlen(test), 1);
		if (edge == START)
		{
			time_first_edge =  HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
			edge = END;
			overflow_cnt = 0;
		}
		else if(edge == END)
		{
			time_second_edge =  HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
		    period = (time_second_edge + overflow_cnt*htim2.Init.Period) - time_first_edge;
		    frequency = CLOCK_FREQUENCY/period;
		    edge = START;
		    if(frequency != 0)
		    {
		    	sprintf(msg, "Frequency = %lu Hz\n\r", frequency);
		    	HAL_UART_Transmit(&huart3, (uint8_t*)msg, sizeof(msg), 100);
		    }
 
		}
 
}
/* 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_USART3_UART_Init();
  MX_USB_OTG_HS_USB_Init();
  MX_ADC1_Init();
  MX_ETH_Init();
  MX_TIM2_Init();
  /* USER CODE BEGIN 2 */
 
  HAL_TIM_Base_Start(&htim2);
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
 
 
static void MX_TIM2_Init(void)
{
 
  /* USER CODE BEGIN TIM2_Init 0 */
 
  /* USER CODE END TIM2_Init 0 */
 
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_IC_InitTypeDef sConfigIC = {0};
 
  /* USER CODE BEGIN TIM2_Init 1 */
 
  /* USER CODE END TIM2_Init 1 */
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 0;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 4294967295;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_IC_Init(&htim2) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
  sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
  sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
  sConfigIC.ICFilter = 0;
  if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM2_Init 2 */
 
  /* USER CODE END TIM2_Init 2 */
 
}

9 REPLIES 9
gbm
Lead III

Are the timer interrupts turned on? Check the timer config, NVIC tab.

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice

Which STM32?

Read out and check timer registers - is the timer running ie. is TIMx_CNT changing? Is the interrupts enabled in TIMx_DIER?

Generic interrupt troubleshooting steps here.

JW

Yeah I turned everything on

I'm using the nucleo-h723zg board. The register DIER seems to be disabled, even though I enabled it on cubeide

> The register DIER seems to be disabled, even though I enabled it on cubeide

How?

I don't use Cube, but it appears that you are supposed to use HAL_TIM_IC_Start_IT() for that.

And, of course, the interrupt has to be enabled also in NVIC.

JW

S.Ma
Principal

you can go debug mode, run then stop the code, open and view the timer HW registers, and manually edit the values. If they can't be written, the clock enable of the timer is missing.

Then manage to trigger a capture (you may have to do few instruction step by step if the timer is frozen during breakpoint).

If the status bit is SET for the capture, the interrupt enable will fire it. Then next instruction step will tell you if the interrupt service routine has been called.... or hardfault, or else

i agree with @Community member​ , use HAL_TIM_Base_Start_IT

we dont need to firmware by ourselves, lets talk

Thanks a lot it was the issue. Now it works

0693W00000Su5ahQAB.gif

we dont need to firmware by ourselves, lets talk