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 36a971567ac706b86fb9e94cceeb3c81083da344 Author: dechao_gong <[email protected]> AuthorDate: Thu Aug 13 10:21:18 2026 +0800 arch/arm/rtl8721f: add SPI master driver support Wire the shared Ameba SPI (DesignWare SSI) driver (arch/arm/src/common/ameba/ameba_spi.c) into the RTL8721F (amebagreen2) build and expose the SPI0/SPI1 masters at /dev/spiN. Add the per-chip ameba_spi_chip.h with the amebagreen2 controller bases (0x40121000 / 0x40122000, non-secure aliases), the group-0 SPI clock masks (bit14/bit15), and the per-signal crossbar pad-mux codes (SPI0 75/76/77/78, SPI1 79/80/81/82). The SSI ip_clk is the PERI_HCLK-domain clock, which the amebagreen2 fwlib exposes directly through HPERI_ClkGet(), so AMEBA_SPI_IPCLK() is a single ROM call rather than the register poking the other ICs need. Compile the common driver and the SDK fwlib SSI RAM source under CONFIG_AMEBA_SPI, register the bus in the board bring-up, and add an "spi" board configuration exercising the system/spi spitool. Assisted-by: Claude <[email protected]> Signed-off-by: dechao_gong <[email protected]> --- .../arm/rtl8721f/boards/rtl8721f_evb/index.rst | 21 +++++ arch/arm/src/rtl8721f/CMakeLists.txt | 12 +++ arch/arm/src/rtl8721f/Make.defs | 4 + arch/arm/src/rtl8721f/ameba_board.mk | 9 ++ arch/arm/src/rtl8721f/ameba_spi_chip.h | 101 ++++++++++++++++++++ .../rtl8721f/rtl8721f_evb/configs/spi/defconfig | 50 ++++++++++ .../arm/rtl8721f/rtl8721f_evb/src/CMakeLists.txt | 7 +- boards/arm/rtl8721f/rtl8721f_evb/src/Makefile | 6 +- .../rtl8721f/rtl8721f_evb/src/rtl8721f_bringup.c | 10 ++ .../rtl8721f_evb/src/rtl8721f_rtl8721f_evb.h | 13 +++ .../arm/rtl8721f/rtl8721f_evb/src/rtl8721f_spi.c | 103 +++++++++++++++++++++ 11 files changed, 334 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 5c8703388c5..5b2e26ef2ef 100644 --- a/Documentation/platforms/arm/rtl8721f/boards/rtl8721f_evb/index.rst +++ b/Documentation/platforms/arm/rtl8721f/boards/rtl8721f_evb/index.rst @@ -38,6 +38,8 @@ Supported in this NuttX port: directly on the SDK fwlib register layer * I2C master buses exposed as ``/dev/i2cN`` character devices, driven directly on the SDK fwlib register layer +* SPI master buses exposed as ``/dev/spiN`` character devices, driven directly + on the SDK fwlib register layer Buttons and LEDs ================ @@ -107,6 +109,25 @@ external pull-ups on SCL/SDA. Probe a bus with the tool:: nsh> i2c dev -b 0 0x03 0x77 # scan /dev/i2c0 for devices +spi +--- + +Minimal NSH with the SPI master driver and the ``spi`` tool +(``system/spi``) enabled (no Wi-Fi). The board registers two buses from its +table (see ``boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_spi.c``): SPI0 at +``/dev/spi0`` with CLK/MOSI/MISO on PA14/PA15/PA16 and a software chip-select +on PA17, and SPI1 at ``/dev/spi1`` with CLK/MOSI/MISO on PB13/PB14/PB15 and a +software chip-select on PB16. Edit that table -- controller, CLK/MOSI/MISO +pads and CS pad -- 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 SPI function through the SDK ROM, while the chip-select is driven as a +plain GPIO. Note that SPI0 and SPI1 route to different pad groups in the chip +pin-mux spec (SPI0 on the PA group, SPI1 on the PB/PC group); pick pads the +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 + nsh --- diff --git a/arch/arm/src/rtl8721f/CMakeLists.txt b/arch/arm/src/rtl8721f/CMakeLists.txt index 62f32a6cae1..29b87c15c97 100644 --- a/arch/arm/src/rtl8721f/CMakeLists.txt +++ b/arch/arm/src/rtl8721f/CMakeLists.txt @@ -58,6 +58,10 @@ if(CONFIG_AMEBA_I2C) list(APPEND SRCS ${AMEBA_COMMON}/ameba_i2c.c) endif() +if(CONFIG_AMEBA_SPI) + list(APPEND SRCS ${AMEBA_COMMON}/ameba_spi.c) +endif() + target_include_directories(arch PRIVATE ${AMEBA_COMMON}) target_sources(arch PRIVATE ${SRCS}) @@ -120,6 +124,14 @@ if(CONFIG_AMEBA_I2C) list(APPEND AMEBA_FWLIB_SRCS ${AMEBA_SOC}/fwlib/ram_common/ameba_i2c.c) endif() +# SPI (DesignWare SSI) register layer. The SPI driver +# (arch/.../common/ameba/ameba_spi.c) calls the fwlib SSI API; the data tables +# and helpers it indexes live in this RAM source and must be compiled in +# (--gc-sections drops the unused DMA/interrupt helpers). +if(CONFIG_AMEBA_SPI) + list(APPEND AMEBA_FWLIB_SRCS ${AMEBA_SOC}/fwlib/ram_common/ameba_spi.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 5958c2b69ab..d5da1572f1e 100644 --- a/arch/arm/src/rtl8721f/Make.defs +++ b/arch/arm/src/rtl8721f/Make.defs @@ -68,6 +68,10 @@ ifeq ($(CONFIG_AMEBA_I2C),y) CHIP_CSRCS += ameba_i2c.c endif +ifeq ($(CONFIG_AMEBA_SPI),y) +CHIP_CSRCS += ameba_spi.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 fa554af7e96..fb99412b3f1 100644 --- a/arch/arm/src/rtl8721f/ameba_board.mk +++ b/arch/arm/src/rtl8721f/ameba_board.mk @@ -158,6 +158,15 @@ endif ifeq ($(CONFIG_AMEBA_I2C),y) AMEBA_FWLIB_SRCS += $(AMEBA_SOC)/fwlib/ram_common/ameba_i2c.c endif + +# SPI (DesignWare SSI) register layer. The SPI driver +# (arch/.../common/ameba/ameba_spi.c) calls the fwlib SSI API; the data tables +# and helpers it indexes live in this RAM source and must be compiled in +# (--gc-sections drops the unused DMA/interrupt helpers). +ifeq ($(CONFIG_AMEBA_SPI),y) +AMEBA_FWLIB_SRCS += $(AMEBA_SOC)/fwlib/ram_common/ameba_spi.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_spi_chip.h b/arch/arm/src/rtl8721f/ameba_spi_chip.h new file mode 100644 index 00000000000..6c2a40981c9 --- /dev/null +++ b/arch/arm/src/rtl8721f/ameba_spi_chip.h @@ -0,0 +1,101 @@ +/**************************************************************************** + * arch/arm/src/rtl8721f/ameba_spi_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_SPI_CHIP_H +#define __ARCH_ARM_SRC_RTL8721F_AMEBA_SPI_CHIP_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per-chip SPI wiring for RTL8721F (amebagreen2). The shared driver + * (arch/arm/src/common/ameba/ameba_spi.c) includes this header to learn how + * many SPI (DesignWare SSI) controllers the chip exposes and, for each, its + * register base, peripheral-clock masks and crossbar pad-mux codes. See the + * amebadplus (rtl8721dx) header for the field-by-field contract; only the + * RTL8721F-specific values (from the amebagreen2 fwlib) are noted here: + * + * 1. Two high-speed SSI masters, SPI0 and SPI1, in the PERI_HCLK domain. + * The bases below are the NON-secure aliases (SPI0/1_REG_BASE in + * hal_platform.h, 0x4012_1000 / 0x4012_2000); the secure aliases + * (0x5012_xxxx) must not be used from the non-secure world. + * + * 2. APBPeriph_SPIx (function) and APBPeriph_SPIx_CLOCK masks live in + * REG_LSYS_FEN_GRP0 / REG_LSYS_CKE_GRP0 (group selector bit30 == 0), + * SPI0 at bit14 and SPI1 at bit15. + * + * 3. Pad mux: amebagreen2 has a generic PINMUX_FUNCTION_SPIx code (9/10) + * but the shared driver programs each signal individually, so the + * per-signal codes from ameba_pinmux.h are used (SPI0 CLK/MISO/MOSI/CS + * = 75/76/77/78, SPI1 = 79/80/81/82). The CS entries are + * informational: this driver drives CS as a plain GPIO. + */ + +#define AMEBA_NSPI 2 + +/* NON-secure SPI register bases (SPI0_REG_BASE / SPI1_REG_BASE). */ + +#define AMEBA_SPI_BASES { 0x40121000ul, 0x40122000ul } + +/* APBPeriph_SPIx (function) and APBPeriph_SPIx_CLOCK masks. Equal on this + * chip; kept as two lists so chips where they differ can supply both. The + * group selector (bit30) is 0 for the SPI block on amebagreen2. + */ + +#define AMEBA_SPI_APBPERIPH \ + { (((uint32_t)0 << 30) | ((uint32_t)1 << 14)), \ + (((uint32_t)0 << 30) | ((uint32_t)1 << 15)) } + +#define AMEBA_SPI_APBPERIPH_CLK \ + { (((uint32_t)0 << 30) | ((uint32_t)1 << 14)), \ + (((uint32_t)0 << 30) | ((uint32_t)1 << 15)) } + +/* Crossbar pad-mux function codes, one list per signal, indexed by + * controller. amebagreen2 per-signal codes: SPI0 CLK=75 / MISO=76 / + * MOSI=77 / CS=78, SPI1 CLK=79 / MISO=80 / MOSI=81 / CS=82. + */ + +#define AMEBA_SPI_CLKFID { 75, 79 } /* SPI0_CLK, SPI1_CLK */ +#define AMEBA_SPI_MOSIFID { 77, 81 } /* SPI0_MOSI, SPI1_MOSI */ +#define AMEBA_SPI_MISOFID { 76, 80 } /* SPI0_MISO, SPI1_MISO */ +#define AMEBA_SPI_CSFID { 78, 82 } /* SPI0_CS, SPI1_CS */ + +/* SSI peripheral clock: ip_clk is the PERI_HCLK-domain clock the SPI baud + * divider divides down to make SCLK. On amebagreen2 the fwlib exposes this + * domain directly through HPERI_ClkGet() (it resolves the HPERI source PLL + * and divider from the RRAM clock-info backup), so the whole computation is + * a single ROM call here rather than register poking as on the other ICs. + */ + +extern uint32_t HPERI_ClkGet(void); + +#define AMEBA_SPI_IPCLK() (HPERI_ClkGet()) + +#endif /* __ARCH_ARM_SRC_RTL8721F_AMEBA_SPI_CHIP_H */ diff --git a/boards/arm/rtl8721f/rtl8721f_evb/configs/spi/defconfig b/boards/arm/rtl8721f/rtl8721f_evb/configs/spi/defconfig new file mode 100644 index 00000000000..9d2a691fe49 --- /dev/null +++ b/boards/arm/rtl8721f/rtl8721f_evb/configs/spi/defconfig @@ -0,0 +1,50 @@ +# +# 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_SPI=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_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=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_SYSTEM_SPITOOL=y +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 782211c2711..357bd6a37d1 100644 --- a/boards/arm/rtl8721f/rtl8721f_evb/src/CMakeLists.txt +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/CMakeLists.txt @@ -34,11 +34,16 @@ if(CONFIG_AMEBA_I2C) list(APPEND SRCS rtl8721f_i2c.c) endif() +if(CONFIG_AMEBA_SPI) + list(APPEND SRCS rtl8721f_spi.c) +endif() + target_sources(board PRIVATE ${SRCS}) if(CONFIG_AMEBA_GPIO OR CONFIG_AMEBA_UART - OR CONFIG_AMEBA_I2C) + OR CONFIG_AMEBA_I2C + OR CONFIG_AMEBA_SPI) # 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 e203ef255ec..b14a234ad0c 100644 --- a/boards/arm/rtl8721f/rtl8721f_evb/src/Makefile +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/Makefile @@ -36,10 +36,14 @@ ifeq ($(CONFIG_AMEBA_I2C),y) CSRCS += rtl8721f_i2c.c endif +ifeq ($(CONFIG_AMEBA_SPI),y) +CSRCS += rtl8721f_spi.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),) +ifneq ($(CONFIG_AMEBA_GPIO)$(CONFIG_AMEBA_UART)$(CONFIG_AMEBA_I2C)$(CONFIG_AMEBA_SPI),) 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 fe8418d75a2..7febad4029a 100644 --- a/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_bringup.c +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_bringup.c @@ -164,6 +164,16 @@ int rtl8721f_bringup(void) } #endif +#ifdef CONFIG_AMEBA_SPI + /* Register the board's SPI master buses at /dev/spiN. */ + + ret = rtl8721f_spi_initialize(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: rtl8721f_spi_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_rtl8721f_evb.h b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_rtl8721f_evb.h index 04376848e0b..ba5fccc3adf 100644 --- a/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_rtl8721f_evb.h +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_rtl8721f_evb.h @@ -122,6 +122,19 @@ int rtl8721f_uart_initialize(void); int rtl8721f_i2c_initialize(void); #endif +#ifdef CONFIG_AMEBA_SPI +/**************************************************************************** + * Name: rtl8721f_spi_initialize + * + * Description: + * Register the board's SPI master buses at /dev/spiN + * (boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_spi.c). + * + ****************************************************************************/ + +int rtl8721f_spi_initialize(void); +#endif + #ifdef CONFIG_RTL8721F_FLASH_FS /**************************************************************************** * Name: ameba_flash_fs_initialize diff --git a/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_spi.c b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_spi.c new file mode 100644 index 00000000000..c9d586762c3 --- /dev/null +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_spi.c @@ -0,0 +1,103 @@ +/**************************************************************************** + * boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_spi.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_spi.h" +#include "rtl8721f_rtl8721f_evb.h" + +#ifdef CONFIG_AMEBA_SPI + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* One entry per SPI bus exposed to NuttX at /dev/spiN. The CLK/MOSI/MISO/CS + * pads below are examples used by the `spi` config (system/spi spitool); + * adjust them to match your board's wiring. + */ + +struct rtl8721f_spi_s +{ + int bus; /* Controller index (AMEBA_SPI0/AMEBA_SPI1) */ + uint8_t clkpin; /* SCLK pad (AMEBA_PA()/AMEBA_PB() encoding) */ + uint8_t mosipin; /* MOSI pad */ + uint8_t misopin; /* MISO pad */ + uint8_t cspin; /* Chip-select pad (software CS GPIO) */ +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct rtl8721f_spi_s g_spi_buses[] = +{ + { + AMEBA_SPI0, AMEBA_PA(14), AMEBA_PA(15), AMEBA_PA(16), AMEBA_PA(17) + }, + { + AMEBA_SPI1, AMEBA_PB(13), AMEBA_PB(14), AMEBA_PB(15), AMEBA_PB(16) + }, +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rtl8721f_spi_initialize + * + * Description: + * Register the board's SPI master buses at /dev/spiN. + * + ****************************************************************************/ + +int rtl8721f_spi_initialize(void) +{ + int i; + + for (i = 0; i < (int)nitems(g_spi_buses); i++) + { + if (ameba_spi_register(g_spi_buses[i].bus, g_spi_buses[i].clkpin, + g_spi_buses[i].mosipin, g_spi_buses[i].misopin, + g_spi_buses[i].cspin) == NULL) + { + syslog(LOG_ERR, + "ERROR: ameba_spi_register(/dev/spi%d) failed\n", + g_spi_buses[i].bus); + return -ENODEV; + } + } + + return OK; +} + +#endif /* CONFIG_AMEBA_SPI */
