Skip to content

Fix for issue #292 #2961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 14, 2015
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions hardware/arduino/avr/cores/arduino/Tone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Version Modified By Date Comments
0006 D Mellis 09/12/29 Replaced objects with functions
0007 M Sproul 10/08/29 Changed #ifdefs from cpu to register
0008 S Kanemoto 12/06/22 Fixed for Leonardo by @maris_HY
0009 J Reucker 15/04/10 Issue #292 Fixed problems with ATmega8 (thanks to Pete62)
*************************************************/

#include <avr/interrupt.h>
Expand Down Expand Up @@ -296,13 +297,13 @@ void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
#if defined(TCCR0B)
if (_timer == 0)
{
TCCR0B = prescalarbits;
TCCR0B = (TCCR0B & 0b11111000) | prescalarbits;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems ok to me, although I've only read the datasheets and did't test it live

}
else
#endif
#if defined(TCCR2B)
{
TCCR2B = prescalarbits;
TCCR2B = (TCCR2B & 0b11111000) | prescalarbits;
}
#else
{
Expand Down Expand Up @@ -456,19 +457,19 @@ void disableTimer(uint8_t _timer)

#if defined(TIMSK3)
case 3:
TIMSK3 = 0;
TIMSK3 &= ~(1 << OCIE3A);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you test it with a Leonardo?
If so, it would be more coherent to use the notation bitWrite(TIMSK3, OCIE3A, 0); as in the previous case

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the problem seems to be TIMSK5... Just tried to compile for the Leonardo:

Arduino: 1.6.1 (Linux), Platine: "Arduino Leonardo"

/home/jan/arduino-1.6.1/hardware/arduino/avr/cores/arduino/Tone.cpp: In function 'void disableTimer(uint8_t)':
/home/jan/arduino-1.6.1/hardware/arduino/avr/cores/arduino/Tone.cpp:472:24: error: 'OCIE5A' was not declared in this scope
       TIMSK5 &= ~(1 << OCIE5A);
                        ^
Fehler beim Kompilieren.

I'll try to find a fix for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the ATmega32u4 datasheet, the Leonardo doesn't have a timer 5 and therefore does not have TIMSK5. Nevertheless, iom32u4.h defines TIMSK5, but without defining individual bits in it:

...
#define TIMSK4 _SFR_MEM8(0x72)
#define TOIE4 2
#define OCIE4B 5
#define OCIE4A 6
#define OCIE4D 7

#define TIMSK5 _SFR_MEM8(0x73)

#ifndef __ASSEMBLER__
#define ADC _SFR_MEM16(0x78)
#endif
#define ADCW _SFR_MEM16(0x78)
...

So the main question is why the CPU header defines TIMSK5 at all. But at least it explains the partly complicated #if defined(...) sequences in Tone.cpp, where the presence of all macros is carefully checked before actually doing something.

I'll push the new fix in a minute.

break;
#endif

#if defined(TIMSK4)
case 4:
TIMSK4 = 0;
TIMSK4 &= ~(1 << OCIE4A);
break;
#endif

#if defined(TIMSK5)
case 5:
TIMSK5 = 0;
TIMSK5 &= ~(1 << OCIE5A);
break;
#endif
}
Expand Down