From 01041d2b1a6a09b8631cb09859b27e43d109db07 Mon Sep 17 00:00:00 2001 From: dechao_gong Date: Mon, 24 Aug 2026 12:38:57 +0800 Subject: [PATCH] arch/arm/rtl8720f: add RTC driver support 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 Assisted-by: Claude --- .../rtl8720f/boards/rtl8720f_evb/index.rst | 16 ++++ arch/arm/src/rtl8720f/CMakeLists.txt | 11 +++ arch/arm/src/rtl8720f/Make.defs | 4 + arch/arm/src/rtl8720f/ameba_board.mk | 9 ++ arch/arm/src/rtl8720f/ameba_rtc_chip.h | 85 +++++++++++++++++++ .../rtl8720f_evb/configs/rtc/defconfig | 51 +++++++++++ .../rtl8720f/rtl8720f_evb/src/CMakeLists.txt | 7 +- boards/arm/rtl8720f/rtl8720f_evb/src/Makefile | 6 +- .../rtl8720f_evb/src/rtl8720f_bringup.c | 10 +++ .../rtl8720f/rtl8720f_evb/src/rtl8720f_rtc.c | 66 ++++++++++++++ .../rtl8720f_evb/src/rtl8720f_rtl8720f_evb.h | 13 +++ 11 files changed, 276 insertions(+), 2 deletions(-) create mode 100644 arch/arm/src/rtl8720f/ameba_rtc_chip.h create mode 100644 boards/arm/rtl8720f/rtl8720f_evb/configs/rtc/defconfig create mode 100644 boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtc.c diff --git a/Documentation/platforms/arm/rtl8720f/boards/rtl8720f_evb/index.rst b/Documentation/platforms/arm/rtl8720f/boards/rtl8720f_evb/index.rst index b917e20a053..8a3231d9752 100644 --- a/Documentation/platforms/arm/rtl8720f/boards/rtl8720f_evb/index.rst +++ b/Documentation/platforms/arm/rtl8720f/boards/rtl8720f_evb/index.rst @@ -43,6 +43,8 @@ Supported in this NuttX port: the SDK fwlib timer register layer * ADC channels exposed as an ``/dev/adc0`` character device, driven directly on the SDK fwlib register layer +* On-chip RTC exposed as a ``/dev/rtc0`` date/time character device with + alarm support, driven directly on the SDK fwlib register layer Buttons and LEDs ================ @@ -161,6 +163,20 @@ example:: nsh> adc -n 1 # one sweep of /dev/adc0 +rtc +--- + +Minimal NSH with the on-chip RTC driver and the ``alarm`` example enabled +(no Wi-Fi). The RTC is registered at ``/dev/rtc0`` from the board bring-up +(``boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtc.c``); it has no board +wiring (it is an internal clock). The hardware stores year + day-of-year, so +the shared driver bridges to a full calendar. Read and set the clock with the +NSH ``date`` command, and arm a one-shot wakeup with the example:: + + nsh> date # read /dev/rtc0 + nsh> date -s "Jun 16 12:00:00 2026" # set the RTC + nsh> alarm 10 # fire an alarm in 10 seconds + nsh --- diff --git a/arch/arm/src/rtl8720f/CMakeLists.txt b/arch/arm/src/rtl8720f/CMakeLists.txt index a9bfd641aac..3b78ed7dbdc 100644 --- a/arch/arm/src/rtl8720f/CMakeLists.txt +++ b/arch/arm/src/rtl8720f/CMakeLists.txt @@ -70,6 +70,10 @@ if(CONFIG_AMEBA_ADC) list(APPEND SRCS ${AMEBA_COMMON}/ameba_adc.c) endif() +if(CONFIG_AMEBA_RTC) + list(APPEND SRCS ${AMEBA_COMMON}/ameba_rtc.c) +endif() + target_include_directories(arch PRIVATE ${AMEBA_COMMON}) target_sources(arch PRIVATE ${SRCS}) @@ -156,6 +160,13 @@ if(CONFIG_AMEBA_ADC) list(APPEND AMEBA_FWLIB_SRCS ${AMEBA_SOC}/fwlib/ram_common/ameba_adc.c) endif() +# RTC register layer. The RTC driver (arch/.../common/ameba/ameba_rtc.c) calls +# the fwlib RTC API; the data tables and helpers it indexes live in this RAM +# source and must be compiled in (mirrors the make build's ameba_board.mk). +if(CONFIG_AMEBA_RTC) + list(APPEND AMEBA_FWLIB_SRCS ${AMEBA_SOC}/fwlib/ram_common/ameba_rtc.c) +endif() + # Silence a couple of warnings the vendored SDK sources trip under NuttX's # warning set, scoped to this fwlib compile only (never relaxing NuttX's own): # -Wno-int-conversion: the SDK passes NULL to irq_register()'s u32 "Data" diff --git a/arch/arm/src/rtl8720f/Make.defs b/arch/arm/src/rtl8720f/Make.defs index 58a8959944d..14ae3933d18 100644 --- a/arch/arm/src/rtl8720f/Make.defs +++ b/arch/arm/src/rtl8720f/Make.defs @@ -80,6 +80,10 @@ ifeq ($(CONFIG_AMEBA_ADC),y) CHIP_CSRCS += ameba_adc.c endif +ifeq ($(CONFIG_AMEBA_RTC),y) +CHIP_CSRCS += ameba_rtc.c +endif + ############################################################################ # Realtek RTL8720F SDK integration # diff --git a/arch/arm/src/rtl8720f/ameba_board.mk b/arch/arm/src/rtl8720f/ameba_board.mk index 8384e73b29e..77ec3645d81 100644 --- a/arch/arm/src/rtl8720f/ameba_board.mk +++ b/arch/arm/src/rtl8720f/ameba_board.mk @@ -161,6 +161,15 @@ ifeq ($(CONFIG_AMEBA_ADC),y) AMEBA_FWLIB_SRCS += $(AMEBA_SOC)/fwlib/ram_common/ameba_adc.c endif +# RTC register layer. The whole fwlib RTC API the RTC driver +# (arch/.../common/ameba/ameba_rtc.c) calls -- RTC_Init/StructInit, +# RTC_SetTime/GetTime and RTC_SetAlarm/GetAlarm/AlarmStructInit/AlarmCmd/ +# AlarmClear -- is compiled from this RAM source (the _LONG_CALL_ prototypes +# resolve here, not to ROM) and must be linked in. +ifeq ($(CONFIG_AMEBA_RTC),y) +AMEBA_FWLIB_SRCS += $(AMEBA_SOC)/fwlib/ram_common/ameba_rtc.c +endif + # -Wno-int-conversion: the vendored SDK passes NULL to irq_register()'s u32 # "Data" (interrupt context) argument in many places -- an intentional # NULL-as-context idiom. Silence -Wint-conversion for the SDK fwlib sources diff --git a/arch/arm/src/rtl8720f/ameba_rtc_chip.h b/arch/arm/src/rtl8720f/ameba_rtc_chip.h new file mode 100644 index 00000000000..2b5178a01bf --- /dev/null +++ b/arch/arm/src/rtl8720f/ameba_rtc_chip.h @@ -0,0 +1,85 @@ +/**************************************************************************** + * arch/arm/src/rtl8720f/ameba_rtc_chip.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +#ifndef __ARCH_ARM_SRC_RTL8720F_AMEBA_RTC_CHIP_H +#define __ARCH_ARM_SRC_RTL8720F_AMEBA_RTC_CHIP_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per-chip RTC wiring for RTL8720F. The shared driver + * (arch/arm/src/common/ameba/ameba_rtc.c) includes this header to learn the + * RTC interrupt line, the peripheral-clock masks and the calendar base year. + * A port to another Ameba chip supplies a same-named header on the chip + * include path; the shared driver is never edited -- it reads only the + * macros below, and it drives the RTC through the SDK fwlib API, which takes + * no register base (it selects the secure/non-secure alias internally), so + * no register address appears here at all. + * + * The RTC register model, structures and constants (year + day-of-year time, + * RTC_Format_BIN, the alarm mask values, RTC_BASE_YEAR = 1900) are identical + * on every current Ameba chip (verified against amebadplus/amebalite/ + * amebasmart/amebagreen2/RTL8720F fwlib ameba_rtc.h), so they live in the + * shared driver. The one thing that genuinely differs per chip is the RTC + * interrupt vector, so only that is abstracted here: + * + * chip RTC_IRQ vector (SDK ameba_vector_table.h) + * ----------- ----------------------------------------- + * amebadplus 46 + * amebalite 53 + * amebasmart 12 + * amebagreen2 41 + * RTL8720F 33 (this header: RTL8720F_IRQ_RTC) + * + * A new chip only edits this header: AMEBA_RTC_IRQ carries the NuttX IRQ + * number of its RTC line -- see the ADC/PWM chip headers for the same + * data-driven, never-computed pattern. + */ + +#define AMEBA_RTC_IRQ RTL8720F_IRQ_RTC + +/* APBPeriph_RTC (function) and APBPeriph_RTC_CLOCK masks, from the SDK + * sysreg_aon.h: bit group selector (3 << 30) plus the RTC enable bit + * (1 << 3). Both are equal on this chip. The driver gates the RTC clock + * with these before the first fwlib call. + */ + +#define AMEBA_RTC_APBPERIPH (((uint32_t)3 << 30) | ((uint32_t)1 << 3)) +#define AMEBA_RTC_APBPERIPH_CLK (((uint32_t)3 << 30) | ((uint32_t)1 << 3)) + +/* Calendar base year the fwlib RTC uses for RTC_TimeTypeDef.RTC_Year + * (RTC_BASE_YEAR); the hardware stores year + day-of-year, not month/day. + */ + +#define AMEBA_RTC_BASE_YEAR 1900 + +#endif /* __ARCH_ARM_SRC_RTL8720F_AMEBA_RTC_CHIP_H */ diff --git a/boards/arm/rtl8720f/rtl8720f_evb/configs/rtc/defconfig b/boards/arm/rtl8720f/rtl8720f_evb/configs/rtc/defconfig new file mode 100644 index 00000000000..bae9b3c8c57 --- /dev/null +++ b/boards/arm/rtl8720f/rtl8720f_evb/configs/rtc/defconfig @@ -0,0 +1,51 @@ +# +# This file is autogenerated: PLEASE DO NOT EDIT IT. +# +# You can use "make menuconfig" to make any modifications to the installed .config file. +# You can then do "make savedefconfig" to generate a new defconfig file that includes your +# modifications. +# +# CONFIG_DEBUG_WARN is not set +CONFIG_AMEBA_RTC=y +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="rtl8720f_evb" +CONFIG_ARCH_BOARD_RTL8720F_EVB=y +CONFIG_ARCH_CHIP="rtl8720f" +CONFIG_ARCH_CHIP_RTL8720F=y +CONFIG_ARCH_INTERRUPTSTACK=2048 +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARMV8M_SYSTICK=y +CONFIG_BUILTIN=y +CONFIG_DEBUG_ASSERTIONS=y +CONFIG_DEBUG_FEATURES=y +CONFIG_DEBUG_FULLOPT=y +CONFIG_DEBUG_SYMBOLS=y +CONFIG_DEFAULT_TASK_STACKSIZE=4096 +CONFIG_EXAMPLES_ALARM=y +CONFIG_EXAMPLES_HELLO=y +CONFIG_FS_PROCFS=y +CONFIG_FS_TMPFS=y +CONFIG_IDLETHREAD_STACKSIZE=4096 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_LIBC_MEMFD_ERROR=y +CONFIG_MM_DEFAULT_ALIGNMENT=32 +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_READLINE=y +CONFIG_PREALLOC_TIMERS=4 +CONFIG_RAM_SIZE=262144 +CONFIG_RAM_START=0x30008000 +CONFIG_RR_INTERVAL=200 +CONFIG_RTC_ALARM=y +CONFIG_SCHED_HPWORK=y +CONFIG_SCHED_HPWORKPRIORITY=192 +CONFIG_SCHED_LPWORK=y +CONFIG_STACK_COLORATION=y +CONFIG_START_DAY=16 +CONFIG_START_MONTH=6 +CONFIG_START_YEAR=2026 +CONFIG_SYSTEM_NSH=y +CONFIG_SYSTEM_NSH_STACKSIZE=2500 +CONFIG_TIMER=y +CONFIG_TIMER_ARCH=y +CONFIG_USEC_PER_TICK=1000 diff --git a/boards/arm/rtl8720f/rtl8720f_evb/src/CMakeLists.txt b/boards/arm/rtl8720f/rtl8720f_evb/src/CMakeLists.txt index 514f1b66d96..b590ce55a80 100644 --- a/boards/arm/rtl8720f/rtl8720f_evb/src/CMakeLists.txt +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/CMakeLists.txt @@ -46,6 +46,10 @@ if(CONFIG_AMEBA_ADC) list(APPEND SRCS rtl8720f_adc.c) endif() +if(CONFIG_AMEBA_RTC) + list(APPEND SRCS rtl8720f_rtc.c) +endif() + target_sources(board PRIVATE ${SRCS}) if(CONFIG_AMEBA_GPIO @@ -53,7 +57,8 @@ if(CONFIG_AMEBA_GPIO OR CONFIG_AMEBA_I2C OR CONFIG_AMEBA_SPI OR CONFIG_AMEBA_PWM - OR CONFIG_AMEBA_ADC) + OR CONFIG_AMEBA_ADC + OR CONFIG_AMEBA_RTC) # The board pin tables pull in the shared drivers' public headers from # arch/arm/src/common/ameba/, not on the default board include path. target_include_directories(board diff --git a/boards/arm/rtl8720f/rtl8720f_evb/src/Makefile b/boards/arm/rtl8720f/rtl8720f_evb/src/Makefile index 8e0f4508014..617e9d4767f 100644 --- a/boards/arm/rtl8720f/rtl8720f_evb/src/Makefile +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/Makefile @@ -48,10 +48,14 @@ ifeq ($(CONFIG_AMEBA_ADC),y) CSRCS += rtl8720f_adc.c endif +ifeq ($(CONFIG_AMEBA_RTC),y) +CSRCS += rtl8720f_rtc.c +endif + # The board pin tables pull in the shared drivers' public headers from # arch/arm/src/common/ameba/, which is not on the default board include path. -ifneq ($(CONFIG_AMEBA_GPIO)$(CONFIG_AMEBA_UART)$(CONFIG_AMEBA_I2C)$(CONFIG_AMEBA_SPI)$(CONFIG_AMEBA_PWM)$(CONFIG_AMEBA_ADC),) +ifneq ($(CONFIG_AMEBA_GPIO)$(CONFIG_AMEBA_UART)$(CONFIG_AMEBA_I2C)$(CONFIG_AMEBA_SPI)$(CONFIG_AMEBA_PWM)$(CONFIG_AMEBA_ADC)$(CONFIG_AMEBA_RTC),) CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)arm$(DELIM)src$(DELIM)common$(DELIM)ameba endif diff --git a/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_bringup.c b/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_bringup.c index 3f5667edd89..ac3e232c9e2 100644 --- a/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_bringup.c +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_bringup.c @@ -180,6 +180,16 @@ int rtl8720f_bringup(void) } #endif +#ifdef CONFIG_AMEBA_RTC + /* Register the board's RTC at /dev/rtc0. */ + + ret = rtl8720f_rtc_initialize(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: rtl8720f_rtc_initialize failed: %d\n", ret); + } +#endif + /* Install the inter-core HW IPC-semaphore RTOS hooks LAST -- after all the * flash / WHC bring-up above, and just before this (board_late_initialize) * path returns and nx_start() hands off to the init task. diff --git a/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtc.c b/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtc.c new file mode 100644 index 00000000000..95dda28d706 --- /dev/null +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtc.c @@ -0,0 +1,66 @@ +/**************************************************************************** + * boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtc.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. The + * ASF licenses this file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance with the + * License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include + +#include "ameba_rtc.h" +#include "rtl8720f_rtl8720f_evb.h" + +#ifdef CONFIG_AMEBA_RTC + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rtl8720f_rtc_initialize + * + * Description: + * Bring up the on-chip RTC and register it at /dev/rtc0. The RTC has no + * board wiring (it is an internal clock), so this simply defers to the + * shared driver. + * + ****************************************************************************/ + +int rtl8720f_rtc_initialize(void) +{ + int ret; + + ret = ameba_rtc_initialize(); + if (ret < 0) + { + syslog(LOG_ERR, + "ERROR: ameba_rtc_initialize(/dev/rtc0) failed: %d\n", ret); + return ret; + } + + return OK; +} + +#endif /* CONFIG_AMEBA_RTC */ diff --git a/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtl8720f_evb.h b/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtl8720f_evb.h index e7fe186adbc..0e2e940bfbb 100644 --- a/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtl8720f_evb.h +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtl8720f_evb.h @@ -91,6 +91,19 @@ int rtl8720f_pwm_initialize(void); int rtl8720f_adc_initialize(void); #endif +#ifdef CONFIG_AMEBA_RTC +/**************************************************************************** + * Name: rtl8720f_rtc_initialize + * + * Description: + * Register the board's RTC at /dev/rtc0 + * (boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtc.c). + * + ****************************************************************************/ + +int rtl8720f_rtc_initialize(void); +#endif + #ifdef CONFIG_RTL8720F_WIFI /**************************************************************************** * Name: rtl8720f_wifi_initialize