Skip to content
This repository was archived by the owner on Sep 6, 2023. It is now read-only.

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

Closed
wants to merge 1 commit into from

Conversation

dragomirecky
Copy link

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:

  • PWM initializer takes two new optional keyword arguments:
    pwm.init(..., timer=0, speed_mode=PWM.HIGH_SPEED_MODE)
  • PWM class defines two new constants:
    PWM.HIGH_SPEED_MODE and PWM.LOW_SPEED_MODE

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.

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:
- PWM initializer takes two new optional keyword arguments:
    `pwm.init(..., timer=0, speed_mode=PWM.HIGH_SPEED_MODE)`
- PWM class defines two new constants:
    `PWM.HIGH_SPEED_MODE` and `PWM.LOW_SPEED_MODE`
@nickzoic
Copy link
Collaborator

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
Copy link
Author

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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants