-
Notifications
You must be signed in to change notification settings - Fork 5.2k
bsp: k230: add support for PWM driver #10536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
📌 Code Review Assignment🏷️ Tag: bsp_k230Reviewers: unicornx Changed Files (Click to expand)
📊 Current Review Status (Last Updated: 2025-07-27 01:32 CST)
📝 Review Instructions
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你这里 .config 的变化有点多啊。我基于 d23006e, 应该也是你提的 pr 所基于的 master 吧,执行一下命令:
cd bsp/k230
scons -c
scons --menuconfig # 什么也不做,只是保存后退出
得到的 .config 和原先的 .config 的 diff 见附件:
diff_config.txt
你是不是把你本地的一些配置修改全部提交上来了?请清理一下。原则上应该只有我这里看到的这些 diff(这主要是 master 升级后其它 kconfig 修改导致的 default 值的变化)加上你这次 pwm 相关的配置变化才对。
bsp/k230/board/Kconfig
Outdated
@@ -5,6 +5,11 @@ menu "Drivers Configuration" | |||
select RT_USING_ADC | |||
default n | |||
|
|||
config BSP_USING_PWM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|
||
static struct rt_pwm_ops drv_ops = | ||
{ | ||
.control=kd_pwm_control |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
等号两边加空格隔开。原则是 c 编码时关键字要用空格凸显出来。
#include "drv_pwm.h" | ||
#include <sys/ioctl.h> | ||
static struct rt_device_pwm kd_pwm; | ||
kd_pwm_t *reg_pwm; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要定义为全局变量
#include "sysctl_clk.h" | ||
#include "drv_pwm.h" | ||
#include <sys/ioctl.h> | ||
static struct rt_device_pwm kd_pwm; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
k230 有两个 pwm,但这里为什么只实现了一个?
可以参考 bsp/cvitek/drivers/drv_pwm.c
的实现做。
|
||
static int check_channel(int channel) | ||
{ | ||
if (channel < 0 || channel > 5) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
6 个通道的概念都是错误的,canaan 原来的代码做出来的效果是一个 PWM 上带 6 个 channel。但是我们要做的效果是 两个 PWM,每个 3 个通道。
canaan 的说法也说明了这个问题。

他们也说了他们linux 的驱动也是按照两个 PWM 设备写的:
TRM 上也有相关描述,现在来看 TRM 上的描述只是针对一个 PWM 设备。
所以我觉得可以这么做:
我们严格按照 TRM 上的做,两个 PWM,每个 4 个通道。
寄存器上体现的也是有 4 个,但是我理解是说 0 号 channel 没有用。可用的是 1/2/3,按照 canaan 的说法:
第一个 PWM0 的基地址是 0x9140_A000, 第二个 PWM1 的基地址是 0x9140_A000 + 0x40
每个 PWM 都对应一组寄存器组:
对于第一个通道我们做成 unavailable,如果api 传入 channelno为 0 就返回错误。
你需要签一下 CLA |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
第二次 review,请继续修改。
bsp/k230/.config
Outdated
@@ -314,12 +317,12 @@ CONFIG_CPUTIME_TIMER_FREQ=25000000 | |||
# CONFIG_RT_USING_I2C is not set | |||
# CONFIG_RT_USING_PHY is not set | |||
# CONFIG_RT_USING_PHY_V2 is not set | |||
# CONFIG_RT_USING_ADC is not set | |||
CONFIG_RT_USING_ADC=y |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为什么会 enable ADC?
bsp/k230/.config
Outdated
# CONFIG_RT_USING_DAC is not set | ||
CONFIG_RT_USING_NULL=y | ||
CONFIG_RT_USING_ZERO=y | ||
CONFIG_RT_USING_RANDOM=y | ||
# CONFIG_RT_USING_PWM is not set | ||
CONFIG_RT_USING_PWM=y |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我感觉你这次提交上来的 .config 文件好像还是多了很多。
附件是我 checkout 到 master 后运行 scons --menuconfig,不做修改直接保存退出后的 .config 和 rtconfig.h, 你对比一下。你那边做出来的应该只会比附件多一些 bsp 的 pwm 的东西,而且 CONFIG_RT_USING_PWM 默认也是 disable 的。
config.txt
rtconfig.h.txt
文件名稍微改了一下,因为 github 贴附件对文件名有限制。
bool "Enable PWM" | ||
select RT_USING_PWM | ||
default n | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你好像没有理解我说的配置里面做两个 PWM 的意思,摘录 bsp/k230/board/Kconfig 里 watchdog 的写法你再参考一下,参考改成 PWM 的样子。因为我们有两个 PWM 设备,允许用户通过配置开一个或者关一个。默认可以都关掉。做的好一点可以根据我们支持的 01studio 的板子的支持情况,譬如板子有默认开哪个就开哪个。这个目前我们做得比较粗糙后面可以改进。但至少要支持多个 PWM 的可配置。
menuconfig BSP_USING_WDT
bool "Enable Watchdog Timer"
select RT_USING_WDT
default n
if BSP_USING_WDT
config BSP_USING_WDT0
bool "Enable WDT0"
default n
config BSP_USING_WDT1
bool "Enable WDT1"
default n
endif
} pwm_param_t; | ||
|
||
#endif | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
多余的文件尾部空行删掉
int rt_hw_pwm_init(void) | ||
{ | ||
kd_pwm_t *reg_pwm0,*reg_pwm1; | ||
static struct rt_device_pwm kd_pwm0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
建议将 pwm 设备封装一下,参考 bsp/k230/drivers/interdrv/wdt/drv_wdt.c 中的 struct k230_wdt_dev
的写法.
另外如果 Kcofnig 支持了配置,原则上初始化时也应该通过配置的宏来决定我们到底要注册几个设备。配置没有 enable 的设备不注册的。这个我看了一下 watchdog 的驱动做的不好,以后是一个改进点。
比较好的参考可以看 bsp/k230/drivers/interdrv/sdio/drv_sdhci.c
的 kd_sdhci_init
#define PWM_SCALE_MAX_BITS 15 | ||
|
||
#define PWM0_BASE_ADDR 0x9140A000 | ||
#define PWM1_BASE_ADDR 0x9140A040 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用 bsp/k230/board/board.h
里定义的 PWM_BASE_ADDR
来定义你的宏,不要重复写立即数。
|
||
pwmscale = reg->pwmcfg & 0xf; | ||
pwm_pclock >>= pwmscale; | ||
period = reg->pwmcmp0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里使用 pwmcmp0 是什么意思?第 0 号 channel 不是我们不用的么,是 canaan 说的作为基准的意思?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cannan那边说的是有0,1,2,3,四个通道,但是第0个通道用不了,只能用1,2,3通道。但是第1,2,3通道在驱动里面的的channel值分别是0,1,2。不知道那个禁用的第0个通道在驱动中代表的数值是几,这个我再向cannan问清楚吧
#define PWM_DEV_NAME "pwm1" /* PWM设备名称 */ | ||
#define PWM_DEV_CHANNEL 1 /* PWM通道 */ | ||
#define TEST_GPIO_LED 52 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
多余的空行
bsp/k230/drivers/utest/test_pwm.c
Outdated
*/ | ||
|
||
#define PWM_DEV_NAME "pwm1" /* PWM设备名称 */ | ||
#define PWM_DEV_CHANNEL 1 /* PWM通道 */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
代码没有对齐,包括注释。
bsp/k230/drivers/utest/test_pwm.c
Outdated
} | ||
|
||
UTEST_TC_EXPORT(pwm_testcase, "pwm", utest_tc_init, utest_tc_cleanup, 10); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
行尾空行
Added a PWM driver and a test file test_pwm.c. The test uses PWM to control the LED brightness, to check if the driver works correctly. Signed-off-by: XU HU <1337858472@qq.com>
Added a PWM driver and a test file test_pwm.c.
The test uses PWM to control the LED brightness,
to check if the driver works correctly.
Signed-off-by: XU HU 1337858472@qq.com