diff --git a/cherryusb.cmake b/cherryusb.cmake index 3d7c28b3..5734745b 100644 --- a/cherryusb.cmake +++ b/cherryusb.cmake @@ -340,6 +340,10 @@ if(CONFIG_CHERRYUSB_HOST) endif() endif() +if(CONFIG_CHERRYUSB_DEVICE AND CONFIG_CHERRYUSB_HOST) +list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/core/usbotg_core.c) +endif() + if(DEFINED CONFIG_CHERRYUSB_OSAL) if("${CONFIG_CHERRYUSB_OSAL}" STREQUAL "freertos") list(APPEND cherryusb_srcs ${CMAKE_CURRENT_LIST_DIR}/osal/usb_osal_freertos.c) diff --git a/cherryusb_config_template.h b/cherryusb_config_template.h index 2f788eb0..34c19cba 100644 --- a/cherryusb_config_template.h +++ b/cherryusb_config_template.h @@ -318,4 +318,7 @@ #define usb_ramaddr2phyaddr(addr) (addr) #endif +/* Enable OTG support, only support hpmicro now */ +// #define CONFIG_USB_OTG_ENABLE + #endif diff --git a/common/usb_otg.h b/common/usb_otg.h index 6adf5451..032d2e9f 100644 --- a/common/usb_otg.h +++ b/common/usb_otg.h @@ -12,6 +12,10 @@ extern "C" { #endif +#define USBOTG_MODE_HOST 0 +#define USBOTG_MODE_DEVICE 1 +#define USBOTG_MODE_OTG 2 + /** * @brief usb otg controller hardware or gpio id simulator init. * @@ -24,12 +28,6 @@ int usb_otg_init(uint8_t busid); * @return On success will return 0, and others indicate fail. */ int usb_otg_deinit(uint8_t busid); -/** - * @brief get current role mode. - * - * @return return USBOTG_MODE_HOST or USBOTG_MODE_DEVICE. - */ -uint8_t usbotg_get_current_mode(uint8_t busid); /* called by user */ void USBOTG_IRQHandler(uint8_t busid); diff --git a/core/usbotg_core.c b/core/usbotg_core.c index bd063828..fb7e64ce 100644 --- a/core/usbotg_core.c +++ b/core/usbotg_core.c @@ -5,39 +5,57 @@ */ #include "usbotg_core.h" +#ifdef CONFIG_USB_OTG_ENABLE +#undef USB_DBG_TAG +#define USB_DBG_TAG "usbotg_core" +#include "usb_log.h" + +#define CONFIG_USB_OTG_MAX_BUS CONFIG_USBHOST_MAX_BUS + struct usbotg_core_priv { uint8_t busid; uint32_t reg_base; - usb_osal_sem_t change_sem; - usb_osal_thread_t change_thread; bool usbh_initialized; bool usbd_initialized; - int (*usbh_initialize)(uint8_t busid, uint32_t reg_base); - int (*usbd_initialize)(uint8_t busid, uint32_t reg_base); -} g_usbotg_core[CONFIG_USBHOST_MAX_BUS]; + void *device_event_callback; + void *host_event_callback; + uint8_t current_mode; + usb_osal_sem_t change_sem; + usb_osal_thread_t change_thread; +} g_usbotg_core[CONFIG_USB_OTG_MAX_BUS]; static void usbotg_host_initialize(uint8_t busid) { + if (g_usbotg_core[busid].usbh_initialized) { + return; + } + if (g_usbotg_core[busid].usbd_initialized) { g_usbotg_core[busid].usbd_initialized = false; usbd_deinitialize(busid); } - if (g_usbotg_core[busid].usbh_initialize && !g_usbotg_core[busid].usbh_initialized) { - g_usbotg_core[busid].usbh_initialized = true; - g_usbotg_core[busid].usbh_initialize(g_usbotg_core[busid].busid, g_usbotg_core[busid].reg_base); - } + + USB_LOG_INFO("Switch to HOST mode\r\n"); + + usbh_initialize(busid, g_usbotg_core[busid].reg_base); + g_usbotg_core[busid].usbh_initialized = true; } static void usbotg_device_initialize(uint8_t busid) { + if (g_usbotg_core[busid].usbd_initialized) { + return; + } + if (g_usbotg_core[busid].usbh_initialized) { g_usbotg_core[busid].usbh_initialized = false; usbh_deinitialize(busid); } - if (g_usbotg_core[busid].usbd_initialize && !g_usbotg_core[busid].usbd_initialize) { - g_usbotg_core[busid].usbd_initialized = true; - g_usbotg_core[busid].usbd_initialize(g_usbotg_core[busid].busid, g_usbotg_core[busid].reg_base); - } + + USB_LOG_INFO("Switch to DEVICE mode\r\n"); + + usbd_initialize(g_usbotg_core[busid].busid, g_usbotg_core[busid].reg_base, g_usbotg_core[busid].device_event_callback); + g_usbotg_core[busid].usbd_initialized = true; } static void usbotg_rolechange_thread(void *argument) @@ -48,58 +66,49 @@ static void usbotg_rolechange_thread(void *argument) while (1) { if (usb_osal_sem_take(g_usbotg_core[busid].change_sem, USB_OSAL_WAITING_FOREVER) == 0) { - if (usbotg_get_current_mode(busid) == USBOTG_MODE_HOST) { + if (g_usbotg_core[busid].current_mode == USBOTG_MODE_HOST) { usbotg_host_initialize(busid); - } else if (usbotg_get_current_mode(busid) == USBOTG_MODE_DEVICE) { + } else if (g_usbotg_core[busid].current_mode == USBOTG_MODE_DEVICE) { usbotg_device_initialize(busid); } } } } -int usbotg_initialize(uint8_t otg_mode, uint8_t busid, uint32_t reg_base, - int (*usbh_initialize)(uint8_t busid, uint32_t reg_base), - int (*usbd_initialize)(uint8_t busid, uint32_t reg_base)) +int usbotg_initialize(uint8_t busid, uint32_t reg_base, void *device_event_callback, void *host_event_callback, uint8_t default_role) { char thread_name[32] = { 0 }; - if (busid >= CONFIG_USBHOST_MAX_BUS) { - USB_LOG_ERR("bus overflow\r\n"); + USB_ASSERT_MSG(busid < CONFIG_USB_OTG_MAX_BUS, "bus overflow\r\n"); + + g_usbotg_core[busid].busid = busid; + g_usbotg_core[busid].reg_base = reg_base; + g_usbotg_core[busid].device_event_callback = device_event_callback; + g_usbotg_core[busid].host_event_callback = host_event_callback; + + g_usbotg_core[busid].change_sem = usb_osal_sem_create(0); + if (g_usbotg_core[busid].change_sem == NULL) { + USB_LOG_ERR("Failed to create change_sem\r\n"); while (1) { } } - g_usbotg_core[busid].busid = busid; - g_usbotg_core[busid].reg_base = reg_base; - g_usbotg_core[busid].usbh_initialize = usbh_initialize; - g_usbotg_core[busid].usbd_initialize = usbd_initialize; - - if (otg_mode == USBOTG_MODE_OTG) { - g_usbotg_core[busid].change_sem = usb_osal_sem_create(0); - if (g_usbotg_core[busid].change_sem == NULL) { - USB_LOG_ERR("Failed to create change_sem\r\n"); - return -1; - } - - snprintf(thread_name, 32, "usbotg%u", busid); - g_usbotg_core[busid].change_thread = usb_osal_thread_create(thread_name, CONFIG_USBHOST_PSC_STACKSIZE, CONFIG_USBHOST_PSC_PRIO, usbotg_rolechange_thread, (void *)(uintptr_t)busid); - if (g_usbotg_core[busid].change_thread == NULL) { - USB_LOG_ERR("Failed to create usbotg thread\r\n"); - return -1; - } - } else { - if (otg_mode == USBOTG_MODE_HOST) { - usbotg_host_initialize(busid); - } else if (otg_mode == USBOTG_MODE_DEVICE) { - usbotg_device_initialize(busid); + snprintf(thread_name, 32, "usbotg%u", busid); + g_usbotg_core[busid].change_thread = usb_osal_thread_create(thread_name, 2048, 10, usbotg_rolechange_thread, (void *)(uintptr_t)busid); + if (g_usbotg_core[busid].change_thread == NULL) { + USB_LOG_ERR("Failed to create usbotg thread\r\n"); + while (1) { } } + usbotg_trigger_role_change(busid, default_role); return 0; } -int usbotg_deinitialize(uint8_t busid, uint32_t reg_base) +int usbotg_deinitialize(uint8_t busid) { + USB_ASSERT_MSG(busid < CONFIG_USB_OTG_MAX_BUS, "bus overflow\r\n"); + if (g_usbotg_core[busid].usbd_initialized) { g_usbotg_core[busid].usbd_initialized = false; usbd_deinitialize(busid); @@ -110,28 +119,37 @@ int usbotg_deinitialize(uint8_t busid, uint32_t reg_base) usbh_deinitialize(busid); } + if (g_usbotg_core[busid].change_thread) { + usb_osal_thread_delete(g_usbotg_core[busid].change_thread); + } + if (g_usbotg_core[busid].change_sem) { usb_otg_deinit(busid); usb_osal_sem_delete(g_usbotg_core[busid].change_sem); } - if (g_usbotg_core[busid].change_thread) { - usb_osal_thread_delete(g_usbotg_core[busid].change_thread); - } - return 0; } -void usbotg_trigger_role_change(uint8_t busid) +void usbotg_trigger_role_change(uint8_t busid, uint8_t mode) { - usb_osal_sem_give(g_usbotg_core[busid].change_sem); + USB_ASSERT_MSG(busid < CONFIG_USB_OTG_MAX_BUS, "bus overflow\r\n"); + + g_usbotg_core[busid].current_mode = mode; + + if (g_usbotg_core[busid].change_sem) { + usb_osal_sem_give(g_usbotg_core[busid].change_sem); + } } void USBOTG_IRQHandler(uint8_t busid) { - if (usbotg_get_current_mode(busid) == USBOTG_MODE_HOST) { + USB_ASSERT_MSG(busid < CONFIG_USB_OTG_MAX_BUS, "bus overflow\r\n"); + + if (g_usbotg_core[busid].current_mode == USBOTG_MODE_HOST && g_usbotg_core[busid].usbh_initialized) { USBH_IRQHandler(busid); - } else if (usbotg_get_current_mode(busid) == USBOTG_MODE_DEVICE) { + } else if (g_usbotg_core[busid].current_mode == USBOTG_MODE_DEVICE && g_usbotg_core[busid].usbd_initialized) { USBD_IRQHandler(busid); } -} \ No newline at end of file +} +#endif /* CONFIG_USB_OTG_ENABLE */ \ No newline at end of file diff --git a/core/usbotg_core.h b/core/usbotg_core.h index 9269a508..4729d8c8 100644 --- a/core/usbotg_core.h +++ b/core/usbotg_core.h @@ -10,22 +10,15 @@ extern "C" { #endif -#define USBOTG_MODE_UNKNOWN 0 -#define USBOTG_MODE_OTG 1 -#define USBOTG_MODE_HOST 2 -#define USBOTG_MODE_DEVICE 3 - #include "usbd_core.h" #include "usbh_core.h" #include "usb_otg.h" -int usbotg_initialize(uint8_t otg_mode, uint8_t busid, uint32_t reg_base, - int (*usbh_initialize)(uint8_t busid, uint32_t reg_base), - int (*usbd_initialize)(uint8_t busid, uint32_t reg_base)); -int usbotg_deinitialize(uint8_t busid, uint32_t reg_base); +int usbotg_initialize(uint8_t busid, uint32_t reg_base, void *device_event_callback, void *host_event_callback, uint8_t default_role); +int usbotg_deinitialize(uint8_t busid); /* called by user */ -void usbotg_trigger_role_change(uint8_t busid); +void usbotg_trigger_role_change(uint8_t busid, uint8_t mode); #ifdef __cplusplus } diff --git a/port/hpmicro/usb_dc_hpm.c b/port/hpmicro/usb_dc_hpm.c index d4c73d4a..61956c37 100644 --- a/port/hpmicro/usb_dc_hpm.c +++ b/port/hpmicro/usb_dc_hpm.c @@ -102,7 +102,9 @@ int usb_dc_init(uint8_t busid) #endif usb_device_init(g_hpm_udc[busid].handle, int_mask); - +#ifdef CONFIG_USB_OTG_ENABLE + g_hpm_udc[busid].handle->regs->OTGSC |= USB_OTGSC_IDIE_MASK; +#endif usb_dc_isr_connect(busid); return 0; diff --git a/port/hpmicro/usb_glue_hpm.c b/port/hpmicro/usb_glue_hpm.c index 51aa9aa4..6099d906 100644 --- a/port/hpmicro/usb_glue_hpm.c +++ b/port/hpmicro/usb_glue_hpm.c @@ -12,6 +12,49 @@ void (*g_usb_hpm_irq[2])(uint8_t busid); uint8_t g_usb_hpm_busid[2]; +#ifdef CONFIG_USB_OTG_ENABLE +#include "usbotg_core.h" +int usb_otg_init(uint8_t busid) +{ + return 0; +} + +int usb_otg_deinit(uint8_t busid) +{ + return 0; +} + +SDK_DECLARE_EXT_ISR_M(IRQn_USB0, hpm_isr_usb0) +void hpm_isr_usb0(void) +{ + bool is_idchange = false; + + is_idchange = USB_OTGSC_IDIS_GET(HPM_USB0->OTGSC); + if (is_idchange) { + HPM_USB0->OTGSC |= USB_OTGSC_IDIS_MASK; + usbotg_trigger_role_change(g_usb_hpm_busid[0], USB_OTGSC_ID_GET(HPM_USB0->OTGSC) ? USBOTG_MODE_DEVICE : USBOTG_MODE_HOST); + } + + USBOTG_IRQHandler(g_usb_hpm_busid[0]); +} + +#ifdef HPM_USB1_BASE +SDK_DECLARE_EXT_ISR_M(IRQn_USB1, hpm_isr_usb1) +void hpm_isr_usb1(void) +{ + bool is_idchange = false; + + is_idchange = USB_OTGSC_IDIS_GET(HPM_USB1->OTGSC); + if (is_idchange) { + HPM_USB1->OTGSC |= USB_OTGSC_IDIS_MASK; + usbotg_trigger_role_change(g_usb_hpm_busid[1], USB_OTGSC_ID_GET(HPM_USB1->OTGSC) ? USBOTG_MODE_DEVICE : USBOTG_MODE_HOST); + } + + USBOTG_IRQHandler(g_usb_hpm_busid[1]); +} +#endif +#endif + ATTR_WEAK void hpm_usb_isr_enable(uint32_t base) { if (base == HPM_USB0_BASE) { @@ -34,6 +77,7 @@ ATTR_WEAK void hpm_usb_isr_disable(uint32_t base) } } +#ifndef CONFIG_USB_OTG_ENABLE #ifndef CONFIG_CHERRYUSB_CUSTOM_IRQ_HANDLER SDK_DECLARE_EXT_ISR_M(IRQn_USB0, hpm_isr_usb0) #endif @@ -51,6 +95,7 @@ void hpm_isr_usb1(void) g_usb_hpm_irq[1](g_usb_hpm_busid[1]); } #endif +#endif #ifdef CONFIG_USB_DCACHE_ENABLE void usb_dcache_clean(uintptr_t addr, size_t size) diff --git a/port/hpmicro/usb_hc_hpm.c b/port/hpmicro/usb_hc_hpm.c index 9ee51500..e6117d33 100644 --- a/port/hpmicro/usb_hc_hpm.c +++ b/port/hpmicro/usb_hc_hpm.c @@ -55,12 +55,18 @@ void usb_hc_low_level2_init(struct usbh_bus *bus) usb_host_mode_init((USB_Type *)(bus->hcd.reg_base)); if (bus->hcd.reg_base == HPM_USB0_BASE) { +#ifdef CONFIG_USB_OTG_ENABLE + HPM_USB0->OTGSC |= USB_OTGSC_IDIE_MASK; +#endif g_usb_hpm_busid[0] = bus->hcd.hcd_id; g_usb_hpm_irq[0] = USBH_IRQHandler; hpm_usb_isr_enable(HPM_USB0_BASE); } else { #ifdef HPM_USB1_BASE +#ifdef CONFIG_USB_OTG_ENABLE + HPM_USB1->OTGSC |= USB_OTGSC_IDIE_MASK; +#endif g_usb_hpm_busid[1] = bus->hcd.hcd_id; g_usb_hpm_irq[1] = USBH_IRQHandler; diff --git a/tests/hpmicro/CMakeLists.txt b/tests/hpmicro/CMakeLists.txt index 40b0e56c..c78a1bd5 100644 --- a/tests/hpmicro/CMakeLists.txt +++ b/tests/hpmicro/CMakeLists.txt @@ -31,6 +31,8 @@ sdk_compile_definitions(-DCONFIG_USBHOST_PLATFORM_CDC_RNDIS) sdk_compile_definitions(-DCONFIG_USBHOST_PLATFORM_ASIX) sdk_compile_definitions(-DCONFIG_USBHOST_PLATFORM_RTL8152) +# sdk_compile_definitions(-DCONFIG_USB_OTG_ENABLE) + sdk_compile_options("-O2") sdk_inc(inc) @@ -40,6 +42,7 @@ sdk_app_src(../../demo/usb_host.c) sdk_inc(src/iperf) sdk_app_src(src/iperf/iperf.c src/iperf/iperf_cli.c src/iperf/utils_getopt.c src/ping.c) +sdk_app_src(src/cdc_acm_otg_template.c) set(CONFIG_CHERRYMP 1) set(CONFIG_CHERRYUSB 1) diff --git a/tests/hpmicro/inc/usb_config.h b/tests/hpmicro/inc/usb_config.h index a9ee4fc5..19067761 100644 --- a/tests/hpmicro/inc/usb_config.h +++ b/tests/hpmicro/inc/usb_config.h @@ -268,10 +268,6 @@ #define CONFIG_USBDEV_MAX_BUS USB_SOC_MAX_COUNT -#ifndef CONFIG_USBDEV_EP_NUM -#define CONFIG_USBDEV_EP_NUM USB_SOC_DCD_MAX_ENDPOINT_COUNT -#endif - #ifndef CONFIG_HPM_USBD_BASE #define CONFIG_HPM_USBD_BASE HPM_USB0_BASE #endif @@ -281,7 +277,6 @@ /* ================ USB Host Port Configuration ==================*/ #define CONFIG_USBHOST_MAX_BUS USB_SOC_MAX_COUNT -#define CONFIG_USBHOST_PIPE_NUM 10 #ifndef CONFIG_HPM_USBH_BASE #define CONFIG_HPM_USBH_BASE HPM_USB0_BASE @@ -295,6 +290,7 @@ #define CONFIG_USB_EHCI_HPMICRO (1) #define CONFIG_USB_EHCI_HCCR_OFFSET (0x100u) #define CONFIG_USB_EHCI_FRAME_LIST_SIZE 1024 +#define CONFIG_USB_EHCI_QH_NUM 10 #define CONFIG_USB_EHCI_QTD_NUM 64 /* ================ Addr Convert Configuration ==================*/ diff --git a/tests/hpmicro/src/cdc_acm_otg_template.c b/tests/hpmicro/src/cdc_acm_otg_template.c new file mode 100644 index 00000000..48915535 --- /dev/null +++ b/tests/hpmicro/src/cdc_acm_otg_template.c @@ -0,0 +1,277 @@ +/* + * Copyright (c) 2024, sakumisu + * + * SPDX-License-Identifier: Apache-2.0 + */ +#include "usbotg_core.h" +#include "usbd_cdc_acm.h" + +/*!< endpoint address */ +#define CDC_IN_EP 0x81 +#define CDC_OUT_EP 0x02 +#define CDC_INT_EP 0x83 + +#define USBD_LANGID_STRING 1033 + +/*!< config descriptor size */ +#define USB_CONFIG_SIZE (9 + CDC_ACM_DESCRIPTOR_LEN) + +#ifdef CONFIG_USB_HS +#define CDC_MAX_MPS 512 +#else +#define CDC_MAX_MPS 64 +#endif + +#ifdef CONFIG_USBDEV_ADVANCE_DESC +static const uint8_t device_descriptor[] = { + USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01) +}; + +static const uint8_t config_descriptor[] = { + USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), + CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02) +}; + +static const uint8_t device_quality_descriptor[] = { + /////////////////////////////////////// + /// device qualifier descriptor + /////////////////////////////////////// + 0x0a, + USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x40, + 0x00, + 0x00, +}; + +static const char *string_descriptors[] = { + (const char[]){ 0x09, 0x04 }, /* Langid */ + "CherryUSB", /* Manufacturer */ + "CherryUSB CDC DEMO", /* Product */ + "2022123456", /* Serial Number */ +}; + +static const uint8_t *device_descriptor_callback(uint8_t speed) +{ + return device_descriptor; +} + +static const uint8_t *config_descriptor_callback(uint8_t speed) +{ + return config_descriptor; +} + +static const uint8_t *device_quality_descriptor_callback(uint8_t speed) +{ + return device_quality_descriptor; +} + +static const char *string_descriptor_callback(uint8_t speed, uint8_t index) +{ + if (index > 3) { + return NULL; + } + return string_descriptors[index]; +} + +const struct usb_descriptor cdc_descriptor = { + .device_descriptor_callback = device_descriptor_callback, + .config_descriptor_callback = config_descriptor_callback, + .device_quality_descriptor_callback = device_quality_descriptor_callback, + .string_descriptor_callback = string_descriptor_callback +}; +#else +/*!< global descriptor */ +static const uint8_t cdc_descriptor[] = { + USB_DEVICE_DESCRIPTOR_INIT(USB_2_0, 0xEF, 0x02, 0x01, USBD_VID, USBD_PID, 0x0100, 0x01), + USB_CONFIG_DESCRIPTOR_INIT(USB_CONFIG_SIZE, 0x02, 0x01, USB_CONFIG_BUS_POWERED, USBD_MAX_POWER), + CDC_ACM_DESCRIPTOR_INIT(0x00, CDC_INT_EP, CDC_OUT_EP, CDC_IN_EP, CDC_MAX_MPS, 0x02), + /////////////////////////////////////// + /// string0 descriptor + /////////////////////////////////////// + USB_LANGID_INIT(USBD_LANGID_STRING), + /////////////////////////////////////// + /// string1 descriptor + /////////////////////////////////////// + 0x14, /* bLength */ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ + 'C', 0x00, /* wcChar0 */ + 'h', 0x00, /* wcChar1 */ + 'e', 0x00, /* wcChar2 */ + 'r', 0x00, /* wcChar3 */ + 'r', 0x00, /* wcChar4 */ + 'y', 0x00, /* wcChar5 */ + 'U', 0x00, /* wcChar6 */ + 'S', 0x00, /* wcChar7 */ + 'B', 0x00, /* wcChar8 */ + /////////////////////////////////////// + /// string2 descriptor + /////////////////////////////////////// + 0x26, /* bLength */ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ + 'C', 0x00, /* wcChar0 */ + 'h', 0x00, /* wcChar1 */ + 'e', 0x00, /* wcChar2 */ + 'r', 0x00, /* wcChar3 */ + 'r', 0x00, /* wcChar4 */ + 'y', 0x00, /* wcChar5 */ + 'U', 0x00, /* wcChar6 */ + 'S', 0x00, /* wcChar7 */ + 'B', 0x00, /* wcChar8 */ + ' ', 0x00, /* wcChar9 */ + 'C', 0x00, /* wcChar10 */ + 'D', 0x00, /* wcChar11 */ + 'C', 0x00, /* wcChar12 */ + ' ', 0x00, /* wcChar13 */ + 'D', 0x00, /* wcChar14 */ + 'E', 0x00, /* wcChar15 */ + 'M', 0x00, /* wcChar16 */ + 'O', 0x00, /* wcChar17 */ + /////////////////////////////////////// + /// string3 descriptor + /////////////////////////////////////// + 0x16, /* bLength */ + USB_DESCRIPTOR_TYPE_STRING, /* bDescriptorType */ + '2', 0x00, /* wcChar0 */ + '0', 0x00, /* wcChar1 */ + '2', 0x00, /* wcChar2 */ + '2', 0x00, /* wcChar3 */ + '1', 0x00, /* wcChar4 */ + '2', 0x00, /* wcChar5 */ + '3', 0x00, /* wcChar6 */ + '4', 0x00, /* wcChar7 */ + '5', 0x00, /* wcChar8 */ + '6', 0x00, /* wcChar9 */ +#ifdef CONFIG_USB_HS + /////////////////////////////////////// + /// device qualifier descriptor + /////////////////////////////////////// + 0x0a, + USB_DESCRIPTOR_TYPE_DEVICE_QUALIFIER, + 0x00, + 0x02, + 0x00, + 0x00, + 0x00, + 0x40, + 0x00, + 0x00, +#endif + 0x00 +}; +#endif + +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_buffer[2048]; /* 2048 is only for test speed , please use CDC_MAX_MPS for common*/ +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t write_buffer[2048]; + +volatile bool ep_tx_busy_flag = false; + +static void usbd_event_handler(uint8_t busid, uint8_t event) +{ + switch (event) { + case USBD_EVENT_RESET: + break; + case USBD_EVENT_CONNECTED: + break; + case USBD_EVENT_DISCONNECTED: + break; + case USBD_EVENT_RESUME: + break; + case USBD_EVENT_SUSPEND: + break; + case USBD_EVENT_CONFIGURED: + ep_tx_busy_flag = false; + /* setup first out ep read transfer */ + usbd_ep_start_read(busid, CDC_OUT_EP, read_buffer, 2048); + break; + case USBD_EVENT_SET_REMOTE_WAKEUP: + break; + case USBD_EVENT_CLR_REMOTE_WAKEUP: + break; + + default: + break; + } +} + +void usbd_cdc_acm_bulk_out(uint8_t busid, uint8_t ep, uint32_t nbytes) +{ + USB_LOG_RAW("actual out len:%d\r\n", (unsigned int)nbytes); + // for (int i = 0; i < 100; i++) { + // printf("%02x ", read_buffer[i]); + // } + // printf("\r\n"); + /* setup next out ep read transfer */ + usbd_ep_start_read(busid, CDC_OUT_EP, read_buffer, 2048); +} + +void usbd_cdc_acm_bulk_in(uint8_t busid, uint8_t ep, uint32_t nbytes) +{ + USB_LOG_RAW("actual in len:%d\r\n", (unsigned int)nbytes); + + if ((nbytes % usbd_get_ep_mps(busid, ep)) == 0 && nbytes) { + /* send zlp */ + usbd_ep_start_write(busid, CDC_IN_EP, NULL, 0); + } else { + ep_tx_busy_flag = false; + } +} + +/*!< endpoint call back */ +struct usbd_endpoint cdc_out_ep = { + .ep_addr = CDC_OUT_EP, + .ep_cb = usbd_cdc_acm_bulk_out +}; + +struct usbd_endpoint cdc_in_ep = { + .ep_addr = CDC_IN_EP, + .ep_cb = usbd_cdc_acm_bulk_in +}; + +static struct usbd_interface intf0; +static struct usbd_interface intf1; + +void cdc_acm_otg_init(uint8_t busid, uintptr_t reg_base) +{ + const uint8_t data[10] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 }; + + memcpy(&write_buffer[0], data, 10); + memset(&write_buffer[10], 'a', 2038); + +#ifdef CONFIG_USBDEV_ADVANCE_DESC + usbd_desc_register(busid, &cdc_descriptor); +#else + usbd_desc_register(busid, cdc_descriptor); +#endif + usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf0)); + usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf1)); + usbd_add_endpoint(busid, &cdc_out_ep); + usbd_add_endpoint(busid, &cdc_in_ep); + + usbotg_initialize(busid, reg_base, usbd_event_handler, NULL, USBOTG_MODE_DEVICE); +} + +volatile uint8_t dtr_enable = 0; + +void usbd_cdc_acm_set_dtr(uint8_t busid, uint8_t intf, bool dtr) +{ + if (dtr) { + dtr_enable = 1; + } else { + dtr_enable = 0; + } +} + +void cdc_acm_data_send_with_dtr_test(uint8_t busid) +{ + if (dtr_enable) { + ep_tx_busy_flag = true; + usbd_ep_start_write(busid, CDC_IN_EP, write_buffer, 2048); + while (ep_tx_busy_flag) { + } + } +} \ No newline at end of file diff --git a/tests/hpmicro/src/main.c b/tests/hpmicro/src/main.c index aaadb1c8..66c22436 100644 --- a/tests/hpmicro/src/main.c +++ b/tests/hpmicro/src/main.c @@ -14,6 +14,7 @@ #include "board.h" #include "hpm_clock_drv.h" #include "hpm_l1c_drv.h" +#include "hpm_gpio_drv.h" #include "shell.h" #include "usbh_core.h" #include "lwip/tcpip.h" @@ -46,8 +47,12 @@ int main(void) uvc2lcd_init(); #endif +#ifndef CONFIG_USB_OTG_ENABLE usbh_initialize(0, CONFIG_HPM_USBH_BASE); - +#else + extern void cdc_acm_otg_init(uint8_t busid, uintptr_t reg_base); + cdc_acm_otg_init(0, CONFIG_HPM_USBH_BASE); +#endif if (pdPASS != xTaskCreate(task_start, "task_start", 1024U, NULL, task_start_PRIORITY, NULL)) { printf("Task start creation failed!\r\n"); for (;;) {