Re: Wire.requestFrom(0x28,0,true) gets a panic
Posted: Thu Jan 12, 2017 12:06 am
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;
}
...