嵌入式Linux驱动笔记(十一)------i2c设备之mpu6050驱动

时间:2021-10-15 16:22:51

你好!这里是风筝的博客,

欢迎和我一起交流。

上一节讲了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引用,再此小试牛刀了。