本实验基于正点原子ALPHT开发板上的AP3216C作为实验开展对象

基础知识
1.IIC总线驱动
  IIC总线驱动是对IIC硬件体系结构中适配器端的实现,适配器可由CPU控制,甚至可以直接集成在CPU内部。IIC适配器在内核中使用了i2c_adapter结构体,IIC适配器核心就是申请一个i2c_adapter结构体,然后设置 i2c_algorithm 中的 master_xfer 函数。
(1)i2c_adapter和 i2c_algorithm
i2c_adapter对应物理上的一个适配器,而 i2c_algorithm对应一套通信方法。它们两个缺少哪一个都是什么都做不了。

2.IIC设备驱动
  IIC设备驱动是对IIC硬件体系结构中设备端的实现,设备一般挂载在受CPU控制的IIC适配器上,通过IIC适配器与CPU交换数据。IIC设备驱动重点关注两个数据结构:i2c_client和i2c_driver。注意,i2c_client是不需要我们编写的,但是i2c_driver是重点,是需要我们去编写的,i2c_driver类似platform平台,也存在probe函数。
i2c_client和i2c_driver定义如下:
(1)i2c_client和i2c_driver
i2c_driver对应一套驱动方法,主要成员有probe、remove、suspend等;而i2c_client的信息通常在BSP的文件中通过i2c_board_info填充。

struct i2c_client {unsigned short flags; /* 标志 */unsigned short addr; /* 芯片地址, 7 位,存在低 7 位*/char name[I2C_NAME_SIZE]; /* 名字 */struct i2c_adapter *adapter; /* 对应的 I2C 适配器 */struct device dev; /* 设备结构体 */int irq; /* 中断 */struct list_head detected;
..
};struct i2c_driver {unsigned int class;int (*attach_adapter)(struct i2c_adapter *) __deprecated;int (*probe)(struct i2c_client *, const struct i2c_device_id *);int (*remove)(struct i2c_client *);void (*shutdown)(struct i2c_client *);void (*alert)(struct i2/int (*command)(struct i2c_client *client, unsigned int cmd,void *arg);struct device_driver driver;const struct i2c_device_id *id_table;int (*detect)(struct i2c_client *, struct i2c_board_info *);const unsigned short *address_list;struct list_head clients;
};

驱动编写过程:


1.修改设备树,如修改IO结点、添加AP3216C设备结点
修改IO

     pinctrl_i2c1: i2c1grp {fsl,pins = <MX6UL_PAD_UART4_TX_DATA__I2C1_SCL 0x4001b8b0MX6UL_PAD_UART4_RX_DATA__I2C1_SDA 0x4001b8b0>;};

添加AP3216C设备结点

&i2c1 {clock-frequency = <100000>;pinctrl-names = "default";pinctrl-0 = <&pinctrl_i2c1>;status = "okay";ap3216c@1e {compatible = "alientek,ap3216c";reg = <0x1e>;};};

2.新建一个头文件,命名为ap3216creg.h,目的是存放寄存器,这里先创建好,暂时为空

#ifndef AP3216C_H
#define AP3216C_H#endif

3.新建一个ap3216c.c文件,用来编写驱动代码。这里我直接把基本框架放上来,有需要的可以直接拿去用,并且不用担心头文件缺失。

#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/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/input.h>
#include <linux/i2c.h>
#include "ap3216creg.h"
#include <linux/delay.h>/*驱动入口函数*/
static int __init ap3216c_init(void)
{return 0;
}/*驱动出口函数*/
static void __exit ap3216c_exit(void)
{}module_init(ap3216c_init);
module_exit(ap3216c_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("kong");

4.添加i2c_driver结构体,并且在驱动入口函数中用i2c_add_driver注册该结构体,在驱动出口函数中用i2c_del_driver注销该结构体

/*i2c_driver*/
static struct i2c_driver ap3216c_driver = {};/*驱动入口函数*/
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);}

5.完善i2c_driver结构体。其中的.of_match_table和.id_table在后面编写

/*i2c_driver*/
static 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,
};

6.建立ap3216c_of_match和ap3216c_id和probe函数和remove函数函数里面内容暂时不填写

/*probe函数*/
static int ap3216c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{return 0;
}
/*remove函数*/
static int ap3216c_remove(struct i2c_client *client)
{return 0;
}
/*匹配表*/
static struct i2c_device_id ap3216c_id[] = {{"alientek,ap3216c", 0},  //匹配名字{}
};
/*设备树匹配表*/
static struct of_device_id ap3216c_of_match[] = {{.compatible = "alientek,ap3216c"},{}
};

7.完整的框架基本就构建好了,完整框架如下

#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/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/input.h>
#include <linux/i2c.h>
#include "ap3216creg.h"
#include <linux/delay.h>/*probe函数*/
static int ap3216c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{return 0;
}
/*remove函数*/
static int ap3216c_remove(struct i2c_client *client)
{return 0;
}
/*匹配表*/
static struct i2c_device_id ap3216c_id[] = {{"alientek,ap3216c", 0},  //匹配名字{}
};
/*设备树匹配表*/
static struct of_device_id ap3216c_of_match[] = {{.compatible = "alientek,ap3216c"},{}
};/*i2c_driver*/
static 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("kong");

8.继续完善probe函数,remove函数、也就是在probe函数和remove函数里面构建一套完整的字符设备驱动框架来实现i2c功能。
这里添加了一整套完整的字符设备驱动框架。

#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/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/input.h>
#include <linux/i2c.h>
#include "ap3216creg.h"
#include <linux/delay.h>#define AP3216C_CNT 1
#define AP3216C_NAME "ap3216c"struct ap3216c_dev {int major;int minor;dev_t devid;struct cdev cdev;struct class *class;struct device *device;
};static struct ap3216c_dev ap3216cdev;static int ap3216c_open(struct inode *inode, struct file *filp)
{filp->private_data = &ap3216cdev;printk("open\r\n");return 0;
}
ssize_t ap3216c_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
{return 0;
}
static int ap3216c_release(struct inode *inode, struct file *filp)
{struct ap3216c_dev *dev = (struct ap3216cdev_dev*)filp->private_data;printk("release\r\n");return 0;
}static const struct file_operations ap3216c_fops = {.owner   = THIS_MODULE,.open    = ap3216c_open,.read   = ap3216c_read,.release = ap3216c_release,
};
/*probe函数*/
static int ap3216c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{int ret = 0;printk("OKOKOKOK!!!!\r\n");ap3216cdev.major = 0; //由系统分配主设备号/*搭建字符设备驱动框架,在/dev*/if(ap3216cdev.major){ap3216cdev.devid = MKDEV(ap3216cdev.major, 0);ret = register_chrdev_region(ap3216cdev.devid, AP3216C_CNT, AP3216C_NAME);}else{ret = alloc_chrdev_region(&ap3216cdev.devid, 0, AP3216C_CNT, AP3216C_NAME);ap3216cdev.major = MAJOR(ap3216cdev.devid);ap3216cdev.minor = MINOR(ap3216cdev.devid);}if(ret < 0){printk("ap3216cdev chrdev_region err!\r\n");goto fail_devid;}printk("ap3216cdev major=%d, minor=%d", ap3216cdev.major, ap3216cdev.minor);ap3216cdev.cdev.owner = THIS_MODULE;cdev_init(&ap3216cdev.cdev, &ap3216c_fops);ret = cdev_add(&ap3216cdev.cdev, ap3216cdev.devid, AP3216C_CNT);if(ret < 0){goto fail_cdev;}ap3216cdev.class = class_create(THIS_MODULE, AP3216C_NAME);if(IS_ERR(ap3216cdev.class)){ret = PTR_ERR(ap3216cdev.class);goto fail_class;}ap3216cdev.device = device_create(ap3216cdev.class, NULL, ap3216cdev.devid, NULL, AP3216C_NAME);if(IS_ERR(ap3216cdev.device)){ret = PTR_ERR(ap3216cdev.device);goto fail_device;}return 0;fail_device:class_destroy(ap3216cdev.class);
fail_class:cdev_del(&ap3216cdev.cdev);
fail_cdev:unregister_chrdev_region(ap3216cdev.devid, AP3216C_CNT);
fail_devid:return ret;
}
/*remove函数*/
static int ap3216c_remove(struct i2c_client *client)
{cdev_del(&ap3216cdev.cdev);unregister_chrdev_region(ap3216cdev.devid, AP3216C_CNT);device_destroy(ap3216cdev.class, ap3216cdev.devid);class_destroy(ap3216cdev.class);return 0;
}
/*匹配表*/
static struct i2c_device_id ap3216c_id[] = {{"alientek,ap3216c", 0},  //匹配名字{}
};
/*设备树匹配表*/
static struct of_device_id ap3216c_of_match[] = {{.compatible = "alientek,ap3216c"},{}
};/*i2c_driver*/
static 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("kong");

9.到这里,可以先进行make编译一下,并且执行应用程序,调试open、read、release、是否正确
注意,这里是应用程序如下:

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include "linux/ioctl.h"
#include "linux/input.h"
/* ./keyinputApp /dev/input/event2*//*input_event结构体变量*/
static struct input_event inputevent;int main(int argc, char *argv[])
{int fd;int err = 0;char *filename;int data;if (argc != 2) {printf("Error Usage!\r\n");return -1;}filename = argv[1];fd = open(filename, O_RDWR);if (fd < 0) {printf("Can't open file %s\r\n", filename);return -1;}err = read(fd, &data, sizeof(data));close(fd);return 0;
}

可以看到是没有问题的,正常打印出了open和release,如同open和release函数中设置的一样。到目前为止,都只是IIC的前期准备工作,接下来才是重头戏。

初始化AP3216C,主要是在read函数里面实现,重点就是通过IIC控制器来向AP3216C里面读取或者发送数据。主要是使用i2c_transfer这个内核的API函数来实现。
int i2c_transfer(struct i2c_adapter *adap, struct i2c_msg *msgs. int num)
adpa:也就是IIC接口,比如我们这里是接到的I2C1接口,当IIC设备和驱动匹配以后,probe函数执行,probe函数传递进来的第一个参数就是i2c_client,也就是说,i2c接口是可以从probe函数中得到。
msgs:传输的数据

10.在ap3216c_dev结构体里面添加一个成员变量,用来保存client,也就是i2c接口

11.再在probe函数里面保存client

12.读取和发送函数

/*读取AP3216C的N个寄存器值
*reg:从哪个寄存器开始读取
*val:保存读取到的数据,一般是个数组
*len:长度
*/
static int ap3216c_read_regs(struct ap3216c_dev *dev, u8 reg, void *val, int len)
{}
/*向AP3216C写N个寄存器的数据
*u8 *buf:意思是ap3216c这个芯片是八位的,如果使用芯片不同,要改变这个
*/
static int ap3216c_write_regs(struct ap3216c_dev *dev, u8 reg, u8 *buf, u8 len)
{}
/*读取AP3216C一个寄存器*/
static unsigned char ap3216c_read_reg(struct ap3216c_dev *dev, u8 reg)
{}
/*写一个数据*/
static void ap3216c_write_reg(struct ap3216c_dev *dev, u8 reg, u8 data)
{}

13.完善读取和写入函数

/*读取AP3216C的N个寄存器值
*reg:从哪个寄存器开始读取
*val:保存读取到的数据,一般是个数组
*len:长度
*/
static int ap3216c_read_regs(struct ap3216c_dev *dev, u8 reg, void *val, int len)
{struct i2c_msg msg[2];struct i2c_client *client = (struct i2c_client*)dev->private_data;/*msg[0]发送要读取的寄存器首地址*/msg[0].addr =client->addr; //从机地址,也就是AP3216C地址msg[0].flags = 0; //表示发送msg[0].buf = &reg;  //要发送的数据,也就是寄存器地址msg[0].len = 1;  //要发送的寄存器地址长度为1/*msg[1]读取数据*/msg[1].addr =client->addr;msg[1].flags = I2C_M_RD; //表示读取msg[1].buf = val;  //要接收的数据msg[1].len = len;  //要读取的寄存器地址长度为1return i2c_transfer(client->adapter, msg , 2);}
/*向AP3216C写N个寄存器的数据
*u8 *buf:意思是ap3216c这个芯片是八位的,如果使用芯片不同,要改变这个
*/
static int ap3216c_write_regs(struct ap3216c_dev *dev, u8 reg, u8 *buf, u8 len)
{u8 b[256];struct i2c_msg msg[1];struct i2c_client *client = (struct i2c_client*)dev->private_data;/*构建要发送的数据,也就是寄存器首地址+实际的数据*/b[0]= reg;memcpy(&b[1] , buf, len);msg.addr =client->addr; //从机地址,也就是AP3216C地址msg.flags = 0; //表示发送msg.buf = b;  //要发送的数据,也就是寄存器地址+实际数据msg.len = len+1;  //要发送的寄存器地址长度为1和实际的数据长度return i2c_transfer(client->adapter, &msg , 1);
}
/*读取AP3216C一个寄存器*/
static unsigned char 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 data)
{u8 buf = 0;buf = data;ap3216c_write_regs(dev, reg, &buf, 1);
}

14.开始配置寄存器,在原来创建的ap3216creg.h文件中添加如下内容

#ifndef AP3216C_H
#define AP3216C_H/* AP3216C寄存器 */
#define AP3216C_SYSTEMCONG  0x00    /* 配置寄存器            */
#define AP3216C_INTSTATUS   0X01    /* 中断状态寄存器          */
#define AP3216C_INTCLEAR    0X02    /* 中断清除寄存器          */
#define AP3216C_IRDATALOW   0x0A    /* IR数据低字节          */
#define AP3216C_IRDATAHIGH  0x0B    /* IR数据高字节          */
#define AP3216C_ALSDATALOW  0x0C    /* ALS数据低字节         */
#define AP3216C_ALSDATAHIGH 0X0D    /* ALS数据高字节         */
#define AP3216C_PSDATALOW   0X0E    /* PS数据低字节          */
#define AP3216C_PSDATAHIGH  0X0F    /* PS数据高字节          */#endif

15.回到ap3216c.c文件中,在open函数中初始化AP3216C

static int ap3216c_open(struct inode *inode, struct file *filp)
{unsigned char value = 0;filp->private_data = &ap3216cdev;printk("open\r\n");/*初始化ap3216c,通过IIC适配器(控制器)配置ap3216c的寄存器*//*向应用返回ap3216c的数据*/ap3216c_write_reg(&ap3216cdev, AP3216C_SYSTEMCONG, 0x4); //复位AP3216C mdelay(50);ap3216c_write_reg(&ap3216cdev, AP3216C_SYSTEMCONG, 0x3); //开启ALS、PS+IR value = ap3216c_read_reg(&ap3216cdev, AP3216C_SYSTEMCONG); /* 读取刚刚写进去的0X03 */printk("AP316C_SYSTEMCONG = %#x\r\n",value); return 0;
}

16.编译、加载、执行应用程序
可见结果是没有问题的

17.在ap3216c_dev结构体里面添加ir,als,ps

struct ap3216c_dev {int major;int minor;dev_t devid;struct cdev cdev;struct class *class;struct device *device;void *private_data;   //用私有数据保存client,也就是i2c接口unsigned short ir,als,ps;
};

18.编写读取AP3216C函数

/*读取AP3216C的数据,读取原始数据,包括ALS,PS和IR, 注意!*如果同时打开ALS,IR+PS的话两次数据读取的时间间隔要大于112.5ms*/
void ap3216c_readdata(struct ap3216c_dev *dev)
{unsigned char buf[6];unsigned char i;/* 循环读取所有传感器数据 */for(i = 0; i < 6; i++) {buf[i] = ap3216c_read_reg(dev, AP3216C_IRDATALOW + i);   }if(buf[0] & 0x80){dev->ir = 0;dev->ps = 0;}else{dev->ir = ((unsigned short)buf[1] << 2) | (buf[0] & 0x03);dev->os = (((unsigned short)buf[5] & 0x3f) << 4) | (buf[4] & 0x0F);}dev->als = ((unsigned short)buf[3] << 8) | buf[2];}

19.在read函数里调用18步的读取函数

ssize_t ap3216c_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
{long err = 0;short data[3];struct ap3216c_dev *dev = (struct ap3216c_dev *)filp->private_data;/*向应用返回AP3216C的原始数据*/ap3216c_readdata(dev);data[0] = dev->ir;data[1] = dev->als;data[2] = dev->ps;err = copy_to_user(buf, data, sizeof(data));return 0;
}

20.修改应用程序。

#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
#include "linux/ioctl.h"
#include "linux/input.h"
/* ./keyinputApp /dev/input/event2*//*input_event结构体变量*/
static struct input_event inputevent;int main(int argc, char *argv[])
{int fd;int err = 0;char *filename;unsigned short data[3];unsigned short ir,ps,als;if (argc != 2) {printf("Error Usage!\r\n");return -1;}filename = argv[1];fd = open(filename, O_RDWR);if (fd < 0) {printf("Can't open file %s\r\n", filename);return -1;}while(1){err = read(fd, data, sizeof(data));if(err == 0){/*读取成功*/ir = data[0];als = data[1];ps =data[2];printf("AP3216C ir=%d, als = %d, ps = %d\r\n", ir, als, ps);}usleep(200000); //延时200ms}close(fd);return 0;
}

21.编译、加载、执行应用程序,可看到AP3216C的数据已经出来了。至此IIC驱动实验完成!!!!!



【完整驱动代码】

#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/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/semaphore.h>
#include <linux/timer.h>
#include <linux/of_irq.h>
#include <linux/irq.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <linux/input.h>
#include <linux/i2c.h>
#include "ap3216creg.h"
#include <linux/delay.h>#define AP3216C_CNT 1
#define AP3216C_NAME "ap3216c"struct ap3216c_dev {int major;int minor;dev_t devid;struct cdev cdev;struct class *class;struct device *device;void *private_data;   //用私有数据保存client,也就是i2c接口unsigned short ir,als,ps;
};static struct ap3216c_dev ap3216cdev;/*读取AP3216C的N个寄存器值
*reg:从哪个寄存器开始读取
*val:保存读取到的数据,一般是个数组
*len:长度
*/
static int ap3216c_read_regs(struct ap3216c_dev *dev, u8 reg, void *val, int len)
{struct i2c_msg msg[2];struct i2c_client *client = (struct i2c_client*)dev->private_data;/*msg[0]发送要读取的寄存器首地址*/msg[0].addr =client->addr; //从机地址,也就是AP3216C地址msg[0].flags = 0; //表示发送msg[0].buf = &reg;  //要发送的数据,也就是寄存器地址msg[0].len = 1;  //要发送的寄存器地址长度为1/*msg[1]读取数据*/msg[1].addr =client->addr;msg[1].flags = I2C_M_RD; //表示读取msg[1].buf = val;  //要接收的数据msg[1].len = len;  //要读取的寄存器地址长度为1return i2c_transfer(client->adapter, msg , 2);}
/*向AP3216C写N个寄存器的数据
*u8 *buf:意思是ap3216c这个芯片是八位的,如果使用芯片不同,要改变这个
*/
static int 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; //从机地址,也就是AP3216C地址msg.flags = 0; //表示发送msg.buf = b;  //要发送的数据,也就是寄存器地址+实际数据msg.len = len+1;  //要发送的寄存器地址长度为1和实际的数据长度return i2c_transfer(client->adapter, &msg , 1);
}/*读取AP3216C一个寄存器*/
static unsigned char 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 data)
{u8 buf = 0;buf = data;ap3216c_write_regs(dev, reg, &buf, 1);
}/*读取AP3216C的数据,读取原始数据,包括ALS,PS和IR, 注意!*如果同时打开ALS,IR+PS的话两次数据读取的时间间隔要大于112.5ms*/
void ap3216c_readdata(struct ap3216c_dev *dev)
{unsigned char buf[6];unsigned char i;/* 循环读取所有传感器数据 */for(i = 0; i < 6; i++) {buf[i] = ap3216c_read_reg(dev, AP3216C_IRDATALOW + i);   }if(buf[0] & 0x80){dev->ir = 0;dev->ps = 0;}else{dev->ir = ((unsigned short)buf[1] << 2) | (buf[0] & 0x03);dev->ps = (((unsigned short)buf[5] & 0x3f) << 4) | (buf[4] & 0x0F);}dev->als = ((unsigned short)buf[3] << 8) | buf[2];}static int ap3216c_open(struct inode *inode, struct file *filp)
{unsigned char value = 0;filp->private_data = &ap3216cdev;/*初始化ap3216c,通过IIC适配器(控制器)配置ap3216c的寄存器*//*向应用返回ap3216c的数据*/ap3216c_write_reg(&ap3216cdev, AP3216C_SYSTEMCONG, 0x4); //复位AP3216C mdelay(50);ap3216c_write_reg(&ap3216cdev, AP3216C_SYSTEMCONG, 0x3); //开启ALS、PS+IR value = ap3216c_read_reg(&ap3216cdev, AP3216C_SYSTEMCONG); /* 读取刚刚写进去的0X03 */printk("AP316C_SYSTEMCONG = %#x\r\n",value);  return 0;
}
ssize_t ap3216c_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
{long err = 0;short data[3];struct ap3216c_dev *dev = (struct ap3216c_dev *)filp->private_data;/*向应用返回AP3216C的原始数据*/ap3216c_readdata(dev);data[0] = dev->ir;data[1] = dev->als;data[2] = dev->ps;err = copy_to_user(buf, data, sizeof(data));return 0;
}
static int ap3216c_release(struct inode *inode, struct file *filp)
{struct ap3216c_dev *dev = (struct ap3216cdev_dev*)filp->private_data;printk("release\r\n");return 0;
}static const struct file_operations ap3216c_fops = {.owner   = THIS_MODULE,.open    = ap3216c_open,.read   = ap3216c_read,.release = ap3216c_release,
};
/*probe函数*/
static int ap3216c_probe(struct i2c_client *client, const struct i2c_device_id *id)
{int ret = 0;printk("OKOKOKOK!!!!\r\n");ap3216cdev.major = 0; //由系统分配主设备号/*搭建字符设备驱动框架,在/dev*/if(ap3216cdev.major){ap3216cdev.devid = MKDEV(ap3216cdev.major, 0);ret = register_chrdev_region(ap3216cdev.devid, AP3216C_CNT, AP3216C_NAME);}else{ret = alloc_chrdev_region(&ap3216cdev.devid, 0, AP3216C_CNT, AP3216C_NAME);ap3216cdev.major = MAJOR(ap3216cdev.devid);ap3216cdev.minor = MINOR(ap3216cdev.devid);}if(ret < 0){printk("ap3216cdev chrdev_region err!\r\n");goto fail_devid;}printk("ap3216cdev major=%d, minor=%d\r\n", ap3216cdev.major, ap3216cdev.minor);ap3216cdev.cdev.owner = THIS_MODULE;cdev_init(&ap3216cdev.cdev, &ap3216c_fops);ret = cdev_add(&ap3216cdev.cdev, ap3216cdev.devid, AP3216C_CNT);if(ret < 0){goto fail_cdev;}ap3216cdev.class = class_create(THIS_MODULE, AP3216C_NAME);if(IS_ERR(ap3216cdev.class)){ret = PTR_ERR(ap3216cdev.class);goto fail_class;}ap3216cdev.device = device_create(ap3216cdev.class, NULL, ap3216cdev.devid, NULL, AP3216C_NAME);if(IS_ERR(ap3216cdev.device)){ret = PTR_ERR(ap3216cdev.device);goto fail_device;}ap3216cdev.private_data = client; return 0;fail_device:class_destroy(ap3216cdev.class);
fail_class:cdev_del(&ap3216cdev.cdev);
fail_cdev:unregister_chrdev_region(ap3216cdev.devid, AP3216C_CNT);
fail_devid:return ret;
}
/*remove函数*/
static int ap3216c_remove(struct i2c_client *client)
{cdev_del(&ap3216cdev.cdev);unregister_chrdev_region(ap3216cdev.devid, AP3216C_CNT);device_destroy(ap3216cdev.class, ap3216cdev.devid);class_destroy(ap3216cdev.class);return 0;
}
/*匹配表*/
static struct i2c_device_id ap3216c_id[] = {{"alientek,ap3216c", 0},  //匹配名字{}
};
/*设备树匹配表*/
static struct of_device_id ap3216c_of_match[] = {{.compatible = "alientek,ap3216c"},{}
};/*i2c_driver*/
static 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("kong");

《正点原子第四期驱动开发第22讲》—左忠凯
《 Linux设备驱动开发详解》—宋宝华
《Linux内核设计与实现》—Robert Love

Linux驱动开发之IIC驱动实验【完整教程】相关推荐

  1. Linux驱动开发之USB驱动深入学习(三)——USB2.0ECHI驱动注册

    一.前言 本篇博客仅对ECHI主机控制器驱动的注册部分进行简要叙述,后面再对一些重要的接口进行分析讲解. 二.USB 1.概述 USB(Universal Serial Bus)即"通用外部 ...

  2. Linux驱动开发之DRM驱动

    作者 QQ群:852283276 微信:arm80x86 微信公众号:青儿创客基地 B站:主页 https://space.bilibili.com/208826118 参考 Linux DRM Gr ...

  3. linux内核单步调试,Linux内核驱动开发之KGDB单步调试内核(kgdboc方式)

    如何单步调试Linux内核一直困扰着linux驱动开发人员,内核有其代码量大.逻辑复杂.与硬件交互的特性.因此,有着不同于应用程序的调试方法,据统计Linux内核开 Linux内核驱动开发之KGDB原 ...

  4. Android驱动开发之Hello实例(基于高通msm8909)

    点击打开链接 Android驱动开发之Hello实例: 驱动部分 modified:   kernel/arch/arm/configs/msm8909-1gb_w100_hd720p-perf_de ...

  5. Android驱动开发之Hello实例

    Android驱动开发之Hello实例: 驱动部分 modified:   kernel/arch/arm/configs/msm8909-1gb_w100_hd720p-perf_defconfig ...

  6. Exynos4412 IIC总线驱动开发(一)—— IIC 基础概念及驱动架构分析 (iic驱动框架,i2c驱动框架)...

    转载于 : http://blog.csdn.net/zqixiao_09/article/details/50917655 关于Exynos4412 IIC 裸机开发请看 :Exynos4412 裸 ...

  7. MSP430杂谈--AD7745硬件IIC驱动与模拟IIC驱动

    和上一篇AD7793类似,项目中也涉及到利用AD7745读取电容值,来测环境湿度.编写了基于MSP430的AD7745的硬件IIC驱动和模拟IIC驱动,分享给大家. AD7745硬件IIC驱动完整版下 ...

  8. 手机APP开发之MIT Appinventor详细实战教程(一),利用通过蓝牙控制单片机,以及实现单片机与android设备之间的串口通信

    目录 (一)前期软件准备和硬件准备 ( 二 ) 实现的思路和操作原理 ( 三) 具体的操作方法 MIT Appinventor 是编程领域较为受欢迎且适用的编程软件 ,因其操作流程和使用方法简单,一直 ...

  9. linux驱动开发之spi-omap-100k.c源码分析

    代码分析 对于linux的驱动代码来说,我们要从后往前分析: /** OMAP7xx SPI 100k controller driver* Author: Fabrice Crohas <fc ...

最新文章

  1. Win7 怎么取消禁(被)ping
  2. 纯JavaScript实现弹出选择第几个单选按钮
  3. 【译】Advanced Blockchain Concepts for Beginners
  4. ITK:计算矢量图像中每个像素的大小以生成大小图像
  5. SAP Spartacus B2B 页面 Popover Component 的条件显示逻辑
  6. css外观样式 1204
  7. Python学习笔记之函数(三)
  8. 如何让不使用vba没办法打开excel表_Excel常见问题
  9. java 静态类实例_Java中多个类的静态实例?
  10. 一个大数据应用是如何炼成的?
  11. Maya: Render Setup System Maya教程:渲染设置系统 Lynda课程中文字幕
  12. 那些年,我们一起踩过的 “Android 坑”
  13. 毕业设计,管理系统,大学生毕业设计应该这么做
  14. 圆角正方形 html,ps正方形角怎么变圆角 ps怎么在原来的矩形中改成圆角
  15. 通过FAI进行全自动安装
  16. Android无埋点数据收集SDK关键技术解析
  17. 从零搭建Spring Boot脚手架:增加通用的功能2
  18. 能否去大厂的关键内功!
  19. HTML5印章绘制电子签章图片,中文英文椭圆章、中文英文椭圆印章 电子签章图片采集
  20. 7-4 最短路径之Dijkstra(朴素dijkstra打印路径)

热门文章

  1. Juniper SRX操作系统软件升级
  2. 第十九次ScrumMeeting博客
  3. H3C IRF MAD检测实战
  4. 怎样用计算机打出Abc,智能ABC输入法如何安装?win7智能ABC输入法安装步骤
  5. 基于java的项目总结
  6. 机器学习--基础算法--机器学习基础
  7. vb.net 简单的方法模拟三体(有行星)
  8. 实战三十九:鲍鱼年龄预测热力图相关性分析
  9. 移动App測试实战:顶级互联网企业软件測试和质量提升最佳实践
  10. 【Maya】编译USD(个人笔记)