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


The following commit(s) were added to refs/heads/master by this push:
     new b0e382be5f stm32h7/linum-stm32h753bi: Add USB console support
b0e382be5f is described below

commit b0e382be5f6b797c640e966a73c604e8e188ec43
Author: Jorge Guzman <jorge....@gmail.com>
AuthorDate: Sun Feb 25 12:00:31 2024 -0300

    stm32h7/linum-stm32h753bi: Add USB console support
    
    This commit adds support to use NSH over USB without
    connecting an external USB/Serial adapter.
    
    Signed-off-by" Jorge Guzman jorge....@gmail.com
---
 .../arm/stm32h7/boards/linum-stm32h753bi/index.rst |  38 ++-
 .../linum-stm32h753bi/configs/nshusb/defconfig     |  61 ++++
 .../arm/stm32h7/linum-stm32h753bi/include/board.h  |   5 +
 .../stm32h7/linum-stm32h753bi/src/CMakeLists.txt   |   4 +
 boards/arm/stm32h7/linum-stm32h753bi/src/Makefile  |   4 +
 .../linum-stm32h753bi/src/linum-stm32h753bi.h      |  35 +++
 .../arm/stm32h7/linum-stm32h753bi/src/stm32_usb.c  | 320 +++++++++++++++++++++
 7 files changed, 459 insertions(+), 8 deletions(-)

diff --git 
a/Documentation/platforms/arm/stm32h7/boards/linum-stm32h753bi/index.rst 
b/Documentation/platforms/arm/stm32h7/boards/linum-stm32h753bi/index.rst
index 48a63d3c2f..5330c2317c 100644
--- a/Documentation/platforms/arm/stm32h7/boards/linum-stm32h753bi/index.rst
+++ b/Documentation/platforms/arm/stm32h7/boards/linum-stm32h753bi/index.rst
@@ -183,14 +183,15 @@ USB
 
 The LINUM-STM32H753BI has one usb port.
 
-  ======= =====
-  USB     PINS
-  ======= =====
-  USB_N   PA11
-  USB_P   PA12
-  USB_EN  PI12
-  USB_FLT PI13
-  ======= =====
+  ========= =====
+  USB       PINS
+  ========= =====
+  USB_VBUS  PA9
+  USB_N     PA11
+  USB_P     PA12
+  USB_EN    PI12
+  USB_FLT   PI13
+  ========= =====
 
 QUADSPI
 ==============
@@ -379,6 +380,27 @@ nsh
 Configures the NuttShell (nsh) located at apps/examples/nsh. This
 configuration enables a serial console on UART1.
 
+usbnsh
+------
+
+Configures the NuttShell (nsh) located at apps/examples/nsh. This 
configuration enables a serial console over USB.
+
+After flasing and reboot your board you should see in your dmesg logs::
+
+    $ sudo dmesg | tail
+    [ 9180.937813] usb 3-1.1.2: SerialNumber: 0
+    [ 9180.946974] cdc_acm 3-1.1.2:1.0: ttyACM0: USB ACM device
+    [ 9715.123387] usb 3-1.1.2: USB disconnect, device number 20
+    [ 9717.393142] usb 3-1.1.2: new full-speed USB device number 21 using 
xhci_hcd
+    [ 9717.494824] usb 3-1.1.2: New USB device found, idVendor=0525, 
idProduct=a4a7, bcdDevice= 1.01
+    [ 9717.494834] usb 3-1.1.2: New USB device strings: Mfr=1, Product=2, 
SerialNumber=3
+    [ 9717.494837] usb 3-1.1.2: Product: CDC/ACM Serial
+    [ 9717.494840] usb 3-1.1.2: Manufacturer: NuttX
+    [ 9717.494842] usb 3-1.1.2: SerialNumber: 0
+    [ 9717.504192] cdc_acm 3-1.1.2:1.0: ttyACM0: USB ACM device
+
+You may need to press ENTER 3 times before the NSH show up.
+
 modbus_slave
 ------------
 
diff --git a/boards/arm/stm32h7/linum-stm32h753bi/configs/nshusb/defconfig 
b/boards/arm/stm32h7/linum-stm32h753bi/configs/nshusb/defconfig
new file mode 100644
index 0000000000..57c87d0605
--- /dev/null
+++ b/boards/arm/stm32h7/linum-stm32h753bi/configs/nshusb/defconfig
@@ -0,0 +1,61 @@
+#
+# 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_NSH_DISABLE_IFCONFIG is not set
+# CONFIG_NSH_DISABLE_PS is not set
+# CONFIG_STANDARD_SERIAL is not set
+# CONFIG_STM32H7_USE_LEGACY_PINMAP is not set
+CONFIG_ARCH="arm"
+CONFIG_ARCH_BOARD="linum-stm32h753bi"
+CONFIG_ARCH_BOARD_LINUM_STM32H753BI=y
+CONFIG_ARCH_CHIP="stm32h7"
+CONFIG_ARCH_CHIP_STM32H753BI=y
+CONFIG_ARCH_CHIP_STM32H7=y
+CONFIG_ARCH_CHIP_STM32H7_CORTEXM7=y
+CONFIG_ARCH_STACKDUMP=y
+CONFIG_ARMV7M_DCACHE=y
+CONFIG_ARMV7M_DCACHE_WRITETHROUGH=y
+CONFIG_ARMV7M_DTCM=y
+CONFIG_ARMV7M_ICACHE=y
+CONFIG_BOARDCTL_USBDEVCTRL=y
+CONFIG_BOARD_LOOPSPERMSEC=43103
+CONFIG_BUILTIN=y
+CONFIG_CDCACM=y
+CONFIG_CDCACM_CONSOLE=y
+CONFIG_DEBUG_FEATURES=y
+CONFIG_DEBUG_SYMBOLS=y
+CONFIG_EXAMPLES_ALARM=y
+CONFIG_INIT_ENTRYPOINT="nsh_main"
+CONFIG_INTELHEX_BINARY=y
+CONFIG_LIBM=y
+CONFIG_MM_REGIONS=4
+CONFIG_NSH_ARCHINIT=y
+CONFIG_NSH_BUILTIN_APPS=y
+CONFIG_NSH_FILEIOSIZE=512
+CONFIG_NSH_LINELEN=64
+CONFIG_NSH_READLINE=y
+CONFIG_OTG_ID_GPIO_DISABLE=y
+CONFIG_PREALLOC_TIMERS=4
+CONFIG_RAM_SIZE=245760
+CONFIG_RAM_START=0x20010000
+CONFIG_RAW_BINARY=y
+CONFIG_RR_INTERVAL=200
+CONFIG_RTC_ALARM=y
+CONFIG_RTC_DATETIME=y
+CONFIG_RTC_DRIVER=y
+CONFIG_SCHED_WAITPID=y
+CONFIG_START_DAY=6
+CONFIG_START_MONTH=12
+CONFIG_START_YEAR=2011
+CONFIG_STM32H7_HSI48=y
+CONFIG_STM32H7_OTGFS=y
+CONFIG_STM32H7_PWR=y
+CONFIG_STM32H7_RTC=y
+CONFIG_STM32H7_USART1=y
+CONFIG_SYSTEM_NSH=y
+CONFIG_TASK_NAME_SIZE=0
+CONFIG_USBDEV=y
diff --git a/boards/arm/stm32h7/linum-stm32h753bi/include/board.h 
b/boards/arm/stm32h7/linum-stm32h753bi/include/board.h
index fae9926bc5..71f067f321 100644
--- a/boards/arm/stm32h7/linum-stm32h753bi/include/board.h
+++ b/boards/arm/stm32h7/linum-stm32h753bi/include/board.h
@@ -379,6 +379,11 @@
 
 #endif
 
+/* OTGFS */
+
+#define GPIO_OTGFS_DM  (GPIO_OTGFS_DM_0  | GPIO_SPEED_100MHz)
+#define GPIO_OTGFS_DP  (GPIO_OTGFS_DP_0  | GPIO_SPEED_100MHz)
+
 /* I2C4 - Used by Touchscreen and Audio Codec */
 
 #define GPIO_I2C4_SCL    (GPIO_I2C4_SCL_1 | GPIO_SPEED_50MHz)  /* PD12 */
diff --git a/boards/arm/stm32h7/linum-stm32h753bi/src/CMakeLists.txt 
b/boards/arm/stm32h7/linum-stm32h753bi/src/CMakeLists.txt
index f086c2a93b..e269c9b372 100644
--- a/boards/arm/stm32h7/linum-stm32h753bi/src/CMakeLists.txt
+++ b/boards/arm/stm32h7/linum-stm32h753bi/src/CMakeLists.txt
@@ -32,6 +32,10 @@ if(CONFIG_BOARDCTL)
   list(APPEND SRCS stm32_appinitialize.c)
 endif()
 
+if(CONFIG_STM32H7_OTGFS)
+  list(APPEND SRCS stm32_usb.c)
+endif()
+
 target_sources(board PRIVATE ${SRCS})
 
 set_property(GLOBAL PROPERTY LD_SCRIPT "${NUTTX_BOARD_DIR}/scripts/flash.ld")
diff --git a/boards/arm/stm32h7/linum-stm32h753bi/src/Makefile 
b/boards/arm/stm32h7/linum-stm32h753bi/src/Makefile
index c4a2f888c2..9e92b0a35a 100644
--- a/boards/arm/stm32h7/linum-stm32h753bi/src/Makefile
+++ b/boards/arm/stm32h7/linum-stm32h753bi/src/Makefile
@@ -30,6 +30,10 @@ else
   endif
 endif
 
+ifeq ($(CONFIG_STM32H7_OTGFS),y)
+CSRCS += stm32_usb.c
+endif
+
 ifeq ($(CONFIG_BOARDCTL),y)
 CSRCS += stm32_appinitialize.c
 endif
diff --git a/boards/arm/stm32h7/linum-stm32h753bi/src/linum-stm32h753bi.h 
b/boards/arm/stm32h7/linum-stm32h753bi/src/linum-stm32h753bi.h
index 99bf68190b..a40c3cda63 100644
--- a/boards/arm/stm32h7/linum-stm32h753bi/src/linum-stm32h753bi.h
+++ b/boards/arm/stm32h7/linum-stm32h753bi/src/linum-stm32h753bi.h
@@ -56,6 +56,28 @@
 #  undef HAVE_RTC_DRIVER
 #endif
 
+/* USB OTG FS
+ *
+ * PA9   OTG_FS_VBUS VBUS sensing
+ * PI12  OTG_FS_PowerSwitchOn
+ * PI13  OTG_FS_Overcurrent
+ */
+
+#define GPIO_OTGFS_VBUS   (GPIO_INPUT|GPIO_FLOAT|GPIO_SPEED_100MHz| \
+                           GPIO_OPENDRAIN|GPIO_PORTA|GPIO_PIN9)
+
+#define GPIO_OTGFS_PWRON  (GPIO_OUTPUT|GPIO_FLOAT|GPIO_SPEED_100MHz|  \
+                           GPIO_PUSHPULL|GPIO_PORTI|GPIO_PIN12)
+
+#ifdef CONFIG_USBHOST
+#  define GPIO_OTGFS_OVER (GPIO_INPUT|GPIO_EXTI|GPIO_FLOAT| \
+                           GPIO_SPEED_100MHz|GPIO_PUSHPULL| \
+                           GPIO_PORTI|GPIO_PIN13)
+#else
+#  define GPIO_OTGFS_OVER (GPIO_INPUT|GPIO_FLOAT|GPIO_SPEED_100MHz| \
+                           GPIO_PUSHPULL|GPIO_PORTI|GPIO_PIN13)
+#endif
+
 /****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/
@@ -77,4 +99,17 @@
 
 int stm32_bringup(void);
 
+/****************************************************************************
+ * Name: stm32_usbinitialize
+ *
+ * Description:
+ *   Called from stm32_usbinitialize very early in inialization to setup
+ *   USB-related GPIO pins for the LINUM-STM32H753BI board.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_STM32H7_OTGFS
+void weak_function stm32_usbinitialize(void);
+#endif
+
 #endif /* __BOARDS_ARM_STM32H7_LINUM_STM32H753BI_SRC_LINUM_STM32H753BI_H */
diff --git a/boards/arm/stm32h7/linum-stm32h753bi/src/stm32_usb.c 
b/boards/arm/stm32h7/linum-stm32h753bi/src/stm32_usb.c
new file mode 100644
index 0000000000..98cadf4a9f
--- /dev/null
+++ b/boards/arm/stm32h7/linum-stm32h753bi/src/stm32_usb.c
@@ -0,0 +1,320 @@
+/****************************************************************************
+ * boards/arm/stm32h7/linum-stm32h753bi/src/stm32_usb.c
+ *
+ * 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/types.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <sched.h>
+#include <errno.h>
+#include <assert.h>
+#include <debug.h>
+
+#include <nuttx/kthread.h>
+#include <nuttx/usb/usbdev.h>
+#include <nuttx/usb/usbhost.h>
+#include <nuttx/usb/usbdev_trace.h>
+
+#include "arm_internal.h"
+#include "chip.h"
+#include "stm32_gpio.h"
+#include "stm32_otg.h"
+#include "linum-stm32h753bi.h"
+
+#ifdef CONFIG_STM32H7_OTGFS
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#if defined(CONFIG_USBDEV) || defined(CONFIG_USBHOST)
+#  define HAVE_USB 1
+#else
+#  warning "CONFIG_STM32_OTGFS is enabled but neither CONFIG_USBDEV nor 
CONFIG_USBHOST"
+#  undef HAVE_USB
+#endif
+
+#ifndef CONFIG_LINUM_STM32H753BI_USBHOST_PRIO
+#  define CONFIG_LINUM_STM32H753BI_USBHOST_PRIO 100
+#endif
+
+#ifndef CONFIG_LINUM_STM32H753BI_USBHOST_STACKSIZE
+#  define CONFIG_LINUM_STM32H753BI_USBHOST_STACKSIZE 1024
+#endif
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+#ifdef CONFIG_USBHOST
+static struct usbhost_connection_s *g_usbconn;
+#endif
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: usbhost_waiter
+ *
+ * Description:
+ *   Wait for USB devices to be connected.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_USBHOST
+static int usbhost_waiter(int argc, char *argv[])
+{
+  struct usbhost_hubport_s *hport;
+
+  uinfo("Running\n");
+  for (; ; )
+    {
+      /* Wait for the device to change state */
+
+      DEBUGVERIFY(CONN_WAIT(g_usbconn, &hport));
+      uinfo("%s\n", hport->connected ? "connected" : "disconnected");
+
+      /* Did we just become connected? */
+
+      if (hport->connected)
+        {
+          /* Yes.. enumerate the newly connected device */
+
+          CONN_ENUMERATE(g_usbconn, hport);
+        }
+    }
+
+  /* Keep the compiler from complaining */
+
+  return 0;
+}
+#endif
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: stm32_usbinitialize
+ *
+ * Description:
+ *   Called from stm32_usbinitialize very early in inialization to setup
+ *   USB-related GPIO pins for the linum-stm32h753bi board.
+ *
+ ****************************************************************************/
+
+void stm32_usbinitialize(void)
+{
+  /* The OTG FS has an internal soft pull-up.
+   * No GPIO configuration is required
+   */
+
+  /* Configure the OTG FS VBUS sensing GPIO,
+   * Power On, and Overcurrent GPIOs
+   */
+
+#ifdef CONFIG_STM32H7_OTGFS
+  stm32_configgpio(GPIO_OTGFS_VBUS);
+  stm32_configgpio(GPIO_OTGFS_PWRON);
+  stm32_configgpio(GPIO_OTGFS_OVER);
+#endif
+}
+
+/****************************************************************************
+ * Name: stm32_usbhost_initialize
+ *
+ * Description:
+ *   Called at application startup time to initialize the USB host
+ *   functionality.
+ *   This function will start a thread that will monitor for device
+ *   connection/disconnection events.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_USBHOST
+int stm32_usbhost_initialize(void)
+{
+  int ret;
+
+  /* First, register all of the class drivers needed to support the drivers
+   * that we care about:
+   */
+
+  uinfo("Register class drivers\n");
+
+#ifdef CONFIG_USBHOST_HUB
+  /* Initialize USB hub class support */
+
+  ret = usbhost_hub_initialize();
+  if (ret < 0)
+    {
+      uerr("ERROR: usbhost_hub_initialize failed: %d\n", ret);
+    }
+#endif
+
+#ifdef CONFIG_USBHOST_MSC
+  /* Register the USB mass storage class class */
+
+  ret = usbhost_msc_initialize();
+  if (ret != OK)
+    {
+      uerr("ERROR: Failed to register the mass storage class: %d\n", ret);
+    }
+#endif
+
+#ifdef CONFIG_USBHOST_CDCACM
+  /* Register the CDC/ACM serial class */
+
+  ret = usbhost_cdcacm_initialize();
+  if (ret != OK)
+    {
+      uerr("ERROR: Failed to register the CDC/ACM serial class: %d\n", ret);
+    }
+#endif
+
+#ifdef CONFIG_USBHOST_HIDKBD
+  /* Initialize the HID keyboard class */
+
+  ret = usbhost_kbdinit();
+  if (ret != OK)
+    {
+      uerr("ERROR: Failed to register the HID keyboard class\n");
+    }
+#endif
+
+#ifdef CONFIG_USBHOST_HIDMOUSE
+  /* Initialize the HID mouse class */
+
+  ret = usbhost_mouse_init();
+  if (ret != OK)
+    {
+      uerr("ERROR: Failed to register the HID mouse class\n");
+    }
+#endif
+
+  /* Then get an instance of the USB host interface */
+
+  uinfo("Initialize USB host\n");
+  g_usbconn = stm32_otgfshost_initialize(0);
+  if (g_usbconn)
+    {
+      /* Start a thread to handle device connection. */
+
+      uinfo("Start usbhost_waiter\n");
+
+      ret = kthread_create("usbhost", CONFIG_LINUM_STM32H753BI_USBHOST_PRIO,
+                           CONFIG_LINUM_STM32H753BI_USBHOST_STACKSIZE,
+                           usbhost_waiter, NULL);
+      return ret < 0 ? -ENOEXEC : OK;
+    }
+
+  return -ENODEV;
+}
+#endif
+
+/****************************************************************************
+ * Name: stm32_usbhost_vbusdrive
+ *
+ * Description:
+ *   Enable/disable driving of VBUS 5V output. This function must be
+ *   provided be each platform that implements the STM32 OTG FS host
+ *   interface
+ *
+ *   "On-chip 5 V VBUS generation is not supported. For this reason, a
+ *    charge pump or, if 5 V are available on the application board, a
+ *    basic power switch, must be added externally to drive the 5 V VBUS
+ *    line. The external charge pump can be driven by any GPIO output.
+ *    When the application decides to power on VBUS using the chosen GPIO,
+ *    it must also set the port power bit in the host port control and status
+ *    register (PPWR bit in OTG_FS_HPRT).
+ *
+ *   "The application uses this field to control power to this port, and
+ *    the core clears this bit on an overcurrent condition."
+ *
+ * Input Parameters:
+ *   iface - For future growth to handle multiple USB host interface.
+ *           Should be zero.
+ *   enable - true: enable VBUS power; false: disable VBUS power
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_USBHOST
+void stm32_usbhost_vbusdrive(int iface, bool enable)
+{
+  DEBUGASSERT(iface == 0);
+
+  /* Set the Power Switch by driving the active high enable pin */
+
+  stm32_gpiowrite(GPIO_OTGFS_PWRON, enable);
+}
+#endif
+
+/****************************************************************************
+ * Name: stm32_setup_overcurrent
+ *
+ * Description:
+ *   Setup to receive an interrupt-level callback if an overcurrent
+ *   condition is detected.
+ *
+ * Input Parameters:
+ *   handler - New overcurrent interrupt handler
+ *   arg     - The argument provided for the interrupt handler
+ *
+ * Returned Value:
+ *   Zero (OK) is returned on success.  Otherwise, a negated errno value
+ *   is returned to indicate the nature of the failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_USBHOST
+int stm32_setup_overcurrent(xcpt_t handler, void *arg)
+{
+  return stm32_gpiosetevent(GPIO_OTGFS_OVER, true, true, true, handler, arg);
+}
+#endif
+
+/****************************************************************************
+ * Name:  stm32_usbsuspend
+ *
+ * Description:
+ *   Board logic must provide the stm32_usbsuspend logic if the USBDEV
+ *   driver is used. This function is called whenever the USB enters or
+ *   leaves suspend mode. This is an opportunity for the board logic to
+ *   shutdown clocks, power, etc. while the USB is suspended.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_USBDEV
+void stm32_usbsuspend(struct usbdev_s *dev, bool resume)
+{
+  uinfo("resume: %d\n", resume);
+}
+#endif
+
+#endif /* CONFIG_STM32_OTGFS */

Reply via email to