天天看点

linux设备驱动之I2C

设备结构体i2c_client中addr的低8位表示设备地址。设备地址由读写位、器件类型和自定义地址组成,第7位是r/w位,0表示写,1表示读,所以i2c设备通常有两个地址,即读地址和写地址类型器件由中间4位组成,这是由半导体公司生产的时候就已经固化了。

自定义类型由低3位组成。由用户自己设置,通常的做法如eeprom这些器件是由外部i芯片的3个引脚所组合电平决定的(a0,a1,a2)。a0,a1,a2 就是自定义的地址码。自定义的地址码只能表示8个地址,所以同一iic总线上同一型号的芯片最多只能挂载8个。如果在两个不同iic总线上挂接了两块类型和地址相同的芯片,那么这两块芯片的地址相同。这显然是地址冲突,解决的办法是为总线适配器指定一个id号,那么新的芯片地址就由总线适配器的id和设备地址组成

在做iic总线实验的时候编译出现问题并且不能够匹配

warning: "i2c_del_driver" [/home/book/workspace/share/drivers/iic_drv/1th/at24cxx.ko] undefined!

warning: "i2c_register_driver" [/home/book/workspace/share/drivers/iic_drv/1th/at24cxx.ko] undefined!

warning: "i2c_probe" [/home/book/workspace/share/drivers/iic_drv/1th/at24cxx.ko] undefined!

这是由于内核配置没有选上接口:

 <*> i2c support  ---> 

 <*>   i2c device interface (选上)

配置上接口口,编译时不会出现问题,可是新的问题出现了,不会自动匹配文件,解决方法如下  

 [ ]   autoselect pertinent helper modules (去掉)  

          <*> i2c bit-banging interfaces(选上)

  <*> s3c2410 i2c driver(选上)

   <*> tiny-usb adapter 选上)    

*** other i2c/smbus bus drivers *** 

#include <linux/kernel.h>

#include <linux/init.h>

#include <linux/module.h>

#include <linux/slab.h>

#include <linux/jiffies.h>

#include <linux/i2c.h>

#include <linux/mutex.h>

#include <linux/fs.h>

#include <asm/uaccess.h>  //copy_to_user

static unsigned short ignore[]      = { i2c_client_end };

static unsigned short normal_addr[] = { 0x50, i2c_client_end }; /* 地址值是7位 */

                                        /* 改为0x60的话, 由于不存在设备地址为0x60的设备, 所以at24cxx_detect不被调用 */

//地址是用的后7位

static unsigned short force_addr[] = {any_i2c_bus, 0x60, i2c_client_end};

static unsigned short * forces[] = {force_addr, null};

static struct i2c_client_address_data addr_data = {

.normal_i2c

= normal_addr,  /* 要发出s信号和设备地址并得到ack信号,才能确定存在这个设备 */

.probe

= ignore,

.ignore

//.forces     = forces, /* 强制认为存在这个设备 */

};

static struct i2c_driver at24cxx_driver;

static int major;

static struct class *cls;

struct i2c_client *at24cxx_client;

static ssize_t at24cxx_read(struct file *file, char __user *buf, size_t size, loff_t * offset)

{

unsigned char address;

unsigned char data;

struct i2c_msg msg[2];

int ret;

/* address = buf[0] 

* data    = buf[1]

*/

if (size != 1)

return -einval;

copy_from_user(&address, buf, 1);

/* 数据传输三要素: 源,目的,长度 */

/* 读at24cxx时,要先把要读的存储空间的地址发给它 */

msg[0].addr  = at24cxx_client->addr;  /* 目的 */

msg[0].buf   = &address;              /* 源 */

msg[0].len   = 1;                     /* 地址=1 byte */

msg[0].flags = 0;                     /* 表示写 */

/* 然后启动读操作 */

msg[1].addr  = at24cxx_client->addr;  /* 源 */

msg[1].buf   = &data;                 /* 目的 */

msg[1].len   = 1;                     /* 数据=1 byte */

msg[1].flags = i2c_m_rd;                     /* 表示读 */

ret = i2c_transfer(at24cxx_client->adapter, msg, 2);

if (ret == 2)

copy_to_user(buf, &data, 1);

return 1;

}

else

return -eio;

static ssize_t at24cxx_write(struct file *file, const char __user *buf, size_t size, loff_t *offset)

unsigned char val[2];

struct i2c_msg msg[1];

if (size != 2)

copy_from_user(val, buf, 2);

msg[0].buf   = val;                   /* 源 */

msg[0].len   = 2;                     /* 地址+数据=2 byte */

ret = i2c_transfer(at24cxx_client->adapter, msg, 1);

if (ret == 1)

return 2;

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

/* 构构一个i2c_client结构体: 以后收改数据时会用到它 */

at24cxx_client = kzalloc(sizeof(struct i2c_client), gfp_kernel);

at24cxx_client->addr    = address;//卸载驱动时候需要i2c_client

at24cxx_client->adapter = adapter;//设置才能调用at24cxx_detach

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

device_create(cls, null, mkdev(major, 0), null, "at24cxx"); /* /dev/at24cxx */

return 0;

static int at24cxx_attach(struct i2c_adapter *adapter)

return i2c_probe(adapter, &addr_data, at24cxx_detect);

static int at24cxx_detach(struct i2c_client *client)

printk("at24cxx_detach\n");

device_destroy(cls, mkdev(major, 0));

class_destroy(cls);

unregister_chrdev(major, "at24cxx");

i2c_detach_client(client);

kfree(i2c_get_clientdata(client));

/* 1. 分配一个i2c_driver结构体 */

/* 2. 设置i2c_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);

static void at24cxx_exit(void)

i2c_del_driver(&at24cxx_driver);

自我总结:

i2c_add_driver->

i2c_register_driver->

at24cxx_attach-   >

i2c_probe    ->

//如果检测到了这个设备的id号,那么就调用at24cxx_detect

i2c_del_driver->

driver_unregister->

at24cxx_detach->

struct i2c_msg msg[1];//传输文件的消息结构体

i2c_transfer(at24cxx_client->adapter, msg, 1);//传输函数

继续阅读