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

Allow waveforms to be specified in clock cycles #7211

Merged
merged 3 commits into from
Apr 16, 2020
Merged
Changes from 1 commit
Commits
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
Cycle-accurate wafveform to specify Tone periods
earlephilhower committed Apr 13, 2020
commit 6001e1df4c76cc2ec4abc01a1fb684a4a74a0bde
11 changes: 6 additions & 5 deletions cores/esp8266/Tone.cpp
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@
*/

#include "Arduino.h"
#include "user_interface.h"
#include "core_esp8266_waveform.h"

// 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

pinMode(_pin, OUTPUT);

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

if (startWaveform(_pin, high, low, (uint32_t) duration * 1000)) {
if (startWaveformCycles(_pin, high, low, microsecondsToClockCycles(duration * 1000))) {
_toneMap |= 1 << _pin;
}
}
@@ -48,7 +49,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
if (frequency == 0) {
noTone(_pin);
} else {
uint32_t period = 1000000L / frequency;
uint32_t period = (1000000L * system_get_cpu_freq()) / frequency;
uint32_t high = period / 2;
uint32_t low = period - high;
_startTone(_pin, high, low, duration);
@@ -62,7 +63,7 @@ void tone(uint8_t _pin, double frequency, unsigned long duration) {
if (frequency < 1.0) { // FP means no exact comparisons
noTone(_pin);
} else {
double period = 1000000.0 / frequency;
double period = (1000000.0L * system_get_cpu_freq()) / frequency;
uint32_t high = (uint32_t)((period / 2.0) + 0.5);
uint32_t low = (uint32_t)(period + 0.5) - high;
_startTone(_pin, high, low, duration);