cancel
Showing results for 
Search instead for 
Did you mean: 

HAL Trigger Input Capture from Comparator Output Internally

abotha
Associate III
Posted on September 27, 2017 at 11:41

Good Day

I am trying to Trigger an Input Capture on TIM1 Channel 1 by connecting to the output of the Comparator internally.

I am using a STM32F303and the latest CubeMX+Libs, and I can verify that the comparator is working, as I can scope the output on a selected pin, but cannot get the TIM1 Configuration to fire the Input Capture Callback.

The generated Config Code for the Comparator and the Timer are as follows, I suspect that my set-up for the Timer ins incorrect.

What should the Timer Mode, Trigger Source and Channel 1 Setting be?

/* COMP1 init function */
static void MX_COMP1_Init(void)
{
 hcomp1.Instance = COMP1;
 hcomp1.Init.InvertingInput = COMP_INVERTINGINPUT_IO1;
 hcomp1.Init.NonInvertingInput = COMP_NONINVERTINGINPUT_IO1;
 hcomp1.Init.Output = COMP_OUTPUT_TIM1IC1;
 hcomp1.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED;
 hcomp1.Init.Hysteresis = COMP_HYSTERESIS_MEDIUM;
 hcomp1.Init.BlankingSrce = COMP_BLANKINGSRCE_NONE;
 hcomp1.Init.Mode = COMP_MODE_HIGHSPEED;
 hcomp1.Init.WindowMode = COMP_WINDOWMODE_DISABLE;
 hcomp1.Init.TriggerMode = COMP_TRIGGERMODE_NONE;
 if (HAL_COMP_Init(&hcomp1) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
}
/* TIM1 init function */
static void MX_TIM1_Init(void)
{
 TIM_ClockConfigTypeDef sClockSourceConfig;
 TIM_SlaveConfigTypeDef sSlaveConfig;
 TIM_MasterConfigTypeDef sMasterConfig;
 TIM_IC_InitTypeDef sConfigIC;
 htim1.Instance = TIM1;
 htim1.Init.Prescaler = 0;
 htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim1.Init.Period = 65535;
 htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim1.Init.RepetitionCounter = 0;
 htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
 if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 if (HAL_TIM_IC_Init(&htim1) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 sSlaveConfig.SlaveMode = TIM_SLAVEMODE_DISABLE;
 sSlaveConfig.InputTrigger = TIM_TS_ITR1;
 if (HAL_TIM_SlaveConfigSynchronization(&htim1, &sSlaveConfig) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING;
 sConfigIC.ICSelection = TIM_ICSELECTION_TRC;
 sConfigIC.ICPrescaler = TIM_ICPSC_DIV1;
 sConfigIC.ICFilter = 0;
 if (HAL_TIM_IC_ConfigChannel(&htim1, &sConfigIC, TIM_CHANNEL_1) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

I have attached some screenshots of the configuration and the CubeMX Config File.

Kind Regards

#timer #trigger-timer #comparator #hal #input_capture
7 REPLIES 7
Jan Waclawek
Senior II
Posted on September 27, 2017 at 13:00

Is there such internal interconnection (comparator-to-capture channel) at all? All I can find in RM is comparator-to-BREAK...

JW

Posted on September 27, 2017 at 13:31

Hi Jan

I believe so, attached the COMP and TIMER block diagrams from the Reference Manual;

0690X000006047yQAA.jpg0690X000006047KQAQ.jpg
Posted on September 27, 2017 at 13:49

Indeed.

I don't speak Cube, but can you get the capture interrupt fired from an external input (pin) rather than the internal trigger?

You might want to read out and check the relevant registers both from TIM and COMP.

JW

abotha
Associate III
Posted on September 28, 2017 at 11:24

Hi Jan

I have linked them externally for now, but would really like to know how to do this internally. As you know reading the timer section from the reference manual is no trivial task, but I have tried to no avail to see how to link the timer to the Comparator output. 

Posted on September 28, 2017 at 12:41

Post the content of timer and comparator registers, when set to internal link (and not working).

JW

Adam BERLINGER
ST Employee
Posted on September 29, 2017 at 10:59

Hello,

the problem seems to be in this line:

sConfigIC.ICSelection = TIM_ICSELECTION_TRC;�?�?

This configuration connects the input capture to the timer trigger.

The correct configuration should be:

sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI;�?�?

Best regards,

Adam

Posted on September 29, 2017 at 11:27

Okay, so the internal trigger is somehow routed through the 'normal' inputs, as if it would be part of the GPIO matrix, correct?

Can this piece of wisdom be please conveyed somehow into the *primary* source, i.e. RMs, particularly into the Timer chapter (which currently has only one mention of compare input namely into BREAK2) and possibly also the 'interconnections' chapter too?

Thanks,

Jan Waclawek

PS. I wonder what is the behaviour if several comparators have enabled outputs towards the same comparator, but then the same question can be asked of the GPIO matrix too.