usb_host.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright (c) 2024, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "usbh_core.h"
  7. #include "usbh_serial.h"
  8. #include "usbh_hid.h"
  9. #include "usbh_msc.h"
  10. // net class demos use socket api
  11. #ifdef CONFIG_TEST_USBH_SERIAL
  12. #define SERIAL_TEST_LEN (1 * 1024)
  13. #if SERIAL_TEST_LEN >= CONFIG_USBHOST_SERIAL_RX_SIZE
  14. #error SERIAL_TEST_LEN is larger than CONFIG_USBHOST_SERIAL_RX_SIZE, please reduce SERIAL_TEST_LEN or increase CONFIG_USBHOST_SERIAL_RX_SIZE
  15. #endif
  16. volatile uint32_t serial_tx_bytes = 0;
  17. volatile uint32_t serial_rx_bytes = 0;
  18. volatile bool serial_is_opened = false;
  19. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t serial_tx_buffer[SERIAL_TEST_LEN];
  20. uint8_t serial_rx_data[SERIAL_TEST_LEN];
  21. #ifdef CONFIG_TEST_USBH_CDC_SPEED
  22. #define TEST_LEN (16 * 1024)
  23. #define TEST_COUNT (10240)
  24. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t serial_speed_buffer[TEST_LEN];
  25. #endif
  26. static void usbh_serial_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  27. {
  28. int ret;
  29. struct usbh_serial *serial;
  30. bool serial_test_success = false;
  31. serial = usbh_serial_open("/dev/ttyACM0", USBH_SERIAL_O_RDWR | USBH_SERIAL_O_NONBLOCK);
  32. if (serial == NULL) {
  33. serial = usbh_serial_open("/dev/ttyUSB0", USBH_SERIAL_O_RDWR | USBH_SERIAL_O_NONBLOCK);
  34. if (serial == NULL) {
  35. USB_LOG_RAW("no serial device found\r\n");
  36. goto delete;
  37. }
  38. }
  39. struct usbh_serial_termios termios;
  40. memset(&termios, 0, sizeof(termios));
  41. termios.baudrate = 115200;
  42. termios.stopbits = 0;
  43. termios.parity = 0;
  44. termios.databits = 8;
  45. termios.rtscts = false;
  46. termios.rx_timeout = 0;
  47. ret = usbh_serial_control(serial, USBH_SERIAL_CMD_SET_ATTR, &termios);
  48. if (ret < 0) {
  49. USB_LOG_RAW("set serial attr error, ret:%d\r\n", ret);
  50. goto delete_with_close;
  51. }
  52. /* test with only one buffer, if you have more cdc acm class, modify by yourself */
  53. #ifdef CONFIG_TEST_USBH_CDC_SPEED
  54. const uint32_t test_len[] = { 512, 1 * 1024, 2 * 1024, 4 * 1024, 8 * 1024, 16 * 1024 };
  55. memset(serial_speed_buffer, 0xAA, TEST_LEN);
  56. for (uint8_t j = 0; j < 6; j++) {
  57. uint32_t start_time = (uint32_t)xTaskGetTickCount();
  58. for (uint32_t i = 0; i < TEST_COUNT; i++) {
  59. usbh_serial_write(serial, serial_speed_buffer, test_len[j]);
  60. if (ret < 0) {
  61. USB_LOG_RAW("bulk out error,ret:%d\r\n", ret);
  62. while (1) {
  63. }
  64. } else {
  65. }
  66. }
  67. uint32_t time_ms = xTaskGetTickCount() - start_time;
  68. 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));
  69. }
  70. usbh_serial_close(serial);
  71. goto delete;
  72. #endif
  73. memset(serial_tx_buffer, 0xA5, sizeof(serial_tx_buffer));
  74. USB_LOG_RAW("start serial loopback test, len: %d\r\n", SERIAL_TEST_LEN);
  75. serial_tx_bytes = 0;
  76. while (1) {
  77. ret = usbh_serial_write(serial, serial_tx_buffer, sizeof(serial_tx_buffer));
  78. if (ret < 0) {
  79. USB_LOG_RAW("serial write error, ret:%d\r\n", ret);
  80. goto delete_with_close;
  81. } else {
  82. serial_tx_bytes += ret;
  83. if (serial_tx_bytes == SERIAL_TEST_LEN) {
  84. USB_LOG_RAW("send over\r\n");
  85. break;
  86. }
  87. }
  88. }
  89. volatile uint32_t wait_timeout = 0;
  90. serial_rx_bytes = 0;
  91. while (1) {
  92. ret = usbh_serial_read(serial, &serial_rx_data[serial_rx_bytes], SERIAL_TEST_LEN - serial_rx_bytes);
  93. if (ret < 0) {
  94. USB_LOG_RAW("serial read error, ret:%d\r\n", ret);
  95. goto delete_with_close;
  96. } else {
  97. serial_rx_bytes += ret;
  98. if (serial_rx_bytes == SERIAL_TEST_LEN) {
  99. USB_LOG_RAW("receive over\r\n");
  100. for (uint32_t i = 0; i < SERIAL_TEST_LEN; i++) {
  101. if (serial_rx_data[i] != 0xa5) {
  102. USB_LOG_RAW("serial loopback data error at index %d, data: 0x%02x\r\n", (unsigned int)i, serial_rx_data[i]);
  103. goto delete_with_close;
  104. }
  105. }
  106. serial_test_success = true;
  107. break;
  108. }
  109. }
  110. wait_timeout++;
  111. if (wait_timeout > 500) { // 5s
  112. USB_LOG_RAW("serial read timeout\r\n");
  113. goto delete_with_close;
  114. }
  115. usb_osal_msleep(10);
  116. }
  117. // clang-format off
  118. delete_with_close:
  119. if (serial_test_success) {
  120. USB_LOG_RAW("serial loopback test success\r\n");
  121. } else {
  122. USB_LOG_RAW("serial loopback test failed\r\n");
  123. }
  124. usbh_serial_close(serial);
  125. delete:
  126. usb_osal_thread_delete(NULL);
  127. // clang-format on
  128. }
  129. #endif
  130. #ifdef CONFIG_TEST_USBH_HID
  131. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t hid_buffer[128];
  132. void usbh_hid_callback(void *arg, int nbytes)
  133. {
  134. struct usbh_hid *hid_class = (struct usbh_hid *)arg;
  135. if (nbytes > 0) {
  136. for (int i = 0; i < nbytes; i++) {
  137. USB_LOG_RAW("0x%02x ", hid_buffer[i]);
  138. }
  139. USB_LOG_RAW("nbytes:%d\r\n", (unsigned int)nbytes);
  140. 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);
  141. usbh_submit_urb(&hid_class->intin_urb);
  142. } else if (nbytes == -USB_ERR_NAK) { /* only dwc2 should do this */
  143. 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);
  144. usbh_submit_urb(&hid_class->intin_urb);
  145. } else {
  146. }
  147. }
  148. static void usbh_hid_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  149. {
  150. int ret;
  151. struct usbh_hid *hid_class = (struct usbh_hid *)CONFIG_USB_OSAL_THREAD_GET_ARGV;
  152. /* test with only one buffer, if you have more hid class, modify by yourself */
  153. /* Suggest you to use timer for int transfer and use ep interval */
  154. 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);
  155. ret = usbh_submit_urb(&hid_class->intin_urb);
  156. if (ret < 0) {
  157. goto delete;
  158. }
  159. // clang-format off
  160. delete:
  161. usb_osal_thread_delete(NULL);
  162. // clang-format on
  163. }
  164. #endif
  165. #ifdef CONFIG_TEST_USBH_MSC
  166. #ifdef CONFIG_TEST_USBH_MSC_FATFS
  167. #include "ff.h"
  168. #ifdef CONFIG_TEST_USBH_MSC_FATFS_SPEED
  169. #define WRITE_SIZE_MB (128UL)
  170. #define WRITE_SIZE (1024UL * 1024UL * WRITE_SIZE_MB)
  171. #define BUF_SIZE (1024UL * 128UL)
  172. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_write_buffer[BUF_SIZE];
  173. #else
  174. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_write_buffer[25 * 100];
  175. #endif
  176. USB_NOCACHE_RAM_SECTION FATFS fs;
  177. USB_NOCACHE_RAM_SECTION FIL fnew;
  178. UINT fnum;
  179. FRESULT res_sd = 0;
  180. int usb_msc_fatfs_test()
  181. {
  182. const char *tmp_data = "cherryusb fatfs demo...\r\n";
  183. USB_LOG_RAW("data len:%d\r\n", (unsigned int)strlen(tmp_data));
  184. for (uint32_t i = 0; i < 100; i++) {
  185. memcpy(&read_write_buffer[i * 25], tmp_data, strlen(tmp_data));
  186. }
  187. res_sd = f_mount(&fs, "2:", 1);
  188. if (res_sd != FR_OK) {
  189. USB_LOG_RAW("mount fail,res:%d\r\n", (unsigned int)res_sd);
  190. return -1;
  191. }
  192. USB_LOG_RAW("test fatfs write\r\n");
  193. res_sd = f_open(&fnew, "2:test.txt", FA_CREATE_ALWAYS | FA_WRITE);
  194. if (res_sd == FR_OK) {
  195. res_sd = f_write(&fnew, read_write_buffer, sizeof(read_write_buffer), &fnum);
  196. if (res_sd == FR_OK) {
  197. USB_LOG_RAW("write success, write len:%d\n", fnum);
  198. } else {
  199. USB_LOG_RAW("write fail\r\n");
  200. goto unmount;
  201. }
  202. f_close(&fnew);
  203. } else {
  204. USB_LOG_RAW("open fail\r\n");
  205. goto unmount;
  206. }
  207. USB_LOG_RAW("test fatfs read\r\n");
  208. res_sd = f_open(&fnew, "2:test.txt", FA_OPEN_EXISTING | FA_READ);
  209. if (res_sd == FR_OK) {
  210. res_sd = f_read(&fnew, read_write_buffer, sizeof(read_write_buffer), &fnum);
  211. if (res_sd == FR_OK) {
  212. USB_LOG_RAW("read success, read len:%d\n", fnum);
  213. } else {
  214. USB_LOG_RAW("read fail\r\n");
  215. goto unmount;
  216. }
  217. f_close(&fnew);
  218. } else {
  219. USB_LOG_RAW("open fail\r\n");
  220. goto unmount;
  221. }
  222. #ifdef CONFIG_TEST_USBH_MSC_FATFS_SPEED
  223. for (uint32_t i = 0; i < BUF_SIZE; i++) {
  224. read_write_buffer[i] = i % 256;
  225. }
  226. USB_LOG_RAW("test fatfs write speed\r\n");
  227. res_sd = f_open(&fnew, "2:cherryusb_msc_test.bin", FA_OPEN_ALWAYS | FA_WRITE);
  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_write(&fnew, read_write_buffer, BUF_SIZE, (UINT *)&fnum);
  233. if (res_sd != FR_OK) {
  234. USB_LOG_RAW("Write 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 write speed:%f MB/S\r\n", (WRITE_SIZE_MB * 1000 / (float)time_ms));
  242. } else {
  243. USB_LOG_RAW("write 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. USB_LOG_RAW("test fatfs read speed\r\n");
  252. res_sd = f_open(&fnew, "2:cherryusb_msc_test.bin", FA_OPEN_EXISTING | FA_READ);
  253. if (res_sd == FR_OK) {
  254. uint32_t write_size = WRITE_SIZE;
  255. uint32_t start_time = (uint32_t)xTaskGetTickCount();
  256. while (write_size > 0) {
  257. res_sd = f_read(&fnew, read_write_buffer, BUF_SIZE, (UINT *)&fnum);
  258. if (res_sd != FR_OK) {
  259. USB_LOG_RAW("Read file failed, cause: %s\n", res_sd);
  260. goto unmount;
  261. }
  262. write_size -= BUF_SIZE;
  263. }
  264. if (res_sd == FR_OK) {
  265. uint32_t time_ms = xTaskGetTickCount() - start_time;
  266. USB_LOG_RAW("Fatfs read speed:%f MB/S\r\n", (WRITE_SIZE_MB * 1000 / (float)time_ms));
  267. } else {
  268. USB_LOG_RAW("read fail\r\n");
  269. goto unmount;
  270. }
  271. f_close(&fnew);
  272. } else {
  273. USB_LOG_RAW("open fail\r\n");
  274. goto unmount;
  275. }
  276. #endif
  277. f_mount(NULL, "2:", 1);
  278. return 0;
  279. unmount:
  280. f_mount(NULL, "2:", 1);
  281. return -1;
  282. }
  283. #endif
  284. USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t partition_table[512];
  285. static void usbh_msc_thread(CONFIG_USB_OSAL_THREAD_SET_ARGV)
  286. {
  287. int ret;
  288. struct usbh_msc *msc_class = (struct usbh_msc *)CONFIG_USB_OSAL_THREAD_GET_ARGV;
  289. (void)msc_class;
  290. /* test with only one buffer, if you have more msc class, modify by yourself */
  291. #ifndef TEST_USBH_MSC_FATFS
  292. ret = usbh_msc_scsi_init(msc_class);
  293. if (ret < 0) {
  294. USB_LOG_RAW("scsi_init error,ret:%d\r\n", ret);
  295. goto delete;
  296. }
  297. /* get the partition table */
  298. ret = usbh_msc_scsi_read10(msc_class, 0, partition_table, 1);
  299. if (ret < 0) {
  300. USB_LOG_RAW("scsi_read10 error,ret:%d\r\n", ret);
  301. goto delete;
  302. }
  303. for (uint32_t i = 0; i < 512; i++) {
  304. if (i % 16 == 0) {
  305. USB_LOG_RAW("\r\n");
  306. }
  307. USB_LOG_RAW("%02x ", partition_table[i]);
  308. }
  309. USB_LOG_RAW("\r\n");
  310. #else
  311. usb_msc_fatfs_test();
  312. #endif
  313. // clang-format off
  314. delete:
  315. usb_osal_thread_delete(NULL);
  316. // clang-format on
  317. }
  318. #endif
  319. #ifdef CONFIG_TEST_USBH_SERIAL
  320. void usbh_serial_run(struct usbh_serial *serial)
  321. {
  322. if (serial_is_opened) {
  323. return;
  324. }
  325. serial_is_opened = true;
  326. usb_osal_thread_create("usbh_serial", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_serial_thread, serial);
  327. }
  328. void usbh_serial_stop(struct usbh_serial *serial)
  329. {
  330. serial_is_opened = false;
  331. }
  332. #endif
  333. #ifdef CONFIG_TEST_USBH_HID
  334. void usbh_hid_run(struct usbh_hid *hid_class)
  335. {
  336. usb_osal_thread_create("usbh_hid", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_hid_thread, hid_class);
  337. }
  338. void usbh_hid_stop(struct usbh_hid *hid_class)
  339. {
  340. }
  341. #endif
  342. #ifdef CONFIG_TEST_USBH_MSC
  343. void usbh_msc_run(struct usbh_msc *msc_class)
  344. {
  345. usb_osal_thread_create("usbh_msc", 2048, CONFIG_USBHOST_PSC_PRIO + 1, usbh_msc_thread, msc_class);
  346. }
  347. void usbh_msc_stop(struct usbh_msc *msc_class)
  348. {
  349. }
  350. #endif
  351. #if 0
  352. #include "usbh_aoa.h"
  353. static struct aoa_string_info deviceinfo = {
  354. .acc_manufacturer = "CherryUSB",
  355. .acc_model = "CherryUSB",
  356. .acc_description = "Android Open Accessory CherryUSB",
  357. .acc_version = "1.0",
  358. .acc_uri = "http://developer.android.com/tools/adk/index.html",
  359. .acc_serial = "CherryUSB"
  360. };
  361. int aoa_switch(int argc, char **argv)
  362. {
  363. struct usbh_hubport *hport = usbh_find_hubport(0, 1, 1);
  364. usbh_aoa_switch(hport, &deviceinfo);
  365. return 0;
  366. }
  367. SHELL_CMD_EXPORT_ALIAS(aoa_switch, aoa_switch, aoa_switch);
  368. #endif