Page 1 of 1

LIS3MDL pigpio I2C multiple byte read - data does not update

Posted: Sun Apr 08, 2018 4:13 am
by alexraspi
I am trying to read the magnetometer data off the ST LIS3MDL using the pigpio library I2C library in C++, which requires me to read multiple consecutive registers. Each register is a byte, and each axis is 2 bytes, so I need to read 6 bytes from consecutive registers (OUT_X_L, OUT_X_H...).

I can write single and multiple bytes and read a single byte, but my read from these 6 consecutive registers seem to never update, i.e. they print the exact same data read after read. My arduino code shows that the data should update frequently so my sensor is not toast ( I have also changed up multiple sensors already). I have narrowed it down (maybe) to the fact that reading multiple bytes from the LIS3MDL requires the code to send a master-acknowledge upon receiving each byte (reference pg 18 in datasheet):

Questions:
1. so does pigpio I2C C library send a master acknowledge upon receiving each byte, either on i2cReadDevice / i2cReadByte and all the other i2cRead.... functions?

2. Likewise, does anyone have any code to share / or any pointers for my code below on how to read the LIS3MDL mag registers (multiple consecutive byte reads)?


Datsheet: http://www.st.com/resource/en/datasheet/lis3mdl.pdf

Code snippet:

void ReadMag()
{
int handle, temp;
handle = i2cOpen(1, m_Address, 0);

// assert MSB to auto-increment register address
temp = i2cWriteByte(handle, OUT_X_L | 0x80); //

i2cSwitchCombined(1); // repeated start ON

char buf[6];
temp = i2cReadDevice(handle, buf, 6); // read six bytes: xlm, xhm, ylm, yhm, zlm, zhm

i2cSwitchCombined(0); // repeated start OFF

temp = i2cClose(handle);

// combine lo and high bytes
m_Mag.x = (int16_t)( (buf[1] << 8) | buf[0]);
m_Mag.y = (int16_t)( (buf[3] << 8) | buf[2]);
m_Mag.z = (int16_t)( (buf[5] << 8) | buf[4]);
}

Re: LIS3MDL pigpio I2C multiple byte read - data does not update

Posted: Sun Apr 08, 2018 11:25 pm
by bitbank
You need to share the rest of your code. If you don't initialize the sensor and tell it a sampling frequency, it won't sample new data. The behavior you're seeing would occur if you haven't properly initialized the sensor. Also, it probably is not needed to explicitly call i2cSwitchCombined(). Normally the default I2C behavior is correct for single and multi-byte reads. I have a bunch of I2C devices (accelerometers, magnetometers, temp sensors) that require the MSB to be set on the register address to enable auto-increment mode and none of them require any other settings for I2C multi-byte transfers to work properly.

Re: LIS3MDL pigpio I2C multiple byte read - data does not update

Posted: Mon Apr 09, 2018 7:23 am
by joan
In addition i2cSwitchCombined() has no effect on the new I2C driver. It only works with the old I2C driver (i2c_bcm2708) which you are unlikely to be using. The new driver normally handles repeated starts correctly.

I'll add a note to the pigpio API documentation.

Re: LIS3MDL pigpio I2C multiple byte read - data does not update

Posted: Mon Sep 10, 2018 10:04 am
by Ayvazofsky
I have the same problem with SPI protocol..Output data doesn't change...