Slightly incorrect and unstable sensor data using Zephyr Sensor API with Accelerometer / Gyro LSM6DS3TR-C

Hello! To summarize my issue, I have a 6 DOF accelerometer/gyroscope [LSM6DS3TR-C]. I am accessing the sensor using SPI and the Zephyr Sensor API, and my readings are somewhat unstable and slightly incorrect - e.g, when not moving, the sensor's gyroscope measures +/- 0.1 - 0.9 rad/s.

My environment:

  • NRF Connect SDK in VSCode - Version 2.1.1
  • Zephyr v3.1.99

I'll attach the relevant code, apologies for the length.

Below is my function for creating the SPI device. 

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/* Function for initializing SPI device */
static const struct device *get_spi_device(void)
{
const struct device *dev = DEVICE_DT_GET(DT_INST(0, st_lsm6dsl));
if (dev == NULL)
{
/* No such node, or the node does not have status "okay". */
printk("\nError: no device found.\n");
return NULL;
}
if (!device_is_ready(dev))
{
printk("\nError: Device \"%s\" is not ready; "
"check the driver initialization logs for errors.\n",
dev->name);
return NULL;
}
printk("Found device \"%s\", getting sensor data\n", dev->name);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

I access the sensor data using the Sensor API enums.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void main(void)
{
//...
const struct device *spi_dev = get_spi_device();
if (spi_dev == NULL) {
return;
}
static struct sensor_value accel_x, accel_y, accel_z, gyro_x, gyro_y, gyro_z;
while(1){
/* Returns values m/s^2 */
sensor_sample_fetch_chan(spi_dev, SENSOR_CHAN_ACCEL_XYZ);
sensor_channel_get(spi_dev, SENSOR_CHAN_ACCEL_X, &accel_x);
sensor_channel_get(spi_dev, SENSOR_CHAN_ACCEL_Y, &accel_y);
sensor_channel_get(spi_dev, SENSOR_CHAN_ACCEL_Z, &accel_z);
// returns gyro data in rad/s
sensor_sample_fetch_chan(spi_dev, SENSOR_CHAN_GYRO_XYZ);
sensor_channel_get(spi_dev, SENSOR_CHAN_GYRO_X, &gyro_x);
sensor_channel_get(spi_dev, SENSOR_CHAN_GYRO_Y, &gyro_y);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX


Here is my prj.conf:

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# assert will cause soft resets, so set to N for now
CONFIG_ASSERT=n
#run on restart
CONFIG_BOARD_ENABLE_DCDC=n
# # Enable RTT to replace UART
CONFIG_RTT_CONSOLE=y
CONFIG_STDOUT_CONSOLE=y
CONFIG_UART_CONSOLE=n
CONFIG_USE_SEGGER_RTT=y
CONFIG_SHELL_BACKEND_RTT=y
# ACCEL/GYRO PRJ.CONF
CONFIG_I2C=y
CONFIG_SPI=y
CONFIG_SENSOR=y
# Allow math.h
CONFIG_NEWLIB_LIBC=y
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

And finally, my overlay file.

Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
&pinctrl {
spi1_default: spi1_default {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 0, 12)>,
<NRF_PSEL(SPIM_MOSI, 0, 13)>,
<NRF_PSEL(SPIM_MISO, 0, 14)>;
};
};
spi1_sleep: spi1_sleep {
group1 {
psels = <NRF_PSEL(SPIM_SCK, 0, 12)>,
<NRF_PSEL(SPIM_MOSI, 0, 13)>,
<NRF_PSEL(SPIM_MISO, 0, 14)>;
low-power-enable;
};
};
};
&spi1 {
compatible = "nordic,nrf-spi";
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

My primary concern is the gyroscope values being non-zero and very jittery when I print out the values. I am aware I'm using the library for the LSM6DSL, but I've compared datasheets with the LSM6DS3TR-C and all the registers and conversions are the exact same. Note that my readings are 'almost' correct, in the sense that I can still read G as ~9.81-10 [m/s^2]. 

I tested this exact same sensor (Adafruit LSM6DS3TR-C Breakout) in an Arduino environment and using the Adafruit library for both LSM6DSL and LSM6DS3TR-C. Both outputted stable zero values for the stationary gyroscope raw register reads. So the culprit has to be somewhere in my Zephyr implementation. 

Is there any advice on what could be going wrong here? Any help is greatly appreciated!