29
29
typedef struct {
30
30
PinName pin;
31
31
int32_t count;
32
- uint8_t state;
33
32
} timerPinInfo_t;
34
33
35
34
static void timerTonePinInit (PinName p, uint32_t frequency, uint32_t duration);
36
35
static void tonePeriodElapsedCallback (HardwareTimer *HT);
37
- static timerPinInfo_t TimerTone_pinInfo;
38
-
36
+ static timerPinInfo_t TimerTone_pinInfo = {NC, 0 };
39
37
static HardwareTimer *TimerTone = NULL ;
40
38
41
- PinName g_lastPin = NC;
42
-
43
39
/* *
44
40
* @brief Tone Period elapsed callback in non-blocking mode
45
41
* @param htim : timer handle
@@ -55,15 +51,13 @@ static void tonePeriodElapsedCallback(HardwareTimer *HT)
55
51
if (TimerTone_pinInfo.count > 0 ) {
56
52
TimerTone_pinInfo.count --;
57
53
}
58
- TimerTone_pinInfo.state = (TimerTone_pinInfo.state == 0 ) ? 1 : 0 ;
59
- digital_io_write (port, STM_LL_GPIO_PIN (TimerTone_pinInfo.pin ), TimerTone_pinInfo.state );
54
+ digital_io_toggle (port, STM_LL_GPIO_PIN (TimerTone_pinInfo.pin ));
60
55
} else {
61
56
digital_io_write (port, STM_LL_GPIO_PIN (TimerTone_pinInfo.pin ), 0 );
62
57
}
63
58
}
64
59
}
65
60
66
-
67
61
/* *
68
62
* @brief This function will reset the tone timer
69
63
* @param port : pointer to port
@@ -72,40 +66,40 @@ static void tonePeriodElapsedCallback(HardwareTimer *HT)
72
66
*/
73
67
static void timerTonePinDeinit ()
74
68
{
75
- TimerTone->timerHandleDeinit ();
76
- pin_function (TimerTone_pinInfo.pin , STM_PIN_DATA (STM_MODE_INPUT, GPIO_NOPULL, 0 ));
69
+ if (TimerTone != NULL ) {
70
+ TimerTone->timerHandleDeinit ();
71
+ }
72
+ if (TimerTone_pinInfo.pin != NC) {
73
+ pin_function (TimerTone_pinInfo.pin , STM_PIN_DATA (STM_MODE_INPUT, GPIO_NOPULL, 0 ));
74
+ TimerTone_pinInfo.pin = NC;
75
+ }
77
76
}
78
77
79
78
static void timerTonePinInit (PinName p, uint32_t frequency, uint32_t duration)
80
79
{
81
80
uint32_t timFreq = 2 * frequency;
82
81
83
- TimerTone_pinInfo. pin = p;
84
-
85
- if (frequency > MAX_FREQ) {
86
- return ;
87
- }
82
+ if (frequency <= MAX_FREQ) {
83
+ if (frequency == 0 ) {
84
+ timerTonePinDeinit ();
85
+ } else {
86
+ TimerTone_pinInfo. pin = p;
88
87
89
- TimerTone_pinInfo.state = 0 ;
88
+ // Calculate the toggle count
89
+ if (duration > 0 ) {
90
+ TimerTone_pinInfo.count = ((timFreq * duration) / 1000 );
91
+ } else {
92
+ TimerTone_pinInfo.count = -1 ;
93
+ }
90
94
91
- if (frequency == 0 ) {
92
- timerTonePinDeinit ();
93
- return ;
94
- }
95
+ pin_function (TimerTone_pinInfo.pin , STM_PIN_DATA (STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0 ));
95
96
96
- // Calculate the toggle count
97
- if (duration > 0 ) {
98
- TimerTone_pinInfo. count = ((timFreq * duration) / 1000 );
99
- } else {
100
- TimerTone_pinInfo. count = - 1 ;
97
+ TimerTone-> setMode ( 1 , TIMER_OUTPUT_COMPARE, NC);
98
+ TimerTone-> setOverflow (timFreq, HERTZ_FORMAT);
99
+ TimerTone-> attachInterrupt (tonePeriodElapsedCallback );
100
+ TimerTone-> resume ();
101
+ }
101
102
}
102
-
103
- pin_function (TimerTone_pinInfo.pin , STM_PIN_DATA (STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0 ));
104
-
105
- TimerTone->setMode (1 , TIMER_OUTPUT_COMPARE, NC);
106
- TimerTone->setOverflow (timFreq, HERTZ_FORMAT);
107
- TimerTone->attachInterrupt (tonePeriodElapsedCallback);
108
- TimerTone->resume ();
109
103
}
110
104
111
105
// frequency (in hertz) and duration (in milliseconds).
@@ -118,30 +112,25 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
118
112
}
119
113
120
114
if (p != NC) {
121
- if ((g_lastPin == NC) || (g_lastPin == p)) {
122
-
115
+ if ((TimerTone_pinInfo.pin == NC) || (TimerTone_pinInfo.pin == p)) {
123
116
timerTonePinInit (p, frequency, duration);
124
- g_lastPin = p;
125
117
}
126
118
}
127
119
}
128
120
129
121
void noTone (uint8_t _pin, bool destruct)
130
122
{
131
123
PinName p = digitalPinToPinName (_pin);
132
- if (p != NC) {
124
+ if (( p != NC) && (TimerTone_pinInfo. pin == p) ) {
133
125
timerTonePinDeinit ();
134
- g_lastPin = NC;
135
- }
136
- if (destruct) {
137
- if (TimerTone != NULL ) {
126
+
127
+ if ((destruct) && (TimerTone != NULL )) {
138
128
delete (TimerTone);
139
129
TimerTone = NULL ;
140
130
}
141
131
}
142
132
}
143
133
#else
144
-
145
134
#warning "TIMER_TONE or HAL_TIM_MODULE_ENABLED not defined"
146
135
void tone (uint8_t _pin, unsigned int frequency, unsigned long duration)
147
136
{
@@ -154,5 +143,4 @@ void noTone(uint8_t _pin)
154
143
{
155
144
UNUSED (_pin);
156
145
}
157
-
158
146
#endif /* HAL_TIM_MODULE_ENABLED && TIMER_TONE */
0 commit comments