test_cherryusb.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * Copyright (c) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-08-08 CDT first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <board.h>
  13. /* 请关注并阅读bsp/hc32/ev_hc32xxxx/README.md中,关于USB使用的相关注意事项
  14. */
  15. #if defined(RT_CHERRYUSB_HOST) && defined(RT_CHERRYUSB_DEVICE)
  16. #if defined(HC32F4A0) || defined(HC32F4A8)
  17. #define TEST_USBH_CORE_BASE (CM_USBFS_BASE)
  18. #define TEST_USBD_CORE_BASE (CM_USBHS_BASE)
  19. #else
  20. #error "Only one USB role can be selected"
  21. #endif
  22. #else
  23. #if defined(RT_CHERRYUSB_HOST)
  24. #define TEST_USBH_CORE_BASE (CM_USBFS_BASE)
  25. #elif defined(RT_CHERRYUSB_DEVICE)
  26. #define TEST_USBD_CORE_BASE (CM_USBFS_BASE)
  27. #endif
  28. #endif
  29. #if defined(RT_CHERRYUSB_HOST)
  30. #include "usbh_core.h"
  31. #if defined(RT_CHERRYUSB_HOST_CDC_ECM) || defined(RT_CHERRYUSB_HOST_CDC_RNDIS) || defined(RT_CHERRYUSB_HOST_MSC)
  32. /* 使用USB Host 时,应确保主机对设备供电充足
  33. menuconfig: ECM 关键配置
  34. RT-Thread Kernel --->[*] Enable soft timer with a timer thread
  35. (4096) The stack size of timer thread
  36. RT-Thread Components--->Devicee Drivers--->[*] Using USB with CherryUSB
  37. [*] Enable usb host mode
  38. Selectot usb host ip.... --->
  39. [*]dwc2_hc
  40. [*] Enable usb cdc ecm driver
  41. RT-Thread Components--->Network---> lwIP--->lwIP version ---> v2.1.2
  42. ...
  43. [*] Enable alloc ip address through DHCP
  44. ...
  45. (4096) the stack size of lwIP thread
  46. [*] Not use Rx thread
  47. [*] Not use Tx thread
  48. ...
  49. [*] Enable ping features
  50. 备注:CherryUSB Host枚举设备时,默认选择Configuration 1,若指定设备(如CH397A模组CDC-ECM模式)需要选择Configuration 2,需在
  51. components/drivers/usb/cherryusb/core/usbh_core.c文件中usbh_enumerate()函数内添加如下代码:
  52. int usbh_enumerate(struct usbh_hubport *hport)
  53. {
  54. ...
  55. config_index = 0;
  56. // Add code start
  57. if((0x1A86 == ((struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid])->idVendor) && \
  58. (0x5397 == ((struct usb_device_descriptor *)ep0_request_buffer[hport->bus->busid])->idProduct)) {
  59. config_index = 1; // For CH397, we need to select configuration 2
  60. }
  61. // Add code end
  62. USB_LOG_DBG("The device selects config %d\r\n", config_index);
  63. ...
  64. }
  65. menuconfig: MSC 关键配置
  66. RT-Thread Kernel --->[*] Enable soft timer with a timer thread
  67. (4096) The stack size of timer thread
  68. RT-Thread Components--->Devicee Drivers--->[*] Using USB with CherryUSB
  69. [*] Enable usb host mode
  70. Selectot usb host ip.... --->
  71. [*]dwc2_hc
  72. [*] Enable usb msc driver
  73. ...
  74. (/)usb host dfs mount point
  75. */
  76. /* ECM 测试
  77. msh />ipconfig
  78. network interface: u0 (Default)
  79. MTU: 1500
  80. MAC: 4e 61 e9 06 9b ff
  81. FLAGS: UP LINK_UP ETHARP BROADCAST IGMP
  82. ip address: 192.168.225.29
  83. gw address: 192.168.225.1
  84. net mask : 255.255.255.0
  85. dns server #0: 108.7.254.31
  86. dns server #1: 112.7.254.31
  87. msh />ping www.baidu.com
  88. 60 bytes from 183.2.172.177 icmp_seq=0 ttl=51 time=149 ms
  89. 60 bytes from 183.2.172.177 icmp_seq=1 ttl=51 time=69 ms
  90. 60 bytes from 183.2.172.177 icmp_seq=2 ttl=51 time=51 ms
  91. 60 bytes from 183.2.172.177 icmp_seq=3 ttl=51 time=52 ms
  92. */
  93. static int cherryusb_host_init(void)
  94. {
  95. usbh_initialize(0, TEST_USBH_CORE_BASE);
  96. return 0;
  97. }
  98. INIT_APP_EXPORT(cherryusb_host_init);
  99. #if defined(RT_CHERRYUSB_HOST_CDC_ECM) || defined(RT_CHERRYUSB_HOST_CDC_RNDIS)
  100. void ipconfig(void)
  101. {
  102. extern void list_if(void);
  103. list_if();
  104. }
  105. MSH_CMD_EXPORT(ipconfig, list network interface information);
  106. #endif
  107. #endif
  108. #endif
  109. #if defined(RT_CHERRYUSB_DEVICE)
  110. #if defined(RT_CHERRYUSB_DEVICE_TEMPLATE_CDC_ACM)
  111. /*
  112. menuconfig:关键配置
  113. RT-Thread Components--->Devicee Drivers--->[*] Using USB with CherryUSB
  114. [*] Enable usb device mode
  115. Selectot usb host ip.... --->
  116. [*]dwc2_hc
  117. [*] Enable usb cdc acm device
  118. Select usb device template...--->
  119. [*] cdc acm
  120. */
  121. static int cherryusb_device_cdc_acm_init(void)
  122. {
  123. extern void cdc_acm_init(uint8_t busid, uint32_t reg_base);
  124. cdc_acm_init(0, TEST_USBD_CORE_BASE);
  125. return 0;
  126. }
  127. INIT_APP_EXPORT(cherryusb_device_cdc_acm_init);
  128. static void cherryusb_cdc_send(int argc, char **argv)
  129. {
  130. extern void cdc_acm_data_send_with_dtr_test(uint8_t busid);
  131. cdc_acm_data_send_with_dtr_test(0);
  132. }
  133. MSH_CMD_EXPORT(cherryusb_cdc_send, cdc acm data send with dtr test);
  134. #endif
  135. #endif