7.4.2. I2C配置

7.4.2.1. 内核配置

7.4.2.1.1. master驱动使能

Device Drivers
        I2C support--->
                <*> I2C support
                <*> I2C device interface
                    I2C Hardware Bus support--->
                        <*> ZX I2C support

7.4.2.1.2. slave驱动使能

Device Drivers
        I2C support--->
                <*> I2C support
                <*> I2C device interface
                    I2C Hardware Bus support--->
                        <*> ZX I2C support
                        <*> ZX I2C as slave mode
                <*> I2C slave support
                <*>     I2C eeprom slave driver

备注

在将I2C作为slave时,需要一个backend程序,作为I2C slave的功能逻辑实现。此处选择内核自带的eeprom,即I2C作为slave时,是当做一个eeprom的功能在使用。也可以根据实际情况自己实现相应的backend代码

7.4.2.1.3. eeprom驱动使能

使用I2C接口与eeprom通信,是一种应用非常广泛的场景。此处介绍如何使能内核的eeprom驱动(需要先使能I2C的master驱动)。

Device Drivers
        Misc devices--->
                EEPROM support--->
                        <*> I2C EEPROMs/RAMs/ROMs from most vendors

7.4.2.2. DTS配置

7.4.2.2.1. I2C公共参数配置

以I2C0为例

i2c0: i2c@19220000 {
        compatible = "zx,aic-i2c-v1.0";
        reg = <0x0 0x19220000 0x0 0x400>;
        interrupts-extended = <&plic0 84 IRQ_TYPE_LEVEL_HIGH>;
        clocks = <&cmu CLK_I2C0>;
        resets = <&rst RESET_I2C0>;
        #address-cells = <1>;
        #size-cells = <0>;
};

7.4.2.2.2. I2C master配置

&i2c0 {
        pinctrl-names = "default";
        pinctrl-0 = <&i2c0_pins_a>;
        status = "okay";
        eeprom@50 {
                compatible = "atmel,24c64";
                reg = <0x50>;
                pagesize = <32>;
        };
};

备注

该配置是将eeprom挂在I2C0总线上的配置,reg属性表示slave设备的地址,pagesize表示eeprom一页有32byte。若在I2C总线上挂其它设备,对DTS进行相应修改即可。

7.4.2.2.3. I2C slave配置

&i2c0 {
        pinctrl-names = "default";
        pinctrl-0 = <&i2c0_pins_a>;
        status = "okay";
        slave@54 {
                compatible = "slave-24c02";
                reg = <0x40000054>;
        };
};

备注

内核的I2C子系统框架会通过查看reg属性的bit30,判断I2C是工作在主机模式还是从机模式。若bit30为1,则为从机模式。上述配置表示I2C工作在从机模式,设备地址为0x54。

若需要设置从机为10bit寻址,则需要设置reg属性的bit31为1,如下图所示,配置从机地址为0x139。

&i2c0 {
        pinctrl-names = "default";
        pinctrl-0 = <&i2c0_pins_a>;
        status = "okay";
        slave@139 {
                compatible = "slave-24c02";
                reg = <0xC0000139>;
        };
};