直流电机桥式控制驱动开发—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
避障会翻电平了。今天让直流电机转起来。桥式驱动要方向加使能,速度靠方波,只拉高电平不会转。


open("/dev/motor_ctl") 返回正的 fd。ioctl(fd, 10)、ioctl(fd, 6)、甚至 ioctl(fd, 8) 全是 0。dmesg 没有报错。按钮点下去,状态栏写「已发送」。轴一声不响。
H 桥要两件事同时成立:使能脚一直有效,方向脚上有持续方波。静态电平几乎不转。只翻 PD1 / PD2、不拉 PC7,ioctl 照样成功,桥不工作。把方波写在界面线程里死循环,页面冻住,看起来像应用崩了。
产品 rk3568_evb,板级 device/board/rk/rk3568_evb/。OpenHarmony 4.1,内核 aarch64,用户态 32 位 armv7 musl。节点 /dev/motor_ctl。ioctl 的 cmd 是裸整数,不是 _IOW 宏。32 位进程走 compat 路径,驱动必须提供 compat_ioctl,否则 errno 25,ENOTTY。脚和超声波、红外避障同一组 GPIO3,要用电机版镜像。节点 chmod 0666,否则 HAP 打不开。

1. H 桥要方波,不要一根常高
直流模块常见是 H 桥加减速齿轮。SoC 侧看到的是几根 GPIO,不是硬件 PWM 控制器。软件用 ioctl 把脚翻来翻去,等效 100 Hz 左右的方波。占空比决定平均电压,也就是转速。
mio(8) ──► GPIO3_C7 EN ──► H 桥使能(常高才工作)
mio(10/9) ──► GPIO3_D1 IN1 ─┐
├─► 方向 + 软件 PWM
mio(6/5) ──► GPIO3_D2 IN2 ─┘
│
▼
直流电机
PC7(cmd 8 高 / 7 低)是使能。PWM 循环开头必须 mio(8) 拉高,停止时 mio(7) 拉低。只翻方向脚不拉 EN,ioctl 返回 0,电机仍不动。这是「ioctl 全返回 0 却不转」的第一号原因。
方向二选一,不要两路同时出方波:
| 方向 | 方波脚 | linux 编号 | 高电平 cmd | 低电平 cmd |
|---|---|---|---|---|
| 0 | GPIO3_D1(PD1) | 121 | 10 | 9 |
| 1 | GPIO3_D2(PD2) | 122 | 6 | 5 |
占空比 1~10 档。周期约 10 ms(100 Hz):10 拍,每拍 1 ms,高电平拍数 = 速度。档 1 几乎只是蠕动,档 10 接近满转。档 0 就是停,方向脚全低。
不要把板载风扇那个硬件 PWM 脚拿来当电机。风扇走 /sys/class/pwm/pwmchipN/,dts 里是 pwm 节点,跟 /dev/motor_ctl 无关。步进那套相序也不是这篇的 PWM。直流要的是占空比,不是一次拉一相。
2. 没有 /dev/motor_ctl 就停
底板扩展排针就那么几根。直流、步进、超声波、红外避障插的是同一组 GPIO3。
| 脚 | linux 编号 | `/dev/motor_ctl` ioctl | 超声波 / 避障 |
|---|---|---|---|
| GPIO3_C6 | 118 | 0 低 / 1 高(PC6) | ECHO |
| GPIO3_D0 | 120 | 3 低 / 4 高(PD0) | TRIG |
| GPIO3_D2 | 122 | 5 低 / 6 高(PD2) | 避障 detect |
| GPIO3_C7 | 119 | 7 低 / 8 高(PC7) | (不用) |
| GPIO3_D1 | 121 | 9 低 / 10 高(PD1) | (不用) |
| GPIO3_D3 | 123 | 11 低 / 12 高(PD3) | (不用) |
没有 cmd 2。编号怎么算:RK3568 GPIO0~GPIO4,每 bank 32 脚,组内 A=0、B=8、C=16、D=24。
GPIO3_C7 = 3×32 + 16 + 7 = 119
GPIO3_D1 = 3×32 + 24 + 1 = 121
GPIO3_D2 = 3×32 + 24 + 2 = 122
C6、D0、D2 硬冲突。pinctrl 不能把同一只脚同时交给两个驱动。plusevm-motor 和 hcsr04 / ir-avoidance 都 okay 时,后 probe 的那个申请 GPIO 失败,节点干脆不出现。
两套 dtb,两份镜像:
| 版本 | `plusevm-motor` | `hcsr04` | `ir-avoidance` |
|---|---|---|---|
| 电机版 | `okay` | `disabled` | `disabled` |
| 超声波版 | `disabled` | `okay` | `okay` |
测本篇,确认镜像是电机版。
hdc shell "ls -l /dev/motor_ctl /dev/hcsr04 /dev/ir_avoidance"
该看到 crw 的 /dev/motor_ctl。超声波两个节点可以不存在。有 hcsr04、没有 motor_ctl:超声波版,换镜像。拔掉超声波塑料壳不会让电机节点出现。节点在设备树里,不在外壳里。
设备树在 device/board/rk/rk3568_evb/kernel/rk3568-evb-linux.dts:
plusevm-motor {
compatible = "plusevm-motor";
pinctrl-names = "default";
pinctrl-0 = <&GPIO3_C6_pin &GPIO3_D0_pin &GPIO3_D2_pin
&GPIO3_C7_pin &GPIO3_D1_pin &GPIO3_D3_pin>;
gpios = <&gpio3 RK_PC6 GPIO_ACTIVE_LOW>, /* 118 */
<&gpio3 RK_PD0 GPIO_ACTIVE_LOW>, /* 120 */
<&gpio3 RK_PD2 GPIO_ACTIVE_LOW>, /* 122 */
<&gpio3 RK_PC7 GPIO_ACTIVE_LOW>, /* 119 */
<&gpio3 RK_PD1 GPIO_ACTIVE_LOW>, /* 121 */
<&gpio3 RK_PD3 GPIO_ACTIVE_LOW>; /* 123 */
status = "okay"; /* 超声波版必须 disabled */
};
超声波版同一份文件里,hcsr04 和 ir-avoidance 改 okay,上面这个节点改 disabled。不要两套都 okay。
改 dts 之后刷 boot_linux 且刷 resource 的 dtb。U-Boot 交给内核的常常是 resource 分区里的 rk-kernel.dtb,只刷 p5,/proc/device-tree 可以完全不动。
刷之前先看盘,不要猜:
hdc shell "cat /proc/partitions"
hdc shell "df -h /vendor"
hdc shell "mount | grep ' on / '"
默认 SD 是 mmcblk1。df /vendor 若在 mmcblk0p8,下面命令里的 mmcblk1 换成 mmcblk0。dd 走 /dev/block/。禁止 /dev/mmcblk1p5(会在 tmpfs 建普通文件,md5 假成功)。禁止 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"
强制重编设备树:
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 -A20 'plusevm-motor'
status 必须是 okay,hcsr04 / ir-avoidance 必须是 disabled。
3. ioctl 是裸整数;compat_ioctl 不能少
驱动 device/board/rk/rk3568_evb/kernel/motor_ctl.c 的 cmd 不是 _IO('M', n)。用户态直接:
ioctl(fd, 8); /* PC7 高,使能 */
ioctl(fd, 7); /* PC7 低,关掉 */
内核 unlocked_ioctl 收到的就是那个整数。OpenHarmony 用户态是 32 位,内核是 64 位。32 位进程的 ioctl 走 compat_ioctl。只挂了 unlocked_ioctl、没挂 compat_ioctl 时,compat 层找不到处理函数,返回 -ENOIOCTLCMD,用户态看到 ioctl() = -1 errno=25 (Not a tty)。所有点转操作全失败,页面还可能把返回值吞掉,显示「已发送」。
read / write 没有这个问题。超声波、避障走 read,躲过了这一枪。电机必须补:
static const struct file_operations motor_ops = {
.owner = THIS_MODULE,
.unlocked_ioctl = motor_ioctl,
.compat_ioctl = motor_ioctl, /* cmd 是裸整数,无指针,直接复用 */
};
cmd 不带用户指针,不需要 compat_ptr。加完重编内核,刷 boot_linux(驱动在 Image 里)。只刷 resource 不够。
验证:
hdc shell "ls -l /dev/motor_ctl"
hdc shell "dmesg | grep motor_ctl"
应有 motor_ctl initialized。然后用下面的探测程序发 cmd 8,看返回值是 0 还是 -1。
用 64 位小程序在板上测 ioctl,会误判「驱动是好的」——64 位走 unlocked_ioctl,根本不经过 compat。用户态工具必须是 32 位 musl,动态链接器 /system/lib/ld-musl-arm.so.1。
4. 脚上的电平,和节点权限
dts 里六根脚常常标 GPIO_ACTIVE_LOW。gpiod 那套 API 会按标志把「逻辑 1」翻成物理低。motor_ctl 实际用的是老接口 of_get_gpio + gpio_set_value,不读这个标志。于是:
ioctl(cmd 8 / 「高」) → gpio_set_value(gpio, 1) → 焊盘物理高
ioctl(cmd 7 / 「低」) → gpio_set_value(gpio, 0) → 焊盘物理低
有人按 ACTIVE_LOW「修正」成 gpiod_set_value(1),使能脚变成物理低,H 桥彻底闭嘴,ioctl 仍然返回 0。
不要假设。先 ioctl 翻转,万用表或一颗 LED 看脚。跑 PWM 之前,先发 8 和 7,表笔接 PC7。cmd 8 后焊盘变高,cmd 7 后变低,才按「8=使能」往下写。反过来,把 7/8 对调。以脚上的电平为准,不以注释为准。
HAP 不是 root。节点 root:root 0600 时应用 open 得到 -1,errno 13。临时:
hdc shell "chmod 0666 /dev/motor_ctl"
hdc shell "ls -l /dev/motor_ctl"
vendor 开机脚本里放同一行才能活过重启。改脚本要重打 vendor.img 刷 p8。hdc 是 root,shell 能打开不要当成 HAP 能打开。
5. 完整可编译 C:mio、dcPwmLoop、run / stop
把下面整份存成 dc_pwm.c。调用者只改方向、速度、停止;方波在 pthread 里跑,不能堵界面。进程被 Ctrl+C 或 SIGTERM 打死之前,必须把使能脚拉低,不要让脚悬在使能上,电机继续转、H 桥继续发热。
循环开头 mio(8) 拉高 PC7。停止 mio(7)。dir=0 走 PD1(cmd 10/9),dir=1 走 PD2(cmd 6/5)。10 拍 × 1 ms = 100 Hz,speed 1~10 是高电平拍数。
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <pthread.h>
#include <sys/ioctl.h>
static int g_fd = -1;
static volatile int g_run;
static volatile int g_speed; /* 1..10 */
static volatile int g_dir; /* 0=PD1 cmd10/9, 1=PD2 cmd6/5 */
static pthread_t g_th;
static int mio(int cmd)
{
int r;
errno = 0;
r = ioctl(g_fd, cmd);
if (r != 0) {
fprintf(stderr, "ioctl(%d)=%d errno=%d (%s)\n",
cmd, r, errno, strerror(errno));
return -1;
}
return 0;
}
static void all_off(void)
{
if (g_fd < 0)
return;
mio(7); /* EN 低,PC7 */
mio(9); /* PD1 低 */
mio(5); /* PD2 低 */
}
static void *dcPwmLoop(void *arg)
{
int cnt = 0;
int hi, lo;
(void)arg;
if (mio(8) < 0) { /* 循环开头拉高 PC7 使能 */
g_run = 0;
return NULL;
}
while (g_run) {
int spd = g_speed;
if (spd < 0)
spd = 0;
if (spd > 10)
spd = 10;
if (g_dir) {
hi = 6; /* PD2 高 */
lo = 5; /* PD2 低 */
} else {
hi = 10; /* PD1 高 */
lo = 9; /* PD1 低 */
}
if (mio(cnt < spd ? hi : lo) < 0)
break;
cnt++;
if (cnt >= 10)
cnt = 0;
usleep(1000); /* 1 ms × 10 拍 = 10 ms,约 100 Hz */
}
all_off();
return NULL;
}
static int run(int dir, int speed)
{
if (g_run) {
g_run = 0;
pthread_join(g_th, NULL);
}
g_dir = dir ? 1 : 0;
g_speed = speed;
g_run = 1;
if (pthread_create(&g_th, NULL, dcPwmLoop, NULL) != 0) {
g_run = 0;
perror("pthread_create");
return -1;
}
return 0;
}
static void stop(void)
{
if (!g_run)
return;
g_run = 0;
pthread_join(g_th, NULL);
}
static void on_signal(int sig)
{
(void)sig;
g_run = 0;
}
static void usage(const char *argv0)
{
fprintf(stderr,
"usage:\n"
" %s fwd <1-10> PD1 方向,持续转\n"
" %s rev <1-10> PD2 方向,持续转\n"
" %s stop\n"
"Ctrl+C 会停转并拉低使能。\n",
argv0, argv0, argv0);
}
int main(int argc, char **argv)
{
struct sigaction sa;
int speed = 5;
int dir = 0;
if (argc < 2) {
usage(argv[0]);
return 1;
}
memset(&sa, 0, sizeof(sa));
sa.sa_handler = on_signal;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
g_fd = open("/dev/motor_ctl", O_RDWR | O_NOCTTY);
if (g_fd < 0) {
fprintf(stderr, "open /dev/motor_ctl: %s (errno=%d)\n",
strerror(errno), errno);
fprintf(stderr,
"没有节点:超声波版镜像,或 dts 里 plusevm-motor 不是 okay。\n"
"open 失败且 errno=13:chmod 0666 /dev/motor_ctl。\n");
return 1;
}
if (strcmp(argv[1], "stop") == 0) {
all_off();
close(g_fd);
return 0;
}
if (argc < 3 || (strcmp(argv[1], "fwd") && strcmp(argv[1], "rev"))) {
usage(argv[0]);
close(g_fd);
return 1;
}
dir = (strcmp(argv[1], "rev") == 0);
speed = atoi(argv[2]);
if (speed < 1)
speed = 1;
if (speed > 10)
speed = 10;
if (run(dir, speed) < 0) {
close(g_fd);
return 1;
}
printf("run dir=%s speed=%d Ctrl+C to stop\n",
dir ? "PD2" : "PD1", speed);
fflush(stdout);
while (g_run)
sleep(1);
stop();
all_off();
close(g_fd);
return 0;
}
信号处理函数只清 g_run。真正 mio(7) 在 dcPwmLoop 退出路径和 main 末尾的 all_off。不要在信号里调 ioctl。
NAPI 里用 std::thread 也行,逻辑相同:run(dir, speed) 起线程,stop() join 再关使能。界面主线程禁止跑 dcPwmLoop。页面离开必须 stop()。
6. 先证 ioctl,再证 PWM
有人一上来就跑 PWM,失败了分不清是 compat 还是使能。先发单次。把下面另存为 motor_probe.c:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
int main(void)
{
int fd = open("/dev/motor_ctl", O_RDWR | O_NOCTTY);
int cmds[] = { 7, 8, 5, 6, 9, 10 };
int i, r;
printf("open=%d", fd);
if (fd < 0) {
printf(" errno=%d (%s)\n", errno, strerror(errno));
fprintf(stderr,
"没有 /dev/motor_ctl:先查是不是超声波版镜像。\n");
return 1;
}
printf(" OK\n");
for (i = 0; i < 6; i++) {
errno = 0;
r = ioctl(fd, cmds[i]);
printf("ioctl(%d)=%d errno=%d (%s)\n",
cmds[i], r, errno, r == 0 ? "OK" : strerror(errno));
}
ioctl(fd, 7);
close(fd);
return 0;
}
全部 =0:compat 不是问题,根因在 PWM / EN。全部 errno=25:缺 compat_ioctl,回去改驱动刷 boot_linux。open 失败 errno=2:节点不存在,超声波版。errno=13:权限,chmod 0666。
stdout 有时被 hdc 吞。更稳:程序写 /data/local/tmp/motor_probe.log,再 hdc shell cat 那份日志。
7. 编译、推板
目标仍是 32 位 musl,链 pthread:
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 \
-pthread -o dc_pwm dc_pwm.c
$CLANG --target=arm-linux-ohos --sysroot=$SYSROOT -O2 -Wall \
-Wl,--dynamic-linker=/system/lib/ld-musl-arm.so.1 \
-o motor_probe motor_probe.c
file dc_pwm motor_probe
# 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 dc_pwm /data/local/tmp/dc_pwm
hdc file send motor_probe /data/local/tmp/motor_probe
hdc shell "chmod 0755 /data/local/tmp/dc_pwm /data/local/tmp/motor_probe"
hdc shell "chmod 0666 /dev/motor_ctl"
hdc shell "/data/local/tmp/motor_probe"
hdc shell "/data/local/tmp/dc_pwm fwd 5"
另开一个 hdc 停转:
hdc shell "/data/local/tmp/dc_pwm stop"
或在同一个终端 Ctrl+C。SIGINT 清 g_run,线程退出时拉低 EN。
8. 验收
持续转、调速可感、停下来。具体:
fwd 3转,慢。fwd 9明显加快。换rev反转。stop或 Ctrl+C 之后轴停,PC7 低。断电前摸 H 桥,不应继续发烫。- 进程被
kill -9打死,脚会停在最后一次 ioctl。所以正式应用要在退出钩子里stop()。命令行用信号;不要依赖内核帮你把 GPIO 拉低——motor_ctl的release是空的。
步进可以阻塞几十到几百步;直流必须后台线程,因为要一直转。不要把两套逻辑混在一个函数里。不要和板载风扇 PWM 混。
供电:电机启动电流大,USB 口分出来的 5 V 会掉。不要私自把超声波和电机两套 dts 都改成 okay。
SELinux 开发期常是 permissive。交付若 enforce,裸 /dev/motor_ctl 会 403,先 permissive 把转跑起来。
9. 现象对照
| 现象 | 先查 | 多半是 |
|---|---|---|
| 没有 `/dev/motor_ctl` | `ls /dev/hcsr04` | 超声波版镜像 |
| ioctl 全 `errno=25` | `compat_ioctl` | 32 位用户态,驱动没挂兼容入口 |
| ioctl 全 0,电机不动 | 表笔看 PC7 | 没发 cmd 8,只翻了方向脚 |
| PC7 高了仍不动 | 方向脚是否在方波 | 静态电平,没有 `dcPwmLoop` |
| 点一下转一下就停 | 方波写在按钮回调里 | 回调结束 GPIO 停在某一电平 |
| 页面冻住 | PWM 在界面线程 | 改到 pthread |
| 退出后继续转 | `release` 是空的 | 退出路径没 `mio(7)` |
| HAP open 失败,hdc 成功 | `ls -l /dev/motor_ctl` | 不是 0666 |
| 改了 dts 板上没变 | 是否刷了 p4 | resource 里的 dtb 才是开机那份 |
| 64 位探测程序 ioctl 成功、HAP 失败 | ELF 位数 | HAP 走 compat,探测程序没走 |
路径:dts 在 device/board/rk/rk3568_evb/kernel/rk3568-evb-linux.dts;驱动同目录 motor_ctl.c;节点 /dev/motor_ctl。测完电机要改去测超声波:三处 status 对调,刷 resource + boot_linux。源码打完建议改回超声波版默认,避免下次整包把电机版盖进去。
系列第 54 篇 · 芯片:瑞芯微 RK3568 · OpenHarmony 4.1(API 11) · Linux 5.10
更多推荐


所有评论(0)