Skip to content

Commit 71a198a

Browse files
Fix problems with tone functions
1 parent dd45335 commit 71a198a

File tree

2 files changed

+25
-16
lines changed

2 files changed

+25
-16
lines changed

STM32F1/cores/maple/tone.cpp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111
#include "Arduino.h"
1212
#include <HardwareTimer.h>
1313

14-
// define default timer and channel
15-
#ifndef TONE_TIMER
16-
#define TONE_TIMER 4
17-
#endif
18-
#ifndef TONE_CHANNEL
19-
#define TONE_CHANNEL 4
20-
#endif
2114

2215
#define PinTimer(pin) (PIN_MAP[pin].timer_device->clk_id-RCC_TIMER1+1)
2316
#define PinChannel(pin) (PIN_MAP[pin].timer_channel)
@@ -30,22 +23,37 @@
3023
#define USE_BSRR
3124

3225
// construct static timer array (
33-
HardwareTimer TTimer1(1), TTimer2(2), TTimer3(3), TTimer4(4);
34-
#ifdef STM32_HIGH_DENSITY
35-
HardwareTimer TTimer5(5), TTimer6(6), TTimer7(7), TTimer8(8);
36-
#endif
26+
3727

3828
#ifdef STM32_HIGH_DENSITY
39-
HardwareTimer *TTimer[8] { &TTimer1,&TTimer2,&TTimer3,&TTimer4,&TTimer5,&TTimer6,&TTimer7,&TTimer8 };
29+
// define default timer and channel
30+
#ifndef TONE_TIMER
31+
#define TONE_TIMER 8
32+
#endif
33+
#ifndef TONE_CHANNEL
34+
#define TONE_CHANNEL 8
35+
#endif
36+
37+
HardwareTimer TTimer1(1), TTimer2(2), TTimer3(3), TTimer4(4),TTimer5(5), TTimer6(6), TTimer7(7), TTimer8(8);
38+
HardwareTimer *TTimer[8] = { &TTimer1,&TTimer2,&TTimer3,&TTimer4,&TTimer5,&TTimer6,&TTimer7,&TTimer8 };
4039
#else
41-
HardwareTimer *TTimer[4] { &TTimer1,&TTimer2,&TTimer3,&TTimer4 };
40+
// define default timer and channel
41+
#ifndef TONE_TIMER
42+
#define TONE_TIMER 4
43+
#endif
44+
#ifndef TONE_CHANNEL
45+
#define TONE_CHANNEL 4
46+
#endif
47+
48+
HardwareTimer TTimer1(1), TTimer2(2), TTimer3(3), TTimer4(4);
49+
HardwareTimer *TTimer[4] = { &TTimer1,&TTimer2,&TTimer3,&TTimer4 };
4250
#endif
4351

4452

4553
uint8_t tone_force_channel = 0; // forced timer channel
4654
uint8_t tone_force_ntimer = 0; // forced timer
4755

48-
HardwareTimer *tone_timer = TTimer[TONE_TIMER]; // timer used to generate frequency
56+
HardwareTimer *tone_timer;// = TTimer[TONE_TIMER-1]; // timer used to generate frequency
4957
uint8_t tone_channel = TONE_CHANNEL; // timer channel used to generate frequency
5058
uint8_t tone_ntimer = TONE_TIMER; // timer used to generate frequency
5159

@@ -138,7 +146,7 @@ void tone(uint32_t pin, uint32_t freq, uint32_t duration) {
138146

139147
tone_timer->pause();
140148

141-
if(freq > 0 || duration >0 ){
149+
if(freq > 0 && duration >0 ){
142150
uint32_t count = 18000000/freq; // timer counts per half wave
143151
tone_ncount = tone_n = (count>>16)+1; // number of 16-bit count chunk
144152
tone_tcount = count/tone_ncount; // size of count chunk
@@ -183,7 +191,7 @@ void tone(uint32_t pin, uint32_t freq, uint32_t duration) {
183191
////////////////////////////////////////////////////////////////////////////////
184192
// disable tone on specified pin, if any
185193
void noTone(uint32_t pin){
186-
tone(pin,-1,0); // it's all handled in tone()
194+
tone(pin,0,0); // it's all handled in tone()
187195
}
188196

189197
////////////////////////////////////////////////////////////////////////////////

STM32F1/cores/maple/wirish.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#include <stdint.h>
7777

7878
#include <WCharacter.h>
79+
#include <tone.h>
7980

8081
typedef unsigned int word;
8182
// typedef uint16 word;// definition from Arduino website, now appears to be incorrect for 32 bit devices

0 commit comments

Comments
 (0)