Skip to content

Commit 47714c3

Browse files
committed
Merge pull request #102 from sgk/leonardoTone
Fix for tone() on Leonardo.
2 parents 33883ab + f60f17f commit 47714c3

File tree

3 files changed

+90
-11
lines changed

3 files changed

+90
-11
lines changed

hardware/arduino/cores/arduino/Arduino.h

+65
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,71 @@
1111

1212
#include "binary.h"
1313

14+
// Workaround for wrong definitions in "iom32u4.h".
15+
// This should be fixed in the AVR toolchain.
16+
#ifdef __AVR_ATmega32U4__
17+
#undef UHCON
18+
#undef UHINT
19+
#undef UHIEN
20+
#undef UHADDR
21+
#undef UHFNUM
22+
#undef UHFNUML
23+
#undef UHFNUMH
24+
#undef UHFLEN
25+
#undef UPINRQX
26+
#undef UPINTX
27+
#undef UPNUM
28+
#undef UPRST
29+
#undef UPCONX
30+
#undef UPCFG0X
31+
#undef UPCFG1X
32+
#undef UPSTAX
33+
#undef UPCFG2X
34+
#undef UPIENX
35+
#undef UPDATX
36+
#undef TCCR2A
37+
#undef WGM20
38+
#undef WGM21
39+
#undef COM2B0
40+
#undef COM2B1
41+
#undef COM2A0
42+
#undef COM2A1
43+
#undef TCCR2B
44+
#undef CS20
45+
#undef CS21
46+
#undef CS22
47+
#undef WGM22
48+
#undef FOC2B
49+
#undef FOC2A
50+
#undef TCNT2
51+
#undef TCNT2_0
52+
#undef TCNT2_1
53+
#undef TCNT2_2
54+
#undef TCNT2_3
55+
#undef TCNT2_4
56+
#undef TCNT2_5
57+
#undef TCNT2_6
58+
#undef TCNT2_7
59+
#undef OCR2A
60+
#undef OCR2_0
61+
#undef OCR2_1
62+
#undef OCR2_2
63+
#undef OCR2_3
64+
#undef OCR2_4
65+
#undef OCR2_5
66+
#undef OCR2_6
67+
#undef OCR2_7
68+
#undef OCR2B
69+
#undef OCR2_0
70+
#undef OCR2_1
71+
#undef OCR2_2
72+
#undef OCR2_3
73+
#undef OCR2_4
74+
#undef OCR2_5
75+
#undef OCR2_6
76+
#undef OCR2_7
77+
#endif
78+
1479
#ifdef __cplusplus
1580
extern "C"{
1681
#endif

hardware/arduino/cores/arduino/Tone.cpp

+23-8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Version Modified By Date Comments
2929
09/11/25 Fixed timer0 from being excluded
3030
0006 D Mellis 09/12/29 Replaced objects with functions
3131
0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register
32+
0008 S Kanemoto 12/06/22 Fixed for Leonardo by @maris_HY
3233
*************************************************/
3334

3435
#include <avr/interrupt.h>
@@ -85,24 +86,34 @@ volatile uint8_t timer5_pin_mask;
8586
#endif
8687

8788

88-
// MLS: This does not make sense, the 3 options are the same
8989
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
9090

9191
#define AVAILABLE_TONE_PINS 1
92+
#define USE_TIMER2
9293

9394
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 3, 4, 5, 1, 0 */ };
9495
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255, 255, 255, 255, 255 */ };
9596

9697
#elif defined(__AVR_ATmega8__)
9798

9899
#define AVAILABLE_TONE_PINS 1
100+
#define USE_TIMER2
99101

100102
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1 */ };
101103
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
102104

105+
#elif defined(__AVR_ATmega32U4__)
106+
107+
#define AVAILABLE_TONE_PINS 1
108+
#define USE_TIMER3
109+
110+
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 3 /*, 1 */ };
111+
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255 /*, 255 */ };
112+
103113
#else
104114

105115
#define AVAILABLE_TONE_PINS 1
116+
#define USE_TIMER2
106117

107118
// Leave timer 0 to last.
108119
const uint8_t PROGMEM tone_pin_to_timer_PGM[] = { 2 /*, 1, 0 */ };
@@ -480,8 +491,7 @@ void noTone(uint8_t _pin)
480491
digitalWrite(_pin, 0);
481492
}
482493

483-
#if 0
484-
#if !defined(__AVR_ATmega8__)
494+
#ifdef USE_TIMER0
485495
ISR(TIMER0_COMPA_vect)
486496
{
487497
if (timer0_toggle_count != 0)
@@ -501,6 +511,7 @@ ISR(TIMER0_COMPA_vect)
501511
#endif
502512

503513

514+
#ifdef USE_TIMER1
504515
ISR(TIMER1_COMPA_vect)
505516
{
506517
if (timer1_toggle_count != 0)
@@ -520,6 +531,7 @@ ISR(TIMER1_COMPA_vect)
520531
#endif
521532

522533

534+
#ifdef USE_TIMER2
523535
ISR(TIMER2_COMPA_vect)
524536
{
525537

@@ -541,12 +553,10 @@ ISR(TIMER2_COMPA_vect)
541553
// *timer2_pin_port &= ~(timer2_pin_mask); // keep pin low after stop
542554
}
543555
}
556+
#endif
544557

545558

546-
547-
//#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
548-
#if 0
549-
559+
#ifdef USE_TIMER3
550560
ISR(TIMER3_COMPA_vect)
551561
{
552562
if (timer3_toggle_count != 0)
@@ -563,7 +573,10 @@ ISR(TIMER3_COMPA_vect)
563573
*timer3_pin_port &= ~(timer3_pin_mask); // keep pin low after stop
564574
}
565575
}
576+
#endif
577+
566578

579+
#ifdef USE_TIMER4
567580
ISR(TIMER4_COMPA_vect)
568581
{
569582
if (timer4_toggle_count != 0)
@@ -580,7 +593,10 @@ ISR(TIMER4_COMPA_vect)
580593
*timer4_pin_port &= ~(timer4_pin_mask); // keep pin low after stop
581594
}
582595
}
596+
#endif
583597

598+
599+
#ifdef USE_TIMER5
584600
ISR(TIMER5_COMPA_vect)
585601
{
586602
if (timer5_toggle_count != 0)
@@ -597,5 +613,4 @@ ISR(TIMER5_COMPA_vect)
597613
*timer5_pin_port &= ~(timer5_pin_mask); // keep pin low after stop
598614
}
599615
}
600-
601616
#endif

libraries/Servo/Servo.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,8 @@
6565
typedef enum { _timer5, _timer1, _timer3, _timer4, _Nbr_16timers } timer16_Sequence_t ;
6666

6767
#elif defined(__AVR_ATmega32U4__)
68-
#define _useTimer3
6968
#define _useTimer1
70-
typedef enum { _timer3, _timer1, _Nbr_16timers } timer16_Sequence_t ;
69+
typedef enum { _timer1, _Nbr_16timers } timer16_Sequence_t ;
7170

7271
#elif defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB1286__)
7372
#define _useTimer3
@@ -124,4 +123,4 @@ class Servo
124123
int8_t max; // maximum is this value times 4 added to MAX_PULSE_WIDTH
125124
};
126125

127-
#endif
126+
#endif

0 commit comments

Comments
 (0)