micropython / micropython-esp32

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

Second call to PWM constructor makes bad duty #208

Closed goatchurchprime closed 6 years ago

goatchurchprime commented 6 years ago

I hope I don't get a reputation for finding these obscure bugs, but I've reduced this down to the simplest example. (And it doesn't manifest in the ESP8266).

It seems to get into a state where the duty number is invalid, and then issues system errors when you try to reset it.

>>> import machine
>>> p = machine.PWM(machine.Pin(2))
>>> p
PWM(2, freq=5000, duty=512)
>>> p = machine.PWM(machine.Pin(2))
>>> p
PWM(2, freq=5000, duty=16992)
>>> p.duty(40)
E (170024) ledc: ledc_set_duty(339): LEDC channel error
E (170024) ledc: ledc_update_duty(289): LEDC channel error
>>>

On another note, should the duty parameter just be doing [int_value]&0x03FF ? It seems a bit confusing when it would be more intuitive if it (a) threw an exception if it's out of range, and (b) not throw an exception when I give it a float value.

MrSurly commented 6 years ago

I hope I don't get a reputation for finding these obscure bugs, but I've reduced this down to the simplest example.

That's a good reputation to have, IMHO.

dpgeorge commented 6 years ago

Thanks for the report, should be fixed by b5385de6b25a2a79f9f69e0e1ef52c538a9b8880