From f95a33c4bd6310f69bc24ee2f4c05157c532e25d Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Fri, 31 Jan 2020 15:08:53 +0200 Subject: test(twi): assembly code to test master transmit #10 --- src/twi.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/twi.ts') diff --git a/src/twi.ts b/src/twi.ts index e2cfe24..027061f 100644 --- a/src/twi.ts +++ b/src/twi.ts @@ -96,6 +96,8 @@ export class NoopTWIEventHandler implements TWIEventHandler { export class AVRTWI { public eventHandler: TWIEventHandler = new NoopTWIEventHandler(this); + private nextTick: (() => void) | null = null; + constructor(private cpu: CPU, private config: TWIConfig, private freqMHz: number) { this.updateStatus(STATUS_TWI_IDLE); this.cpu.writeHooks[config.TWCR] = (value) => { @@ -106,8 +108,7 @@ export class AVRTWI { const { status } = this; if (clearInt && value & TWCR_TWEN) { const twdrValue = this.cpu.data[this.config.TWDR]; - // TODO: this should be executed after the current instruction completes - setTimeout(() => { + this.nextTick = () => { if (value & TWCR_TWSTA) { this.eventHandler.start(status !== STATUS_TWI_IDLE); } else if (value & TWCR_TWSTO) { @@ -120,13 +121,20 @@ export class AVRTWI { const ack = !!(value & TWCR_TWEA); this.eventHandler.readByte(ack); } - }, 0); + }; this.cpu.data[config.TWCR] = value; return true; } }; } + tick() { + if (this.nextTick) { + this.nextTick(); + this.nextTick = null; + } + } + get prescaler() { switch (this.cpu.data[this.config.TWSR] & TWSR_TWPS_MASK) { case 0: -- cgit v1.2.3