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

  • 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

Related