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
Prev Previous commit
Next Next commit
Reduce CPU usage and enhance low range PWM output
Borrow a trick from #7022 to exit the busy loop when the next event is
too far out.  Also reduce the IRQ delta subtraction because it was
initially not NMI so there was much more variation than now.

Keep the PWM state machine active at a higher prio than the standard
tone generation when the next edge is very close (i.e. when we're at
the max or min of the range and have 2 or more near edges).  Adds a
lot of resolution to the response at low and high ranges.

Go from relative to absolute cycle counts in the main IRQ loop so that
we don't mingle delta-cycles when the delta start was significantly
different.
  • Loading branch information
earlephilhower committed Jun 6, 2020
commit 361d4a2664093926f6a9b35c1c7bf14144c1f074
89 changes: 46 additions & 43 deletions cores/esp8266/core_esp8266_waveform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,11 +426,12 @@ static inline ICACHE_RAM_ATTR uint32_t GetCycleCountIRQ() {
return ccount;
}

static inline ICACHE_RAM_ATTR uint32_t min_u32(uint32_t a, uint32_t b) {
if (a < b) {
return a;
}
return b;
// Find the earliest cycle as compared to right now
static inline ICACHE_RAM_ATTR uint32_t earliest(uint32_t a, uint32_t b) {
uint32_t now = GetCycleCountIRQ();
int32_t da = a - now;
int32_t db = b - now;
return (da < db) ? a : b;
}

// The SDK and hardware take some time to actually get to our NMI code, so
Expand All @@ -441,10 +442,10 @@ static inline ICACHE_RAM_ATTR uint32_t min_u32(uint32_t a, uint32_t b) {
// so the ESP cycle counter is actually running at a variable speed.
// adjust(x) takes care of adjusting a delta clock cycle amount accordingly.
#if F_CPU == 80000000
#define DELTAIRQ (microsecondsToClockCycles(3))
#define DELTAIRQ (microsecondsToClockCycles(2))
#define adjust(x) ((x) << (turbo ? 1 : 0))
#else
#define DELTAIRQ (microsecondsToClockCycles(2))
#define DELTAIRQ (microsecondsToClockCycles(1))
#define adjust(x) ((x) >> (turbo ? 0 : 1))
#endif

Expand All @@ -453,7 +454,7 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
// Flag if the core is at 160 MHz, for use by adjust()
bool turbo = (*(uint32_t*)0x3FF00014) & 1 ? true : false;

uint32_t nextEventCycles = microsecondsToClockCycles(MAXIRQUS);
uint32_t nextEventCycle = GetCycleCountIRQ() + microsecondsToClockCycles(MAXIRQUS);
uint32_t timeoutCycle = GetCycleCountIRQ() + microsecondsToClockCycles(14);

if (wvfState.waveformToEnable || wvfState.waveformToDisable) {
Expand All @@ -478,40 +479,43 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
bool done = false;
if (wvfState.waveformEnabled || pwmState.cnt) {
do {
nextEventCycles = microsecondsToClockCycles(MAXIRQUS);
nextEventCycle = GetCycleCountIRQ() + microsecondsToClockCycles(MAXIRQUS);

// PWM state machine implementation
if (pwmState.cnt) {
int32_t cyclesToGo = pwmState.nextServiceCycle - GetCycleCountIRQ();
if (cyclesToGo < 0) {
if (pwmState.idx == pwmState.cnt) { // Start of pulses, possibly copy new
if (pwmState.pwmUpdate) {
// Do the memory copy from temp to global and clear mailbox
pwmState = *(PWMState*)pwmState.pwmUpdate;
}
GPOS = pwmState.mask; // Set all active pins high
if (pwmState.mask & (1<<16)) {
GP16O = 1;
}
pwmState.idx = 0;
} else {
do {
// Drop the pin at this edge
if (pwmState.mask & (1<<pwmState.pin[pwmState.idx])) {
GPOC = 1<<pwmState.pin[pwmState.idx];
if (pwmState.pin[pwmState.idx] == 16) {
GP16O = 0;
int32_t cyclesToGo;
do {
cyclesToGo = pwmState.nextServiceCycle - GetCycleCountIRQ();
if (cyclesToGo < 0) {
if (pwmState.idx == pwmState.cnt) { // Start of pulses, possibly copy new
if (pwmState.pwmUpdate) {
// Do the memory copy from temp to global and clear mailbox
pwmState = *(PWMState*)pwmState.pwmUpdate;
}
GPOS = pwmState.mask; // Set all active pins high
if (pwmState.mask & (1<<16)) {
GP16O = 1;
}
pwmState.idx = 0;
} else {
do {
// Drop the pin at this edge
if (pwmState.mask & (1<<pwmState.pin[pwmState.idx])) {
GPOC = 1<<pwmState.pin[pwmState.idx];
if (pwmState.pin[pwmState.idx] == 16) {
GP16O = 0;
}
}
pwmState.idx++;
// Any other pins at this same PWM value will have delta==0, drop them too.
} while (pwmState.delta[pwmState.idx] == 0);
}
pwmState.idx++;
// Any other pins at this same PWM value will have delta==0, drop them too.
} while (pwmState.delta[pwmState.idx] == 0);
// Preserve duty cycle over PWM period by using now+xxx instead of += delta
cyclesToGo = adjust(pwmState.delta[pwmState.idx]);
pwmState.nextServiceCycle = GetCycleCountIRQ() + cyclesToGo;
}
// Preserve duty cycle over PWM period by using now+xxx instead of += delta
cyclesToGo = adjust(pwmState.delta[pwmState.idx]);
pwmState.nextServiceCycle = GetCycleCountIRQ() + cyclesToGo;
}
nextEventCycles = min_u32(nextEventCycles, cyclesToGo);
nextEventCycle = earliest(nextEventCycle, pwmState.nextServiceCycle);
} while (pwmState.cnt && (cyclesToGo < 100));
}

for (auto i = wvfState.startPin; i <= wvfState.endPin; i++) {
Expand Down Expand Up @@ -585,26 +589,25 @@ static ICACHE_RAM_ATTR void timer1Interrupt() {
}
nextEdgeCycles = adjust(nextEdgeCycles);
wave->nextServiceCycle = now + nextEdgeCycles;
nextEventCycles = min_u32(nextEventCycles, nextEdgeCycles);
wave->lastEdge = now;
} else {
uint32_t deltaCycles = wave->nextServiceCycle - now;
nextEventCycles = min_u32(nextEventCycles, deltaCycles);
}
nextEventCycle = earliest(nextEventCycle, wave->nextServiceCycle);
}

// Exit the loop if we've hit the fixed runtime limit or the next event is known to be after that timeout would occur
uint32_t now = GetCycleCountIRQ();
int32_t cycleDeltaNextEvent = timeoutCycle - (now + nextEventCycles);
int32_t cycleDeltaNextEvent = nextEventCycle - now;
int32_t cyclesLeftTimeout = timeoutCycle - now;
done = (cycleDeltaNextEvent < 0) || (cyclesLeftTimeout < 0);
done = (cycleDeltaNextEvent > microsecondsToClockCycles(4)) || (cyclesLeftTimeout < 0);
} while (!done);
} // if (wvfState.waveformEnabled)

if (wvfState.timer1CB) {
nextEventCycles = min_u32(nextEventCycles, wvfState.timer1CB());
nextEventCycle = earliest(nextEventCycle, GetCycleCountIRQ() + wvfState.timer1CB());
}

int32_t nextEventCycles = nextEventCycle - GetCycleCountIRQ();

if (nextEventCycles < microsecondsToClockCycles(5)) {
nextEventCycles = microsecondsToClockCycles(5);
}
Expand Down
0