您的位置 首页 培训

3.4.2内核下的I2C驱动结构解析

第一种方法:at24cxx_drvc源码:includelinuxkernelhincludelinuxmodulehincludelinuxplatform_devicehincludel

榜首种办法:

at24cxx_drv.c源码:
#include “linux/kernel.h”
#include “linux/module.h”
#include “linux/platform_device.h”
#include “linux/i2c.h”
#include “linux/err.h”
#include “linux/regmap.h”
#include “linux/slab.h”
static int __devinit at24cxx_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
printk(“%s %s %d\n”, __FILE__, __FUNCTION__, __LINE__);
return 0;
}
static int __devexit at24cxx_remove(struct i2c_client *client)
{
printk(“%s %s %d\n”, __FILE__, __FUNCTION__, __LINE__);
return 0;
}
static const struct i2c_device_id at24cxx_id_table[] = {
{ “at24c08”, 0 },
{}
};
// 1. 分配/设置i2c_driver //
static struct i2c_driver at24cxx_driver = {
.driver = {
.name = “100ask”,
.owner = THIS_MODULE,
},
.probe = at24cxx_probe,
.remove = __devexit_p(at24cxx_remove),
.id_table = at24cxx_id_table,
};
static int at24cxx_drv_init(void)
{
// 2. 注册i2c_driver //
i2c_add_driver(&at24cxx_driver);
return 0;
}
static void at24cxx_drv_exit(void)
{
i2c_del_driver(&at24cxx_driver);
}
module_init(at24cxx_drv_init);
module_exit(at24cxx_drv_exit);
MODULE_LICENSE(“GPL”);
at24cxx_dev.c源码:
#include “linux/kernel.h”
#include “linux/module.h”
#include “linux/platform_device.h”
#include “linux/i2c.h”
#include “linux/err.h”
#include “linux/regmap.h”
#include “linux/slab.h”
static struct i2c_board_info at24cxx_info = {
I2C_BOARD_INFO(“at24c08”, 0x50),
};
static struct i2c_client *at24cxx_client;
static int at24cxx_dev_init(void)
{
struct i2c_adapter *i2c_adap;
i2c_adap = i2c_get_adapter(0);
at24cxx_client = i2c_new_device(i2c_adap, &at24cxx_info);
i2c_put_adapter(i2c_adap);
return 0;
}
static void at24cxx_dev_exit(void)
{
i2c_unregister_device(at24cxx_client);
}
module_init(at24cxx_dev_init);
module_exit(at24cxx_dev_exit);
MODULE_LICENSE(“GPL”);
===============================================================
第二种办法:
at24cxx_drv.c源码:
#include “linux/kernel.h”
#include “linux/module.h”
#include “linux/platform_device.h”
#include “linux/i2c.h”
#include “linux/err.h”
#include “linux/regmap.h”
#include “linux/slab.h”
static int __devinit at24cxx_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
printk(“%s %s %d\n”, __FILE__, __FUNCTION__, __LINE__);
return 0;
}
static int __devexit at24cxx_remove(struct i2c_client *client)
{
printk(“%s %s %d\n”, __FILE__, __FUNCTION__, __LINE__);
return 0;
}
static const struct i2c_device_id at24cxx_id_table[] = {
{ “at24c08”, 0 },
{}
};
// 1. 分配/设置i2c_driver //
static struct i2c_driver at24cxx_driver = {
.driver = {
.name = “100ask”,
.owner = THIS_MODULE,
},
.probe = at24cxx_probe,
.remove = __devexit_p(at24cxx_remove),
.id_table = at24cxx_id_table,
};
static int at24cxx_drv_init(void)
{
// 2. 注册i2c_driver //
i2c_add_driver(&at24cxx_driver);
return 0;
}
static void at24cxx_drv_exit(void)
{
i2c_del_driver(&at24cxx_driver);
}
module_init(at24cxx_drv_init);
module_exit(at24cxx_drv_exit);
MODULE_LICENSE(“GPL”);
at24cxx_dev.c源码:
#include “linux/kernel.h”
#include “linux/module.h”
#include “linux/platform_device.h”
#include “linux/i2c.h”
#include “linux/err.h”
#include “linux/regmap.h”
#include “linux/slab.h”
static struct i2c_client *at24cxx_client;
static const unsigned short addr_list[] = { 0x60, 0x50, I2C_CLIENT_END };
static int at24cxx_dev_init(void)
{
struct i2c_adapter *i2c_adap;
struct i2c_board_info at24cxx_info;
memset(&at24cxx_info, 0, sizeof(struct i2c_board_info));
strlcpy(at24cxx_info.type, “at24c08”, I2C_NAME_SIZE);
i2c_adap = i2c_get_adapter(0);
at24cxx_client = i2c_new_probed_device(i2c_adap, &at24cxx_info, addr_list, NULL);
i2c_put_adapter(i2c_adap);
if (at24cxx_client)
return 0;
else
return -ENODEV;
}
static void at24cxx_dev_exit(void)
{
i2c_unregister_device(at24cxx_client);
}
module_init(at24cxx_dev_init);
module_exit(at24cxx_dev_exit);
MODULE_LICENSE(“GPL”);
=============================================================
第四种办法:
at24cxx_drv.c源码:
#include “linux/kernel.h”
#include “linux/module.h”
#include “linux/platform_device.h”
#include “linux/i2c.h”
#include “linux/err.h”
#include “linux/regmap.h”
#include “linux/slab.h”
static int __devinit at24cxx_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
printk(“%s %s %d\n”, __FILE__, __FUNCTION__, __LINE__);
return 0;
}
static int __devexit at24cxx_remove(struct i2c_client *client)
{
printk(“%s %s %d\n”, __FILE__, __FUNCTION__, __LINE__);
return 0;
}
static const struct i2c_device_id at24cxx_id_table[] = {
{ “at24c08”, 0 },
{}
};
static int at24cxx_detect(struct i2c_client *client,
struct i2c_board_info *info)
{
// 能运转到这儿, 表明该addr的设备是存在的
* 可是有些设备单凭地址无法分辩(A芯片的地址是0x50, B芯片的地址也是0x50)
* 还需要进一步读写I2C设备来分辩是哪款芯片
* detect便是用来进一步分辩这个芯片是哪一款,而且设置info->type
//
printk(“at24cxx_detect : addr = 0x%x\n”, client->addr);
// 进一步判别是哪一款 //
strlcpy(info->type, “at24c08”, I2C_NAME_SIZE);
return 0;
}
static const unsigned short addr_list[] = { 0x60, 0x50, I2C_CLIENT_END };
// 1. 分配/设置i2c_driver //
static struct i2c_driver at24cxx_driver = {
.class = I2C_CLASS_HWMON, // 表明去哪些适配器上找设备 //
.driver = {
.name = “100ask”,
.owner = THIS_MODULE,
},
.probe = at24cxx_probe,
.remove = __devexit_p(at24cxx_remove),
.id_table = at24cxx_id_table,
.detect = at24cxx_detect, // 用这个函数来检测设备的确存在 //
.address_list = addr_list, // 这些设备的地址 //
};
static int at24cxx_drv_init(void)
{
// 2. 注册i2c_driver //
i2c_add_driver(&at24cxx_driver);
return 0;
}
static void at24cxx_drv_exit(void)
{
i2c_del_driver(&at24cxx_driver);
}
module_init(at24cxx_drv_init);
module_exit(at24cxx_drv_exit);
MODULE_LICENSE(“GPL”);
===============================================================
1. 结构
1.1 硬件协议简介
1.2 驱动结构
1.3 bus-drv-dev模型及写程序
a. 设备的4种构建办法(关于以下4种办法主张运用前3种,第四种办法无可奈何情况下运用)
a.1 界说一个i2c_board_info, 里边有:姓名, 设备地址
然后i2c_register_board_info(busnum, …) (把它们放入__i2c_board_list链表)
list_add_tail(&devinfo->list, &__i2c_board_list);
链表何时运用:
i2c_register_adapter > i2c_scan_static_board_info > i2c_new_device
运用约束:必须在 i2c_register_adapter 之前 i2c_register_board_info
所以:不适合咱们动态加载insmod
a.2 直接i2c_new_device, i2c_new_probed_device
a.2.1 i2c_new_device : 以为设备必定存在
a.2.2 i2c_new_probed_device :关于”现已辨认出来的设备”(probed_device),才会创立(“new”)
i2c_new_probed_device
probe(adap, addr_list[i]) // 确认设备是否实在存在 //
info->addr = addr_list[i];
i2c_new_device(adap, info);
a.3 从用户空间创立设备
创立设备
echo at24c08 0x50 > /sys/class/i2c-adapter/i2c-0/new_device
导致i2c_new_device被调用
删去设备
echo 0x50 > /sys/class/i2c-adapter/i2c-0/delete_device
导致i2c_unregister_device
a.4 前面的3种办法都要事前确认适配器(I2C总线,I2C控制器)
假如我事前并不知道这个I2C设备在哪个适配器上,怎样办?去class表明的一切的适配器上查找
有上一些I2C设备的地址是相同,怎样持续分配它是哪一款?用detect函数
static struct i2c_driver at24cxx_driver = {
.class = I2C_CLASS_HWMON, // 表明去哪些适配器上找设备 //
.driver = {
.name = “100ask”,
.owner = THIS_MODULE,
},
.probe = at24cxx_probe,
.remove = __devexit_p(at24cxx_remove),
.id_table = at24cxx_id_table,
.detect = at24cxx_detect, // 用这个函数来检测设备的确存在 //
.address_list = addr_list, // 这些设备的地址 //
};
去”class表明的这一类”I2C适配器,用”detect函数”来确认能否找到”address_list里的设备”,
假如能找到就调用i2c_new_device来注册i2c_client, 这会和i2c_driver的id_table比较,
假如匹配,调用probe
i2c_add_driver
i2c_register_driver
a. at24cxx_driver放入i2c_bus_type的drv链表
而且从dev链表里取出能匹配的i2c_client并调用probe
driver_register
b. 关于每一个适配器,调用__process_new_driver
关于每一个适配器,调用它的函数确认address_list里的设备是否存在
假如存在,再调用detect进一步确认、设置,然后i2c_new_device
// Walk the adapters that are already present //
i2c_for_each_dev(driver, __process_new_driver);
__process_new_driver
i2c_do_add_adapter
// Detect supported devices on that bus, and instantiate them //
i2c_detect(adap, driver);
for (i = 0; address_list[i] != I2C_CLIENT_END; i += 1) {
err = i2c_detect_address(temp_client, driver);
// 判别这个设备是否存在:简略的宣布S信号确认有ACK //
if (!i2c_default_probe(adapter, addr))
return 0;
memset(&info, 0, sizeof(struct i2c_board_info));
info.addr = addr;
// 设置info.type
err = driver->detect(temp_client, &info);
i2c_new_device
b. 驱动的写法
2. 完善设备驱动程序
3. 不自己写驱动直接拜访
Device Drivers
I2C support
<*> I2C device interface
4. 编写”总线(适配器adapter)”驱动
Device Drivers
I2C support
I2C Hardware Bus support
< > S3C2410 I2C Driver
nfs 30000000 192.168.1.123:/work/nfs_root/uImage_noi2cbus; bootm 30000000

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/changshang/peixun/263382.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部