Skip to content

Commit 1af4ea6

Browse files
Allow waveforms to be specified in clock cycles (#7211)
* Allow waveforms to be specified in clock cycles Allow the PWM to specify sub-microsecond waveform edges, as have been proposed by @dok-net and me. No other changes intended. This will increase the linearity at 30 and 40 kHZ PWM rates, but leave most other things unaffected. * Cycle-accurate wafveform to specify Tone periods Co-authored-by: Develo <deveyes@gmail.com>
1 parent 6cb1699 commit 1af4ea6

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

cores/esp8266/Tone.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
*/
2323

2424
#include "Arduino.h"
25+
#include "user_interface.h"
2526
#include "core_esp8266_waveform.h"
2627

2728
// Which pins have a tone running on them?
@@ -35,10 +36,10 @@ static void _startTone(uint8_t _pin, uint32_t high, uint32_t low, unsigned long
3536

3637
pinMode(_pin, OUTPUT);
3738

38-
high = std::max(high, (uint32_t)25); // new 20KHz maximum tone frequency,
39-
low = std::max(low, (uint32_t)25); // (25us high + 25us low period = 20KHz)
39+
high = std::max(high, (uint32_t)microsecondsToClockCycles(25)); // new 20KHz maximum tone frequency,
40+
low = std::max(low, (uint32_t)microsecondsToClockCycles(25)); // (25us high + 25us low period = 20KHz)
4041

41-
if (startWaveform(_pin, high, low, (uint32_t) duration * 1000)) {
42+
if (startWaveformCycles(_pin, high, low, microsecondsToClockCycles(duration * 1000))) {
4243
_toneMap |= 1 << _pin;
4344
}
4445
}
@@ -48,7 +49,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
4849
if (frequency == 0) {
4950
noTone(_pin);
5051
} else {
51-
uint32_t period = 1000000L / frequency;
52+
uint32_t period = (1000000L * system_get_cpu_freq()) / frequency;
5253
uint32_t high = period / 2;
5354
uint32_t low = period - high;
5455
_startTone(_pin, high, low, duration);
@@ -62,7 +63,7 @@ void tone(uint8_t _pin, double frequency, unsigned long duration) {
6263
if (frequency < 1.0) { // FP means no exact comparisons
6364
noTone(_pin);
6465
} else {
65-
double period = 1000000.0 / frequency;
66+
double period = (1000000.0L * system_get_cpu_freq()) / frequency;
6667
uint32_t high = (uint32_t)((period / 2.0) + 0.5);
6768
uint32_t low = (uint32_t)(period + 0.5) - high;
6869
_startTone(_pin, high, low, duration);

cores/esp8266/core_esp8266_waveform.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,19 @@ void setTimer1Callback(uint32_t (*fn)()) {
112112
// waveform smoothly on next low->high transition. For immediate change, stopWaveform()
113113
// first, then it will immediately begin.
114114
int startWaveform(uint8_t pin, uint32_t timeHighUS, uint32_t timeLowUS, uint32_t runTimeUS) {
115-
if ((pin > 16) || isFlashInterfacePin(pin)) {
115+
return startWaveformCycles(pin, microsecondsToClockCycles(timeHighUS), microsecondsToClockCycles(timeLowUS), microsecondsToClockCycles(runTimeUS));
116+
}
117+
118+
int startWaveformCycles(uint8_t pin, uint32_t timeHighCycles, uint32_t timeLowCycles, uint32_t runTimeCycles) {
119+
if ((pin > 16) || isFlashInterfacePin(pin)) {
116120
return false;
117121
}
118122
Waveform *wave = &waveform[pin];
119123
// Adjust to shave off some of the IRQ time, approximately
120-
wave->nextTimeHighCycles = microsecondsToClockCycles(timeHighUS);
121-
wave->nextTimeLowCycles = microsecondsToClockCycles(timeLowUS);
122-
wave->expiryCycle = runTimeUS ? GetCycleCount() + microsecondsToClockCycles(runTimeUS) : 0;
123-
if (runTimeUS && !wave->expiryCycle) {
124+
wave->nextTimeHighCycles = timeHighCycles;
125+
wave->nextTimeLowCycles = timeLowCycles;
126+
wave->expiryCycle = runTimeCycles ? GetCycleCount() + runTimeCycles : 0;
127+
if (runTimeCycles && !wave->expiryCycle) {
124128
wave->expiryCycle = 1; // expiryCycle==0 means no timeout, so avoid setting it
125129
}
126130

cores/esp8266/core_esp8266_waveform.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ extern "C" {
5050
// If runtimeUS > 0 then automatically stop it after that many usecs.
5151
// Returns true or false on success or failure.
5252
int startWaveform(uint8_t pin, uint32_t timeHighUS, uint32_t timeLowUS, uint32_t runTimeUS);
53+
// Same as above, but pass in CPU clock cycles instead of microseconds
54+
int startWaveformCycles(uint8_t pin, uint32_t timeHighCycles, uint32_t timeLowCycles, uint32_t runTimeCycles);
5355
// Stop a waveform, if any, on the specified pin.
5456
// Returns true or false on success or failure.
5557
int stopWaveform(uint8_t pin);

cores/esp8266/core_esp8266_wiring_pwm.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "core_esp8266_waveform.h"
2626

2727
extern "C" {
28+
#include "user_interface.h"
2829

2930
static uint32_t analogMap = 0;
3031
static int32_t analogScale = PWMRANGE;
@@ -50,7 +51,7 @@ extern void __analogWrite(uint8_t pin, int val) {
5051
if (pin > 16) {
5152
return;
5253
}
53-
uint32_t analogPeriod = 1000000L / analogFreq;
54+
uint32_t analogPeriod = (1000000L * system_get_cpu_freq()) / analogFreq;
5455
if (val < 0) {
5556
val = 0;
5657
} else if (val > analogScale) {
@@ -68,7 +69,7 @@ extern void __analogWrite(uint8_t pin, int val) {
6869
stopWaveform(pin);
6970
digitalWrite(pin, LOW);
7071
} else {
71-
if (startWaveform(pin, high, low, 0)) {
72+
if (startWaveformCycles(pin, high, low, 0)) {
7273
analogMap |= (1 << pin);
7374
}
7475
}

0 commit comments

Comments
 (0)