Skip to content

Commit 46468d4

Browse files
committed
I2C: generate STOP in case of NACK (fix #698, #254)
thanks @petrd and @Yazzcat
1 parent 4cf72e7 commit 46468d4

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

cores/esp8266/core_esp8266_si2c.c

+13-4
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,15 @@ static unsigned char twi_read_byte(bool nack) {
150150
unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop){
151151
unsigned int i;
152152
if(!twi_write_start()) return 4;//line busy
153-
if(!twi_write_byte(((address << 1) | 0) & 0xFF)) return 2;//received NACK on transmit of address
154-
for(i=0; i<len; i++){
155-
if(!twi_write_byte(buf[i])) return 3;//received NACK on transmit of data
153+
if(!twi_write_byte(((address << 1) | 0) & 0xFF)) {
154+
if (sendStop) twi_write_stop();
155+
return 2; //received NACK on transmit of address
156+
}
157+
for(i=0; i<len; i++) {
158+
if(!twi_write_byte(buf[i])) {
159+
if (sendStop) twi_write_stop();
160+
return 3;//received NACK on transmit of data
161+
}
156162
}
157163
if(sendStop) twi_write_stop();
158164
i = 0;
@@ -168,7 +174,10 @@ unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned i
168174
unsigned char twi_readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop){
169175
unsigned int i;
170176
if(!twi_write_start()) return 4;//line busy
171-
if(!twi_write_byte(((address << 1) | 1) & 0xFF)) return 2;//received NACK on transmit of address
177+
if(!twi_write_byte(((address << 1) | 1) & 0xFF)) {
178+
if (sendStop) twi_write_stop();
179+
return 2;//received NACK on transmit of address
180+
}
172181
for(i=0; i<(len-1); i++) buf[i] = twi_read_byte(false);
173182
buf[len-1] = twi_read_byte(true);
174183
if(sendStop) twi_write_stop();

0 commit comments

Comments
 (0)