-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Preserve phase relationship when waveform updated #7192
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
Changes from 4 commits
37d9fce
6450989
e75da87
6fc6b80
393cc85
4b2562a
545fa10
371ef22
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,7 +38,7 @@ | |
*/ | ||
|
||
#include <Arduino.h> | ||
#include "ets_sys.h" | ||
#include "interrupts.h" | ||
#include "core_esp8266_waveform.h" | ||
|
||
extern "C" { | ||
|
@@ -54,8 +54,10 @@ 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 nextTimeHighCycles; // Copy over low->high to keep smooth waveform | ||
uint32_t nextTimeLowCycles; // Copy over high->low to keep smooth waveform | ||
uint32_t timeHighCycles; // Currently running waveform period | ||
uint32_t timeLowCycles; // | ||
uint32_t gotoTimeHighCycles; // Copied over on the next period to preserve phase | ||
uint32_t gotoTimeLowCycles; // | ||
} Waveform; | ||
|
||
static Waveform waveform[17]; // State of all possible pins | ||
|
@@ -112,20 +114,30 @@ void setTimer1Callback(uint32_t (*fn)()) { | |
// waveform smoothly on next low->high transition. For immediate change, stopWaveform() | ||
// first, then it will immediately begin. | ||
int startWaveform(uint8_t pin, uint32_t timeHighUS, uint32_t timeLowUS, uint32_t runTimeUS) { | ||
return startWaveformCycles(pin, microsecondsToClockCycles(timeHighUS), microsecondsToClockCycles(timeLowUS), microsecondsToClockCycles(runTimeUS)); | ||
} | ||
|
||
int startWaveformCycles(uint8_t pin, uint32_t timeHighCycles, uint32_t timeLowCycles, uint32_t runTimeCycles) { | ||
if ((pin > 16) || isFlashInterfacePin(pin)) { | ||
return false; | ||
} | ||
Waveform *wave = &waveform[pin]; | ||
// Adjust to shave off some of the IRQ time, approximately | ||
wave->nextTimeHighCycles = microsecondsToClockCycles(timeHighUS); | ||
wave->nextTimeLowCycles = microsecondsToClockCycles(timeLowUS); | ||
wave->expiryCycle = runTimeUS ? GetCycleCount() + microsecondsToClockCycles(runTimeUS) : 0; | ||
if (runTimeUS && !wave->expiryCycle) { | ||
|
||
wave->expiryCycle = runTimeCycles ? GetCycleCount() + runTimeCycles : 0; | ||
if (runTimeCycles && !wave->expiryCycle) { | ||
wave->expiryCycle = 1; // expiryCycle==0 means no timeout, so avoid setting it | ||
} | ||
|
||
uint32_t mask = 1<<pin; | ||
if (!(waveformEnabled & mask)) { | ||
if (waveformEnabled & mask) { | ||
esp8266::InterruptLock il; // Make the variable sets atomic | ||
wave->gotoTimeLowCycles = timeLowCycles; | ||
wave->gotoTimeHighCycles = timeHighCycles; | ||
} else { // if (!(waveformEnabled & mask)) | ||
wave->timeHighCycles = timeHighCycles; | ||
wave->timeLowCycles = timeLowCycles; | ||
wave->gotoTimeHighCycles = wave->timeHighCycles; | ||
wave->gotoTimeLowCycles = wave->timeLowCycles; | ||
// Actually set the pin high or low in the IRQ service to guarantee times | ||
wave->nextServiceCycle = GetCycleCount() + microsecondsToClockCycles(1); | ||
waveformToEnable |= mask; | ||
|
@@ -263,16 +275,19 @@ static ICACHE_RAM_ATTR void timer1Interrupt() { | |
} else { | ||
SetGPIO(mask); | ||
} | ||
wave->nextServiceCycle = now + wave->nextTimeHighCycles; | ||
nextEventCycles = min_u32(nextEventCycles, wave->nextTimeHighCycles); | ||
wave->nextServiceCycle = now + wave->timeHighCycles; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is causing a drift in the waveform, because it doesn't consider the time elapsed between the timer firing and the measurement of now (assuming a single waveform). |
||
nextEventCycles = min_u32(nextEventCycles, wave->timeHighCycles); | ||
} else { | ||
if (i == 16) { | ||
GP16O &= ~1; // GPIO16 write slow as it's RMW | ||
} else { | ||
ClearGPIO(mask); | ||
} | ||
wave->nextServiceCycle = now + wave->nextTimeLowCycles; | ||
nextEventCycles = min_u32(nextEventCycles, wave->nextTimeLowCycles); | ||
wave->nextServiceCycle = now + wave->timeLowCycles; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as previosu comment, consider: |
||
nextEventCycles = min_u32(nextEventCycles, wave->timeLowCycles); | ||
// Copy over next full-cycle timings | ||
wave->timeHighCycles = wave->gotoTimeHighCycles; | ||
wave->timeLowCycles = wave->gotoTimeLowCycles; | ||
} | ||
} else { | ||
uint32_t deltaCycles = wave->nextServiceCycle - now; | ||
|
Uh oh!
There was an error while loading. Please reload this page.