From ab7580814e839c759ae2ffab9e0c7e0be803a032 Mon Sep 17 00:00:00 2001 From: Adriaan Date: Mon, 11 May 2020 01:47:11 +0200 Subject: [PATCH 1/2] Fix crash when calling tone() with frequency 0 Avoid division by 0 when a frequency of 0 is passed to the tone() function. If a 0 is passed, noTone() is called instead. --- cores/arduino/Tone.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/cores/arduino/Tone.cpp b/cores/arduino/Tone.cpp index b54781133..811a7c9fd 100644 --- a/cores/arduino/Tone.cpp +++ b/cores/arduino/Tone.cpp @@ -57,6 +57,13 @@ void toneAccurateClock (uint32_t accurateSystemCoreClockFrequency) void tone (uint32_t outputPin, uint32_t frequency, uint32_t duration) { + + if (frequency == 0) + { + noTone(outputPin); + return; + } + // Configure interrupt request NVIC_DisableIRQ(TONE_TC_IRQn); NVIC_ClearPendingIRQ(TONE_TC_IRQn); From 344da94653c7a23f6d2b4a94e0f7c98ecf956870 Mon Sep 17 00:00:00 2001 From: Adriaan Date: Mon, 11 May 2020 02:04:17 +0200 Subject: [PATCH 2/2] Add comment --- cores/arduino/Tone.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/cores/arduino/Tone.cpp b/cores/arduino/Tone.cpp index 811a7c9fd..d427d6075 100644 --- a/cores/arduino/Tone.cpp +++ b/cores/arduino/Tone.cpp @@ -58,6 +58,7 @@ void toneAccurateClock (uint32_t accurateSystemCoreClockFrequency) void tone (uint32_t outputPin, uint32_t frequency, uint32_t duration) { + // Avoid divide by zero error by calling 'noTone' instead if (frequency == 0) { noTone(outputPin);