Skip to content

fix[stm32][pwm]: avoid unnecessary PWM prescaling - #12087

Open
wdfk-prog wants to merge 1 commit into
RT-Thread:masterfrom
wdfk-prog:stm32/drv_pwm
Open

wdfk-prog wants to merge 1 commit into
RT-Thread:masterfrom
wdfk-prog:stm32/drv_pwm

Conversation

@wdfk-prog

Copy link
Copy Markdown
Contributor

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

STM32 PWM 驱动中,原有预分频系数计算公式为:

psc = period / MAX_PERIOD + 1;

当 period 恰好为 MAX_PERIOD 的整数倍时,该公式会产生不必要的预分频,降低 PWM 脉宽调节分辨率,并可能因整数除法截断引入额外的周期误差。

例如,当 period = 65535 时,原实现采用两倍分频,但实际上无需分频即可满足计数范围要求。

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

将预分频系数的计算方式修改为向上取整:

psc = period > 0 ? (period - 1) / MAX_PERIOD + 1 : 1;
  • 避免 period 为 MAX_PERIOD 整数倍时产生额外分频。
  • 保留零周期处理,避免减法下溢和除零。
  • 保留 MAX_PERIOD 为 65535,确保现有 100% 占空比处理所需的 CCR 值不超出 16 位寄存器范围。
  • 同步修复 drv_pwm_set() 和 drv_pwm_set_period(),保持两个周期设置接口的计算逻辑一致。

本次修改不改变现有 API、寄存器配置顺序及占空比处理逻辑。

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

  • BSP:尚未进行目标板编译及实机验证。
  • .config:无需修改配置。
  • action:尚未执行。

已完成整数运算边界验证及三轮代码审查,覆盖零周期、MAX_PERIOD 整数倍以及 PSC、ARR、CCR 寄存器边界,未发现本次修改引入的问题。

- Use ceiling division to calculate the minimum prescaler without an extra division factor at exact multiples of MAX_PERIOD.

- Preserve the zero-period handling and the counter limit required for 100% duty cycle.
@github-actions

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

  • Use workflow from 保持默认分支(通常为 master)
    Keep the default branch (usually master) in Use workflow from
  • 在 branch 输入框填写 PR 分支 stm32/drv_pwm
    Enter PR branch stm32/drv_pwm in the branch field
  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将作为独立提交推送至你的分支。
    The formatting changes will be pushed to your branch as a separate commit.

完成后,提交将自动更新至 stm32/drv_pwm 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the stm32/drv_pwm 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 added BSP: STM32 BSP related with ST/STM32 BSP labels Nov 25, 2026
@github-actions

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: bsp_stm32

Reviewers: @Liang1795 @hamburger-os @wdfk-prog

Changed Files (Click to expand)
  • bsp/stm32/libraries/HAL_Drivers/drivers/drv_pwm.c

📊 Current Review Status (Last Updated: 2026-11-25 10:33 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.

@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BSP: STM32 BSP related with ST/STM32 BSP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant