cancel
Showing results for 
Search instead for 
Did you mean: 

How create master/slave timer ?

lidiriel
Associate II

Hello all,

I work with the board stm32F746G disco

I try to create a master/slave timer. I use the timer3 to master and timer2 in slave.

I would like count every second with timer3 and for each trigger on timer3 i would like launch timer2 for a delay of 1/2 second.

my code doesn't work why...

Here is an my initialize code for master timer : TIM3

void configure_trigger_timer(struct trigger_frequency_entry freq)
{
 
	// Base configuration
        htim3.Instance = TIM3;
	htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
	htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
	htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
        htim3.Init.Prescaler = 10799; //freq.prescaler;
        htim3.Init.Period = 19999;//freq.period;
    
        if (HAL_TIM_Base_Init(&htim3) != HAL_OK){
  		 _Error_Handler(__FILE__,__LINE__);
         }
  // Clock configuration
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK){
  		Error_Handler();
  }
 
  // Master configuration
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK){
  		Error_Handler();
  }
}

And here the code for slave :

void configure_delay_timer(void)
{
  // Base configuration
	htim2.Instance = TIM2;
	htim2.Init.Prescaler = 10799; // 108MHz / 10800 = 10kHz => 100µs
  htim2.Init.Period = 4999; // 10kHz / 5000 = 2Hz => 0.5s
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;(uint32_t)htim2.Init.Period);
  if (HAL_TIM_Base_Init(&htim2) != HAL_OK){
  		_Error_Handler(__FILE__,__LINE__);
  }
  // Clock source configuration
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK){
      Error_Handler();
  }
  // Slave configuration
  TIM_SlaveConfigTypeDef sSlaveConfig = {0};
  sSlaveConfig.SlaveMode = TIM_SLAVEMODE_COMBINED_RESETTRIGGER;
  sSlaveConfig.InputTrigger = TIM_TS_ITR2; // trigger source is TIM3
 
  if (HAL_TIM_SlaveConfigSynchronization(&htim2, &sSlaveConfig) != HAL_OK){
  		Error_Handler();
  }
 
 
  // Master configuration
 
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK){
  		Error_Handler();
  }
 
 
}

i start timer with code :

HAL_TIM_Base_Start_IT(&htim3);
HAL_TIM_Base_Start_IT(&htim2);

I don't find the error in my code.

thanks by advance for the help.

Ludovic

6 REPLIES 6

> my code doesn't work why...

How do you know? What is the expected behaviour and what is the observed one?

Read out the timers' registers' content and check/post.

JW

lidiriel
Associate II

I think i don't understand the timer operation.

I have two counter in HAL_TIM_PeriodElapsedCallback(), one for each timer :

the expected behaviour :

the master timer must start the slave timer.

If i run the master for N elapsed period I hope get the same number N elapsed period for the slave.

I would use the master like a trigger for N ticks. And for each i would like create a delay before a new action.

Ludovic

lidiriel
Associate II

Yes. I understand. It's work. I must :

activate DIER register to enable interrupt

add code to stop delay timer after the end of the delay : i think it is not the good way: one shot mode exist ?

and finaly start only the master

> the expected behaviour :

I don't quite understand your explanation. Try to draw a diagram.

JW

lidiriel
Associate II

this is a diagram of i want. TIM3 can be replace by an external trigger with ETR entry of TIM2. The numbers : N, p, x and y are the parameters of this process.0690X000006DZnSQAW.png

Ludovic

For this, you don't really need master-slave. If you trigger TIM1 directly from the first line signal (whether coming from TIM3 or externally), running it in PWM2 mode, or simply inverting its output, and using one-pulse mode, you should be able to achieve this waveform.

But while I don't use Cube, I can't see any substantial problem in your first post's code. So try to write code, run an experiment, and if it still does not do what you intended, post both the code and your findings.

JW