Skip to content

Commit

Permalink
I2C: generate STOP in case of NACK (fix #698, #254)
Browse files Browse the repository at this point in the history
thanks @petrd and @Yazzcat
  • Loading branch information
igrr committed Nov 8, 2015
1 parent 4cf72e7 commit 46468d4
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions cores/esp8266/core_esp8266_si2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,15 @@ static unsigned char twi_read_byte(bool nack) {
unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned int len, unsigned char sendStop){
unsigned int i;
if(!twi_write_start()) return 4;//line busy
if(!twi_write_byte(((address << 1) | 0) & 0xFF)) return 2;//received NACK on transmit of address
for(i=0; i<len; i++){
if(!twi_write_byte(buf[i])) return 3;//received NACK on transmit of data
if(!twi_write_byte(((address << 1) | 0) & 0xFF)) {
if (sendStop) twi_write_stop();
return 2; //received NACK on transmit of address
}
for(i=0; i<len; i++) {
if(!twi_write_byte(buf[i])) {
if (sendStop) twi_write_stop();
return 3;//received NACK on transmit of data
}
}
if(sendStop) twi_write_stop();
i = 0;
Expand All @@ -168,7 +174,10 @@ unsigned char twi_writeTo(unsigned char address, unsigned char * buf, unsigned i
unsigned char twi_readFrom(unsigned char address, unsigned char* buf, unsigned int len, unsigned char sendStop){
unsigned int i;
if(!twi_write_start()) return 4;//line busy
if(!twi_write_byte(((address << 1) | 1) & 0xFF)) return 2;//received NACK on transmit of address
if(!twi_write_byte(((address << 1) | 1) & 0xFF)) {
if (sendStop) twi_write_stop();
return 2;//received NACK on transmit of address
}
for(i=0; i<(len-1); i++) buf[i] = twi_read_byte(false);
buf[len-1] = twi_read_byte(true);
if(sendStop) twi_write_stop();
Expand Down

0 comments on commit 46468d4

Please sign in to comment.