device tree添加spi设备节点不成功的问题

本文详细解析了在设备树中正确配置SPI设备节点的方法。强调了reg属性的重要性,并提供了具体的配置示例,帮助读者理解如何避免添加设备时的常见错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在设备树中新添加了一个spi设备节点如下:
&spi1 {
gcore_touch@0 {
compatible = “gcore,touchscreen”;
spi-max-frequency = <1000000>;
spi-cpha = <1>;
spi-cpol = <1>;
};
};
看起来没什么问题,但是在系统/sys/bus/spi/devices/目录下却没有看到该设备?

原因:
通过分析代码,如果没有读到reg属性,添加设备错误!

of_register_spi_device(struct spi_master *master, struct device_node *nc)
{
        struct spi_device *spi;
        int rc;
        u32 value;

        /* Alloc an spi_device */
        spi = spi_alloc_device(master);
        if (!spi) {
                dev_err(&master->dev, "spi_device alloc error for %s\n",
                        nc->full_name);
                rc = -ENOMEM;
                goto err_out;
        }

        /* Select device driver */
        rc = of_modalias_node(nc, spi->modalias,
                                sizeof(spi->modalias));
        if (rc < 0) {
                dev_err(&master->dev, "cannot find modalias for %s\n",
                        nc->full_name);
                goto err_out;
        }

        /* Device address */
rc = of_property_read_u32(nc, "reg", &value);
        if (rc) {
                dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
                        nc->full_name, rc);
                goto err_out;
        }
        spi->chip_select = value;

        /* Mode (clock phase/polarity/etc.) */
        if (of_find_property(nc, "spi-cpha", NULL))
                spi->mode |= SPI_CPHA;
        if (of_find_property(nc, "spi-cpol", NULL))
                spi->mode |= SPI_CPOL;
        if (of_find_property(nc, "spi-cs-high", NULL))
                spi->mode |= SPI_CS_HIGH;
        if (of_find_property(nc, "spi-3wire", NULL))
                spi->mode |= SPI_3WIRE;
        if (of_find_property(nc, "spi-lsb-first", NULL))
                spi->mode |= SPI_LSB_FIRST;
...

添加reg属性,/bus/spi/devices/就可以看到了
&spi1 {
#address-cells = <1>;
#size-cells = <0>;
gcore_touch@0 {
compatible = “gcore,touchscreen”;
spi-max-frequency = <1000000>;
reg = <1>;
spi-cpha = <1>;
spi-cpol = <1>;
};
};

注意还需要使用 #address-cells = <1>; #size-cells = <0>;
来重新设定reg属性的长度和大小

设备树(Device Tree,DT)中添加一个SPI(Serial Peripheral Interface)节点是为了描述系统中SPI总线及其连接的设备。以下是创建一个基本SPI节点的基本步骤: 1. **基础结构**: - 创建一个新的`spi`节点,通常命名为` spi@xxxxx`,其中`xxxxx`是一个十六进制地址,代表SPI总线的实际物理位置。 - `status = "okay"`表示该节点是正常的。 ```yaml spi { compatible = "some-compat-spi"; // 根据实际使用的SPI控制器兼容字符串 reg = <0x40>; // SPI总线的起始地址 spi-max-frequency = <5000000>; // 最大频率(Hz) }; ``` 2. **连接至主控芯片**: - 添加`controller`节点,链接到`spi`节点,指定它的ID (`spi-controller-id`) 和控制器兼容字符串(`compatible`)。 ```yaml spi-controller { compatible = "some-compat-spiconfig"; id = <0>; // 主控器编号,从0开始 spi@xxxxx { controller = <&spi-controller>; }; }; ``` 3. **连接外设**: - 对每个连接的SPI设备,如Flash、传感器等,创建`device`节点,引用`spi`节点并提供必要的特性信息,比如`clock-names`(时钟标识)、`cs-gpios`(片选信号)等。 ```yaml spi-flash@xxxxx { compatible = "fixed-partitions"; // 设备兼容字符串 reg = <0x80000000 0x10000>; // 物理内存地址和大小 spi-max-frequency = <5000000>; // 设备的最大频率 spi-slave; // 设备为从设备 clock-names = "spi-clk"; // 用于选择时钟 cs-gpios = <&gpio 17 0>; // 片选GPIO线路 ... }; ``` 4. **编译并验证**: 使用dtc(Device Tree Compiler)工具将这些配置转换为二进制格式,并在内核加载过程中使用它。 完成上述步骤后,设备树就包含了对特定SPI总线及其组件的描述,可供内核和其他需要访问SPI功能的软件使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值