I have a problem when running PWM on LPC1778. I want to be able to use the PWM in burst mode, i.e. have a fixed number of pulses which starts PWM output in single mode at exact moment and finishes after the sequence is completed. I'm using match interrupt for counting the pulses and this works ok. Problem is occuring on the initial call. For some reason when I call the routine first time the initial pulse is skipped and I get output of n - 1 pulses with inherent delay.
What I discovered by trial and error is that PWMENA bit of the PCR register does not always set the output correctly and is somehow influenced by the previous state of Match registers.
To be more precise, if duty cycle 0% is used for the sequence and the sequence runs, then the next sequence is set up with 50% duty cycle and called it will run n - 1 pulses. The sequence after that will actually run n pulses. So by setting the 0% < duty cycle < 100% and running it once the register state is somehow stabilized.
Likewise, if I run the sequence at 100% duty cycle and afterwards call the sequence with 50% duty cycle, or any value in beetween 0% and 100% it will not run correctly. This time the first pulse is not skipped, instead it runs at 100% so first two pulses look merged and then the sequence continues as intended. Only on the next call the sequence will run as intended.
One of the things that user manual mentiones is:
When a PWM Match 0 event occurs (normally also resetting the timer in PWM mode), the
contents of shadow registers will be transferred to the actual Match registers if the
corresponding bit in the Latch Enable register has been set. At that point, the new values
will take effect and determine the course of the next PWM cycle.
I made sure that latch registers were set correctly and that match 0 event occurs. I can confirm this by changing the duty cycle (match register 1 in this case) to any value in between 0% and 100%. If the sequnce is called after that it will run correct number of pulses first time. It is only when I change to 0% or 100% duty cycle and then change back to say 30% that the initial sequnce will not correctly.
So, in between each sequence call I update the match registers, latch them and I reset the PWM timer counter and toggle the PWMEN bit in TCR register. I thought this steps would completely reset the PWM with no previous states retained but they don't. I also tried disabling and enablig power to the PWM via PCONP register but with no success.
I'm currently using a workaround which calls one dummy pulse sequence without output just to get the desired behavior, but I would like to be able to do this in a better manner. Is there a way to completely reset the PWM, no matter what the match register values were before that?