diff options
| author | Uri Shaked | 2020-02-03 22:34:30 +0200 |
|---|---|---|
| committer | Uri Shaked | 2020-02-03 22:34:30 +0200 |
| commit | 13a112968c5527b10b974489d3a2ec19c98736d9 (patch) | |
| tree | 1c0d9819541d4d3e43846c1971ce3418581e763e | |
| parent | test(twi): add master TWI receive test #10 (diff) | |
| download | avr8js-13a112968c5527b10b974489d3a2ec19c98736d9.tar.gz avr8js-13a112968c5527b10b974489d3a2ec19c98736d9.tar.bz2 avr8js-13a112968c5527b10b974489d3a2ec19c98736d9.zip | |
feat(twi): proper interrupt support #10
| -rw-r--r-- | src/twi.spec.ts | 11 | ||||
| -rw-r--r-- | src/twi.ts | 14 |
2 files changed, 20 insertions, 5 deletions
diff --git a/src/twi.spec.ts b/src/twi.spec.ts index de7ceb8..369f22a 100644 --- a/src/twi.spec.ts +++ b/src/twi.spec.ts @@ -46,6 +46,17 @@ describe('TWI', () => { expect(twi.sclFrequency).toEqual(400000); }); + it('should trigger data an interrupt if TWINT is set', () => { + const cpu = new CPU(new Uint16Array(1024)); + const twi = new AVRTWI(cpu, twiConfig, FREQ_16MHZ); + cpu.writeData(0xbc, 0x81); // TWCR <- TWINT | TWIE + cpu.data[95] = 0x80; // SREG: I------- + twi.tick(); + expect(cpu.pc).toEqual(0x30); // 2-wire Serial Interface Vector + expect(cpu.cycles).toEqual(2); + expect(cpu.data[0xbc] & 0x80).toEqual(0); // UCSR0A should clear TWINT + }); + describe('Master mode', () => { it('should call the startEvent handler when TWSTA bit is written 1', () => { const cpu = new CPU(new Uint16Array(1024)); @@ -1,6 +1,6 @@ -import { u8 } from './types'; import { CPU } from './cpu'; import { avrInterrupt } from './interrupt'; +import { u8 } from './types'; export interface TWIEventHandler { start(repeated: boolean): void; @@ -133,6 +133,13 @@ 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() { @@ -186,11 +193,8 @@ export class AVRTWI { } private updateStatus(value: u8) { - const { TWCR, TWSR, twiInterrupt } = this.config; + const { TWCR, TWSR } = this.config; this.cpu.data[TWSR] = (this.cpu.data[TWSR] & ~TWSR_TWS_MASK) | value; this.cpu.data[TWCR] |= TWCR_TWINT; - if (this.cpu.interruptsEnabled && this.cpu.data[TWCR] & TWCR_TWIE) { - avrInterrupt(this.cpu, twiInterrupt); - } } } |
