usb_host.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "usbh_core.h"
  2. #include "usbh_cdc_acm.h"
  3. #include "usbh_hid.h"
  4. #include "usbh_msc.h"
  5. USB_MEM_ALIGN32 uint8_t cdc_buffer[512];
  6. void usbh_cdc_acm_callback(void *arg, int nbytes)
  7. {
  8. //struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)arg;
  9. if (nbytes > 0) {
  10. for (size_t i = 0; i < nbytes; i++) {
  11. USB_LOG_RAW("0x%02x ", cdc_buffer[i]);
  12. }
  13. }
  14. USB_LOG_RAW("nbytes:%d\r\n", nbytes);
  15. }
  16. int cdc_acm_test(void)
  17. {
  18. int ret;
  19. struct usbh_cdc_acm *cdc_acm_class = (struct usbh_cdc_acm *)usbh_find_class_instance("/dev/ttyACM0");
  20. if (cdc_acm_class == NULL) {
  21. USB_LOG_RAW("do not find /dev/ttyACM0\r\n");
  22. return -1;
  23. }
  24. memset(cdc_buffer, 0, 512);
  25. ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkin, cdc_buffer, 512, 3000);
  26. if (ret < 0) {
  27. USB_LOG_RAW("bulk in error,ret:%d\r\n", ret);
  28. } else {
  29. USB_LOG_RAW("recv over:%d\r\n", ret);
  30. for (size_t i = 0; i < ret; i++) {
  31. USB_LOG_RAW("0x%02x ", cdc_buffer[i]);
  32. }
  33. }
  34. USB_LOG_RAW("\r\n");
  35. const uint8_t data1[10] = { 0x02, 0x00, 0x00, 0x00, 0x02, 0x02, 0x08, 0x14 };
  36. memcpy(cdc_buffer, data1, 8);
  37. ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkout, cdc_buffer, 8, 3000);
  38. if (ret < 0) {
  39. USB_LOG_RAW("bulk out error,ret:%d\r\n", ret);
  40. } else {
  41. USB_LOG_RAW("send over:%d\r\n", ret);
  42. }
  43. #if 0
  44. usbh_ep_bulk_async_transfer(cdc_acm_class->bulkin, cdc_buffer, 512, usbh_cdc_acm_callback, cdc_acm_class);
  45. #else
  46. ret = usbh_ep_bulk_transfer(cdc_acm_class->bulkin, cdc_buffer, 512, 3000);
  47. if (ret < 0) {
  48. USB_LOG_RAW("bulk in error,ret:%d\r\n", ret);
  49. } else {
  50. USB_LOG_RAW("recv over:%d\r\n", ret);
  51. for (size_t i = 0; i < ret; i++) {
  52. USB_LOG_RAW("0x%02x ", cdc_buffer[i]);
  53. }
  54. }
  55. USB_LOG_RAW("\r\n");
  56. return ret;
  57. #endif
  58. }
  59. #if 0
  60. #include "ff.h"
  61. #endif
  62. USB_MEM_ALIGN32 uint8_t partition_table[512];
  63. int msc_test(void)
  64. {
  65. int ret;
  66. struct usbh_msc *msc_class = (struct usbh_msc *)usbh_find_class_instance("/dev/sda");
  67. if (msc_class == NULL) {
  68. USB_LOG_RAW("do not find /dev/sda\r\n");
  69. return -1;
  70. }
  71. #if 1
  72. /* get the partition table */
  73. ret = usbh_msc_scsi_read10(msc_class, 0, partition_table, 1);
  74. if (ret < 0) {
  75. USB_LOG_RAW("scsi_read10 error,ret:%d\r\n", ret);
  76. return ret;
  77. }
  78. for (uint32_t i = 0; i < 512; i++) {
  79. if (i % 16 == 0) {
  80. USB_LOG_RAW("\r\n");
  81. }
  82. USB_LOG_RAW("%02x ", partition_table[i]);
  83. }
  84. USB_LOG_RAW("\r\n");
  85. #endif
  86. #if 0
  87. uint8_t *msc_buffer = usb_iomalloc(8192);
  88. ret = usbh_msc_scsi_read10(msc_class, 0, msc_buffer, 16);
  89. usb_iofree(msc_buffer);
  90. // for (uint32_t i = 0; i < 1024; i++) {
  91. // if (i % 16 == 0) {
  92. // USB_LOG_RAW("\r\n");
  93. // }
  94. // USB_LOG_RAW("%02x ", msc_buffer[i]);
  95. // }
  96. // USB_LOG_RAW("\r\n");
  97. #endif
  98. #if 0
  99. FATFS fs;
  100. FIL fnew;
  101. UINT fnum;
  102. FRESULT res_sd = 0;
  103. uint8_t *ReadBuffer;
  104. ReadBuffer = usb_iomalloc(512);
  105. f_mount(&fs, "2:", 1);
  106. res_sd = f_open(&fnew, "2:test.c", FA_OPEN_EXISTING | FA_READ);
  107. if (res_sd == FR_OK) {
  108. res_sd = f_read(&fnew, ReadBuffer, 512, &fnum);
  109. for (uint32_t i = 0; i < fnum; i++) {
  110. if (i % 16 == 0) {
  111. USB_LOG_RAW("\r\n");
  112. }
  113. USB_LOG_RAW("%02x ", ReadBuffer[i]);
  114. }
  115. USB_LOG_RAW("\r\n");
  116. f_close(&fnew);
  117. /*unmount*/
  118. f_mount(NULL, "2:", 1);
  119. } else {
  120. USB_LOG_RAW("open error:%d\r\n", res_sd);
  121. }
  122. usb_iofree(ReadBuffer);
  123. #endif
  124. return ret;
  125. }
  126. USB_MEM_ALIGN32 uint8_t hid_buffer[128];
  127. void usbh_hid_callback(void *arg, int nbytes)
  128. {
  129. //struct usbh_hid *hid_class = (struct usbh_hid *)arg;
  130. if (nbytes > 0) {
  131. for (size_t i = 0; i < nbytes; i++) {
  132. USB_LOG_RAW("0x%02x ", hid_buffer[i]);
  133. }
  134. }
  135. USB_LOG_RAW("nbytes:%d\r\n", nbytes);
  136. }
  137. int hid_test(void)
  138. {
  139. int ret;
  140. struct usbh_hid *hid_class = (struct usbh_hid *)usbh_find_class_instance("/dev/input0");
  141. if (hid_class == NULL) {
  142. USB_LOG_RAW("do not find /dev/input0\r\n");
  143. return -1;
  144. }
  145. #if 1
  146. ret = usbh_ep_intr_async_transfer(hid_class->intin, hid_buffer, 128, usbh_hid_callback, hid_class);
  147. if (ret < 0) {
  148. USB_LOG_RAW("intr asnyc in error,ret:%d\r\n", ret);
  149. }
  150. #else
  151. ret = usbh_ep_intr_transfer(hid_class->intin, hid_buffer, 128, 1000);
  152. if (ret < 0) {
  153. USB_LOG_RAW("intr in error,ret:%d\r\n", ret);
  154. return ret;
  155. }
  156. USB_LOG_RAW("recv len:%d\r\n", ret);
  157. #endif
  158. return ret;
  159. }