linux驱动-I2C设备

来源:互联网 发布:大数据分析工程师 编辑:程序博客网 时间:2024/04/27 23:24

  • UART
    • 硬件
  • I2C设备
    • 硬件
    • I2C驱动框架
    • I2C驱动编写
      • 修改硬件信息修改设备树dts文件添加如下内容
      • 修改驱动程序
  • SPI
    • 硬件

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;
0 0