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

Re-implement PWM generator logic #7231

Merged
merged 68 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
2c010d6
Re-implement PWM generator logic
earlephilhower Apr 19, 2020
122ca23
Merge branch 'master' into realpwm
earlephilhower Apr 19, 2020
118103b
Adjust running PWM when analogWriteFreq changed
earlephilhower Apr 19, 2020
5cd145e
Also preserve phase of running tone/waveforms
earlephilhower Apr 19, 2020
4ce623d
Clean up signed/unsigned mismatch, 160MHz operat'n
earlephilhower Apr 19, 2020
3a6f6c1
Turn off PWM on a Tone or digitalWrite
earlephilhower Apr 20, 2020
27ee6f8
Remove hump due to fixed IRQ delta
earlephilhower Apr 21, 2020
5faf6be
Speed PWM generator by reordering data struct
earlephilhower Apr 22, 2020
94af195
Remove if() that could never evaluate TRUE
earlephilhower Apr 24, 2020
1182cd0
Add error feedback to waveform generation
earlephilhower Apr 27, 2020
7ee7d19
Move _stopPWM and _removePWMEntry to IRAM
earlephilhower Apr 27, 2020
c12e961
Avoid long wait times when PWM freq is low
earlephilhower Apr 27, 2020
143b6ae
Merge branch 'master' into realpwm
earlephilhower Apr 28, 2020
a783621
Fix bug where tone/pwm could happen on same pin
earlephilhower Apr 28, 2020
ad3076c
Adjust for random 160MHZ operation
earlephilhower Apr 28, 2020
dc32961
Clean up leftover debugs in ISR
earlephilhower Apr 28, 2020
ec62ee3
Subtract constant-time overhead for PWM, add 60khz
earlephilhower Apr 28, 2020
a41890b
Fix GPIO16 not toggling properly.
earlephilhower Apr 28, 2020
42bbede
Remove constant offset to PWM period
earlephilhower Apr 29, 2020
79a7f7d
Remove volatiles, replace with explicit membarrier
earlephilhower Apr 29, 2020
cbff7a3
Consolidate data into single structure
earlephilhower Apr 29, 2020
4196bf8
Factor out common timer shutdown code
earlephilhower Apr 29, 2020
14f4416
Remove unneeded extra copy on PWM start
earlephilhower Apr 29, 2020
2002a5d
Factor out common edge work in waveform loop
earlephilhower Apr 29, 2020
5b1f288
Factor out waveform phase feedback loop math
earlephilhower Apr 29, 2020
f42696c
Reduce PWM size by using 32b count, indexes
earlephilhower Apr 29, 2020
2b0dde4
GP16O is a 1-bit register, just write to it
earlephilhower Apr 29, 2020
b0e818f
Merge branch 'master' into realpwm
earlephilhower Apr 29, 2020
dfaa9ce
Increase PWM linearity in low/high regions
earlephilhower May 3, 2020
3909ada
Remove redundant GetCycleCount (non-IRQ)
earlephilhower May 3, 2020
413cd17
Factor out common timer setup operations
earlephilhower May 3, 2020
07f5ff1
Fix clean-waveform transition, lock to tone faster
earlephilhower May 3, 2020
539d0d4
Reduce code size ~145 bytes
earlephilhower May 4, 2020
df51b21
Reduce IRAM by pushing more work to _setPWM
earlephilhower May 4, 2020
867b181
Fix typo in PWM pin 1->0 transition
earlephilhower May 5, 2020
f757778
Combine cleanup and pin remove, save 50 bytes IROM
earlephilhower May 5, 2020
9424090
Remove unused analogMap, toneMap
earlephilhower May 5, 2020
c8b53ef
Save IRAM/heap by adjusting WVF update struct
earlephilhower May 5, 2020
e6b7aa1
Don't duplicate PWM period calculation
earlephilhower May 5, 2020
28645ff
Factor out common PWM update code
earlephilhower May 5, 2020
3ee638c
Merge branch 'master' into realpwm
earlephilhower May 5, 2020
174d19e
Clean up old comments
earlephilhower May 5, 2020
a910eae
Fix indent, remove some unneeded if-else branches
earlephilhower May 5, 2020
179b9d6
Fix regression when analogWrite done cold
earlephilhower May 5, 2020
e44171f
Save 16b of IRAM by not re-setting edge intr bit
earlephilhower May 6, 2020
7fe9a2d
Allow on-the-fly PWM frequency changes
earlephilhower May 7, 2020
e7cb533
Adjust for fixed overhead on PWM period
earlephilhower May 8, 2020
083560d
Fix value reversal when analogWrite out of range
earlephilhower May 8, 2020
e421d81
Merge branch 'master' into realpwm
earlephilhower May 9, 2020
5be4961
Don't optimize the satopWaveform call
earlephilhower May 10, 2020
051008a
Avoid side effects in addPWMtoList
earlephilhower May 13, 2020
6692418
Adjust PWM period as fcn of # of PWM pins
earlephilhower May 18, 2020
606c5cd
Merge branch 'master' into realpwm
earlephilhower Jun 4, 2020
524f047
Fix occasional Tone artifacts
earlephilhower Jun 6, 2020
361d4a2
Reduce CPU usage and enhance low range PWM output
earlephilhower Jun 6, 2020
975fe12
Merge branch 'master' into realpwm
earlephilhower Jun 7, 2020
9e48706
Update min IRQ time to remove humps in PWM linearity
earlephilhower Jun 7, 2020
565f21f
Remove minor bump at high PWM frequencies
earlephilhower Jun 7, 2020
272dc9d
Undo the 160->80 frequency adjust
earlephilhower Jun 8, 2020
8f9af5d
Merge branch 'master' into realpwm
earlephilhower Jul 13, 2020
2961933
Merge branch 'master' into realpwm
earlephilhower Aug 23, 2020
e5afab0
Merge branch 'master' into realpwm
earlephilhower Aug 29, 2020
e5ba217
Update core_esp8266_wiring_pwm.cpp
earlephilhower Aug 29, 2020
f911754
Update core_esp8266_wiring_pwm.cpp
earlephilhower Aug 29, 2020
1b34278
Merge branch 'master' into realpwm
d-a-v Oct 26, 2020
55e8abb
Merge branch 'master' of https://github.com/esp8266/Arduino into realpwm
earlephilhower Nov 20, 2020
4cc3d8a
Fix Servo shutdown changes which caused trouble with Servo::detach()
earlephilhower Nov 20, 2020
a353909
Servo shutdown tweak in PWM path
earlephilhower Nov 20, 2020
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
136 changes: 68 additions & 68 deletions boards.txt

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions cores/esp8266/Tone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ static void _startTone(uint8_t _pin, uint32_t high, uint32_t low, uint32_t durat
return;
}

#ifndef WAVEFORM_LOCKED_PHASE
// Stop any analogWrites (PWM) because they are a different generator
_stopPWM(_pin);
#endif
// If there's another Tone or startWaveform on this pin
// it will be changed on-the-fly (no need to stop it)

pinMode(_pin, OUTPUT);

high = std::max(high, (uint32_t)microsecondsToClockCycles(25)); // new 20KHz maximum tone frequency,
Expand Down
94 changes: 4 additions & 90 deletions cores/esp8266/core_esp8266_waveform.h
Original file line number Diff line number Diff line change
@@ -1,93 +1,7 @@
/*
esp8266_waveform - General purpose waveform generation and control,
supporting outputs on all pins in parallel.

Copyright (c) 2018 Earle F. Philhower, III. All rights reserved.
Copyright (c) 2020 Dirk O. Kaar.

The core idea is to have a programmable waveform generator with a unique
high and low period (defined in microseconds or CPU clock cycles). TIMER1 is
set to 1-shot mode and is always loaded with the time until the next edge
of any live waveforms.

Up to one waveform generator per pin supported.

Each waveform generator is synchronized to the ESP clock cycle counter, not the
timer. This allows for removing interrupt jitter and delay as the counter
always increments once per 80MHz clock. Changes to a waveform are
contiguous and only take effect on the next waveform transition,
allowing for smooth transitions.

This replaces older tone(), analogWrite(), and the Servo classes.

Everywhere in the code where "ccy" or "ccys" is used, it means ESP.getCycleCount()
clock cycle count, or an interval measured in CPU clock cycles, but not TIMER1
cycles (which may be 2 CPU clock cycles @ 160MHz).

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Wrapper to include both versions of the waveform generator

#ifdef WAVEFORM_LOCKED_PHASE

#include <Arduino.h>

#ifndef __ESP8266_WAVEFORM_H
#define __ESP8266_WAVEFORM_H

#ifdef __cplusplus
extern "C" {
#include "core_esp8266_waveform_phase.h"
#else
#include "core_esp8266_waveform_pwm.h"
#endif

// Start or change a waveform of the specified high and low times on specific pin.
// If runtimeUS > 0 then automatically stop it after that many usecs, relative to the next
// full period.
// If waveform is not yet started on pin, and on pin == alignPhase a waveform is running,
// the new waveform is started at phaseOffsetUS phase offset, in microseconds, to that.
// Setting autoPwm to true allows the wave generator to maintain PWM duty to idle cycle ratio
// under load, for applications where frequency or duty cycle must not change, leave false.
// Returns true or false on success or failure.
int startWaveform(uint8_t pin, uint32_t timeHighUS, uint32_t timeLowUS,
uint32_t runTimeUS = 0, int8_t alignPhase = -1, uint32_t phaseOffsetUS = 0, bool autoPwm = false);
// Start or change a waveform of the specified high and low CPU clock cycles on specific pin.
// If runtimeCycles > 0 then automatically stop it after that many CPU clock cycles, relative to the next
// full period.
// If waveform is not yet started on pin, and on pin == alignPhase a waveform is running,
// the new waveform is started at phaseOffsetCcys phase offset, in CPU clock cycles, to that.
// Setting autoPwm to true allows the wave generator to maintain PWM duty to idle cycle ratio
// under load, for applications where frequency or duty cycle must not change, leave false.
// Returns true or false on success or failure.
int startWaveformClockCycles(uint8_t pin, uint32_t timeHighCcys, uint32_t timeLowCcys,
uint32_t runTimeCcys = 0, int8_t alignPhase = -1, uint32_t phaseOffsetCcys = 0, bool autoPwm = false);
// Stop a waveform, if any, on the specified pin.
// Returns true or false on success or failure.
int stopWaveform(uint8_t pin);

// Add a callback function to be called on *EVERY* timer1 trigger. The
// callback returns the number of microseconds until the next desired call.
// However, since it is called every timer1 interrupt, it may be called
// again before this period. It should therefore use the ESP Cycle Counter
// to determine whether or not to perform an operation.
// Pass in NULL to disable the callback and, if no other waveforms being
// generated, stop the timer as well.
// Make sure the CB function has the ICACHE_RAM_ATTR decorator.
void setTimer1Callback(uint32_t (*fn)());

#ifdef __cplusplus
}
#endif

#endif // __ESP8266_WAVEFORM_H

#endif // WAVEFORM_LOCKED_PHASE
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

#ifdef WAVEFORM_LOCKED_PHASE

#include "core_esp8266_waveform.h"
#include "core_esp8266_waveform_phase.h"
#include <Arduino.h>
#include "ets_sys.h"
#include <atomic>
Expand Down
93 changes: 93 additions & 0 deletions cores/esp8266/core_esp8266_waveform_phase.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
esp8266_waveform - General purpose waveform generation and control,
supporting outputs on all pins in parallel.

Copyright (c) 2018 Earle F. Philhower, III. All rights reserved.
Copyright (c) 2020 Dirk O. Kaar.

The core idea is to have a programmable waveform generator with a unique
high and low period (defined in microseconds or CPU clock cycles). TIMER1 is
set to 1-shot mode and is always loaded with the time until the next edge
of any live waveforms.

Up to one waveform generator per pin supported.

Each waveform generator is synchronized to the ESP clock cycle counter, not the
timer. This allows for removing interrupt jitter and delay as the counter
always increments once per 80MHz clock. Changes to a waveform are
contiguous and only take effect on the next waveform transition,
allowing for smooth transitions.

This replaces older tone(), analogWrite(), and the Servo classes.

Everywhere in the code where "ccy" or "ccys" is used, it means ESP.getCycleCount()
clock cycle count, or an interval measured in CPU clock cycles, but not TIMER1
cycles (which may be 2 CPU clock cycles @ 160MHz).

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#ifdef WAVEFORM_LOCKED_PHASE

#include <Arduino.h>

#ifndef __ESP8266_WAVEFORM_H
#define __ESP8266_WAVEFORM_H

#ifdef __cplusplus
extern "C" {
#endif

// Start or change a waveform of the specified high and low times on specific pin.
// If runtimeUS > 0 then automatically stop it after that many usecs, relative to the next
// full period.
// If waveform is not yet started on pin, and on pin == alignPhase a waveform is running,
// the new waveform is started at phaseOffsetUS phase offset, in microseconds, to that.
// Setting autoPwm to true allows the wave generator to maintain PWM duty to idle cycle ratio
// under load, for applications where frequency or duty cycle must not change, leave false.
// Returns true or false on success or failure.
int startWaveform(uint8_t pin, uint32_t timeHighUS, uint32_t timeLowUS,
uint32_t runTimeUS = 0, int8_t alignPhase = -1, uint32_t phaseOffsetUS = 0, bool autoPwm = false);
// Start or change a waveform of the specified high and low CPU clock cycles on specific pin.
// If runtimeCycles > 0 then automatically stop it after that many CPU clock cycles, relative to the next
// full period.
// If waveform is not yet started on pin, and on pin == alignPhase a waveform is running,
// the new waveform is started at phaseOffsetCcys phase offset, in CPU clock cycles, to that.
// Setting autoPwm to true allows the wave generator to maintain PWM duty to idle cycle ratio
// under load, for applications where frequency or duty cycle must not change, leave false.
// Returns true or false on success or failure.
int startWaveformClockCycles(uint8_t pin, uint32_t timeHighCcys, uint32_t timeLowCcys,
uint32_t runTimeCcys = 0, int8_t alignPhase = -1, uint32_t phaseOffsetCcys = 0, bool autoPwm = false);
// Stop a waveform, if any, on the specified pin.
// Returns true or false on success or failure.
int stopWaveform(uint8_t pin);

// Add a callback function to be called on *EVERY* timer1 trigger. The
// callback returns the number of microseconds until the next desired call.
// However, since it is called every timer1 interrupt, it may be called
// again before this period. It should therefore use the ESP Cycle Counter
// to determine whether or not to perform an operation.
// Pass in NULL to disable the callback and, if no other waveforms being
// generated, stop the timer as well.
// Make sure the CB function has the ICACHE_RAM_ATTR decorator.
void setTimer1Callback(uint32_t (*fn)());

#ifdef __cplusplus
}
#endif

#endif // __ESP8266_WAVEFORM_H

#endif // WAVEFORM_LOCKED_PHASE
Loading