@@ -94,6 +94,7 @@ export class NoopTWIEventHandler implements TWIEventHandler {
94
94
95
95
export class AVRTWI {
96
96
public eventHandler : TWIEventHandler = new NoopTWIEventHandler ( this ) ;
97
+ private busy = false ;
97
98
98
99
// Interrupts
99
100
private TWI : AVRInterruptConfig = {
@@ -112,18 +113,23 @@ export class AVRTWI {
112
113
this . cpu . clearInterruptByFlag ( this . TWI , value ) ;
113
114
this . cpu . updateInterruptEnable ( this . TWI , value ) ;
114
115
const { status } = this ;
115
- if ( clearInt && value & TWCR_TWEN ) {
116
+ if ( clearInt && value & TWCR_TWEN && ! this . busy ) {
116
117
const twdrValue = this . cpu . data [ this . config . TWDR ] ;
117
118
this . cpu . addClockEvent ( ( ) => {
118
119
if ( value & TWCR_TWSTA ) {
120
+ this . busy = true ;
119
121
this . eventHandler . start ( status !== STATUS_TWI_IDLE ) ;
120
122
} else if ( value & TWCR_TWSTO ) {
123
+ this . busy = true ;
121
124
this . eventHandler . stop ( ) ;
122
125
} else if ( status === STATUS_START || status === STATUS_REPEATED_START ) {
126
+ this . busy = true ;
123
127
this . eventHandler . connectToSlave ( twdrValue >> 1 , twdrValue & 0x1 ? false : true ) ;
124
128
} else if ( status === STATUS_SLAW_ACK || status === STATUS_DATA_SENT_ACK ) {
129
+ this . busy = true ;
125
130
this . eventHandler . writeByte ( twdrValue ) ;
126
131
} else if ( status === STATUS_SLAR_ACK || status === STATUS_DATA_RECEIVED_ACK ) {
132
+ this . busy = true ;
127
133
const ack = ! ! ( value & TWCR_TWEA ) ;
128
134
this . eventHandler . readByte ( ack ) ;
129
135
}
@@ -153,15 +159,18 @@ export class AVRTWI {
153
159
}
154
160
155
161
completeStart ( ) {
162
+ this . busy = false ;
156
163
this . updateStatus ( this . status === STATUS_TWI_IDLE ? STATUS_START : STATUS_REPEATED_START ) ;
157
164
}
158
165
159
166
completeStop ( ) {
167
+ this . busy = false ;
160
168
this . cpu . data [ this . config . TWCR ] &= ~ TWCR_TWSTO ;
161
169
this . updateStatus ( STATUS_TWI_IDLE ) ;
162
170
}
163
171
164
172
completeConnect ( ack : boolean ) {
173
+ this . busy = false ;
165
174
if ( this . cpu . data [ this . config . TWDR ] & 0x1 ) {
166
175
this . updateStatus ( ack ? STATUS_SLAR_ACK : STATUS_SLAR_NACK ) ;
167
176
} else {
@@ -170,10 +179,12 @@ export class AVRTWI {
170
179
}
171
180
172
181
completeWrite ( ack : boolean ) {
182
+ this . busy = false ;
173
183
this . updateStatus ( ack ? STATUS_DATA_SENT_ACK : STATUS_DATA_SENT_NACK ) ;
174
184
}
175
185
176
186
completeRead ( value : u8 ) {
187
+ this . busy = false ;
177
188
const ack = ! ! ( this . cpu . data [ this . config . TWCR ] & TWCR_TWEA ) ;
178
189
this . cpu . data [ this . config . TWDR ] = value ;
179
190
this . updateStatus ( ack ? STATUS_DATA_RECEIVED_ACK : STATUS_DATA_RECEIVED_NACK ) ;
0 commit comments