Skip to content

Commit 9a27070

Browse files
committed
Added directive for AVR boards
1 parent f33745e commit 9a27070

9 files changed

+894
-741
lines changed

src/PinChangeInterrupt.cpp

Lines changed: 115 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ THE SOFTWARE.
2323

2424
#include "PinChangeInterrupt.h"
2525

26+
#ifdef ARDUINO_ARCH_AVR
27+
2628
// manually include cpp files here to save flash if only 1 ISR is present
2729
// or if the user knows he just wants to compile all enabled ports.
2830
#if defined(PCINT_ALINKAGE) && defined(PCINT_COMPILE_ENABLED_ISR)
@@ -82,167 +84,177 @@ void PinChangeInterruptEventPCINT31(void) __attribute__((weak, alias("pcint_null
8284

8385
// useless function for weak implemented/not used functions, extern c needed for the alias
8486
extern "C" {
85-
void pcint_null_callback(void) {
86-
// useless
87-
}
87+
void pcint_null_callback(void)
88+
{
89+
// useless
90+
}
8891
}
8992

9093
//================================================================================
9194
// PinChangeInterrupt User Functions
9295
//================================================================================
9396

9497
// variables to save the last port states and the interrupt settings
95-
uint8_t oldPorts[PCINT_NUM_USED_PORTS] = { 0 };
96-
uint8_t fallingPorts[PCINT_NUM_USED_PORTS] = { 0 };
97-
uint8_t risingPorts[PCINT_NUM_USED_PORTS] = { 0 };
98+
uint8_t oldPorts[PCINT_NUM_USED_PORTS] = {0};
99+
uint8_t fallingPorts[PCINT_NUM_USED_PORTS] = {0};
100+
uint8_t risingPorts[PCINT_NUM_USED_PORTS] = {0};
98101

99-
void enablePinChangeInterruptHelper(const uint8_t pcintPort, const uint8_t pcintMask, const uint8_t arrayPos){
100-
// Update the old state to the actual state
101-
switch(pcintPort){
102+
void enablePinChangeInterruptHelper(const uint8_t pcintPort, const uint8_t pcintMask, const uint8_t arrayPos)
103+
{
104+
// Update the old state to the actual state
105+
switch (pcintPort)
106+
{
102107
#ifdef PCINT_INPUT_PORT0_USED
103-
case 0:
104-
oldPorts[arrayPos] = PCINT_INPUT_PORT0;
105-
break;
108+
case 0:
109+
oldPorts[arrayPos] = PCINT_INPUT_PORT0;
110+
break;
106111
#endif
107112
#ifdef PCINT_INPUT_PORT1_USED
108-
case 1:
109-
oldPorts[arrayPos] = PCINT_INPUT_PORT1;
110-
break;
113+
case 1:
114+
oldPorts[arrayPos] = PCINT_INPUT_PORT1;
115+
break;
111116
#endif
112117
#ifdef PCINT_INPUT_PORT2_USED
113-
case 2:
114-
oldPorts[arrayPos] = PCINT_INPUT_PORT2;
115-
break;
118+
case 2:
119+
oldPorts[arrayPos] = PCINT_INPUT_PORT2;
120+
break;
116121
#endif
117122
#ifdef PCINT_INPUT_PORT3_USED
118-
case 3:
119-
oldPorts[arrayPos] = PCINT_INPUT_PORT3;
120-
break;
123+
case 3:
124+
oldPorts[arrayPos] = PCINT_INPUT_PORT3;
125+
break;
121126
#endif
122-
}
127+
}
123128

124-
// Pin change mask registers decide which pins are ENABLE as triggers
129+
// Pin change mask registers decide which pins are ENABLE as triggers
125130
#ifdef PCMSK0
126131
#ifdef PCMSK1
127-
// Special case for Attinyx4 where PCMSK1 and PCMSK0 are not next to each other
128-
if(&PCMSK1 - &PCMSK0 == 1){
132+
// Special case for Attinyx4 where PCMSK1 and PCMSK0 are not next to each other
133+
if (&PCMSK1 - &PCMSK0 == 1)
134+
{
129135
#endif
130-
*(&PCMSK0 + pcintPort) |= pcintMask;
136+
*(&PCMSK0 + pcintPort) |= pcintMask;
131137
#ifdef PCMSK1
132-
}
133-
else{
134-
switch(pcintPort){
135-
case 0:
136-
PCMSK0 |= pcintMask;
137-
break;
138-
case 1:
139-
PCMSK1 |= pcintMask;
140-
break;
138+
}
139+
else
140+
{
141+
switch (pcintPort)
142+
{
143+
case 0:
144+
PCMSK0 |= pcintMask;
145+
break;
146+
case 1:
147+
PCMSK1 |= pcintMask;
148+
break;
141149
#ifdef PCMSK2
142-
case 2:
143-
PCMSK2 |= pcintMask;
144-
break;
150+
case 2:
151+
PCMSK2 |= pcintMask;
152+
break;
145153
#endif
146154
#ifdef PCMSK3
147-
case 3:
148-
PCMSK3 |= pcintMask;
149-
break;
155+
case 3:
156+
PCMSK3 |= pcintMask;
157+
break;
150158
#endif
151-
}
152159
}
160+
}
153161
#endif
154162
#elif defined(PCMSK)
155-
*(&PCMSK + pcintPort) |= pcintMask;
163+
*(&PCMSK + pcintPort) |= pcintMask;
156164
#endif
157165

158-
// PCICR: Pin Change Interrupt Control Register - enables interrupt vectors
166+
// PCICR: Pin Change Interrupt Control Register - enables interrupt vectors
159167
#ifdef PCICR
160-
PCICR |= (1 << (pcintPort + PCIE0));
168+
PCICR |= (1 << (pcintPort + PCIE0));
161169
#elif defined(GIMSK) && defined(PCIE0) /* e.g. ATtiny X4 */
162-
GIMSK |= (1 << (pcintPort + PCIE0));
163-
#elif defined(GIMSK) && defined(PCIE) /* e.g. ATtiny X5 */
164-
GIMSK |= (1 << (pcintPort + PCIE));
170+
GIMSK |= (1 << (pcintPort + PCIE0));
171+
#elif defined(GIMSK) && defined(PCIE) /* e.g. ATtiny X5 */
172+
GIMSK |= (1 << (pcintPort + PCIE));
165173
#else
166174
#error MCU has no such a register
167175
#endif
168176
}
169177

170-
void disablePinChangeInterruptHelper(const uint8_t pcintPort, const uint8_t pcintMask) {
171-
bool disable = false;
178+
void disablePinChangeInterruptHelper(const uint8_t pcintPort, const uint8_t pcintMask)
179+
{
180+
bool disable = false;
172181
#ifdef PCMSK0
173182
#ifdef PCMSK1
174-
// Special case for Attinyx4 where PCMSK1 and PCMSK0 are not next to each other
175-
if(&PCMSK1 - &PCMSK0 == 1){
183+
// Special case for Attinyx4 where PCMSK1 and PCMSK0 are not next to each other
184+
if (&PCMSK1 - &PCMSK0 == 1)
185+
{
176186
#endif
177-
// disable the mask.
178-
*(&PCMSK0 + pcintPort) &= ~pcintMask;
187+
// disable the mask.
188+
*(&PCMSK0 + pcintPort) &= ~pcintMask;
179189

180-
// if that's the last one, disable the interrupt.
181-
if (*(&PCMSK0 + pcintPort) == 0)
182-
disable = true;
190+
// if that's the last one, disable the interrupt.
191+
if (*(&PCMSK0 + pcintPort) == 0)
192+
disable = true;
183193
#ifdef PCMSK1
184-
}
185-
else{
186-
switch(pcintPort){
187-
case 0:
188-
// disable the mask.
189-
PCMSK0 &= ~pcintMask;
194+
}
195+
else
196+
{
197+
switch (pcintPort)
198+
{
199+
case 0:
200+
// disable the mask.
201+
PCMSK0 &= ~pcintMask;
190202

191-
// if that's the last one, disable the interrupt.
192-
if (!PCMSK0)
193-
disable = true;
194-
break;
195-
case 1:
196-
// disable the mask.
197-
PCMSK1 &= ~pcintMask;
203+
// if that's the last one, disable the interrupt.
204+
if (!PCMSK0)
205+
disable = true;
206+
break;
207+
case 1:
208+
// disable the mask.
209+
PCMSK1 &= ~pcintMask;
198210

199-
// if that's the last one, disable the interrupt.
200-
if (!PCMSK1)
201-
disable = true;
202-
break;
211+
// if that's the last one, disable the interrupt.
212+
if (!PCMSK1)
213+
disable = true;
214+
break;
203215
#ifdef PCMSK2
204-
case 2:
205-
// disable the mask.
206-
PCMSK2 &= ~pcintMask;
216+
case 2:
217+
// disable the mask.
218+
PCMSK2 &= ~pcintMask;
207219

208-
// if that's the last one, disable the interrupt.
209-
if (!PCMSK2)
210-
disable = true;
211-
break;
220+
// if that's the last one, disable the interrupt.
221+
if (!PCMSK2)
222+
disable = true;
223+
break;
212224
#endif
213225
#ifdef PCMSK3
214-
case 3:
215-
// disable the mask.
216-
PCMSK3 &= ~pcintMask;
226+
case 3:
227+
// disable the mask.
228+
PCMSK3 &= ~pcintMask;
217229

218-
// if that's the last one, disable the interrupt.
219-
if (!PCMSK3)
220-
disable = true;
221-
break;
230+
// if that's the last one, disable the interrupt.
231+
if (!PCMSK3)
232+
disable = true;
233+
break;
222234
#endif
223-
}
224235
}
236+
}
225237
#endif
226238
#elif defined(PCMSK)
227-
// disable the mask.
228-
*(&PCMSK + pcintPort) &= ~pcintMask;
239+
// disable the mask.
240+
*(&PCMSK + pcintPort) &= ~pcintMask;
229241

230-
// if that's the last one, disable the interrupt.
231-
if (*(&PCMSK + pcintPort) == 0)
232-
disable = true;
242+
// if that's the last one, disable the interrupt.
243+
if (*(&PCMSK + pcintPort) == 0)
244+
disable = true;
233245
#endif
234-
if(disable)
235-
{
246+
if (disable)
247+
{
236248
#ifdef PCICR
237-
PCICR &= ~(1 << (pcintPort + PCIE0));
249+
PCICR &= ~(1 << (pcintPort + PCIE0));
238250
#elif defined(GIMSK) && defined(PCIE0) /* e.g. ATtiny X4 */
239-
GIMSK &= ~(1 << (pcintPort + PCIE0));
240-
#elif defined(GIMSK) && defined(PCIE) /* e.g. ATtiny X5 */
241-
GIMSK &= ~(1 << (pcintPort + PCIE));
251+
GIMSK &= ~(1 << (pcintPort + PCIE0));
252+
#elif defined(GIMSK) && defined(PCIE) /* e.g. ATtiny X5 */
253+
GIMSK &= ~(1 << (pcintPort + PCIE));
242254
#else
243255
#error MCU has no such a register
244256
#endif
245-
}
257+
}
246258
}
247259

248260
/*
@@ -325,3 +337,5 @@ pop r1
325337
reti
326338
}
327339
*/
340+
341+
#endif

0 commit comments

Comments
 (0)