cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 - Timer Sync - Trigger Mode does not work

smrtkai
Senior
Posted on February 16, 2017 at 15:49

Hi,

I am having trouble to synchronize the start of two or more timers at the same time. Reading the manual, I figured out that, I have to select one timer as master timer with master configuration 'Enable' ('the counter enable signal is used as a trigger output (TRGO). It is used to start several timers at the same time, ...').

The slaves are configured in slave mode 'Trigger mode' ('the counter start at a rising edge of the trigger TRGI (but is not reset). Only the start of the counter is controlled.').

When I start the master timer with a delay, I expect the slave timer to start with the same delay. But the slave does start immediately. This is how my test code looks like:

HAL_TIM_OC_Start(&htim4, TIM_CHANNEL_2);

HAL_Delay(1000);

HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_4);

My master/slave configuration for the timers looks like this:

void MX_TIM3_Init(void) {

    ....

    sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE;

    sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;

    ....

}

void MX_TIM4_Init(void) {

    ...

    sSlaveConfig.SlaveMode = TIM_SLAVEMODE_TRIGGER;

    sSlaveConfig.InputTrigger = TIM_TS_ITR2;

    ...

}

I am using CubeMX and a STM32F4 Discovery Board.

Thank you for any help. I have no idea what I am missing. I have read the manual several times, I have checked the internal trigger connections and I have also checked the CR2 and SMCR registers. 

#stm32f4 #timer-synchronization #timer-master-slave #timer-trigger
1 ACCEPTED SOLUTION

Accepted Solutions
smrtkai
Senior
Posted on February 20, 2017 at 17:42

Found the solution.

It is a problem using the HAL-library and STM32CubeMX. The HAL-interface HAL_TIM_OC_Start or HAL_TIM_PWM_Start enable the given channel AND start the timer.

If you want to sync timers using trigger mode. You only need to activate the channels of the slave timer and the start the master timer.

To enable a channel, use this code snippet:

htim1.Instance->CCER |= (TIM_CCx_ENABLE << TIM_CHANNEL_1);

I also disable and reset the slave timers. Just to be sure.

htim1.Instance->CR1 &= ~(TIM_CR1_CEN);   // disable timer

htim1.Instance->CNT = 0;                 // reset timer

And finally start the master timer.

HAL_TIM_OC_Start(&htim4, TIM_CHANNEL_2);

View solution in original post

4 REPLIES 4
Posted on February 16, 2017 at 17:23

TIM_SLAVEMODE_TRIGGER vs 

TIM_SLAVEMODE_RESET ? or GATED

In the SPL, PWM Input used

/* Select the TIM2 Input Trigger: TI1FP1 */

TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1);

/* Select the slave Mode: Reset Mode */

TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);

TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
smrtkai
Senior
Posted on February 17, 2017 at 10:11

Thanks for your response. But unfortunately, I do not understand what you mean.

What do you mean with '

In the SPL, PWM Input used

'?
smrtkai
Senior
Posted on February 20, 2017 at 10:04

Does anyone have a working example with two timers synchronized through trigger mode? I can only find examples in gated mode.

smrtkai
Senior
Posted on February 20, 2017 at 17:42

Found the solution.

It is a problem using the HAL-library and STM32CubeMX. The HAL-interface HAL_TIM_OC_Start or HAL_TIM_PWM_Start enable the given channel AND start the timer.

If you want to sync timers using trigger mode. You only need to activate the channels of the slave timer and the start the master timer.

To enable a channel, use this code snippet:

htim1.Instance->CCER |= (TIM_CCx_ENABLE << TIM_CHANNEL_1);

I also disable and reset the slave timers. Just to be sure.

htim1.Instance->CR1 &= ~(TIM_CR1_CEN);   // disable timer

htim1.Instance->CNT = 0;                 // reset timer

And finally start the master timer.

HAL_TIM_OC_Start(&htim4, TIM_CHANNEL_2);