Skip to content

Commit cb71d87

Browse files
committed
fix(gpio): Changing pinMode from INPUT to INPUT_PULLUP doesn't trigger listeners
close #62
1 parent 3770586 commit cb71d87

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

src/peripherals/gpio.spec.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('GPIO', () => {
99
cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f
1010
port.addListener(listener);
1111
cpu.writeData(0x25, 0x55); // PORTB <- 0x55
12-
expect(listener).toHaveBeenCalledWith(0x05, 0);
12+
expect(listener).toHaveBeenCalledWith(0x55, 0);
1313
expect(cpu.data[0x23]).toEqual(0x5); // PINB should return port value
1414
});
1515

@@ -20,7 +20,16 @@ describe('GPIO', () => {
2020
cpu.writeData(0x25, 0x55); // PORTB <- 0x55
2121
port.addListener(listener);
2222
cpu.writeData(0x24, 0xf0); // DDRB <- 0xf0
23-
expect(listener).toHaveBeenCalledWith(0x50, 0);
23+
expect(listener).toHaveBeenCalledWith(0x55, 0x55);
24+
});
25+
26+
it('should invoke the listeners when pullup register enabled (issue #62)', () => {
27+
const cpu = new CPU(new Uint16Array(1024));
28+
const port = new AVRIOPort(cpu, portBConfig);
29+
const listener = jest.fn();
30+
port.addListener(listener);
31+
cpu.writeData(0x25, 0x55); // PORTB <- 0x55
32+
expect(listener).toHaveBeenCalledWith(0x55, 0);
2433
});
2534

2635
it('should toggle the pin when writing to the PIN register', () => {
@@ -31,7 +40,7 @@ describe('GPIO', () => {
3140
cpu.writeData(0x24, 0x0f); // DDRB <- 0x0f
3241
cpu.writeData(0x25, 0x55); // PORTB <- 0x55
3342
cpu.writeData(0x23, 0x01); // PINB <- 0x0f
34-
expect(listener).toHaveBeenCalledWith(0x04, 0x5);
43+
expect(listener).toHaveBeenCalledWith(0x54, 0x55);
3544
expect(cpu.data[0x23]).toEqual(0x4); // PINB should return port value
3645
});
3746

src/peripherals/gpio.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ export class AVRIOPort {
204204
}
205205

206206
private writeGpio(value: u8, ddr: u8) {
207-
const newValue = ((value & this.overrideMask) | this.overrideValue) & ddr;
207+
const newValue = (((value & this.overrideMask) | this.overrideValue) & ddr) | (value & ~ddr);
208208
const prevValue = this.lastValue;
209209
if (newValue !== prevValue || ddr !== this.lastDdr) {
210210
this.lastValue = newValue;

0 commit comments

Comments
 (0)