ut-ras/Rasware2013 — pull request #14 by geky (2013-08-07 16:21:28+00:00)
Title: Merge `Ordered Cyclic PWM`

Alternative Software PWM Generation. Uses an ordered, cyclic, doubly linked list to effectively create a dynamic finite state machine. This allows for a very small and simple interrupt handler and dynamically adjusted timer interrupt removing a large chunk of cpu usage. In testing I was able to get up to 48 pwm signals running with no noticeable lag before running out of pins. Unfortunately, to by cyclic each signal for a single module needs to share a base frequency; as a fix, pwm module assignment occurs at runtime. It consumes all 12 half wide timers allowing 12 different frequencies. Fortunately we rarely use more than 2 so this shouldn't be a problem. Lastly, due to its dynamic one shot timer usage, this is susceptible to significant drift as the interrupt handler doesn't take into account its own execution time. It could be modified to compensate but I didn't bother.

Pros
1. Much much less cpu consumption.
2. Allows for any number of pwm signals.
3. A frequency has absolute timing, allowing phase to be adjusted whenever.
4. Did I mention less cpu consumption?

Cons
1. Signal is prone to drift on the order of about 0.3 microseconds per interrupt
2. The list must be kept ordered, adding overhead when setting pwm duty cycle.
3. Limited to 12 frequencies.
4. Uses all Wide Timers, although we could limit this.
5. Added complexity could be preventive to raslets looking at code.