Skip to content

Commit bda476f

Browse files
committed
fix(twi): broken repeated start #91
fix #91
1 parent cdccc35 commit bda476f

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/peripherals/twi.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,31 @@ describe('TWI', () => {
6868
expect(twi.eventHandler.start).toHaveBeenCalledWith(false);
6969
});
7070

71+
it('should connect successfully in case of repeated start (issue #91)', () => {
72+
const cpu = new CPU(new Uint16Array(1024));
73+
const twi = new AVRTWI(cpu, twiConfig, FREQ_16MHZ);
74+
75+
// Start condition
76+
cpu.writeData(TWCR, TWINT | TWSTA | TWEN);
77+
cpu.cycles++;
78+
cpu.tick();
79+
80+
// Repeated start
81+
jest.spyOn(twi.eventHandler, 'start');
82+
cpu.writeData(TWCR, TWINT | TWSTA | TWEN);
83+
cpu.cycles++;
84+
cpu.tick();
85+
expect(twi.eventHandler.start).toHaveBeenCalledWith(true);
86+
87+
// Now try to connect...
88+
jest.spyOn(twi.eventHandler, 'connectToSlave');
89+
cpu.writeData(TWDR, 0x80); // Address 0x40, write mode
90+
cpu.writeData(TWCR, TWINT | TWEN);
91+
cpu.cycles++;
92+
cpu.tick();
93+
expect(twi.eventHandler.connectToSlave).toHaveBeenCalledWith(0x40, true);
94+
});
95+
7196
it('should successfully transmit a byte to a slave', () => {
7297
// based on the example in page 225 of the datasheet:
7398
// https://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf

src/peripherals/twi.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export class AVRTWI {
119119
this.eventHandler.start(status !== STATUS_TWI_IDLE);
120120
} else if (value & TWCR_TWSTO) {
121121
this.eventHandler.stop();
122-
} else if (status === STATUS_START) {
122+
} else if (status === STATUS_START || status === STATUS_REPEATED_START) {
123123
this.eventHandler.connectToSlave(twdrValue >> 1, twdrValue & 0x1 ? false : true);
124124
} else if (status === STATUS_SLAW_ACK || status === STATUS_DATA_SENT_ACK) {
125125
this.eventHandler.writeByte(twdrValue);

0 commit comments

Comments
 (0)