From 41b165cae0d2c0493f178e0ebfff89a1afe5ed7a Mon Sep 17 00:00:00 2001 From: stevstrong Date: Tue, 20 Mar 2018 11:53:03 +0100 Subject: [PATCH 1/2] Increase tone resolution Small change to increase tone resolution by multiplying before dividing. --- STM32F1/cores/maple/tone.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/STM32F1/cores/maple/tone.cpp b/STM32F1/cores/maple/tone.cpp index b249a78..9d0b846 100644 --- a/STM32F1/cores/maple/tone.cpp +++ b/STM32F1/cores/maple/tone.cpp @@ -60,7 +60,7 @@ uint8_t tone_ntimer = TONE_TIMER; // timer used to generate freque bool tone_state = true; // last pin state for toggling short tone_pin = -1; // pin for outputting sound short tone_freq = 444; // tone frequency (0=pause) -volatile uint32_t tone_nhw = 0; // tone duration in number of half waves +volatile uint32_t tone_nhw = 0; // tone duration in number of half waves uint16_t tone_tcount = 0; // time between handler calls in 1/36 usec uint16_t tone_ncount = 0; // handler call between toggling uint16_t tone_n = 0; // remaining handler calls before toggling @@ -151,7 +151,7 @@ void tone(uint32_t pin, uint32_t freq, uint32_t duration) { tone_ncount = tone_n = (count>>16)+1; // number of 16-bit count chunk tone_tcount = count/tone_ncount; // size of count chunk if(duration > 0) // number of half waves to be generated - tone_nhw = ((duration*freq)/1000)<<1; + tone_nhw = (2*duration*freq)/1000; else // no duration specified, continuous sound until noTone() called tone_nhw = 0; From e76e8adb5254aa48bb04e483a3eb77e032331cea Mon Sep 17 00:00:00 2001 From: stevstrong Date: Fri, 23 Mar 2018 10:55:26 +0100 Subject: [PATCH 2/2] F1 tone resolution: try to force compiler optimisation --- STM32F1/cores/maple/tone.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/STM32F1/cores/maple/tone.cpp b/STM32F1/cores/maple/tone.cpp index 9d0b846..c179c34 100644 --- a/STM32F1/cores/maple/tone.cpp +++ b/STM32F1/cores/maple/tone.cpp @@ -151,7 +151,7 @@ void tone(uint32_t pin, uint32_t freq, uint32_t duration) { tone_ncount = tone_n = (count>>16)+1; // number of 16-bit count chunk tone_tcount = count/tone_ncount; // size of count chunk if(duration > 0) // number of half waves to be generated - tone_nhw = (2*duration*freq)/1000; + tone_nhw = duration*freq*2/1000; else // no duration specified, continuous sound until noTone() called tone_nhw = 0;