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 eaaba5e0a195d67300b83863a9e12d126e7a1b56 Author: dechao_gong <[email protected]> AuthorDate: Mon Aug 17 11:07:00 2026 +0800 arch/arm/rtl8721f: add PWM master driver support Wire the shared Ameba PWM driver (arch/arm/src/common/ameba/ameba_pwm.c) to RTL8721F (amebagreen2). The chip spreads PWM across four four-channel timers (TIM4..TIM7); 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 RTL8721F specifics taken from the SDK fwlib headers: TIM4 at the non-secure base 0x41000000, 40 MHz input clock, IRQ 11 (TIMER4_IRQ), crossbar pad-mux codes 111..114 (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/rtl8721f/boards/rtl8721f_evb/index.rst | 19 +++++ arch/arm/src/rtl8721f/CMakeLists.txt | 13 ++++ arch/arm/src/rtl8721f/Make.defs | 4 + arch/arm/src/rtl8721f/ameba_board.mk | 9 +++ arch/arm/src/rtl8721f/ameba_pwm_chip.h | 90 ++++++++++++++++++++++ .../rtl8721f/rtl8721f_evb/configs/pwm/defconfig | 51 ++++++++++++ .../arm/rtl8721f/rtl8721f_evb/src/CMakeLists.txt | 7 +- boards/arm/rtl8721f/rtl8721f_evb/src/Makefile | 6 +- .../rtl8721f/rtl8721f_evb/src/rtl8721f_bringup.c | 10 +++ .../arm/rtl8721f/rtl8721f_evb/src/rtl8721f_pwm.c | 85 ++++++++++++++++++++ .../rtl8721f_evb/src/rtl8721f_rtl8721f_evb.h | 13 ++++ 11 files changed, 305 insertions(+), 2 deletions(-) diff --git a/Documentation/platforms/arm/rtl8721f/boards/rtl8721f_evb/index.rst b/Documentation/platforms/arm/rtl8721f/boards/rtl8721f_evb/index.rst index 5b2e26ef2ef..a3da36d03d6 100644 --- a/Documentation/platforms/arm/rtl8721f/boards/rtl8721f_evb/index.rst +++ b/Documentation/platforms/arm/rtl8721f/boards/rtl8721f_evb/index.rst @@ -40,6 +40,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 ================ @@ -128,6 +130,23 @@ spec lists for that controller. Exercise a bus with the tool:: nsh> spi exch -b 0 -x 4 deadbeef # full-duplex transfer on /dev/spi0 nsh> spi exch -b 1 -x 4 deadbeef # full-duplex transfer on /dev/spi1 +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/rtl8721f/rtl8721f_evb/src/rtl8721f_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 PA24 and channel 2 to PA25; 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/rtl8721f/CMakeLists.txt b/arch/arm/src/rtl8721f/CMakeLists.txt index 29b87c15c97..b27e3b4a4e0 100644 --- a/arch/arm/src/rtl8721f/CMakeLists.txt +++ b/arch/arm/src/rtl8721f/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}) @@ -132,6 +136,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/rtl8721f/Make.defs b/arch/arm/src/rtl8721f/Make.defs index d5da1572f1e..d415e3d7723 100644 --- a/arch/arm/src/rtl8721f/Make.defs +++ b/arch/arm/src/rtl8721f/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 RTL8721F SDK integration # diff --git a/arch/arm/src/rtl8721f/ameba_board.mk b/arch/arm/src/rtl8721f/ameba_board.mk index fb99412b3f1..b4d0b7f5a09 100644 --- a/arch/arm/src/rtl8721f/ameba_board.mk +++ b/arch/arm/src/rtl8721f/ameba_board.mk @@ -167,6 +167,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/rtl8721f/ameba_pwm_chip.h b/arch/arm/src/rtl8721f/ameba_pwm_chip.h new file mode 100644 index 00000000000..89c3763f905 --- /dev/null +++ b/arch/arm/src/rtl8721f/ameba_pwm_chip.h @@ -0,0 +1,90 @@ +/**************************************************************************** + * arch/arm/src/rtl8721f/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_RTL8721F_AMEBA_PWM_CHIP_H +#define __ARCH_ARM_SRC_RTL8721F_AMEBA_PWM_CHIP_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per-chip PWM wiring for RTL8721F (amebagreen2). 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), amebagreen2 spreads PWM across + * four timers of four channels each (TIM4..TIM7, 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 (0x51000000) 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 (111..114). + * - AMEBA_PWM_APBPERIPH / _CLK: the "function" and "clock" args to + * RCC_PeriphClockCmd(). Unlike amebadplus, amebagreen2 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 11 (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 0x41000000ul /* NON-secure TIM4 base */ +#define AMEBA_PWM_CLKFREQ 40000000ul /* TIM4 input clock (40 MHz) */ +#define AMEBA_PWM_IRQ 11 /* 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 amebagreen2 + * these are PINMUX_FUNCTION_TIM4_PWM0..3 (111..114). + */ + +#define AMEBA_PWM_PINMUX_FIDS { 111, 112, 113, 114 } + +/* APBPeriph_PWM0 (function) and APBPeriph_PWM0_CLOCK masks. The group + * selector (bit30) is 0; amebagreen2 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_RTL8721F_AMEBA_PWM_CHIP_H */ diff --git a/boards/arm/rtl8721f/rtl8721f_evb/configs/pwm/defconfig b/boards/arm/rtl8721f/rtl8721f_evb/configs/pwm/defconfig new file mode 100644 index 00000000000..069ddc9544a --- /dev/null +++ b/boards/arm/rtl8721f/rtl8721f_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="rtl8721f_evb" +CONFIG_ARCH_BOARD_RTL8721F_EVB=y +CONFIG_ARCH_CHIP="rtl8721f" +CONFIG_ARCH_CHIP_RTL8721F=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=401408 +CONFIG_RAM_START=0x20006000 +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/rtl8721f/rtl8721f_evb/src/CMakeLists.txt b/boards/arm/rtl8721f/rtl8721f_evb/src/CMakeLists.txt index 357bd6a37d1..0188e21f7e7 100644 --- a/boards/arm/rtl8721f/rtl8721f_evb/src/CMakeLists.txt +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/CMakeLists.txt @@ -38,12 +38,17 @@ if(CONFIG_AMEBA_SPI) list(APPEND SRCS rtl8721f_spi.c) endif() +if(CONFIG_AMEBA_PWM) + list(APPEND SRCS rtl8721f_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/rtl8721f/rtl8721f_evb/src/Makefile b/boards/arm/rtl8721f/rtl8721f_evb/src/Makefile index b14a234ad0c..308df01fcd2 100644 --- a/boards/arm/rtl8721f/rtl8721f_evb/src/Makefile +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/Makefile @@ -40,10 +40,14 @@ ifeq ($(CONFIG_AMEBA_SPI),y) CSRCS += rtl8721f_spi.c endif +ifeq ($(CONFIG_AMEBA_PWM),y) +CSRCS += rtl8721f_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/rtl8721f/rtl8721f_evb/src/rtl8721f_bringup.c b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_bringup.c index 7febad4029a..ab3b5bdb5cf 100644 --- a/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_bringup.c +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_bringup.c @@ -174,6 +174,16 @@ int rtl8721f_bringup(void) } #endif +#ifdef CONFIG_AMEBA_PWM + /* Register the board's PWM timer at /dev/pwm0. */ + + ret = rtl8721f_pwm_initialize(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: rtl8721f_pwm_initialize failed: %d\n", ret); + } +#endif + IPC_patch_function(rtos_critical_enter, rtos_critical_exit, AMEBA_RTOS_CRITICAL_SEMA); IPC_SEMDelayStub(rtos_time_delay_ms); diff --git a/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_pwm.c b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_pwm.c new file mode 100644 index 00000000000..3d64f8a2b21 --- /dev/null +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_pwm.c @@ -0,0 +1,85 @@ +/**************************************************************************** + * boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_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 "rtl8721f_rtl8721f_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(24), /* channel 1 -> PWM0 */ + AMEBA_PA(25), /* channel 2 -> PWM1 */ + AMEBA_PWM_PIN_NC, /* channel 3 unused */ + AMEBA_PWM_PIN_NC, /* channel 4 unused */ +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rtl8721f_pwm_initialize + * + * Description: + * Register the board's PWM timer at /dev/pwm0. + * + ****************************************************************************/ + +int rtl8721f_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/rtl8721f/rtl8721f_evb/src/rtl8721f_rtl8721f_evb.h b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_rtl8721f_evb.h index ba5fccc3adf..04633cd2a4a 100644 --- a/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_rtl8721f_evb.h +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_rtl8721f_evb.h @@ -135,6 +135,19 @@ int rtl8721f_i2c_initialize(void); int rtl8721f_spi_initialize(void); #endif +#ifdef CONFIG_AMEBA_PWM +/**************************************************************************** + * Name: rtl8721f_pwm_initialize + * + * Description: + * Register the board's PWM timer at /dev/pwm0 + * (boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_pwm.c). + * + ****************************************************************************/ + +int rtl8721f_pwm_initialize(void); +#endif + #ifdef CONFIG_RTL8721F_FLASH_FS /**************************************************************************** * Name: ameba_flash_fs_initialize
