Skip to content

Commit adbd79e

Browse files
fpistmABOSTM
authored andcommitted
[Tone] hardening
Remove field 'state' from timerPinInfo_t using digit_io_toggle api. Remove global variable 'g_lastPin' as TimerTone_pinInfo contains same info. Add some check to avoid NULL or NC usage. Avoid return in void returning function. Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 4817b86 commit adbd79e

File tree

1 file changed

+30
-42
lines changed

1 file changed

+30
-42
lines changed

cores/arduino/Tone.cpp

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,13 @@
2929
typedef struct {
3030
PinName pin;
3131
int32_t count;
32-
uint8_t state;
3332
} timerPinInfo_t;
3433

3534
static void timerTonePinInit(PinName p, uint32_t frequency, uint32_t duration);
3635
static void tonePeriodElapsedCallback(HardwareTimer *HT);
37-
static timerPinInfo_t TimerTone_pinInfo;
38-
36+
static timerPinInfo_t TimerTone_pinInfo = {NC, 0};
3937
static HardwareTimer *TimerTone = NULL;
4038

41-
PinName g_lastPin = NC;
42-
4339
/**
4440
* @brief Tone Period elapsed callback in non-blocking mode
4541
* @param htim : timer handle
@@ -55,15 +51,13 @@ static void tonePeriodElapsedCallback(HardwareTimer *HT)
5551
if (TimerTone_pinInfo.count > 0) {
5652
TimerTone_pinInfo.count--;
5753
}
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));
6055
} else {
6156
digital_io_write(port, STM_LL_GPIO_PIN(TimerTone_pinInfo.pin), 0);
6257
}
6358
}
6459
}
6560

66-
6761
/**
6862
* @brief This function will reset the tone timer
6963
* @param port : pointer to port
@@ -72,40 +66,40 @@ static void tonePeriodElapsedCallback(HardwareTimer *HT)
7266
*/
7367
static void timerTonePinDeinit()
7468
{
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+
}
7776
}
7877

7978
static void timerTonePinInit(PinName p, uint32_t frequency, uint32_t duration)
8079
{
8180
uint32_t timFreq = 2 * frequency;
8281

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;
8887

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+
}
9094

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));
9596

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+
}
101102
}
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();
109103
}
110104

111105
// frequency (in hertz) and duration (in milliseconds).
@@ -118,30 +112,25 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
118112
}
119113

120114
if (p != NC) {
121-
if ((g_lastPin == NC) || (g_lastPin == p)) {
122-
115+
if ((TimerTone_pinInfo.pin == NC) || (TimerTone_pinInfo.pin == p)) {
123116
timerTonePinInit(p, frequency, duration);
124-
g_lastPin = p;
125117
}
126118
}
127119
}
128120

129121
void noTone(uint8_t _pin, bool destruct)
130122
{
131123
PinName p = digitalPinToPinName(_pin);
132-
if (p != NC) {
124+
if ((p != NC) && (TimerTone_pinInfo.pin == p)) {
133125
timerTonePinDeinit();
134-
g_lastPin = NC;
135-
}
136-
if (destruct) {
137-
if (TimerTone != NULL) {
126+
127+
if ((destruct) && (TimerTone != NULL)) {
138128
delete (TimerTone);
139129
TimerTone = NULL;
140130
}
141131
}
142132
}
143133
#else
144-
145134
#warning "TIMER_TONE or HAL_TIM_MODULE_ENABLED not defined"
146135
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
147136
{
@@ -154,5 +143,4 @@ void noTone(uint8_t _pin)
154143
{
155144
UNUSED(_pin);
156145
}
157-
158146
#endif /* HAL_TIM_MODULE_ENABLED && TIMER_TONE */

0 commit comments

Comments
 (0)