Skip to main content
Ludovic Durand-Texte
Associate II
March 27, 2019
Question

Alternative use of timer output compare (pwm) and input compare on the same channel

Hi,

I use a timer channel and a DMA stream to generate a PWM signal.

At Transfer Complete interruption, I swap the channel to input capture mode to get an answer from external device, using the same DMA.

At transfer (reception) complete, I swap again to output compare mode.

It works, except that after having received data and reconfigured the timer channel to output compare, the first two data loaded by the dma into the CCR are not taken in account and the timer channel output stays 1 for two periods.

(It does the same if under debug I manually disable the DMA and force a value into the CCR)

The peripheral don't understand the command and of course do not send any answer. But after that, the timer channel is set again to output compare for next cycle and the output works again normally for one cycle.

So one in two of the cycle works : I send the command, I read the peripheral answer, but I can't send a right command just after having received an answer.

If I configure the external device to do not send answer, it works perfectly, I can send data, turn in input compare, later turn again in output compare and send the data correctly.

So the problem occurs only when the external device send an answer and a TC IT occurs.

To set in output compare mode I use TIM_OCxInit() and TIM_OCxPreloadConfig()

To set in input compare I use TIM_ICInit()

I also change the direction of DMA

I do miss something but do not know what.

Can anybody help?

Thanks in advance.

Ludovic

This topic has been closed for replies.

17 replies

Ludovic Durand-Texte
Associate II
March 16, 2022

Hi,

The timer is in input capture mode with the following config. Each time the input changes, the timer latches the counter value in the TIMx_CCRx register and generates a dma request or interrupt, depending of the bit set into TIMx_DIER (CCxIE and/or CCxDE)

   // init TIM_ICInitStructure

   TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_BothEdge;

   TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

   TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

   TIM_ICInitStructure.TIM_ICFilter = 0x00; // other value generates problem for output...

You have explanation in the "18.3.7 Input Capture Mode" of the "RM0431 Reference Manual".

So, you can check the CCxIF bit of the TIMx_SR register to debug and see if the timer triggers the input change.

Best regards.

Ludovic Durand-Texte
Associate II
March 16, 2022

In addition, to manage the captured data, you use interrupt or DMA (or read the CCxIF bit of the TIMx_SR register). You make the choice with the CCxIE and CCxDE bits in the TIMx_DIER register.

Regards

DN.4
Visitor II
March 30, 2022

Hi @Ludovic Durand-Texte​ ,

It works !! :)

I Wanted to thank you very much, you helped me a lot!

I still need to continue testing and integration but it looks good.

Thank you very much!!