This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 3441bc53b5f74cb9257192aadda0ae8d409273c0 Author: raiden00pl <[email protected]> AuthorDate: Thu Nov 19 13:42:36 2026 +0100 boards/stm32n6570-dk: Add the USB host and the usbhost configuration. The host uses the USB2 Type-A connector (VBUS switch on PB9) or the USB1 Type-C connector (VBUS through the TCPP03 on I2C2). usbhost adds USB mass storage and FAT to nsh. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: raiden00pl <[email protected]> --- .../arm/stm32n6/boards/stm32n6570-dk/index.rst | 26 +++ .../stm32n6570-dk/configs/usbhost/defconfig | 77 +++++++++ boards/arm/stm32n6/stm32n6570-dk/include/board.h | 12 ++ .../arm/stm32n6/stm32n6570-dk/src/CMakeLists.txt | 4 + boards/arm/stm32n6/stm32n6570-dk/src/Make.defs | 4 + .../arm/stm32n6/stm32n6570-dk/src/stm32_bringup.c | 11 ++ .../arm/stm32n6/stm32n6570-dk/src/stm32_usbhost.c | 180 +++++++++++++++++++++ .../arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h | 27 ++++ 8 files changed, 341 insertions(+) diff --git a/Documentation/platforms/arm/stm32n6/boards/stm32n6570-dk/index.rst b/Documentation/platforms/arm/stm32n6/boards/stm32n6570-dk/index.rst index bb878f9b739..18260476ee0 100644 --- a/Documentation/platforms/arm/stm32n6/boards/stm32n6570-dk/index.rst +++ b/Documentation/platforms/arm/stm32n6/boards/stm32n6570-dk/index.rst @@ -207,6 +207,26 @@ With a FAT formatted card:: nsh> dd if=/dev/zero of=/mnt/big.bin bs=4096 count=256 nsh> umount /mnt +USB host +======== + +The host runs at full speed on the embedded PHY (24 MHz reference, HSE / 2) +of the port selected with ``CONFIG_STM32N6_OTGHS_USB1``/``USB2``: + +* USB2 (default in ``usbhost``): the Type-A connector, VBUS switched by PB9. +* USB1: the Type-C connector (CN18). VBUS comes from the TCPP03: PA4 + enables it and the host driver closes its provider switch over I2C2 + (0x34). The CC lines are not driven. Not tested on hardware. + +VBUS of a flash drive can exceed what the ST-LINK USB port supplies: power +the board from CN18 when using the Type-A port. A FAT formatted flash +drive is ``/dev/sda``:: + + nsh> mount -t vfat /dev/sda /mnt + nsh> ls /mnt + nsh> echo hello > /mnt/test.txt + nsh> umount /mnt + Clocking ======== @@ -255,3 +275,9 @@ sdcard NuttShell with the microSD slot on SDMMC2 and the FAT file system, see `microSD`_. + +usbhost +------- + +The ``nsh`` configuration with the USB host on the USB2 Type-A +connector, the mass storage class and the FAT file system, see `USB host`_. diff --git a/boards/arm/stm32n6/stm32n6570-dk/configs/usbhost/defconfig b/boards/arm/stm32n6/stm32n6570-dk/configs/usbhost/defconfig new file mode 100644 index 00000000000..a0e2ac258b9 --- /dev/null +++ b/boards/arm/stm32n6/stm32n6570-dk/configs/usbhost/defconfig @@ -0,0 +1,77 @@ +# +# 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_ARCH_LEDS is not set +CONFIG_ARCH="arm" +CONFIG_ARCH_BOARD="stm32n6570-dk" +CONFIG_ARCH_BOARD_COMMON=y +CONFIG_ARCH_BOARD_STM32N6570_DK=y +CONFIG_ARCH_BUTTONS=y +CONFIG_ARCH_CHIP="stm32n6" +CONFIG_ARCH_CHIP_STM32=y +CONFIG_ARCH_CHIP_STM32N657X0=y +CONFIG_ARCH_CHIP_STM32N6=y +CONFIG_ARCH_INTERRUPTSTACK=4096 +CONFIG_ARCH_IRQBUTTONS=y +CONFIG_ARCH_STACKDUMP=y +CONFIG_ARMV8M_DCACHE=y +CONFIG_ARMV8M_ICACHE=y +CONFIG_BOARD_LOOPSPERMSEC=84000 +CONFIG_BUILTIN=y +CONFIG_DEBUG_FULLOPT=y +CONFIG_DEBUG_SYMBOLS=y +CONFIG_EXAMPLES_ALARM=y +CONFIG_FAT_LCNAMES=y +CONFIG_FAT_LFN=y +CONFIG_FS_FAT=y +CONFIG_FS_PROCFS=y +CONFIG_HAVE_CXX=y +CONFIG_HAVE_CXXINITIALIZE=y +CONFIG_I2C=y +CONFIG_IDLETHREAD_STACKSIZE=4096 +CONFIG_INIT_ENTRYPOINT="nsh_main" +CONFIG_INPUT=y +CONFIG_INPUT_BUTTONS=y +CONFIG_INPUT_BUTTONS_LOWER=y +CONFIG_NSH_BUILTIN_APPS=y +CONFIG_NSH_FILEIOSIZE=512 +CONFIG_NSH_READLINE=y +CONFIG_RAM_SIZE=2620416 +CONFIG_RAM_START=0x34180400 +CONFIG_RAW_BINARY=y +CONFIG_RR_INTERVAL=200 +CONFIG_RTC_ALARM=y +CONFIG_RTC_DATETIME=y +CONFIG_RTC_DRIVER=y +CONFIG_RTC_NALARMS=2 +CONFIG_RTC_PERIODIC=y +CONFIG_SCHED_HPWORK=y +CONFIG_SCHED_LPWORK=y +CONFIG_SCHED_WAITPID=y +CONFIG_SPITOOL_MAXBUS=5 +CONFIG_STM32N6_DEBUG=y +CONFIG_STM32N6_OTGHS_USB2=y +CONFIG_STM32_DMA1=y +CONFIG_STM32_I2C1=y +CONFIG_STM32_I2C2=y +CONFIG_STM32_I2C4=y +CONFIG_STM32_OTGHS=y +CONFIG_STM32_RNG=y +CONFIG_STM32_RTC=y +CONFIG_STM32_SPI5=y +CONFIG_STM32_USART1=y +CONFIG_STM32_USART2=y +CONFIG_SYSTEM_I2CTOOL=y +CONFIG_SYSTEM_NSH=y +CONFIG_SYSTEM_SPITOOL=y +CONFIG_USART1_SERIAL_CONSOLE=y +CONFIG_USBHOST=y +CONFIG_USBHOST_MSC=y +CONFIG_USBHOST_WAITER=y +CONFIG_USBHOST_WAITER_STACKSIZE=4096 +CONFIG_USERLED=y +CONFIG_USERLED_LOWER=y diff --git a/boards/arm/stm32n6/stm32n6570-dk/include/board.h b/boards/arm/stm32n6/stm32n6570-dk/include/board.h index ebc7ad78839..648c5336593 100644 --- a/boards/arm/stm32n6/stm32n6570-dk/include/board.h +++ b/boards/arm/stm32n6/stm32n6570-dk/include/board.h @@ -72,6 +72,7 @@ #define STM32_BOARD_USEHSE #define STM32_HSE_FREQUENCY 48000000ul +#define STM32_HSE_DIV2 /* hse_div2_osc_ck = HSE / 2 (USB PHYs) */ #define STM32_BOARD_VOS_HIGH #define BOARD_VDDCORE_OVERDRIVE_GPIO (GPIO_OUTPUT | GPIO_PUSHPULL | \ GPIO_SPEED_2MHZ | GPIO_OUTPUT_SET | \ @@ -214,6 +215,17 @@ #define STM32_RCC_CCIPR4_LTDCSEL RCC_CCIPR4_LTDCSEL_IC16 +/* USB1 and USB2 OTG HS PHY reference clocks: HSE oscillator / 2 = + * 24 MHz. Not available with the HSI clock tree. + */ + +#ifdef STM32_BOARD_USEHSE +# define STM32_RCC_CCIPR6_OTGPHY1SEL RCC_CCIPR6_OTGPHY1SEL_HSE_DIV_2_OSC +# define STM32_OTGPHY1_FREQUENCY (STM32_HSE_FREQUENCY / 2) +# define STM32_RCC_CCIPR6_OTGPHY2SEL RCC_CCIPR6_OTGPHY2SEL_HSE_DIV_2_OSC +# define STM32_OTGPHY2_FREQUENCY (STM32_HSE_FREQUENCY / 2) +#endif + /* I/O voltage domains ******************************************************/ /* VDDIO2 and VDDIO3 are supplied at 1.8 V on the Discovery kit. diff --git a/boards/arm/stm32n6/stm32n6570-dk/src/CMakeLists.txt b/boards/arm/stm32n6/stm32n6570-dk/src/CMakeLists.txt index 85afaf50fbe..b5c41b524eb 100644 --- a/boards/arm/stm32n6/stm32n6570-dk/src/CMakeLists.txt +++ b/boards/arm/stm32n6/stm32n6570-dk/src/CMakeLists.txt @@ -48,6 +48,10 @@ if(CONFIG_STM32_SDMMC2) list(APPEND SRCS stm32_sdmmc.c) endif() +if(CONFIG_STM32_OTGHS AND CONFIG_USBHOST) + list(APPEND SRCS stm32_usbhost.c) +endif() + target_sources(board PRIVATE ${SRCS}) set_property(GLOBAL PROPERTY LD_SCRIPT "${NUTTX_BOARD_DIR}/scripts/flash.ld") diff --git a/boards/arm/stm32n6/stm32n6570-dk/src/Make.defs b/boards/arm/stm32n6/stm32n6570-dk/src/Make.defs index a4f35c5929a..f689931b36a 100644 --- a/boards/arm/stm32n6/stm32n6570-dk/src/Make.defs +++ b/boards/arm/stm32n6/stm32n6570-dk/src/Make.defs @@ -51,6 +51,10 @@ ifeq ($(CONFIG_STM32_SDMMC2),y) CSRCS += stm32_sdmmc.c endif +ifeq ($(CONFIG_STM32_OTGHS)$(CONFIG_USBHOST),yy) +CSRCS += stm32_usbhost.c +endif + DEPPATH += --dep-path board VPATH += :board CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)board diff --git a/boards/arm/stm32n6/stm32n6570-dk/src/stm32_bringup.c b/boards/arm/stm32n6/stm32n6570-dk/src/stm32_bringup.c index b796fdd50ff..125693f9879 100644 --- a/boards/arm/stm32n6/stm32n6570-dk/src/stm32_bringup.c +++ b/boards/arm/stm32n6/stm32n6570-dk/src/stm32_bringup.c @@ -163,6 +163,17 @@ int stm32_bringup(void) } #endif +#if defined(CONFIG_STM32_OTGHS) && defined(CONFIG_USBHOST) + /* Start the USB host on the selected connector */ + + ret = stm32_usbhost_initialize(); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: stm32_usbhost_initialize() failed: %d\n", + ret); + } +#endif + #ifdef CONFIG_VIDEO_VL53L5CX ret = stm32_tof_initialize(); if (ret < 0) diff --git a/boards/arm/stm32n6/stm32n6570-dk/src/stm32_usbhost.c b/boards/arm/stm32n6/stm32n6570-dk/src/stm32_usbhost.c new file mode 100644 index 00000000000..9dd30ff6a11 --- /dev/null +++ b/boards/arm/stm32n6/stm32n6570-dk/src/stm32_usbhost.c @@ -0,0 +1,180 @@ +/**************************************************************************** + * boards/arm/stm32n6/stm32n6570-dk/src/stm32_usbhost.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 <errno.h> +#include <stdbool.h> +#include <stdint.h> +#include <syslog.h> + +#include <nuttx/i2c/i2c_master.h> +#include <nuttx/usb/usbhost.h> + +#include "stm32_gpio.h" +#include "stm32_i2c.h" +#include "stm32_otg.h" +#include "stm32_usbhost.h" +#include "stm32n6570-dk.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef CONFIG_USBHOST_WAITER +# error "USB host support needs CONFIG_USBHOST_WAITER" +#endif + +#if !defined(CONFIG_STM32N6_OTGHS_USB2) && !defined(CONFIG_STM32_I2C2) +# error "USB1 host support needs CONFIG_STM32_I2C2 for the TCPP03" +#endif + +/* TCPP03 Type-C port protection, register 0 (the only writable one) */ + +#define TCPP03_REG0 0x00 +#define TCPP03_REG0_GDP_CLOSED (1 << 2) /* VBUS provider switch closed */ +#define TCPP03_REG0_GDC_OPEN (1 << 3) /* VBUS consumer switch open */ +#define TCPP03_REG0_MODE_NORMAL (1 << 4) /* Normal power mode */ + +#define TCPP03_I2C_FREQUENCY 100000 + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +#ifndef CONFIG_STM32N6_OTGHS_USB2 +/**************************************************************************** + * Name: stm32_tcpp03_write + * + * Description: + * Write register 0 of the TCPP03 on I2C2. + * + ****************************************************************************/ + +static int stm32_tcpp03_write(uint8_t value) +{ + struct i2c_master_s *i2c; + struct i2c_config_s config; + uint8_t buffer[2]; + int ret; + + i2c = stm32_i2cbus_initialize(TCPP03_I2C_BUS); + if (i2c == NULL) + { + return -ENODEV; + } + + config.frequency = TCPP03_I2C_FREQUENCY; + config.address = TCPP03_I2C_ADDR; + config.addrlen = 7; + + buffer[0] = TCPP03_REG0; + buffer[1] = value; + + ret = i2c_write(i2c, &config, buffer, sizeof(buffer)); + stm32_i2cbus_uninitialize(i2c); + return ret; +} +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: stm32_usbhost_initialize + * + * Description: + * Initialize the USB OTG HS host controller and start the thread that + * waits for device connections. + * + ****************************************************************************/ + +int stm32_usbhost_initialize(void) +{ + struct usbhost_connection_s *conn; + +#ifdef CONFIG_STM32N6_OTGHS_USB2 + /* USB2 Type-A connector: VBUS off until the host driver turns it on */ + + stm32_configgpio(GPIO_USB2_PWR_EN); +#else + /* Enable the TCPP03. It powers up in hibernate mode with VBUS off. */ + + stm32_configgpio(GPIO_TCPP03_EN); +#endif + + conn = stm32_otghshost_initialize(0); + if (conn == NULL) + { + return -ENODEV; + } + + return usbhost_waiter_initialize(conn); +} + +/**************************************************************************** + * Name: stm32_usbhost_vbusdrive + * + * Description: + * Drive VBUS on the USB2 Type-A connector with its power switch, or on + * the USB1 Type-C connector (CN18). There the TCPP03 switches the 5 V + * supply to VBUS through its provider gate driver; it is set up over I2C2 + * with the consumer switch open. The CC lines are not driven: the + * device is expected to draw VBUS without a Type-C attach handshake. + * + ****************************************************************************/ + +void stm32_usbhost_vbusdrive(int iface, bool enable) +{ +#ifdef CONFIG_STM32N6_OTGHS_USB2 + UNUSED(iface); + + stm32_gpiowrite(GPIO_USB2_PWR_EN, enable); +#else + int ret; + + UNUSED(iface); + + if (enable) + { + ret = stm32_tcpp03_write(TCPP03_REG0_MODE_NORMAL | + TCPP03_REG0_GDC_OPEN | + TCPP03_REG0_GDP_CLOSED); + } + else + { + ret = stm32_tcpp03_write(TCPP03_REG0_MODE_NORMAL | + TCPP03_REG0_GDC_OPEN); + } + + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: TCPP03 VBUS %s failed: %d\n", + enable ? "on" : "off", ret); + } +#endif +} diff --git a/boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h b/boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h index def7b66112d..27c96f56901 100644 --- a/boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h +++ b/boards/arm/stm32n6/stm32n6570-dk/src/stm32n6570-dk.h @@ -121,6 +121,20 @@ #define SDIO_SLOTNO 0 #define SDIO_MINOR 0 +/* USB1 Type-C connector (CN18): the TCPP03 port protection switches VBUS. + * It is enabled by PA4 (active high) and set up over I2C2 (0x34). + */ + +#define GPIO_TCPP03_EN (GPIO_OUTPUT | GPIO_PUSHPULL | GPIO_SPEED_2MHZ | \ + GPIO_OUTPUT_SET | GPIO_PORTA | GPIO_PIN4) +#define TCPP03_I2C_BUS 2 +#define TCPP03_I2C_ADDR 0x34 + +/* USB2 Type-A connector: VBUS power switch enabled by PB9 (active high) */ + +#define GPIO_USB2_PWR_EN (GPIO_OUTPUT | GPIO_PUSHPULL | GPIO_SPEED_2MHZ | \ + GPIO_OUTPUT_CLEAR | GPIO_PORTB | GPIO_PIN9) + #ifndef __ASSEMBLY__ /**************************************************************************** @@ -203,5 +217,18 @@ int stm32_tof_initialize(void); int stm32_sdio_initialize(void); #endif +/**************************************************************************** + * Name: stm32_usbhost_initialize + * + * Description: + * Start the USB OTG HS host on the USB1 Type-C or the USB2 Type-A + * connector and wait for devices. + * + ****************************************************************************/ + +#if defined(CONFIG_STM32_OTGHS) && defined(CONFIG_USBHOST) +int stm32_usbhost_initialize(void); +#endif + #endif /* __ASSEMBLY__ */ #endif /* __BOARDS_ARM_STM32N6_STM32N6570_DK_SRC_STM32N6570_DK_H */
