Skip to content

Commit ada82c0

Browse files
authored
add function of tone (#111)
add function of tone for this issue #24
1 parent 3c67f6b commit ada82c0

File tree

7 files changed

+218
-11
lines changed

7 files changed

+218
-11
lines changed

cores/arduino/Arduino.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ void yield(void);
4949
} // extern "C"
5050
#endif // __cplusplus
5151

52-
#ifdef __cplusplus
53-
# include "WCharacter.h"
54-
# include "WString.h"
55-
# include "WMath.h"
56-
//# include <Tone.h>
57-
# include <HardwareSerial.h>
58-
#endif
52+
// Move the following content to the file wiring.h //by TempersLee
53+
// #ifdef __cplusplus
54+
// #include "WCharacter.h"
55+
// #include "WString.h"
56+
// #include "WMath.h"
57+
// # include <Tone.h>
58+
// # include <HardwareSerial.h>
59+
// #endif
5960

6061
// Include pins variant
6162
#include "pins_arduino.h"

cores/arduino/HardwareTimer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ HardwareTimer::HardwareTimer(TIM_TypeDef *instance)
7373
NVIC_EnableIRQ(TIM2_IRQn);
7474
#endif
7575

76-
#if defined(TIM3_BASE) && defined(TIM3_IRQn) //v006 has no interruption
76+
#if defined(TIM3_BASE) && !defined(CH32VM00X) //v006 has no interruption
7777
NVIC_EnableIRQ(TIM3_IRQn);
7878
#endif
7979

cores/arduino/Tone.cpp

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/* Tone.cpp
2+
3+
A Tone Generator Library
4+
5+
Written by Brett Hagman
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
21+
//Modified 3 june 2024 by TempersLee for CH32duino
22+
23+
#include "Arduino.h"
24+
#include "HardwareTimer.h"
25+
26+
#if defined(TIM_MODULE_ENABLED) && defined(TIMER_TONE) && !defined(TIM_MODULE_ONLY)
27+
28+
#define MAX_FREQ 65535
29+
30+
typedef struct {
31+
PinName pin;
32+
int32_t count;
33+
} timerPinInfo_t;
34+
35+
static void timerTonePinInit(PinName p, uint32_t frequency, uint32_t duration);
36+
static void tonePeriodElapsedCallback();
37+
static timerPinInfo_t TimerTone_pinInfo = {NC, 0};
38+
static HardwareTimer *TimerTone = NULL;
39+
40+
/**
41+
* @brief Tone Period elapsed callback in non-blocking mode
42+
* @param htim : timer handle
43+
* @retval None
44+
*/
45+
static void tonePeriodElapsedCallback()
46+
{
47+
GPIO_TypeDef *port = get_GPIO_Port(CH_PORT(TimerTone_pinInfo.pin));
48+
49+
if (port != NULL) {
50+
if (TimerTone_pinInfo.count != 0) {
51+
if (TimerTone_pinInfo.count > 0) {
52+
TimerTone_pinInfo.count--;
53+
}
54+
digital_io_toggle(port, CH_MAP_GPIO_PIN(TimerTone_pinInfo.pin));
55+
} else {
56+
digital_io_write(port, CH_MAP_GPIO_PIN(TimerTone_pinInfo.pin), 0);
57+
}
58+
}
59+
}
60+
61+
/**
62+
* @brief This function will reset the tone timer
63+
* @param port : pointer to port
64+
* @param pin : pin number to toggle
65+
* @retval None
66+
*/
67+
static void timerTonePinDeinit()
68+
{
69+
if (TimerTone != NULL) {
70+
TimerTone->timerHandleDeinit();
71+
}
72+
if (TimerTone_pinInfo.pin != NC) {
73+
pin_function(TimerTone_pinInfo.pin, CH_PIN_DATA(CH_MODE_INPUT, CH_CNF_INPUT_FLOAT, NOPULL, AFIO_NONE));
74+
TimerTone_pinInfo.pin = NC;
75+
}
76+
}
77+
78+
static void timerTonePinInit(PinName p, uint32_t frequency, uint32_t duration)
79+
{
80+
uint32_t timFreq = 2 * frequency;
81+
82+
if (frequency <= MAX_FREQ) {
83+
if (frequency == 0) {
84+
if (TimerTone != NULL) {
85+
TimerTone->pause();
86+
}
87+
} else {
88+
TimerTone_pinInfo.pin = p;
89+
90+
//Calculate the toggle count
91+
if (duration > 0) {
92+
TimerTone_pinInfo.count = ((timFreq * duration) / 1000);
93+
} else {
94+
TimerTone_pinInfo.count = -1;
95+
}
96+
97+
pin_function(TimerTone_pinInfo.pin, CH_PIN_DATA(CH_MODE_OUTPUT_50MHz, CH_CNF_OUTPUT_PP, NOPULL, AFIO_NONE));
98+
99+
TimerTone->setOverflow(timFreq, HERTZ_FORMAT);
100+
TimerTone->attachInterrupt(tonePeriodElapsedCallback);
101+
TimerTone->resume();
102+
}
103+
}
104+
}
105+
106+
// frequency (in hertz) and duration (in milliseconds).
107+
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
108+
{
109+
PinName p = digitalPinToPinName(_pin);
110+
111+
if (TimerTone == NULL) {
112+
TimerTone = new HardwareTimer(TIMER_TONE);
113+
}
114+
115+
if (p != NC) {
116+
if ((TimerTone_pinInfo.pin == NC) || (TimerTone_pinInfo.pin == p)) {
117+
timerTonePinInit(p, frequency, duration);
118+
}
119+
}
120+
}
121+
122+
void noTone(uint8_t _pin, bool destruct)
123+
{
124+
PinName p = digitalPinToPinName(_pin);
125+
if ((p != NC) && (TimerTone_pinInfo.pin == p) && (TimerTone != NULL)) {
126+
if (destruct) {
127+
timerTonePinDeinit();
128+
delete (TimerTone);
129+
TimerTone = NULL;
130+
} else {
131+
TimerTone->pause();
132+
}
133+
}
134+
}
135+
#else
136+
#warning "TIMER_TONE or TIM_MODULE_ENABLED not defined"
137+
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
138+
{
139+
(void)(_pin);
140+
(void)(frequency);
141+
(void)(duration);
142+
}
143+
144+
void noTone(uint8_t _pin, bool destruct)
145+
{
146+
(void)(_pin);
147+
(void)(destruct);
148+
}
149+
#endif /* TIM_MODULE_ENABLED && TIMER_TONE && !TIM_MODULE_ONLY*/

cores/arduino/Tone.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright (c) 2011 Arduino. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef _WIRING_TONE_
20+
#define _WIRING_TONE_
21+
22+
#ifdef __cplusplus
23+
/*
24+
* \brief Generate a tone to a pin.
25+
*
26+
* \param _pin
27+
* \param frequency Tone frequency (in hertz)
28+
* \param duration Tone duration (in milliseconds)
29+
*/
30+
extern void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
31+
32+
/*
33+
* \brief Stop tone generation on pin.
34+
*
35+
* \param _pin
36+
*/
37+
extern void noTone(uint8_t _pin, bool destruct = false);
38+
39+
#endif
40+
41+
#endif /* _WIRING_TONE_ */

cores/arduino/wiring.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@
4040
#include <board.h>
4141

4242
#ifdef __cplusplus
43-
// #include "HardwareTimer.h"
44-
// #include "Tone.h"
45-
// #include "WCharacter.h"
43+
#include "HardwareTimer.h"
44+
#include "Tone.h"
45+
#include "WCharacter.h"
4646
#include "WInterrupts.h"
4747
#include "WMath.h"
4848
#include "WSerial.h"

variants/CH32V00x/CH32V003F4/variant_CH32V003F4.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@
103103
#define PIN_WIRE_SCL PC2
104104
#endif
105105

106+
// Timer Definitions
107+
#ifndef TIMER_TONE
108+
#define TIMER_TONE TIM2
109+
#endif
110+
#ifndef TIMER_SERVO
111+
#define TIMER_SERVO TIM1
112+
#endif
113+
114+
106115
/*----------------------------------------------------------------------------
107116
* Arduino objects - C++ only
108117
*----------------------------------------------------------------------------*/

variants/CH32VM00X/CH32V006K8/variant_CH32V006K8.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,13 @@
116116
#define PIN_WIRE_SCL PC2
117117
#endif
118118

119+
#ifndef TIMER_TONE
120+
#define TIMER_TONE TIM2
121+
#endif
122+
#ifndef TIMER_SERVO
123+
#define TIMER_SERVO TIM1
124+
#endif
125+
119126
/*----------------------------------------------------------------------------
120127
* Arduino objects - C++ only
121128
*----------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)