usb_host.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbh_core.h"
  7. #include "usbh_cdc_acm.h"
  8. #include "usbh_hid.h"
  9. #include "usbh_msc.h"
  10. #include "usbh_video.h"
  11. #include "usbh_audio.h"
  12. #ifndef TEST_USBH_CDC_ACM
  13. #define TEST_USBH_CDC_ACM 1
  14. #endif
  15. #ifndef TEST_USBH_CDC_SPEED
  16. #define TEST_USBH_CDC_SPEED 0
  17. #endif
  18. #ifndef TEST_USBH_HID
  19. #define TEST_USBH_HID 1
  20. #endif
  21. #ifndef TEST_USBH_MSC
  22. #define TEST_USBH_MSC 1
  23. #endif
  24. #ifndef TEST_USBH_MSC_FATFS
  25. #define TEST_USBH_MSC_FATFS 0
  26. #endif
  27. #ifndef TEST_USBH_AUDIO
  28. #define TEST_USBH_AUDIO 0
  29. #endif
  30. #ifndef TEST_USBH_VIDEO
  31. #define TEST_USBH_VIDEO 0
  32. #endif
  33. #if defined(TEST_USBH_CDC_ECM) || defined(TEST_USBH_CDC_RNDIS) || defined(TEST_USBH_ASIX) || defined(TEST_USBH_RTL8152)
  34. #error we have move those class implements into platform/none/usbh_lwip.c, and you should call tcpip_init(NULL, NULL) in your app
  35. #endif
  36. #if TEST_USBH_CDC_ACM
  37. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t cdc_buffer[4096];
  38. #if TEST_USBH_CDC_SPEED
  39. #define TEST_LEN (16 * 1024)
  40. #define TEST_COUNT (10240)
  41. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t cdc_speed_buffer[TEST_LEN];
  42. #endif
  43. void usbh_cdc_acm_callback(void *arg, int nbytes)
  44. {
  45. //struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)arg;
  46. if (nbytes > 0) {
  47. for (size_t i = 0; i < nbytes; i++) {
  48. USB_LOG_RAW("0x%02x ", cdc_buffer[i]);
  49. }
  50. USB_LOG_RAW("nbytes:%d\r\n", nbytes);
  51. }
  52. }
  53. static void usbh_cdc_acm_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  54. {
  55. int ret;
  56. struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)CONFIG_USB_OSAL_THREAD_GET_ARGV;
  57. /* test with only one buffer, if you have more cdc acm class, modify by yourself */
  58. #if TEST_USBH_CDC_SPEED
  59. const uint32_t test_len[] = { 512, 1 * 1024, 2 * 1024, 4 * 1024, 8 * 1024, 16 * 1024 };
  60. memset(cdc_speed_buffer, 0xAA, TEST_LEN);
  61. for (uint8_t j = 0; j < 6; j++) {
  62. uint32_t start_time = (uint32_t)xTaskGetTickCount();
  63. for (uint32_t i = 0; i < TEST_COUNT; i++) {
  64. usbh_bulk_urb_fill(&cdc_acm_class->bulkout_urb, cdc_acm_class->hport, cdc_acm_class->bulkout, cdc_speed_buffer, test_len[j], 0XFFFFFFF, NULL, NULL);
  65. ret = usbh_submit_urb(&cdc_acm_class->bulkout_urb);
  66. if (ret < 0) {
  67. USB_LOG_RAW("bulk out error,ret:%d\r\n", ret);
  68. while (1) {
  69. }
  70. } else {
  71. }
  72. }
  73. uint32_t time_ms = xTaskGetTickCount() - start_time;
  74. USB_LOG_RAW("per packet len:%d, out speed:%f MB/S\r\n", test_len[j], (test_len[j] * TEST_COUNT / 1024 / 1024) * 1000 / ((float)time_ms));
  75. }
  76. #endif
  77. memset(cdc_buffer, 0x55, 4096);
  78. /* for common, we use timeout with 0xffffffff, this is just a test */
  79. usbh_bulk_urb_fill(&cdc_acm_class->bulkout_urb, cdc_acm_class->hport, cdc_acm_class->bulkout, cdc_buffer, sizeof(cdc_buffer), 3000, NULL, NULL);
  80. ret = usbh_submit_urb(&cdc_acm_class->bulkout_urb);
  81. if (ret < 0) {
  82. USB_LOG_RAW("bulk out error,ret:%d\r\n", ret);
  83. goto delete;
  84. } else {
  85. USB_LOG_RAW("send over:%d\r\n", cdc_acm_class->bulkout_urb.actual_length);
  86. }
  87. /* we can change cdc_acm_class->bulkin->wMaxPacketSize with 4096 for testing zlp, default is ep mps */
  88. usbh_bulk_urb_fill(&cdc_acm_class->bulkin_urb, cdc_acm_class->hport, cdc_acm_class->bulkin, cdc_buffer, cdc_acm_class->bulkin->wMaxPacketSize, 0xffffffff, usbh_cdc_acm_callback, cdc_acm_class);
  89. ret = usbh_submit_urb(&cdc_acm_class->bulkin_urb);
  90. if (ret < 0) {
  91. USB_LOG_RAW("bulk in error,ret:%d\r\n", ret);
  92. goto delete;
  93. } else {
  94. }
  95. // clang-format off
  96. delete:
  97. usb_osal_thread_delete(NULL);
  98. // clang-format on
  99. }
  100. #endif
  101. #if TEST_USBH_HID
  102. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t hid_buffer[128];
  103. void usbh_hid_callback(void *arg, int nbytes)
  104. {
  105. struct usbh_hid *hid_class = (struct usbh_hid *)arg;
  106. if (nbytes > 0) {
  107. for (int i = 0; i < nbytes; i++) {
  108. USB_LOG_RAW("0x%02x ", hid_buffer[i]);
  109. }
  110. USB_LOG_RAW("nbytes:%d\r\n", nbytes);
  111. usbh_int_urb_fill(&hid_class->intin_urb, hid_class->hport, hid_class->intin, hid_buffer, hid_class->intin->wMaxPacketSize, 0, usbh_hid_callback, hid_class);
  112. usbh_submit_urb(&hid_class->intin_urb);
  113. } else if (nbytes == -USB_ERR_NAK) { /* only dwc2 should do this */
  114. usbh_int_urb_fill(&hid_class->intin_urb, hid_class->hport, hid_class->intin, hid_buffer, hid_class->intin->wMaxPacketSize, 0, usbh_hid_callback, hid_class);
  115. usbh_submit_urb(&hid_class->intin_urb);
  116. } else {
  117. }
  118. }
  119. static void usbh_hid_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  120. {
  121. int ret;
  122. struct usbh_hid *hid_class = (struct usbh_hid *)CONFIG_USB_OSAL_THREAD_GET_ARGV;
  123. ;
  124. /* test with only one buffer, if you have more hid class, modify by yourself */
  125. /* Suggest you to use timer for int transfer and use ep interval */
  126. usbh_int_urb_fill(&hid_class->intin_urb, hid_class->hport, hid_class->intin, hid_buffer, hid_class->intin->wMaxPacketSize, 0, usbh_hid_callback, hid_class);
  127. ret = usbh_submit_urb(&hid_class->intin_urb);
  128. if (ret < 0) {
  129. goto delete;
  130. }
  131. // clang-format off
  132. delete:
  133. usb_osal_thread_delete(NULL);
  134. // clang-format on
  135. }
  136. #endif
  137. #if TEST_USBH_MSC
  138. #if TEST_USBH_MSC_FATFS
  139. #include "ff.h"
  140. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_write_buffer[25 * 100];
  141. USB_NOCACHE_RAM_SECTION FATFS fs;
  142. USB_NOCACHE_RAM_SECTION FIL fnew;
  143. UINT fnum;
  144. FRESULT res_sd = 0;
  145. int usb_msc_fatfs_test()
  146. {
  147. const char *tmp_data = "cherryusb fatfs demo...\r\n";
  148. USB_LOG_RAW("data len:%d\r\n", strlen(tmp_data));
  149. for (uint32_t i = 0; i < 100; i++) {
  150. memcpy(&read_write_buffer[i * 25], tmp_data, strlen(tmp_data));
  151. }
  152. res_sd = f_mount(&fs, "2:", 1);
  153. if (res_sd != FR_OK) {
  154. USB_LOG_RAW("mount fail,res:%d\r\n", res_sd);
  155. return -1;
  156. }
  157. USB_LOG_RAW("test fatfs write\r\n");
  158. res_sd = f_open(&fnew, "2:test.txt", FA_CREATE_ALWAYS | FA_WRITE);
  159. if (res_sd == FR_OK) {
  160. res_sd = f_write(&fnew, read_write_buffer, sizeof(read_write_buffer), &fnum);
  161. if (res_sd == FR_OK) {
  162. USB_LOG_RAW("write success, write len:%d\n", fnum);
  163. } else {
  164. USB_LOG_RAW("write fail\r\n");
  165. goto unmount;
  166. }
  167. f_close(&fnew);
  168. } else {
  169. USB_LOG_RAW("open fail\r\n");
  170. goto unmount;
  171. }
  172. USB_LOG_RAW("test fatfs read\r\n");
  173. res_sd = f_open(&fnew, "2:test.txt", FA_OPEN_EXISTING | FA_READ);
  174. if (res_sd == FR_OK) {
  175. res_sd = f_read(&fnew, read_write_buffer, sizeof(read_write_buffer), &fnum);
  176. if (res_sd == FR_OK) {
  177. USB_LOG_RAW("read success, read len:%d\n", fnum);
  178. } else {
  179. USB_LOG_RAW("read fail\r\n");
  180. goto unmount;
  181. }
  182. f_close(&fnew);
  183. } else {
  184. USB_LOG_RAW("open fail\r\n");
  185. goto unmount;
  186. }
  187. f_mount(NULL, "2:", 1);
  188. return 0;
  189. unmount:
  190. f_mount(NULL, "2:", 1);
  191. return -1;
  192. }
  193. #endif
  194. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t partition_table[512];
  195. static void usbh_msc_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  196. {
  197. int ret;
  198. struct usbh_msc *msc_class = (struct usbh_msc *)CONFIG_USB_OSAL_THREAD_GET_ARGV;
  199. /* test with only one buffer, if you have more msc class, modify by yourself */
  200. #if TEST_USBH_MSC_FATFS == 0
  201. ret = usbh_msc_scsi_init(msc_class);
  202. if (ret < 0) {
  203. USB_LOG_RAW("scsi_init error,ret:%d\r\n", ret);
  204. goto delete;
  205. }
  206. /* get the partition table */
  207. ret = usbh_msc_scsi_read10(msc_class, 0, partition_table, 1);
  208. if (ret < 0) {
  209. USB_LOG_RAW("scsi_read10 error,ret:%d\r\n", ret);
  210. goto delete;
  211. }
  212. for (uint32_t i = 0; i < 512; i++) {
  213. if (i % 16 == 0) {
  214. USB_LOG_RAW("\r\n");
  215. }
  216. USB_LOG_RAW("%02x ", partition_table[i]);
  217. }
  218. USB_LOG_RAW("\r\n");
  219. #else
  220. usb_msc_fatfs_test();
  221. #endif
  222. // clang-format off
  223. delete:
  224. usb_osal_thread_delete(NULL);
  225. // clang-format on
  226. }
  227. #endif
  228. #if TEST_USBH_CDC_ACM
  229. void usbh_cdc_acm_run(struct usbh_cdc_acm *cdc_acm_class)
  230. {
  231. usb_osal_thread_create("usbh_cdc", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_cdc_acm_thread, cdc_acm_class);
  232. }
  233. void usbh_cdc_acm_stop(struct usbh_cdc_acm *cdc_acm_class)
  234. {
  235. }
  236. #endif
  237. #if TEST_USBH_HID
  238. void usbh_hid_run(struct usbh_hid *hid_class)
  239. {
  240. usb_osal_thread_create("usbh_hid", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_hid_thread, hid_class);
  241. }
  242. void usbh_hid_stop(struct usbh_hid *hid_class)
  243. {
  244. }
  245. #endif
  246. #if TEST_USBH_MSC
  247. void usbh_msc_run(struct usbh_msc *msc_class)
  248. {
  249. usb_osal_thread_create("usbh_msc", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_msc_thread, msc_class);
  250. }
  251. void usbh_msc_stop(struct usbh_msc *msc_class)
  252. {
  253. }
  254. #endif
  255. #if TEST_USBH_AUDIO
  256. #error "commercial charge"
  257. #endif
  258. #if TEST_USBH_VIDEO
  259. #error "commercial charge"
  260. #endif
  261. #if 0
  262. #include "usbh_aoa.h"
  263. static struct aoa_string_info deviceinfo = {
  264. .acc_manufacturer = "CherryUSB",
  265. .acc_model = "CherryUSB",
  266. .acc_description = "Android Open Accessory CherryUSB",
  267. .acc_version = "1.0",
  268. .acc_uri = "http://developer.android.com/tools/adk/index.html",
  269. .acc_serial = "CherryUSB"
  270. };
  271. int aoa_switch(int argc, char **argv)
  272. {
  273. struct usbh_hubport *hport = usbh_find_hubport(0, 1, 1);
  274. usbh_aoa_switch(hport, &deviceinfo);
  275. return 0;
  276. }
  277. SHELL_CMD_EXPORT_ALIAS(aoa_switch, aoa_switch, aoa_switch);
  278. #endif