Linux I2C 驱动实验

这篇具有很好参考价值的文章主要介绍了Linux I2C 驱动实验。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

一、Linux I2C 驱动简介

1、I2C 总线驱动

2、I2C 设备驱动

1、 i2c_client 结构体

2、 i2c_driver 结构体

二、硬件分析

三、设备树编写

1、pinctrl_i2c1

2、在 i2c1 节点追加 ap3216c 子节点

3、验证

四、 代码编写

1、makefile

2、ap3216c.h

 3、ap3216c.c

①、头文件

②、驱动出入口 

③、 i2c驱动结构体 

④、匹配函数

⑤、probe 函数

⑥、remove 函数

 ⑦、函数入口出口添加注册i2c_drive

⑧、读取AP3216C的N个寄存器

⑨、向AP3216C的N个寄存器写数据

⑩、读、写AP3216C一个寄存器

⑩①、读取AP3216C的数据

⑩②完善ap3216c_open、read函数

 代码如下

 

4、ap3216c.c


一、Linux I2C 驱动简介

        在IMX6ULL开发篇中"IIC实验"写了四个文件: bsp_i2c.c、bsp_i2c.h、 bsp_ap3216c.c 和 bsp_ap3216c.h。其中前两个是 I.MX6U 的 IIC 接口驱动,后两个文件是 AP3216C 这个 I2C 设备驱动文件。相当于有两部分驱动:
①、 I2C 主机驱动。
②、 I2C 设备驱动。
        对于 I2C 主机驱动,一旦编写完成就不需要再做修改,其他的 I2C 设备直接调用主机驱动提供的 API 函数完成读写操作即可。这个正好符合 Linux 的驱动分离与分层的思想,因此 Linux内核也将 I2C 驱动分为两部分:
①、 I2C 总线驱动, I2C 总线驱动就是 SOC 的 I2C 控制器驱动,也叫做 I2C 适配器驱动。
②、 I2C 设备驱动, I2C 设备驱动就是针对具体的 I2C 设备而编写的驱动

1、I2C 总线驱动

        在”platform驱动实验"的时候就说过, platform 是虚拟出来的一条总线,目的是为了实现总线、设备、驱动框架。对于 I2C 而言,不需要虚拟出一条总线,直接使用 I2C总线即可。 I2C 总线驱动重点是 I2C 适配器(也就是 SOC 的 I2C 接口控制器)驱动,Linux 内核将 SOC 的 I2C 适配器(控制器)抽象成 i2c_adapter, i2c_adapter 结构体定义在 include/linux/i2c.h 文件中,i2c_algorithm 类型的指针变量 algo,对于一个 I2C 适配器,肯定要对外提供读写 API 函数,设备驱动程序可以使用这些 API 函数来完成读写操作。 i2c_algorithm 就是 I2C 适配器与 IIC 设备进行通信的方法;i2c_algorithm 结构体定义在 include/linux/i2c.h 文件中,其中的master_xfer 就是 I2C 适配器的传输函数,可以通过此函数来完成与 IIC 设备之间的通信,smbus_xfer 是 SMBUS 总线的传输函数

        一般 SOC 的 I2C 总线驱动都是由半导体厂商编写的,比如 I.MX6U 的 I2C 适配器驱动 NXP 已经编写好了,这个不需要用户去编写。因此 I2C 总线驱动对我们这些 SOC 使用者来说是被屏蔽掉的,我们只要专注于 I2C 设备驱动即可。

2、I2C 设备驱动

        I2C 设备驱动重点关注两个数据结构: i2c_client 和 i2c_driver,i2c_client 就是描述设备信息的, i2c_driver 描述驱动内容,类似于 platform_driver

1、 i2c_client 结构体

i2c_client 结构体定义在 include/linux/i2c.h 文件中,内容如下:

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;
......
 };

一个设备对应一个 i2c_client,每检测到一个 I2C 设备就会给这个 I2C 设备分配一个i2c_client

2、 i2c_driver 结构体

i2c_driver 类似 platform_driver,i2c_driver 结构体定义在 include/linux/i2c.h 文件中,部分如下

 struct i2c_driver {

    .........
    /* Standard driver model interfaces */
1    int (*probe)(struct i2c_client *, const struct i2c_device_id *);
    ......
2    struct device_driver driver;
3    const struct i2c_device_id *id_table;
    ..........

}

第 1 行,当 I2C 设备和驱动匹配成功以后 probe 函数就会执行,和 platform 驱动一样。
第 2行, device_driver 驱动结构体,如果使用设备树的话,需要设置 device_driver

                of_match_table 成员变量,也就是驱动的兼容(compatible)属性。
第3行, id_table 是传统的、未使用设备树的设备匹配 ID 表

重点工作就是构建 i2c_driver,构建完成以后需要向Linux 内核注册这个 i2c_driver

二、硬件分析

这部分在在IMX6ULL开发篇中"IIC实验"有详细介绍,下面就简单介绍

打开AP3216C原理图,这是一个IIC接口的器件,是一个环境光传感器

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

 I2C1_SCL: 使用的是UART4_TXD这个IO;I2C1_SDA: 使用的是UART4_RXD这个IO

打开ap3216数据手册

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

地址为0x1e

三、设备树编写

1、pinctrl_i2c1

打开设备树,找到i2c1,pinctrl_i2c1 就是 I2C1 的 IO 节点
linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

        这里将 UART4_TXD 和 UART4_RXD 这两个 IO 分别复用为 I2C1_SCL 和 I2C1_SDA,电气属性都设置为 0x4001b8b0

2、在 i2c1 节点追加 ap3216c 子节点

        AP3216C 是连接到 I2C1 上的,因此需要在 i2c1 节点下添加 ap3216c 的设备子节点,默认内容如下

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

305-316行,NXP 官方的 EVK 开发板上接了 mag3110和fxls8471,这里使用的开发板并没有这这两个器件,所以删掉,并添加上我们使用的

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

300行,clock-frequency 属性为 I2C 频率,这里设置为 100KHz

302行,inctrl-0 属性指定 I2C 所使用的 IO 为上面的pinctrl_i2c1

305行,ap3216c 子节点, @后面的“1e”是 ap3216c 的器件地址

306行,设置 compatible 值为“my,ap3216c”

307行,reg 属性也是设置 ap3216c 器件地址的,因此 reg 设置为 0x1e

3、验证

改完成以后使用“make dtbs”重新编译一下,然后使用新的设备树启动 Linux 内核

/sys/bus/i2c/devices 目录下存放着所有 I2C 设备,如果设备树修改正确的话,会在/sys/bus/i2c/devices 目录下看到一个名为“0-001e”的子目录,如下

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

“0-001e”就是 ap3216c 的设备目录,“1e”就是 ap3216c 器件地址。进入0-001e 目录,可以看到“name”文件, name 问价就保存着此设备名字,在这里就是“ap3216c”,如下

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

四、 代码编写

1、makefile

需要的文件如下图左,修改makefile

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

2、ap3216c.h

定义器件及其寄存器地址

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

 3、ap3216c.c

①、头文件

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

②、驱动出入口 

 linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

③、 i2c驱动结构体 

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

46行,当 I2C 设备和驱动匹配成功以后 probe 函数就会执行,和 platform 驱动一样

47行,当关闭 驱动的时候remove 函数就会执行,和 platform 驱动一样

49行,name是在无设备树时用于和设备进行匹配,也就是上面写的实验,也作为驱动名字;

        使用设备树就设置of_match_table变量,也就是驱动的兼容(compatible)属性
53行,id_table 是传统的、未使用设备树的设备匹配 ID 表

④、匹配函数

i2c驱动结构体 里对应的函数

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

 236行,无设备树的时候匹配 ID 表
241行,设备树所使用的匹配表

⑤、probe 函数

在这里面实现基本的字符设备

#define AP3216C_CNT 1
#define AP3216C_NAME "ap3216c"

struct ap3216c_dev{
    int devid;
    int major;
    int minor;
    struct cdev cedv;
    struct class *class;
    struct device *device;
    void *private_data;/*私有数据*/
    unsigned short ir, als, ps;		/* 三个光传感器数据 */ 
};
static struct ap3216c_dev ap3216cdev;
static int ap3216c_open(struct inode *inode, struct file *filp)
{
   
    return 0;
}

static int ap3216c_release(struct inode *inode, struct file *filp)
{
    printk("ap3216c_release\r\n");
    return 0;
}
static ssize_t ap3216c_read(struct file *filp, char __user *buf, size_t cnt, loff_t *off)
{
    return 0;
}

static struct file_operations ap3216c_fops  = {
    .owner  =   THIS_MODULE,
    .open   =   ap3216c_open,
    .read   =   ap3216c_read,
    .release = ap3216c_release,
};

static int ap3216c_probe(struct i2c_client *client,const struct i2c_device_id *id)
{
    int ret =0;
    /*搭建字符设备驱动框架*/
    /*1、创建字符设备*/
    ap3216cdev.major=0;
    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 error!\r\n");
        goto fail_devid;
    }
    printk("ap3216cde major: %d minor: %d\r\n",ap3216cdev.major,ap3216cdev.minor);
    /*2、注册字符设备*/
    ap3216cdev.cedv.owner = THIS_MODULE;
    cdev_init(&ap3216cdev.cedv,&ap3216c_fops);
    ret = cdev_add(&ap3216cdev.cedv,ap3216cdev.devid,AP3216C_CNT);
    if(ret < 0)
    {
        printk("cdev_add error!\r\n");
        goto fail_cdev;
    }
    /*3、自动创建设备节点*/
    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;
    }

    printk("ap3216c_probe\r\n");
    ap3216cdev.private_data = client;
    return 0;
fail_device:
    class_destroy(ap3216cdev.class);
fail_class:
    cdev_del(&ap3216cdev.cedv);
fail_cdev:
    unregister_chrdev_region(ap3216cdev.devid,AP3216C_CNT);
fail_devid:
    return ret;
}

" return 0 "前一行,将此函数的第一 个参数 client 传递给 ap3216cdev 的 private_data 成员变量

当设备和驱动匹配成功后,probe函数执行,传递的第一个参数就是i2c_client ,i2c_client 每检测到一个 I2C 设备就会给这个 I2C 设备分配一个i2c_client,这里用 ap3216cdev结构体中的private_data 成员接收

⑥、remove 函数

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

 ⑦、函数入口出口添加注册i2c_drive

 linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

50行,调用 i2c_add_driver 来向 Linux 内 核注册 i2c_driver,也就是 ap3216c_driver

i2c_add_driver 是一个宏,定义如下:

#define i2c_add_driver(driver) \
         i2c_register_driver(THIS_MODULE, driver)

i2c_add_driver 就是对 i2c_register_driver 做了一个简单的封装,只有一个参数,就是要注册的 i2c_driver。

66行,调用 i2c_del_driver 来注销掉前面 注册的 ap3216c_driver,函数原型如下:

void i2c_del_driver(struct i2c_driver *driver)

driver:要注销的 i2c_driver。返回值: 无。

⑧、读取AP3216C的N个寄存器

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

 这里先看61行,一般需要在 probe 函数里面初始化 I2C 设备,要初始化 I2C 设备就必须能够对 I2C 设备寄存器进行读写操作,这里就要用到 i2c_transfer 函数了,i2c_transfer 函数最终会调用 I2C 适配器中 i2c_algorithm 里面的 master_xfer 函数,对于 I.MX6U 而言就是i2c_imx_xfer 这个函数。 i2c_transfer 函数原型如下:

int i2c_transfer(struct i2c_adapter *adap,

                                struct i2c_msg *msgs,

                                int num)

adap: 所使用的 I2C 适配器, i2c_client 会保存其对应的 i2c_adapter。
msgs: I2C 要发送的一个或多个消息。
num: 消息数量,也就是 msgs 的数量。
返回值: 负值,失败,其他非负值,发送的 msgs 数量

 从上面介绍i2c_client结构体就能看到有adapter成员,在probe函数里面已经获取到ap3216cdev设备的private_data成员中

48行,就是把private_data成员中把私有数据提出来,就在61行第一个参数中使用i2c_client中的adapter成员

继续看 i2c_transfer 函数msgs 这个参数,这是一个 i2c_msg 类型的指针参数, I2C 进行数据收发
说白了就是消息的传递, Linux 内核使用 i2c_msg 结构体来描述一个消息。 i2c_msg 结构体定义
在 include/uapi/linux/i2c.h 文件中

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

 69行,从机地址

 70行,标志

72行,read data,读数据

79行,要发送消息(本 msg)长度

80行,发送消息的数据

47行,定义结构体数组,按照I2C读时序把操作分为两部分

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

前面两段作为一部分,就是50-54行;后面两段作另一部分,就是56-59行

50-54行,发送器件寄存器地址。从i2c_client中获取从机地址,标志为0,表示发送数据,要发送的数据,也就是器件寄存器地址,寄存器地址长度为1个字节

56-59行,读取数据。从i2c_client中获取从机地址,标志表示读数据,读出来的数据保存在val,最后为读取的长度

再回到61行,第二个参数就是结构体数组msg,第三个参数就是2,结构体数组msg的长度

⑨、向AP3216C的N个寄存器写数据

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

 76行,这里根据I2C写时序,寄存器地址和数据一次可以发完,所以只需要一个msg即可

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

79行,寄存器地址保存在b[0],先发寄存器地址

80行,把要发送的数据拷贝到b数组里面

⑩、读、写AP3216C一个寄存器

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

 就是调用函数

⑩①、读取AP3216C的数据

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

⑩②完善ap3216c_open、read函数

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

 linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

 代码如下

 

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/io.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/slab.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <linux/atomic.h>
#include <linux/timer.h>
#include <linux/jiffies.h>
#include <linux/string.h>
#include <linux/irq.h>
#include <linux/interrupt.h>
#include <linux/input.h>
#include <linux/i2c.h>
#include <linux/delay.h>
#include "ap3216c.h"

#define AP3216C_CNT 1
#define AP3216C_NAME "ap3216c"

struct ap3216c_dev{
    int devid;/* 设备号 	 */
    int major;
    int minor;
    struct cdev cedv;/* cdev 	*/
    struct class *class;/* 类 		*/
    struct device *device;/* 设备 	 */
    void *private_data;/*私有数据*/
    unsigned short ir, als, ps;		/* 三个光传感器数据 */ 
};

static struct ap3216c_dev ap3216cdev;

/*读取AP3216C的N个寄存器*/
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 = &reg;/*要发送的数据,也就是寄存器地址*/
    msg[0].len = 1;/*要发送的寄存器地址长度为1*/
    /*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;
}

/*向AP3216C的N个寄存器写数据*/
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;/*msg[0]发送要读取的寄存器首地址*/
    memcpy(&b[1], buf, len);
     
    msg.addr = client->addr;/*从机地址,也就是ap3216c*/
    msg.flags = 0;/*表示为要发送的数据*/
    msg.buf = b;/*要发送的数据,寄存器首地址+实际的数据*/
    msg.len = len +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;
}
/*向AP3216C一个寄存器写数据*/
static void ap3216c_write_reg(struct ap3216c_dev *dev, u8 reg , u8 data)
{
    u8 buf = data;
    ap3216c_write_regs(dev,reg,&buf,1);
}

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); 
}

static int ap3216c_open(struct inode *inode, struct file *filp)
{
    unsigned char value = 0;
    filp->private_data = &ap3216cdev;
    printk("ap3216c_open\r\n");
    /*初始化ap3216c*/
    ap3216c_write_reg(&ap3216cdev,AP3216C_SYSTEMCONG,0x4);/*复位*/
    mdelay(50);/*50ms*/
    ap3216c_write_reg(&ap3216cdev,AP3216C_SYSTEMCONG,0x3);/*复位*/
    value = ap3216c_read_reg(&ap3216cdev,AP3216C_SYSTEMCONG);
    printk("AP3216C_SYSTEMCONG = %#x\r\n",value);

  
    return 0;
}

static int ap3216c_release(struct inode *inode, struct file *filp)
{
    printk("ap3216c_release\r\n");
    return 0;
}
static 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 struct file_operations ap3216c_fops  = {
    .owner  =   THIS_MODULE,
    .open   =   ap3216c_open,
    .read   =   ap3216c_read,
    .release = ap3216c_release,
};

static int ap3216c_probe(struct i2c_client *client,const struct i2c_device_id *id)
{
    int ret =0;
    /*搭建字符设备驱动框架*/
    /*1、创建字符设备*/
    ap3216cdev.major=0;
    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 error!\r\n");
        goto fail_devid;
    }
    printk("ap3216cde major: %d minor: %d\r\n",ap3216cdev.major,ap3216cdev.minor);
    /*2、注册字符设备*/
    ap3216cdev.cedv.owner = THIS_MODULE;
    cdev_init(&ap3216cdev.cedv,&ap3216c_fops);
    ret = cdev_add(&ap3216cdev.cedv,ap3216cdev.devid,AP3216C_CNT);
    if(ret < 0)
    {
        printk("cdev_add error!\r\n");
        goto fail_cdev;
    }
    /*3、自动创建设备节点*/
    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;
    }

    printk("ap3216c_probe\r\n");
    ap3216cdev.private_data = client;
    return 0;
fail_device:
    class_destroy(ap3216cdev.class);
fail_class:
    cdev_del(&ap3216cdev.cedv);
fail_cdev:
    unregister_chrdev_region(ap3216cdev.devid,AP3216C_CNT);
fail_devid:
    return ret;
}
static int ap3216c_remove(struct i2c_client *client)
{
    cdev_del(&ap3216cdev.cedv);
    unregister_chrdev_region(ap3216cdev.devid,AP3216C_CNT);
    device_destroy(ap3216cdev.class, ap3216cdev.devid);
    class_destroy(ap3216cdev.class);
    printk("ap3216c_remove\r\n");
    return 0;
}
/*传统匹配表*/
static struct i2c_device_id ap3216c_id[] = {
    {"my,ap3216c",0},
    {}
};
/*设备树匹配*/
static struct of_device_id ap3216c_of_match[] = {
    {.compatible = "my,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("ba che kai qi lai");

4、ap3216c.c

#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数组元素个数)
    argv:具体参数,也可以写作char **argv
    ./ap3216cAPP <filename>    
    ./ap3216cAPP  /dev/ap3216c
*/


int main(int argc, char *argv[])
{
    int fd,err;
    char *filename;
    unsigned short ir, als, ps, data[3];
    
    /*判断命令行输入参数是否正确*/
    if(argc != 2){
        printf("error usage!\r\n");
        return -1;
    }
    /*用指针指向文件*/
    filename = argv[1];
    /*打开文件*/
    fd = open(filename , O_RDWR);
    if(fd < 0){
        printf("file open failed\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;
}

 用APP测试驱动

linux i2c_transfer,# IMX6ULL驱动,linux,ubuntu,arm开发,arm,驱动开发

 用手接近和用灯照ap3216c传感器,数值都会发生变化文章来源地址https://www.toymoban.com/news/detail-718632.html

到了这里,关于Linux I2C 驱动实验的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • linux-2.6.22.6内核i2c驱动框架源码分析

    i2c是常见的通信协议,协议比较简单,只有数据和时钟两条线(SDA和SCL),i2c的通信分为主机和从机,主机一般占主导地位,从机可以有多个。 i2c通信的数据格式为(SDA上的数据):开始的7位里面指定了设备地址(因为有多个从机),第8位是读或写信号,表示此次传输是读还

    2024年02月11日
    浏览(47)
  • linux i2c驱动开发之最简单入门:设备树与驱动的匹配

    1在设备树中声明我们的模拟I2C设备: i2c的设备加入是必须放在某个i2c的总线下面的,我们这里是放在 i2c7下面。 可以直接在设备树搜索i2c7,然后把status 修改为okay,然后再加入设备信息. 这里我们指定和驱动匹配的字符串是 myi2c-dev,在bus segment 上分配的地址是 0x70 。 当然这

    2024年02月16日
    浏览(39)
  • 详解AT24CXX驱动开发(linux platform tree - i2c应用)

    目录 概述 1 认识AT24Cxx 1.1 AT24CXX的特性 1.2 AT24CXX描述 1.2.1 引脚 1.2.2 容量描述 1.2.3 设备地址 1.3 操作时序 1.3.1 写单个字节时序 1.3.2 写page字节时序 1.3.3 读取当前数据时序 1.3.4 随机读取数据 1.3.5 连续读取多个数据 2 驱动开发 2.1 硬件接口 2.2 代码实现 2.2.1 查看设备信息 2.2.2 编写

    2024年02月22日
    浏览(38)
  • Linux设备驱动开发学习笔记(等待队列,锁,字符驱动程序,设备树,i2C...)

    container_of函数可以通过结构体的成员变量检索出整个结构体 函数原型: 内核开发者只实现了循环双链表,因为这个结构能够实现FIFO和LIFO,并且内核开发者要保持最少代码。 为了支持链表,代码中要添加的头文件是linux/list.h。内核中链表实现核心部分的数据结构 是struct li

    2024年01月22日
    浏览(46)
  • 【I2C】基于Linux移植i2c-tool工具

    i2c-tool 工具下载地址: https://mirrors.edge.kernel.org/pub/software/utils/i2c-tools/ 因为我这里需要将i2c-tool移植到imx6ull嵌入式平台,所以编译时肯定需要重新指定GCC。查看i2c-tool根目录下的 Makefile 文件,默认为系统GCC工具编译: 在ubuntu编译i2c-tool源码之前,首先设置imx6ull嵌入式平台的交

    2024年02月13日
    浏览(31)
  • linux下i2c调试神器i2c-tools安装及使用

    在嵌入式linux开发中,有时候需要确认i2c硬件是否正常连接,设备是否正常工作,设备的地址是多少等等,这里我们就需要使用一个用于测试I2C总线的工具——i2c-tools。 i2c-tools是一个专门调试i2c的开源工具,可获取挂载的设备及设备地址,还可以读写I2C设备寄存器。调试新的

    2024年04月26日
    浏览(29)
  • 【IMX6ULL驱动开发学习】10.Linux I2C驱动实战:AT24C02驱动设计流程

    前情回顾:【IMX6ULL驱动开发学习】09.Linux之I2C框架简介和驱动程序模板_阿龙还在写代码的博客-CSDN博客 目录 一、修改设备树(设备树用来指定引脚资源) 二、编写驱动 2.1 i2c_drv_read 2.2 i2c_drv_write 2.3 完整驱动程序 三、上机测试 放在哪个I2C控制器下面 AT24C02的I2C设备地址(查

    2024年02月11日
    浏览(41)
  • I2C总线驱动

    SOC芯片平台的外设分为: 一级外设:外设控制器集成在SOC芯片内部 二级外设:外设控制器由另一块芯片负责,通过一些通讯总线与SOC芯片相连 Inter-Integrated Circuit: 字面意思是用于“集成电路之间”的通信总线,简写:IIC(或者I2C) i2c传输的要点就是: 传输一个字节 后面必然

    2024年02月15日
    浏览(35)
  • Linux在应用层上使用I2C

    通常情况下i2c读写一般是在kernel中使用,但是在应用层上一样可以使用。在应用上可以通过读写/dev/i2c-x这个节点从而控制i2c接口进行读写数据。 通常一个SOC有多个I2C控制器,假设有这个SOC有3个控制器,我们会在/dev目录下看到i2c-0、i2c-1、i2c-2,计数从0开始。 1.首先使用的时

    2024年02月02日
    浏览(37)
  • <Linux开发> linux开发工具-之-I2C TOOLS工具使用

    <Linux开发> linux开发工具-之-I2C TOOLS工具使用 <Android开发> Android开发工具- 之-I2C TOOLS工具使用 <Linux开发>驱动开发 -之- Linux I2C 驱动 在笔者的另一篇文章 <Android开发> Android开发工具- 之-I2C TOOLS工具使用讲解过,如何在android上使用I2C TOOLS工具。本文主要是分析如何在

    2024年02月16日
    浏览(43)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包