Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow to set pin to OUTPUT_OPEN_DRAIN in analogWriteMode #7841

Merged
merged 5 commits into from
Jan 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ int digitalRead(uint8_t pin);
int analogRead(uint8_t pin);
void analogReference(uint8_t mode);
void analogWrite(uint8_t pin, int val);
void analogWriteMode(uint8_t pin, int val, bool openDrain);
void analogWriteFreq(uint32_t freq);
void analogWriteResolution(int res);
void analogWriteRange(uint32_t range);
Expand Down
11 changes: 10 additions & 1 deletion cores/esp8266/core_esp8266_wiring_pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ extern void __analogWriteFreq(uint32_t freq) {
}

extern void __analogWrite(uint8_t pin, int val) {
analogWriteMode(pin, val, false);
}

extern void __analogWriteMode(uint8_t pin, int val, bool openDrain) {
if (pin > 16) {
return;
}
Expand All @@ -62,7 +66,11 @@ extern void __analogWrite(uint8_t pin, int val) {
analogMap &= ~(1 << pin);
}
else {
pinMode(pin, OUTPUT);
if(openDrain) {
pinMode(pin, OUTPUT_OPEN_DRAIN);
} else {
pinMode(pin, OUTPUT);
}
}
uint32_t high = (analogPeriod * val) / analogScale;
uint32_t low = analogPeriod - high;
Expand All @@ -88,6 +96,7 @@ extern void __analogWriteResolution(int res) {
}

extern void analogWrite(uint8_t pin, int val) __attribute__((weak, alias("__analogWrite")));
extern void analogWriteMode(uint8_t pin, int val, bool openDrain) __attribute__((weak, alias("__analogWriteMode")));
extern void analogWriteFreq(uint32_t freq) __attribute__((weak, alias("__analogWriteFreq")));
extern void analogWriteRange(uint32_t range) __attribute__((weak, alias("__analogWriteRange")));
extern void analogWriteResolution(int res) __attribute__((weak, alias("__analogWriteResolution")));
Expand Down
3 changes: 3 additions & 0 deletions doc/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ PWM range may be changed by calling ``analogWriteRange(new_range)`` or
``analogWriteResolution(bits)``. ``new_range`` may be from 15...65535
or ``bits`` may be from 4...16.

The function ``analogWriteMode(pin, value, openDrain)`` allows to sets
the pin mode to ``OUTPUT_OPEN_DRAIN`` instead of ``OUTPUT``.

**NOTE:** The default ``analogWrite`` range was 1023 in releases before
3.0, but this lead to incompatibility with external libraries which
depended on the Arduino core default of 256. Existing applications which
Expand Down