Linux IIC驱动笔记

Linux IIC驱动笔记

最近看了百问网的linux驱动视频,关于IIC部分总结如下: 

一、IIC 驱动框架
应用层        open        read        write
——————————————————
驱动层
IIC设备驱动(drv_open     drv_read    drv_write) 
                IIC总线驱动         
——————————————————
硬件  (例如:AT24C02 )

IIC设备驱动的drv_open、drv_read、drv_write分别对应应用层得open 、read、write等函数的接口,知道传递的数据的具体含义。在内核源代码的drivers/i2c/chips目录中,有很多IIC设备驱动程序。
IIC 总线驱动用于 识别IIC设备,提供读写函数,提供 如何收发数据,但是不 知道数据的具体含义。在内核源代码的drivers/i2c/busses/目录中有很多IIC总线驱动,例如S3C2440,对应i2c-s3c2410.c。

总线   设备   驱动   模型

                                                        bus

            i2c_add_adapter                                        i2c_add_driver

            (1)dev                                                                    driver(2)


(1) i2c总线驱动程序功能(以drivers/i2c/busses/i2c-s3c2410.c为例):
    <1>定义分配一个 struct    s3c24xx_i2c *i2c的结构体,在该结构体 包含一个i2c_adapter的结构体:     
static struct     s3c24xx_i2c  s3c24xx_i2c ={
        .lock                =__SPIN_LOCK_UNLOCKED(s3c24xx_i2c.lock),
        .wait                =__WAIT_QUEUE_HEAD_INITIALIZER(s3c24xx_i2c.wait),
        .tx_setup        =50,
        . adap        ={
                .name                        ="s3c2410-i2c",
                .owner                        =THIS_MODULE,
                . algo            =&s3c24xx_i2c_algorithm,
                .retries                =2,
                .class                        =I2C_CLASS_HWMON,
        },
};
在这个结构体中,最重要的是algo算法!
在 s3c24xx_i2c_algorithm中,最重要的是s3c24xx_i2c_xfer函数,实现了 数据的收发功能。
static const struct i2c_algorithm s3c24xx_i2c_algorithm ={
        .master_xfer                =s3c24xx_i2c_xfer,
        .functionality                =s3c24xx_i2c_func,
};

<2>  i2c_add_adapter 函数  对adapter进行 注册
i2c_add_adapter 作用
      将adapter放入链表;
    调用driver中的attach_adapter函数;
    在attach_adapte调用i2c_probe函数。
用adapter的master_xfer发信号确定有没有该设备,如果有,调用i2c_probe中的定义的发现这个设备后要调用的函数!

(2)i2c设备驱动程序功能(以drivers/i2c/chip/eeprom.c为例):
分配构造一个i2c_driver
static   struct   i2c_driver  eeprom_driver ={
        .driver= {
                .name        ="eeprom",
        },
        .id                =I2C_DRIVERID_EEPROM,
        .attach_adapter        =eeprom_attach_adapter,
        .detach_client        =eeprom_detach_client,
};
.attach_adapter 成员表示调用adapter连接设备,

使用i2c_add_driver函数将i2c_driver放入链表,,
从adapter链表取出适配器调用driver的attach_adapter函数,在attach_adapter中调用i2c_probe函数,用adapter的master_xfer发信号,确定有没有该设备,如果有,调用i2c_probe中的定义的发现这个设备后要调用的函数!

二、怎么
写I2C设备驱动程序   !!!!!!!!!!!!!!!
1. 分配一个 i2c_driver结构体
2.  设置attach_adapte函数和detach_client函数
            attach_adapter直接调用i2c_probe(adap, 设备地址, 发现这个设备后要调用的函数);
            detach_client表示 卸载这个驱动后,如果之前发现能够支持的设备,则调用它来清理
           
3. 注册:使用i2c_add_driver来注册。

三、以at24cxx.c为例介绍一下 i2c驱动的编写
Linux内核版本:linux-2.6.22.6
开发板:mini2440

建立一个at24cxx.c的文件
包含头文件如下:
#include
#include
#include
#include
#include
#include
#include
#include
#include

1、首先 定义出入口和出口函数,定义一个结构体  i2c_driver at24cxx_driver包括driver->name、attach_adapter和detach_client三个成员, 在at24cxx_init中使用i2c_add_driver注册该驱动,在 at24cxx_exit中使用i2c_del_driver卸载该驱动。
代码如下:
static   struct    i2c_driver    at24cxx_driver = {
        .driver= {
                .name        ="at24cxx",
        },
        . attach_adapter =at24cxx_attach,
        .detach_client    =at24cxx_detach,
};

static   int  at24cxx_init(void)
{
        i2c_add_driver(&at24cxx_driver);
        return0;
}

static void at24cxx_exit(void)
{
        i2c_del_driver(&at24cxx_driver);
}

module_init(at24cxx_init);
module_exit(at24cxx_exit);

MODULE_LICENSE("GPL");

2、具体 实现at24cxx_attach函数。执行 i2c_add_driver(&at24cxx_driver) 如果内核中已经注册了i2c适配器,则顺序调用这些适配器来连接我们的i2c设备,此过程 是通过调用i2c_driver中的attach_adapter方法完成的。代码如下:

static unsigned short  ignore[]            ={ I2C_CLIENT_END };       //????
static unsigned short  normal_addr[] = { 0x50, I2C_CLIENT_END};

static   struct    i2c_client_address_data  addr_data ={
        . normal_i2c     normal_addr 
        .probe                =ignore,
        .ignore                =ignore,
        };
static int  at24cxx_attach(struct i2c_adapter*adapter)
{
        return  i2c_probe(adapter,&addr_data, at24cxx_detect);
}
3、具体实现at24cxx_detect函数, 在at24cxx_attach函数中,调用i2c_probe函数,i2c_probe探测到设备后,调用at24cxx_detect函数,并把探测的地址作为参数输入。在  at24cxx_detect函数中,构造一个i2c_client结构体用于收发I2C数据,调用 i2c_attach_client将client和adapter关联!然后 注册字符驱动设备,用于读写IIC数据


struct i2c_client *at24cxx_client;

static int major;
static struct class *cls;

static   struct    file_operations  at24cxx_fops ={
        .owner= THIS_MODULE,
        .read    =at24cxx_read,
        .write= at24cxx_write,
};

static int  at24cxx_detect(struct i2c_adapter *adapter,int address, int kind)
     
        printk("at24cxx_detect\n");

       
        at24cxx_client= kzalloc(sizeof(struct i2c_client), GFP_KERNEL);
        at24cxx_client->addr        =address;
        at24cxx_client->adapter = adapter;
        at24cxx_client->driver    =&at24cxx_driver;
        strcpy(at24cxx_client->name,"at24cxx");
        i2c_attach_client(at24cxx_client);
       
        major= register_chrdev(0, "at24cxx", &at24cxx_fops);

        cls= class_create(THIS_MODULE, "at24cxx");
        class_device_create(cls,NULL, MKDEV(major, 0), NULL, "at24cxx");
       
        return0;
}

4、剩下的就是具体实现at24cxx_write和at24cxx_read了!
在at24cxx_write中:
使用copy_from_user(val, buf,2)获得用户空间传入的要写入的寄存器地址和寄存器数据。构造一个写消息,通过i2c_transfer()函数完成消息的传递,最终写入相应寄存器数值。

在at24cxx_read中
使用copy_from_user(&address, buf,1);获得用户空间传入的要读出的寄存器地址,构造一个读消息,一个写消息,通过i2c_transfer()函数完成消息的传递,读出相应寄存器数值,通过copy_to_user(buf,&data, 1)发送给应用层


static ssize_t at24cxx_ write(struct file *file, const char __user*buf, size_t size, loff_t *offset)
{
        unsignedchar val[2];
        struct  i2c_msg msg[1];
        intret;
       
        if(size != 2)
                return-EINVAL;
       
        copy_from_user(val,buf, 2);

       
        msg[0].addr    =at24cxx_client->addr;   
        msg[0].buf    =val;                                   
        msg[0].len    =2;                                       
        msg[0].flags=0;                                       

        ret= i2c_transfer(at24cxx_client->adapter, msg, 1);
        if(ret == 1)
                return2;
        else
                return-EIO;
}


static ssize_t at24cxx_ read(struct file *file, char __user *buf,size_t size, loff_t * offset)
{
        unsignedchar address;
        unsignedchar data;
        struct  i2c_msg msg[2];
        intret;
       
       
        if(size != 1)
                return-EINVAL;
       
        copy_from_user(&address,buf, 1);

       

       
        msg[0].addr    =at24cxx_client->addr;   
        msg[0].buf    =&address;                           
        msg[0].len    =1;                                       
        msg[0].flags=0;                                       

       
        msg[1].addr    =at24cxx_client->addr;   
        msg[1].buf    =&data;                               
        msg[1].len    =1;                                       
        msg[1].flags=I2C_M_RD;                                       


        ret= i2c_transfer(at24cxx_client->adapter, msg, 2);
        if(ret == 2)
        {
                copy_to_user(buf,&data, 1);
                return1;
        }
        else
                return-EIO;
}
5、at24cxx_ detach是 调用内核中注册的适配器 来    断开我们注册过的i2c设备
static int at24cxx_detach(struct i2c_client *client)
{
        printk("at24cxx_detach\n");
        class_device_destroy(cls,MKDEV(major, 0));
        class_destroy(cls);
        unregister_chrdev(major,"at24cxx");

        i2c_detach_client(client);
        kfree(i2c_get_clientdata(client));

        return0;
}


6应用程序
首先使用fd = open("/dev/at24cxx", O_RDWR)打开at24cxx设备文件, 通过传入的参数“r”“w”来判断是读寄存器还是写寄存器,如果是读寄存器,则调用read(fd,buf, 1)完成读取,如果是写寄存器,则调用write(fd, buf, 2)完成写入。

#include
#include
#include
#include
#include
#include




void print_usage(char *file)
{
        printf("%sr addr\n", file);
        printf("%sw addr val\n", file);
}

int main(int argc, char **argv)
{
        intfd;
        unsignedchar buf[2];
       
        if((argc != 3) && (argc != 4))
        {
                print_usage(argv[0]);
                return-1;
        }

        fd= open("/dev/at24cxx", O_RDWR);
        if(fd < 0)
        {
                printf("can'topen /dev/at24cxx\n");
                return-1;
        }

        if(strcmp( argv[1], "r") ==0)
        {
                buf[0]= strtoul(argv[2], NULL, 0);
                read(fd,buf, 1);
                printf("data:%c, %d, 0x%2x\n", buf[0], buf[0], buf[0]);
        }
        elseif (strcmp( argv[1], "w") ==0)
        {
                buf[0]= strtoul(argv[2], NULL, 0);
                buf[1]= strtoul(argv[3], NULL, 0);
                write(fd,buf, 2);
        }
        else
        {
                print_usage(argv[0]);
                return-1;
        }
       
        return0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值