裝置結構體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);//傳輸函數