aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/gpio.spec.ts14
-rw-r--r--src/gpio.ts3
2 files changed, 16 insertions, 1 deletions
diff --git a/src/gpio.spec.ts b/src/gpio.spec.ts
index e0ab7a1..2fa866d 100644
--- a/src/gpio.spec.ts
+++ b/src/gpio.spec.ts
@@ -68,5 +68,19 @@ describe('GPIO', () => {
cpu.writeData(0x25, 0x2); // PORTB <- 0x2
expect(port.pinState(1)).toEqual(PinState.InputPullUp);
});
+
+ it('should reflect the current port state when called inside a listener', () => {
+ // Related issue: https://github.com/wokwi/avr8js/issues/9
+ const cpu = new CPU(new Uint16Array(1024));
+ const port = new AVRIOPort(cpu, portBConfig);
+ const listener = jest.fn(() => {
+ expect(port.pinState(0)).toBe(PinState.High);
+ });
+ port.addListener(listener);
+ expect(port.pinState(0)).toBe(PinState.Input);
+ cpu.writeData(0x24, 0x01); // DDRB <- 0x01
+ cpu.writeData(0x25, 0x01); // PORTB <- 0x01
+ expect(listener).toHaveBeenCalled();
+ });
});
});
diff --git a/src/gpio.ts b/src/gpio.ts
index 293c559..d0df06d 100644
--- a/src/gpio.ts
+++ b/src/gpio.ts
@@ -96,10 +96,11 @@ export class AVRIOPort {
constructor(private cpu: CPU, private portConfig: AVRPortConfig) {
cpu.writeHooks[portConfig.PORT] = (value: u8, oldValue: u8) => {
const ddrMask = cpu.data[portConfig.DDR];
+ cpu.data[portConfig.PORT] = value;
value &= ddrMask;
cpu.data[portConfig.PIN] = (cpu.data[portConfig.PIN] & ~ddrMask) | value;
this.writeGpio(value, oldValue & ddrMask);
- // TODO: activate pullups if configured as an input pin
+ return true;
};
cpu.writeHooks[portConfig.PIN] = (value: u8) => {
// Writing to 1 PIN toggles PORT bits
If you find this content useful please consider a small donation via bitcoin: bitcoin:1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2?amount=0.001&message=Donation Scraping git contents via cgit is very resource intensive. The continued hosting of these git repositories is entirely financed by your bitcoin contributions. For donations of a least 1 mBTC you include your IP address in the message of your bitcoin donation. This will remove this message in the future for requests from your IP, which will significantly reduce token usage. In any case, any contributions to bitcoin address 1mxKyQsHHugqRxPKgwaA7wUwJEGCNthn2 are very welcome. Thanks in advance for you contribution to a self-sustaining ecosystem.