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 af49db4483bf8f423b444a3b10aef690427d088d Author: dechao_gong <[email protected]> AuthorDate: Wed Aug 12 14:41:03 2026 +0800 arch/arm/rtl8720f: add I2C master driver support Wire the RTL8720F to the shared Ameba I2C master driver (arch/arm/src/common/ameba/ameba_i2c.c), reusing it unchanged. Add the per-chip header arch/arm/src/rtl8720f/ameba_i2c_chip.h supplying the chip's I2C wiring: two controllers (I2C0/I2C1) on their non-secure register aliases (0x401c8000 / 0x401c9000), the APBPeriph function/clock masks, the crossbar SCL/SDA pad-mux codes (59/60 and 61/62), and AMEBA_I2C_HAS_DMA_FIELDS=1 (the chip's I2C_InitTypeDef carries the DMA request-level fields). Add the board glue: rtl8720f_i2c.c registers I2C0 at /dev/i2c0 (PA22/PA23) and I2C1 at /dev/i2c1 (PA24/PA25), plus the build wiring (Make.defs / CMakeLists.txt / ameba_board.mk pull in the common driver and the fwlib ram_common/ameba_i2c.c data-table source), the bringup registration hook and the board header declaration. Add the i2c defconfig (minimal NSH with the i2ctool) and document the config in the board index. Signed-off-by: dechao_gong <[email protected]> Assisted-by: Claude <[email protected]> --- .../arm/rtl8720f/boards/rtl8720f_evb/index.rst | 16 +++ arch/arm/src/rtl8720f/CMakeLists.txt | 10 ++ arch/arm/src/rtl8720f/Make.defs | 4 + arch/arm/src/rtl8720f/ameba_board.mk | 9 ++ arch/arm/src/rtl8720f/ameba_i2c_chip.h | 96 ++++++++++++++++++ .../rtl8720f/rtl8720f_evb/configs/i2c/defconfig | 50 ++++++++++ .../arm/rtl8720f/rtl8720f_evb/src/CMakeLists.txt | 8 +- boards/arm/rtl8720f/rtl8720f_evb/src/Makefile | 6 +- .../rtl8720f/rtl8720f_evb/src/rtl8720f_bringup.c | 10 ++ .../arm/rtl8720f/rtl8720f_evb/src/rtl8720f_i2c.c | 107 +++++++++++++++++++++ .../rtl8720f_evb/src/rtl8720f_rtl8720f_evb.h | 14 +++ 11 files changed, 328 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 d28315b3489..183664de23e 100644 --- a/Documentation/platforms/arm/rtl8720f/boards/rtl8720f_evb/index.rst +++ b/Documentation/platforms/arm/rtl8720f/boards/rtl8720f_evb/index.rst @@ -35,6 +35,8 @@ Supported in this NuttX port: interrupt), driven directly on the SDK fwlib register layer * General-purpose UARTs exposed as ``/dev/ttySN`` serial devices, driven directly on the SDK fwlib register layer +* I2C master buses exposed as ``/dev/i2cN`` character devices, driven directly + on the SDK fwlib register layer Buttons and LEDs ================ @@ -90,6 +92,20 @@ or wire it to a host serial adapter):: The line format can be changed at runtime through ``tcsetattr()`` (the config enables ``CONFIG_SERIAL_TERMIOS``). UART2 is not exposed by the driver. +i2c +--- + +Minimal NSH with the I2C master driver and the ``i2ctool`` (``system/i2c``) +enabled (no Wi-Fi). The board registers its I2C controllers from a table (see +``boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_i2c.c``): I2C0 at ``/dev/i2c0`` +on PA22/PA23 and I2C1 at ``/dev/i2c1`` on PA24/PA25. Edit that table -- +controller and SCL/SDA pads -- to match a board's wiring; the pads use the +same ``AMEBA_PA()`` encoding as the GPIO table and are muxed to the I2C +function through the SDK ROM. The I2C bus is open-drain, so fit external +pull-ups on SCL/SDA. Probe a bus with the tool:: + + nsh> i2c dev -b 0 0x03 0x77 # scan /dev/i2c0 for devices + nsh --- diff --git a/arch/arm/src/rtl8720f/CMakeLists.txt b/arch/arm/src/rtl8720f/CMakeLists.txt index 8b077a53bce..a972f9765c0 100644 --- a/arch/arm/src/rtl8720f/CMakeLists.txt +++ b/arch/arm/src/rtl8720f/CMakeLists.txt @@ -54,6 +54,10 @@ if(CONFIG_AMEBA_UART) list(APPEND SRCS ${AMEBA_COMMON}/ameba_uart.c) endif() +if(CONFIG_AMEBA_I2C) + list(APPEND SRCS ${AMEBA_COMMON}/ameba_i2c.c) +endif() + target_include_directories(arch PRIVATE ${AMEBA_COMMON}) target_sources(arch PRIVATE ${SRCS}) @@ -109,6 +113,12 @@ if(CONFIG_AMEBA_UART) list(APPEND AMEBA_FWLIB_SRCS ${AMEBA_SOC}/fwlib/ram_common/ameba_uart.c) endif() +# I2C register layer (same rationale as the UART block above): the fwlib I2C +# data tables live in this RAM source and must be compiled in. +if(CONFIG_AMEBA_I2C) + list(APPEND AMEBA_FWLIB_SRCS ${AMEBA_SOC}/fwlib/ram_common/ameba_i2c.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 0ac126000c9..1069e461e9c 100644 --- a/arch/arm/src/rtl8720f/Make.defs +++ b/arch/arm/src/rtl8720f/Make.defs @@ -64,6 +64,10 @@ ifeq ($(CONFIG_AMEBA_UART),y) CHIP_CSRCS += ameba_uart.c endif +ifeq ($(CONFIG_AMEBA_I2C),y) +CHIP_CSRCS += ameba_i2c.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 d9f841a48ad..bd7a629c6f0 100644 --- a/arch/arm/src/rtl8720f/ameba_board.mk +++ b/arch/arm/src/rtl8720f/ameba_board.mk @@ -127,6 +127,15 @@ ifeq ($(CONFIG_AMEBA_UART),y) AMEBA_FWLIB_SRCS += $(AMEBA_SOC)/fwlib/ram_common/ameba_uart.c endif +# I2C register layer. Like the UART one above, the I2C driver +# (arch/.../common/ameba/ameba_i2c.c) calls the fwlib I2C API, which resolves +# to the ROM symbol table; the ROM routines index the fwlib data tables that +# live in this RAM source, so it must be compiled in (--gc-sections drops the +# unused DMA/slave helpers). +ifeq ($(CONFIG_AMEBA_I2C),y) +AMEBA_FWLIB_SRCS += $(AMEBA_SOC)/fwlib/ram_common/ameba_i2c.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_i2c_chip.h b/arch/arm/src/rtl8720f/ameba_i2c_chip.h new file mode 100644 index 00000000000..542bb33fed1 --- /dev/null +++ b/arch/arm/src/rtl8720f/ameba_i2c_chip.h @@ -0,0 +1,96 @@ +/**************************************************************************** + * arch/arm/src/rtl8720f/ameba_i2c_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_I2C_CHIP_H +#define __ARCH_ARM_SRC_RTL8720F_AMEBA_I2C_CHIP_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per-chip I2C wiring for RTL8720F. The shared driver + * (arch/arm/src/common/ameba/ameba_i2c.c) includes this header to learn how + * many I2C controllers the chip exposes and, for each, its register base, + * peripheral-clock masks and crossbar pad-mux codes. It also learns the + * chip's I2C_InitTypeDef layout through AMEBA_I2C_HAS_DMA_FIELDS. + * + * The values below come from the RTL8720F fwlib headers: + * + * 1. Two controllers (I2C0/I2C1). The bases are the NON-secure peripheral + * aliases (I2C0_REG_BASE / I2C1_REG_BASE in hal_platform.h, 0x401C_8000 + * / 0x401C_9000; the secure aliases I2Cx_REG_BASE_S live at + * 0x501C_xxxx). NuttX runs on the KM4TZ core in the SECURE state, but + * the I2C block responds on its non-secure alias, so the driver hands + * the fwlib these non-secure bases (see the note in ameba_i2c.c). + * + * 2. APBPeriph_I2Cx (function) and APBPeriph_I2Cx_CLOCK masks + * (sysreg_lsys.h): group bit30 plus bit10 (I2C0) / bit11 (I2C1). Equal + * for the function and clock arguments on this chip. + * + * 3. Crossbar pad-mux: a distinct function code per SCL/SDA signal + * (PINMUX_FUNCTION_I2Cx_SCL/SDA in ameba_pinmux.h): 59/60 for I2C0 and + * 61/62 for I2C1. + * + * 4. The RTL8720F I2C_InitTypeDef carries the three DMA request-level + * fields (I2CTxDMARqLv / I2CRxDMARqLv / I2CDMAMod) between I2CFilter + * and I2CAckAddr1, so AMEBA_I2C_HAS_DMA_FIELDS is 1 to keep the + * driver's mirror struct byte-for-byte identical to the fwlib one. + */ + +#define AMEBA_NI2C 2 + +/* NON-secure I2C register bases (I2C0_REG_BASE / I2C1_REG_BASE). */ + +#define AMEBA_I2C_BASES { 0x401c8000ul, 0x401c9000ul } + +/* APBPeriph_I2Cx (function) and APBPeriph_I2Cx_CLOCK masks. Equal on this + * chip; kept as two lists so chips where they differ can supply both. + */ + +#define AMEBA_I2C_APBPERIPH \ + { (((uint32_t)1 << 30) | ((uint32_t)1 << 10)), \ + (((uint32_t)1 << 30) | ((uint32_t)1 << 11)) } + +#define AMEBA_I2C_APBPERIPH_CLK \ + { (((uint32_t)1 << 30) | ((uint32_t)1 << 10)), \ + (((uint32_t)1 << 30) | ((uint32_t)1 << 11)) } + +/* Crossbar pad-mux function codes (PINMUX_FUNCTION_I2Cx_SCL/SDA), indexed by + * controller. + */ + +#define AMEBA_I2C_SCLFID { 59, 61 } /* I2C0_SCL, I2C1_SCL */ +#define AMEBA_I2C_SDAFID { 60, 62 } /* I2C0_SDA, I2C1_SDA */ + +/* The RTL8720F I2C_InitTypeDef carries the DMA request-level fields. */ + +#define AMEBA_I2C_HAS_DMA_FIELDS 1 + +#endif /* __ARCH_ARM_SRC_RTL8720F_AMEBA_I2C_CHIP_H */ diff --git a/boards/arm/rtl8720f/rtl8720f_evb/configs/i2c/defconfig b/boards/arm/rtl8720f/rtl8720f_evb/configs/i2c/defconfig new file mode 100644 index 00000000000..b0841332368 --- /dev/null +++ b/boards/arm/rtl8720f/rtl8720f_evb/configs/i2c/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_I2C=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_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_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_I2CTOOL=y +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 aeee1692e6d..1746ab388de 100644 --- a/boards/arm/rtl8720f/rtl8720f_evb/src/CMakeLists.txt +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/CMakeLists.txt @@ -30,9 +30,15 @@ if(CONFIG_AMEBA_UART) list(APPEND SRCS rtl8720f_uart.c) endif() +if(CONFIG_AMEBA_I2C) + list(APPEND SRCS rtl8720f_i2c.c) +endif() + target_sources(board PRIVATE ${SRCS}) -if(CONFIG_AMEBA_GPIO OR CONFIG_AMEBA_UART) +if(CONFIG_AMEBA_GPIO + OR CONFIG_AMEBA_UART + OR CONFIG_AMEBA_I2C) # 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 0c920798152..67cf764902c 100644 --- a/boards/arm/rtl8720f/rtl8720f_evb/src/Makefile +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/Makefile @@ -32,10 +32,14 @@ ifeq ($(CONFIG_AMEBA_UART),y) CSRCS += rtl8720f_uart.c endif +ifeq ($(CONFIG_AMEBA_I2C),y) +CSRCS += rtl8720f_i2c.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),) +ifneq ($(CONFIG_AMEBA_GPIO)$(CONFIG_AMEBA_UART)$(CONFIG_AMEBA_I2C),) 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 7acc10bb528..8069c6090e3 100644 --- a/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_bringup.c +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_bringup.c @@ -140,6 +140,16 @@ int rtl8720f_bringup(void) } #endif +#ifdef CONFIG_AMEBA_I2C + /* Register the board's I2C master buses at /dev/i2cN. */ + + ret = rtl8720f_i2c_initialize(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: rtl8720f_i2c_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_i2c.c b/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_i2c.c new file mode 100644 index 00000000000..fb362d90eee --- /dev/null +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_i2c.c @@ -0,0 +1,107 @@ +/**************************************************************************** + * boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_i2c.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 "ameba_gpio.h" +#include "ameba_i2c.h" +#include "rtl8720f_rtl8720f_evb.h" + +#ifdef CONFIG_AMEBA_I2C + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/* One entry per I2C bus exposed to NuttX at /dev/i2cN. The SCL/SDA pads are + * examples used by the `i2c` config (system/i2c i2ctool) -- any pad can be + * routed to an I2C controller through the pin mux, so adjust them to match + * your board's wiring. I2C0 is routed to PA22/PA23 and I2C1 to PA24/PA25, + * free general-purpose pads on this board. RTL8720F drives all GPIO through + * a single port A controller, so pads use the AMEBA_PA() encoding. Note the + * I2C bus is open-drain: fit external pull-ups on SCL/SDA (the on-chip + * pull-ups are weak, and probe loading such as a logic-analyzer clip can + * keep the line from rising). + */ + +struct rtl8720f_i2c_s +{ + int bus; /* Controller index (AMEBA_I2C0/AMEBA_I2C1) */ + uint8_t sclpin; /* SCL pad (AMEBA_PA() encoding) */ + uint8_t sdapin; /* SDA pad (AMEBA_PA() encoding) */ +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct rtl8720f_i2c_s g_i2c_buses[] = +{ + { + AMEBA_I2C0, AMEBA_PA(22), AMEBA_PA(23) + }, + { + AMEBA_I2C1, AMEBA_PA(24), AMEBA_PA(25) + }, +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rtl8720f_i2c_initialize + * + * Description: + * Register the board's I2C master buses at /dev/i2cN. + * + ****************************************************************************/ + +int rtl8720f_i2c_initialize(void) +{ + int ret; + int i; + + for (i = 0; i < (int)nitems(g_i2c_buses); i++) + { + ret = ameba_i2c_register(g_i2c_buses[i].bus, g_i2c_buses[i].sclpin, + g_i2c_buses[i].sdapin); + if (ret < 0) + { + syslog(LOG_ERR, + "ERROR: ameba_i2c_register(/dev/i2c%d) failed: %d\n", + g_i2c_buses[i].bus, ret); + return ret; + } + } + + return OK; +} + +#endif /* CONFIG_AMEBA_I2C */ 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 7a5728a5281..610e31a285f 100644 --- a/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtl8720f_evb.h +++ b/boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_rtl8720f_evb.h @@ -123,5 +123,19 @@ int rtl8720f_gpio_initialize(void); int rtl8720f_uart_initialize(void); #endif +#ifdef CONFIG_AMEBA_I2C +/**************************************************************************** + * Name: rtl8720f_i2c_initialize + * + * Description: + * Register the board's I2C master buses with the NuttX I2C character + * driver at /dev/i2cN + * (boards/arm/rtl8720f/rtl8720f_evb/src/rtl8720f_i2c.c). + * + ****************************************************************************/ + +int rtl8720f_i2c_initialize(void); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __BOARDS_ARM_RTL8720F_RTL8720F_EVB_SRC_RTL8720F_RTL8720F_EVB_H */
