Skip to content

arch/arm/ameba: add shared RTC driver for RTL8721Dx / RTL8720F / RTL8721F - #19945

Open
dcgong2917 wants to merge 3 commits into
apache:masterfrom
dcgong2917:ameba-rtc
Open

arch/arm/ameba: add shared RTC driver for RTL8721Dx / RTL8720F / RTL8721F#19945
dcgong2917 wants to merge 3 commits into
apache:masterfrom
dcgong2917:ameba-rtc

Conversation

@dcgong2917

@dcgong2917 dcgong2917 commented Aug 24, 2026

Copy link
Copy Markdown
Contributor

Summary

Adds an on-chip RTC driver for the Realtek Ameba family, exposed as a
/dev/rtc0 date/time character device with alarm support. The driver lives in
the shared layer arch/arm/src/common/ameba/ameba_rtc.c and is wired into three
ICs — rtl8721dx , rtl8720f and rtl8721f — reusing the common code unchanged.

Design notes:

  • Sits on the SDK fwlib RTC API (mirrored structures + local externs, no vendor
    headers pulled into the NuttX include world). The fwlib RTC API lives in the
    RAM source ameba_rtc.c, added to the fwlib build under CONFIG_AMEBA_RTC.
  • Provides both the lower-half RTC (ameba_rtc_lowerhalf; alarm
    set/cancel/relative + upper-half callback) and the arch date/time hooks
    (up_rtc_initialize/getdatetime/settime, g_rtc_enabled), so NuttX
    system time is seeded from the RTC.
  • The hardware keeps a year plus a day-of-year (no month/day register); the
    driver bridges that to the NuttX calendar with the libc UTC routines
    (timegm/gmtime_r), which is exact and reversible. Cold start seeds the
    build date when the stored year predates CONFIG_START_YEAR.
  • The only per-chip fact — the RTC interrupt vector — lives in the per-chip
    ameba_rtc_chip.h (46 / 33 / 41); the shared driver is never edited for a new
    Ameba chip.

A rtc board profile (examples/alarm) is added to each board for verification,
and the board documentation is updated.

Impact

  • New peripheral, opt-in via CONFIG_AMEBA_RTC (default n). No impact on
    existing configs.
  • Affects rtl8721dx / rtl8720f / rtl8721f only.

Testing

  • tools/checkpatch.sh -g master..HEAD: all checks pass.

  • Builds and links cleanly on all three ICs (rtl8721dx, rtl8720f, rtl8721f).

  • Hardware: verified on all three EVBs (rtl8721dx, rtl8720f, rtl8721f)
    with the rtc config, exercising every driver path:

    • Cold start / seeding: on first power-up the RTC reads back the seeded
      build date instead of a pre-epoch value, so date is valid immediately.
    • Read / set (RTC_RD_TIME / RTC_SET_TIME via NSH date): the
      year/day-of-year hardware model is correctly bridged to the full
      month/day calendar in both directions.
    • Timekeeping: consecutive date reads show the clock advancing in real
      time.
    • Alarm (RTC_SET_ALARM + IRQ callback via alarm example): a one-shot
      alarm fires once at the requested offset and does not re-fire.

    Representative NSH session (rtl8721dx):

    nsh> date
    Tue, Jun 16 00:00:07 2026            # seeded build date after cold start
    nsh> date -s "Jun 16 12:00:00 2026"
    nsh> date
    Tue, Jun 16 12:00:06 2026            # set time read back (+ elapsed seconds)
    nsh> date
    Tue, Jun 16 12:00:09 2026            # clock advancing
    nsh> alarm 5
    alarm_daemon: Running
    Opening /dev/rtc0
    Alarm 0 set in 5 seconds
    alarm_daemon: alarm 0 received       # fires once, 5 s later

Expose the Ameba on-chip RTC as a NuttX date/time RTC at /dev/rtc0
(rdtime/settime) with a single one-shot alarm (setalarm/rdalarm/
cancelalarm/setrelative) that fires the RTC interrupt and the upper-half
callback.  The same hardware also backs the arch date/time RTC hooks
(up_rtc_initialize/getdatetime/settime, g_rtc_enabled) so the NuttX
system time is seeded from it.

The driver sits on the SDK fwlib RTC API (mirrored structures + local
externs, no vendor headers pulled into the NuttX include world).  The
fwlib RTC API lives in the RAM source ameba_rtc.c, so it is added to the
board fwlib build under CONFIG_AMEBA_RTC.  The hardware keeps a year plus
a day-of-year (no month/day register); the driver bridges that to the
NuttX month/day calendar with the libc UTC routines (timegm/gmtime_r),
which is exact and reversible.

The only per-chip fact -- the RTC interrupt vector -- lives in the
per-chip ameba_rtc_chip.h; the shared driver is never edited for a new
Ameba chip.  A configs/rtc profile (examples/alarm) is added for
verification.

Signed-off-by: dechao_gong <dechao_gong@realsil.com.cn>
Assisted-by: Claude <noreply@anthropic.com>
Wire the shared Ameba RTC driver (arch/arm/src/common/ameba/ameba_rtc.c)
into RTL8720F.  The driver is chip-agnostic and reads only per-chip macros
from ameba_rtc_chip.h; RTL8720F differs from amebadplus only in the RTC
interrupt vector (RTL8720F_IRQ_RTC, vector 33).  The APBPeriph_RTC masks
and RTC_BASE_YEAR (1900) are identical across all current Ameba chips.

 - ameba_rtc_chip.h: per-chip RTC IRQ / clock masks / base year
 - Make.defs, ameba_board.mk: compile ameba_rtc.c and the fwlib RAM
   RTC source when CONFIG_AMEBA_RTC=y
 - board: rtl8720f_rtc.c registers /dev/rtc0 from bringup
 - configs/rtc: examples/alarm profile

Signed-off-by: dechao_gong <dechao_gong@realsil.com.cn>
Assisted-by: Claude <noreply@anthropic.com>
Wire the shared Ameba RTC driver (arch/arm/src/common/ameba/ameba_rtc.c)
into RTL8721F (amebagreen2).  The driver is chip-agnostic and reads only
per-chip macros from ameba_rtc_chip.h; RTL8721F differs from amebadplus
only in the RTC interrupt vector (RTL8721F_IRQ_RTC, vector 41).  The
APBPeriph_RTC masks and RTC_BASE_YEAR (1900) are identical across all
current Ameba chips.

 - ameba_rtc_chip.h: per-chip RTC IRQ / clock masks / base year
 - Make.defs, ameba_board.mk: compile ameba_rtc.c and the fwlib RAM
   RTC source when CONFIG_AMEBA_RTC=y
 - board: rtl8721f_rtc.c registers /dev/rtc0 from bringup
 - configs/rtc: examples/alarm profile

Signed-off-by: dechao_gong <dechao_gong@realsil.com.cn>
Assisted-by: Claude <noreply@anthropic.com>
@github-actions github-actions Bot added Area: Documentation Improvements or additions to documentation Arch: arm Issues related to ARM (32-bit) architecture Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces. Board: arm labels Aug 24, 2026
@github-actions

github-actions Bot commented Aug 24, 2026

Copy link
Copy Markdown

MemBrowse Memory Report

No memory changes detected for:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: arm Issues related to ARM (32-bit) architecture Area: Documentation Improvements or additions to documentation Board: arm Size: XL The size of the change in this PR is very large. Consider breaking down the PR into smaller pieces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants