micropython / micropython-esp32

Old port of MicroPython to the ESP32 -- new port is at https://github.com/micropython/micropython
MIT License
680 stars 219 forks source link

esp32/machine_pwm: Add support for all PWM timers and modes. #245

Closed dragomirecky closed 6 years ago

dragomirecky commented 6 years ago

ESP32 has 16 PWM channels with 8 separate timers, but current PWM implementation forces user to share single timer between all channels. This commit allows to use all 8 timers and does not break backward compatibility.

API additions:

Example:

from machine import Pin, PWM

pwm1 = PWM(Pin(22), freq=440)
pwm2 = PWM(Pin(23), freq=880)
# old (unchanged) behaviour: pwm1 and pwm2 both share frequency 880hz as they
#   share the same high speed timer #0

pwm3 = PWM(Pin(2), freq=440, timer=0, speed_mode=PWM.LOW_SPEED_MODE)
pwm4 = PWM(Pin(15), freq=880, timer=1, speed_mode=PWM.LOW_SPEED_MODE)
# pwm3 has frequency 440hz and pwm4 880hz running on different timers

There are 4 low speed timers and 4 high speed timers on ESP32.

nickzoic commented 6 years ago

G'day!

Thanks, having access to all PWM channels sounds like a good addition. I'll have a closer look as soon as possible.

However, we've moved our development across to the main 'micropython/micropython' repo, so if possible could you please rebase onto master and submit a PR over there?

-----Nick

dragomirecky commented 6 years ago

Hi Nick, sorry, I missed that the ESP32 port has moved already. I created new pull request there.