maitienduy / MyUno

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wire.requestFrom() should not be followed by Wire.endTransmission().

Koepel opened this issue · comments

In the file "MyUno/i2c_BMP280/i2c.h" is a Wire.endTransmission() after the Wire.requestFrom(). That is not correct.
Explanation: Common-mistakes#2

However, when the length is zero, the Wire.endTransmission(false) does not do a Stop on the I2C bus. Making a Stop there is also not okay, since more and more sensor require that a Repeated Start is used.

A solution could be to split the function into two parts:

void WirePlus::read(const uint8_t address, const uint8_t registeraddress, uint8_t buff[], const uint8_t length=1)
{
    if( length == 0)
    {
        Wire.beginTransmission(address); 	// Adress + WRITE (0)
        Wire.write(registeraddress);
        Wire.endTransmission(true); 		// with Stop
    }
    else
    {
        Wire.beginTransmission(address); 	// Adress + WRITE (0)
        Wire.write(registeraddress);
        Wire.endTransmission(false); 		// No Stop Condition, for repeated Talk

        Wire.requestFrom(address, length); 	// Address + READ (1) + Stop
        uint8_t _i = 0;
        while(Wire.available())
        {
            buff[_i] = Wire.read();
            _i++;
        }
    }
}