有关linux的i2c相关文章有一下几篇,他们互相关联,应该一同看:
1. 用 sysfs 实现 i2c,其实质是将 i2c 和 sysfs 结合起来,那首先来看看什么是 sysfs:
【1】sysfs简介:sysfs 是基于内存的虚拟的文件系统,他的作用于 proc 相似,他的设计是吸取的 proc 中的很多教训,使之导出内核或其他的数据的方式更为统一
【2】此部分,有一个博客写的很好,在这里,暂时不做整理,直接点击跳转
【3】借助于 sysfs ,我们可以把要呈现的参数/数据以文件夹的形式呈现给用户,最直接的好处是你能够用命令行来读(cat)写(echo)参数/数据
【4】对sysfs中的函数,有一篇文章写的很好,直接点击跳转
2. 说的再多,不如一个例子:
由于手头上有个 mpu6050,所以,就以mpu6050为例,采用的传感器的小板子是 GY-521
目录结构:
.
├── dev
│ ├── Makefile
│ └── mpu6050_dev.c
└── dri
├── Makefile
└── mpu6050_dri.c
mpu6050_dev.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/delay.h>
MODULE_LICENSE("GPL");
static struct i2c_client *mpu6050_clientp;
static struct i2c_board_info mpu6050_i2c_devs = {
.type = "mpu6050",
.addr = 0x68,
/*I2C_BOARD_INFO("mpu6050", 0x50), //这个宏是上边两个变量赋值的替换*/
};
static int mpu6050_dev_init(void)
{
struct i2c_adapter *i2c_adap; //分配一个适配器的指针
printk("mpu6050_dev_init.\n");
i2c_adap = i2c_get_adapter(0); //0 是 i2c 总线编号
mpu6050_clientp = i2c_new_device(i2c_adap, &mpu6050_i2c_devs); //只能添加一个
if (!mpu6050_clientp)
printk("register i2c error!\n");
i2c_put_adapter(i2c_adap); //释放 adapter
return 0;
}
static void mpu6050_dev_exit(void)
{
printk("mpu6050_dev_exit.\n");
i2c_unregister_device(mpu6050_clientp);
}
module_init(mpu6050_dev_init);
module_exit(mpu6050_dev_exit);
Makefile
ifeq ($(KERNELRELEASE),)
#KERNELDIR ?= /lib/modules/$(shell uname -r)/build
KERNELDIR ?= ~/wor_lip/linux-3.4.112
PWD := $(shell pwd)
modules:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
$(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install
clean:
rm -rf *.o *~ core .depend .*.cmd *.ko *.mod.c .tmp_versions modules* Module*
.PHONY: modules modules_install clean
else
obj-m := mpu6050_dev.o
endif
mpu6050_dri.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/delay.h>
MODULE_LICENSE("GPL");
#define SMPLRT_DIV 0x19
#define CONFIG 0x1A
#define GYRO_CONFIG 0x1B
#define ACCEL_CONFIG 0x1C
#define ACCEL_XOUT_H 0x3B
#define ACCEL_XOUT_L 0x3C
#define ACCEL_YOUT_H 0x3D
#define ACCEL_YOUT_L 0x3E
#define ACCEL_ZOUT_H 0x3F
#define ACCEL_ZOUT_L 0x40
#define TEMP_OUT_H 0x41
#define TEMP_OUT_L 0x42
#define GYRO_XOUT_H 0x43
#define GYRO_XOUT_L 0x44
#define GYRO_YOUT_H 0x45
#define GYRO_YOUT_L 0x46
#define GYRO_ZOUT_H 0x47
#define GYRO_ZOUT_L 0x48
#define PWR_MGMT_1 0x6B
static struct kobject *example_kobj;
struct i2c_client *glb_client;
static int mpu6050_read_byte(struct i2c_client *client, unsigned char reg)
{
int ret;
char txbuf[1] = { reg };
char rxbuf[1];
struct i2c_msg msg[] = {
{client->addr, 0, 1, txbuf},
{client->addr, I2C_M_RD, 1, rxbuf}
};
ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
if (ret < 0) {
printk("ret = %d\n", ret);
return ret;
}
return rxbuf[0];
}
static int mpu6050_write_byte(struct i2c_client *client, unsigned char reg, unsigned char val)
{
char txbuf[2] = {reg, val};
struct i2c_msg msg[] = {
{client->addr, 0, 2, txbuf},
};
i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
return 0;
}
/* 读/sys/kernel/mpu6050_i2c文件下的文件时调用 */
static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,
char *buf) /* buf是返回给用户空间的值 */
{
unsigned short accel_x = 0, accel_y = 0, accel_z = 0;
unsigned short gyro_x = 0, gyro_y = 0, gyro_z = 0;
unsigned short temp = 0;
mpu6050_write_byte(glb_client, PWR_MGMT_1, 0x00);
mpu6050_write_byte(glb_client, SMPLRT_DIV, 0x07);
mpu6050_write_byte(glb_client, CONFIG, 0x06);
mpu6050_write_byte(glb_client, GYRO_CONFIG, 0x18);
mpu6050_write_byte(glb_client, ACCEL_CONFIG, 0x01);
mdelay(10);
accel_x = mpu6050_read_byte(glb_client, ACCEL_XOUT_L);
accel_x |= mpu6050_read_byte(glb_client, ACCEL_XOUT_H) << 8;
accel_y = mpu6050_read_byte(glb_client, ACCEL_YOUT_L);
accel_y |= mpu6050_read_byte(glb_client, ACCEL_YOUT_H) << 8;
accel_z = mpu6050_read_byte(glb_client, ACCEL_ZOUT_L);
accel_z |= mpu6050_read_byte(glb_client, ACCEL_ZOUT_H) << 8;
printk("acceleration data: x = %04x, y = %04x, z = %04x\n", accel_x, accel_y, accel_z);
gyro_x = mpu6050_read_byte(glb_client, GYRO_XOUT_L);
gyro_x |= mpu6050_read_byte(glb_client, GYRO_XOUT_H) << 8;
gyro_y = mpu6050_read_byte(glb_client, GYRO_YOUT_L);
gyro_y |= mpu6050_read_byte(glb_client, GYRO_YOUT_H) << 8;
gyro_z = mpu6050_read_byte(glb_client, GYRO_ZOUT_L);
gyro_z |= mpu6050_read_byte(glb_client, GYRO_ZOUT_H) << 8;
printk("gyroscope data: x = %04x, y = %04x, z = %04x\n", gyro_x, gyro_y, gyro_z);
temp = mpu6050_read_byte(glb_client, TEMP_OUT_L);
temp |= mpu6050_read_byte(glb_client, TEMP_OUT_H) << 8;
printk("temperature data: %x\n", temp);
return sprintf(buf, "%x\n", temp);
}
/* 写/sys/kernel/mpu6050_i2c文件下的文件时调用 */
static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr,
const char *buf, size_t count)
{
return 0;
}
static struct kobj_attribute foo_attribute =
__ATTR(mpu6050_accelerationd, 0666, foo_show, foo_store);
/* (文件名, 文件权限, 读这个文件时调用的函数, 写文件时调用时的函数)
* 在/sys/kernel/mpu6050_i2c/下的文件
*/
static struct kobj_attribute foo_attribute2 =
__ATTR(mpu6050_gyroscope, 0666, foo_show, foo_store);
static struct kobj_attribute foo_attribute3 =
__ATTR(mpu6050_temperature, 0666, foo_show, foo_store);
/*
* 创建一个属性组,方便我们一次性的创建和删除
*/
static struct attribute *attrs[] = {
&foo_attribute.attr,
&foo_attribute2.attr,
&foo_attribute3.attr,
NULL, /* 需要用NULL来表示属性列表的结束 */
};
/*
* An unnamed attribute group will put all of the attributes directly in
* the kobject directory. If we specify a name, a subdirectory will be
* created for the attributes with the directory being the name of the
* attribute group.
*/
static struct attribute_group attr_group = {
.attrs = attrs,
};
static int mpu6050_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
int retval;
glb_client = client;
/*
* 创建目录 /sys/kernel/mpu6050_i2c
*/
example_kobj = kobject_create_and_add("mpu6050_i2c", kernel_kobj); /* (kernel_kobj这个父文件夹下的叫mpu6050_i2c的文件夹) */
if (!example_kobj)
return -ENOMEM;
/* Create the files associated with this kobject */
retval = sysfs_create_group(example_kobj, &attr_group);
if (retval)
kobject_put(example_kobj);
return 0;
}
static int mpu6050_remove(struct i2c_client *client)
{
/*struct bin_attribute *binp;*/
/*binp = i2c_get_clientdata(client);*/
/*sysfs_remove_bin_file(&client->dev.kobj, binp);*/
kobject_put(example_kobj);
printk("driver removed.\n");
return 0;
}
static const struct i2c_device_id mpu6050_id[] = {
{ "mpu6050", 0 },
};
/*MODULE_DEVICE_TABLE(i2c, mpu6050_id);*/
static struct i2c_driver mpu6050_driver = {
.driver = {
.name = "xx", //供模块匹配用
.owner = THIS_MODULE,
},
.probe = mpu6050_probe,
.remove = mpu6050_remove,
.id_table = mpu6050_id, /* 用于I2C driver的probe函数调用 */
};
/*i2c_add_driver();*/
module_i2c_driver(mpu6050_driver);//简单方式,内核中是个宏,完成了模块的初始化和卸载
【1】当设备 和 驱动 匹配成功后,会在 /sys/kernel/ 下创建一个mpu6050_i2c 目录,里边有3个文件,如下图
【2】读传感器的值
【3】很明显,程序中3个参数以文件夹的方式呈现给了用户,用户可以像操作文件的形式读写相应文件,在本程序中,我偷了个懒,读这3个参数执行了相同的函数,没有实现写的函数
【4】可以想到的是,程序中可以实现不同的读函数,实现单独的参数读取