diff options
| author | Uri Shaked | 2020-05-10 09:01:53 +0300 |
|---|---|---|
| committer | Uri Shaked | 2020-05-10 09:01:53 +0300 |
| commit | eed745eaca8af2a5f8a7a4e40e84828b8c155dc8 (patch) | |
| tree | 714f6766006dea0d216996939207965eace1b8b6 /src/peripherals/gpio.ts | |
| parent | refactor(demo): use pinState() method (diff) | |
| download | avr8js-eed745eaca8af2a5f8a7a4e40e84828b8c155dc8.tar.gz avr8js-eed745eaca8af2a5f8a7a4e40e84828b8c155dc8.tar.bz2 avr8js-eed745eaca8af2a5f8a7a4e40e84828b8c155dc8.zip | |
feat(gpio): add setPin() function
close #26
Diffstat (limited to 'src/peripherals/gpio.ts')
| -rw-r--r-- | src/peripherals/gpio.ts | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/peripherals/gpio.ts b/src/peripherals/gpio.ts index ba3217b..e3c8eaa 100644 --- a/src/peripherals/gpio.ts +++ b/src/peripherals/gpio.ts @@ -92,10 +92,12 @@ export enum PinState { export class AVRIOPort { private listeners: GPIOListener[] = []; + private pinValue: u8 = 0; constructor(private cpu: CPU, private portConfig: AVRPortConfig) { cpu.writeHooks[portConfig.DDR] = (value, oldValue) => { const portValue = cpu.data[portConfig.PORT]; + this.updatePinRegister(portValue, value); this.writeGpio(value & portValue, oldValue & oldValue); }; cpu.writeHooks[portConfig.PORT] = (value: u8, oldValue: u8) => { @@ -103,6 +105,7 @@ export class AVRIOPort { cpu.data[portConfig.PORT] = value; value &= ddrMask; cpu.data[portConfig.PIN] = (cpu.data[portConfig.PIN] & ~ddrMask) | value; + this.updatePinRegister(value, ddrMask); this.writeGpio(value, oldValue & ddrMask); return true; }; @@ -145,6 +148,23 @@ export class AVRIOPort { } } + /** + * Sets the input value for the given pin. This is the value that + * will be returned when reading from the PIN register. + */ + setPin(index: number, value: boolean) { + const bitMask = 1 << index; + this.pinValue &= ~bitMask; + if (value) { + this.pinValue |= bitMask; + } + this.updatePinRegister(this.cpu.data[this.portConfig.PORT], this.cpu.data[this.portConfig.DDR]); + } + + private updatePinRegister(port: u8, ddr: u8) { + this.cpu.data[this.portConfig.PIN] = (this.pinValue & ~ddr) | (port & ddr); + } + private writeGpio(value: u8, oldValue: u8) { for (const listener of this.listeners) { listener(value, oldValue); |
