测距的计时会了。今天换红外对管:只回答前方有没有遮挡,没有距离值,脚却往往在同一组上。

模块上那颗指示灯跟着你的手亮灭。比较器在干活,红外对管也在干活。打开 /dev/ir_avoidance 去 read,空闲是 1,手挡上去还是 1。外设测试页写着「有障碍」,拿开也不变。

灯亮只证明模块本地的比较器翻了。它不证明 OUT 接到了 SoC,更不证明用户态读到的那一字节极性是对的。TCRT5000 这类反射式红外,OUT 低电平才是「有障碍」。设备树里 detect-gpios 写了 GPIO_ACTIVE_LOW 之后,gpiod_get_value 已经把物理电平翻成逻辑值:1 = 有障碍,0 = 无。应用或驱动里再手动取反,就是双重取反,读出来的是裸物理电平。空闲时比较器输出高,物理 1,于是页面永远「有障碍」。

产品 rk3568_evb,板级路径 device/board/rk/rk3568_evb/。OpenHarmony 4.1,内核 aarch64,用户态 32 位 armv7 musl。节点 /dev/ir_avoidance,一次 read 1 字节。脚是 GPIO3_D2,linux 编号 122。和超声波同组 GPIO3,和 /dev/motor_ctl 互斥。


1. 比较器,不是 ADC

红外发射管 ──► 物体反射 ──► 接收管
                              │
                              ▼
                         比较器 + 电位器
                              │  OUT,低有效
                              ▼
                    GPIO3_D2  detect-gpios
                    linux gpio 122
                              │
                              ▼
                    /dev/ir_avoidance  读 1 字节

电位器拧到头:要么永远报警,要么永远不报。阳光直射接收管,误报。先在室内桌面测,手掌停在镜头前方 2~10 cm。

这块底板的这组排针没有把模拟脚引到 SARADC。页面不要留「读电压」当验收。验收就一件事:挡住变 1,拿开变 0。

模块灯焊在比较器旁边。灯亮灭和 OUT 是否进 SoC 是两条路。PIR、烟雾模块也有同样的坑:灯在闪,sysfs 那根脚纹丝不动,因为你读的不是那根线。


2. 逻辑值和物理值,只认一张表

GPIO_ACTIVE_LOW 的含义:物理低电平 = 逻辑有效 = 有障碍。

状态比较器 OUT 物理`gpiod_get_value``/dev/ir_avoidance`sysfs `gpio122/value`
空闲,没挡高,1001
手挡 2~10 cm低,0110

sysfs 导出后读到的是物理层。驱动走 gpiod,读到的是逻辑层。两套数差一个取反。排障时不要把 /dev 的 1 和 sysfs 的 1 当成同一件事。

错误写法:

val = gpiod_get_value_cansleep(priv->detect_gpiod);
val = !val;          /* 错。DTS 已经 ACTIVE_LOW */

或在用户态:

unsigned char b = 0;
read(fd, &b, 1);
int obstacle = !b;   /* 错。驱动已经按逻辑值给你 */

双重取反之后,空闲恒为 1。挡住变成 0,看起来像「极性反了」,有人再取反一次,空闲又回到 1。停手。驱动按上面那张表给逻辑值,应用原样显示。


3. 共脚:节点不出现,先看 status

底板扩展排针就那么几根。红外避障、超声波、电机类模块插的是同一组 GPIO3。

脚linux 编号超声波 / 避障`/dev/motor_ctl` ioctl
GPIO3_C6118ECHO0 低 / 1 高(PC6)
GPIO3_D0120TRIG3 低 / 4 高(PD0)
GPIO3_D2122避障 detect5 低 / 6 高(PD2)
GPIO3_C7119不用7 低 / 8 高(PC7)
GPIO3_D1121不用9 低 / 10 高(PD1)
GPIO3_D3123不用11 低 / 12 高(PD3)

C6、D0、D2 硬冲突。pinctrl 不能把同一只脚同时交给两个驱动。plusevm-motor 和 hcsr04 / ir-avoidance 都 okay 时,后 probe 的那个申请 GPIO 失败,节点干脆不出现。

两套 dtb,两份镜像:

版本`plusevm-motor``hcsr04``ir-avoidance`拿来测什么
超声波版`disabled``okay``okay`超声波 + 本篇避障
电机版`okay``disabled``disabled`直流、步进、移位寄存器、蜂鸣器、继电器

拔掉电机塑料壳,不会把 status 改成 okay。节点在设备树里,不在外壳里。

现场最常见的对话:

  • 「电机拔了,怎么还是没有 /dev/ir_avoidance?」
  • 「刷的是电机版。电机驱动还占着 D2。拔插头没有用。」

反过来也成立:超声波版镜像上没有 /dev/motor_ctl。

编号怎么算:RK3568 GPIO0~GPIO4,每 bank 32 脚,组内 A=0、B=8、C=16、D=24。

GPIO3_D2 = 3×32 + 24 + 2 = 122

对不上先别写应用。


4. 设备树

板级文件:

device/board/rk/rk3568_evb/kernel/rk3568-evb-linux.dts

避障节点(超声波版,status 为 okay):

ir-avoidance {
    compatible = "board,ir-avoidance";
    pinctrl-names = "default";
    pinctrl-0 = <&GPIO3_D2_pin>;
    detect-gpios = <&gpio3 RK_PD2 GPIO_ACTIVE_LOW>; /* GPIO3_D2 = 122 */
    status = "okay"; /* 电机版必须 disabled */
};

compatible 必须和驱动 of_device_id 一字不差。写成随便一个字符串,内核不会 bind,dmesg 里连失败都没有——只是没人认领这个节点。不要把厂商名写进 compatible,驱动按 board,ir-avoidance 匹配。

同一份 dts 里,超声波版电机节点必须是:

plusevm-motor {
    compatible = "plusevm-motor";
    /* gpios: C6, D0, D2, C7, D1, D3 */
    status = "disabled";
};

电机版反过来:plusevm-motor 改 okay,hcsr04 和 ir-avoidance 改 disabled。

改完 dts 不要以为只刷 boot_linux 就够。这块产品真正开机用的 dtb,常常在 resource 分区 的 rk-kernel.dtb。改外设脚,resource 和 boot_linux 都要跟上。

强制重编设备树:

cd <OpenHarmony 源码根>
rm -rf out/kernel/checkpoint
./build.sh --product-name rk3568_evb --ccache

打完用反编译核对,别凭感觉:

dtc -I dtb -O dts out/kernel/OBJ/linux-5.10/arch/arm64/boot/dts/rockchip/*.dtb \
    | grep -A12 'ir-avoidance'

看到 status = "okay" 和 detect-gpios,再去打包、刷。刷之前先看盘:

hdc shell "cat /proc/partitions"
hdc shell "df -h / /vendor"
hdc shell "mount | grep ' on / '"

默认 SD:mmcblk1p4 = resource,mmcblk1p5 = boot_linux。dd 必须 /dev/block/mmcblkXpY,禁止 /dev/mmcblkXpY(会在 tmpfs 建普通文件,md5 假成功)。禁止 /dev/block/by-name/(箭头永远指向 eMMC)。

hdc file send boot_linux.img /data/local/tmp/boot_linux.img
hdc file send resource.img /data/local/tmp/resource.img
hdc shell "dd if=/data/local/tmp/boot_linux.img of=/dev/block/mmcblk1p5 bs=4M conv=fsync"
hdc shell "dd if=/data/local/tmp/resource.img of=/dev/block/mmcblk1p4 bs=4M conv=fsync"
hdc shell "sync"
hdc shell "reboot"

eMMC 启动则把 mmcblk1 换成 mmcblk0。刷前以 df /vendor 为准,不要猜。


5. 内核驱动:misc + gpiod_get("detect")

驱动是平台设备。gpiod_get(..., "detect", ...) 对应 dts 里的 detect-gpios。名字对不上,probe 直接失败。

没有 IRQ,没有 completion。挡一下就翻,松开就回去,用户态自己轮询。不要用 mutex 当信号,这里连「等边沿」都没有。

把下面整份存成 device/board/rk/rk3568_evb/kernel/ir_avoidance.c,由板级内核脚本编进 Image。

// SPDX-License-Identifier: GPL-2.0-only
/* TCRT5000 + 比较器。/dev/ir_avoidance:read 1 字节,1=有障碍,0=无。 */

#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/gpio/consumer.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/of.h>

struct ir_avoidance_data {
    struct gpio_desc *detect_gpiod;
    struct miscdevice mdev;
};

static ssize_t ir_avoidance_read(struct file *filp, char __user *buf,
                                 size_t count, loff_t *ppos)
{
    struct ir_avoidance_data *priv =
        container_of(filp->private_data, struct ir_avoidance_data, mdev);
    u8 val;

    if (count < 1)
        return -EINVAL;

    /* DTS 已声明 GPIO_ACTIVE_LOW。gpiod 返回逻辑值:
     * 1 = 有效 = 有障碍,0 = 无。不要再取反。 */
    val = gpiod_get_value_cansleep(priv->detect_gpiod) ? 1 : 0;
    if (copy_to_user(buf, &val, 1))
        return -EFAULT;
    return 1;
}

static const struct file_operations ir_avoidance_fops = {
    .owner = THIS_MODULE,
    .read  = ir_avoidance_read,
};

static int ir_avoidance_probe(struct platform_device *pdev)
{
    struct ir_avoidance_data *priv;

    priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
    if (!priv)
        return -ENOMEM;

    priv->detect_gpiod = devm_gpiod_get(&pdev->dev, "detect", GPIOD_IN);
    if (IS_ERR(priv->detect_gpiod))
        return dev_err_probe(&pdev->dev, PTR_ERR(priv->detect_gpiod),
                             "failed to get detect-gpios\n");

    priv->mdev.minor  = MISC_DYNAMIC_MINOR;
    priv->mdev.name   = "ir_avoidance";
    priv->mdev.fops   = &ir_avoidance_fops;
    priv->mdev.parent = &pdev->dev;
    platform_set_drvdata(pdev, priv);
    return misc_register(&priv->mdev);
}

static int ir_avoidance_remove(struct platform_device *pdev)
{
    struct ir_avoidance_data *priv = platform_get_drvdata(pdev);
    misc_deregister(&priv->mdev);
    return 0;
}

static const struct of_device_id ir_avoidance_of_match[] = {
    { .compatible = "board,ir-avoidance" },
    { }
};
MODULE_DEVICE_TABLE(of, ir_avoidance_of_match);

static struct platform_driver ir_avoidance_driver = {
    .probe  = ir_avoidance_probe,
    .remove = ir_avoidance_remove,
    .driver = {
        .name = "ir-avoidance",
        .of_match_table = ir_avoidance_of_match,
    },
};
module_platform_driver(ir_avoidance_driver);

MODULE_DESCRIPTION("IR obstacle avoidance (TCRT5000 + comparator)");
MODULE_LICENSE("GPL v2");

file_operations 只有 .read。32 位用户态调 64 位内核,read 走现成兼容路径。不要改成 ioctl 传指针——一改就要自己写 compat_ioctl,电机那条路上已经栽过:缺了它,errno 是 25,ENOTTY。

misc_register 的 minor 是动态的。不要写死 mknod。内核自己会出 /dev/ir_avoidance。


6. 用户态:循环读 /dev/ir_avoidance

把下面整份存成 ir_read.c。这是命令行工具,也是 NAPI 里那一字节读的原型。

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

int main(void)
{
    int fd;
    unsigned char b;
    ssize_t n;
    int last = -1;

    fd = open("/dev/ir_avoidance", O_RDONLY);
    if (fd < 0) {
        fprintf(stderr, "open /dev/ir_avoidance: %s (errno=%d)\n",
                strerror(errno), errno);
        fprintf(stderr,
                "没有节点:多半是电机版镜像,或 dts status 不是 okay。\n"
                "有节点 open 失败:HAP 不是 root,先 chmod 0666。\n");
        return 1;
    }

    for (;;) {
        n = read(fd, &b, 1);
        if (n != 1) {
            fprintf(stderr, "read: n=%zd %s\n", n, strerror(errno));
            close(fd);
            return 1;
        }
        if ((int)b != last) {
            printf("logic=%u  %s\n", b, b ? "OBSTACLE" : "CLEAR");
            fflush(stdout);
            last = (int)b;
        }
        usleep(100 * 1000);
    }
}

交叉编译走 32 位 musl,板上只有 /system/lib/ld-musl-arm.so.1:

OHOS=/path/to/OpenHarmony
CLANG=$OHOS/prebuilts/clang/ohos/linux-x86_64/llvm/bin/clang
SYSROOT=$OHOS/prebuilts/ohos-sdk/linux/11/native/sysroot

$CLANG --target=arm-linux-ohos --sysroot=$SYSROOT -O2 -Wall \
  -Wl,--dynamic-linker=/system/lib/ld-musl-arm.so.1 \
  -o ir_read ir_read.c

file ir_read
# ELF 32-bit LSB executable, ARM, ... interpreter /system/lib/ld-musl-arm.so.1

用 aarch64-linux-gnu-gcc 编出来的,板上 exec 直接失败,提示 No such file or directory——那是解释器路径不存在,不是文件没送过去。

hdc file send ir_read /data/local/tmp/ir_read
hdc shell "chmod 0755 /data/local/tmp/ir_read"
hdc shell "chmod 0666 /dev/ir_avoidance"
hdc shell "/data/local/tmp/ir_read"

手掌在 2~10 cm 挡住,应打印 logic=1 OBSTACLE;拿开,logic=0 CLEAR。模块灯可以一起亮,但以这一字节为准。

NAPI 侧不要再取反。失败返回 -1,不要返回 0——0 是「无障碍」,会把「节点不存在」显示成空闲。

int fd = open("/dev/ir_avoidance", O_RDONLY);
int v = -1;
if (fd >= 0) {
    unsigned char b = 0;
    if (read(fd, &b, 1) == 1)
        v = b ? 1 : 0;
    close(fd);
}
/* v: 1 有障碍,0 无,-1 读失败 */

7. 没有驱动时:sysfs 读 gpio122

驱动没编进内核、节点还没出来,hdc 是 root,可以先用 sysfs 证伪「脚到底动不动」。注意:这里读的是物理值。挡住是物理 0。

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

static int write_str(const char *path, const char *s)
{
    int fd = open(path, O_WRONLY);
    if (fd < 0)
        return -1;
    if (write(fd, s, strlen(s)) < 0) {
        close(fd);
        return -1;
    }
    close(fd);
    return 0;
}

int main(void)
{
    char buf[8];
    int fd, n, last = -1;

    /* 已被 export 时 EBUSY 可忽略 */
    write_str("/sys/class/gpio/export", "122");
    if (write_str("/sys/class/gpio/gpio122/direction", "in") < 0) {
        fprintf(stderr, "direction: %s\n", strerror(errno));
        return 1;
    }

    fd = open("/sys/class/gpio/gpio122/value", O_RDONLY);
    if (fd < 0) {
        fprintf(stderr, "open value: %s\n", strerror(errno));
        return 1;
    }

    for (;;) {
        if (lseek(fd, 0, SEEK_SET) < 0)
            break;
        n = read(fd, buf, sizeof(buf) - 1);
        if (n <= 0)
            break;
        buf[n] = 0;
        int phys = buf[0] - '0';
        if (phys != last) {
            /* ACTIVE_LOW:物理 0 = 挡住 */
            printf("phys=%d  %s\n", phys,
                   phys == 0 ? "BLOCKED(phys0)" : "IDLE(phys1)");
            fflush(stdout);
            last = phys;
        }
        usleep(100 * 1000);
    }
    close(fd);
    return 0;
}

编译、推板与上一节相同,二进制名换成 ir_sysfs。

hdc shell "echo 122 > /sys/class/gpio/export"
hdc shell "echo in > /sys/class/gpio/gpio122/direction"
hdc shell "for i in 1 2 3 4 5 6 7 8 9 10; do cat /sys/class/gpio/gpio122/value; sleep 0.3; done"

空闲应是 1,挡住应是 0。完全不翻:脚没接到 OUT,或电机驱动占着 122,或你 export 的不是 122。

HAP 不是 root,/sys/class/gpio/export 写不进去。sysfs 这条路只给 hdc 诊断。给应用用,走 /dev/ir_avoidance,开机脚本 chmod 0666。临时:

hdc shell "ls -l /dev/ir_avoidance /dev/hcsr04 /dev/motor_ctl"
hdc shell "chmod 0666 /dev/ir_avoidance"

改 vendor 里的 perm 脚本才能活过重启。单装 HAP 不够。hdc 是 root,shell 能打开不要当成应用能打开。


8. 上板顺序

先确认镜像:

hdc shell "ls -l /dev/ir_avoidance /dev/hcsr04 /dev/motor_ctl"
hdc shell "cat /proc/device-tree/ir-avoidance/status"
hdc shell "dmesg | grep -i ir-avoidance"

超声波版该看到 ir_avoidance 和 hcsr04 两个字符设备。motor_ctl 可以不存在。有 motor_ctl、没有 ir_avoidance:电机版,停下来换镜像,别在应用里加日志。

status 是 okay,dmesg 有 failed to get detect-gpios:脚被占,或 pinctrl 名不对。

然后 chmod 0666,跑 ir_read。手挡 2~10 cm,逻辑值 1↔0 翻转。不翻:电位器、供电、挡在接收管视野外、阳光。恒 1:先删应用里的 !,再对拍 sysfs 物理值。

SELinux 开发期常是 permissive。交付若 enforce,裸 /dev/ir_avoidance 会 403,那是策略文件的事,先 permissive 把数跑对。


9. 现象对照

现象先查多半是
没有 `/dev/ir_avoidance``ls /dev/motor_ctl`;dts status电机版镜像,或没编进内核
有节点,open 失败`ls -l`不是 0666,HAP 非 root
空闲恒 1,挡住也是 1应用 / 驱动是否 `!`双重取反,读成物理电平
空闲恒 1,挡住变 0你把物理值当逻辑值用 sysfs 却按 `/dev` 的表显示
灯亮,数字不动sysfs gpio122 是否翻OUT 没接到 122,灯是本地的
数字乱跳阳光、电位器先室内桌面,再拧电位器
`export 122` busy`debugfs` gpio电机驱动占着 D2

矩阵键盘在 GPIO1,和这组不重叠,可以同时开。这和「电机共脚」不是一件事。

路径:dts 在 device/board/rk/rk3568_evb/kernel/rk3568-evb-linux.dts,驱动在同目录 ir_avoidance.c,节点 /dev/ir_avoidance。测完避障要改去测电机:三处 status 对调,删 checkpoint,全量编译,刷 resource + boot_linux。源码默认建议停在超声波版,免得下次打包把电机版盖进去。


系列第 53 篇 · 芯片:瑞芯微 RK3568 · OpenHarmony 4.1(API 11) · Linux 5.10

Logo

作为“人工智能6S店”的官方数字引擎,为AI开发者与企业提供一个覆盖软硬件全栈、一站式门户。

更多推荐