AnsweredAssumed Answered

Slave mode trigger on STM32F411RE

Question asked by Christophe on Sep 2, 2015
Latest reply on Sep 3, 2015 by Christophe
Hi,


I'm trying to enable the timer 2 on a rising edge of a signal that i have
connect on the tim2_ch1. I understand that the slave trigger mode of the timer
can do that. I want to generate a signal on the tim2_ch2 on the rising edge of
the tim2_ch1.

Additionally, I would like to generate another signal on tim3_ch2 on
the rising edge of the tim2_ch2.


I did that by connecting the tim2 and tim3 as master and slave and it's worked.


But the tim2_ch2 is generated before the rising edge of the tim2_ch1.

Is it possible to start a counter on an external trigger or i have missed something?


I've copied my program below :


    TimHandle.Instance = TIM2;
  TimHandle.Init.ClockDivision = 0;
  TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
  TimHandle.Init.Period =  10; //Compteur_SCK
  TimHandle.Init.Prescaler = 0;
  TimHandle.Init.RepetitionCounter = 0;

  ocConfig.OCMode = TIM_OCMODE_TOGGLE;
  ocConfig.OCPolarity = TIM_OCPOLARITY_HIGH;

  icConfig.ICFilter         = 1;
  icConfig.ICPolarity         = TIM_ICPOLARITY_RISING;
  icConfig.ICPrescaler         = TIM_ICPSC_DIV1;
  icConfig.ICSelection         = TIM_ICSELECTION_DIRECTTI;

  if(HAL_TIM_OC_Init(&TimHandle) != HAL_OK)
  {
    Error_Handler();
  }


  if(HAL_TIM_OC_ConfigChannel(&TimHandle,&ocConfig,TIM_CHANNEL_2) != HAL_OK)
  {
    Error_Handler();
  }

  if(HAL_TIM_IC_ConfigChannel(&TimHandle,&icConfig,TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }

  slaveConfig.InputTrigger = TIM_TS_TI1FP1;
  slaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;
  slaveConfig.TriggerFilter = 1;
  slaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING;
  slaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;

  if( HAL_TIM_SlaveConfigSynchronization(&TimHandle,&slaveConfig) != HAL_OK)
  {
    Error_Handler();
  }

  masterConfig.MasterOutputTrigger =  TIM_TRGO_OC2REF;
  masterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;

  if(HAL_TIMEx_MasterConfigSynchronization(&TimHandle,&masterConfig) != HAL_OK)
  {
    Error_Handler();
  }

  /////////////////////////////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////////////////////////////


  Tim2Handle.Instance = TIM3;
  Tim2Handle.Init.ClockDivision = 0;
  Tim2Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
  Tim2Handle.Init.Period = 2000;
  Tim2Handle.Init.Prescaler = 0;


  oc2Config.OCMode = TIM_OCMODE_PWM1;
  oc2Config.OCPolarity = TIM_OCPOLARITY_HIGH;
  oc2Config.Pulse     = 20;



  if(HAL_TIM_OC_Init(&Tim2Handle) != HAL_OK)
  {
    Error_Handler();
  }


  if(HAL_TIM_PWM_ConfigChannel(&Tim2Handle,&oc2Config,TIM_CHANNEL_2) != HAL_OK)
  {
    Error_Handler();
  }


  if(HAL_TIM_IC_ConfigChannel(&Tim2Handle,&icConfig,TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }

  slaveConfig.InputTrigger = TIM_TS_ITR1;
  slaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;
  slaveConfig.TriggerFilter = 1;
  slaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING;
  slaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;

  if( HAL_TIM_SlaveConfigSynchronization(&Tim2Handle,&slaveConfig) != HAL_OK)
  {
      Error_Handler();
  }


  if(HAL_TIM_IC_Start(&TimHandle,TIM_CHANNEL_1) != HAL_OK)
  {
  Error_Handler();
  }

  if(HAL_TIM_IC_Start(&Tim2Handle,TIM_CHANNEL_1) != HAL_OK)
  {
  Error_Handler();
  }




  if(HAL_TIM_OC_Start(&TimHandle,TIM_CHANNEL_2) != HAL_OK)
  {
    Error_Handler();
  }


  if(HAL_TIM_OC_Start(&Tim2Handle,TIM_CHANNEL_2) != HAL_OK)
  {
      Error_Handler();
  }




Thanks for your help, sorry for my english..

Outcomes