linux驱动-I2C设备

UART

全双工,异步, **U**niversual **A**sync **R**eceive **T**ransport.

硬件

  • 1根 发送线Tx;
  • 1根 接收线Rx;

I2C设备

硬件:

  • CLK —– 1根时钟线: 标准的时钟方波
  • DATA —– 1根数据线: 分时复用

i2c是一种同步,半双工的通信总线,使用i2c总线的设备称为I2C设备。

如图:
这里写图片描述

I2C驱动框架

半双工, 同步

如图:
这里写图片描述

I2C驱动编写

1. 修改硬件信息,修改设备树.dts文件,添加如下内容

i2c@138B0000 {   //对应 29 Inter-Integrated Circuit  的I2C 5  -->  0x138B_0000
    samsung,i2c-sda-delay = <100>;
    samsung,i2c-max-bus-freq = <20000>;
    pinctrl-0 = <&i2c5_bus>;   //看电路图 I2C_SDA5  I2C_SCL5  --> GPB2  GPB3  --> 见 exynos4x12-pinctrl.dtsi 的 i2c5_bus
    pinctrl-names = "default";
    status = "okay";

    mpu6050-3-axis@68 {
        compatible = "invensense,mpu6050";  //要与驱动里的名字一致
        reg = <0x68>;   /*i2c slave address  见mpu6050 的芯片手册 PS-MPU-6000A-00v3.4.pdf 的  9.2 I2C Interface
                             The slave address of the MPU-60X0 is b110100X which is 7 bits long.
                             The LSB bit of the 7 bit address is determined by the logic level on pin AD0.
                             This allows two MPU-60X0s to be connected to the same I2C bus.
                             When used in this configuration, the address of the one of the devices should be b1101000 (pin AD0 is logic low)
                              and the address of the other should be b1101001 (pin AD0 is logic high).
                           因电路图中 AD0 结地 故
                           i2c设备的地址是 b1101000 -->0x68
                         */
        interrupt-parent = <&gpx3>;  //见电路 GYRO_INT -> GPX3_3
        interrupts = <3 2>;  //3 对应 gpx3 的3号管脚 2表示中断的触发方式为 下降沿
    };

};

2. 修改驱动程序

驱动框架类似于平台设备.

static int mpu6050_read_byte(struct i2c_client *client, unsigned char reg)
{
    int ret;

    char txbuf[1] = { reg };
    char rxbuf[1];

    struct i2c_msg msg[2] = {   //设定 I2C消息格式
        {client->addr, 0, 1, txbuf},       //0         指定i2c设备里的寄存器地址   txbuf 存放其地址
        {client->addr, I2C_M_RD, 1, rxbuf} //I2C_M_RD  表示读i2c设备里该寄存器的数据
    };

    ret = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg)); //传输I2C消息
    if (ret < 0) {
        printk("ret = %d\n", ret);
        return ret;
    }

    return rxbuf[0];
}

SPI

硬件

  • 1根时钟线;
  • 1根接收线Rx;
  • 1根发送线Tx;
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MichaelJay2015

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值