Linux驱动开发+QT应用编程实现IIC读取ap3216c
时间:2023-11-28 08:07:02
Linux驱动开发:IIC驱动开发
Linux I2C介绍驱动框架
bsp_i2c.c 称之为 主机驱动,写完不需要修改。
bsp_ap3216c.c称为设备驱动,根据主机驱动适应设备。
Linux内核将IIC驱动分为
- IIC总线驱动:总线驱动相当于裸机IIC这里叫主机驱动IIC适配器驱动
- IIC设备驱动:设备驱动ap3216c专门编写的驱动。
1、IIC读写代码示例(重要)
使用 i2c_transfer 在函数发送数据之前,应先构建 i2c_msg,使用 i2c_transfer 进行 I2C 数据收发的示例代码如下:
/* 设备结构体 */ struct xxx_dev {
...... void *private_data; /* 私有数据一般设置为 i2c_client */ }; /* * @description : 读取 I2C 多个寄存器数据的设备 * @param – dev : I2C 设备 * @param – reg : 要读取的寄存器首地址 * @param – val : 读取的数据 * @param – len : 要读取的数据长度 * @return : 操作结果 */ static int xxx_read_regs(struct xxx_dev *dev, u8 reg, void *val,int len) {
int ret; struct i2c_msg msg[2]; struct i2c_client *client = (struct i2c_client *) dev->private_data; /* msg[0],第一条写信息,发送要读取的寄存器首地址 */ msg[0].addr = client->addr; /* I2C 器件地址 */ msg[0].flags = 0; /* 发送数据的标记 */ msg[0].buf = ® /* 读取的第一个地址 */ msg[0].len = 1; /* reg 长度 */ /* msg[1]第二条读取信息,读取寄存器数据 */ msg[1].addr = client->addr; /* I2C 器件地址 */ msgspan class="token punctuation">[1].flags = I2C_M_RD; /* 标记为读取数据 */
msg[1].buf = val; /* 读取数据缓冲区 */
msg[1].len = len; /* 要读取的数据长度 */
ret = i2c_transfer(client->adapter, msg, 2);
if(ret == 2) {
ret = 0;
} else {
ret = -EREMOTEIO;
}
return ret;
}
/* * @description : 向 I2C 设备多个寄存器写入数据 * @param – dev : 要写入的设备结构体 * @param – reg : 要写入的寄存器首地址 * @param – buf : 要写入的数据缓冲区 * @param – len : 要写入的数据长度 * @return : 操作结果 */
static s32 xxx_write_regs(struct xxx_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); /* 将要发送的数据拷贝到数组 b 里面 */
msg.addr = client->addr; /* I2C 器件地址 */
msg.flags = 0; /* 标记为写数据 */
msg.buf = b; /* 要发送的数据缓冲区 */
msg.len = len + 1; /* 要发送的数据长度 */
return i2c_transfer(client->adapter, &msg, 1);
}
另外两个API函数分别用于I2C数据的收发操作,这两个函数最终都会调用i2c_transfer。
函数原型如下:
int i2c_master_send(const struct i2c_client *client, const char *buf, int count)
函数参数和返回值含义如下:
client:I2C 设备对应的 i2c_client。
buf:要发送的数据。
count:要发送的数据字节数,要小于 64KB,以为 i2c_msg 的 len 成员变量是一个 u16(无符号 16 位)类型的数据。
返回值:负值,失败,其他非负值,发送的字节数。
I2C 数据接收函数为 i2c_master_recv,函数原型如下:
int i2c_master_recv(const struct i2c_client *client, char *buf, int count)
函数参数和返回值含义如下:
client:I2C 设备对应的 i2c_client。
buf:要接收的数据。
count:要接收的数据字节数,要小于 64KB,以为 i2c_msg 的 len 成员变量是一个 u16(无符号 16 位)类型的数据。
返回值:负值,失败,其他非负值,发送的字节数。
2、i2c_driver 的设备树注册示例代码如下:
i2c 驱动的 probe 函数
static int xxx_probe(struct i2c_client *client,const struct i2c_device_id *id)
{
/* 函数具体程序 */
return 0;
}
i2c 驱动的 remove 函数
static int xxx_remove(struct i2c_client *client)
{
函数具体程序
return 0;
}
设备树匹配列表
static const struct of_device_id xxx_of_match[] = {
{
.compatible = "xxx" },
{
/* Sentinel */ }
};
i2c 驱动结构体
static struct i2c_driver xxx_driver = {
.probe = xxx_probe,
.remove = xxx_remove,
.driver = {
.owner = THIS_MODULE,
.name = "xxx",
.of_match_table = xxx_of_match,
},
.id_table = xxx_id,
};
驱动入口函数
static int __init xxx_init(void)
{
int ret = 0;
ret = i2c_add_driver(&xxx_driver);
return ret;
}
驱动出口函数
static void __exit xxx_exit(void)
{
i2c_del_driver(&xxx_driver);
}
module_init(xxx_init);
module_exit(xxx_exit);
- of_device_id,设备树所使用的匹配表。
- i2c_driver,当 I2C 设备和 I2C 驱动匹配成功以后 probe 函数就会执行,这些和 platform 驱动一样,probe函数里面基本就是标准的字符设备驱动那一套了。
ap3216c驱动开发
1、修改设备树,pinctrl子节点添加设备的电气属性,配置GPIO并且设置电气属性:
pinctrl_i2c1 就是 I2C1 的 IO 节点,这里将 UART4_TXD 和 UART4_RXD 这两个 IO 分别复用为 I2C1_SCL 和 I2C1_SDA,电气属性都设置为 0x4001b8b0。
2、在 i2c1 节点追加 ap3216c 子节点
&i2c1 {
clock-frequency = <100000>;
pinctrl-names = "default";
pinctrl-0 = <&pinctrl_i2c1>;
status = "okay";
ap3216c@1e {
compatible = "alientek,ap3216c";
reg = <0x1e>;
};
};
结果:
寄存器地址:
#ifndef AP3216C_H
#define AP3216C_H
#define AP3216C_ADDR 0X1E /* AP3216C器件地址 */
/* AP3316C寄存器 */
#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
驱动代码:
#include
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "ap3216creg.h" #define AP3216C_CNT 1 #define AP3216C_NAME "ap3216c" struct ap3216c_dev { dev_t devid; /* 设备号 */ struct cdev cdev; /* cdev */ struct class *class; /* 类 */ struct device *device; /* 设备 */ struct device_node *nd; /* 设备节点 */ int major; /* 主设备号 */ void *private_data; /* 私有数据 */ unsigned short ir, als, ps; /* 三个光 传感器数据 */ }; static struct ap3216c_dev ap3216cdev; /* * @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) { int ret; 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 = ® /* 读取的首地址 */ msg[0].len = 1; /* reg长度*/ /* msg[1]读取数据 */ msg[1].addr = client->addr; /* ap3216c地址 */ msg[1].flags = I2C_M_RD; /* 标记为读取数据*/ msg[1].buf = val; /* 读取数据缓冲区 */ msg[1].len = len; /* 要读取的数据长度*/ ret = i2c_transfer(client->adapter, msg, 2); if(ret == 2) { ret = 0; } else { printk("i2c rd failed=%d reg=%06x len=%d\n",ret, reg, len); ret = -EREMOTEIO; } return ret; } /* * @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); /* 将要写入的数据拷贝到数组b里面 */ msg.addr = client->addr; /* ap3216c地址 */ msg.flags = 0; /* 标记为写数据 */ msg.buf = b; /* 要写入的数据缓冲区 */ msg.len = len + 1; /* 要写入的数据长度 */ //msg 的 len 为 len+1,因为要加上一个字节的寄存器地址。 return i2c_transfer(client->adapter, &msg, 1); } /* * @description : 读取ap3216c指定寄存器值,读取一个寄存器 * @param - dev: ap3216c设备 * @param - reg: 要读取的寄存器 * @return : 读取到的寄存器值 */ static unsigned char ap3216c_read_reg(struct ap3216c_dev *dev, u8 reg) { u8 data = 0; ap3216c_read_regs(dev, reg, &data, 1); return data; #if 0 struct i2c_client *client = (struct i2c_client *)dev->private_data; return i2c_smbus_read_byte_data(client, reg); #endif } /* * @description : 向ap3216c指定寄存器写入指定的值,写一个寄存器 * @param - dev: ap3216c设备 * @param - reg: 要写的寄存器 * @param - data: 要写入的值 * @return : 无 */ 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); } /* * @description : 读取AP3216C的数据,读取原始数据,包括ALS,PS和IR, 注意! * : 如果同时打开ALS,IR+PS的话两次数据读取的时间间隔要大于112.5ms * @param - ir : ir数据 * @param - ps : ps数据 * @param - ps : als数据 * @return : 无。 */ void ap3216c_readdata(struct ap3216c_dev *dev) { unsigned char i =0; unsigned char buf[6]; /* 循环读取所有传感器数据 */ for(i = 0; i < 6; i++) { buf[i] = ap3216c_read_reg(dev, AP3216C_IRDATALOW + i); } if(buf[0] & 0X80) /* IR_OF位为1,则数据无效 */ dev->ir = 0; else /* 读取IR传感器的数据 */ dev->ir = ((unsigned short)buf[1] << 2) | (buf[0] & 0X03); dev->als = ((unsigned short)buf[3] << 8) | buf[2]; /* 读取ALS传感器的数据 */ if(buf[4] & 0x40) /* IR_OF位为1,则数据无效 */ dev->ps = 0; else /* 读取PS传感器的数据 */ dev->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 = &ap3216cdev; /* 初始化AP3216C */ ap3216c_write_reg(&ap3216cdev, AP3216C_SYSTEMCONG, 0x04); /* 复位AP3216C */ mdelay(50); /* AP3216C复位最少10ms */ ap3216c_write_reg(&ap3216cdev, AP3216C_SYSTEMCONG, 0X03); /* 开启ALS、PS+IR */ 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 *off) { short data[3]; long err = 0; struct ap3216c_dev *dev = (struct ap3216c_dev *)filp->private_data; 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; } /* * @description : 关闭/释放设备 * @param - filp : 要关闭的设备文件(文件描述符) * @return : 0 成功;其他 失败 */ static int ap3216c_release(struct inode *inode, struct file *filp) { return 0; } /* AP3216C操作函数 */ static const struct file_operations ap3216c_ops = { .owner = THIS_MODULE, .open = ap3216c_open, .read = ap3216c_read, .release = ap3216c_release, }; /* * @description : i2c驱动的probe函数,当驱动与 * 设备匹配以后此函数就会执行 * @param - client : i2c设备 * @param - id : i2c设备ID * @return : 0,成功;其他负值,失败 */ static int ap3216c_probe(struct i2c_client *client, const struct i2c_device_id *id) { /* 1、构建设备号 */ if (ap3216cdev.major) { ap3216cdev.devid = MKDEV(ap3216cdev.major, 0); register_chrdev_region(ap3216cdev.devid, AP3216C_CNT, AP3216C_NAME); } else { alloc_chrdev_region(&ap3216cdev.devid, 0, AP3216C_CNT, AP3216C_NAME); ap3216cdev.major = MAJOR(ap3216cdev.devid); } /* 2、注册设备 */ cdev_init(&ap3216cdev.cdev, &ap3216c_ops); cdev_add(&ap3216cdev.cdev, ap3216cdev.devid, AP3216C_CNT); /* 3、创建类 */ ap3216cdev.class = class_create(THIS_MODULE, AP3216C_NAME); if (IS_ERR(ap3216cdev.class)) { return PTR_ERR(ap3216cdev.class); } /* 4、创建设备 */ ap3216cdev.device = device_create(ap3216cdev.class, NULL, ap3216cdev.devid,