驱动_I2c驱动框架

本文深入解析I2C总线驱动框架,包括i2c_driver层的应用交互,i2c_core层的总线维护,及i2c_adapter层的硬件交互。详细介绍了结构体如i2c_msg、i2c_driver、i2c_adapter和i2c_client的使用,以及关键函数i2c_transfer和master_xfer的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 I2c驱动框架

 


 

 

 

 

 

 

 

应用


(从设备驱动层)

i2c_driver层

 (自己编写)


 

 (核心层)

         

i2c_core层                                                                                                    i2c_bus_type    

      

 

(i2c_core.c)


 (控制器层)

i2c_adapter层                                      

 (i2c_xxx.c)


硬件 

 

 

 

 

< 结构体> 


 struct i2c_msg {
    __u16 addr;     //从设备地址
    __u16 flags;    // 类型 (读1写0)
    __u16 len;      /* msg length */   数据包个数
    __u8 *buf;      /* pointer to msg data */   数据包
  };

 

  struct i2c_driver {      //描述一个i2c从设备驱动的操作方法
    unsigned int class;
    /* Standard driver model interfaces */
    int (*probe)(struct i2c_client *, const struct i2c_device_id *);
    int (*remove)(struct i2c_client *);

    struct device_driver driver;    //父类
    const struct i2c_device_id *id_table;    // 用于匹配的id_table(列表)
    const unsigned short *address_list;
    struct list_head clients;
  };

  

  struct i2c_adapter {
    const struct i2c_algorithm *algo; /* the algorithm to access the bus */
    |
    int (*master_xfer)(struct i2c_adapter *adap, struct i2c_msg *msgs,int num);
    int nr;   //编号
    struct device dev;   //父类
  }

   

  struct i2c_client {                //代表i2c 从设备的信息
    unsigned short addr;       //7bit 从设备地址
    char name[I2C_NAME_SIZE];   //用于和i2c driver进行匹配
    struct i2c_adapter *adapter;    //指向创建自己的适配器(控制器)
    struct i2c_driver *driver;      // 指针已经匹配成功之后的i2c driver
    struct device dev;       //父类--用于加入总线
  
};

  

<函数>


数据传输接口:i

i2c_master_send(const struct i2c_client * client,const char * buf,int count)
i2c_master_recv(const struct i2c_client * client,char * buf,int count)

   ↓

i2c_adapter   /   i2c_msg →  i2c_transfer(struct i2c_adapter * adap,struct i2c_msg * msgs,int num) //通用

   ↓

i2c_algorithm → master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs, int num); 

 

代码示例:https://www.cnblogs.com/panda-w/p/11118591.html

 

 

 

<代码框架> :


 

 

 

 

 

 

 

 

 

 

 

 

 

 <笔记>


 

1.  i2c driver层:应用交互,知道数据,不知传输

   i2c core 层: 维护了一个i2c总线

   i2c adapter:  硬件交互,不知数据,只知传输

2. 查看i2c设备信息:ls /sys/bus/i2c/devices/0-0050/(name)

 查看i2c驱动名字:/sys/bus/i2c/drivers

3. 每个i2c控制器总线上都可以挂载多个i2c外设

4.每个I2C器件在出厂时都会固化自己的I2C地址的,也有通过硬件引脚选择I2C地址的

5. i2c控制器不用管,引出的引脚与控制器已经连死

6. 宏定义在结构体,说明在结构体中有效

 

 

 

 

3.

 

转载于:https://www.cnblogs.com/panda-w/p/10922747.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值