Skip to content

bsp: add CAN-FD and loopback support - #11579

Closed
ghost wants to merge 1 commit into
masterfrom
unknown repository
Closed

ghost wants to merge 1 commit into
masterfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Jul 6, 2026 •

Copy link
Copy Markdown

拉取/合并请求描述:(PR description)

add CAN-FD and loopback support

- Extend flexcan driver with CAN-FD TX/RX and FD frame parsing
- Add loopback and loopback+listen-only mode handling
- Fix mailbox layout, clock init, and safe controller deinit
- Add debug assert handlers for frdm-mcxa366 board

为什么提交这份PR (why to submit this PR)

MCXA BSP 的 FlexCAN 驱动原先仅支持经典 CAN 的基本收发,缺少 CAN-FD、回环(loopback)和只听(listen-only)模式支持,无法满足 CAN-FD 应用及驱动自测需求。同时 frdm-mcxa366 在 Debug 构建下缺少标准 assert 处理函数,断言失败时无法输出有效调试信息。

你的解决方案是什么 (what is your solution)

  1. 增强 drv_can.c FlexCAN 驱动:

    • 增加 CAN-FD 支持:FD 帧收发、DLC 编解码,以及 RT_CAN_CMD_SET_CANFD / RT_CAN_CMD_SET_BAUD_FD 控制命令
    • 增加回环模式(RT_CAN_MODE_LOOPBACK)和回环+只听模式(RT_CAN_MODE_LOOPBACKANLISTEN),回环模式下通过轮询完成 TX/RX 自测
    • 增加只听模式(RT_CAN_MODE_LISTEN)支持
    • 重构驱动:抽取帧填充/解析、时钟使能、安全反初始化、RX 启动等辅助函数
    • 修正邮箱布局(TX_MB_IDX=0,RX_MB_IDX=1),完善时钟初始化与控制器复位流程
  2. 在 frdm-mcxa366/board/board.c 中增加 Debug 断言处理函数(__assert_func / __aeabi_assert),断言失败时通过 rt_kprintf 输出信息并触发断点,便于调试。

请提供验证的bsp和config (provide the config and bsp)

  • BSP:

    • bsp/nxp/mcx/mcxa/frdm-mcxa366
  • .config:

    CONFIG_BSP_USING_CAN=y
    CONFIG_BSP_USING_CAN0=y
    CONFIG_RT_CAN_USING_CANFD=y        # 验证 CAN-FD 时启用
    CONFIG_RT_CAN_USING_HDR=y          # 可选,验证硬件过滤器时启用
    

    验证方式:

    • 经典 CAN:在 can 设备上设置 loopback 模式,执行 can_sample 或 MSH 命令收发,确认 TX/RX 正常
    • CAN-FD:启用 CONFIG_RT_CAN_USING_CANFD,设置 FD 波特率(默认 2 Mbps),在 loopback 模式下收发 FD 帧,确认数据长度与内容正确
  • action:

    • (推送分支后,在此填写您 fork 仓库触发的 CI 编译成功链接,例如:https://github.com/<your-username>/rt-thread/actions/workflows/manual_dist.yml)

CAN0 引脚复用

信号 MCU 引脚 复用 配置要点
CAN0_RXD PORT1_11 Alt11 输入使能,无上下拉
CAN0_TXD PORT1_2 Alt11 输入使能,无上下拉

FlexCAN 外设时钟(驱动层)

入口:

  • 第一次:rt_hw_can_init()(板级组件自动导出)
  • 之后:每次 _can_config() 也会调用

文件: Libraries/drivers/drv_can.c

CAN0 静态配置

    {
        .name = "can0",
        .base = CAN0,
        .instance = 0,
        .irqn = CAN0_IRQn,
        .clock_div_name = kCLOCK_DivFLEXCAN0,
        .clock_attach_id = kFRO_HF_DIV_to_FLEXCAN0,
    },

_can_enable_periph_clock() 做了什么

static void _can_enable_periph_clock(struct imxrt_can *can)
{
    clock_ip_name_t can_clocks[] = FLEXCAN_CLOCKS;

    CLOCK_EnableClock(can_clocks[can->instance]);   // ① 打开 FLEXCAN0 时钟门控
    CLOCK_SetClockDiv(can->clock_div_name, 2u);     // ② 分频 /2
    CLOCK_AttachClk(can->clock_attach_id);          // ③ 时钟源 = FRO_HF_DIV
}
步骤 API 作用
① CLOCK_EnableClock(kCLOCK_GateFLEXCAN0) 打开 FlexCAN0 时钟门控(早期 bug 就是漏了这步,导致 clk=0)
② CLOCK_SetClockDiv(kCLOCK_DivFLEXCAN0, 2) FlexCAN 模块时钟再 ÷2
③ CLOCK_AttachClk(kFRO_HF_DIV_to_FLEXCAN0) 时钟源接 FRO_HF_DIV

频率估算:
Boot 后 FRO HF = 180 MHz,FRO_HF_DIV 通常也为 180 MHz(÷1),再经 FlexCAN 分频 ÷2 → FlexCAN 模块时钟约 90 MHz。

配置/发送前会调用 CLOCK_GetFlexcanClkFreq(0) 读实际频率,用于 500 kbps 位时序计算;若为 0 则报错退出。

验证截图

截图为 CAN FD 内部环回测试验证

8661e50e-f8fc-4069-80a4-0b525aa3378f

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • [] 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • 本拉取/合并请求是一个成熟版本 This PR is mature, and ready to be integrated into the repo

代码质量 Code Quality:

我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:

  • 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • 代码是高质量的 Code in this PR is of high quality
  • 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@ghost
ghost requested a review from Rbb666 as a code owner July 6, 2026 23:50
@CLAassistant

CLAassistant commented Jul 6, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@github-actions github-actions Bot added BSP: NXP Code related with NXP BSP labels Jul 6, 2026
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!

为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。
To ensure your code complies with RT-Thread's coding style, please run the code formatting workflow by following the steps below (If the formatting of CI fails to run).


🛠 操作步骤 | Steps

  1. 前往 Actions 页面 | Go to the Actions page
    点击进入工作流 → | Click to open workflow →

  2. 点击 Run workflow | Click Run workflow

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:mcxa366-canfd-support
  • 设置PR number为 \ Set the PR number to:11579
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 mcxa366-canfd-support 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the mcxa366-canfd-support branch automatically, and the related Pull Request will be updated.

如有问题欢迎联系我们,再次感谢您的贡献!💐
If you have any questions, feel free to reach out. Thanks again for your contribution!

@github-actions

github-actions Bot commented Jul 6, 2026 •

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: bsp_mcxa

Reviewers: @hywing

Changed Files (Click to expand)
  • bsp/nxp/mcx/mcxa/Libraries/drivers/drv_can.c
  • bsp/nxp/mcx/mcxa/frdm-mcxa366/board/board.c

📊 Current Review Status (Last Updated: 2026-07-11 11:26 CST)


📝 Review Instructions

  1. 维护者可以通过单击此处来刷新审查状态: 🔄 刷新状态
    Maintainers can refresh the review status by clicking here: 🔄 Refresh Status

  2. 确认审核通过后评论 LGTM/lgtm
    Comment LGTM/lgtm after confirming approval

  3. PR合并前需至少一位维护者确认
    PR must be confirmed by at least one maintainer before merging

ℹ️ 刷新CI状态操作需要具备仓库写入权限。
ℹ️ Refresh CI status operation requires repository Write permission.

@ghost
ghost force-pushed the mcxa366-canfd-support branch from 3544929 to 3994359 Compare July 6, 2026 23:59
@ghost ghost changed the title add CAN-FD and loopback support bsp: add CAN-FD and loopback support Jul 7, 2026
@CYFS3

CYFS3 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

@waldenice 麻烦把引脚和时钟的初始化提交上来,然后最好能提供一下验证能正常收发的截图,谢谢!

@ghost

ghost commented Jul 7, 2026

Copy link
Copy Markdown
Author

@waldenice 麻烦把引脚和时钟的初始化提交上来,然后最好能提供一下验证能正常收发的截图,谢谢!

已增加,请审阅!

@CYFS3

CYFS3 commented Jul 7, 2026 •

Copy link
Copy Markdown
Contributor

@waldenice 麻烦把引脚和时钟的初始化提交上来,然后最好能提供一下验证能正常收发的截图,谢谢!

已增加,请审阅!

是不是没有推送上了,应该需要有pin_mux.c\h,clock_config.c\h

@ghost

ghost commented Jul 7, 2026

Copy link
Copy Markdown
Author

@waldenice 麻烦把引脚和时钟的初始化提交上来,然后最好能提供一下验证能正常收发的截图,谢谢!

已增加,请审阅!

是不是没有推送上了,应该需要有pin_mux.c\h,clock_config.c\h

pin_mux / clock_config 已经在 FRDM-MCXA366 板级 BSP 基线。

CAN FD 和经典 CAN 共用 FlexCAN0 和同一组 TX/RX 引脚;引脚复用与复位释放与是否 FD 无关,基线里配好 CAN0 即可。
CAN FD 驱动 (drv_can.c) 做的是:FLEXCAN_FDInit()、FD 位时序、Loopback、邮箱等,不再改 pin_mux。

文件 基线里已有 CAN FD PR 是否修改
pin_mux.c/h CAN0 引脚 + 复位 否
clock_config.c/h 180 MHz 系统时钟 否
drv_can.c 经典 CAN 骨架 是(FD + Loopback)

所以本次提交不涉及改这两个文件。

- Extend flexcan driver with CAN-FD TX/RX and FD frame parsing
- Add loopback and loopback+listen-only mode handling
- Fix mailbox layout, clock init, and safe controller deinit
- Add debug assert handlers for frdm-mcxa366 board
@ghost
ghost force-pushed the mcxa366-canfd-support branch from 3994359 to d8e270d Compare July 11, 2026 03:26
@ghost

ghost commented Jul 11, 2026

Copy link
Copy Markdown
Author

lgtm

@ghost

ghost commented Jul 11, 2026

Copy link
Copy Markdown
Author

增强版用 #11609 跟踪,此 PR 关闭。

@ghost ghost closed this Jul 11, 2026
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BSP: NXP Code related with NXP BSP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants