cancel
Showing results for 
Search instead for 
Did you mean: 

stm32h743 Timer2 or whatever timer capture with external clock

aatom
Visitor

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).

2 REPLIES 2
TDK
Super User

The Reference Manual goes into great depth to describe each feature of the timer.

https://www.st.com/resource/en/reference_manual/rm0433-stm32h742-stm32h743753-and-stm32h750-value-line-advanced-armbased-32bit-mcus-stmicroelectronics.pdf

 

There are also many different working example projects that show various features of the timer. Here is one for input catpure:

https://github.com/STMicroelectronics/STM32CubeH7/tree/master/Projects/NUCLEO-H743ZI/Examples/TIM/TIM_InputCapture

 

It's unclear what you're trying to do here specifically. Do you have an external clock and you're trying to do input capture on a separate pin? External vs internal clock setup is independent of input capture on a channel.

Look at the various HAL_TIM_* function calls in the example to see how to use them.

If you feel a post has answered your question, please click "Accept as Solution".
aatom
Visitor

At moment I don't get to work 2M parallel camera on STM32H753I-EVAL (on EVAL there is no camera interface, I connect to big connectors / STM32H753I pins) and like to check camera signals, so CLK timer input and LF (horisontal timing) capture, so I can see, how stable signal is and colums count.

But it's doesn't matter, there is more solutions, what can need precise (external clock) time meansurings.

 

I have read Reference Manual many times and sorry, its not "great depth":

* What is Timer internal input frequency, on clock configuration, there is APB1 / APB2 Timer clocks, what is what

* Trigger controller / Slave controller / Encoder interface is just like black box, no good explanation

* StmCubeMx configuration and Reference Manual don't match, example on manual "External clock mode1: external input pin (TIx)" , but on Reference Manual timer block diagram there is TIMx_ETR and same on StmCubeMx.

* ITR input detailed explanation

... and this is just begginning

 

Github example also don't help, because uses internal clock and StmCubeMx generates different code.