代码拉取完成,页面将自动刷新
/*
* 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")
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。