← index #5696PR #6809
Off-topic · high · value 1.330
QUERY · ISSUE

esp8266: open drain PWM?

openby osresearchopened 2020-02-25updated 2020-03-03
port-esp8266

When driving a high-side switching transistor with an external pull-up resistor the Pin.OPEN_DRAIN output works well (with negative logic, so grounding the output turns it on and letting it float turns it off). The PWM module however has a comment that it explicitly disables the open drain, which prevents the transistor from being able to fully turn off: https://github.com/micropython/micropython/blob/master/ports/esp8266/esppwm.c#L387

Is there a reason to do this rather than leaving the pin in the mode configured by the python code?

CANDIDATE · PULL REQUEST

esp32: Bound PWM duty value.

closedby jejopened 2021-01-28updated 2021-02-26
port-esp32

I had a potential out-of-boundary bug on an esp8266 device when setting the PWM duty value to control fan speed (negative value or >1023). On esp8266 the machine.PWM bounds the duty value:
https://github.com/micropython/micropython/blob/203e1d2a65273db3f6ff063ba1124a89c3482c0f/ports/esp8266/esppwm.c#L230-L236
...so I didn't notice it. The PWM behaviour was normal, just bounded to 0 and 1023.

When I switched my code to an esp32, the fan behaviour was really strange, because the PWM duty value is only binary ANDed:
https://github.com/micropython/micropython/blob/4eaebc1988699db6ebfd35fbe56a3e8d4cd0b373/ports/esp32/machine_pwm.c#L270

Negative values may be higher and bigger values lower. Combined with issue https://github.com/micropython/micropython/issues/5737 I thought first about a hardware problem...

This PR solves the problem and makes the esp32 PWM to have the same behaviour as esp8266/stm32 (AFAIK), by bounding the duty value between 0 and max PWM resolution. My previous buggy code works again :)

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied