garmin / LIDARLite_Arduino_Library

High-performance optical distance sensing.
Apache License 2.0
181 stars 84 forks source link

LIDARLite_v3HP_read uses Repeated Start but datasheet says its not supported #17

Open kais0r opened 4 years ago

kais0r commented 4 years ago

Hi. I just realized that the Library for the v3HP uses the Repeated Start Condition in LIDARLite_v3HP_read Function in /src/LIDARLite_v3HP.cpp on Line 398: nackCatcher = Wire.endTransmission(false); // false means perform repeated start

Compleete function:

void LIDARLite_v3HP::read(uint8_t regAddr, uint8_t * dataBytes,
                          uint16_t numBytes, uint8_t lidarliteAddress)
{
    uint16_t i = 0;
    int nackCatcher = 0;

    // Set the internal register address pointer in the Lidar Lite
    Wire.beginTransmission((int) lidarliteAddress);
    Wire.write((int) regAddr); // Set the register to be read

    // A nack means the device is not responding, report the error over serial
    nackCatcher = Wire.endTransmission(false); // false means perform repeated start
    if (nackCatcher != 0)
    {
        Serial.println("> nack");
    }

    // Perform read, save in dataBytes array
    Wire.requestFrom((int)lidarliteAddress, (int) numBytes);
    if ((int) numBytes <= Wire.available())
    {
        while (i < numBytes)
        {
            dataBytes[i] = (uint8_t) Wire.read();
            i++;
        }
    }

} /* LIDARLite_v3HP::read */

In the Datasheet on Page 6 it says:

Notes: • This device does not work with repeated START conditions. It must first receive a STOP condition before a new START condition. • The ACK and NACK items are responses from the master device to the slave device. • The last NACK in the read is technically optional, but the formal I2C protocol states that the master shall not acknowledge the last byte.

It seems to work. But i implemented it into an other microcontroller and we ran into some I2C stucks when having higher EMI Problems. By digging into the code we found this issue...