Skip to content

Commit 060e221

Browse files
committed
Tone: avoid multiple FspTimer.begin() without end()
1 parent 55ed9e9 commit 060e221

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

cores/arduino/Tone.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class Tone {
2525
}
2626

2727
void start(void) {
28-
if (frequency) {
28+
if ((frequency != 0) && (channel != -1)) {
2929
tone_timer.start();
3030
}
3131
if (duration != 0) {
@@ -42,7 +42,7 @@ class Tone {
4242
}
4343

4444
void stop(void) {
45-
if (frequency) {
45+
if ((frequency != 0) && (channel != -1)) {
4646
tone_timer.stop();
4747
}
4848
digitalWrite(pin, LOW);
@@ -51,15 +51,18 @@ class Tone {
5151
static void timer_config(uint32_t period_us) {
5252
// Configure and enable the tone timer.
5353
uint8_t type = 0;
54-
if (channel == -1) {
54+
if (tone_timer.is_opened()) {
55+
tone_timer.set_frequency(1000000.0f/period_us);
56+
} else {
5557
channel = FspTimer::get_available_timer(type);
58+
if (channel != -1) {
59+
tone_timer.begin(TIMER_MODE_PERIODIC, type, channel,
60+
1000000.0f/period_us, 50.0f, tone_timer_callback, nullptr);
61+
tone_timer.setup_overflow_irq();
62+
tone_timer.open();
63+
tone_timer.stop();
64+
}
5665
}
57-
58-
tone_timer.begin(TIMER_MODE_PERIODIC, type, channel,
59-
1000000.0f/period_us, 50.0f, tone_timer_callback, nullptr);
60-
tone_timer.setup_overflow_irq();
61-
tone_timer.open();
62-
tone_timer.stop();
6366
}
6467
};
6568

@@ -76,8 +79,8 @@ void tone(pin_size_t pin, unsigned int frequency, unsigned long duration) {
7679
delete active_tone;
7780
}
7881
Tone* t = new Tone(pin, frequency, duration);
79-
t->start();
8082
active_tone = t;
83+
t->start();
8184
};
8285

8386
void noTone(pin_size_t __attribute__((unused)) pin) {

0 commit comments

Comments
 (0)