为LSM6DS3TR-C创建设备树绑定遇‘仅接受I2C节点’问题求助
问题描述
我在Zephyr平台上使用Seeed Xiao Sense开发板驱动LSM6DS3TR-C IMU,发现现有示例绑定仅在特定条件下生效,因此决定从头构建设备树/绑定文件。我从Linux内核档案中找到st,lsm6dsx.yaml绑定文件,但在overlay文件的compatible字段中使用该绑定时,出现“only i2c nodes accepted”错误。
我的overlay文件代码:
&i2c0 { compatible = "nordic,nrf-twim"; /* Cannot be used together with spi0. */ status = "okay"; pinctrl-0 = <&i2c0_default>; pinctrl-1 = <&i2c0_sleep>; pinctrl-names = "default", "sleep"; clock-frequency = <I2C_BITRATE_FAST>; label = "I2C0"; imu@6a{ compatible = "st,lsm6dsx"; reg = <0x6a>; label = "IMU"; vdd-supply=<®ulator_imu>; vddio-supply=<®ulator_imu>; st,pullups; }; };
该绑定文件内容:
# SPDX-License-Identifier: (GPL-2.0 OR BSD-2-Clause) %YAML 1.2 --- $id: http://devicetree.org/schemas/iio/imu/st,lsm6dsx.yaml# $schema: http://devicetree.org/meta-schemas/core.yaml# title: STM 6-axis (acc + gyro) IMU Mems sensors maintainers: - Lorenzo Bianconi <lorenzo@kernel.org> description: Devices have both I2C and SPI interfaces. properties: compatible: oneOf: - enum: - st,lsm6ds3 - st,lsm6ds3h - st,lsm6dsl - st,lsm6dsm - st,ism330dlc - st,lsm6dso - st,asm330lhh - st,lsm6dsox - st,lsm6dsr - st,lsm6ds3tr-c - st,ism330dhcx - st,lsm9ds1-imu - st,lsm6ds0 - st,lsm6dsrx - st,lsm6dst - st,lsm6dsop - st,lsm6dsv - st,lsm6dso16is - items: - enum: - st,asm330lhhx - st,asm330lhhxg1 - const: st,lsm6dsr - items: - const: st,lsm6dstx - const: st,lsm6dst - items: - const: st,lsm6dsv16x - const: st,lsm6dsv - items: - const: st,ism330is - const: st,lsm6dso16is - items: - const: st,asm330lhb - const: st,asm330lhh reg: maxItems: 1 interrupts: minItems: 1 maxItems: 2 description: Supports up to 2 interrupt lines via the INT1 and INT2 pins. vdd-supply: description: if defined provides VDD power to the sensor. vddio-supply: description: if defined provides VDD IO power to the sensor. st,drdy-int-pin: $ref: /schemas/types.yaml#/definitions/uint32 description: | The pin on the package that will be used to signal data ready enum: - 1 - 2 st,pullups: type: boolean description: enable/disable internal i2c controller pullup resistors. st,disable-sensor-hub: type: boolean description: Enable/disable internal i2c controller slave autoprobing at bootstrap. Disable sensor-hub is useful if i2c controller clock/data lines are connected through a pull-up with other chip lines (e.g. SDO/SA0). drive-open-drain: type: boolean description: The interrupt/data ready line will be configured as open drain, which is useful if several sensors share the same interrupt line. wakeup-source: $ref: /schemas/types.yaml#/definitions/flag mount-matrix: description: an optional 3x3 mounting rotation matrix required: - compatible - reg allOf: - $ref: /schemas/iio/iio.yaml# - $ref: /schemas/spi/spi-peripheral-props.yaml# unevaluatedProperties: false examples: - | #include <dt-bindings/interrupt-controller/irq.h> i2c { #address-cells = <1>; #size-cells = <0>; imu@6b { compatible = "st,lsm6dsm"; reg = <0x6b>; interrupt-parent = <&gpio0>; interrupts = <0 IRQ_TYPE_EDGE_RISING>; }; }; ...
我注意到该绑定文件的allOf部分未引用I2C相关schema,但示例中却展示了I2C用法,因此有以下疑问:
- 该绑定是否兼容I2C?
- 若要实现I2C版本的绑定,需具备哪些基础属性?
- 是否有更优的LSM6DS3TR-C实现方案?
解答
1. 该绑定是否兼容I2C?
这个绑定完全兼容I2C,原因如下:
- 绑定文件的
description明确说明设备支持I2C和SPI双接口; - 示例中直接给出了I2C总线挂载设备的写法;
- 未引用I2C schema是因为Linux内核设备树核心逻辑已经覆盖了I2C外设的基础校验(比如
reg字段的要求),不需要额外引用。你遇到的“only i2c nodes accepted”错误,是Zephyr的设备树校验逻辑与Linux内核的差异导致——Zephyr要求I2C节点必须显式关联I2C相关schema,而你移植的Linux绑定未做这个声明。
2. I2C版本绑定需要的基础属性
若要适配Zephyr的校验要求,调整后的绑定需要包含以下内容:
- 强制属性:
compatible:必须包含st,lsm6ds3tr-c(或绑定中枚举的st,lsm6dsx);reg:I2C设备地址,比如你的0x6a;
- 可选但常用属性:
vdd-supply/vddio-supply:传感器电源域配置;st,pullups:启用内部I2C上拉电阻;interrupts/interrupt-parent:配置数据就绪中断引脚;
- Zephyr校验适配补充:
在绑定文件的allOf中添加I2C schema引用:
这样Zephyr的设备树校验器就会识别这是合法的I2C节点。allOf: - $ref: /schemas/iio/iio.yaml# - $ref: /schemas/spi/spi-peripheral-props.yaml# - $ref: /schemas/i2c/i2c-device.yaml# # 新增该行
3. 更优的LSM6DS3TR-C实现方案
方案一:复用Zephyr官方绑定
Zephyr已为LSM6DS系列提供适配好的绑定(路径:dts/bindings/iio/imu/st,lsm6dsx.yaml),其中包含st,lsm6ds3tr-c的兼容值,直接使用即可:
- 将overlay中的
compatible改为"st,lsm6ds3tr-c",而非泛用的"st,lsm6dsx"; - 参考Zephyr官方板级DTS示例(如
boards/arm/seeed_xiao_nrf52840/seeed_xiao_nrf52840.dts)配置I2C节点。
方案二:调整移植的绑定文件
若坚持使用从Linux移植的绑定,除添加I2C schema引用外,还需:
- 确认
compatible枚举中st,lsm6ds3tr-c存在(你的绑定已包含); - 移除Zephyr不支持的属性(如Linux特有的
wakeup-source,若Zephyr不兼容可暂时注释); - 验证板级DTS中是否定义了
regulator_imu,未定义则需补充电源域配置。
方案三:使用Zephyr Sensor API简化开发
直接基于Zephyr的sensor子系统开发,调用sensor_sample_fetch()、sensor_channel_get()等标准API读取数据,无需手动处理寄存器操作。官方驱动已封装好逻辑,稳定性与兼容性更佳。
内容的提问来源于stack exchange,提问作者Harmandeep Dubb
相关产品推荐
相关产品推荐

