Skip to content

Commit

Permalink
tinyusb: Add support for LPC55xx devices
Browse files Browse the repository at this point in the history
This adds code required by TinyUSB to configure
USB clocks, pins and interrupts.

Signed-off-by: Jerzy Kasenberg <[email protected]>
  • Loading branch information
kasjer committed Aug 22, 2024
1 parent 273851f commit 8c97aeb
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 1 deletion.
8 changes: 7 additions & 1 deletion hw/mcu/nxp/mcux-sdk/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -1221,3 +1224,6 @@ syscfg.defs:
MCUX_DRIVERS_XRDC2:
description: Add drivers/xrdc2 to the build
value:

syscfg.vals.MCU_LPC55S28:
MCU_LPC55XX: 1
92 changes: 92 additions & 0 deletions hw/usb/tinyusb/lpc55/include/tusb_hw.h
Original file line number Diff line number Diff line change
@@ -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
33 changes: 33 additions & 0 deletions hw/usb/tinyusb/lpc55/pkg.yml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"
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"
112 changes: 112 additions & 0 deletions hw/usb/tinyusb/lpc55/src/lpc55.c
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions hw/usb/tinyusb/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 8c97aeb

Please sign in to comment.