You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by je...@apache.org on 2024/09/10 07:22:01 UTC

(mynewt-core) 04/04: tinyusb: Add support for LPC55xx devices

This is an automated email from the ASF dual-hosted git repository.

jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit 6a86c2b6b021456da8fafd008cdc27ec730e5a2e
Author: Jerzy Kasenberg <je...@codecoup.pl>
AuthorDate: Thu Aug 22 10:58:52 2024 +0200

    tinyusb: Add support for LPC55xx devices
    
    This adds code required by TinyUSB to configure
    USB clocks, pins and interrupts.
    
    Signed-off-by: Jerzy Kasenberg <je...@codecoup.pl>
---
 hw/mcu/nxp/mcux-sdk/syscfg.yml         |   8 ++-
 hw/usb/tinyusb/lpc55/include/tusb_hw.h |  92 +++++++++++++++++++++++++++
 hw/usb/tinyusb/lpc55/pkg.yml           |  33 ++++++++++
 hw/usb/tinyusb/lpc55/src/lpc55.c       | 112 +++++++++++++++++++++++++++++++++
 hw/usb/tinyusb/pkg.yml                 |   2 +
 5 files changed, 246 insertions(+), 1 deletion(-)

diff --git a/hw/mcu/nxp/mcux-sdk/syscfg.yml b/hw/mcu/nxp/mcux-sdk/syscfg.yml
index 69bdfe1b0..6634813fe 100644
--- a/hw/mcu/nxp/mcux-sdk/syscfg.yml
+++ b/hw/mcu/nxp/mcux-sdk/syscfg.yml
@@ -519,7 +519,10 @@ syscfg.defs:
         description: MCU MKW41Z4 is used.
         value:
     MCU_LPCXXXX:
-        description: LPC_XXXXX MCU
+        description: LPCXXX MCU
+        value:
+    MCU_LPC55XX:
+        description: LPC55XX MCU
         value:
 
     MCUX_DRIVERS_ACMP:
@@ -1221,3 +1224,6 @@ syscfg.defs:
     MCUX_DRIVERS_XRDC2:
         description: Add drivers/xrdc2 to the build
         value:
+
+syscfg.vals.MCU_LPC55S28:
+    MCU_LPC55XX: 1
diff --git a/hw/usb/tinyusb/lpc55/include/tusb_hw.h b/hw/usb/tinyusb/lpc55/include/tusb_hw.h
new file mode 100644
index 000000000..55eaa57a1
--- /dev/null
+++ b/hw/usb/tinyusb/lpc55/include/tusb_hw.h
@@ -0,0 +1,92 @@
+/*
+ * 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 __TUSB_HW_H__
+#define __TUSB_HW_H__
+
+#include <syscfg/syscfg.h>
+
+#define CFG_TUSB_MEM_ALIGN __attribute__((aligned(64)))
+#define CFG_TUSB_MEM_SECTION __attribute__((section(".usbram")))
+
+#if MYNEWT_VAL(MCU_LPC55XX)
+#define CFG_TUSB_MCU OPT_MCU_LPC55XX
+#endif
+
+#if defined(MYNEWT_VAL_USBD_CDC_NOTIFY_EP)
+#define USBD_CDC_NOTIFY_EP      MYNEWT_VAL(USBD_CDC_NOTIFY_EP)
+#else
+#define USBD_CDC_NOTIFY_EP      0x81
+#endif
+
+#if defined(MYNEWT_VAL_USBD_CDC_NOTIFY_EP_SIZE)
+#define USBD_CDC_NOTIFY_EP_SIZE MYNEWT_VAL(USBD_CDC_NOTIFY_EP_SIZE)
+#else
+#define USBD_CDC_NOTIFY_EP_SIZE 0x08
+#endif
+
+#if defined(MYNEWT_VAL_USBD_CDC_DATA_OUT_EP)
+#define USBD_CDC_DATA_OUT_EP    MYNEWT_VAL(USBD_CDC_DATA_OUT_EP)
+#else
+#define USBD_CDC_DATA_OUT_EP    0x01
+#endif
+
+#if defined(MYNEWT_VAL_USBD_CDC_DATA_IN_EP)
+#define USBD_CDC_DATA_IN_EP     MYNEWT_VAL(USBD_CDC_DATA_IN_EP)
+#else
+#define USBD_CDC_DATA_IN_EP     0x82
+#endif
+
+#if defined(MYNEWT_VAL_USBD_CDC_DATA_EP_SIZE)
+#define USBD_CDC_DATA_EP_SIZE   MYNEWT_VAL(USBD_CDC_DATA_EP_SIZE)
+#else
+#define USBD_CDC_DATA_EP_SIZE   0x40
+#endif
+
+#if defined(MYNEWT_VAL_USBD_HID_REPORT_EP)
+#define USBD_HID_REPORT_EP      MYNEWT_VAL(USBD_HID_REPORT_EP)
+#else
+#define USBD_HID_REPORT_EP      0x83
+#endif
+
+#if defined(MYNEWT_VAL_USBD_HID_REPORT_EP_SIZE)
+#define USBD_HID_REPORT_EP_SIZE MYNEWT_VAL(USBD_HID_REPORT_EP_SIZE)
+#else
+#define USBD_HID_REPORT_EP_SIZE 0x10
+#endif
+
+#if defined(MYNEWT_VAL_USBD_HID_REPORT_EP_INTERVAL)
+#define USBD_HID_REPORT_EP_INTERVAL MYNEWT_VAL(USBD_HID_REPORT_EP_INTERVAL)
+#else
+#define USBD_HID_REPORT_EP_INTERVAL 10
+#endif
+
+#if defined(MYNEWT_VAL_USBD_MSC_DATA_IN_EP)
+#define USBD_MSC_DATA_IN_EP     MYNEWT_VAL(USBD_MSC_DATA_IN_EP)
+#else
+#define USBD_MSC_DATA_IN_EP     0x84
+#endif
+
+#if defined(MYNEWT_VAL_USBD_MSC_DATA_OUT_EP)
+#define USBD_MSC_DATA_OUT_EP     MYNEWT_VAL(USBD_MSC_DATA_OUT_EP)
+#else
+#define USBD_MSC_DATA_OUT_EP     0x04
+#endif
+
+#endif
diff --git a/hw/usb/tinyusb/lpc55/pkg.yml b/hw/usb/tinyusb/lpc55/pkg.yml
new file mode 100644
index 000000000..e38796f29
--- /dev/null
+++ b/hw/usb/tinyusb/lpc55/pkg.yml
@@ -0,0 +1,33 @@
+#
+# 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.
+#
+
+pkg.name: hw/usb/tinyusb/lpc55
+pkg.description: Hardware initialization for USB on LP55xx
+pkg.author: "Apache Mynewt <de...@mynewt.apache.org>"
+pkg.homepage: "http://mynewt.apache.org/"
+pkg.keywords:
+    - usb
+    - tinyusb
+
+pkg.apis:
+    - TINYUSB_HW_INIT
+
+pkg.deps:
+    - "@apache-mynewt-core/kernel/os"
+    - "@apache-mynewt-core/hw/usb/tinyusb"
diff --git a/hw/usb/tinyusb/lpc55/src/lpc55.c b/hw/usb/tinyusb/lpc55/src/lpc55.c
new file mode 100644
index 000000000..156fdb988
--- /dev/null
+++ b/hw/usb/tinyusb/lpc55/src/lpc55.c
@@ -0,0 +1,112 @@
+/*
+ * 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.
+ */
+
+#include <os/mynewt.h>
+#include <mcu/mcu.h>
+
+#include <tusb.h>
+
+#include <clock_config.h>
+#include <fsl_clock.h>
+#include <fsl_iocon.h>
+#include <fsl_power.h>
+
+void USB0_IRQHandler(void) {
+    tud_int_handler(0);
+}
+
+void USB1_IRQHandler(void) {
+    tud_int_handler(1);
+}
+
+void
+tinyusb_hardware_init(void)
+{
+    CLOCK_EnableClock(kCLOCK_Iocon);
+
+    NVIC_SetVector(USB0_IRQn, (uint32_t)USB0_IRQHandler);
+    NVIC_SetPriority(USB0_IRQn, 2);
+    NVIC_SetVector(USB1_IRQn, (uint32_t)USB1_IRQHandler);
+    NVIC_SetPriority(USB1_IRQn, 2);
+
+    /* PORT0 PIN22 configured as USB0_VBUS */
+    IOCON_PinMuxSet(IOCON, 0U, 22U, IOCON_FUNC7 | IOCON_DIGITAL_EN);
+
+#if MYNEWT_VAL_CHOICE(USBD_RHPORT, USB0)
+    /* Port0 is Full Speed */
+
+    /* Turn on USB0 Phy */
+    POWER_DisablePD(kPDRUNCFG_PD_USB0_PHY);
+
+    /* reset the IP to make sure it's in reset state. */
+    RESET_PeripheralReset(kUSB0D_RST_SHIFT_RSTn);
+    RESET_PeripheralReset(kUSB0HSL_RST_SHIFT_RSTn);
+    RESET_PeripheralReset(kUSB0HMR_RST_SHIFT_RSTn);
+
+    /* Enable USB Clock Adjustments to trim the FRO for the full speed controller */
+    ANACTRL->FRO192M_CTRL |= ANACTRL_FRO192M_CTRL_USBCLKADJ_MASK;
+    CLOCK_SetClkDiv(kCLOCK_DivUsb0Clk, 1, false);
+    CLOCK_AttachClk(kFRO_HF_to_USB0_CLK);
+
+    /* According to reference manual, device mode setting has to be set by access usb host register */
+    CLOCK_EnableClock(kCLOCK_Usbhsl0);  /* enable usb0 host clock */
+    USBFSH->PORTMODE |= USBFSH_PORTMODE_DEV_ENABLE_MASK;
+    CLOCK_DisableClock(kCLOCK_Usbhsl0); /* disable usb0 host clock */
+
+    /* enable USB Device clock */
+    CLOCK_EnableUsbfs0DeviceClock(kCLOCK_UsbfsSrcFro, CLOCK_GetFreq(kCLOCK_FroHf));
+#endif
+
+#if MYNEWT_VAL_CHOICE(USBD_RHPORT, USB1)
+    /* Port1 is High Speed */
+
+    /* Turn on USB1 Phy */
+    POWER_DisablePD(kPDRUNCFG_PD_USB1_PHY);
+
+    /* reset the IP to make sure it's in reset state. */
+    RESET_PeripheralReset(kUSB1H_RST_SHIFT_RSTn);
+    RESET_PeripheralReset(kUSB1D_RST_SHIFT_RSTn);
+    RESET_PeripheralReset(kUSB1_RST_SHIFT_RSTn);
+    RESET_PeripheralReset(kUSB1RAM_RST_SHIFT_RSTn);
+
+    /* According to reference manual, device mode setting has to be set by access usb host register */
+    CLOCK_EnableClock(kCLOCK_Usbh1); // enable usb0 host clock
+
+    /* Put PHY power down under software control */
+    USBHSH->PORTMODE = USBHSH_PORTMODE_SW_PDCOM_MASK;
+    USBHSH->PORTMODE |= USBHSH_PORTMODE_DEV_ENABLE_MASK;
+
+    /* disable usb0 host clock */
+    CLOCK_DisableClock(kCLOCK_Usbh1);
+
+    /* enable USB Device clock */
+    CLOCK_EnableUsbhs0PhyPllClock(kCLOCK_UsbPhySrcExt, BOARD_XTAL0_CLK_HZ);
+    CLOCK_EnableUsbhs0DeviceClock(kCLOCK_UsbSrcUnused, 0U);
+    CLOCK_EnableClock(kCLOCK_UsbRam1);
+
+    /* Enable PHY support for Low speed device + LS via FS Hub */
+    USBPHY->CTRL |= USBPHY_CTRL_SET_ENUTMILEVEL2_MASK | USBPHY_CTRL_SET_ENUTMILEVEL3_MASK;
+
+    /* Enable all power for normal operation */
+    USBPHY->PWD = 0;
+
+    USBPHY->CTRL_SET = USBPHY_CTRL_SET_ENAUTOCLR_CLKGATE_MASK;
+    USBPHY->CTRL_SET = USBPHY_CTRL_SET_ENAUTOCLR_PHY_PWD_MASK;
+#endif
+}
diff --git a/hw/usb/tinyusb/pkg.yml b/hw/usb/tinyusb/pkg.yml
index 2f0e41830..0f5e94145 100644
--- a/hw/usb/tinyusb/pkg.yml
+++ b/hw/usb/tinyusb/pkg.yml
@@ -44,6 +44,8 @@ pkg.deps.'MCU_TARGET == "nRF52840"':
     - "@apache-mynewt-core/hw/usb/tinyusb/nrf5x"
 pkg.deps.'MCU_TARGET == "nRF5340_APP"':
     - "@apache-mynewt-core/hw/usb/tinyusb/nrf53"
+pkg.deps.MCU_LPC55XX:
+    - "@apache-mynewt-core/hw/usb/tinyusb/lpc55"
 pkg.deps.MCU_STM32F4:
     - "@apache-mynewt-core/hw/usb/tinyusb/synopsys"
 pkg.deps.MCU_STM32H7: