This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 254680291170659754b002d9ed72574adbad05d8 Author: dechao_gong <[email protected]> AuthorDate: Mon Aug 17 11:06:47 2026 +0800 arch/arm/rtl8720f: add PWM master driver support Wire the shared Ameba PWM driver (arch/arm/src/common/ameba/ameba_pwm.c) to RTL8720F. The chip spreads PWM across several four-channel timers (TIM4/TIM5); this port drives TIM4 as the single time base with four compare channels, matching the shared driver's model. A new ameba_pwm_chip.h supplies the RTL8720F specifics taken from the SDK fwlib headers: TIM4 at the non-secure base 0x401c7000, 40 MHz input clock, IRQ 9 (TIMER4_IRQ), crossbar pad-mux codes 45..48 (PINMUX_FUNCTION_TIM4_PWM0..3) and the distinct function/clock enable bits (APBPeriph_PWM0 / APBPeriph_PWM0_CLOCK). The board registers one timer at /dev/pwm0 with channel 1 on PB18 and channel 2 on PB19 for the pwm example; edit the table to match a board's wiring. The common driver is not touched. Signed-off-by: dechao_gong <[email protected]> Assisted-by: Claude <[email protected]> --- .../arm/rtl8720f/boards/rtl8720f_evb/index.rst | 19 +++++ arch/arm/src/rtl8720f/CMakeLists.txt | 13 ++++ arch/arm/src/rtl8720f/Make.defs | 4 + arch/arm/src/rtl8720f/ameba_board.mk | 9 +++ arch/arm/src/rtl8720f/ameba_pwm_chip.h | 90 ++++++++++++++++++++++ .../rtl8720f/rtl8720f_evb/configs/pwm/defconfig | 51 ++++++++++++ .../arm/rtl8720f/rtl8720f_evb/src/CMakeLists.txt | 7 +- boards/arm/rtl8720f/rtl8720f_evb/src/Makefile | 6 +- .../rtl8720f/rtl8720f_evb/src/rtl8720f_bringup.c | 10 +++ .../arm/rtl8720f/rtl8720f_evb/src/rtl8720f_pwm.c | 85 ++++++++++++++++++++ .../rtl8720f_evb/src/rtl8720f_rtl8720f_evb.h | 12 +++ 11 files changed, 304 insertions(+), 2 deletions(-) diff --git a/Documentation/platforms/arm/rtl8720f/boards/rtl8720f_evb/index.rst b/Documentation/platforms/arm/rtl8720f/boards/rtl8720f_evb/index.rst index ad657b4a4b5..64fd298d08f 100644 --- a/Documentation/platforms/arm/rtl8720f/boards/rtl8720f_evb/index.rst +++ b/Documentation/platforms/arm/rtl8720f/boards/rtl8720f_evb/index.rst @@ -39,6 +39,8 @@ Supported in this NuttX port: on the SDK fwlib register layer * SPI master buses exposed as ``/dev/spiN`` character devices, driven directly on the SDK fwlib register layer +* PWM output exposed as a ``/dev/pwm0`` character device, driven directly on + the SDK fwlib timer register layer Buttons and LEDs ================ @@ -125,6 +127,23 @@ actually routes to the controller. Exercise a bus with the tool:: nsh> spi exch -b 0 -x 4 deadbeef # full-duplex transfer on /dev/spi0 +pwm +--- + +Minimal NSH with the PWM driver and the ``pwm`` example +(``examples/pwm``) enabled (no Wi-Fi). The board registers one timer at +``/dev/pwm0`` (see ``boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_pwm.c``): +TIM4 drives up to four compare channels off one shared time base, so every +channel shares one frequency and each carries its own duty cycle. The example +table routes channel 1 to PA23 and channel 2 to PA24; edit it -- one pad per +channel, ``AMEBA_PWM_PIN_NC`` for the unused ones -- to match a board's +wiring. The pads use the same ``AMEBA_PA()`` / ``AMEBA_PB()`` encoding as the +GPIO table and are muxed to the PWM function through the crossbar. Set +``CONFIG_PWM_NCHANNELS`` to the number of channels used. Exercise it with the +example:: + + nsh> pwm -d 25 -f 1000 # 1 kHz, 25% duty on /dev/pwm0 + nsh --- diff --git a/arch/arm/src/rtl8720f/CMakeLists.txt b/arch/arm/src/rtl8720f/CMakeLists.txt index 4f705205b7a..7918bc09a10 100644 --- a/arch/arm/src/rtl8720f/CMakeLists.txt +++ b/arch/arm/src/rtl8720f/CMakeLists.txt @@ -62,6 +62,10 @@ if(CONFIG_AMEBA_SPI) list(APPEND SRCS ${AMEBA_COMMON}/ameba_spi.c) endif() +if(CONFIG_AMEBA_PWM) + list(APPEND SRCS ${AMEBA_COMMON}/ameba_pwm.c) +endif() + target_include_directories(arch PRIVATE ${AMEBA_COMMON}) target_sources(arch PRIVATE ${SRCS}) @@ -131,6 +135,15 @@ if(CONFIG_AMEBA_SPI) list(APPEND AMEBA_FWLIB_SRCS ${AMEBA_SOC}/fwlib/ram_common/ameba_spi.c) endif() +# PWM timer register layer. The time-base entry points (RTIM_TimeBaseInit/ +# StructInit/Cmd) are in ROM, but the compare/period helpers the PWM driver +# (arch/.../common/ameba/ameba_pwm.c) calls -- RTIM_CCStructInit/CCxInit/ +# CCRxSet/CCxCmd/ChangePeriod/PrescalerConfig -- are compiled from this RAM +# source and linked in (--gc-sections drops the unused input-capture paths). +if(CONFIG_AMEBA_PWM) + list(APPEND AMEBA_FWLIB_SRCS ${AMEBA_SOC}/fwlib/ram_common/ameba_tim.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 7f56d819fe4..ba3dc354b81 100644 --- a/arch/arm/src/rtl8720f/Make.defs +++ b/arch/arm/src/rtl8720f/Make.defs @@ -72,6 +72,10 @@ ifeq ($(CONFIG_AMEBA_SPI),y) CHIP_CSRCS += ameba_spi.c endif +ifeq ($(CONFIG_AMEBA_PWM),y) +CHIP_CSRCS += ameba_pwm.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 7d686573d29..9931c1fe317 100644 --- a/arch/arm/src/rtl8720f/ameba_board.mk +++ b/arch/arm/src/rtl8720f/ameba_board.mk @@ -144,6 +144,15 @@ ifeq ($(CONFIG_AMEBA_SPI),y) AMEBA_FWLIB_SRCS += $(AMEBA_SOC)/fwlib/ram_common/ameba_spi.c endif +# PWM timer register layer. The time-base entry points (RTIM_TimeBaseInit/ +# StructInit/Cmd) are in ROM, but the compare/period helpers the PWM driver +# (arch/.../common/ameba/ameba_pwm.c) calls -- RTIM_CCStructInit/CCxInit/ +# CCRxSet/CCxCmd/ChangePeriod/PrescalerConfig -- are compiled from this RAM +# source and linked in (--gc-sections drops the unused input-capture paths). +ifeq ($(CONFIG_AMEBA_PWM),y) +AMEBA_FWLIB_SRCS += $(AMEBA_SOC)/fwlib/ram_common/ameba_tim.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_pwm_chip.h b/arch/arm/src/rtl8720f/ameba_pwm_chip.h new file mode 100644 index 00000000000..0a0f105af4c --- /dev/null +++ b/arch/arm/src/rtl8720f/ameba_pwm_chip.h @@ -0,0 +1,90 @@ +/**************************************************************************** + * arch/arm/src/rtl8720f/ameba_pwm_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_PWM_CHIP_H +#define __ARCH_ARM_SRC_RTL8720F_AMEBA_PWM_CHIP_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per-chip PWM wiring for RTL8720F. The shared driver + * (arch/arm/src/common/ameba/ameba_pwm.c) includes this header to learn + * which timer generates PWM, how many output channels it drives, its + * register base, input clock, peripheral-clock masks and crossbar pad-mux + * codes. The shared driver is never edited (it reads only the macros below + * -- the pad-mux codes are a per-channel table, NOT computed). + * + * Unlike amebadplus (one 8-channel TIM8), RTL8720F spreads PWM across + * several timers of four channels each (TIM4/TIM5, see IS_TIM_PWM_TIM in the + * SDK ameba_pwmtimer.h). This port drives TIM4 as the single time base with + * four compare channels, matching the shared driver's model. All values + * below are taken from the SDK fwlib headers (verified, not guessed): + * + * - AMEBA_PWM_TIMER_IDX / _NCHAN: TIM4 is timer index 4 with four CCRs. + * - AMEBA_PWM_BASE: the NON-secure TIM4 base (TIMER4_REG_BASE in the SDK + * hal_platform.h); the secure alias (0x501C7000) must not be used from + * the non-secure world the KM4 runs in (same rule as the GPIO/SPI + * drivers). + * - AMEBA_PWM_CLKFREQ: TIM4 clocked from the 40 MHz XTAL (IS_TIM_40M_TIM + * in the SDK); f_out = CLKFREQ / ((PSC + 1) * (ARR + 1)) with 16-bit + * PSC and ARR. + * - AMEBA_PWM_PINMUX_FIDS: the per-channel crossbar function codes + * PINMUX_FUNCTION_TIM4_PWM0..3 (45..48). + * - AMEBA_PWM_APBPERIPH / _CLK: the "function" and "clock" args to + * RCC_PeriphClockCmd(). Unlike amebadplus, RTL8720F uses distinct + * function-enable and clock-enable bits (APBPeriph_PWM0 = bit23, + * APBPeriph_PWM0_CLOCK = bit24, both in group 0). + * - AMEBA_PWM_IRQ: RTIM_TimeBaseInit() takes it even though this polling + * driver registers no callback (NULL); TIM4 is IRQ 9 (TIMER4_IRQ). + */ + +#define AMEBA_PWM_TIMER_IDX 4 /* TIM4 (IS_TIM_PWM_TIM) */ +#define AMEBA_PWM_NCHAN 4 /* CCR0..CCR3 (PWM_CHAN_MAX) */ +#define AMEBA_PWM_BASE 0x401c7000ul /* NON-secure TIM4 base */ +#define AMEBA_PWM_CLKFREQ 40000000ul /* TIM4 input clock (40 MHz) */ +#define AMEBA_PWM_IRQ 9 /* TIMER4_IRQ */ + +/* Crossbar pad-mux function code per channel, indexed by channel number + * minus one (channel n uses AMEBA_PWM_PINMUX_FIDS[n - 1]). On RTL8720F + * these are PINMUX_FUNCTION_TIM4_PWM0..3 (45..48). + */ + +#define AMEBA_PWM_PINMUX_FIDS { 45, 46, 47, 48 } + +/* APBPeriph_PWM0 (function) and APBPeriph_PWM0_CLOCK masks. The group + * selector (bit30) is 0; RTL8720F splits function-enable (bit23) and + * clock-enable (bit24) into distinct bits. + */ + +#define AMEBA_PWM_APBPERIPH (((uint32_t)0 << 30) | ((uint32_t)1 << 23)) +#define AMEBA_PWM_APBPERIPH_CLK (((uint32_t)0 << 30) | ((uint32_t)1 << 24)) + +#endif /* __ARCH_ARM_SRC_RTL8720F_AMEBA_PWM_CHIP_H */ diff --git a/boards/arm/rtl8720f/rtl8720f_evb/configs/pwm/defconfig b/boards/arm/rtl8720f/rtl8720f_evb/configs/pwm/defconfig new file mode 100644 index 00000000000..2c6728db726 --- /dev/null +++ b/boards/arm/rtl8720f/rtl8720f_evb/configs/pwm/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_PWM=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_HELLO=y +CONFIG_EXAMPLES_PWM=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_PWM_NCHANNELS=2 +CONFIG_RAM_SIZE=262144 +CONFIG_RAM_START=0x30008000 +CONFIG_RR_INTERVAL=200 +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 bb933ffb1c1..e09dd066457 100644 --- a/boards/arm/rtl8720f/rtl8720f_evb/src/CMakeLists.txt +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/CMakeLists.txt @@ -38,12 +38,17 @@ if(CONFIG_AMEBA_SPI) list(APPEND SRCS rtl8720f_spi.c) endif() +if(CONFIG_AMEBA_PWM) + list(APPEND SRCS rtl8720f_pwm.c) +endif() + target_sources(board PRIVATE ${SRCS}) if(CONFIG_AMEBA_GPIO OR CONFIG_AMEBA_UART OR CONFIG_AMEBA_I2C - OR CONFIG_AMEBA_SPI) + OR CONFIG_AMEBA_SPI + OR CONFIG_AMEBA_PWM) # 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 17f442412dd..08dd553040d 100644 --- a/boards/arm/rtl8720f/rtl8720f_evb/src/Makefile +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/Makefile @@ -40,10 +40,14 @@ ifeq ($(CONFIG_AMEBA_SPI),y) CSRCS += rtl8720f_spi.c endif +ifeq ($(CONFIG_AMEBA_PWM),y) +CSRCS += rtl8720f_pwm.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),) +ifneq ($(CONFIG_AMEBA_GPIO)$(CONFIG_AMEBA_UART)$(CONFIG_AMEBA_I2C)$(CONFIG_AMEBA_SPI)$(CONFIG_AMEBA_PWM),) 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 f559600d244..aab22384188 100644 --- a/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_bringup.c +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_bringup.c @@ -160,6 +160,16 @@ int rtl8720f_bringup(void) } #endif +#ifdef CONFIG_AMEBA_PWM + /* Register the board's PWM timer at /dev/pwm0. */ + + ret = rtl8720f_pwm_initialize(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: rtl8720f_pwm_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_pwm.c b/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_pwm.c new file mode 100644 index 00000000000..9d13e5e30d7 --- /dev/null +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_pwm.c @@ -0,0 +1,85 @@ +/**************************************************************************** + * boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_pwm.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 <nuttx/config.h> + +#include <sys/param.h> +#include <syslog.h> +#include <errno.h> + +#include "ameba_gpio.h" +#include "ameba_pwm.h" +#include "rtl8720f_rtl8720f_evb.h" + +#ifdef CONFIG_AMEBA_PWM + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Output pad for each PWM hardware channel (0..AMEBA_PWM_NCHAN-1), all off + * the one shared time base. Channels the board does not use carry + * AMEBA_PWM_PIN_NC. The pads are examples used by the `pwm` config + * (examples/pwm); adjust them to match your board's wiring. Any pad can be + * routed to any channel through the crossbar, so only this table changes. + */ + +static const uint8_t g_pwm_pins[] = +{ + AMEBA_PA(23), /* channel 1 -> PWM0 */ + AMEBA_PA(24), /* channel 2 -> PWM1 */ + AMEBA_PWM_PIN_NC, /* channel 3 unused */ + AMEBA_PWM_PIN_NC, /* channel 4 unused */ +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rtl8720f_pwm_initialize + * + * Description: + * Register the board's PWM timer at /dev/pwm0. + * + ****************************************************************************/ + +int rtl8720f_pwm_initialize(void) +{ + int ret; + + ret = ameba_pwm_register("/dev/pwm0", g_pwm_pins, nitems(g_pwm_pins)); + if (ret < 0) + { + syslog(LOG_ERR, + "ERROR: ameba_pwm_register(/dev/pwm0) failed: %d\n", ret); + return ret; + } + + return OK; +} + +#endif /* CONFIG_AMEBA_PWM */ 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 d0c201feda4..d025a111b9b 100644 --- a/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtl8720f_evb.h +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtl8720f_evb.h @@ -67,6 +67,18 @@ int rtl8720f_bringup(void); int rtl8720f_spi_initialize(void); #endif +#ifdef CONFIG_AMEBA_PWM +/**************************************************************************** + * Name: rtl8720f_pwm_initialize + * + * Description: + * Register the board's PWM timer at /dev/pwm0. + * + ****************************************************************************/ + +int rtl8720f_pwm_initialize(void); +#endif + #ifdef CONFIG_RTL8720F_WIFI /**************************************************************************** * Name: rtl8720f_wifi_initialize
