設置 | 登錄 | 註冊

目前共有12篇帖子。

Luckfox RV1103通过I2C3接口读取MPU9250寄存器

1樓 巨大八爪鱼 2025-3-18 15:38
连线:
3V3 3-3V3(OUT)
GND 2-GND
SCL 15-GPIO1_D3
SDA 14-GPIO1_D2
2樓 巨大八爪鱼 2025-3-18 15:39
设备树(rv1103g-luckfox-pico-mini.dts):
/**********I2C**********/
/* I2C3_M1 */
&i2c3 {
    status = "okay";
    clock-frequency = <100000>;
   
    myi2c3dev@68 {
        compatible = "myboard,myi2c3dev";
        reg = <0x68>;
    };
};
3樓 巨大八爪鱼 2025-3-18 15:39
程序代码:
#include <linux/i2c.h>
#include <linux/module.h>

static uint8_t read_reg(struct i2c_client *client, uint8_t addr)
{
    int ret;
    struct i2c_msg msg[2];
    uint8_t value;
   
    msg[0].addr = client->addr;
    msg[0].flags = 0;
    msg[0].buf = &addr;
    msg[0].len = 1;
    msg[1].addr = client->addr;
    msg[1].flags = I2C_M_RD;
    msg[1].buf = &value;
    msg[1].len = 1;
    ret = i2c_transfer(client->adapter, msg, 2);
    if (ret != 2)
    {
        dev_err(&client->dev, "ret=%d\n", ret);
        return 0;
    }
    return value;
}

static int myi2c3dev_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
    dev_err(&client->dev, "probe i2c_addr=0x%02x\n", client->addr);
    dev_err(&client->dev, "WHO_AM_I=0x%02x\n", read_reg(client, 117));
    dev_err(&client->dev, "PWR_MGMT_1=0x%02x\n", read_reg(client, 107));
    return 0;
}

static int myi2c3dev_remove(struct i2c_client *client)
{
    dev_err(&client->dev, "remove\n");
    return 0;
}

static const struct of_device_id myi2c3dev_match_ids[] = {
    {.compatible = "myboard,myi2c3dev"},
    {}
};

static struct i2c_driver myi2c3dev_driver = {
    .driver = {
        .name = "myi2c3dev_driver",
        .owner = THIS_MODULE,
        .of_match_table = of_match_ptr(myi2c3dev_match_ids)
    },
    .probe = myi2c3dev_probe,
    .remove = myi2c3dev_remove
};

module_i2c_driver(myi2c3dev_driver);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Oct1158");

4樓 巨大八爪鱼 2025-3-18 15:40
Makefile:
KDIR := /home/oct1158/Documents/Code/C/kernel_for_modules
CROSS_COMPILE := /home/oct1158/Documents/Code/C/luckfox-pico/tools/linux/toolchain/arm-rockchip830-linux-uclibcgnueabihf/bin/arm-rockchip830-linux-uclibcgnueabihf-

obj-m := test3.o

build:
    $(MAKE) -C $(KDIR) M=$(shell pwd) modules ARCH=arm CROSS_COMPILE=$(CROSS_COMPILE)

clean:
    $(MAKE) -C $(KDIR) M=$(shell pwd) clean

5樓 巨大八爪鱼 2025-3-18 15:42
【程序运行结果(器件插在了板子上)】
insmod test3.ko输出:
[ 2410.409664] myi2c3dev_driver 3-0068: probe i2c_addr=0x68
[ 2410.410223] myi2c3dev_driver 3-0068: WHO_AM_I=0x71
[ 2410.410709] myi2c3dev_driver 3-0068: PWR_MGMT_1=0x01
rmmod test3输出:
[ 2415.722149] myi2c3dev_driver 3-0068: remove

【程序运行结果(器件没有插在板子上)】
insmod test3.ko输出:
[ 2789.838449] myi2c3dev_driver 3-0068: probe i2c_addr=0x68
[ 2790.043751] rk3x-i2c ff460000.i2c: timeout, ipd: 0x00, state: 3
[ 2790.043823] myi2c3dev_driver 3-0068: ret=-110
[ 2790.043849] myi2c3dev_driver 3-0068: WHO_AM_I=0x00
[ 2790.253764] rk3x-i2c ff460000.i2c: timeout, ipd: 0x00, state: 3
[ 2790.253829] myi2c3dev_driver 3-0068: ret=-110
[ 2790.253855] myi2c3dev_driver 3-0068: PWR_MGMT_1=0x00
rmmod test3输出:
[ 2792.388451] myi2c3dev_driver 3-0068: remove
6樓 巨大八爪鱼 2025-3-18 15:47
根据MPU9250手册,当芯片的AD0引脚接的是低电平时,7位I2C器件地址为1101000,也就是0x68。设备树里面要写0x68这个地址才对。
The slave address of the MPU-9250 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-9250s 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).
这和I2C协议实际发送的地址11010000(I2C Write:0xd0)或11010001(I2C Read:0xd1)是不一样的。
7樓 巨大八爪鱼 2025-3-18 15:48

根据MPU9250寄存器手册,芯片上电时,除了107和117寄存器的值不为0外,其他所有寄存器的值都为0。
The reset value is 0x00 for all registers other than the registers below.

• Register 107 (0x01) Power Management 1

• Register 117 (0x71) WHO_AM_I

8樓 巨大八爪鱼 2025-3-18 15:58
【开发板已有的设备树pinctrl代码】
rv1103.dtsi:
&i2c3 {
    pinctrl-names = "default";
    pinctrl-0 = <&i2c3m1_xfer>;
};

rv1103-luckfox-pico-ipc.dtsi:
&i2c3 {
    pinctrl-0 = <&i2c3m1_xfer>;
};

rv1106-pinctrl.dtsi:
&pinctrl {
    i2c3 {
        /omit-if-no-ref/
        i2c3m1_xfer: i2c3m1-xfer {
            rockchip,pins =
                /* i2c3_scl_m1 */
                <1 RK_PD3 3 &pcfg_pull_none_smt>,
                /* i2c3_sda_m1 */
                <1 RK_PD2 3 &pcfg_pull_none_smt>;
        };
    };
};
其中i2c3m1_xfer指的就是GPIO1_D2和GPIO1_D3这两个引脚。
9樓 巨大八爪鱼 2025-3-18 16:04
10樓 巨大八爪鱼 2025-3-18 16:04
巨大八爪鱼MPU9250。

內容轉換:

回覆帖子
內容:
用戶名: 您目前是匿名發表。
驗證碼:
看不清?換一張
©2010-2025 Purasbar Ver3.0 [手機版] [桌面版]
除非另有聲明,本站採用知識共享署名-相同方式共享 3.0 Unported許可協議進行許可。