BMI270 interrupts seem not to be usable in nRF Connect SDK v2.2.0 with zephyr

Hi,

I am developing a nrf5340 based board containing the BMI270 IMU device via SPI interface like in the Thingy:53.

Unfortunately I am not able to set the device up for using the data ready interrupt and reacting to the interrupt via the zephyr device driver so far. The device works fine just polling for values regularly, but I need it interrupt driven. Looking at the official zephyr project documentation, the interrupt interface simply does not seem to be implemented for BMI270.

Is there a simple example I could take a look at for implementing an interrupt driven approach using the interrupt lines of the BMI270 that e.g. to send a data ready interrupt (which the device could provide, if configured accordingly) and react by just reading the 16 bit values via SPI? I need the zephyr RTOS for other things in the code, but maybe there is a simple way of implementing the BMI270 interrupt interface without using the BMI270 zephyr driver?

How is the BMI270 interrupt line 1 (that is connected in the schematics) used in the thingy:53 software?

Any help is appreciated!

Regards,

Jens

nRF Connect SDK v2.2.0 on macOS Ventura 13.2.1, Apple M1 Pro
nrf5340 on custom board

Parents
  • Hello again, and thank you for your patience. 

    First of all for some SPI interrupt examples, have a look here

    The cleanest way would probably be to modify the sensor driver, but the easiest would be to add the interrupt pin as an interrupt GPIO, and when triggered add reading the sensor value using sensor_sample_fetch() to a workqueue.

    How is the BMI270 interrupt line 1 (that is connected in the schematics) used in the thingy:53 software?

    Which software are you referring to? I believe it might just be included so that you can use it if you want to, not that we need to use it in one particular use-case.

    Regards,

    Elfving

  • Hi Elfving,

    Thank you very much for your answer. I have thought of using the pins as you suggested before I wrote my post. But I hesitated, because BMI270 will not simply send interrupts, but needs to be configured to do so by setting registers via SPI. I have not tried yet, but I was assuming that I cannot combine the BMI270 driver with the zephyr SPI driver for the same device - or can I?

    As for the Thingy:53 - I was only thinking that you might be using it in some way (or have tried using it), but I did not find any source on the net that uses this BMI270 interrupt line of the nrf5340 and the current device tree file you provide for the sdk does not show any entry for the interrupt (there is one for the first device, but the BMI270 is not set for interrupt in this tree).

    &spi3 {
    compatible = "nordic,nrf-spim";
    status = "okay";
    cs-gpios = <&gpio0 22 GPIO_ACTIVE_LOW>,
    <&gpio1 4 GPIO_ACTIVE_LOW>,
    <&gpio0 24 GPIO_ACTIVE_LOW>;

    pinctrl-0 = <&spi3_default>;
    pinctrl-1 = <&spi3_sleep>;
    pinctrl-names = "default", "sleep";

    adxl362: spi-dev-adxl362@0 {
    compatible = "adi,adxl362";
    spi-max-frequency = <8000000>;
    reg = <0>;
    int1-gpios = <&gpio0 19 0>;
    };

    bmi270: spi-dev-bmi270@1 {
    compatible = "bosch,bmi270";
    status = "disabled";
    spi-max-frequency = <8000000>;
    reg = <1>;
    };

    ...

    That was the reason for my question for a BMI270 example - I thought, maybe there might be something you plan to implement in the next version or you have some unofficial example code I could take a look at...

    Now, what's left of my question is:

    Do I have to abandon the BMI270 driver completely, if I need to use SPI communication to set registers in the device that is not supported by the device's driver, or can I mix drivers (pure SPI with the device specific one) on the same device? How would I configure that in the device tree?

    Thanks for your comments.

    Regards,

    Jens

  • Hi Elfving,
    I took a look at the BMI270 driver code and used that device specific knowledge to write my own little functions to set the needed registers etc.. and I am using the Standard GPIO-interrupt as you suggested. So my code works for now. But still it would be great, if these functions would officially be exposed through the attributes you can set in the sensor driver in some later version of zephyr and the SDK.

    Thanks a lot for your support. I think this ticket can be closed now.

    Regards, Jens

  • Great!

    Glad to hear that it got solved, and I'll forward this request to the relevant people.

    Regards,

    Elfving

  • Hey,

    would it be possible for you to share your solution for interrupt handling with BMI270?

  • In case you don't get a response here MatiM, feel free to open a new ticket for this. We'll be happy to help.

    Regards,

    Elfving

  • Hi MatiM,

    I cannot share the full code with you, but some snippets and the concept, I think. Currently I am running it unter nRF Connect SDK v2.4.0 - and I have not checked, if any new version of the driver now is supporting the interrupts.

    So her are the snippets of my code (from a test version) - with everything referencing my project removed - so there are some includes removed and some short specifics of the code, but I hope it is still a guide helping you along with all related to interrupt processing included...

    #include <zephyr/kernel.h>
    #include <zephyr/drivers/gpio.h>
    #include <zephyr/drivers/spi.h>
    #include <zephyr/drivers/sensor.h>
    
    #include "BMI270.h"
    #include "leds_IO_power.h"
    
    // setup GPIO-PIN for BMI270 interrupts
    #define GPIO_BMI270_INT1 12
    #define GPIO_BMI270_INT2 28
    
    // setup some structs for BMI270 interrupt callbacks
    static struct gpio_callback BMI270_int1_cb_data;
    static struct gpio_callback BMI270_int2_cb_data;
    
    // counters increased by the interrupts, decreased by the reaction
    atomic_t BMI270_int1_cnt;	// atomic avariables are always initialized to zero
    atomic_t BMI270_int2_cnt;	// atomic avariables are always initialized to zero
    
    // store reference to sensor device
    const struct device *const sensor_device = DEVICE_DT_GET_ONE(bosch_bmi270);
    
    // interrupt callbacks for BMI270
    void BMI270_int1_callback(const struct device *port, struct gpio_callback *cb, gpio_port_pins_t pins)
    {
    	atomic_inc(&BMI270_int1_cnt);
    }
    
    void BMI270_int2_callback(const struct device *port, struct gpio_callback *cb, gpio_port_pins_t pins)
    {
    	atomic_inc(&BMI270_int2_cnt);
    }
    
    int setup_BMI270_interrupt1(void)
    {
    	int err = 0;
    
    	if(!device_is_ready(gpio0Dev))
    		err = -ENODEV;
    	else
    	{
    		err = gpio_pin_configure(gpio0Dev, GPIO_BMI270_INT1, GPIO_INPUT | GPIO_ACTIVE_LOW);
    		if(!err)
    		{
    			err = gpio_pin_interrupt_configure(gpio0Dev, GPIO_BMI270_INT1, GPIO_INT_EDGE_TO_ACTIVE);
    			if(!err)
    			{
    				gpio_init_callback(&BMI270_int1_cb_data, BMI270_int1_callback, BIT(GPIO_BMI270_INT1));
    				err = gpio_add_callback(gpio0Dev, &BMI270_int1_cb_data);
    			}
    		}
    	}
    
    	return(err);
    }
    
    int setup_BMI270_interrupt2(void)
    {
    	int err = 0;
    
    	if(!device_is_ready(gpio0Dev))
    		err = -ENODEV;
    	else
    	{
    		err = gpio_pin_configure(gpio0Dev, GPIO_BMI270_INT2, GPIO_INPUT | GPIO_ACTIVE_LOW);
    		if(!err)
    		{
    			err = gpio_pin_interrupt_configure(gpio0Dev, GPIO_BMI270_INT2, GPIO_INT_EDGE_TO_ACTIVE);
    			if(!err)
    			{
    				gpio_init_callback(&BMI270_int2_cb_data, BMI270_int2_callback, BIT(GPIO_BMI270_INT2));
    				err = gpio_add_callback(gpio0Dev, &BMI270_int2_cb_data);
    			}
    		}
    	}
    
    	return(err);
    }
    
    int setup_bmi270(void)
    {
    	const struct bmi270_config *cfg;
    	uint8_t bmi_data_8;
     	struct sensor_value full_scale, sampling_freq, oversampling;
    
        int err = 0;
    
    	if (!device_is_ready(sensor_device))
        {
            err = -1;
    		return(err);
    	}
    
        // -------- setting up acceleration detection -----------------------------
    	// Setting scale in G, due to loss of precision, if SI unit m/s^2 is used
    	full_scale.val1 = (int)(BMI270_ACC_RANGE);   // set in G
    	full_scale.val2 = (int)((BMI270_ACC_RANGE - full_scale.val1) * 1000000.0);
    	sampling_freq.val1 = (int)(BMI270_SAMPLING_FREQUENCY); // in Hz
    	sampling_freq.val2 = (int)((BMI270_SAMPLING_FREQUENCY - sampling_freq.val1)
                                                                 * 1000000.0);
    	oversampling.val1 = 1;
    	oversampling.val2 = 0;
    
    	sensor_attr_set(sensor_device, SENSOR_CHAN_ACCEL_XYZ, 
                        SENSOR_ATTR_FULL_SCALE, &full_scale);
    	sensor_attr_set(sensor_device, SENSOR_CHAN_ACCEL_XYZ,
                        SENSOR_ATTR_OVERSAMPLING, &oversampling);
    	// Set sampling frequency last as this also sets the appropriate power
        // mode. If already sampling, change to 0.0Hz before changing other 
        // attributes
    	sensor_attr_set(sensor_device, SENSOR_CHAN_ACCEL_XYZ,
    			        SENSOR_ATTR_SAMPLING_FREQUENCY, &sampling_freq);
    
        // ---------- setting up yaw rate detection -------------------------------
    	// Setting scale in degrees/s to match the sensor scale
    	full_scale.val1 = (int)(BMI270_GYR_RANGE);                   // dps
    	full_scale.val2 = (int)((BMI270_GYR_RANGE - full_scale.val1) * 1000000.0);
    	sampling_freq.val1 = (int)(BMI270_SAMPLING_FREQUENCY);       // in Hz 
    	sampling_freq.val2 = (int)((BMI270_SAMPLING_FREQUENCY - sampling_freq.val1)
                                                                * 1000000.0);
    	oversampling.val1 = 1;                                       
    	oversampling.val2 = 0;
    
    	sensor_attr_set(sensor_device, SENSOR_CHAN_GYRO_XYZ,
                        SENSOR_ATTR_FULL_SCALE, &full_scale);
    	sensor_attr_set(sensor_device, SENSOR_CHAN_GYRO_XYZ,
                        SENSOR_ATTR_OVERSAMPLING, &oversampling);
    	// Set sampling frequency last as this also sets the appropriate power
        // mode. If already sampling, change to 0.0Hz before changing other 
        // attributes
    	sensor_attr_set(sensor_device, SENSOR_CHAN_GYRO_XYZ,
    			        SENSOR_ATTR_SAMPLING_FREQUENCY, &sampling_freq);
    
     	// ---------- change Register for interrupt data mapping and check --------
    
    	// write BMI270_REG_INT_MAP_DATA for putting drdy interupt to int1
    
    	bmi_data_8 = 0x04; // map drdy to int1
    	err = cfg->bus_io->write(&cfg->bus,BMI270_REG_INT_MAP_DATA,&bmi_data_8, 1);
    	if(err)
    		printk("error writing BMI270_REG_INT_MAP_DATA to BMI270: %d\n", err);
    	else
    		printk("value of 0x%x was written to BMI270_REG_INT_MAP_DATA\n", 
                                                                 bmi_data_8);
    
    	// read BMI270_REG_INT_MAP_DATA
    	err = cfg->bus_io->read(&cfg->bus, BMI270_REG_INT_MAP_DATA,&bmi_data_8, 1);
    	if(err)
    		printk("error reading BMI270_REG_INT_MAP_DATA from BMI270: %d\n", err);
    	else
    		printk("current value of BMI270_REG_INT_MAP_DATA is 0x%02x\n", 
                                                                 bmi_data_8);
    
    	bmi_data_8 = 0x08; // enable int1 as active low in push-pull mode
    	err = cfg->bus_io->write(&cfg->bus,BMI270_REG_INT1_IO_CTRL,&bmi_data_8, 1);
    	if(err)
    		printk("error writing BMI270_REG_INT1_IO_CTRL to BMI270: %d\n", err);
    	else
    		printk("value of 0x%x was written to BMI270_REG_INT1_IO_CTRL\n",
                                                                 bmi_data_8);
    
    	// read BMI270_REG_INT1_IO_CTRL
    	err = cfg->bus_io->read(&cfg->bus, BMI270_REG_INT1_IO_CTRL,&bmi_data_8, 1);
    	if(err)
    		printk("error reading BMI270_REG_INT1_IO_CTRL from BMI270: %d\n", err);
    	else
    		printk("current value of BMI270_REG_INT1_IO_CTRL is 0x%02x\n",
                                                                 bmi_data_8);
    
        err = setup_BMI270_interrupt1();
    	if(err)
    	{
    		printk("ERROR starting up BMI270 interrupt1 handling (%d)\n", err);
    	}
    
    	err = setup_BMI270_interrupt2();
    	if(err)
    	{
    		printk("ERROR starting up BMI270 interrupt2 handling (%d)\n", err);
    	}
    
        return(err);
    }
    
    int wait_for_drdy(void)
    {
        while(atomic_get(&BMI270_int1_cnt) <= 0)
        {
            // this is intended to be empty - it is a waiting function
        }
    
        // returns the counter after decrement 
    	return(atomic_dec(&BMI270_int1_cnt) - 1);
    }
    
    void clear_bmi270(void)
    {
        atomic_set(&BMI270_int1_cnt, 0);
    }
    
    int read_bmi270_data(struct imuSensorDataStruct *sensorData)
    {
    	struct bmi270_data *imu_data;
        int err = 0;
    
        if(!(err = sensor_sample_fetch(sensor_device)))
        {
     		imu_data = sensor_device->data;
    		sensorData->a.x = imu_data->ax;
    		sensorData->a.y = imu_data->ay;
    		sensorData->a.z = imu_data->az;
    		sensorData->g.x = imu_data->gx;
    		sensorData->g.y = imu_data->gy;
    		sensorData->g.z = imu_data->gz;
        }
        return(err);
    }
    

    The easiest way to use this in a test, would be to call "setup_bmi270" and then "wait_for_drdy" in a loop and "read_bmi270_data". On the HW side you would need to connect the BMI270 interrupts to the defined pins and configure those accordingly and you need to setup "Bosch_bmi270" for use with SPI in the device tree, of course.

    I hope, this is of some help to you.

    Regards,

    Jens

Reply
  • Hi MatiM,

    I cannot share the full code with you, but some snippets and the concept, I think. Currently I am running it unter nRF Connect SDK v2.4.0 - and I have not checked, if any new version of the driver now is supporting the interrupts.

    So her are the snippets of my code (from a test version) - with everything referencing my project removed - so there are some includes removed and some short specifics of the code, but I hope it is still a guide helping you along with all related to interrupt processing included...

    #include <zephyr/kernel.h>
    #include <zephyr/drivers/gpio.h>
    #include <zephyr/drivers/spi.h>
    #include <zephyr/drivers/sensor.h>
    
    #include "BMI270.h"
    #include "leds_IO_power.h"
    
    // setup GPIO-PIN for BMI270 interrupts
    #define GPIO_BMI270_INT1 12
    #define GPIO_BMI270_INT2 28
    
    // setup some structs for BMI270 interrupt callbacks
    static struct gpio_callback BMI270_int1_cb_data;
    static struct gpio_callback BMI270_int2_cb_data;
    
    // counters increased by the interrupts, decreased by the reaction
    atomic_t BMI270_int1_cnt;	// atomic avariables are always initialized to zero
    atomic_t BMI270_int2_cnt;	// atomic avariables are always initialized to zero
    
    // store reference to sensor device
    const struct device *const sensor_device = DEVICE_DT_GET_ONE(bosch_bmi270);
    
    // interrupt callbacks for BMI270
    void BMI270_int1_callback(const struct device *port, struct gpio_callback *cb, gpio_port_pins_t pins)
    {
    	atomic_inc(&BMI270_int1_cnt);
    }
    
    void BMI270_int2_callback(const struct device *port, struct gpio_callback *cb, gpio_port_pins_t pins)
    {
    	atomic_inc(&BMI270_int2_cnt);
    }
    
    int setup_BMI270_interrupt1(void)
    {
    	int err = 0;
    
    	if(!device_is_ready(gpio0Dev))
    		err = -ENODEV;
    	else
    	{
    		err = gpio_pin_configure(gpio0Dev, GPIO_BMI270_INT1, GPIO_INPUT | GPIO_ACTIVE_LOW);
    		if(!err)
    		{
    			err = gpio_pin_interrupt_configure(gpio0Dev, GPIO_BMI270_INT1, GPIO_INT_EDGE_TO_ACTIVE);
    			if(!err)
    			{
    				gpio_init_callback(&BMI270_int1_cb_data, BMI270_int1_callback, BIT(GPIO_BMI270_INT1));
    				err = gpio_add_callback(gpio0Dev, &BMI270_int1_cb_data);
    			}
    		}
    	}
    
    	return(err);
    }
    
    int setup_BMI270_interrupt2(void)
    {
    	int err = 0;
    
    	if(!device_is_ready(gpio0Dev))
    		err = -ENODEV;
    	else
    	{
    		err = gpio_pin_configure(gpio0Dev, GPIO_BMI270_INT2, GPIO_INPUT | GPIO_ACTIVE_LOW);
    		if(!err)
    		{
    			err = gpio_pin_interrupt_configure(gpio0Dev, GPIO_BMI270_INT2, GPIO_INT_EDGE_TO_ACTIVE);
    			if(!err)
    			{
    				gpio_init_callback(&BMI270_int2_cb_data, BMI270_int2_callback, BIT(GPIO_BMI270_INT2));
    				err = gpio_add_callback(gpio0Dev, &BMI270_int2_cb_data);
    			}
    		}
    	}
    
    	return(err);
    }
    
    int setup_bmi270(void)
    {
    	const struct bmi270_config *cfg;
    	uint8_t bmi_data_8;
     	struct sensor_value full_scale, sampling_freq, oversampling;
    
        int err = 0;
    
    	if (!device_is_ready(sensor_device))
        {
            err = -1;
    		return(err);
    	}
    
        // -------- setting up acceleration detection -----------------------------
    	// Setting scale in G, due to loss of precision, if SI unit m/s^2 is used
    	full_scale.val1 = (int)(BMI270_ACC_RANGE);   // set in G
    	full_scale.val2 = (int)((BMI270_ACC_RANGE - full_scale.val1) * 1000000.0);
    	sampling_freq.val1 = (int)(BMI270_SAMPLING_FREQUENCY); // in Hz
    	sampling_freq.val2 = (int)((BMI270_SAMPLING_FREQUENCY - sampling_freq.val1)
                                                                 * 1000000.0);
    	oversampling.val1 = 1;
    	oversampling.val2 = 0;
    
    	sensor_attr_set(sensor_device, SENSOR_CHAN_ACCEL_XYZ, 
                        SENSOR_ATTR_FULL_SCALE, &full_scale);
    	sensor_attr_set(sensor_device, SENSOR_CHAN_ACCEL_XYZ,
                        SENSOR_ATTR_OVERSAMPLING, &oversampling);
    	// Set sampling frequency last as this also sets the appropriate power
        // mode. If already sampling, change to 0.0Hz before changing other 
        // attributes
    	sensor_attr_set(sensor_device, SENSOR_CHAN_ACCEL_XYZ,
    			        SENSOR_ATTR_SAMPLING_FREQUENCY, &sampling_freq);
    
        // ---------- setting up yaw rate detection -------------------------------
    	// Setting scale in degrees/s to match the sensor scale
    	full_scale.val1 = (int)(BMI270_GYR_RANGE);                   // dps
    	full_scale.val2 = (int)((BMI270_GYR_RANGE - full_scale.val1) * 1000000.0);
    	sampling_freq.val1 = (int)(BMI270_SAMPLING_FREQUENCY);       // in Hz 
    	sampling_freq.val2 = (int)((BMI270_SAMPLING_FREQUENCY - sampling_freq.val1)
                                                                * 1000000.0);
    	oversampling.val1 = 1;                                       
    	oversampling.val2 = 0;
    
    	sensor_attr_set(sensor_device, SENSOR_CHAN_GYRO_XYZ,
                        SENSOR_ATTR_FULL_SCALE, &full_scale);
    	sensor_attr_set(sensor_device, SENSOR_CHAN_GYRO_XYZ,
                        SENSOR_ATTR_OVERSAMPLING, &oversampling);
    	// Set sampling frequency last as this also sets the appropriate power
        // mode. If already sampling, change to 0.0Hz before changing other 
        // attributes
    	sensor_attr_set(sensor_device, SENSOR_CHAN_GYRO_XYZ,
    			        SENSOR_ATTR_SAMPLING_FREQUENCY, &sampling_freq);
    
     	// ---------- change Register for interrupt data mapping and check --------
    
    	// write BMI270_REG_INT_MAP_DATA for putting drdy interupt to int1
    
    	bmi_data_8 = 0x04; // map drdy to int1
    	err = cfg->bus_io->write(&cfg->bus,BMI270_REG_INT_MAP_DATA,&bmi_data_8, 1);
    	if(err)
    		printk("error writing BMI270_REG_INT_MAP_DATA to BMI270: %d\n", err);
    	else
    		printk("value of 0x%x was written to BMI270_REG_INT_MAP_DATA\n", 
                                                                 bmi_data_8);
    
    	// read BMI270_REG_INT_MAP_DATA
    	err = cfg->bus_io->read(&cfg->bus, BMI270_REG_INT_MAP_DATA,&bmi_data_8, 1);
    	if(err)
    		printk("error reading BMI270_REG_INT_MAP_DATA from BMI270: %d\n", err);
    	else
    		printk("current value of BMI270_REG_INT_MAP_DATA is 0x%02x\n", 
                                                                 bmi_data_8);
    
    	bmi_data_8 = 0x08; // enable int1 as active low in push-pull mode
    	err = cfg->bus_io->write(&cfg->bus,BMI270_REG_INT1_IO_CTRL,&bmi_data_8, 1);
    	if(err)
    		printk("error writing BMI270_REG_INT1_IO_CTRL to BMI270: %d\n", err);
    	else
    		printk("value of 0x%x was written to BMI270_REG_INT1_IO_CTRL\n",
                                                                 bmi_data_8);
    
    	// read BMI270_REG_INT1_IO_CTRL
    	err = cfg->bus_io->read(&cfg->bus, BMI270_REG_INT1_IO_CTRL,&bmi_data_8, 1);
    	if(err)
    		printk("error reading BMI270_REG_INT1_IO_CTRL from BMI270: %d\n", err);
    	else
    		printk("current value of BMI270_REG_INT1_IO_CTRL is 0x%02x\n",
                                                                 bmi_data_8);
    
        err = setup_BMI270_interrupt1();
    	if(err)
    	{
    		printk("ERROR starting up BMI270 interrupt1 handling (%d)\n", err);
    	}
    
    	err = setup_BMI270_interrupt2();
    	if(err)
    	{
    		printk("ERROR starting up BMI270 interrupt2 handling (%d)\n", err);
    	}
    
        return(err);
    }
    
    int wait_for_drdy(void)
    {
        while(atomic_get(&BMI270_int1_cnt) <= 0)
        {
            // this is intended to be empty - it is a waiting function
        }
    
        // returns the counter after decrement 
    	return(atomic_dec(&BMI270_int1_cnt) - 1);
    }
    
    void clear_bmi270(void)
    {
        atomic_set(&BMI270_int1_cnt, 0);
    }
    
    int read_bmi270_data(struct imuSensorDataStruct *sensorData)
    {
    	struct bmi270_data *imu_data;
        int err = 0;
    
        if(!(err = sensor_sample_fetch(sensor_device)))
        {
     		imu_data = sensor_device->data;
    		sensorData->a.x = imu_data->ax;
    		sensorData->a.y = imu_data->ay;
    		sensorData->a.z = imu_data->az;
    		sensorData->g.x = imu_data->gx;
    		sensorData->g.y = imu_data->gy;
    		sensorData->g.z = imu_data->gz;
        }
        return(err);
    }
    

    The easiest way to use this in a test, would be to call "setup_bmi270" and then "wait_for_drdy" in a loop and "read_bmi270_data". On the HW side you would need to connect the BMI270 interrupts to the defined pins and configure those accordingly and you need to setup "Bosch_bmi270" for use with SPI in the device tree, of course.

    I hope, this is of some help to you.

    Regards,

    Jens

Children
Related