Skip to content

Commit ebda795

Browse files
Support multiple tone(), analogWrite(), and Servo (#4640)
Remove and rewrite all the parts of the core/libraries using TIMER1 and consolidate into a single, shared waveform generation interrupt structure. Tone, analogWrite(), Servo all now just call into this shared resource to perform their tasks so are all compatible and can be used simultaneously. This setup enables multiple tones, analogWrites, servos, and stepper motors to be controlled with reasonable accuracy. It uses both TIMER1 and the internal ESP cycle counter to handle timing of waveform edges. TIMER1 is used in non-reload mode and only edges cause interrupts. The interrupt is started and stopped as required, minimizing overhead when these features are not being used. A generic "startWaveform(pin, high-US, low-US, runtime-US)" and "stopWaveform(pin)" allow for further types of interfaces. Minimum high or low period is ~1 us. Add a tone(float) method, useful when working with lower frequencies. Fixes #4321. Fixes 4349.
1 parent ea4720b commit ebda795

12 files changed

+600
-841
lines changed

cores/esp8266/Arduino.h

+1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 100000
279279
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
280280

281281
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
282+
void tone(uint8_t _pin, double frequency, unsigned long duration = 0);
282283
void noTone(uint8_t _pin);
283284

284285
// WMath prototypes

cores/esp8266/Tone.cpp

+38-94
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
A Tone Generator Library for the ESP8266
55
6-
Copyright (c) 2016 Ben Pirt. All rights reserved.
6+
Original Copyright (c) 2016 Ben Pirt. All rights reserved.
77
This file is part of the esp8266 core for Arduino environment.
88
99
This library is free software; you can redistribute it and/or
@@ -22,115 +22,59 @@
2222
*/
2323

2424
#include "Arduino.h"
25-
#include "pins_arduino.h"
25+
#include "core_esp8266_waveform.h"
2626

27-
#define AVAILABLE_TONE_PINS 1
28-
const uint8_t tone_timers[] = { 1 };
29-
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255, };
30-
static long toggle_counts[AVAILABLE_TONE_PINS] = { 0, };
31-
#define T1INDEX 0
27+
// Which pins have a tone running on them?
28+
static uint32_t _toneMap = 0;
3229

33-
void t1IntHandler();
3430

35-
static int8_t toneBegin(uint8_t _pin) {
36-
int8_t _index = -1;
37-
38-
// if we're already using the pin, reuse it.
39-
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
40-
if (tone_pins[i] == _pin) {
41-
return i;
42-
}
31+
static void _startTone(uint8_t _pin, uint32_t high, uint32_t low, unsigned long duration) {
32+
if (_pin > 16) {
33+
return;
4334
}
4435

45-
// search for an unused timer.
46-
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
47-
if (tone_pins[i] == 255) {
48-
tone_pins[i] = _pin;
49-
_index = i;
50-
break;
51-
}
52-
}
36+
pinMode(_pin, OUTPUT);
5337

54-
return _index;
55-
}
38+
high = std::max(high, (uint32_t)100);
39+
low = std::max(low, (uint32_t)100);
5640

57-
// frequency (in hertz) and duration (in milliseconds).
58-
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
59-
int8_t _index;
60-
61-
_index = toneBegin(_pin);
62-
63-
if (_index >= 0) {
64-
// Set the pinMode as OUTPUT
65-
pinMode(_pin, OUTPUT);
66-
67-
// Alternate handling of zero freqency to avoid divide by zero errors
68-
if (frequency == 0)
69-
{
70-
noTone(_pin);
71-
return;
72-
}
73-
74-
// Calculate the toggle count
75-
if (duration > 0) {
76-
toggle_counts[_index] = 2 * frequency * duration / 1000;
77-
} else {
78-
toggle_counts[_index] = -1;
79-
}
80-
81-
// set up the interrupt frequency
82-
switch (tone_timers[_index]) {
83-
case 0:
84-
// Not currently supported
85-
break;
86-
87-
case 1:
88-
timer1_disable();
89-
timer1_isr_init();
90-
timer1_attachInterrupt(t1IntHandler);
91-
timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP);
92-
timer1_write((clockCyclesPerMicrosecond() * 500000) / frequency);
93-
break;
94-
}
41+
if (startWaveform(_pin, high, low, (uint32_t) duration * 1000)) {
42+
_toneMap |= 1 << _pin;
9543
}
9644
}
9745

98-
void disableTimer(uint8_t _index) {
99-
tone_pins[_index] = 255;
100-
101-
switch (tone_timers[_index]) {
102-
case 0:
103-
// Not currently supported
104-
break;
10546

106-
case 1:
107-
timer1_disable();
108-
break;
47+
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
48+
if (frequency == 0) {
49+
noTone(_pin);
50+
} else {
51+
uint32_t period = 1000000L / frequency;
52+
uint32_t high = period / 2;
53+
uint32_t low = period - high;
54+
_startTone(_pin, high, low, duration);
10955
}
11056
}
11157

112-
void noTone(uint8_t _pin) {
113-
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
114-
if (tone_pins[i] == _pin) {
115-
tone_pins[i] = 255;
116-
disableTimer(i);
117-
break;
118-
}
58+
59+
// Separate tone(float) to hopefully not pull in floating point libs unless
60+
// it's called with a float.
61+
void tone(uint8_t _pin, double frequency, unsigned long duration) {
62+
if (frequency < 1.0) { // FP means no exact comparisons
63+
noTone(_pin);
64+
} else {
65+
double period = 1000000.0 / frequency;
66+
uint32_t high = (uint32_t)((period / 2.0) + 0.5);
67+
uint32_t low = (uint32_t)(period + 0.5) - high;
68+
_startTone(_pin, high, low, duration);
11969
}
120-
digitalWrite(_pin, LOW);
12170
}
12271

123-
ICACHE_RAM_ATTR void t1IntHandler() {
124-
if (toggle_counts[T1INDEX] != 0){
125-
// toggle the pin
126-
digitalWrite(tone_pins[T1INDEX], toggle_counts[T1INDEX] % 2);
127-
toggle_counts[T1INDEX]--;
128-
// handle the case of indefinite duration
129-
if (toggle_counts[T1INDEX] < -2){
130-
toggle_counts[T1INDEX] = -1;
131-
}
132-
}else{
133-
disableTimer(T1INDEX);
134-
digitalWrite(tone_pins[T1INDEX], LOW);
72+
73+
void noTone(uint8_t _pin) {
74+
if (_pin > 16) {
75+
return;
13576
}
77+
stopWaveform(_pin);
78+
_toneMap &= ~(1 << _pin);
79+
digitalWrite(_pin, 0);
13680
}

cores/esp8266/base64.cpp

100755100644
File mode changed.

cores/esp8266/base64.h

100755100644
File mode changed.

0 commit comments

Comments
 (0)