linux系统之字符设备驱动——IIC子系统驱动mma8451q
1. 原理图
2. 驱动程序
mma8451q.c
/*
* @Author: your name
* @Date: 2021-02-23 22:16:37
* @LastEditTime: 2021-02-24 13:57:31
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \6_I2Cmma8451q\mma8451q.c
*/
#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include "mma8451q.h"
#define WHO_AM_I 0x0d
#define NAME "mma8451q" // 设备节点名字
struct mma8451q_dev{
unsigned int major;
struct class *class; // 设备节点目录
struct device *dev; // 设备
struct i2c_client *client; // i2c子系统
}mma8451qDevice;
/**
* 使用i2c子系统读写寄存器
*/
int i2c_write_reg(unsigned char regaddr, char value)
{
int ret = 0;
char w_buf[] = {regaddr, value}; // 关键就在这
struct i2c_msg w_msg = {
.addr = mma8451qDevice.client->addr, // 从机设备地址
.flags = 0, // 0:写,1:I2C_M_RD读,I2C_M_TEN:10位从机地址
.len = 2, // 消息的长度
.buf = w_buf, // 消息的首地址
};
ret = i2c_transfer(mma8451qDevice.client->adapter, &w_msg, 1); // 有几个起始位,就有几个消息
if(ret != 1)
{
printk("sned i2c msg error!\n");
return -EAGAIN;
}
return 0;
}
int i2c_read_reg(unsigned char regaddr)
{
int ret = 0;
char value;
char r_buf[] = {regaddr};
struct i2c_msg r_msg[] = {
[0] = {
.addr = mma8451qDevice.client->addr,
.flags = 0,
.len = 1,
.buf = r_buf,
},
[1] = {
.addr = mma8451qDevice.client->addr,
.flags = I2C_M_RD,
.len = 1,
.buf = &value,
},
};
ret = i2c_transfer(mma8451qDevice.client->adapter, r_msg, 2);
if(ret != 2)
{
printk("recvive i2c msg error!\n");
return -EAGAIN;
}
return value;
}
int mma8451q_open (struct inode *inode, struct file *file)
{
printk("mma8451q_open OK!\n");
return 0;
}
int mma8451q_close (struct inode *inode, struct file *file)
{
printk("mma8451q_close OK!\n");
return 0;
}
ssize_t mma8451q_read (struct file *file, char __user *ubuf, size_t size, loff_t *loff)
{
int ret = 0;
char x_H, x_L, y_H, y_L, z_H, z_L;
short x, y, z;
struct mma8451q_accData{
short x_acc;
short y_acc;
short z_acc;
}acc_data;
if(size > sizeof(struct mma8451q_accData))
size = sizeof(struct mma8451q_accData);
x_H = i2c_read_reg(0x01);
x_L = i2c_read_reg(0x02);
x = x_H;
x = x << 8 | x_L;
acc_data.x_acc = x >> 2;
y_H = i2c_read_reg(0x01);
y_L = i2c_read_reg(0x02);
y = y_H;
y = y << 8 | y_L;
acc_data.y_acc = y >> 2;
z_H = i2c_read_reg(0x01);
z_L = i2c_read_reg(0x02);
z = z_H;
z = z << 8 | z_L;
acc_data.z_acc = z >> 2;
ret = copy_to_user(ubuf, &acc_data, size);
if(ret)
{
printk("copy data to user error\n");
return -EINVAL;
}
return size;
}
const struct file_operations fops =
{
.open = mma8451q_open,
.release = mma8451q_close,
.read = mma8451q_read,
};
int mma8451q_probe(struct i2c_client *cli, const struct i2c_device_id *id)
{
int ret = -1;
mma8451qDevice.client = cli;
printk("mma8451q_probe started\n");
// 1. 注册字符设备驱动
mma8451qDevice.major = register_chrdev(mma8451qDevice.major, NAME, &fops);
if(mma8451qDevice.major < 0)
{
printk("register_chrdev Error!\n");
goto ERR1;
}
// 2. 创建设备驱动目录
mma8451qDevice.class = class_create(THIS_MODULE, "cmma8451qdev");
if(IS_ERR(mma8451qDevice.class)){
printk("class_create Error!\n");
goto ERR2;
}
// 3. 创建设备驱动节点
mma8451qDevice.dev = device_create(mma8451qDevice.class, NULL, MKDEV(mma8451qDevice.major, 0), NULL, NAME);
if(IS_ERR(mma8451qDevice.dev)){
printk("device_create Error!\n");
ret = PTR_ERR(mma8451qDevice.dev);
goto ERR3;
}
// mmq8451q设备的初始化
ret = i2c_read_reg(WHO_AM_I);
printk("WHO_AM_I(0x1a) = %#x\n", ret);
printk("mma8451q_probe OK!\n");
i2c_write_reg(0x2A, 0X01); // 激活设备
i2c_write_reg(0x2B, 0X02); // 高精度模式
return 0;
ERR3:
class_destroy(mma8451qDevice.class);
ERR2:
unregister_chrdev(mma8451qDevice.major, NAME);
ERR1:
return ret;
}
int mma8451q_remove(struct i2c_client *cli)
{
device_destroy(mma8451qDevice.class, MKDEV(mma8451qDevice.major, 0));
// 3. 销毁设备驱动目录
class_destroy(mma8451qDevice.class);
// 4. 注销字符设备驱动
unregister_chrdev(mma8451qDevice.major, NAME);
printk("mma8451q_remove OK!\n");
return 0;
}
struct i2c_device_id mma8451q_id_table[] = {
{"mma8451q", 0},
{},
};
struct i2c_driver mma8451q_driver = {
.probe = mma8451q_probe,
.remove = mma8451q_remove,
.driver = {
.name = "mma8451q",
},
.id_table = mma8451q_id_table,
};
static int __init mma8451q_Init(void)
{
return i2c_add_driver(&mma8451q_driver);
}
static void __exit mma8451q_Exit(void)
{
i2c_del_driver(&mma8451q_driver);
}
module_init(mma8451q_Init);
module_exit(mma8451q_Exit);
MODULE_LICENSE("GPL");
- 应用程序
main.c
/*
* @Author: your name
* @Date: 2021-02-23 20:09:13
* @LastEditTime: 2021-02-24 14:55:45
* @LastEditors: Please set LastEditors
* @Description: In User Settings Edit
* @FilePath: \2-led\main.c
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
struct mma8451qData
{
short acc_x;
short acc_y;
short acc_z;
}acc_data;
#define g 9.8
float GetAccelerated(short acc)
{
return 4*g*acc/32768;
}
int main(int argc, char const *argv[])
{
int fd = open("/dev/mma8451q", O_RDWR);
if(fd < 0)
{
perror("open");
return -1;
}
while(1)
{
read(fd, &acc_data, sizeof(struct mma8451qData));
printf("%.4f\t%.4f,\t%.4f\r\n", GetAccelerated(acc_data.acc_x), GetAccelerated(acc_data.acc_y), GetAccelerated(acc_data.acc_z));
usleep(100000);
}
close(fd);
return 0;
}
3. 下载调试
这两个地方需要添加,才可以正常运行。