7-i2C总线_mpu6050驱动编程

本文介绍了I2C总线协议的历史、优点、信号标准以及使用原则,并详细讲解了其框架和数据结构。同时,通过MPU6050传感器为例,展示了配置寄存器进行驱动编程的关键步骤,包括采样率分频、低通滤波频率等参数设置。
摘要由CSDN通过智能技术生成
i2c总线协议:
  • 历史:
    • 是飞利浦公司在80年代,为研发音视频模块的通讯而产生。因其具有的诸多优点,而沿用至今。
  • 优点:
    • 1.简单性
    • 2.有效性
    • 3.多主控性。
i2c总线的信号:
  • 1.起始信号:当scl保持高电平期间,sda由高到低跳变,称为起始信号。
  • 2.结束信号:当scl保持高电平期间,sda由低到高跳变,称为结束信号。
  • 3.数据位: 当scl保持高电平期间,sda保持电平稳定有效性,称为有效数据位。
  • 3.应答信号:当scl保持高电平期间,当发送一个字节的数据后,必须要有对方的应答信号。
i2c总线使用几大原则:
  • a – 在SCL=1(高电平)时,SDA千万别忽悠!!!否则,SDA下跳则”判罚”为”起始信号S”,SDA上跳则”判罚”为”停止信号P”.
  • b – 在SCL=0(低电平)时,SDA随便忽悠!!!(可别忽悠过火到SCL跳高)
  • c – 每个字节后应该由对方回送一个应答信号ACK做为对方在线的标志.一般要由双方协议签定.
  • d – SCL必须由主机发送,否则天下大乱
  • e – 首字节是”片选信号”,即7位从机地址加1位方向(读写)控制.从机收到(听到)自己的地址才能发送应答信号(必须应答!!!)表示自己在线.其他地址的从机不允许忽悠!!!(当然群呼可以忽悠但只能听不许说话)
  • f – 读写是站在主机的立场上定义的.”读”是主机接收从机数据,”写”是主机发送数据给从机.
  • g – 7位IIC总线可以挂接127个不同地址的IIC设备,0号”设备”作为群呼地址.10位IIC总线可以挂接更多的10位IIC设备.
i2c总线的框架:
  • 1.总线驱动层:主要数据结构是i2c_adapter和i2c_algorithm,总线驱动层完成总线控制器设备的软件实现,和i2c总线通信的方法等
  • 2.i2c核心层:主要提供api给总线驱动层和设备驱动层,完成总线驱动层和设备驱动层之间的通信和匹配。
  • 3.设备驱动层:主要数据结构是i2c_client和i2c_driver,完成从设备的硬件和软件框架功能的实现。
主要数据结构:
  • 1.i2c_adapter:是描述i2c控制器的数据结构
  • 2.i2c_algorithm: 是描述i2c控制器的通信方法,通俗讲就是i2c控制器的驱动部分
  • 3.i2c_client: 是描述总线上从设备的硬件部分的数据结构
  • 4.i2c_driver: 是描述总线上从设备的驱动部分的数据结构
  • 5.i2c_msg: 是在i2c总线上传输的数据的格式
主要使用函数:
  • 1.i2c_transfer:完成i2c总线上,主从设备之间的信号或者数据的发送和接收。

/*
 * i2c_adapter is the structure used to identify a physical i2c bus along
 * with the access algorithms necessary to access it.
 */
struct i2c_adapter {
    struct module *owner;
    unsigned int class;       /* classes to allow probing for */
    const struct i2c_algorithm *algo; /* the algorithm to access the bus */
    void *algo_data;

    /* data fields that are valid for all devices   */
    struct rt_mutex bus_lock;

    int timeout;            /* in jiffies */
    int retries;
    struct device dev;      /* the adapter device */

    int nr;
    char name[48];
    struct completion dev_released;

    struct mutex userspace_clients_lock;
    struct list_head userspace_clients;

    struct i2c_bus_recovery_info *bus_recovery_info;
};

/**
 * struct i2c_algorithm - represent I2C transfer method
 * @master_xfer: Issue a set of i2c transactions to the given I2C adapter
 *   defined by the msgs array, with num messages available to transfer via
 *   the adapter specified by adap.
 * @smbus_xfer: Issue smbus transactions to the given I2C adapter. If this
 *   is not present, then the bus layer will try and convert the SMBus calls
 *   into I2C transfers instead.
 * @functionality: Return the flags that this algorithm/adapter pair supports
 *   from the I2C_FUNC_* flags.
 *
 * The following structs are for those who like to implement new bus drivers:
 * i2c_algorithm is the interface to a class of hardware solutions which can
 * be addressed using the same bus algorithms - i.e. bit-banging or the PCF8584
 * to name two of the most common.
 *
 * The return codes from the @master_xfer field should indicate the type of
 * error code that occured during the transfer, as documented in the kernel
 * Documentation file Documentation/i2c/fault-codes.
 */

struct i2c_algorithm {
    /* If an adapter algorithm can't do I2C-level access, set master_xfer
       to NULL. 
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值