Skip to content

Commit

Permalink
fix(twi): broken repeated start #91
Browse files Browse the repository at this point in the history
fix #91
  • Loading branch information
urish committed Apr 15, 2021
1 parent cdccc35 commit bda476f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/peripherals/twi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,31 @@ describe('TWI', () => {
expect(twi.eventHandler.start).toHaveBeenCalledWith(false);
});

it('should connect successfully in case of repeated start (issue #91)', () => {
const cpu = new CPU(new Uint16Array(1024));
const twi = new AVRTWI(cpu, twiConfig, FREQ_16MHZ);

// Start condition
cpu.writeData(TWCR, TWINT | TWSTA | TWEN);
cpu.cycles++;
cpu.tick();

// Repeated start
jest.spyOn(twi.eventHandler, 'start');
cpu.writeData(TWCR, TWINT | TWSTA | TWEN);
cpu.cycles++;
cpu.tick();
expect(twi.eventHandler.start).toHaveBeenCalledWith(true);

// Now try to connect...
jest.spyOn(twi.eventHandler, 'connectToSlave');
cpu.writeData(TWDR, 0x80); // Address 0x40, write mode
cpu.writeData(TWCR, TWINT | TWEN);
cpu.cycles++;
cpu.tick();
expect(twi.eventHandler.connectToSlave).toHaveBeenCalledWith(0x40, true);
});

it('should successfully transmit a byte to a slave', () => {
// based on the example in page 225 of the datasheet:
// https://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf
Expand Down
2 changes: 1 addition & 1 deletion src/peripherals/twi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export class AVRTWI {
this.eventHandler.start(status !== STATUS_TWI_IDLE);
} else if (value & TWCR_TWSTO) {
this.eventHandler.stop();
} else if (status === STATUS_START) {
} else if (status === STATUS_START || status === STATUS_REPEATED_START) {
this.eventHandler.connectToSlave(twdrValue >> 1, twdrValue & 0x1 ? false : true);
} else if (status === STATUS_SLAW_ACK || status === STATUS_DATA_SENT_ACK) {
this.eventHandler.writeByte(twdrValue);
Expand Down

0 comments on commit bda476f

Please sign in to comment.