1. 程式人生 > >嵌入式Linux驅動筆記(十一)------i2c裝置之mpu6050驅動

嵌入式Linux驅動筆記(十一)------i2c裝置之mpu6050驅動

你好!這裡是風箏的部落格,

歡迎和我一起交流。

上一節講了i2c框架: 嵌入式Linux驅動筆記(十)——通俗易懂式瞭解i2c框架
這次就來寫一寫真正的i2c裝置驅動:
mpu6050是一款6軸運動處理元件,採用i2c通訊介面。
首先是廠家提供的mpu6050.h檔案:

#ifndef __MPU6050_H_
#define __MPU6050_H_
//定義MPU6050硬體地址
#define MPU_ADDR    0X68//接地為0X68 接高電平為0X69

//定義MPU6050暫存器地址
//#define MPU_ACCEL_OFFS_REG        0X06    //accel_offs暫存器,可讀取版本號,暫存器手冊未提到
//#define MPU_PROD_ID_REG 0X0C //prod id暫存器,在暫存器手冊未提到 #define MPU_SELF_TESTX_REG 0X0D //自檢暫存器X #define MPU_SELF_TESTY_REG 0X0E //自檢暫存器Y #define MPU_SELF_TESTZ_REG 0X0F //自檢暫存器Z #define MPU_SELF_TESTA_REG 0X10 //自檢暫存器A #define MPU_SAMPLE_RATE_REG 0X19 //取樣頻率分頻器 #define MPU_CFG_REG 0X1A //配置暫存器
#define MPU_GYRO_CFG_REG 0X1B //陀螺儀配置暫存器 #define MPU_ACCEL_CFG_REG 0X1C //加速度計配置暫存器 #define MPU_MOTION_DET_REG 0X1F //運動檢測閥值設定暫存器 #define MPU_FIFO_EN_REG 0X23 //FIFO使能暫存器 #define MPU_I2CMST_CTRL_REG 0X24 //IIC主機控制暫存器 #define MPU_I2CSLV0_ADDR_REG 0X25 //IIC從機0器件地址暫存器 #define MPU_I2CSLV0_REG 0X26 //IIC從機0資料地址暫存器
#define MPU_I2CSLV0_CTRL_REG 0X27 //IIC從機0控制暫存器 #define MPU_I2CSLV1_ADDR_REG 0X28 //IIC從機1器件地址暫存器 #define MPU_I2CSLV1_REG 0X29 //IIC從機1資料地址暫存器 #define MPU_I2CSLV1_CTRL_REG 0X2A //IIC從機1控制暫存器 #define MPU_I2CSLV2_ADDR_REG 0X2B //IIC從機2器件地址暫存器 #define MPU_I2CSLV2_REG 0X2C //IIC從機2資料地址暫存器 #define MPU_I2CSLV2_CTRL_REG 0X2D //IIC從機2控制暫存器 #define MPU_I2CSLV3_ADDR_REG 0X2E //IIC從機3器件地址暫存器 #define MPU_I2CSLV3_REG 0X2F //IIC從機3資料地址暫存器 #define MPU_I2CSLV3_CTRL_REG 0X30 //IIC從機3控制暫存器 #define MPU_I2CSLV4_ADDR_REG 0X31 //IIC從機4器件地址暫存器 #define MPU_I2CSLV4_REG 0X32 //IIC從機4資料地址暫存器 #define MPU_I2CSLV4_DO_REG 0X33 //IIC從機4寫資料暫存器 #define MPU_I2CSLV4_CTRL_REG 0X34 //IIC從機4控制暫存器 #define MPU_I2CSLV4_DI_REG 0X35 //IIC從機4讀資料暫存器 #define MPU_I2CMST_STA_REG 0X36 //IIC主機狀態暫存器 #define MPU_INTBP_CFG_REG 0X37 //中斷/旁路設定暫存器 #define MPU_INT_EN_REG 0X38 //中斷使能暫存器 #define MPU_INT_STA_REG 0X3A //中斷狀態暫存器 #define MPU_ACCEL_XOUTH_REG 0X3B //加速度值,X軸高8位暫存器 #define MPU_ACCEL_XOUTL_REG 0X3C //加速度值,X軸低8位暫存器 #define MPU_ACCEL_YOUTH_REG 0X3D //加速度值,Y軸高8位暫存器 #define MPU_ACCEL_YOUTL_REG 0X3E //加速度值,Y軸低8位暫存器 #define MPU_ACCEL_ZOUTH_REG 0X3F //加速度值,Z軸高8位暫存器 #define MPU_ACCEL_ZOUTL_REG 0X40 //加速度值,Z軸低8位暫存器 #define MPU_TEMP_OUTH_REG 0X41 //溫度值高八位暫存器 #define MPU_TEMP_OUTL_REG 0X42 //溫度值低8位暫存器 #define MPU_GYRO_XOUTH_REG 0X43 //陀螺儀值,X軸高8位暫存器 #define MPU_GYRO_XOUTL_REG 0X44 //陀螺儀值,X軸低8位暫存器 #define MPU_GYRO_YOUTH_REG 0X45 //陀螺儀值,Y軸高8位暫存器 #define MPU_GYRO_YOUTL_REG 0X46 //陀螺儀值,Y軸低8位暫存器 #define MPU_GYRO_ZOUTH_REG 0X47 //陀螺儀值,Z軸高8位暫存器 #define MPU_GYRO_ZOUTL_REG 0X48 //陀螺儀值,Z軸低8位暫存器 #define MPU_I2CSLV0_DO_REG 0X63 //IIC從機0資料暫存器 #define MPU_I2CSLV1_DO_REG 0X64 //IIC從機1資料暫存器 #define MPU_I2CSLV2_DO_REG 0X65 //IIC從機2資料暫存器 #define MPU_I2CSLV3_DO_REG 0X66 //IIC從機3資料暫存器 #define MPU_I2CMST_DELAY_REG 0X67 //IIC主機延時管理暫存器 #define MPU_SIGPATH_RST_REG 0X68 //訊號通道復位暫存器 #define MPU_MDETECT_CTRL_REG 0X69 //運動檢測控制暫存器 #define MPU_USER_CTRL_REG 0X6A //使用者控制暫存器 #define MPU_PWR_MGMT1_REG 0X6B //電源管理暫存器1 #define MPU_PWR_MGMT2_REG 0X6C //電源管理暫存器2 #define MPU_FIFO_CNTH_REG 0X72 //FIFO計數暫存器高八位 #define MPU_FIFO_CNTL_REG 0X73 //FIFO計數暫存器低八位 #define MPU_FIFO_RW_REG 0X74 //FIFO讀寫暫存器 #define MPU_DEVICE_ID_REG 0X75 //器件ID暫存器 #endif

再看下device部分檔案,可惜裝置樹還沒弄好,麻煩啊……
mpu_dev.c檔案:

#include <linux/module.h>
#include <linux/version.h>

#include <linux/init.h>

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/serial_core.h>
#include <linux/platform_device.h>
#include <linux/i2c.h>


static struct i2c_board_info mpu6050_info = {   
    I2C_BOARD_INFO("mpu6050", 0X68),//接地為0X68 接高電平為0X69
};

static struct i2c_client *mpu6050_client;

static int I2C_mpu6050_init(void)
{
    struct i2c_adapter *i2c_adap;

    i2c_adap = i2c_get_adapter(0);
    mpu6050_client = i2c_new_device(i2c_adap, &mpu6050_info);
    i2c_put_adapter(i2c_adap);

    return 0;
}

static void I2C_mpu6050_exit(void)
{
    i2c_unregister_device(mpu6050_client);
}

module_init(I2C_mpu6050_init);
module_exit(I2C_mpu6050_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("kite");/*modinfo my_keyboard.ko*/
MODULE_DESCRIPTION("A mpu6050 Module for testing module ");
MODULE_VERSION("V1.0");

因為是接在i2c0上,所以是獲取adapter0,同時寫上i2c器件的地址0x68。

再接著當然是driver部分了:
mpu_drv.c檔案:

#include <linux/module.h>
#include <linux/version.h>

#include <linux/init.h>

#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/interrupt.h>
#include <linux/list.h>
#include <linux/timer.h>
#include <linux/init.h>
#include <linux/serial_core.h>
#include <linux/platform_device.h>

#include <asm/mach/map.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include "mpu6050.h"


/* 1. 確定主裝置號 */
static int major;
static struct cdev mpu6050_cdev;
static struct class *cls;

static struct i2c_client * mpu6050_client;


static int mpu6050_read_len(struct i2c_client * client, unsigned char reg_add , unsigned char len, unsigned char *buf)
{
    int ret;

    /* 要讀取的那個暫存器的地址 */
    char txbuf = reg_add;

    struct i2c_msg msg[] = {
        {client->addr, 0, 1, &txbuf},       //0表示寫,
        {client->addr, I2C_M_RD, len, buf}, //讀資料
    };

    /* 通過i2c_transfer函式操作msg */
    ret = i2c_transfer(client->adapter, msg, 2);    //執行2條msg
    if (ret < 0)
    {
        printk("i2c_transfer read err\n");
        return -1;
    }

    return 0;
}


static int mpu6050_read_byte(struct i2c_client * client, unsigned char reg_add)
{
    int ret;

    /* 要讀取的那個暫存器的地址 */
    char txbuf = reg_add;

    /* 用來接收讀到的資料 */
    char rxbuf[1];

    /* i2c_msg指明要操作的從機地址,方向,長度,緩衝區 */
    struct i2c_msg msg[] = {
        {client->addr, 0, 1, &txbuf},       //0表示寫,
        {client->addr, I2C_M_RD, 1, rxbuf}, //讀資料
    };

    /* 通過i2c_transfer函式操作msg */
    ret = i2c_transfer(client->adapter, msg, 2);    //執行2條msg
    if (ret < 0)
    {
        printk("i2c_transfer read err\n");
        return -1;
    }

    return rxbuf[0];
}

static int mpu6050_write_byte(struct i2c_client * client, unsigned char reg_addr, unsigned char data)
{
    int ret;

    /* 要寫的那個暫存器的地址和要寫的資料 */
    char txbuf[] = {reg_addr, data};

    struct i2c_msg msg[] = {
        {client->addr, 0, 2, txbuf}//0表示寫
    };

    ret = i2c_transfer(client->adapter, msg, 1);
    if (ret < 0)
    {
        printk("i2c_transfer write err\n");
        return -1;
    }

    return 0;
}

static int mpu6050_open(struct inode *inode, struct file *file)   
{
    char res;

    printk("%s called\n", __func__);

    mpu6050_write_byte(mpu6050_client, MPU_PWR_MGMT1_REG, 0X80);/*復位MPU6050*/
    mdelay(100);
    mpu6050_write_byte(mpu6050_client, MPU_PWR_MGMT1_REG, 0X00);
    mpu6050_write_byte(mpu6050_client, MPU_GYRO_CFG_REG, 3<<3);/*陀螺儀感測器,±2000dps*/
    mpu6050_write_byte(mpu6050_client, MPU_ACCEL_CFG_REG, 0<<3);/*加速度感測器,±2g*/
    mpu6050_write_byte(mpu6050_client, MPU_SAMPLE_RATE_REG, 1000 /50-1);/*設定取樣率50Hz*/
    mpu6050_write_byte(mpu6050_client, MPU_CFG_REG, 4);/*自動設定LPF為取樣率的一半*/
    mpu6050_write_byte(mpu6050_client, MPU_INT_EN_REG, 0X00);/*關閉所有中斷*/
    mpu6050_write_byte(mpu6050_client, MPU_USER_CTRL_REG, 0X00);/*I2C主模式關閉*/
    mpu6050_write_byte(mpu6050_client, MPU_FIFO_EN_REG, 0X00);/*關閉FIFO*/
    mpu6050_write_byte(mpu6050_client, MPU_INTBP_CFG_REG, 0X80);/*INT引腳低電平有效*/

    res = mpu6050_read_byte(mpu6050_client, MPU_DEVICE_ID_REG);
    mpu6050_write_byte(mpu6050_client, MPU_CFG_REG, 3);//設定數字低通濾波器
    if (res == MPU_ADDR)//器件ID正確
    {
        printk("I2C ID is right ! \n");
        mpu6050_write_byte(mpu6050_client, MPU_PWR_MGMT1_REG, 0X01);    /*設定CLKSEL,PLL X軸為參考*/
        mpu6050_write_byte(mpu6050_client, MPU_PWR_MGMT2_REG, 0X00);    /*加速度與陀螺儀都工作*/
        return 0;
    }
    printk("failed !I2C ID is error ! \n");
    return 0;  
}  

static ssize_t mpu6050_read(struct file * file, char __user *buf, size_t count, loff_t *off)
{
    char val;
    unsigned char rxbuf[6], res;
    copy_from_user(&val, buf, 1);
    res = mpu6050_read_len(mpu6050_client, MPU_ACCEL_XOUTH_REG, 6 , rxbuf);
    if (res == 0)/* 加速度計原始資料   */
    {
        printk("ax = %d \n", ((u16)rxbuf[0] << 8) | rxbuf[1]);
        printk("ay = %d \n", ((u16)rxbuf[2] << 8) | rxbuf[3]);
        printk("az = %d \n", ((u16)rxbuf[4] << 8) | rxbuf[5]);
    }
    res = mpu6050_read_len(mpu6050_client, MPU_GYRO_XOUTH_REG, 6 , rxbuf);
    if (res == 0)/*陀螺儀原始資料*/
    {
        printk("gx = %d \n", ((u16)rxbuf[0] << 8) | rxbuf[1]);
        printk("gy = %d \n", ((u16)rxbuf[2] << 8) | rxbuf[3]);
        printk("gz = %d \n", ((u16)rxbuf[4] << 8) | rxbuf[5]);
    }
    return 0;
}
static ssize_t mpu6050_write(struct file *file, const char __user *buf, size_t count , loff_t * ppos)
{
    return 0;
}
static long mpu6050_ioctl(struct file *file, unsigned int cmd, unsigned long arg)  
{
    return 0;
}

/* 2. 構造file_operations */
static struct file_operations mpu6050_fops = {
    .owner  = THIS_MODULE,
    .open   = mpu6050_open,
    .read   = mpu6050_read,    
    .write  = mpu6050_write,
    .unlocked_ioctl   =   mpu6050_ioctl,
};

static int mpu6050_probe(struct i2c_client *client, const struct i2c_device_id *id)  
{
    int res;
    struct device *mpu6050_res;
    dev_t devid;

    mpu6050_client = client;

    /* 3. 告訴核心 */
#if 0
    major = register_chrdev(0, "hello", &hello_fops); /* (major,  0), (major, 1), ..., (major, 255)都對應hello_fops */
#else /*僅僅是註冊裝置號*/
    if (major) {
        devid = MKDEV(major, 0);
        register_chrdev_region(devid, 1, "mpu6050");  /* (major,0) 對應 pwm_fops, (major, 1~255)都不對應pwm_fops */
    } else {
        alloc_chrdev_region(&devid, 0, 1, "mpu6050"); /* (major,0) 對應 pwm_fops, (major, 1~255)都不對應pwm_fops */
        major = MAJOR(devid);                     
    }

    cdev_init(&mpu6050_cdev, &mpu6050_fops);
    res=cdev_add(&mpu6050_cdev, devid, 1);
    if(res)
    {
        printk("cdev_add failed\n");
        unregister_chrdev_region(MKDEV(major, 0), 1);
        return 0;
    }
#endif
    cls = class_create(THIS_MODULE, "mpu6050");
    mpu6050_res = device_create(cls, NULL, MKDEV(major, 0), NULL, "mpu6050"); /* /dev/xxx */
    if (IS_ERR(mpu6050_res)) 
    {
        printk("device_create failed\n");
        return 0;
    }

    return 0;   
}
static int mpu6050_remove(struct i2c_client *client)  
{  
    device_destroy(cls, MKDEV(major, 0));//class_device_destroy(cls,MKDEV(major, 0));

    class_destroy(cls);

    cdev_del(&mpu6050_cdev);
    unregister_chrdev_region(MKDEV(major, 0), 1);   

    return 0;  
}

static const struct i2c_device_id mpu6050_id[] = {  
    { "mpu6050", 0},  
    {}  
};

struct i2c_driver mpu6050_driver = {  
    .driver = {  
        .name           = "mpu6050",  
        .owner          = THIS_MODULE,  
    },  
    .probe      = mpu6050_probe,  
    .remove     = mpu6050_remove,  
    .id_table   = mpu6050_id,  
};

static int I2C_mpu6050_init(void)
{
    return i2c_add_driver(&mpu6050_driver);
}

static void I2C_mpu6050_exit(void)
{
    return i2c_del_driver(&mpu6050_driver);
}

module_init(I2C_mpu6050_init);
module_exit(I2C_mpu6050_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("kite");/*modinfo my_keyboard.ko*/
MODULE_DESCRIPTION("A i2c-mpu6050 Module for testing module ");
MODULE_VERSION("V1.0");

如果理解了之前講的i2c框架,這部分就很好理解咯。
i2c裝置的讀寫函式都是用到了i2c_transfer函式。

最後就是應用程式咯:
mpu6050_test.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char **argv)
{
    int fd;
    char val;
    fd = open("/dev/mpu6050", O_RDWR);
    if (fd < 0)
        printf("can't open /dev/pwm\n");
    else
        printf("can open /dev/pwm\n");

    read(fd, &val, 1);  

    return 0;
}

很簡單的i2c引用,再此小試牛刀了。

後記,編寫i2c驅動時,可以善用i2ctools,具體的可以去網上了解下:
cd /sys/bus/i2c/devices/
i2cdetect -y 0
這樣可以看到掛在i2c匯流排0下的所以i2c器件地址。
i2cdump -f -y 0 0x68
可以看到i2c匯流排0下的0x68地址的器件的暫存器內容