Skip to content

Commit 7d409db

Browse files
Roy, Elizabethfacchinm
authored andcommitted
Moved helper functions
1 parent 59b03f1 commit 7d409db

File tree

1 file changed

+57
-54
lines changed

1 file changed

+57
-54
lines changed

cores/arduino/Tone.cpp

Lines changed: 57 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -103,33 +103,11 @@ volatile uint8_t *timerb2_outtgl_reg;
103103
volatile uint8_t timerb2_bit_mask;
104104
#endif
105105

106-
static int8_t toneBegin(uint8_t _pin)
107-
{
108-
int8_t _timer = -1;
109-
110-
// If pin already being used for tone, return the associated timer
111-
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
112-
if (tone_pins[i] == _pin) {
113-
return pgm_read_byte(tone_pin_to_timer_PGM + i);
114-
}
115-
}
116-
117-
// If not, search for an unused timer
118-
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
119-
if (tone_pins[i] == NOT_A_PIN) {
120-
tone_pins[i] = _pin;
121-
_timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
122-
break;
123-
}
124-
}
125-
126-
return _timer;
127-
}
128-
129-
106+
// helper functions
107+
static uint8_t toneBegin(uint8_t _pin);
108+
static void disableTimer(uint8_t _timer);
130109

131110
// frequency (in hertz) and duration (in milliseconds).
132-
133111
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
134112
{
135113
long toggle_count = 0;
@@ -221,43 +199,35 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
221199
}
222200
}
223201

224-
/* Works for all timers -- the timer being disabled will go back to the
225-
configuration it had to output PWM for analogWrite() */
226-
void disableTimer(uint8_t _timer)
202+
// helper function for tone()
203+
static uint8_t toneBegin(uint8_t _pin)
227204
{
228-
// Reinit back to producing PWM -- timer will be type B
229-
230-
// Get timer struct
231-
TCB_t *timer_B = ((TCB_t *)&TCB0 + (_timer - TIMERB0));
232-
233-
// Disable interrupt
234-
timer_B->INTCTRL = 0;
235-
236-
// Disable timer
237-
timer_B->CTRLA = 0;
238-
239-
// RESTORE PWM FUNCTIONALITY:
240-
241-
/* 8 bit PWM mode, but do not enable output yet, will do in analogWrite() */
242-
timer_B->CTRLB = (TCB_CNTMODE_PWM8_gc);
205+
uint8_t _timer = NOT_ON_TIMER;
243206

244-
/* Assign 8-bit period */
245-
timer_B->CCMPL = PWM_TIMER_PERIOD;
246-
247-
/* default duty 50%, set when output enabled */
248-
timer_B->CCMPH = PWM_TIMER_COMPARE;
207+
// If pin already being used for tone, return the associated timer
208+
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
209+
if (tone_pins[i] == _pin) {
210+
return pgm_read_byte(tone_pin_to_timer_PGM + i);
211+
}
212+
}
213+
214+
// If not, search for an unused timer
215+
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
216+
if (tone_pins[i] == NOT_A_PIN) {
217+
tone_pins[i] = _pin;
218+
_timer = pgm_read_byte(tone_pin_to_timer_PGM + i);
219+
break;
220+
}
221+
}
249222

250-
/* Use TCA clock (250kHz) and enable */
251-
/* (sync update commented out, might try to synchronize later */
252-
timer_B->CTRLA = (TCB_CLKSEL_CLKTCA_gc) | (TCB_ENABLE_bm);
253-
223+
return _timer;
254224
}
255225

256226
// pin which currently is being used for a tone
257227
void noTone(uint8_t _pin)
258228
{
259229
int8_t _timer = NOT_ON_TIMER;
260-
230+
261231
// Find timer associated with pin
262232
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
263233

@@ -277,14 +247,47 @@ void noTone(uint8_t _pin)
277247

278248
if(_timer > NOT_ON_TIMER){
279249
disableTimer(_timer);
280-
250+
281251
// Keep pin low after disabling of timer
282252
digitalWrite(_pin, LOW);
283253
}
284254

285255
}
286256

287257
// helper function for noTone()
258+
/* Works for all timers -- the timer being disabled will go back to the
259+
configuration it had to output PWM for analogWrite() */
260+
static void disableTimer(uint8_t _timer)
261+
{
262+
// Reinit back to producing PWM -- timer will be type B
263+
264+
// Get timer struct
265+
TCB_t *timer_B = ((TCB_t *)&TCB0 + (_timer - TIMERB0));
266+
267+
// Disable interrupt
268+
timer_B->INTCTRL = 0;
269+
270+
// Disable timer
271+
timer_B->CTRLA = 0;
272+
273+
// RESTORE PWM FUNCTIONALITY:
274+
275+
/* 8 bit PWM mode, but do not enable output yet, will do in analogWrite() */
276+
timer_B->CTRLB = (TCB_CNTMODE_PWM8_gc);
277+
278+
/* Assign 8-bit period */
279+
timer_B->CCMPL = PWM_TIMER_PERIOD;
280+
281+
/* default duty 50%, set when output enabled */
282+
timer_B->CCMPH = PWM_TIMER_COMPARE;
283+
284+
/* Use TCA clock (250kHz) and enable */
285+
/* (sync update commented out, might try to synchronize later */
286+
timer_B->CTRLA = (TCB_CLKSEL_CLKTCA_gc) | (TCB_ENABLE_bm);
287+
288+
}
289+
290+
288291

289292
#ifdef USE_TIMERB0
290293
ISR(TCB0_INT_vect)

0 commit comments

Comments
 (0)