AVRTools
A Library for the AVR ATmega328 and ATmega2560 Microcontrollers
InterruptUtils.h
Go to the documentation of this file.
1 /*
2  InterruptUtils.h - Utilities for managing interrupts for
3  AVR ATMega328p (Arduino Uno) and ATMega2560 (Arduino Mega).
4  This is part of the AVRTools library.
5  Copyright (c) 2015 Igor Mikolic-Torreira. All right reserved.
6  Functions printNumber() and printFloat() adapted from Arduino code that
7  is Copyright (c) 2008 David A. Mellis and licensed under LGPL.
8 
9  This program is free software: you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  This program is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 
36 #ifndef InterruptUtils_h
37 #define InterruptUtils_h
38 
39 #include <stdint.h>
40 #include <stddef.h>
41 
42 #include <avr/io.h>
43 #include <util/atomic.h>
44 
45 
46 
47 
48 
54 namespace Interrupts
55 {
56 
63  class AllOff
64  {
65  public:
66 
72  {
73  mSreg = SREG;
74  cli();
75  }
76 
77 
84  {
85  // Turn on global interrupt, only if it was already on. Leave other bits alone.
86  if ( mSreg & static_cast<uint8_t>(1 << SREG_I) )
87  {
88  sei();
89  }
90  }
91 
92 
93  private:
94 
95  uint8_t mSreg;
96  };
97 
98 
99 
100 
101 
102 
103 
104 #if defined(__AVR_ATmega328P__)
105 #define kExternalInterruptMask 0x03
106 #elif defined(__AVR_ATmega2560__)
107 #define kExternalInterruptMask 0xFF
108 #else
109 #error "Undefined AVR processor type"
110 #endif
111 
112 
121  {
122  kExternalInterrupt0 = ( 1 << INT0 ),
123  kExternalInterrupt1 = ( 1 << INT1 ),
124 
125 #if defined(__AVR_ATmega2560__)
126  kExternalInterrupt2 = ( 1 << INT2 ),
127  kExternalInterrupt3 = ( 1 << INT3 ),
128  kExternalInterrupt4 = ( 1 << INT4 ),
129  kExternalInterrupt5 = ( 1 << INT5 ),
130  kExternalInterrupt6 = ( 1 << INT6 ),
131  kExternalInterrupt7 = ( 1 << INT7 ),
132 #endif
133 
134  kExternalInterruptAll = kExternalInterruptMask
135  };
136 
137 
138 
145  {
146  public:
147 
157  ExternalOff( uint8_t whichOnesToTurnOff = kExternalInterruptMask )
158  : mExternalInterruptsToSuppress( whichOnesToTurnOff & kExternalInterruptMask )
159  {
160  // Disable the selected interrupts
161  EIMSK &= ~(mExternalInterruptsToSuppress);
162  }
163 
164 
171  {
172  // Enable the selected interrupts
173  EIMSK |= mExternalInterruptsToSuppress;
174  }
175 
176 
177  private:
178 
179  uint8_t mExternalInterruptsToSuppress;
180  };
181 
182 
183 
184 
185 
186 
187 
188 #if defined(__AVR_ATmega328P__)
189 #define kPinChangeInterruptMask 0x07
190 #elif defined(__AVR_ATmega2560__)
191 #define kPinChangeInterruptMask 0x07
192 #else
193 #error "Undefined AVR processor type"
194 #endif
195 
196 
205  {
206  kPinChangeInterrupt0 = ( 1 << PCINT0 ),
207  kPinChangeInterrupt1 = ( 1 << PCINT1 ),
208  kPinChangeInterrupt2 = ( 1 << PCINT2 ),
209  kPinChangeInterruptAll = kPinChangeInterruptMask
210  };
211 
212 
213 
220  {
221  public:
222 
231  PinChangeOff( uint8_t whichOnesToTurnOff = kPinChangeInterruptMask )
232  : mPinChangeInterruptsToSuppress( whichOnesToTurnOff & kPinChangeInterruptMask )
233  {
234  // Disable the selected interrupts
235  PCICR &= ~(mPinChangeInterruptsToSuppress);
236  }
237 
238 
244  {
245  // Enable the selected interrupts
246  PCICR |= mPinChangeInterruptsToSuppress;
247  }
248 
249 
250  private:
251 
252  uint8_t mPinChangeInterruptsToSuppress;
253  };
254 
255 
256 
257 }; // End namespace
258 
259 
260 #endif
261 
~ExternalOff()
Re-enable the selected external interrupts.
Definition: InterruptUtils.h:170
All external interrupts.
Definition: InterruptUtils.h:134
PinChangeOff(uint8_t whichOnesToTurnOff=kPinChangeInterruptMask)
Suppress some or all of the pin change interrupts when the object is instantiated.
Definition: InterruptUtils.h:231
Pin change interrupt 1.
Definition: InterruptUtils.h:207
ExternalOff(uint8_t whichOnesToTurnOff=kExternalInterruptMask)
Suppress some or all of the external interrupts when the object is instantiated.
Definition: InterruptUtils.h:157
Pin change interrupt 0.
Definition: InterruptUtils.h:206
~AllOff()
Re-enable interrupts, restoring the interrupt state as it was when the object was instantiated...
Definition: InterruptUtils.h:83
External interrupt 0.
Definition: InterruptUtils.h:122
External interrupt 6 (ATmega2560 only)
Definition: InterruptUtils.h:130
External interrupt 1.
Definition: InterruptUtils.h:123
All pin change interrupts.
Definition: InterruptUtils.h:209
This namespace bundles various utility classes designed to suppress selected interrupts using the RAI...
Definition: InterruptUtils.h:54
Pin change interrupt 2.
Definition: InterruptUtils.h:208
This class defines an object that disables selected pin change interrupts during its lifetime...
Definition: InterruptUtils.h:219
External interrupt 4 (ATmega2560 only)
Definition: InterruptUtils.h:128
~PinChangeOff()
Re-enable the selected pin change interrupts.
Definition: InterruptUtils.h:243
PinChangeInterrupts
This enum lists the pin change interrupts that can be suppressed (disabled). To pass more than one pi...
Definition: InterruptUtils.h:204
AllOff()
Suppress all interrupts when the object is instantiated.
Definition: InterruptUtils.h:71
This class defines an object that disables selected external interrupts during its lifetime...
Definition: InterruptUtils.h:144
External interrupt 7 (ATmega2560 only)
Definition: InterruptUtils.h:131
External interrupt 2 (ATmega2560 only)
Definition: InterruptUtils.h:126
External interrupt 5 (ATmega2560 only)
Definition: InterruptUtils.h:129
This class defines an object that disables all interrupts during its lifetime. Interrupt state is res...
Definition: InterruptUtils.h:63
ExternalInterrupts
This enum lists the external interrupts that can be suppressed (disabled). To pass more than one exte...
Definition: InterruptUtils.h:120
External interrupt 3 (ATmega2560 only)
Definition: InterruptUtils.h:127