Skip to content

bsp: k230: fix PWM pulse width readback - #12032

Merged
Rbb666 merged 4 commits into
RT-Thread:masterfrom
usualtreee:fix/k230-pwm-readback
Nov 13, 2026
Merged

Rbb666 merged 4 commits into
RT-Thread:masterfrom
usualtreee:fix/k230-pwm-readback

Conversation

@usualtreee

@usualtreee usualtreee commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

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

[

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

K230 kd_pwm_get() 存在脉宽读回错误:设置侧向输出通道比较寄存器写入 period_count - high_count,读取侧却直接把比较值换算为脉宽,因此返回的是互补脉宽。例如请求 100000/25000 ns,旧驱动读回 100000/75000 ns。

此外,同组输出通道共享周期寄存器 pwmcmp0。另一通道设置更短周期后,原通道的比较值可能大于新的周期计数;若直接相减会产生无符号下溢。

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

  • 在同一次 kd_pwm_get() 中读取一次 pwmcmp0,周期和脉宽换算统一使用该值。
  • 比较值小于周期计数时,以 period_count - compare_value 恢复高电平计数;比较值大于或等于周期计数时返回零脉宽和 RT_EOK,符合比较器 0% 状态及 pulse <= period 的接口约定。
  • 保持设置、分频、周期处理和输出使能路径不变。
  • 扩展现有 bsp.k230.drivers.pwm 测试,保留 0%、25%、75%、100% 和非零分频测试点,新增跨通道场景,并断言具体返回值、周期和脉宽。

主机模拟使用真实 drv_pwm.c、模拟寄存器和 100 MHz 测试时钟,验证正常读回及跨通道无符号下溢;主机验证不替代实板测试。

实板验证使用同一块 01Studio CanMV K230、同一 TF 卡和 CPU0 UART,执行 utest_run bsp.k230.drivers.pwm。旧驱动五个脉宽断言均失败;修复后完整 utest 为 PASSED,0%、25%、75%、100%、非零分频及跨通道场景均通过。跨通道场景读回 RT_EOK、50000/0 ns。

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

  • BSP: bsp/k230,01Studio CanMV K230;GPIO52 复用为 PWM4,对应 pwm1 逻辑通道 1。

  • .config: CONFIG_SOC_K230=y、CONFIG_BSP_USING_PWM=y、CONFIG_BSP_USING_PWM1=y、CONFIG_RT_USING_UTEST=y、CONFIG_BSP_UTEST_DRIVERS=y、CONFIG_UTEST_THR_STACK_SIZE=8192。

  • action: K230 编译通过。另已通过 clang-format 22.1.8 变更行检查和 git diff --check。

]

当前拉取/合并请求的状态 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
  • 已经使用clang-format 源码格式化工具确保格式符合RT-Thread代码规范 This PR has been formatted with clang-format and complies with RT-Thread code specification
  • 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

kd_pwm_set() stores period_count - high_count in the output compare register, but kd_pwm_get() previously interpreted that register as high_count. As a result, rt_pwm_get() returned the complementary pulse width.

Read pwmcmp0 once and use the same period count for both conversions. Recover the high-level count from the period and channel compare values, and return zero pulse when a shorter shared period leaves another channel's compare value above pwmcmp0. This is a valid 0% comparator state, so keep returning RT_EOK and preserve pulse <= period.

Extend the existing 01Studio PWM utest to cover 0%, 25%, 75%, and 100% duty cycle, a period requiring nonzero scaling, and the cross-channel compare-underflow scenario.

Tested with the real driver functions and simulated registers on the host, and on an 01Studio CanMV K230 using GPIO52/PWM4 (pwm1 channel 1). The original five points still pass. The cross-channel readback changes from 50000/4294942296 ns to 50000/0 ns, both returning RT_EOK.
@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 分支 fix/k230-pwm-readback
    Enter PR branch fix/k230-pwm-readback 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.

完成后,提交将自动更新至 fix/k230-pwm-readback 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix/k230-pwm-readback 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 the BSP label Nov 11, 2026
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

k230

  • SRAM: .eh_frame +152 B, .sbss +8 B (+0.0%, 1,395,428 B / 60,682,240 B, total: 2% used)

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

@BernardXiong BernardXiong left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

批准。 该 BSP 属于 RT-Smart 相关配置,按流程做了 review。head ec72f032:1 << (PWM_SCALE_MAX_BITS + PWM_PERIOD_BITS) 是 31 位有符号左移,属未定义行为且会被当成负数,导致合法 period 被拒;改为 1ULL << 31 后边界恢复为 2^31-1,并补齐大括号。CI 绿(12 成功、3 跳过)。

Approved. This Smart-capable BSP is reviewed rather than skipped. Replacing the signed 1 << 31 (undefined behavior and negative in practice) with 1ULL << 31 restores the intended upper bound of 2^31-1. CI is green.

@Rbb666
Rbb666 merged commit 4aaa84b into RT-Thread:master Nov 13, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants