This works now nicely wit fast update rates. Some delays are good to keep not to over poll the I2C bus. See the comments at the start of the coded
Code:
/*Sample code for LIDAR lite connected to Teensy 3.1. 2 April 2015
uses i2c_t3.h V7
https://forum.pjrc.com/attachment.php?attachmentid=3344&d=1421617902
Wire.endTransmission(I2C_STOP,300); is problematic and return sometimes unknown error.
Wire.beginTransmission((int)LIDARLite_ADDRESS); seems to recover from the error.
Kim Janson,
www.levitezer.com
*/
#include <i2c_t3.h>
#define LIDARLite_ADDRESS 0x62 // Default I2C Address of LIDAR-Lite.
#define RegisterMeasure 0x00 // Register to write to initiate ranging.
#define MeasureValue 0x04 // Value to initiate ranging.
#define RegisterHighLowB 0x8f // Register to get both High and Low bytes in 1 call.
byte MeasureArray[2];
int LIDARreading = 0;
int LIDARreading2 = 0;
int LIDARreadingPrevious = 0;
uint8_t nackack = 100;
int32_t timer = 0;
int32_t errors = 0;
void setup()
{
Wire.begin(I2C_MASTER,0x00 ,I2C_PINS_18_19, I2C_PULLUP_INT, I2C_RATE_100);
Wire.setOpMode(I2C_OP_MODE_ISR);
Serial.begin(115200);
MeasureArray[0]=0x00; // Register to write to initiate ranging.
MeasureArray[1]=0x04; // Value to initiate ranging.
}
void loop()
{
nackack = 0;
while ( nackack == 0){ Wire.beginTransmission((int)LIDARLite_ADDRESS);
nackack = Wire.write( MeasureArray,2); if (nackack == 0 ){delay(1); Serial.print("Wire.write FAIL");} }
nackack = 100;
while ( nackack != 0){ nackack = Wire.endTransmission(I2C_STOP,900); if (nackack != 0){delay(1);
Serial.print("Wire.endTransmission 1 FAIL : "); Serial.print(nackack);} }
nackack = 0;
while ( nackack == 0){ Wire.beginTransmission((int)LIDARLite_ADDRESS);
nackack = Wire.write((int)RegisterHighLowB); if (nackack == 0){delay(1); Serial.print("Wire.write FAIL");} }
delay(1);
nackack = 100;
while ( nackack != 0){ nackack = Wire.endTransmission(I2C_STOP,300); if (nackack != 0){
delay(2);
if (nackack == 4){Wire.beginTransmission((int)LIDARLite_ADDRESS); errors=errors+1;} //recovers from unknown error
//Serial.print("Wire.endTransmission 2 FAIL : ");
//Serial.print(nackack);
//Serial.print(" ");
}}
nackack = 0;
while ( nackack == 0){ nackack = Wire.requestFrom((int)LIDARLite_ADDRESS, 2, I2C_STOP,900); if (nackack == 0){delay(1);Serial.print(" Wire.requestFrom FAIL");}}
if(2 == Wire.available())
{
LIDARreading = Wire.readByte();
LIDARreading = LIDARreading << 8; // shift high byte to be high 8 bits
LIDARreading2 = Wire.readByte();
LIDARreading |= LIDARreading2; // receive low byte as lower 8 bits
} else while(Wire.available() != 0 ) { Serial.print(" Wire.available : "); Serial.print(Wire.readByte());}
delay (1);
//if (LIDARreadingPrevious != LIDARreading) {
Serial.print(" Sample rate (ms) : ");
Serial.print(millis()-timer);
Serial.print(" LIDARreading: ");
Serial.print(LIDARreading);
Serial.print(" errors :");
Serial.println(errors);
timer=millis();
//}
LIDARreadingPrevious=LIDARreading;
}
Excample results:
Sample rate (ms) : 14 LIDARreading: 500 errors :41
Sample rate (ms) : 13 LIDARreading: 485 errors :41
Sample rate (ms) : 11 LIDARreading: 471 errors :41
Sample rate (ms) : 11 LIDARreading: 463 errors :41
Sample rate (ms) : 12 LIDARreading: 456 errors :41
Sample rate (ms) : 13 LIDARreading: 432 errors :41
Sample rate (ms) : 13 LIDARreading: 297 errors :41
Sample rate (ms) : 12 LIDARreading: 275 errors :41
Sample rate (ms) : 11 LIDARreading: 285 errors :41
Sample rate (ms) : 14 LIDARreading: 500 errors :41
Sample rate (ms) : 13 LIDARreading: 485 errors :41
Sample rate (ms) : 11 LIDARreading: 471 errors :41
Sample rate (ms) : 11 LIDARreading: 463 errors :41
Sample rate (ms) : 12 LIDARreading: 456 errors :41
Sample rate (ms) : 13 LIDARreading: 432 errors :41
Sample rate (ms) : 13 LIDARreading: 297 errors :41
Sample rate (ms) : 12 LIDARreading: 275 errors :41
Sample rate (ms) : 11 LIDARreading: 285 errors :41
Edit: but not tested much yet.
Edit2: and this hangs too, but some progress anyway. Maybe something needs to be fixed on the i2c_t3.h Wire.endTransmission?
And would be nice to know what I2C_STOP and I2C_NOSTOP actually do.
Edit3: and it seems I2C_STOP does not work if SPI.h is used :-(