Skip to content

Commit 0af3bb4

Browse files
committed
fix(gpio): INT0 broken on ATtiny85
make the ISCx0/ISCx1 bit offsets part of the `AVRExternalInterrupt` configuration object.
1 parent 79feeab commit 0af3bb4

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

src/peripherals/gpio.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@ import { AVRInterruptConfig, CPU } from '../cpu/cpu';
99
import { u8 } from '../types';
1010

1111
export interface AVRExternalInterrupt {
12-
EICRA: u8;
13-
EICRB: u8;
12+
/** either EICRA or EICRB, depending on which register holds the ISCx0/ISCx1 bits for this interrupt */
13+
EICR: u8;
1414
EIMSK: u8;
1515
EIFR: u8;
16+
17+
/* Offset of the ISCx0/ISCx1 bits in the EICRx register */
18+
iscOffset: u8;
19+
20+
/** Bit index in the EIMSK / EIFR registers */
1621
index: u8; // 0..7
22+
23+
/** Interrupt vector index */
1724
interrupt: u8;
1825
}
1926

@@ -39,20 +46,20 @@ export interface AVRPortConfig {
3946
}
4047

4148
export const INT0: AVRExternalInterrupt = {
42-
EICRA: 0x69,
43-
EICRB: 0,
49+
EICR: 0x69,
4450
EIMSK: 0x3d,
4551
EIFR: 0x3c,
4652
index: 0,
53+
iscOffset: 0,
4754
interrupt: 2,
4855
};
4956

5057
export const INT1: AVRExternalInterrupt = {
51-
EICRA: 0x69,
52-
EICRB: 0,
58+
EICR: 0x69,
5359
EIMSK: 0x3d,
5460
EIFR: 0x3c,
5561
index: 1,
62+
iscOffset: 2,
5663
interrupt: 4,
5764
};
5865

@@ -254,10 +261,10 @@ export class AVRIOPort {
254261
}
255262
: null
256263
);
257-
const EICRA = externalInterrupts.find((item) => item && item.EICRA)?.EICRA ?? 0;
258-
this.attachInterruptHook(EICRA);
259-
const EICRB = externalInterrupts.find((item) => item && item.EICRB)?.EICRB ?? 0;
260-
this.attachInterruptHook(EICRB);
264+
const EICR = new Set(externalInterrupts.map((item) => item?.EICR));
265+
for (const EICRx of EICR) {
266+
this.attachInterruptHook(EICRx || 0);
267+
}
261268
const EIMSK = externalInterrupts.find((item) => item && item.EIMSK)?.EIMSK ?? 0;
262269
this.attachInterruptHook(EIMSK, 'mask');
263270
const EIFR = externalInterrupts.find((item) => item && item.EIFR)?.EIFR ?? 0;
@@ -394,11 +401,9 @@ export class AVRIOPort {
394401
const externalConfig = externalInterrupts[pin];
395402
const external = externalInts[pin];
396403
if (external && externalConfig) {
397-
const { index, EICRA, EICRB, EIMSK } = externalConfig;
404+
const { EIMSK, index, EICR, iscOffset } = externalConfig;
398405
if (cpu.data[EIMSK] & (1 << index)) {
399-
const configRegister = index >= 4 ? EICRB : EICRA;
400-
const configShift = (index % 4) * 2;
401-
const configuration = (cpu.data[configRegister] >> configShift) & 0x3;
406+
const configuration = (cpu.data[EICR] >> iscOffset) & 0x3;
402407
let generateInterrupt = false;
403408
external.constant = false;
404409
switch (configuration) {
@@ -469,13 +474,11 @@ export class AVRIOPort {
469474
continue;
470475
}
471476
const pinValue = !!(this.lastPin & (1 << pin));
472-
const { index, EICRA, EICRB, EIMSK, EIFR, interrupt } = external;
477+
const { EIFR, EIMSK, index, EICR, iscOffset, interrupt } = external;
473478
if (!(cpu.data[EIMSK] & (1 << index)) || pinValue) {
474479
continue;
475480
}
476-
const configRegister = index >= 4 ? EICRB : EICRA;
477-
const configShift = (index % 4) * 2;
478-
const configuration = (cpu.data[configRegister] >> configShift) & 0x3;
481+
const configuration = (cpu.data[EICR] >> iscOffset) & 0x3;
479482
if (configuration === InterruptMode.LowLevel) {
480483
cpu.queueInterrupt({
481484
address: interrupt,

0 commit comments

Comments
 (0)