CAN总线协议解析和驱动开发—【万物智能之开源鸿蒙OpenHarmony系统实战开发系列教程】
显示模组会写了。今天开 CAN。设备树 okay 了不等于有网卡,脚还可能和 I2C 传感器完全重合。



cat /proc/device-tree/can@fe570000/status 读出来是 okay。ls /sys/class/net/can0 没有这一项。dmesg 安静。有人开始调 ArkTS 的 canOpen,报找不到接口,于是改 NAPI、改 PF_CAN 宏。
控制器在 RK3568 里面。设备树 status = "okay" 只是声明。内核要有匹配 compatible 的 platform 驱动编进 Image,probe 成功,才会出现 netdev。Kconfig 写了 CONFIG_CAN_ROCKCHIP=y、源码树没有对应 .c 时,选项会被静默丢掉,.config 里看不见。
compatible = "rockchip,rk3568-can-2.0" 匹配的是 CAN 2.0 那份驱动,不是旧的 CAN 1.0 只认 can-1.0 的那份。配错,同样没有 can0。
引脚 can0m0 = GPIO0_B3 / GPIO0_B4,和 i2c1_xfer 同一组球。开 CAN 必须关 I2C1。温湿度、环境光、手势会一起消失。触摸若在 i2c2,不受影响。两份镜像。
板上 toybox 没有 ip。用 netlink 设 bitrate 500000 再 IFF_UP。应用 socket(PF_CAN, SOCK_RAW, CAN_RAW)。标准帧 ID ≤ 0x7FF;更长的置扩展帧标志。
产品 rk3568_evb。用户态 32 位 armv7 musl。

1. 过程
1. dts:can0 okay,i2c1 disabled,pinctrl can0m0
2. 内核:CONFIG_CAN、CAN_RAW、CAN_DEV,以及匹配 rk3568-can-2.0 的 .c 编进 Image
3. ls /sys/class/net/can0 存在
4. canup can0 500000:netlink 设 bittiming + IFF_UP
5. socket PF_CAN SOCK_RAW bind
6. write 一帧
7. 对端或 USB-CAN 收到。单节点 write>0 不等于总线上有 ACK
第 2 步最容易假装成功:dts okay、netdev 没有。
2. 设备树和共脚
&can0 {
compatible = "rockchip,rk3568-can-2.0";
assigned-clock-rates = <200000000>;
pinctrl-0 = <&can0m0_pins>;
status = "okay";
};
&i2c1 {
status = "disabled";
};
默认镜像反过来。测完 CAN,源码改回 can0 disabled、i2c1 okay,免得下次打包把 CAN 版盖进主包。
can0m0: GPIO0_B3 func2 TX (gpio 11), GPIO0_B4 func2 RX (gpio 12)
i2c1: GPIO0_B3 func1 SCL, GPIO0_B4 func1 SDA
GPIO11 若还当数字传感器 DO 去 export,和 CAN 抢脚。
rm -rf out/kernel/checkpoint
./build.sh --product-name rk3568_evb --ccache
刷 resource + boot_linux。只刷 p5,开机可能仍用 resource 里的旧 dtb。
hdc shell "cat /proc/partitions"
hdc shell "cat /proc/device-tree/can@fe570000/status"
hdc shell "ls /sys/class/net/can0"
hdc shell "dmesg | grep -i can"
status okay 但没有 netdev:驱动没 bind。查 .config 里 CAN 项是不是真的 y,以及 compatible 对应哪一份 .c。
3. canup:没有 ip
ip link set can0 type can bitrate 500000 不存在。自己组 RTNetlink。
/* canup.c — 32 位 musl:canup can0 500000 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#ifndef IFLA_LINKINFO
#define IFLA_LINKINFO 18
#endif
#define INFO_KIND 1
#define INFO_DATA 2
#define CAN_BITTIMING 1
#define NLA_ALIGN4(n) (((n) + 3) & ~3)
struct can_bt {
unsigned int bitrate, sample_point, tq, prop_seg, phase_seg1, phase_seg2, sjw, brp;
};
static struct rtattr *addattr(struct nlmsghdr *nlh, int type, const void *data, int alen)
{
struct rtattr *rta = (struct rtattr *)((char *)nlh + NLA_ALIGN4(nlh->nlmsg_len));
rta->rta_type = type;
rta->rta_len = sizeof(struct rtattr) + alen;
if (alen && data)
memcpy((char *)rta + sizeof(struct rtattr), data, alen);
nlh->nlmsg_len = NLA_ALIGN4(nlh->nlmsg_len) + NLA_ALIGN4(rta->rta_len);
return rta;
}
static int if_index(const char *name)
{
int s = socket(AF_INET, SOCK_DGRAM, 0);
struct ifreq ifr;
int idx;
if (s < 0) return -1;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
idx = (ioctl(s, SIOCGIFINDEX, &ifr) == 0) ? ifr.ifr_ifindex : -1;
close(s);
return idx;
}
static int send_nl(int s, struct nlmsghdr *nlh)
{
struct sockaddr_nl sa;
char buf[512];
nlh->nlmsg_type = RTM_NEWLINK;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
memset(&sa, 0, sizeof(sa));
sa.nl_family = AF_NETLINK;
if (sendto(s, nlh, nlh->nlmsg_len, 0, (struct sockaddr *)&sa, sizeof(sa)) < 0) {
perror("sendto");
return -1;
}
if (recv(s, buf, sizeof(buf), 0) < 0) {
perror("recv");
return -1;
}
{
struct nlmsghdr *r = (struct nlmsghdr *)buf;
if (r->nlmsg_type == NLMSG_ERROR) {
struct nlmsgerr *e = (struct nlmsgerr *)NLMSG_DATA(r);
if (e->error != 0) {
fprintf(stderr, "netlink %d (%s)\n", e->error, strerror(-e->error));
return e->error;
}
}
}
return 0;
}
int main(int argc, char **argv)
{
const char *ifn;
unsigned int bitrate;
int idx, s;
char buf[512];
struct nlmsghdr *nlh;
struct ifinfomsg *ifi;
struct rtattr *linkinfo, *infodata;
struct can_bt bt;
if (argc < 3) {
fprintf(stderr, "用法: canup <can0> <bitrate>\n");
return 1;
}
ifn = argv[1];
bitrate = (unsigned int)strtoul(argv[2], NULL, 10);
idx = if_index(ifn);
if (idx < 0) {
fprintf(stderr, "找不到 %s。dts okay?驱动 probe?\n", ifn);
return 1;
}
s = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
if (s < 0) { perror("netlink"); return 1; }
memset(buf, 0, sizeof(buf));
nlh = (struct nlmsghdr *)buf;
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
ifi = (struct ifinfomsg *)NLMSG_DATA(nlh);
ifi->ifi_family = AF_UNSPEC;
ifi->ifi_index = idx;
linkinfo = addattr(nlh, IFLA_LINKINFO, NULL, 0);
addattr(nlh, INFO_KIND, "can", 4);
infodata = addattr(nlh, INFO_DATA, NULL, 0);
memset(&bt, 0, sizeof(bt));
bt.bitrate = bitrate;
addattr(nlh, CAN_BITTIMING, &bt, sizeof(bt));
infodata->rta_len = (char *)nlh + nlh->nlmsg_len - (char *)infodata;
linkinfo->rta_len = (char *)nlh + nlh->nlmsg_len - (char *)linkinfo;
if (send_nl(s, nlh) != 0) { close(s); return 1; }
printf("%s bitrate=%u set\n", ifn, bitrate);
memset(buf, 0, sizeof(buf));
nlh = (struct nlmsghdr *)buf;
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
ifi = (struct ifinfomsg *)NLMSG_DATA(nlh);
ifi->ifi_family = AF_UNSPEC;
ifi->ifi_index = idx;
ifi->ifi_flags = IFF_UP;
ifi->ifi_change = IFF_UP;
if (send_nl(s, nlh) != 0) { close(s); return 1; }
printf("%s is up\n", ifn);
close(s);
return 0;
}
$CLANG --target=arm-linux-ohos --sysroot=$SYSROOT \
-Wl,--dynamic-linker=/system/lib/ld-musl-arm.so.1 -O2 -o canup canup.c
hdc file send canup /data/canup
hdc shell "chmod +x /data/canup; /data/canup can0 500000"
hdc shell "cat /sys/class/net/can0/operstate"
期望 up。每次开机要再跑。找不到接口:回到第 2 节。musl 和 linux/socket.h 可能撞 sockaddr_storage,编译失败时对 linux 头做 guard。
4. PF_CAN SOCK_RAW
用户态不一定有完整 linux/can.h。自己定义 16 字节帧:
#define PF_CAN 29
#define AF_CAN 29
#define CAN_RAW 1
#define EFF_FLAG 0x80000000U
#define EFF_MASK 0x1FFFFFFFU
#define SFF_MASK 0x7FFU
struct can_frame {
uint32_t can_id;
uint8_t can_dlc;
uint8_t pad[3];
uint8_t data[8];
};
int can_open(const char *name)
{
int s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
struct ifreq ifr;
struct sockaddr_can addr;
if (s < 0) return -1;
memset(&ifr, 0, sizeof(ifr));
strncpy(ifr.ifr_name, name, IFNAMSIZ - 1);
if (ioctl(s, SIOCGIFINDEX, &ifr) != 0) { close(s); return -1; }
memset(&addr, 0, sizeof(addr));
addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) != 0) { close(s); return -1; }
return s;
}
int can_send(int s, uint32_t id, const uint8_t *data, int n)
{
struct can_frame f;
memset(&f, 0, sizeof(f));
if (id > SFF_MASK)
f.can_id = (id & EFF_MASK) | EFF_FLAG;
else
f.can_id = id & SFF_MASK;
if (n > 8) n = 8;
f.can_dlc = (uint8_t)n;
memcpy(f.data, data, n);
return (int)write(s, &f, sizeof(f));
}
int can_recv(int s, int timeout_ms, struct can_frame *out)
{
struct pollfd p;
p.fd = s;
p.events = POLLIN;
p.revents = 0;
if (poll(&p, 1, timeout_ms) <= 0)
return 0;
if (read(s, out, sizeof(*out)) < (ssize_t)sizeof(*out))
return -1;
return 1;
}
板 A:canecho send can0 123 DEADBEEF。板 B:canecho recv can0 5000,应打出 123#DEADBEEF。自己把上面的 can_open / can_send / can_recv 接到 main 即可。id>0x7FF 自动扩展帧。收用 poll 带超时,不要堵 UI。NAPI 把 ID、数据做成 hex 字符串。
let op = board.readText('/sys/class/net/can0/operstate').trim();
if (op.startsWith('ERR'))
this.msg = '没有 can0。CAN 镜像?驱动 probe?';
else
this.msg = 'operstate=' + op + '。down 则 canup can0 500000';
board.canOpen('can0');
board.canSend('123', '11223344');
自检:能 up、能 send。端到端要第二节点或 USB-CAN。收发器 CANH/CANL、共地、两端 120 Ω。短线一对一可先不接电阻。
5. 排障
1. status 是否 okay
2. ls /sys/class/net/can0 —— 没有 = 驱动没 probe
3. .config CAN 项是否真的 y,.c 是否在 Image
4. compatible 是否 rk3568-can-2.0
5. i2c1 是否 disabled
6. canup,operstate=up
7. bind 失败:down 或名字不对
8. send 成功对端没有:第二节点、电阻、极性、波特率
测完切回默认镜像,I2C1 传感器才回来。不要用「所有 I2C 都死了」当判据——i2c2 上的触摸可能还在。
两套模块:CANH 接 CANH,CANL 接 CANL,地共。不要交叉。终端 120 Ω,总线两端各一颗;短线桌面一对一可以先不接。单板无第二节点时,write 返回 16 只说明套接字吃了帧,recv 超时是预期,不要为此改 PF_CAN=29。
刷了 CAN 镜像之后 i2cdetect 上 0x5a / 0x10 / 0x73 全是 --,这是共脚,不是测温芯片坏了。测完换回 i2c1 okay 的镜像。不要在同一份 dts 里 can0 和 i2c1 都 okay:后 probe 的那个随机失败。
socket(PF_CAN) 报 Protocol not supported:CONFIG_CAN / CONFIG_CAN_RAW 没进内核。netdev 在、协议不在,是两件不同的缺。32 位 can_frame 是 16 字节:id 4 + dlc 1 + pad 3 + data 8。写短了内核当残帧丢掉。
验收:/sys/class/net/can0 存在;canup can0 500000 之后 operstate 不是 down;两套对发 123#DEADBEEF 对得上;同一镜像上 0x5a 允许消失,换回默认镜像 can0 消失、0x5a 回来。dts 在 device/board/rk/rk3568_evb/。NAPI 用 canOpen / canSend / canRecv。
6. 内核三处同步
defconfig、out/ 里那份、板级脚本合并段都要有:
CONFIG_CAN=y
CONFIG_CAN_RAW=y
CONFIG_CAN_DEV=y
CONFIG_CANFD_ROCKCHIP=y
rk3568-can-2.0 走 CANFD 那份驱动,旧 CONFIG_CAN_ROCKCHIP 对不上。menuconfig 勾了却没源文件,选项静默消失。.config 里 grep 不到,不要只信 defconfig 文本。
驱动 .c 放到 drivers/net/can/,Kconfig source 挂进 CAN 菜单,Makefile 加上 obj-y。三步缺一,dts 写得再对也没有 can0。
canup / canecho 必须 32 位 musl:
$CC --target=arm-linux-ohos --sysroot=$SYSROOT \
-Wl,--dynamic-linker=/system/lib/ld-musl-arm.so.1 -O2 -o canup canup.c
hdc file send canup /data/local/tmp/canup
hdc shell "chmod 755 /data/local/tmp/canup; /data/local/tmp/canup can0 500000"
64 位主机编出来的 ELF 在板上直接 Exec format error。用户态只有 ld-musl-arm.so.1。
收发器侧 CANH/CANL 是差分,SoC 侧 TX/RX 是 TTL。中间那颗芯片没上电时,write 仍可能成功——控制器把帧推进自己的 FIFO。对端没有波形,先量收发器 VCC,再量差分。
系列第 58 篇 · 芯片:瑞芯微 RK3568 · OpenHarmony 4.1(API 11) · Linux 5.10
更多推荐



所有评论(0)