usb_host.c 12 KB

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