I have another board sending 3-byte packets of data to the Due Serial1 port at about 100 packets/second. I also am reading an IMU via i2c at about 200/sec. This always crashes after a few seconds. The crashes only occur when data is being received on the serial port. I have similar results on the Serial2 port. Here is a code snippit that give this result:
#include <Wire.h>
#include <LSM6.h>
#define ADDRESS 0b1101011
#define OUTX_L_G 0x22
#define OUTX_L_XL 0x28
LSM6 lsm6;
int z = 0;
void setup() {
Serial1.begin(115200); // UP Board
Serial.begin(115200);
pinMode(13,OUTPUT); // Status LED, also blue LED
Wire.begin();
if (!lsm6.init()) Serial.println("IMU initialize failed!");
lsm6.enableDefault();
lsm6.writeReg(LSM6::INT1_CTRL, 0X02); // Accel data ready on INT1
lsm6.writeReg(LSM6::INT2_CTRL, 0X01); // Gyro data ready on INT2
lsm6.writeReg(LSM6::CTRL2_G, 0X5C); // Gyro 2000fs, 208hz
lsm6.writeReg(LSM6::CTRL1_XL, 0X50); // Accel 2g, 208hz
} // end setup()
void loop() { //Main Loop
static unsigned int tRead = 0;
while(true) {
while (Serial1.available()) {
byte b = Serial1.read();
Serial.print(b); Serial.print(" ");
if (b == 0) Serial.println();
}
unsigned int t = micros();
if (t > tRead) {
tRead = t + 5000;
readGyro();
readAcc();
Serial.println(z);
}
}
} // End loop().
void readGyro(void)
{
Wire.beginTransmission(ADDRESS);
Wire.write(OUTX_L_G);
Wire.endTransmission();
Wire.requestFrom(ADDRESS, (uint8_t)6);
while (Wire.available() < 6) ;
uint8_t xlg = Wire.read();
uint8_t xhg = Wire.read();
uint8_t ylg = Wire.read();
uint8_t yhg = Wire.read();
uint8_t zlg = Wire.read();
uint8_t zhg = Wire.read();
// combine high and low bytes
z = (int16_t)(zhg << 8 | zlg);
}
void readAcc(void)
{
Wire.beginTransmission(ADDRESS);
Wire.write(OUTX_L_XL);
Wire.endTransmission();
Wire.requestFrom(ADDRESS, (uint8_t)6);
while (Wire.available() < 6) ;
uint8_t xla = Wire.read();
uint8_t xha = Wire.read();
uint8_t yla = Wire.read();
uint8_t yha = Wire.read();
uint8_t zla = Wire.read();
uint8_t zha = Wire.read();
}
the output looks like this:
151 48 0
151 48 0
151 48 0
-18
151 48 0
151 48 0
-20
151 48 0
151 48 0
151 48 0
-20
151 48 0
151 48 0
-18
151 48 0
151 48 0
151 48 0
-19
151 48 0
151 48 0
-21
151 48 0
151 48 0
151 48 0
Any ideas? It sure feels like a library bug to me. Should I file a bug report?