Skip to content

feat(firmware): Use user buttons as bootloader force-stay overrides#49

Merged
qzhhhi merged 2 commits into
mainfrom
dev/force-bootloader
Apr 30, 2026
Merged

feat(firmware): Use user buttons as bootloader force-stay overrides#49
qzhhhi merged 2 commits into
mainfrom
dev/force-bootloader

Conversation

@qzhhhi
Copy link
Copy Markdown
Member

@qzhhhi qzhhhi commented Apr 29, 2026

  • Interpret each board's user button as the bootloader force-stay input so operators can intentionally remain in DFU instead of auto-jumping to the app.
  • Keep the user button as the highest-priority veto while the bootloader is running, including across automatic reboot paths.

固件更新:使用用户按钮作为引导程序强制停留覆盖

概述

本次改动将各板的用户按钮作为引导程序(bootloader)“force-stay”输入:在引导阶段按下用户按钮可阻止引导程序自动跳转到应用程序(进入或保持 DFU 模式)。按钮在引导路径上作为最高优先级的否决信号(veto),跳转条件在两个 bootloader 实现中都加入了对该信号的检查,包括通过自动重启回到引导程序的路径。

主要变化

引导程序逻辑

  • firmware/c_board/bootloader/src/main.cpp

    • 新增基于 Cortex-M DWT 周期计数的微秒延时函数并对 USER_BUTTON 进行多次采样(4 次、每次间隔 250µs),实现稳定按键读取(函数 bootloader_check_bootloader_force_stay_requested)。
    • 在主流程中读取该按键结果到 force_stay;在判断是否跳转到应用时额外要求 !force_stay,若 force_stay 为真则阻止跳转(即使 mailbox 请求或镜像校验允许也不跳转)。
  • firmware/rmcs_board/bootloader/src/main.cpp

    • 在 RMCS 引导程序中同样调用 board_check_bootloader_force_stay_requested(),并将 !force_stay 作为跳转到应用的必要条件(在 AUTO/非 AUTO 模式下都生效)。

硬件与 BSP(C-Board)

  • firmware/c_board/bsp/cubemx/Core/Inc/main.h
    • 添加/暴露了 USER_BUTTON 的 GPIO 宏定义(引脚/端口在 main.h 中可用)。
  • firmware/c_board/bsp/cubemx/Core/Src/gpio.c
    • 在 MX_GPIO_Init() 中将 USER_BUTTON_Pin 配置为带上拉的输入。
  • firmware/c_board/bsp/cubemx/rmcs_slave.ioc
    • .ioc 文件中将 PA0-WKUP 标记为 USER_BUTTON(上拉输入),更新了引脚映射/配置。

板级实现(RMCS Board:Lite / Pro)

  • firmware/rmcs_board/boards/lite/board.c / board.h

    • 新增并导出函数:bool board_check_bootloader_force_stay_requested(void)(在 board.h 中声明)。
    • 实现细节:暂时将 PA07 切换为 SoC GPIO(设置 PAD 控制、GPIOM 路由、配置为输入),对该引脚进行 4 次采样(每次延迟 250µs),若所有采样均为断言态则返回 true;采样完成后将 PA07 恢复为 JTAG TMS,便于在引导程序停留时调试器附着。
  • firmware/rmcs_board/boards/lite/app/board_app.hpp / board_app.cpp

    • 添加编译时 GPIO 描述符 kUserButtonPin(SoC GPIO0,端口 A,pin 7)。
    • init_user_button_and_switch_pins() 扩展以初始化 kUserButtonPin(设置控制器、IOC 功能、PAD 配置并配置为输入)。
  • firmware/rmcs_board/boards/pro/board.c / board.h

    • 同样新增并导出 bool board_check_bootloader_force_stay_requested(void)。
    • 实现细节:将 PY03 设置为 SoC GPIO(IOC/PAD 配置、GPIOM 路由、设为输入),进行 4 次 250µs 采样;只在 4 次均为断言态时返回 true。

公共 API 变更

  • 新增并导出的函数(Lite 与 Pro 板):
    • bool board_check_bootloader_force_stay_requested(void);

(注意:没有新增其它公共类型或宏,c_board 的更改局限于 main.h/gpio.c 的引脚定义与初始化。)

代码审查注意点

  • 按键采样策略为多次延迟采样(4 次、250µs 间隔);c_board 使用 DWT 计数延时,Lite/Pro 使用 board_delay_us;需留意在极端时序或低频率 CPU 下的延时准确性。
  • Lite 实现将引脚临时切回 JTAG TMS,确保在引导停留时仍可调试;需确认此切换在所有调试场景下行为符合预期。
  • 引导跳转条件已在两个引导实现中同步修改,避免了跨板行为不一致的问题。

变更量与复杂度

  • 多处文件修改,主要为引导逻辑与板级按键检测、少量 BSP 引脚配置变更。总体审查工作量中等,重点关注硬件引脚路由与时序相关的实现细节。

- Interpret each board's user button as the bootloader force-stay input so operators can intentionally remain in DFU instead of auto-jumping to the app.
- Keep the user button as the highest-priority veto while the bootloader is running, including across automatic reboot paths.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 29, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e14ab0e3-5e20-4f49-9406-2ba264a50b81

📥 Commits

Reviewing files that changed from the base of the PR and between 487b2d1 and b9f4b37.

📒 Files selected for processing (6)
  • firmware/c_board/bootloader/src/main.cpp
  • firmware/rmcs_board/boards/lite/board.c
  • firmware/rmcs_board/boards/lite/board.h
  • firmware/rmcs_board/boards/pro/board.c
  • firmware/rmcs_board/boards/pro/board.h
  • firmware/rmcs_board/bootloader/src/main.cpp
✅ Files skipped from review due to trivial changes (1)
  • firmware/rmcs_board/boards/lite/board.h
🚧 Files skipped from review as they are similar to previous changes (1)
  • firmware/rmcs_board/bootloader/src/main.cpp

Walkthrough

引导加载程序新增“强制驻留”检查:在启动时通过板级GPIO多次采样用户按键状态决定是否阻止跳转到应用镜像;C板与RMCS Lite/Pro 各自增加按键映射、初始化与采样实现,并在启动跳转条件中加入该标志。

Changes

Cohort / File(s) Summary
C 板引脚与初始化
firmware/c_board/bsp/cubemx/Core/Inc/main.h, firmware/c_board/bsp/cubemx/Core/Src/gpio.c, firmware/c_board/bsp/cubemx/rmcs_slave.ioc
新增 USER_BUTTON (PA0) 宏,CubeMX 配置更新,GPIO 初始化为上拉输入。
C 板引导加载程序
firmware/c_board/bootloader/src/main.cpp
引导启动时读取按钮(多次采样延时),将 force_stay 纳入跳转决策:若按下则阻止跳转至应用。
RMCS Lite — API 与初始化
firmware/rmcs_board/boards/lite/app/board_app.hpp, firmware/rmcs_board/boards/lite/app/board_app.cpp, firmware/rmcs_board/boards/lite/board.h, firmware/rmcs_board/boards/lite/board.c
新增 kUserButtonPin 描述,按键引脚初始化(上拉、施密特触发等)、通过多次采样实现 board_check_bootloader_force_stay_requested()
RMCS Pro — 引脚与查询实现
firmware/rmcs_board/boards/pro/board.h, firmware/rmcs_board/boards/pro/board.c
为 Pro 板配置相应引脚(PY03 / GPIO_Y_03)、初始化 GPIO/GPIOM 路由并实现多次延时采样的查询函数。
RMCS 引导加载程序整合
firmware/rmcs_board/bootloader/src/main.cpp
引导逻辑加入板级 force_stay 查询,AUTO 与 非 AUTO 模式下均以 !force_stay 作为跳转门控条件之一。

Sequence Diagram(s)

sequenceDiagram
    participant Bootloader as Bootloader
    participant BoardGPIO as Board GPIO / IOC
    participant Mailbox as Mailbox (host request)
    participant Flash as Flash Validator
    participant App as Application

    Bootloader->>BoardGPIO: 初始化按键引脚并多次采样(250µs 间隔)
    BoardGPIO-->>Bootloader: 返回 force_stay (true/false)
    Bootloader->>Mailbox: 检查 force_dfu 请求
    Mailbox-->>Bootloader: 返回 force_dfu (true/false)
    Bootloader->>Flash: 校验应用镜像有效性
    Flash-->>Bootloader: 返回 valid (true/false)
    alt 条件: not force_stay && not force_dfu && valid
        Bootloader->>App: 跳转到应用
    else 否则
        Bootloader-->>Bootloader: 保持在引导加载程序 / 进入 DFU
    end
Loading

Estimated code review effort

🎯 3 (中等) | ⏱️ ~20 分钟

Possibly related PRs

Poem

🐰 小兔子蹦跳在电路旁,按下了一个金属心房,
按键轻敲,信号悄悄说“不走”,
引导停住脚步,应用等候微笑,
硬件与固件手牵手,启动的路上多了一道守候。

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题准确地概括了变更集的主要内容:在多个固件板的启动加载程序中实现了用户按钮作为强制保持覆盖机制。
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch dev/force-bootloader

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
Review rate limit: 0/1 reviews remaining, refill in 60 minutes.

Comment @coderabbitai help to get the list of available commands and usage tips.

@qzhhhi qzhhhi merged commit d00cae1 into main Apr 30, 2026
5 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in RMCS Slave SDK Apr 30, 2026
@qzhhhi qzhhhi deleted the dev/force-bootloader branch April 30, 2026 08:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant