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