File tree Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Expand file tree Collapse file tree 2 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -68,6 +68,31 @@ describe('TWI', () => {
68
68
expect ( twi . eventHandler . start ) . toHaveBeenCalledWith ( false ) ;
69
69
} ) ;
70
70
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
+
71
96
it ( 'should successfully transmit a byte to a slave' , ( ) => {
72
97
// based on the example in page 225 of the datasheet:
73
98
// https://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf
Original file line number Diff line number Diff line change @@ -119,7 +119,7 @@ export class AVRTWI {
119
119
this . eventHandler . start ( status !== STATUS_TWI_IDLE ) ;
120
120
} else if ( value & TWCR_TWSTO ) {
121
121
this . eventHandler . stop ( ) ;
122
- } else if ( status === STATUS_START ) {
122
+ } else if ( status === STATUS_START || status === STATUS_REPEATED_START ) {
123
123
this . eventHandler . connectToSlave ( twdrValue >> 1 , twdrValue & 0x1 ? false : true ) ;
124
124
} else if ( status === STATUS_SLAW_ACK || status === STATUS_DATA_SENT_ACK ) {
125
125
this . eventHandler . writeByte ( twdrValue ) ;
You can’t perform that action at this time.
0 commit comments