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

BREAKING - analogWriteRange 8-bit default #7456

Merged
merged 8 commits into from
Jul 29, 2020
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
3 changes: 1 addition & 2 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ extern "C" {
#define HIGH 0x1
#define LOW 0x0

#define PWMRANGE 1023

//GPIO FUNCTIONS
#define INPUT 0x00
#define INPUT_PULLUP 0x02
Expand Down Expand Up @@ -176,6 +174,7 @@ int analogRead(uint8_t pin);
void analogReference(uint8_t mode);
void analogWrite(uint8_t pin, int val);
void analogWriteFreq(uint32_t freq);
void analogWriteResolution(int res);
void analogWriteRange(uint32_t range);

unsigned long millis(void);
Expand Down
15 changes: 13 additions & 2 deletions cores/esp8266/core_esp8266_wiring_pwm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@
extern "C" {

static uint32_t analogMap = 0;
static int32_t analogScale = PWMRANGE;
static int32_t analogScale = 255; // Match upstream default, breaking change from 2.x.x
static uint16_t analogFreq = 1000;

extern void __analogWriteRange(uint32_t range) {
if (range > 0) {
if ((range >= 15) && (range <= 65535)) {
analogScale = range;
}
}

extern void __analogWriteResolution(int res) {
if ((res >= 4) && (res <= 16)) {
analogScale = (1 << res) - 1;
}
}

extern void __analogWriteFreq(uint32_t freq) {
if (freq < 100) {
analogFreq = 100;
Expand All @@ -57,6 +63,10 @@ extern void __analogWrite(uint8_t pin, int val) {
val = analogScale;
}

// Per the Arduino docs at https://www.arduino.cc/reference/en/language/functions/analog-io/analogwrite/
// val: the duty cycle: between 0 (always off) and 255 (always on).
// So if val = 0 we have digitalWrite(LOW), if we have val==range we have digitalWrite(HIGH)

analogMap &= ~(1 << pin);
uint32_t high = (analogPeriod * val) / analogScale;
uint32_t low = analogPeriod - high;
Expand All @@ -75,5 +85,6 @@ extern void __analogWrite(uint8_t pin, int val) {
extern void analogWrite(uint8_t pin, int val) __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")));

};
18 changes: 14 additions & 4 deletions doc/reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,19 @@ Analog output

``analogWrite(pin, value)`` enables software PWM on the given pin. PWM
may be used on pins 0 to 16. Call ``analogWrite(pin, 0)`` to disable PWM
on the pin. ``value`` may be in range from 0 to ``PWMRANGE``, which is
equal to 1023 by default. PWM range may be changed by calling
``analogWriteRange(new_range)``.
on the pin.

``value`` may be in range from 0 to 255 (which is the Arduino default).
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.

**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
rely on the prior 1023 value may add a call to ``analogWriteRange(1023)``
to their ``setup()`` routine to retrurn to their old behavior. Applications
which already were calling ``analogWriteRange`` need no change.

PWM frequency is 1kHz by default. Call
``analogWriteFreq(new_frequency)`` to change the frequency. Valid values
Expand All @@ -113,7 +123,7 @@ are from 100Hz up to 40000Hz.
The ESP doesn't have hardware PWM, so the implementation is by software.
With one PWM output at 40KHz, the CPU is already rather loaded. The more
PWM outputs used, and the higher their frequency, the closer you get to
the CPU limits, and the less CPU cycles are available for sketch execution.
the CPU limits, and the fewer CPU cycles are available for sketch execution.

Timing and delays
-----------------
Expand Down