2017-09-27 02:41 AM
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_capture2017-09-27 04:00 AM
Is there such internal interconnection (comparator-to-capture channel) at all? All I can find in RM is comparator-to-BREAK...
JW
2017-09-27 06:31 AM
Hi Jan
I believe so, attached the COMP and TIMER block diagrams from the Reference Manual;
2017-09-27 06:49 AM
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
2017-09-28 02:24 AM
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.
2017-09-28 05:41 AM
Post the content of timer and comparator registers, when set to internal link (and not working).
JW
2017-09-29 01:59 AM
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
2017-09-29 04:27 AM
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.