前言
本章介绍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_H
typedef unsigned char uint8_t;
#define MPU6050_SMPLRT_DIV 0x19
#define MPU6050_CONFIG 0x1A
#define MPU6050_GYRO_CONFIG 0x1B
#define MPU6050_ACCEL_CONFIG 0x1C
#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
#define MPU6050_WHO_AM_I 0x75
#define MPU6050_SlaveAddress 0xD0
#define MPU6050_IIC_ADDR 0x68
#define MPU6050_INT_STATUS 0x3A
#define MPU6050_INT_ENABLE 0x38
#define MPU6050_INT_PIN_CFG 0x37
struct 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
#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 <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;
struct class *class;
struct device *device;
dev_t devid;
struct i2c_client *client;
void *private_data;
int16_t acceleration[3], gyro[3], temp;
};
static struct mpu6050_dev mpu6050dev;
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].addr = client->addr;
msg[0].flags = 0;
msg[0].buf = ®
msg[0].len = 1;
msg[1].addr = client->addr;
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;
}
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);
msg.addr = client->addr;
msg.flags = 0;
msg.buf = b;
msg.len = len + 1;
return i2c_transfer(client->adapter, &msg, 1);
}
static unsigned char mpu6050_read_reg(struct mpu6050_dev *dev, u8 reg)
{
u8 data = 0;
mpu6050_read_regs(dev, reg, &data, 1);
return data;
}
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);
}
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);
}
void mpu6050_readdata(struct mpu6050_dev *dev)
{
unsigned char i =0;
unsigned char buf[6];
unsigned char val = 0x3B;
mpu6050_read_regs(dev, val, buf, 6);
for(i = 0; i < 3; i++)
{
dev->acceleration[i] = (buf[i * 2] << 8 | buf[(i * 2) + 1]);
}
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]);
}
val = 0x41;
mpu6050_read_regs(dev, val, buf, 2);
for(i = 0; i < 3; i++)
{
dev->temp = buf[0] << 8 | buf[1];
}
}
static int mpu6050_open(struct inode *inode,struct file *filp)
{
filp->private_data = &mpu6050dev;
printk("mpu6050-Drive_open\r\n");
return 0;
}
static int mpu6050_release(struct inode *inode,struct file *filp)
{
return 0;
}
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,
};
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;
}
static int mpu6050_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
printk("mpu6050_probe\r\n");
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);
}
mpu6050dev.cdev.owner = THIS_MODULE;
cdev_init(&mpu6050dev.cdev,&mpu6050_fops);
cdev_add(&mpu6050dev.cdev,mpu6050dev.devid,mpu6050dev_CNT);
mpu6050dev.class = class_create(THIS_MODULE,mpu6050dev_NAME);
if (IS_ERR(mpu6050dev.class)) {
return PTR_ERR(mpu6050dev.class);
}
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;
}
static struct i2c_device_id mpu6050_id[] = {
{"invensense,mpu6050",0},
{}
};
static struct of_device_id mpu6050_of_match[] ={
{ .compatible = "invensense,mpu6050" },
{}
};
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_sd
all:
make -C $(KERN_DIR) M=$(PWD) modules
$(CROSS_COMPILE)gcc mpu6050-app.c -o mpu6050-app -Wall -pthread -O2
clean:
make -C $(KERN_DIR) M=$(PWD) modules clean
rm -rf modules.order
rm -rf mpu6050-app
obj-m += mpu6050.o
六、编写测试文件
#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>
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);
printf("Temp. = %f\n", (temp_adc / 340.0) + 36.53);
}
usleep(1000000);
}
close(fd);
return 0;
}
七、编译
make
file mpu6050-app
modinfo mpu6050.ko
八、安装ko
insmod mpu6050.ko
lsmod
dmesg | tail
ls /dev
九、测试