linux裝置驅動之I2C

2021-09-23 22:50:14 字數 2837 閱讀 1656

裝置結構體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 inte***ce (選上)

配置上介面口,編譯時不會出現問題,可是新的問題出現了,不會自動匹配檔案,解決方法如下  

[ ]   autoselect pertinent helper modules (去掉)  

<*> i2c bit-banging inte***ces(選上)

<*> s3c2410 i2c driver(選上)

<*> tiny-usb adapter 選上)    

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

#include

#include

#include

#include

#include

#include

#include

#include

#include //copy_to_user

static unsigned short ignore      = ;

static unsigned short normal_addr = ; /* 位址值是7位 */

/* 改為0x60的話, 由於不存在裝置位址為0x60的裝置, 所以at24cxx_detect不被呼叫 */

//位址是用的後7位

static unsigned short force_addr = ;

static unsigned short * forces = ;

static struct i2c_client_address_data addr_data = ;

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)

else

return -eio;

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

static struct file_operations at24cxx_fops = ;

static int at24cxx_detect(struct i2c_adapter *adapter, int address, int kind)

static int at24cxx_attach(struct i2c_adapter *adapter)

static int at24cxx_detach(struct i2c_client *client)

/* 1. 分配乙個i2c_driver結構體 */

/* 2. 設定i2c_driver結構體 */

static struct i2c_driver at24cxx_driver = ,

.attach_adapter = at24cxx_attach,

.detach_client  = at24cxx_detach,};

static int at24cxx_init(void)

static void at24cxx_exit(void)

自我總結:

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);//傳輸函式

Linux驅動之I2C裝置驅動

核心 4.20 晶元 hym8563 rtc 下面的 分析主要都在注釋中,會按照驅動中函式的執行順序分析。static const struct i2c device id hym8563 id module device table i2c,hym8563 id static const stru...

linux裝置驅動之I2C

裝置結構體i2c client中addr的低8位表示裝置位址。裝置位址由讀寫位 器件型別和自定義位址組成,第7位是r w位,0表示寫,1表示讀,所以i2c裝置通常有兩個位址,即讀位址和寫位址型別器件由中間4位組成,這是由半導體公司生產的時候就已經固化了。自定義型別由低3位組成。由使用者自己設定,通常...

i2c裝置驅動

1,i2c 裝置註冊 static struct i2c board info i2c2 devices i2c裝置一般在板級 中註冊 static void msm8916 add i2c deivces void 2,i2c驅動註冊 include static const struct i2c...