FIFO_Pattern seems desynchronized
I have configured the LSM6SDM into continuous mode, 104Hz as is described in AN487, Example 1. I am reading first the number of words in the FIFO, and then the FIFO Pattern (from FIFO Status 3).
Here are some code snippet for initialization:
unsigned char init[6][2]={
{0x08,0x09}, // FIFO_CTRL3 set to no decimation (accel+gyro)
{0x0a,0x26}, // FIFO_CTRL5 set to continuous mode, 104 Hz (accel+gyro)
{0x10,0x40}, // CTRL1_XL set to 104Hz, +/- 2g, ODR/2
{0x11,0x42}, // CTRL2_G set to 104Hz, +/- 125 dps
{0x12,0x44}, // CTRL3_C set to BDU + IF_INC
{0x14,0x60}}; // CTRL5_C set to rounding for accel+gyro
for (i=0;i<6;++i)
write(i2c_fd,&init[i][0],2);
Roughly every 100ms I call a function which does the following:
write(i2c_fd,&fs1,1); // Read FIFO_STATUS1
read(i2c_fd,data,2);
numsamples=((data[1]&7)<<8)+data[0];
if ((data[1]&64)==64) numsamples=1023; // I don't really understand this, but sometimes the FIFO shows 0 bytes available until the first read
write(i2c_fd,&fs3,1); // Read FIFO_STATUS3
read(i2c_fd,,&fp,1); // Get FIFO Pattern (ignoring the top 8-bits because it should never exceed 5
switch (fp) { // Ignore incomplete data based on FIFO_Pattern
case 1 : start=2; fp=3; break;
case 2 : start=1; fp=3; break;
case 4 : start=2; fp=0; break;
case 5 : start=1; fp=0; break;
default : start=0;
}
numsamples-=(numsamples-start)%3; // Adjust numsamples to bring in an integer number of 3-axis samples plus whatever is at the front of the buffer. That means after the first read, it should always be aligned with either fp=0 or fp=3.
write(i2c_fd,&fsl,1); // Read FIFO_DATAOUT_LOW
read(i2c_fd,data,numsamples<<1); // Collect the data
Of the 6 words, one of them is approximately 16600, the remainder are <400. I think it's reasonable to assume since the board is sitting on my bench, that the 16600 number is Az (FIFO_Pattern=5), we can figure out the others from that and they appear to be in order. But when Az is the first word to be read, FIFO_Pattern isn't always 5. It seems to drift, as if some reads do not update FIFO_Pattern, or update it too much. Once it gets desynchronized, it stays desynchronized for hundreds, thousands, or even tens of thousands of patterns before it appears to jump again. So I believe whatever is causing it is screwing something up on chip, it's not garbled communications (and the number in FIFO_Status3 is always between 0 and 5).
