本实验在 Linux 下开发 I2C 接口器件驱动,重点是学习 Linux 下的 I2C 驱动框架,按照指定的框架去编写 I2C 设备驱动。
一、修改设备树
在驱动编写之前需要在设备树文件下添加具体的设备到I2C1下,由于驱动的器件ap3216c使用的是I2C1,因此需要在I2C1下加载子节点。
&i2c1 {
clock-frequency = <100000>;
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_i2c1>;
status = "okay";
ap3216c@1e {
compatible = "alientek,ap3216c";
reg = <0x1e>;
};
};
//I2C设备在设备树下挂载需要挂载到具体使用到的总线节点下
ap3216c 子节点,@后面的“1e”是 ap3216c 的器件地址;
设置 compatible 值为“alientek,ap3216c”;
reg 属性也是设置 ap3216c 器件地址的,因此 reg 设置为 0x1e。
设备树修改完成以后使用“make dtbs”重新编译一下,然后使用新的设备树启动 Linux 内
核。
/sys/bus/i2c/devices 目录下存放着所有 I2C 设备,如果设备树修改正确的话,会在
/sys/bus/i2c/devices 目录下看到一个名为“0-001e”的子目录;“0-001e”就是 ap3216c 的设备目录,“1e”就是 ap3216c 器件地址。进入0-001e 目录,可以看到“name”文件,name 文件就保存着此设备名字,在这里就是“ap3216c”。
二、AP3216C 驱动编写
#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/slab.h>
#include <linux/fs.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/errno.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <asm/mach/map.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/of_gpio.h>
#include <linux/timer.h>
#include <linux/ioctl.h>
#include <linux/input.h>
#include <linux/i2c.h>
#include "ap3216c.h"
#define AP3216C_CNT 1
#define AP3216C_NAME "ap3216c"
#define KEYVALUE 0XFE
#define KEYNOVALUE 0
struct ap3216c_dev{
int major;
int minor;
dev_t devid;
struct cdev cdev;
struct class *class;
struct device *device;
void *private_data;
};
static struct ap3216c_dev ap3216c;
/*
* @description : 从ap3216c读取多个寄存器数据
* @param - dev: ap3216c设备
* @param - reg: 要读取的寄存器首地址
* @param - val: 读取到的数据
* @param - len: 要读取的数据长度
* @return : 操作结果
*/
static int ap3216c_read_regs(struct ap3216c_dev *dev, u8 reg, void *val, int len)
{
struct i2c_client *client=(struct i2c_client *)dev->private_data;
struct i2c_msg msg[2];
//msg[0]表示发送的要读取的寄存器的地址
msg[0].addr=client->addr;/*从机地址*/
msg[0].flags=0;/*表示发送*/
msg[0].buf=®/*要发送的数据,也就是寄存器的地址*/
msg[0].len=1;/*要发送的数据长度*/
//msg[1]表示读取数据
msg[1].addr=client->addr;
msg[1].flags=I2C_M_RD;
msg[1].buf=val;/*接收到从机发送的数据*/
msg[1].len=len;/*要读取的寄存器的长度*/
return i2c_transfer(client->adapter,msg,2);
}
/*
* @description : 向ap3216c多个寄存器写入数据
* @param - dev: ap3216c设备
* @param - reg: 要写入的寄存器首地址
* @param - val: 要写入的数据缓冲区
* @param - len: 要写入的数据长度
* @return : 操作结果
*/
static s32 ap3216c_write_regs(struct ap3216c_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 u8 ap3216c_read_reg(struct ap3216c_dev *dev,u8 reg)
{
u8 data=0;
ap3216c_read_regs(dev, reg, &data, 1);
return data;
}
static void ap3216c_write_reg(struct ap3216c_dev *dev,u8 reg,u8 val)
{
ap3216c_write_regs(dev, reg, &val, 1);
}
/*
* @description : 读取AP3216C的数据,读取原始数据,包括ALS,PS和IR, 注意!
* : 如果同时打开ALS,IR+PS的话两次数据读取的时间间隔要大于112.5ms
* @param - ir : ir数据
* @param - ps : ps数据
* @param - ps : als数据
* @return : 无。
*/
void ap3216c_readdata(unsigned short *ir, unsigned short *ps, unsigned short *als)
{
unsigned char buf[6];
unsigned char i;
/* 循环读取所有传感器数据 */
for(i = 0; i < 6; i++)
{
buf[i] = ap3216c_read_reg(&ap3216c, AP3216C_IRDATALOW + i);
}
if(buf[0] & 0X80) /* IR_OF位为1,则数据无效 */
*ir = 0;
else /* 读取IR传感器的数据 */
*ir = ((unsigned short)buf[1] << 2) | (buf[0] & 0X03);
*als = ((unsigned short)buf[3] << 8) | buf[2]; /* 读取ALS传感器的数据 */
if(buf[4] & 0x40) /* IR_OF位为1,则数据无效 */
*ps = 0;
else /* 读取PS传感器的数据 */
*ps = ((unsigned short)(buf[5] & 0X3F) << 4) | (buf[4] & 0X0F);
}
/*
* @description : 打开设备
* @param - inode : 传递给驱动的inode
* @param - filp : 设备文件,file结构体有个叫做private_data的成员变量
* 一般在open的时候将private_data指向设备结构体。
* @return : 0 成功;其他 失败
*/
static int ap3216c_open(struct inode *inode, struct file *filp)
{
//filp->private_data = &ap3216c; /* 设置私有数据 */
printk("xiongxiao open\r\n");
u8 data;
/*初始化ap3216c*/
ap3216c_write_reg(&ap3216c, AP3216C_SYSTEMCONG, 0X04); /* 复位AP3216C */
mdelay(50); /* AP33216C复位至少10ms */
ap3216c_write_reg(&ap3216c, AP3216C_SYSTEMCONG, 0X03); /* 开启ALS、PS+IR */
data = ap3216c_read_reg(&ap3216c, AP3216C_SYSTEMCONG); /* 读取刚刚写进去的0X03 */
printk("%d\r\n",data);
return 0;
}
/*
* @description : 从设备读取数据
* @param - filp : 要打开的设备文件(文件描述符)
* @param - buf : 返回给用户空间的数据缓冲区
* @param - cnt : 要读取的数据长度
* @param - offt : 相对于文件首地址的偏移
* @return : 读取的字节数,如果为负值,表示读取失败
*/
static ssize_t ap3216c_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
short data[3];
long err = 0;
struct ap3216c_dev *dev = (struct ap3216c_dev *)filp->private_data;
unsigned short ir, ps, als;
ap3216c_readdata(&ir, &ps, &als);
data[0] = ir;
data[1] = als;
data[2] = ps;
err = copy_to_user(buf, data, sizeof(data));
printk("xiongxiao read\r\n");
return 0;
}
/*
* @description : 关闭/释放设备
* @param - filp : 要关闭的设备文件(文件描述符)
* @return : 0 成功;其他 失败
*/
static int ap3216c_release(struct inode *inode, struct file *filp)
{
printk("xiongxiao release\r\n");
return 0;
}
/* 设备操作函数 */
static struct file_operations ap3216c_fops = {
.owner = THIS_MODULE,
.open = ap3216c_open,
.read = ap3216c_read,
.release = ap3216c_release,
};
/*传统的匹配表*/
static struct i2c_device_id ap3216c_id[]={
{"alientek,ap3216c",0},
{}
};
/*设备树匹配表*/
static struct of_device_id ap3216c_of_match[]={
{.compatible="alientek,ap3216c"},
{}
};
static int ap3216c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
int ret=0;
/*搭建字符设备驱动框架*/
/* 注册字符设备驱动 */
/* 1、创建设备号 */
ap3216c.major=0;
if (ap3216c.major) { /* 定义了设备号 */
ap3216c.devid = MKDEV(ap3216c.major, 0);
register_chrdev_region(ap3216c.devid, AP3216C_CNT, AP3216C_NAME);
} else { /* 没有定义设备号 */
alloc_chrdev_region(&ap3216c.devid, 0, AP3216C_CNT, AP3216C_NAME); /* 申请设备号 */
ap3216c.major = MAJOR(ap3216c.devid); /* 获取分配号的主设备号 */
ap3216c.minor = MINOR(ap3216c.devid); /* 获取分配号的次设备号 */
}
printk("ap3216c major=%d,minor=%d\r\n",ap3216c.major, ap3216c.minor);
/* 2、初始化cdev */
ap3216c.cdev.owner = THIS_MODULE;
cdev_init(&ap3216c.cdev, &ap3216c_fops);
/* 3、添加一个cdev */
cdev_add(&ap3216c.cdev, ap3216c.devid, AP3216C_CNT);
/* 4、创建类 */
ap3216c.class = class_create(THIS_MODULE, AP3216C_NAME);
if (IS_ERR(ap3216c.class)) {
return PTR_ERR(ap3216c.class);
}
/* 5、创建设备 */
ap3216c.device = device_create(ap3216c.class, NULL, ap3216c.devid, NULL, AP3216C_NAME);
if (IS_ERR(ap3216c.device)) {
return PTR_ERR(ap3216c.device);
}
ap3216c.private_data=client; /*将此函数的第一个参数 client 传递给 ap3216cdev 的 private_data 成员变量*/
return 0;
}
static int ap3216c_remove(struct i2c_client *client)
{
/* 注销字符设备驱动 */
cdev_del(&ap3216c.cdev);/* 删除cdev */
unregister_chrdev_region(ap3216c.devid, AP3216C_CNT); /* 注销设备号 */
device_destroy(ap3216c.class, ap3216c.devid);
class_destroy(ap3216c.class);
return 0;
}
struct i2c_driver ap3216c_driver={
.probe=ap3216c_probe,
.remove=ap3216c_remove,
.driver={
.name="ap3216c",
.owner=THIS_MODULE,
.of_match_table=of_match_ptr(ap3216c_of_match),
},
.id_table=ap3216c_id,
};
static int __init ap3216c_init(void)
{
int ret =0;
ret=i2c_add_driver(&ap3216c_driver);
return ret;
}
static void __exit ap3216c_exit(void)
{
i2c_del_driver(&ap3216c_driver);
}
/*模块的加载与卸载*/
module_init(ap3216c_init);
module_exit(ap3216c_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("liuchuanqiang");
程序说明:
- ap3216c_read_regs从ap3216c器件中读取数据,这是驱动程序中调用的最终函数。该函数通过i2c_transfer函数与I2C主控制器驱动进行相联系。
- ap3216c_write_regs向ap3216c器件中写入数据。
3. ap3216c_read_reg 函数用于读取 AP3216C 的指定寄存器数据,用于一个寄存器的数据读 取;ap3216c_write_reg 函数用于向 AP3216C 的指定寄存器写入数据,用于一个寄存器的数据写操作。
4. ap3216c_readdata读取 AP3216C 的 PS、ALS 和 IR 等传感器原始数据值。
5. ap3216c_probe 函数,当 I2C 设备和驱动匹配成功以后此函数就会执行,和platform 驱动框架一样。此函数前面都是标准的字符设备注册代码,最后面会将此函数的第一个参数 client 传递给 ap3216cdev 的 private_data 成员变量。
6. ap3216c_id 匹配表,i2c_device_id 类型。用于传统的设备和驱动匹配,也就是没有使用设备树的时候。
7. ap3216c_of_match 匹配表,of_device_id 类型,用于设备树设备和驱动匹 配。这里只写了一个 compatible 属性,值为“alientek,ap3216c”。
8、i2c_add_driver(&ap3216c_driver);向Linux内核注册ap3216c_driver;
9、i2c_del_driver(&ap3216c_driver);向Linux内核注销ap3216c_driver。
三、编写测试 APP
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/input.h>
/*
argc:应用程序参数个数
argv[]:具体的参数内容,字符串形式,这个函数中的第一个参数内容./ap3216cAPP;
第二个参数内容filename;
执行文件格式:
./ap3216cAPP /dev/ap3216c
*/
int main(int argc,char *argv[])
{
int ret=0;
int fd=0;
char *filename;
unsigned short databuf[3];
unsigned short ir,als,ps;
filename=argv[1];//一个字符串的声明
printf("filename=%s\r\n",filename);
if(argc!=2)
{
printf("error\n");
return -1;
}
//打开文件
fd=open(filename, O_RDWR);
//判断有没有打开成功
if(fd<0)
{
printf("open failed\r\n");
return -1;
}
//read(fd,&data,sizeof(data));
while(1){
ret = read(fd, databuf, sizeof(databuf));
if(ret == 0) { /* 数据读取成功 */
ir = databuf[0]; /* ir传感器数据 */
als = databuf[1]; /* als传感器数据 */
ps = databuf[2]; /* ps传感器数据 */
printf("ir = %d, als = %d, ps = %d\r\n", ir, als, ps);
}
usleep(200000); /*200ms */
}
//关闭文件
ret=close(fd);
if(ret<0)
{
printf("close failed\r\n");
return -1;
}
return 0;
}
ap3216cApp.c 文件内容很简单,就是在 while 循环中不断的读取 AP3216C 的设备文件,从而得到 ir、als 和 ps 这三个数据值,然后将其输出到终端上。