Interfacing MPU9250 with BMD300.

I have designed a custom board and it has following components:
BMD300 (Module based on nRF52832 IC)
MPU9250
GPS (L80-R QUECTEL)
There are some other components as well but for now I just want to interface MPU9250 with BMD300. For that I am using the sample provided in the nRF Connect SDK (V 2.6.0) in VS Code. 
Pin Connection:
MPU9250       :   BMD300
SDA                :   9
SCL                :   8
Int                   :   22

I have pulled up the SDA and SCL lines with 4.7k resistors. AD0 pin of MPU9250 pulled down with 10k resistor so the address would be 0x68 according to the data sheet. 

Prj.conf:

CONFIG_I2C=y
CONFIG_SENSOR=y
CONFIG_MPU6050_TRIGGER_NONE=y
CONFIG_CBPRINTF_FP_SUPPORT=y

CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=y
CONFIG_CLOCK_CONTROL_NRF_K32SRC_XTAL=n
CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC_CALIBRATION=y
CONFIG_NEWLIB_LIBC=y

CONFIG_LOG=y
CONFIG_RTT_CONSOLE=y

overlay file:
&i2c0 {
    mpu6050@68 {
        compatible = "invensense,mpu6050";
        reg = <0x68>;
        status = "okay";
        int-gpios = <&gpio0 22 GPIO_ACTIVE_HIGH>;
    };
};


Code builds without any errors. I am using J-Link (Segger) to program the BMD300. After uploading the code I get this:

Device mpu6050@68 is not ready
[00:00:00.500,152] <err> MPU6050: Failed to read chip ID.
*** Booting Zephyr OS build v3.5.99-ncs1 ***

BMD300 is getting the required power (3.2V) and so does the MPU9250. There are I2C header pins on my board so when I connect these pin with esp32. After uploading the code to the esp32 using Arduino IDE I am able to get the values.
  •  &i2c0 {
        mpu6050@68 {
            compatible = "invensense,mpu9250"; //updated due to copy past error
            reg = <0x68>;
            status = "okay";
            gyro-sr-div = <10>;
            gyro-dlpf = <5>;  //input fitting value
            gyro-fs = <250>; //input fitting value
            accel-fs = <2>;  //input fitting value
            accel-dlpf="5.05"; //input fitting value
        };
    };


    #include <zephyr/kernel.h>
    #include <zephyr/device.h>
    #include <zephyr/drivers/sensor.h>
    #include <stdio.h>

    static int process_mpu6050(const struct device *dev)
    {
        struct sensor_value temperature;
        struct sensor_value accel[3];
        struct sensor_value gyro[3];
        int rc = sensor_sample_fetch(dev);

        if (rc == 0) {
            rc = sensor_channel_get(dev, SENSOR_CHAN_ACCEL_XYZ,
                        accel);
        }
        if (rc == 0) {
            rc = sensor_channel_get(dev, SENSOR_CHAN_GYRO_XYZ,
                        gyro);
        }
        if (rc == 0) {
            rc = sensor_channel_get(dev, SENSOR_CHAN_DIE_TEMP,
                        &temperature);
        }
        if (rc == 0) {
            printf("[%s]:%g Cel\n"
                   "  accel %f %f %f m/s/s\n"
                   "  gyro  %f %f %f rad/s\n",
                   now_str(),
                   sensor_value_to_double(&temperature),
                   sensor_value_to_double(&accel[0]),
                   sensor_value_to_double(&accel[1]),
                   sensor_value_to_double(&accel[2]),
                   sensor_value_to_double(&gyro[0]),
                   sensor_value_to_double(&gyro[1]),
                   sensor_value_to_double(&gyro[2]));
        } else {
            printf("sample fetch/get failed: %d\n", rc);
        }

        return rc;
    }

    #ifdef CONFIG_MPU6050_TRIGGER
    static struct sensor_trigger trigger;

    static void handle_mpu6050_drdy(const struct device *dev,
                    const struct sensor_trigger *trig)
    {
        int rc = process_mpu6050(dev);

        if (rc != 0) {
            printf("cancelling trigger due to failure: %d\n", rc);
            (void)sensor_trigger_set(dev, trig, NULL);
            return;
        }
    }
    #endif /* CONFIG_MPU6050_TRIGGER */

    int main(void)
    {
        const struct device *const mpu6050 = DEVICE_DT_GET_ONE(invensense_mpu6050);
        // const struct device *const mpu6050 = DEVICE_DT_NAME(mpu6050);

        if (!device_is_ready(mpu6050)) {
            printf("Device %s is not ready\n", mpu6050->name);
            return 0;
        }

    #ifdef CONFIG_MPU6050_TRIGGER
        trigger = (struct sensor_trigger) {
            .type = SENSOR_TRIG_DATA_READY,
            .chan = SENSOR_CHAN_ALL,
        };
        if (sensor_trigger_set(mpu6050, &trigger,
                       handle_mpu6050_drdy) < 0) {
            printf("Cannot configure trigger\n");
            return 0;
        }
        printk("Configured for triggered sampling.\n");
    #endif

        while (!IS_ENABLED(CONFIG_MPU6050_TRIGGER)) {
            int rc = process_mpu6050(mpu6050);

            if (rc != 0) {
                break;
            }
            k_sleep(K_SECONDS(2));
        }

        /* triggered runs with its own thread after exit */
        return 0;
    }
  • One thing I removed the int-gpios property as it was giving error that this property is not mentioned in invensense,mpu9250

  • Hi, the following setup compiles for me. You need to include the irq-gpios

    //overlay
    &i2c1 {
        status="okay";
        compatible = "nordic,nrf-twim";
    
    
        mpu9250@68 {
            compatible = "invensense,mpu9250"; //updated due to copy past error
            reg = <0x68>;
            status = "okay";
            gyro-sr-div = <10>;
        	gyro-dlpf = <5>;  //input fitting value
        	gyro-fs = <250>; //input fitting value
        	accel-fs = <2>;  //input fitting value
        	accel-dlpf="5.05"; //input fitting value
            irq-gpios = <&gpio0 0 0 >;
    
        };
    };
    // prj.conf
    CONFIG_I2C=y
    CONFIG_SENSOR=y
    CONFIG_MPU9250=y
    
    //main
    #include <zephyr/kernel.h>
    #include <stdio.h>
    #include <zephyr/drivers/sensor.h>
    #include <zephyr/devicetree.h>
    #include <zephyr/device.h>
    #include <zephyr/drivers/gpio.h>
    #include <zephyr/drivers/i2c.h>
    
    
    int main(void)
    {
    	static const struct device *sensor = DEVICE_DT_GET_ANY(mpu9250);
    
        if (sensor == NULL || !device_is_ready(sensor))
        {
            printf("Could not get accel0 device\n");
            
        }
    
    	printf("Hello World! %s\n", CONFIG_BOARD);
    	return 0;
    }
    

    Regards

    Runar

  • Hi Runar,
    Thanks. Let me check and I will let you know if any error occurs.

  • One more question Runar. Should I change pins in overlay file or in dts? B/C I am using pin 8 and 9 as mentioned at the top.

Related