How create master/slave timer ?
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