From 5d74235ba25b756fa6719f5e0eea7b629460d285 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Mon, 16 Sep 2019 10:58:20 +0200 Subject: [PATCH] Fixing infinite hang within 'noTone' if it is called before 'tone' was called at least once --- cores/arduino/Tone.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/cores/arduino/Tone.cpp b/cores/arduino/Tone.cpp index 45b438d9b..b54781133 100644 --- a/cores/arduino/Tone.cpp +++ b/cores/arduino/Tone.cpp @@ -152,9 +152,19 @@ void tone (uint32_t outputPin, uint32_t frequency, uint32_t duration) void noTone (uint32_t outputPin) { - resetTC(TONE_TC); - digitalWrite(outputPin, LOW); - toneIsActive = false; + /* 'tone' need to run at least once in order to enable GCLK for + * the timers used for the tone-functionality. If 'noTone' is called + * without ever calling 'tone' before then 'WAIT_TC16_REGS_SYNC(TCx)' + * will wait infinitely. The variable 'firstTimeRunning' is set the + * 1st time 'tone' is set so it can be used to detect wether or not + * 'tone' has been called before. + */ + if(firstTimeRunning) + { + resetTC(TONE_TC); + digitalWrite(outputPin, LOW); + toneIsActive = false; + } } #ifdef __cplusplus