aboutsummaryrefslogtreecommitdiff
path: root/src/peripherals/twi.ts
diff options
context:
space:
mode:
authorUri Shaked2020-12-09 00:51:13 +0200
committerUri Shaked2020-12-09 00:51:58 +0200
commit36c4134a26063248a2ef47f5ac8defe50d9476b1 (patch)
tree44433290b0d684768216b33550a7af9a6fb4235f /src/peripherals/twi.ts
parenttest(cpu): improve test name (diff)
downloadavr8js-36c4134a26063248a2ef47f5ac8defe50d9476b1.tar.gz
avr8js-36c4134a26063248a2ef47f5ac8defe50d9476b1.tar.bz2
avr8js-36c4134a26063248a2ef47f5ac8defe50d9476b1.zip
refactor: central interrupt handling #38
Diffstat (limited to 'src/peripherals/twi.ts')
-rw-r--r--src/peripherals/twi.ts30
1 files changed, 15 insertions, 15 deletions
diff --git a/src/peripherals/twi.ts b/src/peripherals/twi.ts
index 52258a6..914b67f 100644
--- a/src/peripherals/twi.ts
+++ b/src/peripherals/twi.ts
@@ -1,5 +1,4 @@
-import { CPU } from '../cpu/cpu';
-import { avrInterrupt } from '../cpu/interrupt';
+import { AVRInterruptConfig, CPU } from '../cpu/cpu';
import { u8 } from '../types';
export interface TWIEventHandler {
@@ -98,13 +97,22 @@ export class AVRTWI {
private nextTick: (() => void) | null = null;
+ // Interrupts
+ private TWI: AVRInterruptConfig = {
+ address: this.config.twiInterrupt,
+ flagRegister: this.config.TWCR,
+ flagMask: TWCR_TWINT,
+ enableRegister: this.config.TWCR,
+ enableMask: TWCR_TWIE,
+ };
+
constructor(private cpu: CPU, private config: TWIConfig, private freqMHz: number) {
this.updateStatus(STATUS_TWI_IDLE);
this.cpu.writeHooks[config.TWCR] = (value) => {
+ this.cpu.data[config.TWCR] = value;
const clearInt = value & TWCR_TWINT;
- if (clearInt) {
- value &= ~TWCR_TWINT;
- }
+ this.cpu.clearInterruptByFlag(this.TWI, value);
+ this.cpu.updateInterruptEnable(this.TWI, value);
const { status } = this;
if (clearInt && value & TWCR_TWEN) {
const twdrValue = this.cpu.data[this.config.TWDR];
@@ -122,7 +130,6 @@ export class AVRTWI {
this.eventHandler.readByte(ack);
}
};
- this.cpu.data[config.TWCR] = value;
return true;
}
};
@@ -133,13 +140,6 @@ export class AVRTWI {
this.nextTick();
this.nextTick = null;
}
- if (this.cpu.interruptsEnabled) {
- const { TWCR, twiInterrupt } = this.config;
- if (this.cpu.data[TWCR] & TWCR_TWIE && this.cpu.data[TWCR] & TWCR_TWINT) {
- avrInterrupt(this.cpu, twiInterrupt);
- this.cpu.data[TWCR] &= ~TWCR_TWINT;
- }
- }
}
get prescaler() {
@@ -193,8 +193,8 @@ export class AVRTWI {
}
private updateStatus(value: u8) {
- const { TWCR, TWSR } = this.config;
+ const { TWSR } = this.config;
this.cpu.data[TWSR] = (this.cpu.data[TWSR] & ~TWSR_TWS_MASK) | value;
- this.cpu.data[TWCR] |= TWCR_TWINT;
+ this.cpu.setInterruptFlag(this.TWI);
}
}