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

Fix for issue #292 #2961

merged 2 commits into from
Apr 14, 2015

Conversation

jan-r
Copy link
Contributor

@jan-r jan-r commented Apr 10, 2015

This bugfix makes the Tone library work with ATmega8-based boards. See #292.

Rebased the bugfix from the original Google Code issue 292 to work with Arduino 1.6.x

Description of original fix provided by Pete62:
The later 8 bit AVR's use two registers (TCCRxA, TCCRxB) whereas the ATmega8 only uses a single register (TCCR2) to house the control bits for Timer 2. Bits were inadvertently being cleared.

Rebased the bugfix from the original Google Code issue arduino#292 to work with Arduino 1.6.x

Description of original fix provided by Pete62:
The later 8 bit AVR's use two registers (TCCRxA, TCCRxB) whereas the ATmega8 only uses a single register (TCCR2) to house the control bits for Timer 2.  Bits were inadvertently being cleared.
@cmaglie cmaglie added Component: Core Related to the code for the standard Arduino API feature request A request to make an enhancement (not a bug fix) Architecture: AVR Applies only to the AVR microcontrollers (Uno, etc.) labels Apr 13, 2015
@@ -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.

@facchinm facchinm self-assigned this Apr 13, 2015
Replaced direct register manipulation with calls to bitWrite(). Fixed TIMSK5 usage on Leonardo (as well as some other preprocessor statements).
@jan-r
Copy link
Contributor Author

jan-r commented Apr 13, 2015

I've fixed this now by extending these preprocessor checks. The toneMelody example was tested with these changes on the following platforms:

Compiles & runs on Leonardo
Compiles & runs on "bare" ATmega8 (\w 16 MHz XTAL)
Compiles on Uno
Compiles on Yun
Compiles on LilyPad
Compiles on Mini (\w ATmega168)

facchinm added a commit that referenced this pull request Apr 14, 2015
@facchinm facchinm merged commit 3bbdf49 into arduino:master Apr 14, 2015
@facchinm
Copy link
Member

Thank you @jan-r !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Architecture: AVR Applies only to the AVR microcontrollers (Uno, etc.) Component: Core Related to the code for the standard Arduino API feature request A request to make an enhancement (not a bug fix)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants