Skip to content

Conversation

@POMIN-163
Copy link

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

[

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

add GD32VW553 hw timer support.
bsp/gd32/risc-v/gd32vw553h-eval TEST OK.

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

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

  • BSP:
  • .config:
  • action:

]

当前拉取/合并请求的状态 Intent for your PR

必须选择一项 Choose one (Mandatory):

  • 本拉取/合并请求是一个草稿版本 This PR is for a code-review and is intended to get feedback
  • [x ] 本拉取/合并请求是一个成熟版本 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:

  • [ x] 已经仔细查看过代码改动的对比 Already check the difference between PR and old code
  • [x ] 代码风格正确,包括缩进空格,命名及其他风格 Style guide is adhered to, including spacing, naming and other styles
  • [ x] 没有垃圾代码,代码尽量精简,不包含#if 0代码,不包含已经被注释了的代码 All redundant code is removed and cleaned up
  • [ x] 所有变更均有原因及合理的,并且不会影响到其他软件组件代码或BSP All modifications are justified and not affect other components or BSP
  • [ x] 对难懂代码均提供对应的注释 I've commented appropriately where code is tricky
  • [ x] 代码是高质量的 Code in this PR is of high quality
  • [ x] 已经使用formatting 等源码格式化工具确保格式符合RT-Thread代码规范 This PR complies with RT-Thread code specification
  • [ x] 如果是新增bsp, 已经添加ci检查到.github/ALL_BSP_COMPILE.json 详细请参考链接BSP自查

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.


pomin seems not to be a GitHub user. You need a GitHub account to be able to sign the CLA. If you have already a GitHub account, please add the email address used for this commit to your account.
You have signed the CLA already but the status is still pending? Let us recheck it.

@github-actions
Copy link

👋 感谢您对 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

  • 设置需排除的文件/目录(目录请以"/"结尾)
    Set files/directories to exclude (directories should end with "/")
  • 将目标分支设置为 \ Set the target branch to:master
  • 设置PR number为 \ Set the PR number to:11157
  1. 等待工作流完成 | Wait for the workflow to complete
    格式化后的代码将自动推送至你的分支。
    The formatted code will be automatically pushed to your branch.

完成后,提交将自动更新至 master 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the master 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 BSP: GD32 BSP related with GD32 labels Jan 27, 2026
@kurisaW
Copy link
Member

kurisaW commented Jan 28, 2026

@POMIN-163
Copy link
Author

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request adds hardware timer (HWTIMER) driver support for the GD32VW553 RISC-V BSP. The implementation provides a standard RT-Thread hardware timer device driver interface for timers 0, 1, 2, 5, 15, and 16.

Changes:

  • Adds new hardware timer driver implementation (drv_hwtimer.c/h) with timer initialization, start/stop operations, and interrupt handlers
  • Adds timer configuration header (hwtimer_config.h) defining timer parameters and hardware mappings
  • Updates SConscript to include timer driver compilation and config directory path
  • Adds Kconfig options to enable/disable individual hardware timers

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
bsp/gd32/risc-v/libraries/gd32_drivers/drv_hwtimer.h Header file defining timer data structures and index enumeration
bsp/gd32/risc-v/libraries/gd32_drivers/drv_hwtimer.c Main driver implementation with timer operations and interrupt handlers
bsp/gd32/risc-v/libraries/gd32_drivers/config/hwtimer_config.h Configuration macros for each supported timer instance
bsp/gd32/risc-v/libraries/gd32_drivers/SConscript Build script updated to include timer driver and config path
bsp/gd32/risc-v/gd32vw553h-eval/board/Kconfig Configuration options for enabling hardware timers

Comment on lines +226 to +247
#ifdef BSP_USING_HWTIMER6
void TIMER6_IRQHandler(void)
{
rt_interrupt_enter();
rt_device_hwtimer_isr(&g_gd32_hwtimer[TIM6_INDEX].hwtimer_dev);
timer_flag_clear((uint32_t)g_gd32_hwtimer[TIM6_INDEX].hwtimer_dev.parent.user_data, \
TIMER_INT_UP);
rt_interrupt_leave();
}
#endif

#ifdef BSP_USING_HWTIMER7
void TIMER7_UP_IRQHandler(void)
{
rt_interrupt_enter();
rt_device_hwtimer_isr(&g_gd32_hwtimer[TIM7_INDEX].hwtimer_dev);
timer_flag_clear((uint32_t)g_gd32_hwtimer[TIM7_INDEX].hwtimer_dev.parent.user_data, \
TIMER_INT_UP);
rt_interrupt_leave();
}
#endif

Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

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

These interrupt handlers (TIMER3_IRQHandler, TIMER4_IRQHandler, TIMER6_IRQHandler, TIMER7_UP_IRQHandler) are defined in the code but:

  1. They reference TIM3_INDEX, TIM4_INDEX, TIM6_INDEX, TIM7_INDEX which are not defined in drv_hwtimer.h enum
  2. There are no corresponding BSP_USING_HWTIMER3/4/6/7 guards in drv_hwtimer.c g_gd32_hwtimer array
  3. There are no HWTIMER3/4/6/7_CONFIG macros in hwtimer_config.h
  4. There are no configuration options in Kconfig for these timers

This creates dead code that will never be compiled or used. Either remove these unused interrupt handlers or add the complete support (enum entries, config macros, Kconfig options) for TIMER3, TIMER4, TIMER6, and TIMER7.

这些中断处理程序(TIMER3_IRQHandler、TIMER4_IRQHandler、TIMER6_IRQHandler、TIMER7_UP_IRQHandler)在代码中定义,但是:

  1. 它们引用的 TIM3_INDEX、TIM4_INDEX、TIM6_INDEX、TIM7_INDEX 未在 drv_hwtimer.h 枚举中定义
  2. 在 drv_hwtimer.c 的 g_gd32_hwtimer 数组中没有对应的 BSP_USING_HWTIMER3/4/6/7 保护
  3. 在 hwtimer_config.h 中没有 HWTIMER3/4/6/7_CONFIG 宏
  4. 在 Kconfig 中没有这些定时器的配置选项

这会创建永远不会被编译或使用的死代码。请删除这些未使用的中断处理程序,或者为 TIMER3、TIMER4、TIMER6 和 TIMER7 添加完整的支持(枚举条目、配置宏、Kconfig 选项)。

Suggested change
#ifdef BSP_USING_HWTIMER6
void TIMER6_IRQHandler(void)
{
rt_interrupt_enter();
rt_device_hwtimer_isr(&g_gd32_hwtimer[TIM6_INDEX].hwtimer_dev);
timer_flag_clear((uint32_t)g_gd32_hwtimer[TIM6_INDEX].hwtimer_dev.parent.user_data, \
TIMER_INT_UP);
rt_interrupt_leave();
}
#endif
#ifdef BSP_USING_HWTIMER7
void TIMER7_UP_IRQHandler(void)
{
rt_interrupt_enter();
rt_device_hwtimer_isr(&g_gd32_hwtimer[TIM7_INDEX].hwtimer_dev);
timer_flag_clear((uint32_t)g_gd32_hwtimer[TIM7_INDEX].hwtimer_dev.parent.user_data, \
TIMER_INT_UP);
rt_interrupt_leave();
}
#endif

Copilot uses AI. Check for mistakes.
/* whether should frequency doubling */
temp = (temp < 4) ? 0 : 1;

prescaler = (ap2freq << temp) / freq - 1;
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

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

Potential division by zero vulnerability. If freq parameter is 0, this will cause a division by zero error. Add validation to check that freq is not zero before performing the division.

潜在的除零漏洞。如果 freq 参数为 0,这将导致除零错误。在执行除法之前添加验证以检查 freq 不为零。

Copilot uses AI. Check for mistakes.
Comment on lines +109 to +129
static rt_err_t gd32_hwtimer_control(struct rt_hwtimer_device *timer, rt_uint32_t cmd, \
void *args)
{
int ret = RT_EOK;
rt_int32_t freq;
rt_hwtimer_mode_t mode;

switch (cmd)
{
case HWTIMER_CTRL_FREQ_SET:
freq = *(rt_uint32_t *)args;
__set_timerx_freq((uint32_t)timer->parent.user_data, freq);
break;
default:
rt_kprintf("invalid cmd:%x\n", cmd);
ret = -RT_EINVAL;
break;
}

return ret;
}
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

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

The control function only handles HWTIMER_CTRL_FREQ_SET but does not handle other standard control commands defined in the hwtimer framework:

  • HWTIMER_CTRL_STOP (stop timer)
  • HWTIMER_CTRL_INFO_GET (get timer feature information)
  • HWTIMER_CTRL_MODE_SET (setting the timing mode)

Consider implementing these control commands for completeness, or at least handle HWTIMER_CTRL_INFO_GET which could return the timer info structure, as this is commonly used by applications to query timer capabilities.

控制函数只处理 HWTIMER_CTRL_FREQ_SET,但不处理 hwtimer 框架中定义的其他标准控制命令:

  • HWTIMER_CTRL_STOP(停止定时器)
  • HWTIMER_CTRL_INFO_GET(获取定时器功能信息)
  • HWTIMER_CTRL_MODE_SET(设置定时模式)

考虑实现这些控制命令以保证完整性,或至少处理 HWTIMER_CTRL_INFO_GET,它可以返回定时器信息结构,因为应用程序通常使用它来查询定时器功能。

Copilot uses AI. Check for mistakes.
{
int ret = RT_EOK;
rt_int32_t freq;
rt_hwtimer_mode_t mode;
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

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

The variable mode is declared but never used in the function. This will generate a compiler warning. Remove this unused variable declaration.

变量 mode 已声明但从未在函数中使用。这将产生编译器警告。删除此未使用的变量声明。

Suggested change
rt_hwtimer_mode_t mode;

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,274 @@
/*
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

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

PR title format issue: The title "bsp: [gd32] add GD32VW553 hw timer support." does not follow the required format. Based on RT-Thread coding guidelines and the modified files, the title should follow the pattern [module][subsystem] Description in lowercase.

Suggested title: [gd32][drivers] Add GD32VW553 hardware timer support or [gd32vw553h-eval][hwtimer] Add hardware timer driver support

PR 标题格式问题:标题 "bsp: [gd32] add GD32VW553 hw timer support." 不符合要求的格式。根据 RT-Thread 编码指南和修改的文件,标题应遵循小写的 [模块][子系统] 描述 格式。

建议标题:[gd32][drivers] Add GD32VW553 hardware timer support[gd32vw553h-eval][hwtimer] Add hardware timer driver support

Copilot generated this review using guidance from repository custom instructions.
Comment on lines +165 to +166
timer_flag_clear((uint32_t)g_gd32_hwtimer[TIM0_INDEX].hwtimer_dev.parent.user_data, \
TIMER_INT_UP);
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

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

The line continuation backslash is inconsistent with the rest of the function calls in the codebase. In this file, timer_flag_clear should not need line continuation as it fits within reasonable line length. Consider removing the line continuation for better readability:

timer_flag_clear((uint32_t)g_gd32_hwtimer[TIM0_INDEX].hwtimer_dev.parent.user_data, TIMER_INT_UP);

行继续反斜杠与代码库中其余函数调用不一致。在此文件中,timer_flag_clear 不需要行继续,因为它在合理的行长度内。为了更好的可读性,请考虑删除行继续。

Copilot uses AI. Check for mistakes.
Comment on lines +176 to +177
timer_flag_clear((uint32_t)g_gd32_hwtimer[TIM1_INDEX].hwtimer_dev.parent.user_data, \
TIMER_INT_UP);
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

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

The line continuation backslash is inconsistent with the rest of the function calls in the codebase. In this file, timer_flag_clear should not need line continuation as it fits within reasonable line length. Consider removing the line continuation for better readability. This issue appears in multiple interrupt handlers (lines 176-177, 187-188, 198-199, 209-210, 220-221, 231-232, 242-243).

行继续反斜杠与代码库中其余函数调用不一致。在此文件中,timer_flag_clear 不需要行继续,因为它在合理的行长度内。为了更好的可读性,请考虑删除行继续。此问题出现在多个中断处理程序中(第176-177、187-188、198-199、209-210、220-221、231-232、242-243行)。

Copilot uses AI. Check for mistakes.
/* whether should frequency doubling */
temp = (temp < 4) ? 0 : 1;

prescaler = (ap1freq << temp) / freq - 1;
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

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

Potential division by zero vulnerability. If freq parameter is 0, this will cause a division by zero error. Add validation to check that freq is not zero before performing the division. This is the same issue as line 40.

潜在的除零漏洞。如果 freq 参数为 0,这将导致除零错误。在执行除法之前添加验证以检查 freq 不为零。这与第40行是同样的问题。

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,274 @@
/*
* Copyright (c) 2006-2022, RT-Thread Development Team
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

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

Copyright year inconsistency: This file uses "2006-2022" while drv_hwtimer.h uses "2006-2025". For new files created in 2025/2026, the copyright should consistently use "2006-2025" or the current year range.

版权年份不一致:此文件使用"2006-2022",而 drv_hwtimer.h 使用"2006-2025"。对于 2025/2026 年创建的新文件,版权应一致使用"2006-2025"或当前年份范围。

Suggested change
* Copyright (c) 2006-2022, RT-Thread Development Team
* Copyright (c) 2006-2025, RT-Thread Development Team

Copilot uses AI. Check for mistakes.
Comment on lines +165 to +166
timer_flag_clear((uint32_t)g_gd32_hwtimer[TIM0_INDEX].hwtimer_dev.parent.user_data, \
TIMER_INT_UP);
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

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

The interrupt flag being cleared is TIMER_INT_UP, but this appears to be an interrupt enable flag, not an interrupt status flag. According to GD32 timer peripheral conventions, the correct flag to clear should be TIMER_FLAG_UP or similar status flag. Using TIMER_INT_UP may not properly clear the interrupt status, potentially causing repeated interrupts or incorrect behavior.

Please verify the correct flag name for clearing timer update interrupt status in the GD32VW553 HAL library.

清除的中断标志是 TIMER_INT_UP,但这似乎是中断使能标志,而不是中断状态标志。根据 GD32 定时器外设约定,清除的正确标志应该是 TIMER_FLAG_UP 或类似的状态标志。使用 TIMER_INT_UP 可能无法正确清除中断状态,可能导致重复中断或不正确的行为。

请验证 GD32VW553 HAL 库中清除定时器更新中断状态的正确标志名称。

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

BSP: GD32 BSP related with GD32 BSP

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants