From 7c3d21aedbc02c53543a7b1cff275523f62626d9 Mon Sep 17 00:00:00 2001 From: pfeerick Date: Tue, 20 Dec 2016 13:11:42 +1000 Subject: [PATCH 1/4] Prevent divide by zero error causing code to crash As per the issue at #2491, there is a divide by error issue resulting from the specification of 0 as the frequency. This does not appear to affect the AVR implementation, but it crashes on ESP8266s. I have merely removed the division if the frequency is zero, which appears to be giving the expected results (no tone), without any code crashes. To test, simply load the toneMelody sketch included with the Arduino IDE (Examples -> 02. Digital -> toneMelody) and change the piezo to something else if you need to. On the Witty module used to test this, I could also tell by the wifi led blinking every time the code crashed as the ESP8266 immediately rebooted. --- cores/esp8266/Tone.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/Tone.cpp b/cores/esp8266/Tone.cpp index bcdd358a85..723fd20afa 100644 --- a/cores/esp8266/Tone.cpp +++ b/cores/esp8266/Tone.cpp @@ -82,7 +82,12 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) { timer1_isr_init(); timer1_attachInterrupt(t1IntHandler); timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP); - timer1_write((clockCyclesPerMicrosecond() * 500000) / frequency); + if (frequency == 0) { + timer1_write((clockCyclesPerMicrosecond() * 500000)); + } + else { + timer1_write((clockCyclesPerMicrosecond() * 500000) / frequency); + } break; } } From d4aa9becca3428dd7c5c8f447b34f9b0b24875db Mon Sep 17 00:00:00 2001 From: pfeerick Date: Tue, 10 Jan 2017 21:26:24 +1000 Subject: [PATCH 2/4] Use noTone when frequency is zero When a frequency of zero is given to tone(), instead call noTone() and exit. Placed after some of the initialisation stuff to ensure the pin is mapped as a output, etc. Tested as functional against a Node MCU 1.0 board and the toneMelody example sketch, using GPIO5 (pin D1). --- cores/esp8266/Tone.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/cores/esp8266/Tone.cpp b/cores/esp8266/Tone.cpp index 723fd20afa..55ab86fded 100644 --- a/cores/esp8266/Tone.cpp +++ b/cores/esp8266/Tone.cpp @@ -64,6 +64,13 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) { // Set the pinMode as OUTPUT pinMode(_pin, OUTPUT); + // Alternate handling of zero freqency to avoid divide by zero errors + if (frequency == 0) + { + noTone(_pin); + return; + } + // Calculate the toggle count if (duration > 0) { toggle_counts[_index] = 2 * frequency * duration / 1000; @@ -82,12 +89,7 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) { timer1_isr_init(); timer1_attachInterrupt(t1IntHandler); timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP); - if (frequency == 0) { - timer1_write((clockCyclesPerMicrosecond() * 500000)); - } - else { - timer1_write((clockCyclesPerMicrosecond() * 500000) / frequency); - } + timer1_write((clockCyclesPerMicrosecond() * 500000) / frequency); break; } } From 641faab4362df39672fd1976697a192501903537 Mon Sep 17 00:00:00 2001 From: pfeerick Date: Tue, 10 Jan 2017 21:27:55 +1000 Subject: [PATCH 3/4] Errant tab in formatting --- cores/esp8266/Tone.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/Tone.cpp b/cores/esp8266/Tone.cpp index 55ab86fded..7ec9336877 100644 --- a/cores/esp8266/Tone.cpp +++ b/cores/esp8266/Tone.cpp @@ -64,8 +64,8 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) { // Set the pinMode as OUTPUT pinMode(_pin, OUTPUT); - // Alternate handling of zero freqency to avoid divide by zero errors - if (frequency == 0) + // Alternate handling of zero freqency to avoid divide by zero errors + if (frequency == 0) { noTone(_pin); return; From d5ccfcf0e3c4e701fd3c677e6f2652dfb4b508ca Mon Sep 17 00:00:00 2001 From: pfeerick Date: Tue, 10 Jan 2017 21:31:02 +1000 Subject: [PATCH 4/4] Rest of tabs that crept in from web editor Defaulted to tabs and 8 indent :sigh: --- cores/esp8266/Tone.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cores/esp8266/Tone.cpp b/cores/esp8266/Tone.cpp index 7ec9336877..fb7837ee46 100644 --- a/cores/esp8266/Tone.cpp +++ b/cores/esp8266/Tone.cpp @@ -67,10 +67,10 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) { // Alternate handling of zero freqency to avoid divide by zero errors if (frequency == 0) { - noTone(_pin); - return; - } - + noTone(_pin); + return; + } + // Calculate the toggle count if (duration > 0) { toggle_counts[_index] = 2 * frequency * duration / 1000;