2015-11-13 08:22 AM
Hi,
I'm trying to measure a frequency of tenth kHz using TIM2IC2 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 AF 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 */
voidMX_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 */ voidTIMMeas(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 */ voidHAL_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.2015-11-16 06:21 AM
Why do I see this in TIM2 register?
2015-11-16 08:22 AM
Guess you'd want to double check the clocks, and check IAR's documentation about how their debugger works, or why it might report in this fashion.
BTWuwDiffCapture = (uwIC2Value2 - uwIC2Value1);
// Works for ALL unsigned 16-bit values, your other math is broken
2015-11-17 01:14 AM
So now I get the frequency (polling fro flags) but still interrupts aren't working.
'W' means write-only register. But DIER is actually a RW register. So I guess it is only a debugger problem. DIER contains bits to enable IC interrupts as far as I know.Thanks for the remark on computation.2015-11-17 02:03 AM
Ok it seems to work now. I must yet try to compute 4-5 channels simultaneously.
Thanks for the helps