diff --git a/arch/arm/src/rtl8721f/CMakeLists.txt b/arch/arm/src/rtl8721f/CMakeLists.txt index b64d618e018..62f32a6cae1 100644 --- a/arch/arm/src/rtl8721f/CMakeLists.txt +++ b/arch/arm/src/rtl8721f/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}) @@ -108,6 +112,14 @@ if(CONFIG_AMEBA_UART) list(APPEND AMEBA_FWLIB_SRCS ${AMEBA_SOC}/fwlib/ram_common/ameba_uart.c) endif() +# I2C register layer. Unlike UART, the fwlib I2C API is NOT in ROM: the I2C +# driver (arch/.../common/ameba/ameba_i2c.c) calls I2C_Init/StructInit/Cmd and +# I2C_MasterWrite/Read/RepeatRead, all compiled from this RAM source and linked +# in (--gc-sections drops the unused DMA/interrupt helpers). +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/rtl8721f/Make.defs b/arch/arm/src/rtl8721f/Make.defs index bbd38f0161c..5958c2b69ab 100644 --- a/arch/arm/src/rtl8721f/Make.defs +++ b/arch/arm/src/rtl8721f/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 RTL8721F SDK integration # diff --git a/arch/arm/src/rtl8721f/ameba_board.mk b/arch/arm/src/rtl8721f/ameba_board.mk index a986df8b311..fa554af7e96 100644 --- a/arch/arm/src/rtl8721f/ameba_board.mk +++ b/arch/arm/src/rtl8721f/ameba_board.mk @@ -149,6 +149,15 @@ endif ifeq ($(CONFIG_AMEBA_UART),y) AMEBA_FWLIB_SRCS += $(AMEBA_SOC)/fwlib/ram_common/ameba_uart.c endif + +# I2C register layer. Unlike UART, the fwlib I2C API is NOT in ROM: the I2C +# driver (arch/.../common/ameba/ameba_i2c.c) calls I2C_Init/StructInit/Cmd and +# I2C_MasterWrite/Read/RepeatRead, all of which are compiled from this RAM +# source and must be linked in (--gc-sections drops the unused DMA/interrupt +# 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/rtl8721f/ameba_i2c_chip.h b/arch/arm/src/rtl8721f/ameba_i2c_chip.h new file mode 100644 index 00000000000..1982839e38b --- /dev/null +++ b/arch/arm/src/rtl8721f/ameba_i2c_chip.h @@ -0,0 +1,96 @@ +/**************************************************************************** + * arch/arm/src/rtl8721f/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_RTL8721F_AMEBA_I2C_CHIP_H +#define __ARCH_ARM_SRC_RTL8721F_AMEBA_I2C_CHIP_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* Per-chip I2C wiring for RTL8721F (amebagreen2). 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 amebagreen2 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, 0x4100_8000 + * / 0x4100_9000; the secure aliases I2Cx_REG_BASE_S live at + * 0x5100_xxxx). NuttX runs on the KM4 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): 89/90 for I2C0 and + * 91/92 for I2C1. + * + * 4. The amebagreen2 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 { 0x41008000ul, 0x41009000ul } + +/* 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 { 89, 91 } /* I2C0_SCL, I2C1_SCL */ +#define AMEBA_I2C_SDAFID { 90, 92 } /* I2C0_SDA, I2C1_SDA */ + +/* The amebagreen2 I2C_InitTypeDef carries the DMA request-level fields. */ + +#define AMEBA_I2C_HAS_DMA_FIELDS 1 + +#endif /* __ARCH_ARM_SRC_RTL8721F_AMEBA_I2C_CHIP_H */ diff --git a/boards/arm/rtl8721f/rtl8721f_evb/configs/i2c/defconfig b/boards/arm/rtl8721f/rtl8721f_evb/configs/i2c/defconfig new file mode 100644 index 00000000000..bc8d13cd976 --- /dev/null +++ b/boards/arm/rtl8721f/rtl8721f_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="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_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/rtl8721f/rtl8721f_evb/src/CMakeLists.txt b/boards/arm/rtl8721f/rtl8721f_evb/src/CMakeLists.txt index a617cffaa01..782211c2711 100644 --- a/boards/arm/rtl8721f/rtl8721f_evb/src/CMakeLists.txt +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/CMakeLists.txt @@ -30,9 +30,15 @@ if(CONFIG_AMEBA_UART) list(APPEND SRCS rtl8721f_uart.c) endif() +if(CONFIG_AMEBA_I2C) + list(APPEND SRCS rtl8721f_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/rtl8721f/rtl8721f_evb/src/Makefile b/boards/arm/rtl8721f/rtl8721f_evb/src/Makefile index 9f36cddb198..e203ef255ec 100644 --- a/boards/arm/rtl8721f/rtl8721f_evb/src/Makefile +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/Makefile @@ -32,10 +32,14 @@ ifeq ($(CONFIG_AMEBA_UART),y) CSRCS += rtl8721f_uart.c endif +ifeq ($(CONFIG_AMEBA_I2C),y) +CSRCS += rtl8721f_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/rtl8721f/rtl8721f_evb/src/rtl8721f_bringup.c b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_bringup.c index 63a9cae246f..fe8418d75a2 100644 --- a/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_bringup.c +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_bringup.c @@ -154,6 +154,16 @@ int rtl8721f_bringup(void) } #endif +#ifdef CONFIG_AMEBA_I2C + /* Register the board's I2C master buses at /dev/i2cN. */ + + ret = rtl8721f_i2c_initialize(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: rtl8721f_i2c_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_i2c.c b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_i2c.c new file mode 100644 index 00000000000..a65d06d57b6 --- /dev/null +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_i2c.c @@ -0,0 +1,105 @@ +/**************************************************************************** + * boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_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 + +#include +#include + +#include "ameba_gpio.h" +#include "ameba_i2c.h" +#include "rtl8721f_rtl8721f_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. 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 rtl8721f_i2c_s +{ + int bus; /* Controller index (AMEBA_I2C0/AMEBA_I2C1) */ + uint8_t sclpin; /* SCL pad (AMEBA_PA()/AMEBA_PB() encoding) */ + uint8_t sdapin; /* SDA pad (AMEBA_PA()/AMEBA_PB() encoding) */ +}; + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +static const struct rtl8721f_i2c_s g_i2c_buses[] = +{ + { + AMEBA_I2C0, AMEBA_PA(22), AMEBA_PA(23) + }, + { + AMEBA_I2C1, AMEBA_PA(24), AMEBA_PA(25) + }, +}; + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: rtl8721f_i2c_initialize + * + * Description: + * Register the board's I2C master buses at /dev/i2cN. + * + ****************************************************************************/ + +int rtl8721f_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/rtl8721f/rtl8721f_evb/src/rtl8721f_rtl8721f_evb.h b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_rtl8721f_evb.h index acbc7c21e83..04376848e0b 100644 --- a/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_rtl8721f_evb.h +++ b/boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_rtl8721f_evb.h @@ -109,6 +109,19 @@ int rtl8721f_gpio_initialize(void); int rtl8721f_uart_initialize(void); #endif +#ifdef CONFIG_AMEBA_I2C +/**************************************************************************** + * Name: rtl8721f_i2c_initialize + * + * Description: + * Register the board's I2C master buses at /dev/i2cN + * (boards/arm/rtl8721f/rtl8721f_evb/src/rtl8721f_i2c.c). + * + ****************************************************************************/ + +int rtl8721f_i2c_initialize(void); +#endif + #ifdef CONFIG_RTL8721F_FLASH_FS /**************************************************************************** * Name: ameba_flash_fs_initialize