[Android]Linux kernel 開發基礎 (四)- 新增I2C device 範例
新增platform device 範例 是直接向platform bus註冊一個裝置
其他較常見的裝置要註冊也是大同小異,
makefile 與 kconfig 寫法是相同的,差異在於在哪個Bus下註冊裝置
底下為新增I2C device 範例
1. makefile
根據kconfig的設定,定義要build的檔案
例如範例中的
obj-$(CONFIG_HELLO_DRIVER) += hello.o
這裡定義了:當CONFIG_HELLO_DRIVER為y
就將hello這個檔案build進kernel
2. kconfig
定義了driver 簡易描述以及預設值,與makefile一起,為kernel建立tree管理之用,
在make menuconfig時,可將該driver相關訊息顯示在視窗界面
config HELLO_DRIVER
default y
tristate “Hello Driver”
help
Say Y here if you want to enable hello driver.
這裡定義了一個新的driver HELLO_DRIVER,,
預設值為y
3.如果要向I2C bus註冊新裝置,必須先找到I2C bus initial 的檔案(根據每個BSP設計不同,這個位置與名稱也不同,通常位於kernel/arch/arm/”cpu_name”),透過i2c_register_board_info來註冊新裝置,
(參考自: https://www.kernel.org/doc/htmldocs/device-drivers/API-i2c-register-board-info.html)
Name
Table of Contents
i2c_register_board_info — statically declare I2C devices
Synopsis
int i2c_register_board_info ( |
int busnum, |
struct i2c_board_info const * info, | |
unsigned len) ; |
Arguments
busnum
- identifies the bus to which these devices belong
info
- vector of i2c device descriptors
len
- how many descriptors in the vector; may be zero to reserve the specified bus number.
例如:
i2c_register_board_info(i2c_bus_num, i2c_devs_define, ARRAY_SIZE(i2c_devs_define));
而i2c_devs_define 必須明確指出i2c device address 與i2c_bus_num才能根據設定將資料送到該裝置 ,定義範例如下:
static struct i2c_board_info i2c_devs_define[] __initdata = {
{
I2C_BOARD_INFO(i2c_device_name, i2c_address >> 1),
.irq = IRQ_EINT(IRQ_num),
},
};
4.最後調用i2c_add_driver(&i2c_driver);即可完成I2C device 新增
static const unsigned short normal_i2c[2] = {i2c_address,I2C_CLIENT_END};
static struct i2c_driver i2c_driver = {
.driver = {
.name = i2c_device_name,
.owner = THIS_MODULE,
},
.class = I2C_CLASS_HWMON,
.address_list = normal_i2c,
.id_table = id,
.probe = probe,
.remove = __exit_p(remove),
.suspend = suspend,
.resume = resume,
};
i2c_add_driver(&i2c_driver);
================================
分享與讚美,是我們繼續打拼的原動力.
若文章對您有幫助,望請不吝按讚或分享.
或者對影片有興趣可以訂閱頻道接收通知
================================
YouTube 頻道
FB 粉絲專頁
================================