• 正文
  • 相关推荐
申请入驻 产业图谱

飞凌嵌入式ElfBoard ELF 1板卡-I2C设备驱动之I2C驱动构建流程

04/15 10:20
341
加入交流群
扫码加入
获取工程师必备礼包
参与热点资讯讨论

i2c_driver结构体原型如下:

struct i2c_driver {

&nbspunsigned int class;

&nbspint (*attach_adapter)(struct i2c_adapter *);

&nbspint (*detach_adapter)(struct i2c_adapter *);

&nbspint (*probe)(struct i2c_client *, const struct i2c_device_id *);

&nbspint (*remove)(struct i2c_client *);

&nbspvoid (*shutdown)(struct i2c_client *);

&nbspint (*suspend)(struct i2c_client *, pm_message_t mesg);

&nbspint (*resume)(struct i2c_client *);

&nbspvoid (*alert)(struct i2c_client *, unsigned int data);

&nbspint (*command)(struct i2c_client *client, unsigned int cmd, void*arg);

&nbspstruct device_driver driver;

&nbspconst struct i2c_device_id *id_table;

&nbspint (*detect)(struct i2c_client *, struct i2c_board_info *);

&nbspconst unsigned short *address_list;

&nbspstruct list_head clients;

};

主要的成员描述如下:

attach_adapter:依附i2c_adapter函数指针

detach_adapter:脱离i2c_adapter函数指针

driver:struct device_driver类型的成员,指定驱动程序的名称和所属的总线类型。

probe:指向设备探测函数的回调函数指针,在设备匹配时调用。

remove:指向设备移除函数的回调函数指针,在设备被卸载时调用。

id_table:指向一个数组的指针,用于匹配驱动程序和I2C设备。

detect:指向设备检测函数的回调函数指针,用于检测特定类型的设备是否存在。

当I2C设备和驱动匹配成功以后probe函数就会执行,和platform驱动一样,简单注册示例如下:

#include <linux/i2c.h>

static int my_i2c_probe(struct i2c_client *client, const struct i2c_device_id *id)

{

// I2C设备探测函数,处理设备初始化和配置

// ...

return 0;

}

static int my_i2c_remove(struct i2c_client *client)

{

// I2C设备移除函数,处理设备的清理操作

// ...

return 0;

}

//传统匹配方式 ID 列表

static const struct i2c_device_id my_i2c_id[] = {

{ "my_i2c_device", 0 },

{ }

};

//设备树匹配列表

static const struct of_device_id my_i2c_of_match[] = {

&nbsp&nbsp{ .compatible = "my_i2c_device" },

&nbsp&nbsp{ /* Sentinel */ }

};

MODULE_DEVICE_TABLE(i2c, my_i2c_id);

static struct i2c_driver my_i2c_driver = {

.driver = {

.name = "my_i2c_driver",

.owner = THIS_MODULE,

.of_match_table = my_i2c_of_match

},

.probe = my_i2c_probe,

.remove = my_i2c_remove,

.id_table = my_i2c_id,

};

// 驱动程序初始化函数

static int __init my_i2c_driver_init(void)

{

return i2c_add_driver(&my_i2c_driver);

}

// 驱动程序卸载函数

static void __exit my_i2c_driver_exit(void)

{

i2c_del_driver(&my_i2c_driver);

}

module_init(my_i2c_driver_init);

module_exit(my_i2c_driver_exit);

MODULE_AUTHOR("Your Name");

MODULE_DESCRIPTION("Sample I2C Driver");

MODULE_LICENSE("GPL");

点赞
收藏
评论
分享
加入交流群
举报

相关推荐

登录即可解锁
  • 海量技术文章
  • 设计资源下载
  • 产业链客户资源
  • 写文章/发需求
立即登录