cancel
Showing results for 
Search instead for 
Did you mean: 

Nucleo-f746zg input_capture not working

shabbir.hussain
Associate III

I'm trying to get the basic input_capture example to work. I've created a project for the nucleo-f746zg board using cubemx and I've followed the cubeF7 example for input capture to be able to capture the ON time of the blue push button on my board. However, interrupt never triggers. I have a breakpoint in my truestudio IDE that never triggers. I have also tried reading the memory address using stmstudio, but the value never changes either. Have I improperly set up my code?

The following is my main function where I call the timer config and start the input_capture (IC) in interrupt mode.

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_ETH_Init();
  MX_USART3_UART_Init();
  MX_USB_OTG_FS_PCD_Init();
  MX_TIM2_Init();
  /* USER CODE BEGIN 2 */
 
  HAL_StatusTypeDef status;
   status = HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);
   if(status != HAL_OK) // Start timer
   {
 	  Error_Handler();
   }
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

the following is the timer config function:

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 = 0xffff;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  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 */
 
}

Lastly, I have my interrupt callback:

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
	 if (htim->Instance==TIM2)
	 {
		 input_capture= __HAL_TIM_GetCompare(&htim2, TIM_CHANNEL_1);    //read TIM2 channel 1 capture value
	     __HAL_TIM_SetCounter(&htim2, 0);    //reset counter after input capture interrupt occurs
	 }
}

1 REPLY 1
shabbir.hussain
Associate III

I'm happy to report that I've fixed this issue and here is how:

  1. On the Nucleo-144 pin boards the user button and timer2_channel 1 are not shorted. There is a pad that can be bridged (SB180) to short the blue user button to timer2_channel 1.
  2. My CubeMX project was poorly configured such that the NVIC interrupt was not enabled. When it is enabled, the stm32f7xx_it.c file has the following function: void TIM2_IRQHandler(void)

If anyone has the same problem as me (input_capture not working) here is a checklist of items to look for:

  1. in CubeMX: a clock source is chosen, the channel is set to input_capture direct mode, the counter period is not 0, the NVIC menu has global interrupt checked.
  2. On you board, verify that the chosen channel pin is connected to the signal you want to capture
  3. In the project, the stm32f7xx_it.c file has the following function: void TIM2_IRQHandler(void)
  4. In the project, in your main method you enable the interrupt by calling: HAL_TIM_IC_Start_IT(&htimX, TIM_CHANNEL_X);
  5. In the project, you implement the function: HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_1);