cancel
Showing results for 
Search instead for 
Did you mean: 

TIM2 Retriggerable Output (COMP1_Output ETR)

simo zz
Senior

Hello,

I am using an STM32L432KCU and I trying to use the TIM2_CH1 output as a re-triggerable output with TIM2 ETR input connected to COMP1_Output.

The configuration done by STM32CubeIDE (version 1.3.0, build 5720_20200220_1053 (UTC)) is the following:

0693W000000WePZQA0.png

0693W000000WeLIQA0.png

0693W000000WeL4QAK.png

while the code generated is:

/* TIM2 init function */
void MX_TIM2_Init(void)
{
  LL_TIM_InitTypeDef TIM_InitStruct = {0};
  LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
 
  LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
  /* Peripheral clock enable */
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
 
  TIM_InitStruct.Prescaler = 65535;
  TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  TIM_InitStruct.Autoreload = 1220;
  TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  LL_TIM_Init(TIM2, &TIM_InitStruct);
  LL_TIM_EnableARRPreload(TIM2);
  LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
  TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_RETRIG_OPM2;
  TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
  TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
  TIM_OC_InitStruct.CompareValue = 4294967295;
  TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  LL_TIM_OC_Init(TIM2, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
  LL_TIM_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH1);
  LL_TIM_SetOnePulseMode(TIM2, LL_TIM_ONEPULSEMODE_SINGLE);
  LL_TIM_SetOCRefClearInputSource(TIM2, LL_TIM_OCREF_CLR_INT_NC);
  LL_TIM_DisableExternalClock(TIM2);
  LL_TIM_ConfigETR(TIM2, LL_TIM_ETR_POLARITY_NONINVERTED, LL_TIM_ETR_PRESCALER_DIV1, LL_TIM_ETR_FILTER_FDIV1);
  LL_TIM_SetTriggerInput(TIM2, LL_TIM_TS_ETRF);
  LL_TIM_SetSlaveMode(TIM2, LL_TIM_SLAVEMODE_COMBINED_RESETTRIGGER);
  LL_TIM_DisableIT_TRIG(TIM2);
  LL_TIM_DisableDMAReq_TRIG(TIM2);
  LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_UPDATE);
  LL_TIM_DisableMasterSlaveMode(TIM2);
  LL_TIM_SetRemap(TIM2, LL_TIM_TIM2_ITR1_RMP_USB_SOF);
  LL_TIM_SetETRSource(TIM2, LL_TIM_ETRSOURCE_COMP1);
  LL_TIM_OC_EnablePreload(TIM2, LL_TIM_CHANNEL_CH1);
  LL_AHB2_GRP1_EnableClock(LL_AHB2_GRP1_PERIPH_GPIOA);
    /**TIM2 GPIO Configuration    
    PA0     ------> TIM2_CH1 
    */
  GPIO_InitStruct.Pin = LL_GPIO_PIN_0;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_DOWN;
  GPIO_InitStruct.Alternate = LL_GPIO_AF_1;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}

In the main function, COMP1 and TIM2 are simply enabled as follows:

LL_COMP_Enable(COMP1);
LL_TIM_EnableCounter(TIM2);

but the TIM2_CH1 output does not respond to changes on COMP1_INP.

What's wrong with the setup ?

Thanks,

s.

6 REPLIES 6
berendi
Principal

What exactly are you trying to achieve? What should the timer do upon receiving a trigger from the comparator?

simo zz
Senior

​@berendi​ , what I need to do is to have a re-triggerable output each time the COMP1 triggers the TIM2 TRGI as shown below.

0693W000000WeigQAC.png

For this purpose the TIM2 ETR is connected to COMP1_OUT.

berendi
Principal

How many APB1 cycles long should the output remain high?

Can you post the contents of the timer/GPIOA/comparator registers? I can't decypher HAL/LL code.

simo zz
Senior

I wouldn't like to have misunderstood the configuration. STM32CubeIDE and the MCU TRM say that TIM2 ETR input can be connected to COMP2_OUT (TIM2 OR2 register), but I don't see this into the TIM2 block diagram:

0693W000000WggcQAC.png

s.

The timer block diagram is just a copy-paste job common to all STM32 series, it doesn't show options specific to your MCU.​

chaida13
Associate II

I want to use Trigger + reset mode for my timer but I can't find it, someone has an idea please.

I'm using nucleo stm32 f429zi and stm32CubeIDE 1.3.0 version