Skip to content

Commit 5d74235

Browse files
committed
Fixing infinite hang within 'noTone' if it is called before 'tone' was called at least once
1 parent aef4af4 commit 5d74235

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

cores/arduino/Tone.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,19 @@ void tone (uint32_t outputPin, uint32_t frequency, uint32_t duration)
152152

153153
void noTone (uint32_t outputPin)
154154
{
155-
resetTC(TONE_TC);
156-
digitalWrite(outputPin, LOW);
157-
toneIsActive = false;
155+
/* 'tone' need to run at least once in order to enable GCLK for
156+
* the timers used for the tone-functionality. If 'noTone' is called
157+
* without ever calling 'tone' before then 'WAIT_TC16_REGS_SYNC(TCx)'
158+
* will wait infinitely. The variable 'firstTimeRunning' is set the
159+
* 1st time 'tone' is set so it can be used to detect wether or not
160+
* 'tone' has been called before.
161+
*/
162+
if(firstTimeRunning)
163+
{
164+
resetTC(TONE_TC);
165+
digitalWrite(outputPin, LOW);
166+
toneIsActive = false;
167+
}
158168
}
159169

160170
#ifdef __cplusplus

0 commit comments

Comments
 (0)