Repository navigation
bsp: k230: fix PWM pulse width readback - #12032
Conversation
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.
|
👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread! 为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流(如果格式化CI运行失败)。 🛠 操作步骤 | Steps
完成后,提交将自动更新至 如有问题欢迎联系我们,再次感谢您的贡献!💐 |
|
BernardXiong
left a comment
There was a problem hiding this comment.
批准。 该 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.
拉取/合并请求描述:(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):
代码质量 Code Quality:
我在这个拉取/合并请求中已经考虑了 As part of this pull request, I've considered the following:
#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up