From 144ac90967f2782e2b4c971456a899fe20c66113 Mon Sep 17 00:00:00 2001 From: Michael Kluge Date: Mon, 25 Jan 2021 20:19:51 +0100 Subject: [PATCH 1/4] allow to set pin to OUTPUT_OPEN_DRAIN in analogWrite --- cores/esp8266/Arduino.h | 2 +- cores/esp8266/core_esp8266_wiring_pwm.cpp | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index b484a3765a..d7ae4354d7 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -170,7 +170,7 @@ void digitalWrite(uint8_t pin, uint8_t val); int digitalRead(uint8_t pin); int analogRead(uint8_t pin); void analogReference(uint8_t mode); -void analogWrite(uint8_t pin, int val); +void analogWrite(uint8_t pin, int val, bool openDrain = false); void analogWriteFreq(uint32_t freq); void analogWriteResolution(int res); void analogWriteRange(uint32_t range); diff --git a/cores/esp8266/core_esp8266_wiring_pwm.cpp b/cores/esp8266/core_esp8266_wiring_pwm.cpp index f730f649ef..42ce106f18 100644 --- a/cores/esp8266/core_esp8266_wiring_pwm.cpp +++ b/cores/esp8266/core_esp8266_wiring_pwm.cpp @@ -43,7 +43,7 @@ extern void __analogWriteFreq(uint32_t freq) { _setPWMFreq(freq); } -extern void __analogWrite(uint8_t pin, int val) { +extern void __analogWrite(uint8_t pin, int val, bool openDrain) { if (pin > 16) { return; } @@ -62,7 +62,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; @@ -87,7 +91,7 @@ extern void __analogWriteResolution(int res) { } } -extern void analogWrite(uint8_t pin, int val) __attribute__((weak, alias("__analogWrite"))); +extern void analogWrite(uint8_t pin, int val, bool openDrain) __attribute__((weak, alias("__analogWrite"))); 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"))); From 244c2bb140b0e5f18fedf263a2520305b4da7cae Mon Sep 17 00:00:00 2001 From: Michael Kluge Date: Mon, 25 Jan 2021 20:40:08 +0100 Subject: [PATCH 2/4] remove parameter with default value --- cores/esp8266/Arduino.h | 3 ++- cores/esp8266/core_esp8266_wiring_pwm.cpp | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index d7ae4354d7..2aab5770c9 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -170,7 +170,8 @@ void digitalWrite(uint8_t pin, uint8_t val); int digitalRead(uint8_t pin); int analogRead(uint8_t pin); void analogReference(uint8_t mode); -void analogWrite(uint8_t pin, int val, bool openDrain = false); +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); diff --git a/cores/esp8266/core_esp8266_wiring_pwm.cpp b/cores/esp8266/core_esp8266_wiring_pwm.cpp index 42ce106f18..28f5deda74 100644 --- a/cores/esp8266/core_esp8266_wiring_pwm.cpp +++ b/cores/esp8266/core_esp8266_wiring_pwm.cpp @@ -43,7 +43,11 @@ extern void __analogWriteFreq(uint32_t freq) { _setPWMFreq(freq); } -extern void __analogWrite(uint8_t pin, int val, bool openDrain) { +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; } @@ -91,7 +95,8 @@ extern void __analogWriteResolution(int res) { } } -extern void analogWrite(uint8_t pin, int val, bool openDrain) __attribute__((weak, alias("__analogWrite"))); +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"))); From 4db7a30a73d5a8ea89bc9b58ff65c5abbf905ce1 Mon Sep 17 00:00:00 2001 From: Michael Kluge Date: Mon, 25 Jan 2021 20:44:15 +0100 Subject: [PATCH 3/4] Update core_esp8266_wiring_pwm.cpp --- cores/esp8266/core_esp8266_wiring_pwm.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cores/esp8266/core_esp8266_wiring_pwm.cpp b/cores/esp8266/core_esp8266_wiring_pwm.cpp index 28f5deda74..5bd5416d70 100644 --- a/cores/esp8266/core_esp8266_wiring_pwm.cpp +++ b/cores/esp8266/core_esp8266_wiring_pwm.cpp @@ -44,7 +44,7 @@ extern void __analogWriteFreq(uint32_t freq) { } extern void __analogWrite(uint8_t pin, int val) { - __analogWriteMode(pin, val, false); + analogWriteMode(pin, val, false); } extern void __analogWriteMode(uint8_t pin, int val, bool openDrain) { From d6e6c2ff463d3a3fd356600769dd5380ecfd81e8 Mon Sep 17 00:00:00 2001 From: Michael Kluge Date: Tue, 26 Jan 2021 19:27:36 +0100 Subject: [PATCH 4/4] update documentation accordingly --- doc/reference.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/reference.rst b/doc/reference.rst index 2ae8ca5cd8..32f5987f85 100644 --- a/doc/reference.rst +++ b/doc/reference.rst @@ -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