8000 Re-implement PWM generator logic by earlephilhower · Pull Request #7231 · esp8266/Arduino · GitHub
[go: up one dir, main page]

Skip to content

Re-implement PWM generator logic #7231

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

Merged
merged 68 commits into from
Nov 20, 2020
Merged
Changes from 1 commit
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
8000
Prev Previous commit
Next Next commit
Save IRAM/heap by adjusting WVF update struct
The waveform update structure included 2 32-bit quantities (so, used
8 * 17 = 136 bytes of RAM) for the next cycle of a waveform.

Replace that with a single update register, in a posted fashion.  The
logic now sets the new state of a single waveform and returns
immediately (so, no need to wait 1ms if you've got an existing waveform
of 1khz).  The waveform NMI will pick up the changed value on its next
cycle.

Reduces IRAM by 40 bytes, and heap by 144 bytes.
  • Loading branch information
earlephilhower committed May 5, 2020
commit c8b53ef7c285b882a6916f372daf6fe2cc76fc15
56 changes: 27 additions & 29 deletions cores/esp8266/core_esp8266_waveform.cpp
F6E3
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,11 @@ extern "C" {
typedef struct {
uint32_t nextServiceCycle; // ESP cycle timer when a transition required
uint32_t expiryCycle; // For time-limited waveform, the cycle when this waveform must stop
uint32_t timeHighCycles; // Currently running waveform period
uint32_t timeHighCycles; // Actual running waveform period (adjusted using desiredCycles)
uint32_t timeLowCycles; //
uint32_t desiredHighCycles; // Currently running waveform period
uint32_t desiredLowCycles; //
uint32_t gotoTimeHighCycles; // Copied over on the next period to preserve phase
uint32_t gotoTimeLowCycles; //
uint32_t lastEdge; //
uint32_t desiredHighCycles; // Ideal waveform period to drive the error signal
uint32_t desiredLowCycles; //
8000 uint32_t lastEdge; // Cycle when this generator last changed
} Waveform;

class WVFState {
Expand All @@ -74,7 +72,7 @@ class WVFState {
uint32_t waveformToEnable = 0; // Message to the NMI handler to start a waveform on a inactive pin
uint32_t waveformToDisable = 0; // Message to the NMI handler to disable a pin from waveform generation

int32_t waveformToChange = -1;
uint32_t waveformToChange = 0; // Mask of pin to change. One bit set in main app, cleared when effected in the NMI
uint32_t waveformNewHigh = 0;
uint32_t waveformNewLow = 0;

Expand All @@ -83,8 +81,8 @@ class WVFState {
// Optimize the NMI inner loop by keeping track of the min and max GPIO that we
// are generating. In the common case (1 PWM) these may be the same pin and
// we can avoid looking at the other pins.
int startPin = 0;
int endPin = 0;
uint16_t startPin = 0;
uint16_t endPin = 0;
};
static WVFState wvfState;

Expand Down Expand Up @@ -318,22 +316,22 @@ int startWaveformClockCycles(uint8_t pin, uint32_t timeHighCycles, uint32_t time
uint32_t mask = 1<<pin;
MEMBARRIER();
if (wvfState.waveformEnabled & mask) {
wvfState.waveformNewHigh = timeHighCycles;
wvfState.waveformNewLow = timeLowCycles;
MEMBARRIER();
wvfState.waveformToChange = pin;
while (wvfState.waveformToChange >= 0) {
// Make sure no waveform changes are waiting to be applied
while (wvfState.waveformToChange) {
delay(0); // Wait for waveform to update
// No mem barrier here, the call to a global function implies global state updated
}
wvfState.waveformNewHigh = timeHighCycles;
wvfState.waveformNewLow = timeLowCycles;
MEMBARRIER();
wvfState.waveformToChange = mask;
// The waveform will be updated some time in the future on the next period for the signal
} else { // if (!(wvfState.waveformEnabled & mask)) {
wave->timeHighCycles = timeHighCycles;
wave->timeLowCycles = timeLowCycles;
wave->desiredHighCycles = wave->timeHighCycles;
wave->desiredLowCycles = wave->timeLowCycles;
wave->lastEdge = 0;
wave->gotoTimeHighCycles = wave->timeHighCycles;
wave->gotoTimeLowCycles = wave->timeLowCycles; // Actually set the pin high or low in the IRQ service to guarantee times
wave->nextServiceCycle = ESP.getCycleCount() + microsecondsToClockCycles(1);
wvfState.waveformToEnable |= mask;
MEMBARRIER();
Expand Down Expand Up @@ -387,8 +385,13 @@ int ICACHE_RAM_ATTR stopWaveform(uint8_t pin) {
}
// If user sends in a pin >16 but <32, this will always point to a 0 bit
// If they send >=32, then the shift will result in 0 and it will also return false
if (wvfState.waveformEnabled & (1UL << pin)) {
wvfState.waveformToDisable = 1UL << pin;
uint32_t mask = 1<<pin;
if (wvfState.waveformEnabled & mask) {
wvfState.waveformToDisable = mask;
// Cancel any pending updates for this waveform, too.
if (wvfState.waveformToChange & mask) {
wvfState.waveformToChange = 0;
}
forceTimerInterrupt();
while (wvfState.waveformToDisable) {
MEMBARRIER(); // If it wasn't written yet, it has to be by now
Expand Down Expand Up @@ -450,11 +453,6 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
pwmState.nextServiceCycle = GetCycleCountIRQ(); // Do it this loop!
// No need for mem barrier here. Global must be written by IRQ exit
#endif
} else if (wvfState.waveformToChange >= 0) {
wvfState.waveform[wvfState.waveformToChange].gotoTimeHighCycles = wvfState.waveformNewHigh;
wvfState.waveform[wvfState.waveformToChange].gotoTimeLowCycles = wvfState.waveformNewLow;
wvfState.waveformToChange = -1;
// No need for memory barrier here. The global has to be written before exit the ISR.
}

bool done = false;
Expand Down Expand Up @@ -541,13 +539,13 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
} else {
SetGPIO(mask);
}
if (wave->gotoTimeHighCycles) {
if (wvfState.waveformToChange & mask) {
// Copy over next full-cycle timings
wave->timeHighCycles = wave->gotoTimeHighCycles;
wave->desiredHighCycles = wave->gotoTimeHighCycles;
wave->timeLowCycles = wave->gotoTimeLowCycles;
wave->desiredLowCycles = wave->gotoTimeLowCycles;
wave->gotoTimeHighCycles = 0;
wave->timeHighCycles = wvfState.waveformNewHigh;
wave->desiredHighCycles = wvfState.waveformNewHigh;
wave->timeLowCycles = wvfState.waveformNewLow;
wave->desiredLowCycles = wvfState.waveformNewLow;
wvfState.waveformToChange = 0;
} else {
#ifdef ENABLE_FEEDBACK
if (wave->lastEdge) {
Expand Down
0