思路清晰!Linux設(shè)備驅(qū)動子系統(tǒng)之I2C_第1頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、思路清晰!linux設(shè)備驅(qū)動子系統(tǒng)之i2c· i2c inter-integrated circuit· smbus system management bus, the i2c subset1.2 characteristics· the amount of data exchanged is small.· the required data transfer rate is low.1.3 speed· fast speed 400 kbps· full speed 100 kbps1.4 topology2 data stru

2、cture理解數(shù)據(jù)結(jié)構(gòu)對理解囫圇驅(qū)動程序子系統(tǒng)是很重要的。i2c的主要有兩大數(shù)據(jù)結(jié)構(gòu),struct i2c_client 和 structi2c_adapter。2.1 i2c_clientstruct i2c_client unsigned short flags; /* div., see below */unsigned short addr; /* chip address */char namei2c_name_size;struct i2c_adapter *adapter; /* the adapter we sit on */struct i2c_driver *driver;

3、 /* and our access routines */struct device dev; /* the device structure */int irq; /* irq issued by device (or -1) */char driver_namekobj_name_len;struct list_head list; /* deprecated */struct completion released;struct i2c_client代表一個掛載到i2c上的i2c從設(shè)備,該設(shè)備所需要的數(shù)據(jù)結(jié)構(gòu),其中包括· 該i2c從設(shè)備所依附的i2c主設(shè)備 struct i2

4、c_adapter *adapter· 該i2c從設(shè)備的驅(qū)動程序struct i2c_driver *driver· 作為i2c從設(shè)備所通用的成員變量,比如addr, name等· 該i2c從設(shè)備驅(qū)動所特有的數(shù)據(jù),依附于dev->driver_data下2.2 i2c_adapterstruct i2c_adapter struct module *owner;unsigned int id;unsigned int class;const struct i2c_algorithm *algo; /* the algorithm to access the

5、bus */void *algo_data;. .;struct i2c_adapter代表主芯片所支持的一個i2c主設(shè)備,該設(shè)備所需要的數(shù)據(jù)結(jié)構(gòu),其中,struct i2c_algorithm *algo是該i2c主設(shè)備傳輸數(shù)據(jù)的一種算法,或者說是在i2c總線上完成主從設(shè)備間數(shù)據(jù)通信的一種能力。struct i2c_algorithm int (*master_xfer)(struct i2c_adapter *adap,struct i2c_msg *msgs, int num);int (*smbus_xfer) (struct i2c_adapter *adap, u16 addr,u

6、nsigned short flags, char read_write,u8 command, int size, union i2c_smbus_data * data);u32 (*functionality) (struct i2c_adapter *);接下來,要實現(xiàn)囫圇i2c子系統(tǒng)的驅(qū)動,便圍繞這兩個數(shù)據(jù)結(jié)構(gòu)綻開,其主要步驟可總結(jié)為以下三步,· 實現(xiàn)i2c主設(shè)備驅(qū)動 (drivers/i2c/bus/*)· 注冊i2c從設(shè)備的i2c_client (drivers/i2c/i2c-core)· 實現(xiàn)i2c從設(shè)備驅(qū)動3 adapter內(nèi)核名目driver

7、s/i2c下有兩個文件夾,algorithm和bus,其中bus存放i2c主設(shè)備的驅(qū)動,主設(shè)備驅(qū)動完成兩大任務(wù),· 提供該i2c主設(shè)備與從設(shè)備間完成數(shù)據(jù)通信的能力· 完成該i2c_adapter和全部已知的i2c_client的注冊以i2c-pxa.c為例,/* drivers/i2c/bus/i2c-pxa.c */static int _init i2c_adap_pxa_init(void)return platform_driver_register(&i2c_pxa_driver);static struct platform_driver i2c_pxa

8、_driver = .probe = i2c_pxa_probe,. .id_table = i2c_pxa_id_table,;static int i2c_pxa_probe(struct platform_device *dev)struct pxa_i2c *i2c;i2c->adap.algo = i2c_pxa_algorithm; / 提供該i2c主設(shè)備與從設(shè)備間完成數(shù)據(jù)通信的能力i2c_add_numbered_adapter(&i2c->adap); / 調(diào)用i2c-core.c中的接口函數(shù),完成該i2c_adapter和i2c_client的注冊. .s

9、tatic const struct i2c_algorithm i2c_pxa_algorithm = .master_xfer = i2c_pxa_xfer, / 按照pxa詳細(xì)芯片的要求,完成i2c數(shù)據(jù)傳輸.functionality = i2c_pxa_functionality,;4 i2c-core內(nèi)核名目drivers/i2c下的i2c-core.c,顧名思義,是內(nèi)核為i2c提供的統(tǒng)一系統(tǒng)接口??纯磇2c_add_numbered_adapter做了些什么,int i2c_add_numbered_adapter(struct i2c_adapter *adap). .statu

10、s = i2c_register_adapter(adap);return status;static int i2c_register_adapter(struct i2c_adapter *adap). .device_register(&adap->dev); /完成i2c主設(shè)備adapter的注冊,即注冊object和發(fā)送uevent等i2c_scan_static_board_info(adap);. .i2c_scan_static_board_info(adap),此函數(shù)為囫圇i2c子系統(tǒng)的核心,它會去遍歷一個由i2c從設(shè)備組成的雙向循環(huán)鏈表,并完成全部i2c從設(shè)備

11、的i2c_client的注冊,詳細(xì)過程如下,static void i2c_scan_static_board_info(struct i2c_adapter *adapter)struct i2c_devinfo *devinfo; /已經(jīng)建立好了的i2c從設(shè)備鏈表list_for_each_entry(devinfo, &_i2c_board_list, list) i2c_new_device(adapter,&devinfo->board_info);. .struct i2c_client *i2c_new_device(struct i2c_adapter *

12、adap, struct i2c_board_info const *info). .i2c_attach_client(client);. .int i2c_attach_client(struct i2c_client *client). .device_register(&client->dev); /完成i2c從設(shè)備client的注冊. .那么,這個i2c從設(shè)備組成的雙向循環(huán)鏈表,是什么時候通過什么方式建立起來的呢?以某重力感應(yīng)設(shè)備為例,/* /arch/arm/mach-pxa/starwood_p1.c */static void _init saar_init(vo

13、id). .i2c_register_board_info(0, array_and_size(saar_i2c_bma220_info);. .static struct i2c_board_info saar_i2c_bma220_info = .driver_name = "bma220",.addr = 0x0b,.irq = irq_gpio(mfp_to_gpio(mfp_pin_gpio15),;/* drivers/i2c/i2c-boardinfo.c */int _init i2c_register_board_info(int busnum, stru

14、ct i2c_board_info const *info, unsigned len). .struct i2c_devinfo *devinfo;devinfo->board_info = *info;list_add_tail(&devinfo->list, &_i2c_board_list); /將i2c從設(shè)備加入該鏈表中. .所以,在系統(tǒng)初始化的過程中,我們可以通過 i2c_register_board_info,將所需要的i2c從設(shè)備加入一個名為_i2c_board_list雙向循環(huán)鏈表,系統(tǒng)在勝利加載i2c主設(shè)備adapt后,就會對這張鏈表里全部i2c

15、從設(shè)備逐一地完成 i2c_client的注冊。5 slave driver假如說硬件方面,i2c主設(shè)備已經(jīng)集成在主芯片內(nèi),軟件方面,linux也為我們提供了相應(yīng)的驅(qū)動程序,位于drivers/i2c/bus下,那么接下來i2c從設(shè)備驅(qū)動就變得簡單得多。既然系統(tǒng)加載i2c主設(shè)備驅(qū)動時已經(jīng)注冊了i2c_adapter和i2c_client,那么i2c從設(shè)備主要完成三大任務(wù),· 系統(tǒng)初始化時添加以i2c_board_info為結(jié)構(gòu)的i2c從設(shè)備的信息· 在i2c從設(shè)備驅(qū)動程序里用法i2c_adapter里所提供的算法,即實現(xiàn)i2c通信。· 將i2c從設(shè)備的特有數(shù)據(jù)結(jié)構(gòu)掛

16、在到i2c_client.dev->driver_data下。以重力感應(yīng)裝置為例,static int _init bma220_init(void)return i2c_add_driver(&bma220_driver);static struct i2c_driver bma220_driver = .driver = .owner = this_module,.name = "bma220",.class = i2c_class_hwmon,.probe = bma220_probe,.remove = bma220_remove,;static in

17、t bma220_probe(struct i2c_client *client, const struct i2c_device_id *id)struct bma220_data *data;i2c_check_functionality(client->adapter, i2c_func_i2c)i2c_smbus_read_word_data(client, 0x00); / i2c-core提供的接口,利用i2c_adapter的算法實現(xiàn)i2c通信i2c_set_clientdata(bma220_client, data); / 將設(shè)備的數(shù)據(jù)結(jié)構(gòu)掛到i2c_client.dev->driver_data下misc_register(&b

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論