stm32h743 Timer2 or whatever timer capture with external clock
This is driving me crazy, whay timers on STM32 are so complicated and no normal information / datasheets etc, how to use.
Timer2 is working (external clock), Timer interrupt (TIM_IT_UPDATE) also working, but not capture interrupt. And signal is also present on capture pin, when I manually write capturing code without timer inside main() thread, then it's working. And if I check premade HAL code, there is no timer starting function to enable capture interrupt, just only update interrupt.
So my initial code:
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_DISABLE;
if (HAL_TIM_Base_Init(&htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_ETRMODE2;
sClockSourceConfig.ClockPolarity = TIM_CLOCKPOLARITY_NONINVERTED;
sClockSourceConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1;
sClockSourceConfig.ClockFilter = 0;
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_4) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
htim2.IC_CaptureCallback = User_TIM2_IC_CaptureCallback;
htim2.PeriodElapsedCallback = User_TIM2_IC_CaptureCallback; // TESTING
/* USER CODE END TIM2_Init 2 */
}Interrupt tread:
void User_TIM2_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
//if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_4)
{
Line++; //TESTING
}
}
And how I start timer before main() while(1) { ...
htim2.State = HAL_TIM_STATE_BUSY;
__HAL_TIM_ENABLE_IT(&htim2, TIM_IT_CC4);
__HAL_TIM_ENABLE(&htim2);
More, I test now this way, that I use internal clock, then promt capture starts working. So finally how can I use timer capture with external clock (what is major in my solution, slave mode by external clock also doesn't work).
