Using the new Arduino IDE for ESP8266 and found bugs, report them here

Moderator: igrr

User avatar
By mnewman401
#60868
martinayotte wrote:1.6.2 framework is way too old, we are currently at 2.3.0.
Maybe you should try to upgrade to see if the issue still there.


The issue is still present in 2.3.0.

I found the code fragment below in

c:\Users\mnewman\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.3.0\cores\esp8266\core_esp8266_si2c.c



The code obviously has the bug of not checking for 0 length and then counting backwards to -1.
Similar places in the library likely have the same bug.

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)) {
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();
i = 0;
while(SDA_READ() == 0 && (i++) < 10){
SCL_LOW();
twi_delay(twi_dcount);
SCL_HIGH();
twi_delay(twi_dcount);
}
return 0;
}


I think the code should do this:

...
if(!twi_write_byte(((address << 1) | 1) & 0xFF)) {
if (sendStop) twi_write_stop();
return 2;//received NACK on transmit of address
}
if (len == 0) {
// Read of 0 bytes. Just send the stop bit after the address.
if (sendStop) twi_write_stop();
return 0;
}
...
User avatar
By martinayotte
#60884 The "for" loop is not an issue, since it will never get in.
But it is the next line that causing the crash :
Code: Select allbuf[len-1] = twi_read_byte(true);

should be replace by:
Code: Select allif (len > 0)
    buf[len-1] = twi_read_byte(true);

Your solution maybe not work, since the "while(SDA_READ()" still needed.
Try the one above.