Skip to content

[components][drivers][rtc] fix minute/hour alarm firing before trigger time - #12114

Open
BernardXiong wants to merge 2 commits into
RT-Thread:masterfrom
BernardXiong:fix/rtc-alarm-minute-early-trigger
Open

BernardXiong wants to merge 2 commits into
RT-Thread:masterfrom
BernardXiong:fix/rtc-alarm-minute-early-trigger

Conversation

@BernardXiong

Copy link
Copy Markdown
Member

问题

Issue #12108 报告:停止闹钟 A 时,另一个尚未到期的 RT_ALARM_MINUTE 闹钟 B 会被提前触发。

原因是 rt_alarm_stop() 会调用 alarm_update(0),后者遍历所有闹钟执行 alarm_wakeup();而 RT_ALARM_MINUTE 分支只比较 tm_sec,不比较 tm_min。若 B 保存的触发秒与当前秒相同,就会被误判为到期。RT_ALARM_HOUR 分支同样只比较分、秒,未比较小时,存在同类问题。

改动

wktime 中保存的正是下一次触发时间,因此在唤醒前比较完整的时间字段:

  • RT_ALARM_MINUTE:增加 tm_min 比较;
  • RT_ALARM_HOUR:增加 tm_hour 比较。

由停止/修改其他闹钟引起的重排不会再提前唤醒尚未到期的闹钟。

验证

  • 在 bsp/simulator 临时启用 RT_USING_ALARM 后编译,components/drivers/rtc/dev_alarm.o 编译通过;
  • 本机缺少 SDL2,最终链接阶段失败,与本次改动无关;
  • 未做运行时复现。

未覆盖

  • 未在硬件或 QEMU 上做运行时验证;
  • RT_ALARM_SECOND 在重排时的唤醒行为未在本次改动范围内。

Fixes #12108

…r time

alarm_wakeup() 对 RT_ALARM_MINUTE 只比较 tm_sec,RT_ALARM_HOUR 只比较
tm_min/tm_sec。rt_alarm_stop() 停止或修改其他闹钟时会调用 alarm_update(),
此时秒字段相同的闹钟会被误判为到期并提前执行回调。

wktime 保存的正是下一次触发时间,因此在唤醒前比较完整的时间字段:
MINUTE 增加 tm_min,HOUR 增加 tm_hour。这样由其他闹钟引起的重排不会再
提前触发尚未到期的闹钟。

Fixes RT-Thread#12108
@github-actions

github-actions Bot commented Dec 2, 2026

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/rtc-alarm-minute-early-trigger
    Enter PR branch fix/rtc-alarm-minute-early-trigger 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/rtc-alarm-minute-early-trigger 分支,关联的 Pull Request 也会同步更新。
Once completed, commits will be pushed to the fix/rtc-alarm-minute-early-trigger 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 commented Dec 2, 2026

Copy link
Copy Markdown

📌 Code Review Assignment

🏷️ Tag: components

Reviewers: @Maihuanyi

Changed Files (Click to expand)
  • components/drivers/rtc/dev_alarm.c

📊 Current Review Status (Last Updated: 2026-12-03 02:15 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

github-actions Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

Comment thread components/drivers/rtc/dev_alarm.c Outdated
if (alarm->wktime.tm_sec == now->tm_sec)
/* wktime stores the next trigger time; compare the minute as well
* so a reschedule triggered by another alarm cannot fire it early */
if ((alarm->wktime.tm_min == now->tm_min) &&

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.

[P1] 重排时保留跨整点的触发时间

分钟闹钟 B 原定 13:00:00 触发。12:59:00 停止另一个闹钟后,上面的赋值会先把 B 改成 12:00:00。分钟比较失败后仍会下发这个错误时间,导致漏掉 13:00:00。请移除比较前的小时赋值,用 sec_alarm == sec_now 判断到期,只在命中后推进下一次时间。

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

已按建议修改(14f80c2c7d):移除比较前的 tm_hour 赋值,改用 sec_alarm == sec_now 判断是否到期,只在命中后才推进 wktime。这样停止/修改其他闹钟触发重排时,不会改写尚未到期闹钟的触发时间,也不会下发已经过期的时间。

验证:在 bsp/simulator 临时启用 RT_USING_ALARM 后编译,components/drivers/rtc/dev_alarm.o 编译通过;链接仍因本机缺少 SDL2 失败,与本次改动无关。

RT_ALARM_MINUTE 在比较前会先把 wktime.tm_hour 改成当前小时,若该次比较
未命中,wktime 中保存的下一次触发时间就被改坏,alarm_update() 随后可能
下发一个已经过期的时间,导致跨整点的闹钟漏触发。

改为直接用 sec_alarm == sec_now 判断是否到期;只有命中后才更新下一次
触发时间。RT_ALARM_HOUR 采用同样的判断方式。

Fixes RT-Thread#12108
@wdfk-prog

Copy link
Copy Markdown
Contributor
  • 这个修改解决了闹钟提前触发的问题,不过建议合入前补一个「闹钟线程晚处理 1 秒」的回归用例。

  • 以 RT_ALARM_MINUTE 为例,闹钟原定 13:00:00,第一次扫描在 13:00:01。按新旧代码的判断逻辑回放,两版都会漏掉这一次;但到 13:01:00 准时扫描时,旧版能恢复触发,新版 14f80c2 仍不触发。原因是当前实现只有 sec_alarm == sec_now 才推进 wktime,错过一次后,wktime 仍停在 13:00:00。RT_ALARM_HOUR 也有同样的情况。

  • 建议把「是否补发这次回调」和「计算下一次触发时间」分开处理:即使选择跳过已经错过的那次,也应把 wktime 推进到下一个有效周期,并验证下一次准时扫描只回调一次。

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] RTC Alarm:停止一个闹钟时,另一个 RT_ALARM_MINUTE 闹钟可能提前触发

3 participants