cancel
Showing results for 
Search instead for 
Did you mean: 

TIM1 PWM output lags [SOLVED]

zexx86
Associate II
Posted on March 27, 2014 at 16:23

Hello

My STM32F103RBT6 has problem with PWM output (TIM2 CH4) as servo control.

This problem only occuring while USART2 is processing interrupts because of received data.

Interestingly, there is also some parallel with SysTick.

Problem is, that PWM output is not updated in specified frequency, but only about 1Hz instead 100Hz.

Interestingly, when Systick is configured with SysTick_Config (SystemCoreClock / 10000), it is as I described. But when I change SysTick to be 10x slower (SystemCoreClock / 1000), then PWM output is updated cca 10x faster too (10Hz).

This problem only occur with USART2, USART1 and USART3 seems to be OK.

Sadly, I need USART2 because of own board.

Thanks for any help.

10 REPLIES 10
chen
Associate II
Posted on March 27, 2014 at 16:32

Hi

Without code to inspect - we can only give you things to look at :

ISRs - do they clear the Status register bit? If certain bits are not cleared - the IRQ is not cleared therefore no other IRQ at the same level or below can trigger.

IRQ levels - try putting all the IRQs at different levels. I think there are 15 levels to play with, you only mentioned 3 IRQs so there should be plenty of room without having to use the sublevels.

zexx86
Associate II
Posted on March 27, 2014 at 17:42

Thanks

Here is my code http://pastebin.com/msSK6CG5

I can't include all things (its huge, ...), but all important stuff is here.

Nothing special. It uses 4 next PWM outputs (piece of code is in servos()) and one USART1 too.

I've tried to set different NVIC_IRQChannelSubPriority and NVIC_PriorityGroup, but it does not help at all.

Only reducing SysTick tick rate helped a lot.

As soon as USART2 receive some data, PWM output (only on TIM2 CH4) starts to behave incorrectly (slowly)
chen
Associate II
Posted on March 27, 2014 at 18:13

Hi

I am not seeing any code for the ISR for the SysTick.

Am I right - you set the timer up with no IRQs and just change the PWM ratio.

(You may want to provide a default ISR for the timer just in case it is tripped.

You are relying on the default ISR otherwise)

The only thing that has IRQs is the UART - is that right?

Like you, I am not seeing anything that should cause the PWM to change when USART is sending/receiving.

zexx86
Associate II
Posted on March 27, 2014 at 18:29

Our code is based on ST's stdlibrary.

There is also SysTick_Handler(void) defined in our code. But it is used to count time and to do some basic stuff, not tied with HW.

So only 3 interrupts are triggered. USART1 (nearly not used), USART2 used as receiver only, but all the time, SysTick used all the time.

Core clock is 72Mhz.

PWM output only use TIM_SetCompare to change PWM ratio. Frequency should be constant, but is'nt as I described.

When I disable SysTick interrupts, it works correctly. Unfortunately I need it.

chen
Associate II
Posted on March 27, 2014 at 18:42

Hi

Are you saying you have 2 semi-related faults :

1) when receiving over UASRT2 - PWM is disrupted

2) when enabling SysTick - PWM is disrupted

OR is it just the SysTick?

Can you show the SysTick code?

zexx86
Associate II
Posted on March 27, 2014 at 19:01

Thank you

It seems as SysTick is causing all these issues.

We do not implemented SysTick stuff, it is implemented by ST in stdlib.

Only Handler is defined in our code.

So combination USART2 interrupts + TIM2CH4 PWM output + SysTick with fast ticks = fail

http://pastebin.com/t74awcBZ

I forgot to say, we are using I2C 2 for gyro communication! But no interrupts are here.

Posted on March 28, 2014 at 03:51

Does process_byte() or log_event() do anything stupid that will snarl up the interrupts, or block in any way?

Here's a thought, post something that is complete, and simpler that demonstrates the failure mechanism.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
zexx86
Associate II
Posted on March 28, 2014 at 11:02

Thank you very much

Well, It seems I was wrong all the time. My observation was from year ago.

But now I did some new tests and checked there is no problem at all with TIM2CH4, but TIM1CH1!

I am very sorry, it was due two hw versions..

Whats more, it seems USART2 and SysTick tick rate does not affect PWM output at all.

Problem is only TIM1CH1 alone.

My complete code for faultly PWM output with TIM1:

http://pastebin.com/1WnRKUze

So again, it does not matter if USART2 is enabled and/or there are interrupts from it.

I disabled I2C2 too, does not helped. Disabled SysTick with:

SysTick->CTRL = 0x04;

SysTick->VAL = 0;

does not helped.

So area is now quite small. Unfortunately I need to use pin PA8, which corresponds to TIM1CH1

zexx86
Associate II
Posted on March 28, 2014 at 11:23

UPDATE: I checked there is probably problem with PWM frequency settings only!

When I changed PWM rate 2x, output frequency is 2 times faster too.

So configured frequency is maybe 10x slower in real.

Probably some faultly bit operation?

I found TIM1 and TIM8 are little bit special, but I can't find anything wrong (yet).

I am sure my code is correct, at least for TIM2, TIM3 and TIM4, because it just works.