Skip to content

Commit 7ee4350

Browse files
Merge pull request #369 from stevstrong/patch-2
Update tone.cpp to fix issue if no duration was passed etc
2 parents 44d5894 + e14c83f commit 7ee4350

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

STM32F1/cores/maple/tone.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ uint8_t tone_ntimer = TONE_TIMER; // timer used to generate freque
6060
bool tone_state = true; // last pin state for toggling
6161
short tone_pin = -1; // pin for outputting sound
6262
short tone_freq = 444; // tone frequency (0=pause)
63-
uint32_t tone_nhw = 0; // tone duration in number of half waves
63+
volatile uint32_t tone_nhw = 0; // tone duration in number of half waves
6464
uint16_t tone_tcount = 0; // time between handler calls in 1/36 usec
6565
uint16_t tone_ncount = 0; // handler call between toggling
6666
uint16_t tone_n = 0; // remaining handler calls before toggling
@@ -146,12 +146,12 @@ void tone(uint32_t pin, uint32_t freq, uint32_t duration) {
146146

147147
tone_timer->pause();
148148

149-
if(freq > 0 && duration >0 ){
149+
if(freq > 0){
150150
uint32_t count = (F_CPU/4)/freq; // timer counts per half wave
151151
tone_ncount = tone_n = (count>>16)+1; // number of 16-bit count chunk
152152
tone_tcount = count/tone_ncount; // size of count chunk
153153
if(duration > 0) // number of half waves to be generated
154-
tone_nhw = ((duration*(freq>0?freq:100))/1000)<<1;
154+
tone_nhw = ((duration*freq)/1000)<<1;
155155
else // no duration specified, continuous sound until noTone() called
156156
tone_nhw = 0;
157157

@@ -186,6 +186,7 @@ void tone(uint32_t pin, uint32_t freq, uint32_t duration) {
186186
pinMode(tone_pin, INPUT);
187187

188188
}
189+
while(tone_nhw) ; // blocks till duration elapsed
189190
}
190191

191192
////////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)