AnsweredAssumed Answered

Input Capture - Nucleo STM32L152RE

Question asked by gugger.nicolas on Nov 13, 2015
Latest reply on Nov 17, 2015 by gugger.nicolas
Hi,
I'm trying to measure a frequency of tenth kHz using TIM2 IC2 on PA1. I first took example code STM32Cube_FW_F3_V1.3.0\Projects\STM32F3-Discovery\Examples\TIM\TIM_InputCapture and tried to adapt it to my platform and program.
First try wasn't successful. GPIOA register and PA1 wasn't configured correctly. I set it as AF14. Then TIM2 register wasn't configured as well (even while using MX_TIM_Config function). Problem was TIM2 clock wasn't enabled. After enabling its clock I got TIM2 register configured.
I see now TIM2->CNT counting but no interrupt occurs. I don't exactly understand why I'm facing so many problems while using HAL functions and code example. Hopefully someone can help out or tell me where could be the problem. My program also uses SPI1, RTC and TempSensor and VREFINT measurements.
Here are parts of my code:
/*
   *  Configure TIM2 port:
   *        PA1 - TIM2_CH2
   */
  GPIO_InitStruct.Pin =  GPIO_PIN_1;
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF14_TIM_IC2;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

/* TIM2 init function */
void MX_TIM2_Init(void)
{
  __TIM2_CLK_ENABLE();     //Added
   
  TIM_SlaveConfigTypeDef sSlaveConfig;
  TIM_MasterConfigTypeDef sMasterConfig;
  TIM_IC_InitTypeDef sConfigIC;
 
  htim2.Instance = TIM2;
  htim2.Init.Prescaler = 0;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 0xFFFF;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  HAL_TIM_Base_Init(&htim2);
 
  HAL_TIM_IC_Init(&htim2);
 
//  sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
//  sSlaveConfig.InputTrigger = TIM_TS_ITR0;
//  HAL_TIM_SlaveConfigSynchronization(&htim2, &sSlaveConfig);
//
//  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
//  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
//  HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
 
  sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
  sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
  sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
  sConfigIC.ICFilter = 0;
//  HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1);
 
  HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_1);
 
//  HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_3);
//
//  HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_4);

/**
  * @brief  Measure Frequency
  * @param  Period
  * @retval None
  */
void TIMMeas(int8_t *Period)
{
  measDone = false;
   
  /*##-3- Start the Input Capture in interrupt mode ##########################*/
  HAL_TIM_IC_Start_IT(&htim2, TIM_CHANNEL_2);
 
  while(measDone != true);
   
  HAL_TIM_IC_Stop_IT(&htim2, TIM_CHANNEL_2);
 
}

call_back function as in code example:
/**
  * @brief  Conversion complete callback in non blocking mode
  * @param  htim : hadc handle
  * @retval None
  */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
  if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2)
  {
    if(uhCaptureIndex == 0)
    {
      /* Get the 1st Input Capture value */
      uwIC2Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
      uhCaptureIndex = 1;
    }
    else if(uhCaptureIndex == 1)
    {
      /* Get the 2nd Input Capture value */
      uwIC2Value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);
 
      /* Capture computation */
      if (uwIC2Value2 > uwIC2Value1)
      {
        uwDiffCapture = (uwIC2Value2 - uwIC2Value1);
      }
      else if (uwIC2Value2 < uwIC2Value1)
      {
        uwDiffCapture = ((0xFFFF - uwIC2Value1) + uwIC2Value2);
      }
      else
      {
 
      }
      /* Frequency computation: for this example TIMx (TIM1) is clocked by
         APB2Clk */     
      uwFrequency = HAL_RCC_GetPCLK2Freq() / uwDiffCapture;
      uhCaptureIndex = 0;
       
      measDone = true;
    }
  }
}

Thanks for the help. Let me know if anything is unclear in my explanation.

Outcomes