micropython/micropython — pull request #809 by dpgeorge (2014-08-18 22:07:14+00:00)
Title: stmhal: Initial PWM support, timer centric.

I'm not ready to merge this. I wanted to put it up for review to get ideas for how to do PWM.

PWM does not fit the "I have a PWM object" point of view. For PWM you need 2 entities: a timer (to generate the clock) and a pin (to output the PWM). On the timer's side of things you need to set:

- frequency, prescaler, period
- PWM polarity
- which channel it's connected to

On the pin side you configure:

- push-pull or open drain
- pull up/down/none
- which timer it's driven by

You can have multiple pins driven by the same timer (but they would be on different channels). A given pin can have the option to be driven by different timers.

There are 3 ways of implementing PWM configuration:

1. Timer centric (as implemented in this PR). Here, you ask the timer to output PWM, and pass it the pin you want to do PWM on. It then configures the pin.

2. Pin centric. You would ask a pin to do PWM, passing a timer in.

3. A special PWM function which takes a timer and a pin, and connects them together for PWM.

Thoughts/comments/ideas?