前言
本章介绍mpu6050的驱动添加以及测试。
其中驱动没有采用sdk提供的驱动,一方面需要配置irq,另一方面可以学习下如何通过ko方式添加驱动。
一、参考文章
驱动及测试文件编译流程:
https://community.milkv.io/t/risc-v-milk-v-lsm6dsr-i2c-module/284
代码来源:https://blog.csdn.net/m0_58844968/article/details/124994041
二、电路图
add接地,i2c地址为0x68
三、添加dts
duo-buildroot-sdk\build\boards\cv180x\cv1800b_milkv_duo_sd\dts_riscv\cv1800b_milkv_duo_sd.dts
mpu6050:mpu6050@68 {compatible = "invensense,mpu6050";reg = <0x68>;status = "okay";//interrupt-parent = <&gpio26>;//interrupts = <0 IRQ_TYPE_LEVEL_LOW>;
};
四、添加驱动
代码来源:https://blog.csdn.net/m0_58844968/article/details/124994041
注意驱动mpu6050.c中要和dts中的compatible一致
#ifndef MPU6050REG_H
#define MPU6050REG_Htypedef unsigned char uint8_t;/* register define */
#define MPU6050_SMPLRT_DIV 0x19 /*陀螺仪采样率,典型值:0x07(125Hz) */
#define MPU6050_CONFIG 0x1A /*低通滤波频率,典型值:0x06(5Hz)*/
#define MPU6050_GYRO_CONFIG 0x1B /*陀螺仪自检及测量范围,典型值:0x18(不自检,2000deg/s)*/
#define MPU6050_ACCEL_CONFIG 0x1C /*加速计自检、测量范围及高通滤波频率,典型值:0x01(不自检,2G,5Hz)*/
#define MPU6050_ACCEL_XOUT_H 0x3B /**/
#define MPU6050_ACCEL_XOUT_L 0x3C
#define MPU6050_ACCEL_YOUT_H 0x3D
#define MPU6050_ACCEL_YOUT_L 0x3E
#define MPU6050_ACCEL_ZOUT_H 0x3F
#define MPU6050_ACCEL_ZOUT_L 0x40
#define MPU6050_TEMP_OUT_H 0x41
#define MPU6050_TEMP_OUT_L 0x42
#define MPU6050_GYRO_XOUT_H 0x43
#define MPU6050_GYRO_XOUT_L 0x44
#define MPU6050_GYRO_YOUT_H 0x45
#define MPU6050_GYRO_YOUT_L 0x46
#define MPU6050_GYRO_ZOUT_H 0x47
#define MPU6050_GYRO_ZOUT_L 0x48
#define MPU6050_PWR_MGMT_1 0x6B /*电源管理,典型值:0x00(正常启用) */
#define MPU6050_WHO_AM_I 0x75 /* IIC地址寄存器(默认数值0x68,只读) */
#define MPU6050_SlaveAddress 0xD0 /*IIC写入时的地址字节数据,+1为读取*/
#define MPU6050_IIC_ADDR 0x68 /*MPU6050 IIC 器件地址*//* 中断状态寄存器*/
#define MPU6050_INT_STATUS 0x3A
#define MPU6050_INT_ENABLE 0x38
#define MPU6050_INT_PIN_CFG 0x37struct mpu6050_accel {short x;short y;short z;
};struct mpu6050_gyro {short x;short y;short z;
};struct mpu6050_data {struct mpu6050_accel accel;struct mpu6050_gyro gyro;
};#endif // __MPU6050REG_H
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
// #include <asm/mach/map.h>
// #include <asm/uaccess.h>
// #include <asm/io.h>
// #include <linux/mach/map.h>
#include <linux/uaccess.h>
#include <linux/io.h>
#include <linux/i2c.h>
#include "mpu6050reg.h"#define mpu6050dev_CNT 1
#define mpu6050dev_NAME "mpu6050"
#define MPU6050_USE_INT 0 /* 不使用中断 */struct mpu6050_dev{ int major; /* 主设备号 */ int minor; /* 次设备号 */ struct cdev cdev; /* cdev */struct class *class; /* 类 */struct device *device; /* 设备 */dev_t devid; /* 设备号 */struct i2c_client *client; void *private_data; /* 私有数据 */int16_t acceleration[3], gyro[3], temp; /* mpu6050数据 acceleration:加速度数据 gyro:陀螺仪数据 temp:温度指数据 */};static struct mpu6050_dev mpu6050dev;/** @description : 从mpu6050读取多个寄存器数据* [url=home.php?mod=space&uid=3142012]@param[/url] - dev: mpu6050设备* @param - reg: 要读取的寄存器首地址* @param - val: 读取到的数据* @param - len: 要读取的数据长度* [url=home.php?mod=space&uid=1141835]@Return[/url] : 操作结果*/
static int mpu6050_read_regs(struct mpu6050_dev *dev, u8 reg, void *val, int len)
{int ret;struct i2c_msg msg[2];struct i2c_client *client = (struct i2c_client *)dev->private_data;/* msg[0]为发送要读取的首地址 */msg[0].addr = client->addr; /* mpu6050地址 */msg[0].flags = 0; /* 标记为发送数据 */msg[0].buf = ® /* 读取的首地址 */msg[0].len = 1; /* reg长度*//* msg[1]读取数据 */msg[1].addr = client->addr; /* mpu6050地址 */msg[1].flags = I2C_M_RD; /* 标记为读取数据*/msg[1].buf = val; /* 读取数据缓冲区 */msg[1].len = len; /* 要读取的数据长度*/ret = i2c_transfer(client->adapter, msg, 2);if(ret == 2) {ret = 0;} else {printk("i2c read failed=%d reg=%06x len=%d\\n",ret, reg, len);ret = -EREMOTEIO;}return ret;
}/** @description : 向mpu6050多个寄存器写入数据* @param - dev: mpu6050设备* @param - reg: 要写入的寄存器首地址* @param - val: 要写入的数据缓冲区* @param - len: 要写入的数据长度* @return : 操作结果*/
static s32 mpu6050_write_regs(struct mpu6050_dev *dev, u8 reg, u8 *buf, u8 len)
{u8 b[256];struct i2c_msg msg;struct i2c_client *client = (struct i2c_client *)dev->private_data;b[0] = reg; /* 寄存器首地址 */memcpy(&b[1],buf,len); /* 将要写入的数据拷贝到数组b里面 */msg.addr = client->addr; /* mpu6050地址 */msg.flags = 0; /* 标记为写数据 */msg.buf = b; /* 要写入的数据缓冲区 */msg.len = len + 1; /* 要写入的数据长度 */return i2c_transfer(client->adapter, &msg, 1);
}/** @description : 读取mpu6050指定寄存器值,读取一个寄存器* @param - dev: mpu6050设备* @param - reg: 要读取的寄存器* @return : 读取到的寄存器值*/
static unsigned char mpu6050_read_reg(struct mpu6050_dev *dev, u8 reg)
{u8 data = 0;mpu6050_read_regs(dev, reg, &data, 1);return data;
}/** @description : 向mpu6050指定寄存器写入指定的值,写一个寄存器* @param - dev: mpu6050设备* @param - reg: 要写的寄存器* @param - data: 要写入的值* @return : 无*/
static void mpu6050_write_reg(struct mpu6050_dev *dev, u8 reg, u8 data)
{u8 buf = 0;buf = data;mpu6050_write_regs(dev, reg, &buf, 1);
}/*MPU6050 设备初始化 */
static void mpu6050_reset(void) {mpu6050_write_reg(&mpu6050dev,MPU6050_PWR_MGMT_1, 0x00); //解除休眠状态mpu6050_write_reg(&mpu6050dev,MPU6050_SMPLRT_DIV, 0x07);mpu6050_write_reg(&mpu6050dev,MPU6050_CONFIG, 0x06);mpu6050_write_reg(&mpu6050dev,MPU6050_GYRO_CONFIG, 0x18);mpu6050_write_reg(&mpu6050dev,MPU6050_ACCEL_CONFIG, 0x01);
}
/** @description : 读取mpu6050的数据,读取原始数据* @param - dev: mpu6050设备* @return : 无。*/
void mpu6050_readdata(struct mpu6050_dev *dev)
{unsigned char i =0;unsigned char buf[6];unsigned char val = 0x3B;/*从加速度寄存器(0x3B)开始读取6字节数据Start reading acceleration registers from register 0x3B for 6 bytes */mpu6050_read_regs(dev, val, buf, 6);for(i = 0; i < 3; i++) {dev->acceleration[i] = (buf[i * 2] << 8 | buf[(i * 2) + 1]);}/*从陀螺仪器数据寄存器(0x43)读取6字节数据The register is auto incrementing on each readNow gyro data from reg 0x43 for 6 bytesThe register is auto incrementing on each read*/val = 0x43;mpu6050_read_regs(dev, val, buf, 6);for(i = 0; i < 3; i++) {dev->gyro[i] = (buf[i * 2] << 8 | buf[(i * 2) + 1]);}/*从温度寄存器(0x41)读取2字节数据寄存器在每次读取时自动递增Now temperature from reg 0x41 for 2 bytesThe register is auto incrementing on each read*/val = 0x41;mpu6050_read_regs(dev, val, buf, 2);for(i = 0; i < 3; i++) {dev->temp = buf[0] << 8 | buf[1];}
}/** @description : 打开设备* @param - inode : 传递给驱动的inode* @param - filp : 设备文件,file结构体有个叫做private_data的成员变量* 一般在open的时候将private_data指向设备结构体。* @return : 0 成功;其他 失败*/
static int mpu6050_open(struct inode *inode,struct file *filp)
{filp->private_data = &mpu6050dev;printk("mpu6050-Drive_open\\r\\n");return 0;
}/** @description : 关闭/释放设备* @param - filp : 要关闭的设备文件(文件描述符)* @return : 0 成功;其他 失败*/
static int mpu6050_release(struct inode *inode,struct file *filp)
{return 0;
}/** @description : 从设备读取数据 * @param - filp : 要打开的设备文件(文件描述符)* @param - buf : 返回给用户空间的数据缓冲区* @param - cnt : 要读取的数据长度* @param - offt : 相对于文件首地址的偏移* @return : 读取的字节数,如果为负值,表示读取失败*/
static ssize_t mpu6050_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{int16_t MPU6050_data[7];long err = 0;struct mpu6050_dev *dev = (struct mpu6050_dev *)filp->private_data;mpu6050_readdata(dev);MPU6050_data[0] = dev->acceleration[0];MPU6050_data[1] = dev->acceleration[1];MPU6050_data[2] = dev->acceleration[2];MPU6050_data[3] = dev->gyro[0];MPU6050_data[4] = dev->gyro[1];MPU6050_data[5] = dev->gyro[2];MPU6050_data[6] = dev->temp;err = copy_to_user(buf, MPU6050_data, sizeof(MPU6050_data));return 0;}/*字符操作集*/
static const struct file_operations mpu6050_fops = {.open = mpu6050_open,.release = mpu6050_release,.read = mpu6050_read,.owner = THIS_MODULE,
};/** @description : i2c驱动的remove函数,移除i2c驱动的时候此函数会执行* @param - client : i2c设备* @return : 0,成功;其他负值,失败*/
static int mpu6050_remove(struct i2c_client *client)
{/* 卸载字符设备驱动 */cdev_del(&mpu6050dev.cdev);unregister_chrdev_region(mpu6050dev.devid,mpu6050dev_CNT);device_destroy(mpu6050dev.class, mpu6050dev.devid);class_destroy(mpu6050dev.class);printk("mpu6050-Drive_EXIT!\\r\\n");return 0;
}/*当 I2C 设备和 I2C 驱动匹配成功以后 probe 函数就会执行*//** @description : i2c驱动的probe函数,当驱动与* 设备匹配以后此函数就会执行* @param - client : i2c设备* @param - id : i2c设备ID* @return : 0,成功;其他负值,失败*/
static int mpu6050_probe(struct i2c_client *client,const struct i2c_device_id *id)
{printk("mpu6050_probe\\r\\n");/*搭建字符设备框架*//* 1.注册字符设备驱动 */mpu6050dev.major = 0;if(mpu6050dev.major){mpu6050dev.devid = MKDEV(mpu6050dev.major,0);register_chrdev_region(mpu6050dev.devid,mpu6050dev_CNT,mpu6050dev_NAME);}else{ alloc_chrdev_region(&mpu6050dev.devid,0,mpu6050dev_CNT,mpu6050dev_NAME);mpu6050dev.major = MAJOR(mpu6050dev.devid);mpu6050dev.minor = MINOR(mpu6050dev.devid);}/* 2.初始化cdev */mpu6050dev.cdev.owner = THIS_MODULE;cdev_init(&mpu6050dev.cdev,&mpu6050_fops);/* 3.添加cdev */cdev_add(&mpu6050dev.cdev,mpu6050dev.devid,mpu6050dev_CNT);/* 4.创建类 */mpu6050dev.class = class_create(THIS_MODULE,mpu6050dev_NAME);if (IS_ERR(mpu6050dev.class)) {return PTR_ERR(mpu6050dev.class);}/* 5.创建设备 */mpu6050dev.device = device_create(mpu6050dev.class, NULL, mpu6050dev.devid, NULL,mpu6050dev_NAME);if (IS_ERR(mpu6050dev.device)) {return PTR_ERR(mpu6050dev.device);}mpu6050dev.private_data = client;mpu6050_reset();printk("mpu6050_reset\\n");return 0;
}
/* 传统的匹配表 无设备树的时候匹配 ID 表 */
static struct i2c_device_id mpu6050_id[] = {{"invensense,mpu6050",0},{}
};/*设备匹配表*/
static struct of_device_id mpu6050_of_match[] ={{ .compatible = "invensense,mpu6050" },{}
};/*i2c_driver*/
static struct i2c_driver mpu6050_driver = {.driver = {.name = "mpu6050",.owner = THIS_MODULE,.of_match_table = of_match_ptr(mpu6050_of_match),},.probe = mpu6050_probe,.remove = mpu6050_remove,.id_table = mpu6050_id,
};static int __init mpu6050dev_init(void){int ret = 0;ret = i2c_add_driver(&mpu6050_driver);return 0;}static void __exit mpu6050dev_exit(void){i2c_del_driver(&mpu6050_driver);
}module_init(mpu6050dev_init);
module_exit(mpu6050dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("XXXX");
五、编写makefile
SDK_DIR = /home/milkv/duo_buildroot_sdk/duo-buildroot-sdk
KERN_DIR = $(SDK_DIR)/linux_5.10/build/cv1800b_milkv_duo_sdall:make -C $(KERN_DIR) M=$(PWD) modules$(CROSS_COMPILE)gcc mpu6050-app.c -o mpu6050-app -Wall -pthread -O2clean:make -C $(KERN_DIR) M=$(PWD) modules cleanrm -rf modules.orderrm -rf mpu6050-appobj-m += mpu6050.o
六、编写测试文件
//https://blog.csdn.net/m0_58844968/article/details/124994041
#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/ioctl.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include <poll.h>
#include <sys/select.h>
#include <sys/time.h>
#include <signal.h>
#include <fcntl.h>/** @description : main主程序* @param - argc : argv数组元素个数* @param - argv : 具体参数* @return : 0 成功;其他 失败*/
int main(int argc, char *argv[])
{int fd;char *filename;int16_t databuf[7];int16_t gyro_x_adc, gyro_y_adc, gyro_z_adc;int16_t accel_x_adc, accel_y_adc, accel_z_adc;int16_t temp_adc;int ret = 0;filename = "/dev/mpu6050";fd = open(filename, O_RDWR);if(fd < 0) {printf("can't open file %s\\r\\n", filename);return -1;}while (1) {ret = read(fd, databuf, sizeof(databuf));if(ret == 0) { /* 数据读取成功 */accel_x_adc = databuf[0];accel_y_adc = databuf[1];accel_z_adc = databuf[2];gyro_x_adc = databuf[3];gyro_y_adc = databuf[4];gyro_z_adc = databuf[5];temp_adc = databuf[6];printf("Acc. X = %d, Y = %d, Z = %d\\n", accel_x_adc, accel_y_adc, accel_z_adc);printf("Gyro. X = %d, Y = %d, Z = %d\\n", gyro_x_adc, gyro_y_adc, gyro_z_adc);// Temperature is simple so use the datasheet calculation to get deg C.// Note this is chip temperature.printf("Temp. = %f\\n", (temp_adc / 340.0) + 36.53);}usleep(1000000); /*1000ms */}close(fd); /* 关闭文件 */ return 0;
}
七、编译
make
file mpu6050-app
modinfo mpu6050.ko
八、安装ko
insmod mpu6050.ko
lsmod
dmesg | tail
ls /dev