• Stars
    star
    332
  • Rank 126,957 (Top 3 %)
  • Language
    C
  • License
    MIT License
  • Created about 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

MPU9250 full function driver for general MCU and Linux.

English | 简体中文 | 繁體中文 | 日本語 | Deutsch | 한국어

LibDriver MPU9250

MISRA API License

MPU9250 is a multi-chip module (MCM) consisting of two dies integrated into a single QFN package. One die houses the 3-Axis gyroscope and the 3-Axis accelerometer. The other die houses the AK8963 3-Axis magnetometer from Asahi Kasei Microdevices Corporation. Hence, the MPU9250 is a 9-axis MotionTracking device that combines a 3-axis gyroscope, 3-axis accelerometer, 3-axis magnetometer and a Digital Motion Processor™ (DMP) all in a small 3x3x1mm package available as a pin-compatible upgrade from the MPU6515.With its dedicated I2C sensor bus, the MPU9250 directly provides complete 9-axis MotionFusion™ output. The MPU9250 MotionTracking device, with its 9-axis integration, on-chip MotionFusion™, and runtime calibration firmware, enables manufacturers to eliminate the costly and complex selection, qualification, and system level integration of discrete devices, guaranteeing optimal motion performance for consumers.MPU9250 is also designed to interface with multiple non-inertial digital sensors, such as pressure sensors, on its auxiliary I2C port.MPU9250 features three 16-bit analog-to-digital converters (ADCs) for digitizing the gyroscope outputs, three 16-bit ADCs for digitizing the accelerometer outputs, and three 16-bit ADCs for digitizing the magnetometer outputs. For precision tracking of both fast and slow motions, the parts feature a user-programmable gyroscope full-scale range of ±250, ±500, ±1000, and ±2000°/sec (dps), a user-programmable accelerometer full-scale range of ±2g, ±4g, ±8g, and ±16g, and a magnetometer full-scale range of ±4800μT.

LibDriver MPU9250 is the full function driver of mpu9250 launched by LibDriver. It provides acceleration reading, angular velocity reading, magnetometer reading, attitude angle reading, dmp reading, tap detection and other functions. LibDriver is MISRA compliant.

Table of Contents

Instruction

/src includes LibDriver MPU9250 source files.

/interface includes LibDriver MPU9250 IIC, SPI platform independent template.

/test includes LibDriver MPU9250 driver test code and this code can test the chip necessary function simply.

/example includes LibDriver MPU9250 sample code.

/doc includes LibDriver MPU9250 offline document.

/datasheet includes MPU9250 datasheet.

/project includes the common Linux and MCU development board sample code. All projects use the shell script to debug the driver and the detail instruction can be found in each project's README.md.

/misra includes the LibDriver MISRA code scanning results.

Install

Reference /interface IIC, SPI platform independent template and finish your platform IIC, SPI driver.

Add /src, /interface and /example to your project.

Usage

example basic

#include "driver_mpu9250_basic.h"

uint8_t res;
uint32_t i;
uint32_t times;
float g[3];
float dps[3];
float ut[3];
float degrees;
mpu9250_address_t addr;

/* init */
addr = MPU9250_ADDRESS_AD0_LOW;
res = mpu9250_basic_init(MPU9250_INTERFACE_IIC, addr);
if (res != 0)
{
    return 1;
}

...
    
/* read all */
times = 3;
for (i = 0; i < times; i++)
{
    /* read */
    if (mpu9250_basic_read(g, dps, ut) != 0)
    {
        (void)mpu9250_basic_deinit();

        return 1;
    }

    ...
        
    if (mpu9250_basic_read_temperature(&degrees) != 0)
    {
        (void)mpu9250_basic_deinit();

        return 1;
    }

    ...
        
    /* output */
    mpu9250_interface_debug_print("mpu9250: %d/%d.\n", i + 1, times);
    mpu9250_interface_debug_print("mpu9250: acc x is %0.2fg.\n", g[0]);
    mpu9250_interface_debug_print("mpu9250: acc y is %0.2fg.\n", g[1]);
    mpu9250_interface_debug_print("mpu9250: acc z is %0.2fg.\n", g[2]);
    mpu9250_interface_debug_print("mpu9250: gyro x is %0.2fdps.\n", dps[0]);
    mpu9250_interface_debug_print("mpu9250: gyro y is %0.2fdps.\n", dps[1]);
    mpu9250_interface_debug_print("mpu9250: gyro z is %0.2fdps.\n", dps[2]);
    mpu9250_interface_debug_print("mpu9250: mag x is %0.2fuT.\n", ut[0]);
    mpu9250_interface_debug_print("mpu9250: mag y is %0.2fuT.\n", ut[1]);
    mpu9250_interface_debug_print("mpu9250: mag z is %0.2fuT.\n", ut[2]);
    mpu9250_interface_debug_print("mpu9250: temperature %0.2fC.\n", degrees);

    ...
        
    /* delay 1000 ms */
    mpu9250_interface_delay_ms(1000);

    ...
}

...
    
/* deinit */
(void)mpu9250_basic_deinit();

return 0;

example fifo

#include "driver_mpu9250_fifo.h"

uint32_t i;
uint32_t times;
uint16_t len;
uint8_t (*g_gpio_irq)(void) = NULL;
static int16_t gs_accel_raw[128][3];
static float gs_accel_g[128][3];
static int16_t gs_gyro_raw[128][3];
static float gs_gyro_dps[128][3];
atic int16_t gs_mag_raw[128][3];
static float gs_mag_ut[128][3];
mpu9250_address_t addr;

/* gpio init */
if (gpio_interrupt_init() != 0)
{
    return 1;
}
g_gpio_irq = mpu9250_fifo_irq_handler;

/* init */
addr = MPU9250_ADDRESS_AD0_LOW;
if (mpu9250_fifo_init(MPU9250_INTERFACE_IIC, addr) != 0)
{
    g_gpio_irq = NULL;
    (void)gpio_interrupt_deinit();

    return 1;
}

/* delay 100 ms */
mpu9250_interface_delay_ms(100);

...

times = 3;
for (i = 0; i < times; i++)
{
    len = 128;

    /* read */
    if (mpu9250_fifo_read(gs_accel_raw, gs_accel_g,
                          gs_gyro_raw, gs_gyro_dps, gs_mag_raw, gs_mag_ut, &len) != 0)
    {
        (void)mpu9250_fifo_deinit();
        g_gpio_irq = NULL;
        (void)gpio_interrupt_deinit();

        return 1;
    }
    
    ...
        
    /* output */
    mpu9250_interface_debug_print("mpu9250: %d/%d.\n", i + 1, times);
    mpu9250_interface_debug_print("mpu9250: fifo %d.\n", len);
    mpu9250_interface_debug_print("mpu9250: acc x[0] is %0.2fg.\n", gs_accel_g[0][0]);
    mpu9250_interface_debug_print("mpu9250: acc y[0] is %0.2fg.\n", gs_accel_g[0][1]);
    mpu9250_interface_debug_print("mpu9250: acc z[0] is %0.2fg.\n", gs_accel_g[0][2]);
    mpu9250_interface_debug_print("mpu9250: gyro x[0] is %0.2fdps.\n", gs_gyro_dps[0][0]);
    mpu9250_interface_debug_print("mpu9250: gyro y[0] is %0.2fdps.\n", gs_gyro_dps[0][1]);
    mpu9250_interface_debug_print("mpu9250: gyro z[0] is %0.2fdps.\n", gs_gyro_dps[0][2]);
    mpu9250_interface_debug_print("mpu9250: mag x[0] is %0.2fuT.\n", gs_mag_ut[0][0]);
    mpu9250_interface_debug_print("mpu9250: mag y[0] is %0.2fuT.\n", gs_mag_ut[0][1]);
    mpu9250_interface_debug_print("mpu9250: mag z[0] is %0.2fuT.\n", gs_mag_ut[0][2]);
    
    ...
        
    /* delay 100 ms */
    mpu9250_interface_delay_ms(100);
    
    ...
}

...
    
/* deinit */
(void)mpu9250_fifo_deinit();
g_gpio_irq = NULL;
(void)gpio_interrupt_deinit();

return 0;

example dmp

#include "driver_mpu9250_dmp.h"

uint32_t i;
uint32_t times;
uint32_t cnt;
uint16_t len;
uint8_t (*g_gpio_irq)(void) = NULL;
static int16_t gs_accel_raw[128][3];
static float gs_accel_g[128][3];
static int16_t gs_gyro_raw[128][3];      
static float gs_gyro_dps[128][3];
static int32_t gs_quat[128][4];          
static float gs_pitch[128];              
static float gs_roll[128];                
static float gs_yaw[128];                      
mpu9250_address_t addr;

static void a_receive_callback(uint8_t type)
{
    switch (type)
    {
        case MPU9250_INTERRUPT_MOTION :
        {
            mpu9250_interface_debug_print("mpu9250: irq motion.\n");
            
            break;
        }
        case MPU9250_INTERRUPT_FIFO_OVERFLOW :
        {
            mpu9250_interface_debug_print("mpu9250: irq fifo overflow.\n");
            
            break;
        }
        case MPU9250_INTERRUPT_FSYNC_INT :
        {
            mpu9250_interface_debug_print("mpu9250: irq fsync int.\n");
            
            break;
        }
        case MPU9250_INTERRUPT_DMP :
        {
            mpu9250_interface_debug_print("mpu9250: irq dmp\n");
            
            break;
        }
        case MPU9250_INTERRUPT_DATA_READY :
        {
            mpu9250_interface_debug_print("mpu9250: irq data ready\n");
            
            break;
        }
        default :
        {
            mpu9250_interface_debug_print("mpu9250: irq unknown code.\n");
            
            break;
        }
    }
}

static void a_dmp_tap_callback(uint8_t count, uint8_t direction)
{
    switch (direction)
    {
        case MPU9250_DMP_TAP_X_UP :
        {
            mpu9250_interface_debug_print("mpu9250: tap irq x up with %d.\n", count);
            
            break;
        }
        case MPU9250_DMP_TAP_X_DOWN :
        {
            mpu9250_interface_debug_print("mpu9250: tap irq x down with %d.\n", count);
            
            break;
        }
        case MPU9250_DMP_TAP_Y_UP :
        {
            mpu9250_interface_debug_print("mpu9250: tap irq y up with %d.\n", count);
            
            break;
        }
        case MPU9250_DMP_TAP_Y_DOWN :
        {
            mpu9250_interface_debug_print("mpu9250: tap irq y down with %d.\n", count);
            
            break;
        }
        case MPU9250_DMP_TAP_Z_UP :
        {
            mpu9250_interface_debug_print("mpu9250: tap irq z up with %d.\n", count);
            
            break;
        }
        case MPU9250_DMP_TAP_Z_DOWN :
        {
            mpu9250_interface_debug_print("mpu9250: tap irq z down with %d.\n", count);
            
            break;
        }
        default :
        {
            mpu9250_interface_debug_print("mpu9250: tap irq unknown code.\n");
            
            break;
        }
    }
}

static void a_dmp_orient_callback(uint8_t orientation)
{
    switch (orientation)
    {
        case MPU9250_DMP_ORIENT_PORTRAIT :
        {
            mpu9250_interface_debug_print("mpu9250: orient irq portrait.\n");
            
            break;
        }
        case MPU9250_DMP_ORIENT_LANDSCAPE :
        {
            mpu9250_interface_debug_print("mpu9250: orient irq landscape.\n");
            
            break;
        }
        case MPU9250_DMP_ORIENT_REVERSE_PORTRAIT :
        {
            mpu9250_interface_debug_print("mpu9250: orient irq reverse portrait.\n");
            
            break;
        }
        case MPU9250_DMP_ORIENT_REVERSE_LANDSCAPE :
        {
            mpu9250_interface_debug_print("mpu9250: orient irq reverse landscape.\n");
            
            break;
        }
        default :
        {
            mpu9250_interface_debug_print("mpu9250: orient irq unknown code.\n");
            
            break;
        }
    }
}

/* init */
if (gpio_interrupt_init() != 0)
{
    return 1;
}
g_gpio_irq = mpu9250_dmp_irq_handler;

/* init */
addr = MPU9250_ADDRESS_AD0_LOW;
if (mpu9250_dmp_init(MPU9250_INTERFACE_IIC, addr, a_receive_callback, 
                     a_dmp_tap_callback, a_dmp_orient_callback) != 0)
{
    g_gpio_irq = NULL;
    (void)gpio_interrupt_deinit();

    return 1;
}

/* delay 500 ms */
mpu9250_interface_delay_ms(500);

...
    
times = 3;
for (i = 0; i < times; i++)
{
    len = 128;

    /* read */
    if (mpu9250_dmp_read_all(gs_accel_raw, gs_accel_g,
                             gs_gyro_raw, gs_gyro_dps, 
                             gs_quat,
                             gs_pitch, gs_roll, gs_yaw,
                             &len) != 0)
    {
        (void)mpu9250_dmp_deinit();
        g_gpio_irq = NULL;
        (void)gpio_interrupt_deinit();

        return 1;
    }

    /* output */
    mpu9250_interface_debug_print("mpu9250: %d/%d.\n", i + 1, times);
    mpu9250_interface_debug_print("mpu9250: fifo %d.\n", len);
    mpu9250_interface_debug_print("mpu9250: pitch[0] is %0.2fdps.\n", gs_pitch[0]);
    mpu9250_interface_debug_print("mpu9250: roll[0] is %0.2fdps.\n", gs_roll[0]);
    mpu9250_interface_debug_print("mpu9250: yaw[0] is %0.2fdps.\n", gs_yaw[0]);
    mpu9250_interface_debug_print("mpu9250: acc x[0] is %0.2fg.\n", gs_accel_g[0][0]);
    mpu9250_interface_debug_print("mpu9250: acc y[0] is %0.2fg.\n", gs_accel_g[0][1]);
    mpu9250_interface_debug_print("mpu9250: acc z[0] is %0.2fg.\n", gs_accel_g[0][2]);
    mpu9250_interface_debug_print("mpu9250: gyro x[0] is %0.2fdps.\n", gs_gyro_dps[0][0]);
    mpu9250_interface_debug_print("mpu9250: gyro y[0] is %0.2fdps.\n", gs_gyro_dps[0][1]);
    mpu9250_interface_debug_print("mpu9250: gyro z[0] is %0.2fdps.\n", gs_gyro_dps[0][2]);

    /* delay 500 ms */
    mpu9250_interface_delay_ms(500);
    
    ....
        
    /* get the pedometer step count */
    res = mpu9250_dmp_get_pedometer_counter(&cnt);
    if (res != 0)
    {
        (void)mpu9250_dmp_deinit();
        g_gpio_irq = NULL;
        (void)gpio_interrupt_deinit();

        return 1;
    }
    
    ...
}

...

/* deinit */
(void)mpu9250_dmp_deinit();
g_gpio_irq = NULL;
(void)gpio_interrupt_deinit();

return 0;

Document

Online documents: https://www.libdriver.com/docs/mpu9250/index.html.

Offline documents: /doc/html/index.html.

Contributing

Please refer to CONTRIBUTING.md.

License

Copyright (c) 2015 - present LibDriver All rights reserved

The MIT License (MIT)

Permission is hereby granted, free of charge, to any person obtaining a copy

of this software and associated documentation files (the "Software"), to deal

in the Software without restriction, including without limitation the rights

to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

copies of the Software, and to permit persons to whom the Software is

furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all

copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

SOFTWARE.

Contact Us

Please sent an e-mail to [email protected].

More Repositories

1

w25qxx

W25QXX full function driver for general MCU and Linux.
C
470
star
2

ssd1306

SSD1306 full function driver for general MCU and Linux.
C
402
star
3

nrf24l01

NRF24L01 full function driver for general MCU and Linux.
C
323
star
4

mpu6050

MPU6050 full function driver for general MCU and Linux.
C
300
star
5

ds18b20

DS18B20 full function driver for general MCU and Linux.
C
233
star
6

adxl345

ADXL345 full function driver for general MCU and Linux.
C
170
star
7

mfrc522

MFRC522 full function driver for general MCU and Linux.
C
157
star
8

mifare_classic

MIFARE Classic full function driver for general MCU and Linux.
C
155
star
9

ws2812b

WS2812B full function driver for general MCU and Linux.
C
153
star
10

dht11

DHT11 full function driver for general MCU and Linux.
C
139
star
11

pcf8574

PCF8574 full function driver for general MCU and Linux.
C
109
star
12

max7219

MAX7219 full function driver for general MCU and Linux.
C
96
star
13

max30102

MAX30102 full function driver for general MCU and Linux.
C
95
star
14

nrf905

NRF905 full function driver for general MCU and Linux.
C
94
star
15

as5600

AS5600 full function driver for general MCU and Linux.
C
86
star
16

hmc5883l

HMC5883L full function driver for general MCU and Linux.
C
79
star
17

amg8833

AMG8833 full function driver for general MCU and Linux.
C
79
star
18

bmp180

BMP180 full function driver for general MCU and Linux.
C
75
star
19

pca9685

PCA9685 full function driver for general MCU and Linux.
C
70
star
20

hx711

HX711 full function driver for general MCU and Linux.
C
66
star
21

ds3231

DS3231 full function driver for general MCU and Linux.
C
64
star
22

htu21d

HTU21D full function driver for general MCU and Linux.
C
63
star
23

tea5767

TEA5767 full function driver for general MCU and Linux.
C
62
star
24

ir_remote

NEC IR remote decode full function driver for general MCU and Linux.
C
60
star
25

bh1750fvi

BH1750FVI full function driver for general MCU and Linux.
C
54
star
26

ms5837

MS5837 full function driver for general MCU and Linux.
C
52
star
27

max31865

MAX31865 full function driver for general MCU and Linux.
C
51
star
28

mifare_ultralight

MIFARE Ultralight full function driver for general MCU and Linux.
C
51
star
29

ads1115

ADS1115 full function driver for general MCU and Linux.
C
43
star
30

ssd1351

SSD1351 full function driver for general MCU and Linux.
C
40
star
31

sx1268

SX1268 full function driver for general MCU and Linux.
C
40
star
32

max30105

MAX30105 full function driver for general MCU and Linux.
C
39
star
33

st7920

ST7920 full function driver for general MCU and Linux.
C
39
star
34

sps30

SPS30 full function driver for general MCU and Linux.
C
36
star
35

sgp30

SGP30 full function driver for general MCU and Linux.
C
35
star
36

tsl2561

TSL2561 full function driver for general MCU and Linux.
C
35
star
37

pcf8591

PCF8591 full function driver for general MCU and Linux.
C
34
star
38

at24cxx

AT24CXX full function driver for general MCU and Linux.
C
32
star
39

ntag21x

NTAG21X full function driver for general MCU and Linux.
C
32
star
40

sht31

SHT31 full function driver for general MCU and Linux.
C
31
star
41

max6675

MAX6675 full function driver for general MCU and Linux.
C
31
star
42

ina219

INA219 full function driver for general MCU and Linux.
C
30
star
43

hcsr04

HCSR04 full function driver for general MCU and Linux.
C
27
star
44

l3gd20h

L3GD20H full function driver for general MCU and Linux.
C
25
star
45

apds9960

APDS9960 full function driver for general MCU and Linux.
C
25
star
46

mcp9600

MCP9600 full function driver for general MCU and Linux.
C
24
star
47

ld3320

LD3320 full function driver for general MCU and Linux.
C
24
star
48

mlx90614

MLX90614 full function driver for general MCU and Linux.
C
24
star
49

pmw3901mb

PMW3901MB full function driver for general MCU and Linux.
C
23
star
50

isd17xx

ISD17XX full function driver for general MCU and Linux.
C
22
star
51

aht20

AHT20 full function driver for general MCU and Linux.
C
22
star
52

dht20

DHT20 full function driver for general MCU and Linux.
C
21
star
53

max30205

MAX30205 full function driver for general MCU and Linux.
C
20
star
54

tcs34725

TCS34725 full function driver for general MCU and Linux.
C
20
star
55

syn6288

SYN6288 full function driver for general MCU and Linux.
C
19
star
56

bmp390

BMP390 full function driver for general MCU and Linux.
C
18
star
57

mcp4725

MCP4725 full function driver for general MCU and Linux.
C
18
star
58

bmp388

BMP388 full function driver for general MCU and Linux.
C
16
star
59

uvis25

UVIS25 full function driver for general MCU and Linux.
C
15
star
60

adxl362

ADXL362 full function driver for general MCU and Linux.
C
15
star
61

lm75b

LM75B full function driver for general MCU and Linux.
C
15
star
62

fm24clxx

FM24CLXX full function driver for general MCU and Linux.
C
15
star
63

ssd1681

SSD1681 full function driver for general MCU and Linux.
C
12
star
64

sgp41

SGP41 full function driver for general MCU and Linux.
C
4
star
65

sgp40

SGP40 full function driver for general MCU and Linux.
C
1
star
66

lan8720

LAN8720 full function driver for general MCU and Linux.
C
1
star
67

ch9121

CH9121 full function driver for general MCU and Linux.
C
1
star
68

em4100

EM4100 full function driver library for general MCU and Linux.
C
1
star