cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4 Discovery timer syncronization

Leonelf
Associate II
Posted on March 30, 2015 at 16:46

I am currently trying to generate a squarewave on A8 with TIM1 (50% duty PWM1) and use TIM2 as a slave to trigger an interrupt every 4096th tick (this is used to reset the counter of an LED driver and to push new data).

The problem is, that the interrupt is once triggered before the timers are even initialized (breakpoint) and then never again. My code: Timer initialization:


RCC->APB1ENR |= RCC_APB1Periph_TIM2;

RCC->APB2ENR |= RCC_APB2Periph_TIM1;


TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);

TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Reset);

TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_External1);

TIM_SelectInputTrigger(TIM2, TIM_TS_ITR0);


TIM_TimeBaseInitTypeDef timerInitStructure;

timerInitStructure.TIM_Prescaler = 2 - 1;

timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

timerInitStructure.TIM_Period = 4 - 1;

timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;

timerInitStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM1, &timerInitStructure);


timerInitStructure.TIM_Prescaler = 2 - 1;

timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;

timerInitStructure.TIM_Period = 2048 - 1;

timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;

timerInitStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM2, &timerInitStructure);


NVIC_InitTypeDef nvicStructure;

nvicStructure.NVIC_IRQChannel = TIM2_IRQn;

nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;

nvicStructure.NVIC_IRQChannelSubPriority = 1;

nvicStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&nvicStructure);

TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);


TIM_OCInitTypeDef ocInitStruct;

ocInitStruct.TIM_OCMode = TIM_OCMode_PWM1;

ocInitStruct.TIM_OutputState = TIM_OutputState_Enable;

ocInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;

ocInitStruct.TIM_Pulse = 2;

TIM_OC1Init(TIM1, &ocInitStruct);

TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);


GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);

TIM_Cmd(TIM1, ENABLE);

TIM_Cmd(TIM2, ENABLE);

ISR:


void
TIM2_IRQHandler()

{

if
(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

{

TIM_ClearITPendingBit(TIM2, TIM_IT_Update);

GPIO_SetBits(BLANK_Port, BLANK_Pin);

GPIO_ResetBits(BLANK_Port, BLANK_Pin);

}

}

What did I do wrong? are TIM1 and 2 not syncable with each over?
8 REPLIES 8
Posted on March 30, 2015 at 17:54

This perhaps


TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);

TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Enable);

TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);

TIM_SelectInputTrigger(TIM2, TIM_TS_ITR0); // TIM1's TRGO into TIM2

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Posted on March 30, 2015 at 17:55

Plus this to get PA8 outputting

  /* Main Output Enable */

  TIM_CtrlPWMOutputs(TIM1, ENABLE);

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Leonelf
Associate II
Posted on March 30, 2015 at 18:24

Your second post helped me with activating the PWM signal, it's now outputting 21MHz on A8, thanks!

But I don't really understand the point of your first post. In the documentation it states that the External1 option clocks the timer at the rising edge of the internal trigger (output of TIM1). 

TIM_SlaveMode_Reset according to documentation:

''Reset Mode - Rising edge of the selected trigger input (TRGI) reinitializes the counter

and generates an update of the registers.''

TIM_SlaveMode_External1:

''External Clock Mode 1 - Rising edges of the selected trigger (TRGI) clock the counter.''
Posted on March 30, 2015 at 21:17

Probably because I was confused about you using Reset as a source, and that you wanted them synchronized.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Leonelf
Associate II
Posted on March 30, 2015 at 22:37

what now remains is the problem with the interrupt since that one still isnt being triggered :(

Posted on March 30, 2015 at 22:53

what now remains is the problem with the interrupt since that one still isnt being triggered :(

I'd guess because Reset or Enable are one time conditions, and you'd need thousands to get an interrupt? Update?

That and with C++ syntax, you'd want to watch for name mangling of the IRQ, but you indicated it was called at least once.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
Leonelf
Associate II
Posted on March 31, 2015 at 12:34

I now replaced the master output source with ''update'' (instead of reset) and now tim2 is being triggered. I will see if it updates every tim1 tick or on overflow... but at least it does something :p

thanks for your help!
Posted on March 31, 2015 at 13:50

I would imagine you could lose the interrupt, and have two timers on the same APB, without prescalers, in PWM mode, that could do what you're doing.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..