AnsweredAssumed Answered

STM32F407VG Timers Synchronization question.

Question asked by Vycas.Arturas.001 on Nov 13, 2015
Latest reply on Nov 18, 2015 by Vycas.Arturas.001
Hello everyone, i need some help, so if anyone could, i'd be grateful. I need to start two timers instantaneously at the same time, when external event happens. Lets say, rising edge on the pin should trigger two timers to start. How it should look like can be seen in RM0090 18.3.15 - Timer Synchronization, Figure 179. However, i am not able to achieve is and i dont quite understand from RM which Timers can work as masters and which ones can work as slaves. I was trying different kind of combinations, but here is my code for now (which is not doing what i want). 

#include "main.h"

uint32_t counter = 0;

int main(void)
{
rcc_config();
get_system_clocks();
gpio_config();
timers_config();

GPIO_SetBits(GPIOD, GPIO_Pin_12);

while(1)
{
counter++;
}
}

void get_system_clocks(void)
{
RCC_GetClocksFreq(&rcc_struct);
}

void rcc_config(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIODEN, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1ENR_GPIOAEN, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

}

void gpio_config(void)
{
/* Configure PD12, PD13 in output push-pull mode for LEDs. PD13 used as CS for SPI */
gpio_struct.GPIO_Pin = GPIO_Pin_12;
gpio_struct.GPIO_Mode = GPIO_Mode_OUT;
gpio_struct.GPIO_OType = GPIO_OType_PP;
gpio_struct.GPIO_Speed = GPIO_Speed_25MHz;
gpio_struct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOD, &gpio_struct);

/* TIM2 chennel2 configuration : PD.02 */
gpio_struct.GPIO_Pin   = GPIO_Pin_6;
gpio_struct.GPIO_Mode  = GPIO_Mode_AF;
gpio_struct.GPIO_Speed = GPIO_Speed_25MHz;
gpio_struct.GPIO_OType = GPIO_OType_PP;
gpio_struct.GPIO_PuPd  = GPIO_PuPd_DOWN ;
GPIO_Init(GPIOA, &gpio_struct);

/* Connect TIM3 External trigger pin to AF2 */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM3);

}

void timers_config(void)
{
/* Timer 3 Configuration */
timerbase_struct.TIM_Prescaler         = 8400;
timerbase_struct.TIM_Period            = 20000;
timerbase_struct.TIM_CounterMode       = TIM_CounterMode_Up;
timerbase_struct.TIM_ClockDivision         = TIM_CKD_DIV1;
timerbase_struct.TIM_RepetitionCounter = 0x00;
TIM_TimeBaseInit(TIM3, &timerbase_struct);

//TIM_ETRConfig(TIM3, TIM_ExtTRGPSC_OFF,  TIM_ExtTRGPolarity_NonInverted, 0x00);

/* Timer 4 Configuration */
timerbase_struct.TIM_Prescaler         = 8400;
timerbase_struct.TIM_Period            = 20000;
timerbase_struct.TIM_CounterMode       = TIM_CounterMode_Up;
timerbase_struct.TIM_ClockDivision         = TIM_CKD_DIV1;
timerbase_struct.TIM_RepetitionCounter = 0x00;
TIM_TimeBaseInit(TIM4, &timerbase_struct);

timer_input_struct.TIM_Channel = TIM_Channel_1;
timer_input_struct.TIM_ICPolarity = TIM_ICPolarity_Rising;
timer_input_struct.TIM_ICSelection = TIM_ICSelection_DirectTI;
timer_input_struct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
timer_input_struct.TIM_ICFilter = 0x0;
TIM_ICInit(TIM3, &timer_input_struct);

/* Configure Timer3 as master */
TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Enable);
TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable);
TIM_SelectInputTrigger(TIM3, TIM_TS_ITR1);
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);


/* Configure Timer4 as slave */
TIM_SelectInputTrigger(TIM4, TIM_TS_ITR1);
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger);

}

Outcomes