diff --git a/Kconfig b/Kconfig index 811676a7..0084b833 100644 --- a/Kconfig +++ b/Kconfig @@ -40,6 +40,8 @@ if CHERRYUSB bool "dwc2_at" config CHERRYUSB_DEVICE_DWC2_HC bool "dwc2_hc" + config CHERRYUSB_DEVICE_DWC2_NATION + bool "dwc2_nation" config CHERRYUSB_DEVICE_DWC2_GD bool "dwc2_gd" config CHERRYUSB_DEVICE_DWC2_CUSTOM @@ -199,6 +201,8 @@ if CHERRYUSB bool "dwc2_kendryte" config CHERRYUSB_HOST_DWC2_HC bool "dwc2_hc" + config CHERRYUSB_HOST_DWC2_NATION + bool "dwc2_nation" config CHERRYUSB_HOST_DWC2_CUSTOM bool "dwc2_custom" config CHERRYUSB_HOST_MUSB_ES diff --git a/Kconfig.rtt b/Kconfig.rtt index 75681edf..4c1586e2 100644 --- a/Kconfig.rtt +++ b/Kconfig.rtt @@ -40,6 +40,8 @@ if RT_USING_CHERRYUSB bool "dwc2_at" config RT_CHERRYUSB_DEVICE_DWC2_HC bool "dwc2_hc" + config RT_CHERRYUSB_DEVICE_DWC2_NATION + bool "dwc2_nation" config RT_CHERRYUSB_DEVICE_DWC2_GD bool "dwc2_gd" config RT_CHERRYUSB_DEVICE_DWC2_CUSTOM @@ -203,6 +205,8 @@ if RT_USING_CHERRYUSB bool "dwc2_kendryte" config RT_CHERRYUSB_HOST_DWC2_HC bool "dwc2_hc" + config RT_CHERRYUSB_HOST_DWC2_NATION + bool "dwc2_nation" config RT_CHERRYUSB_HOST_DWC2_CUSTOM bool "dwc2_custom" config RT_CHERRYUSB_HOST_MUSB_ES diff --git a/Kconfig.rttpkg b/Kconfig.rttpkg index 74c99aae..2e328b85 100644 --- a/Kconfig.rttpkg +++ b/Kconfig.rttpkg @@ -41,6 +41,8 @@ if PKG_USING_CHERRYUSB bool "dwc2_at" config PKG_CHERRYUSB_DEVICE_DWC2_HC bool "dwc2_hc" + config PKG_CHERRYUSB_DEVICE_DWC2_NATION + bool "dwc2_nation" config PKG_CHERRYUSB_DEVICE_DWC2_GD bool "dwc2_gd" config PKG_CHERRYUSB_DEVICE_DWC2_CUSTOM @@ -202,6 +204,8 @@ if PKG_USING_CHERRYUSB bool "dwc2_kendryte" config PKG_CHERRYUSB_HOST_DWC2_HC bool "dwc2_hc" + config PKG_CHERRYUSB_HOST_DWC2_NATION + bool "dwc2_nation" config PKG_CHERRYUSB_HOST_DWC2_CUSTOM bool "dwc2_custom" config PKG_CHERRYUSB_HOST_MUSB_ES diff --git a/README.md b/README.md index a7c85d2c..6c8ddfa7 100644 --- a/README.md +++ b/README.md @@ -194,6 +194,7 @@ TODO |Espressif | esp32s2/esp32s3/esp32p4 | dwc2 |[esp32_repo](https://github.com/CherryUSB/cherryusb_esp32)|<= latest | Long-term | |NXP | mcx | kinetis/chipidea/ehci |[nxp_mcx_repo](https://github.com/CherryUSB/cherryusb_mcx)|<= latest | Long-term | |Kendryte | k230 | dwc2 |[k230_repo](https://github.com/CherryUSB/k230_sdk)|v1.2.0 | Long-term | +|Nationstech | n32h4x | dwc2 |[nation_repo](https://github.com/CherryUSB/cherryusb_nation)|>=1.5.0 | Long-term | |Raspberry pi | rp2040/rp2350 | rp2040 |[pico-examples](https://github.com/CherryUSB/pico-examples)|<= latest | Long-term | |AllwinnerTech | F1C100S/F1C200S | musb |[cherryusb_rtt_f1c100s](https://github.com/CherryUSB/cherryusb_rtt_f1c100s)|<= latest | the same with musb | |Bekencorp | bk7256/bk7258 | musb |[bk_idk](https://github.com/CherryUSB/bk_idk)| v0.7.0 | the same with musb | @@ -221,3 +222,4 @@ CherryUSB discord: https://discord.com/invite/wFfvrSAey8. Thanks to the following companies for their support (in no particular order): + diff --git a/README_zh.md b/README_zh.md index 45554a53..4afcb995 100644 --- a/README_zh.md +++ b/README_zh.md @@ -194,6 +194,7 @@ TODO |Espressif | esp32s2/esp32s3/esp32p4 | dwc2 |[esp32_repo](https://github.com/CherryUSB/cherryusb_esp32)|<= latest | Long-term | |NXP | mcx | kinetis/chipidea/ehci |[nxp_mcx_repo](https://github.com/CherryUSB/cherryusb_mcx)|<= latest | Long-term | |Kendryte | k230 | dwc2 |[k230_repo](https://github.com/CherryUSB/k230_sdk)|v1.2.0 | Long-term | +|Nationstech | n32h4x | dwc2 |[nation_repo](https://github.com/CherryUSB/cherryusb_nation)|>=1.5.0 | Long-term | |Raspberry pi | rp2040/rp2350 | rp2040 |[pico-examples](https://github.com/CherryUSB/pico-examples)|<= latest | Long-term | |AllwinnerTech | F1C100S/F1C200S | musb |[cherryusb_rtt_f1c100s](https://github.com/CherryUSB/cherryusb_rtt_f1c100s)|<= latest | the same with musb | |Bekencorp | bk7256/bk7258 | musb |[bk_idk](https://github.com/CherryUSB/bk_idk)| v0.7.0 | the same with musb | @@ -223,3 +224,4 @@ CherryUSB 微信群:与我联系后邀请加入 感谢以下企业支持(顺序不分先后): + diff --git a/SConscript b/SConscript index 602ba5fe..93730091 100644 --- a/SConscript +++ b/SConscript @@ -53,6 +53,9 @@ if GetDepend(['PKG_CHERRYUSB_DEVICE']): if GetDepend(['PKG_CHERRYUSB_DEVICE_DWC2_HC']): src += Glob('port/dwc2/usb_dc_dwc2.c') src += Glob('port/dwc2/usb_glue_hc.c') + if GetDepend(['PKG_CHERRYUSB_DEVICE_DWC2_NATION']): + src += Glob('port/dwc2/usb_dc_dwc2.c') + src += Glob('port/dwc2/usb_glue_nation.c') if GetDepend(['PKG_CHERRYUSB_DEVICE_DWC2_GD']): src += Glob('port/dwc2/usb_dc_dwc2.c') src += Glob('port/dwc2/usb_glue_gd.c') @@ -197,6 +200,9 @@ if GetDepend(['PKG_CHERRYUSB_HOST']): if GetDepend(['PKG_CHERRYUSB_HOST_DWC2_HC']): src += Glob('port/dwc2/usb_hc_dwc2.c') src += Glob('port/dwc2/usb_glue_hc.c') + if GetDepend(['PKG_CHERRYUSB_HOST_DWC2_NATION']): + src += Glob('port/dwc2/usb_hc_dwc2.c') + src += Glob('port/dwc2/usb_glue_nation.c') if GetDepend(['PKG_CHERRYUSB_HOST_DWC2_CUSTOM']): src += Glob('port/dwc2/usb_hc_dwc2.c') if GetDepend(['PKG_CHERRYUSB_HOST_MUSB_STANDARD']): diff --git a/docs/assets/nationstech.jpg b/docs/assets/nationstech.jpg new file mode 100644 index 00000000..2c48f6b9 Binary files /dev/null and b/docs/assets/nationstech.jpg differ diff --git a/port/dwc2/README.md b/port/dwc2/README.md index b35a7a03..5e61d1c4 100644 --- a/port/dwc2/README.md +++ b/port/dwc2/README.md @@ -44,3 +44,7 @@ CONFIG_USBDEV_EP_NUM 必须为4 或者 6,并删除 usb_dc_dwc2.c 中 while(1){ ## Kendryte - K230 + +## Nationstech + +- N32H4X \ No newline at end of file diff --git a/port/dwc2/usb_glue_nation.c b/port/dwc2/usb_glue_nation.c new file mode 100644 index 00000000..f899e738 --- /dev/null +++ b/port/dwc2/usb_glue_nation.c @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2025, sakumisu + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "usbd_core.h" +#include "usbh_core.h" + +__WEAK void USBD_IRQHandler(uint8_t busid) +{ +} + +__WEAK void USBH_IRQHandler(uint8_t busid) +{ +} + +#if defined(N32H473) || defined(N32H474) || defined(N32H475) || defined(N32H482) || defined(N32H487) +#include "n32h47x_48x_rcc.h" +#include "n32h47x_48x_gpio.h" +#include "n32h47x_48x_exti.h" +#include "misc.h" + +typedef void (*usb_dwc2_irq)(uint8_t busid); + +static usb_dwc2_irq g_usb_dwc2_irq; +static volatile uint8_t g_usb_dwc2_busid = 0; + +static void usbhs_common_init(void) +{ + RCC_EnableAHB1PeriphClk(RCC_AHB_PERIPHEN_GPIOA | RCC_AHB_PERIPHEN_GPIOB, ENABLE); + RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO, ENABLE); + /* PLL= 240MHz */ +#if (HSE_VALUE == 8000000) + /* Select PLL clock as USBHS clock source , clock 15 divisions = 16M */ + RCC_ConfigUSBPLLPresClk(RCC_USBPLLCLK_SRC_PLL, RCC_USBPLLCLK_DIV15); + + /* Select the corresponding bandwidth and frequency*/ + RCC_ConfigUSBHSBandwidth(RCC_USBHS_BW_16M); + + /* Select USBHS clock source frequency */ + RCC_ConfigUSBHSFrequency(RCC_USBHS_FREQ_16_OR_32M); + + /* Select PLL as USBHS clock */ + RCC_ConfigUSBHSClk(RCC_USBHS_CLKSRC_PLLPRES); +#else //HSE 16M + RCC_ConfigUSBHSBandwidth(RCC_USBHS_BW_16M); + RCC_ConfigUSBHSFrequency(RCC_USBHS_FREQ_16_OR_32M); + RCC_ConfigUSBHSClk(RCC_USBHS_CLKSRC_HSE); +#endif + /* Reset the USBHS phy clock*/ + RCC_EnableAHBPeriphReset(RCC_AHBPRST_USBHSPHYRST); + /* Enables the USBHS peripheral clock*/ + RCC_EnableAHBPeriphClk(RCC_AHB_PERIPHEN_USBHS, ENABLE); + + NVIC_InitType NVIC_InitStructure; + // EXTI_InitType EXTI_InitStructure; + + // NVIC_InitStructure.NVIC_IRQChannel = USB_HS_WKUP_IRQn; + // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; + // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; + // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + // NVIC_Init(&NVIC_InitStructure); + + NVIC_InitStructure.NVIC_IRQChannel = USB_HS_IRQn; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; + NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + NVIC_Init(&NVIC_InitStructure); + + /* Configure the EXTI line 23 connected internally to the USBHS IP */ + // EXTI_ClrITPendBit(EXTI_LINE23); + // EXTI_InitStructure.EXTI_Line = EXTI_LINE23; + // EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; + // EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; + // EXTI_InitStructure.EXTI_LineCmd = ENABLE; + // EXTI_InitPeripheral(&EXTI_InitStructure); +} + +static void usbhs_gpio_init(void) +{ + GPIO_InitType GPIO_InitStructure; + + RCC_EnableAHB1PeriphClk(RCC_AHB_PERIPHEN_GPIOA | RCC_AHB_PERIPHEN_GPIOB | RCC_AHB_PERIPHEN_GPIOE, ENABLE); + RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO, ENABLE); + GPIO_ConfigPinRemap(0, 0, GPIO_RMP_SWJ_SWD); + + GPIO_InitStruct(&GPIO_InitStructure); +#ifdef USBHS_HOST + GPIO_InitStructure.Pin = GPIO_PIN_1; + GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUTPUT_PP; + GPIO_InitPeripheral(GPIOE, &GPIO_InitStructure); + GPIO_ResetBits(GPIOE, GPIO_PIN_1); + + GPIO_InitStructure.Pin = GPIO_PIN_3; + GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure); + GPIO_ResetBits(GPIOB, GPIO_PIN_3); +#endif + // VBUS + // GPIO_InitStructure.Pin = GPIO_PIN_13; + // GPIO_InitStructure.GPIO_Mode = GPIO_MODE_INPUT; + // GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure); + + // // SOF + // GPIO_InitStructure.Pin = GPIO_PIN_4; + // GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP; + // GPIO_InitStructure.GPIO_Alternate = 8; // SOF + // GPIO_InitPeripheral(GPIOA, &GPIO_InitStructure); + + // // ID + // GPIO_InitStructure.Pin = GPIO_PIN_12; + // GPIO_InitStructure.GPIO_Mode = GPIO_MODE_AF_PP; + // GPIO_InitStructure.GPIO_Alternate = 14; // ID + // GPIO_InitPeripheral(GPIOB, &GPIO_InitStructure); +} + +void usb_dc_low_level_init(uint8_t busid) +{ + g_usb_dwc2_irq = USBD_IRQHandler; + g_usb_dwc2_busid = busid; + + usbhs_common_init(); + + RCC->USBHSCTRL2 |= (uint32_t)0x01 << 19; + RCC->USBHSCTRL1 |= (uint32_t)0x01 << 31; +} + +void usb_dc_low_level_deinit(uint8_t busid) +{ + NVIC_InitType NVIC_InitStructure; + + NVIC_InitStructure.NVIC_IRQChannel = USB_HS_IRQn; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; + NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE; + NVIC_Init(&NVIC_InitStructure); + + RCC_EnableAHBPeriphReset(RCC_AHBPRST_USBHSPHYRST); + /* Enables the USBHS peripheral clock*/ + RCC_EnableAHBPeriphClk(RCC_AHB_PERIPHEN_USBHS, DISABLE); +} + +void usb_hc_low_level_init(struct usbh_bus *bus) +{ + g_usb_dwc2_irq = USBH_IRQHandler; + g_usb_dwc2_busid = 0; + + usbhs_common_init(); + + RCC->USBHSCTRL2 &= ~(uint32_t)0x01 << 19; + RCC->USBHSCTRL1 |= (uint32_t)0x01 << 31; +} + +void usb_hc_low_level_deinit(struct usbh_bus *bus) +{ + NVIC_InitType NVIC_InitStructure; + + NVIC_InitStructure.NVIC_IRQChannel = USB_HS_IRQn; + NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; + NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; + NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE; + NVIC_Init(&NVIC_InitStructure); + + RCC_EnableAHBPeriphReset(RCC_AHBPRST_USBHSPHYRST); + /* Enables the USBHS peripheral clock*/ + RCC_EnableAHBPeriphClk(RCC_AHB_PERIPHEN_USBHS, DISABLE); +} + +uint32_t usbd_get_dwc2_gccfg_conf(uint32_t reg_base) +{ + return 0; +} + +uint32_t usbh_get_dwc2_gccfg_conf(uint32_t reg_base) +{ + return 0; +} + +void USB_HS_IRQHandler(void) +{ + g_usb_dwc2_irq(g_usb_dwc2_busid); +} + +void usbd_dwc2_delay_ms(uint8_t ms) +{ + +} +#endif