1 Star 0 Fork 0

AnQi/linux_study

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mpu6050.c 3.90 KB
一键复制 编辑 原始数据 按行查看 历史
AnQi 提交于 2018-07-18 16:11 . stash
/*
* mpu6050.c
*
* Created on: 2018年6月25日
* Author: anqi
*/
#include "mpu6050.h"
#include <linux/i2c.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/device.h>
MODULE_LICENSE("Dual BSD/GPL")
#define DEV_CNT 1
#define DEV_MI 0
#define DEV_NAME "mpu6050"
dev_t dev_no;
struct class *cls;
struct mpu6050_pri mpu_dev;
static void mpu6050_write_byte(struct i2c_client *client,const unsigned char reg,const unsigned char val)
{
char txbuf[2] = {reg,val};
struct i2c_msg msg[2] =
{
[0] = {
.addr = client->addr,
.flags = W_FLG,
.len = sizeof(txbuf),
.buf = txbuf,
},
};
i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
}
static char mpu6050_read_byte(struct i2c_client *client,const unsigned char reg)
{
char txbuf[1] = {reg};
char rxbuf[1] = {0};
struct i2c_msg msg[2] =
{
[0] = {
.addr = client->addr,
.flags = W_FLG,
.len = sizeof(txbuf),
.buf = txbuf,
},
[1] = {
.addr = client->addr,
.flags = R_FLG,
.len = sizeof(txbuf),
.buf = txbuf,
},
};
i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
return rxbuf[0];
}
static long dev_ioctl(struct file *fp, unsigned int cmd, unsigned long arg)
{
int res;
union mpu6050_data data = {0};
switch(cmd)
{
case GET_ACCEL:
data.accel.x = mpu6050_read_byte(mpu_dev.client, ACCEL_XOUT_L);
data.accel.x |= mpu6050_read_byte(mpu_dev.client, ACCEL_XOUT_H) << 8;
data.accel.y = mpu6050_read_byte(mpu_dev.client, ACCEL_YOUT_L);
data.accel.y |= mpu6050_read_byte(mpu_dev.client, ACCEL_YOUT_H) << 8;
data.accel.z = mpu6050_read_byte(mpu_dev.client, ACCEL_ZOUT_L);
data.accel.z |= mpu6050_read_byte(mpu_dev.client, ACCEL_ZOUT_H) << 8;
break;
case GET_GYRO:
data.gyro.x = mpu6050_read_byte(mpu_dev.client, GYRO_XOUT_L);
data.gyro.x |= mpu6050_read_byte(mpu_dev.client, GYRO_XOUT_H) << 8;
data.gyro.y = mpu6050_read_byte(mpu_dev.client, GYRO_YOUT_L);
data.gyro.y |= mpu6050_read_byte(mpu_dev.client, GYRO_YOUT_H) << 8;
data.gyro.z = mpu6050_read_byte(mpu_dev.client, GYRO_ZOUT_L);
data.gyro.z |= mpu6050_read_byte(mpu_dev.client, GYRO_ZOUT_H) << 8;
break;
case GET_TEMP:
data.temp = mpu6050_read_byte(mpu_dev.client, TEMP_OUT_H);
data.temp |= mpu6050_read_byte(mpu_dev.client, TEMP_OUT_L) <<8;
break;
}
res = copy_to_user((void *)arg, &data, sizeof(data));//把数据拷贝到用户空间
return sizeof(data);
}
static int dev_open(struct inode *ip, struct file *fp)
{
return 0;
}
static int dev_release(struct inode *ip, struct file *fp)
{
return 0;
}
struct file_operations fops = {
.open = dev_open,
.release = dev_release,
.unlocked_ioctl = dev_ioctl,
};
static void mpu6050_init(struct i2c_client *client)
{
mpu6050_write_byte(client, PWR_MGMT_1, 0x00);
mpu6050_write_byte(client, SMPLRT_DIV, 0x07);
mpu6050_write_byte(client, CONFIG, 0x06);
mpu6050_write_byte(client, GYRO_CONFIG, 0x18);
mpu6050_write_byte(client, ACCEL_CONFIG, 0x0);
}
int mpu6050_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
mpu_dev.client = client;
printk("xj match ok\n");
cdev_init(&mpu_dev.cdev, &fops);
alloc_chrdev_region(&dev_no, DEV_CNT, DEV_MI, DEV_NAME);
cdev_add(&mpu_dev.cdev, dev_no, DEV_CNT);
mpu6050_init(client);
cls = class_create(THIS_MODULE, DEV_NAME);
device_create(cls, NULL, dev_no, NULL, "%s%d", DEV_NAME, DEV_MI);
printk("probe\n");
return 0;
}
int mpu6050_remove(struct i2c_client *)
{
device_destroy(cls, &dev_no);
class_destroy(cls);
unregister_chrdev_region(&dev_no, DEV_CNT);
return 0;
}
struct of_device_id mpu6050_dt_match =
{
{.compatible = "stm32,mpu6050"},
{},
};
struct i2c_device_id mpu6050_dev_match = {
};
struct i2c_driver mpu6050_drv =
{
.probe = mpu6050_probe,
.remove = mpu6050_remove,
.driver = {
.owner = THIS_MODULE,
.name = "mpu6050drv",
.of_match_table = of_match_ptr(mpu6050_dt_match),
},
.id_table = mpu6050_dev_match,
};
module_i2c_driver(mpu6050_drv);
MODULE_LICENSE("GPL")
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mygit245/linux_study.git
git@gitee.com:mygit245/linux_study.git
mygit245
linux_study
linux_study
master

搜索帮助