Using LSM6DSO sensor on NRF52832

Hello, 

I'm new to pretty much all of this, but I'll do my best to explain my issue. As the title says, I'm currently trying to get a LSM6DSO sensor to work with the NRF52832 board. I'm using the Nordic sample code for LSM6DSO and using VSCode to build it for the NRF52832. I was having issues with the devicetree not being able to find the sensor using DEVICE_DT_GET_ONE (this function initially returned a negative value), so I added an extra overlay file: 

arduino_i2c: &i2c0 {
    lsm6dso@6a {
        compatible = "st,lsm6dso";
        label = "LSM6DSO";
        reg = <0x6a>;
    };
};
This seemed to fix the issue. The only other thing I changed from the base project code was adding the lines
CONFIG_LOG=y
CONFIG_LOG_PRINTK=y
to my prj.conf file so that I could log any errors.
I've fully connected the sensor to the board and have verified the connections are correct using a colleagues code, so I don't think the connections are the issue. When I build and flash the code, this is the output I get:
As I said, I'm fairly new to how Nordic organizes code and devicetree, so I'm really just not sure what to do from here. Obviously, the code is running through its main function, but it gets an error when it tries to test the trigger mode. I believe the sample code is supposed to work with an ST board, so I think getting this code to work is just a matter of making small changes to the overlay files or the configuration files, although I could easily be wrong.
Any advice would be greatly appreciated, and let me know if I can provide any more information that might help. Thank you!
Parents
  • I realize that I didn't actually include the code sample I was using, so here it is.

    /*
     * Copyright (c) 2020 Yestin Sun
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <stdio.h>
    #include <zephyr/kernel.h>
    #include <zephyr/device.h>
    #include <zephyr/drivers/sensor.h>
    
    static inline float out_ev(struct sensor_value *val)
    {
    	return (val->val1 + (float)val->val2 / 1000000);
    }
    
    static void fetch_and_display(const struct device *dev)
    {
    	struct sensor_value x, y, z;
    	static int trig_cnt;
    
    	trig_cnt++;
    
    	/* lsm6dso accel */
    	sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ);
    	sensor_channel_get(dev, SENSOR_CHAN_ACCEL_X, &x);
    	sensor_channel_get(dev, SENSOR_CHAN_ACCEL_Y, &y);
    	sensor_channel_get(dev, SENSOR_CHAN_ACCEL_Z, &z);
    
    	printf("accel x:%f ms/2 y:%f ms/2 z:%f ms/2\n",
    			(double)out_ev(&x), (double)out_ev(&y), (double)out_ev(&z));
    
    	/* lsm6dso gyro */
    	sensor_sample_fetch_chan(dev, SENSOR_CHAN_GYRO_XYZ);
    	sensor_channel_get(dev, SENSOR_CHAN_GYRO_X, &x);
    	sensor_channel_get(dev, SENSOR_CHAN_GYRO_Y, &y);
    	sensor_channel_get(dev, SENSOR_CHAN_GYRO_Z, &z);
    
    	printf("gyro x:%f rad/s y:%f rad/s z:%f rad/s\n",
    			(double)out_ev(&x), (double)out_ev(&y), (double)out_ev(&z));
    
    	printf("trig_cnt:%d\n\n", trig_cnt);
    }
    
    static int set_sampling_freq(const struct device *dev)
    {
    	int ret = 0;
    	struct sensor_value odr_attr;
    
    	/* set accel/gyro sampling frequency to 12.5 Hz */
    	odr_attr.val1 = 12.5;
    	odr_attr.val2 = 0;
    
    	ret = sensor_attr_set(dev, SENSOR_CHAN_ACCEL_XYZ,
    			SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr);
    	if (ret != 0) {
    		printf("Cannot set sampling frequency for accelerometer.\n");
    		return ret;
    	}
    
    	ret = sensor_attr_set(dev, SENSOR_CHAN_GYRO_XYZ,
    			SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr);
    	if (ret != 0) {
    		printf("Cannot set sampling frequency for gyro.\n");
    		return ret;
    	}
    
    	return 0;
    }
    
    #ifdef CONFIG_LSM6DSO_TRIGGER
    static void trigger_handler(const struct device *dev,
    			    const struct sensor_trigger *trig)
    {
    	fetch_and_display(dev);
    }
    
    static void test_trigger_mode(const struct device *dev)
    {
    	struct sensor_trigger trig;
    
    	if (set_sampling_freq(dev) != 0)
    		return;
    
    	trig.type = SENSOR_TRIG_DATA_READY;
    	trig.chan = SENSOR_CHAN_ACCEL_XYZ;
    
    	if (sensor_trigger_set(dev, &trig, trigger_handler) != 0) {
    		printf("Could not set sensor type and channel\n");
    		return;
    	}
    }
    
    #else
    static void test_polling_mode(const struct device *dev)
    {
    	if (set_sampling_freq(dev) != 0) {
    		return;
    	}
    
    	while (1) {
    		fetch_and_display(dev);
    		k_sleep(K_MSEC(1000));
    	}
    }
    #endif
    
    int main(void)
    {
    	printf("We are in main\n");
    	const struct device *const dev = DEVICE_DT_GET_ONE(st_lsm6dso);
    	if (!device_is_ready(dev)) {
    		printk("%s: device not ready.\n", dev->name);
    		return 0;
    	}
    
    #ifdef CONFIG_LSM6DSO_TRIGGER
    	printf("Testing LSM6DSO sensor in trigger mode.\n\n");
    	test_trigger_mode(dev);
    #else
    	printf("Testing LSM6DSO sensor in polling mode.\n\n");
    	test_polling_mode(dev);
    #endif
    	return 0;
    }
    

Reply
  • I realize that I didn't actually include the code sample I was using, so here it is.

    /*
     * Copyright (c) 2020 Yestin Sun
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <stdio.h>
    #include <zephyr/kernel.h>
    #include <zephyr/device.h>
    #include <zephyr/drivers/sensor.h>
    
    static inline float out_ev(struct sensor_value *val)
    {
    	return (val->val1 + (float)val->val2 / 1000000);
    }
    
    static void fetch_and_display(const struct device *dev)
    {
    	struct sensor_value x, y, z;
    	static int trig_cnt;
    
    	trig_cnt++;
    
    	/* lsm6dso accel */
    	sensor_sample_fetch_chan(dev, SENSOR_CHAN_ACCEL_XYZ);
    	sensor_channel_get(dev, SENSOR_CHAN_ACCEL_X, &x);
    	sensor_channel_get(dev, SENSOR_CHAN_ACCEL_Y, &y);
    	sensor_channel_get(dev, SENSOR_CHAN_ACCEL_Z, &z);
    
    	printf("accel x:%f ms/2 y:%f ms/2 z:%f ms/2\n",
    			(double)out_ev(&x), (double)out_ev(&y), (double)out_ev(&z));
    
    	/* lsm6dso gyro */
    	sensor_sample_fetch_chan(dev, SENSOR_CHAN_GYRO_XYZ);
    	sensor_channel_get(dev, SENSOR_CHAN_GYRO_X, &x);
    	sensor_channel_get(dev, SENSOR_CHAN_GYRO_Y, &y);
    	sensor_channel_get(dev, SENSOR_CHAN_GYRO_Z, &z);
    
    	printf("gyro x:%f rad/s y:%f rad/s z:%f rad/s\n",
    			(double)out_ev(&x), (double)out_ev(&y), (double)out_ev(&z));
    
    	printf("trig_cnt:%d\n\n", trig_cnt);
    }
    
    static int set_sampling_freq(const struct device *dev)
    {
    	int ret = 0;
    	struct sensor_value odr_attr;
    
    	/* set accel/gyro sampling frequency to 12.5 Hz */
    	odr_attr.val1 = 12.5;
    	odr_attr.val2 = 0;
    
    	ret = sensor_attr_set(dev, SENSOR_CHAN_ACCEL_XYZ,
    			SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr);
    	if (ret != 0) {
    		printf("Cannot set sampling frequency for accelerometer.\n");
    		return ret;
    	}
    
    	ret = sensor_attr_set(dev, SENSOR_CHAN_GYRO_XYZ,
    			SENSOR_ATTR_SAMPLING_FREQUENCY, &odr_attr);
    	if (ret != 0) {
    		printf("Cannot set sampling frequency for gyro.\n");
    		return ret;
    	}
    
    	return 0;
    }
    
    #ifdef CONFIG_LSM6DSO_TRIGGER
    static void trigger_handler(const struct device *dev,
    			    const struct sensor_trigger *trig)
    {
    	fetch_and_display(dev);
    }
    
    static void test_trigger_mode(const struct device *dev)
    {
    	struct sensor_trigger trig;
    
    	if (set_sampling_freq(dev) != 0)
    		return;
    
    	trig.type = SENSOR_TRIG_DATA_READY;
    	trig.chan = SENSOR_CHAN_ACCEL_XYZ;
    
    	if (sensor_trigger_set(dev, &trig, trigger_handler) != 0) {
    		printf("Could not set sensor type and channel\n");
    		return;
    	}
    }
    
    #else
    static void test_polling_mode(const struct device *dev)
    {
    	if (set_sampling_freq(dev) != 0) {
    		return;
    	}
    
    	while (1) {
    		fetch_and_display(dev);
    		k_sleep(K_MSEC(1000));
    	}
    }
    #endif
    
    int main(void)
    {
    	printf("We are in main\n");
    	const struct device *const dev = DEVICE_DT_GET_ONE(st_lsm6dso);
    	if (!device_is_ready(dev)) {
    		printk("%s: device not ready.\n", dev->name);
    		return 0;
    	}
    
    #ifdef CONFIG_LSM6DSO_TRIGGER
    	printf("Testing LSM6DSO sensor in trigger mode.\n\n");
    	test_trigger_mode(dev);
    #else
    	printf("Testing LSM6DSO sensor in polling mode.\n\n");
    	test_polling_mode(dev);
    #endif
    	return 0;
    }
    

Children
No Data
Related