usb_com.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "include.h"
  2. #include "usb_com.h"
  3. #include "usb_dev_enum.h"
  4. #include "usb_hid.h"
  5. #include "usb_audio.h"
  6. #include "usb_vendor.h"
  7. ep_cb_t ep_cb;
  8. usb_handler_cb_t usb_handler_cb;
  9. #define SWDIAG(func, filed, value)
  10. AT(.com_text.thread.driver)
  11. void thread_driver_event_handle(uint8_t msg)
  12. {
  13. switch (msg) {
  14. #if USB_HID_EN
  15. case THREAD_DRIVER_MSG_HID_IN:
  16. usb_hid_send_kick();
  17. break;
  18. #endif
  19. #if USB_VENDOR_EN
  20. case THREAD_DRIVER_MSG_VENDOR_IN:
  21. usb_vendor_send_kick();
  22. break;
  23. #endif
  24. #if USB_EN
  25. case THREAD_DRIVER_MSG_USB_IND:
  26. if (sys_cb.usb_is_active) {
  27. wireless_send_ctrl_cmd(WIRELESS_CTRL_CMD_CODE_USB_CONNECTED, 0, NULL);
  28. }
  29. break;
  30. #endif // USB_EN
  31. #if USB_MIC_EN
  32. case THREAD_DRIVER_MSG_UAC_MIC_CTRL:
  33. if (uda_mic_check()) {
  34. wireless_send_ctrl_cmd(WIRELESS_CTRL_CMD_CODE_MIC_ALLOW, 0, NULL);
  35. }
  36. break;
  37. case THREAD_DRIVER_MSG_UAC_DEC:
  38. if (uda_mic_check()) {
  39. SWDIAG(DEC, IO, ENTER);
  40. #if (AUDIO_DEC_ADPCM_EN)
  41. usb_audio_mic_stream(NULL, ADPCM_BYTE_2_SAMPLE(AUDIO_MIC_PACKET_SIZE));
  42. #elif (AUDIO_DEC_MSBC_EN)
  43. usb_audio_mic_stream(NULL, 120);
  44. #elif (AUDIO_DEC_SBC_EN)
  45. usb_audio_mic_stream(NULL, 128);
  46. #endif
  47. SWDIAG(DEC, IO, EXIT);
  48. }
  49. break;
  50. #endif
  51. default:
  52. break;
  53. }
  54. }
  55. void usb_ep_callback_register(ep_handler callback, uint8_t ep_index, uint8_t ep_dir)
  56. {
  57. if (ep_index < EP_CNT) {
  58. if (ep_dir) {
  59. ep_cb.eptx_handler[ep_index] = callback;
  60. } else {
  61. ep_cb.eprx_handler[ep_index] = callback;
  62. }
  63. }
  64. }
  65. void usb_ep_reset_deal(void)
  66. {
  67. #if USB_HID_EN
  68. usb_int_ep_reset();
  69. #endif
  70. #if USB_AUDIO_EN
  71. usb_isoc_ep_reset();
  72. #endif
  73. #if USB_VENDOR_EN
  74. usb_vendor_ep_reset();
  75. #endif
  76. }
  77. AT(.usbdev.com)
  78. void usb_device_process(void)
  79. {
  80. #if USB_HID_EN
  81. usb_hid_process();
  82. #endif
  83. #if USB_AUDIO_EN
  84. usb_audio_process();
  85. #endif
  86. #if USB_VENDOR_EN
  87. usb_vendor_process();
  88. #endif
  89. }
  90. AT(.com_text.isr.sof)
  91. void usb_sof_callback(void)
  92. {
  93. // Enter: interrupt IRQ
  94. #if USB_HID_EN
  95. usb_sof_hid_isr();
  96. #endif
  97. // Exit: interrupt IRQ
  98. }
  99. AT(.usbdev.com)
  100. bool usb_event_callback(uint8_t event, spb_wrap_t *spb, uint8_t *data, uint8_t data_len)
  101. {
  102. switch (event){
  103. case MSG_USB_EP_RESET:
  104. printf("-->MSG_USB_EP_RESET\n");
  105. ude_state_reset(&ude_cb);
  106. usb_ep_reset_deal();
  107. break;
  108. case MSG_USB_EP0_CTL_FLOW:
  109. return usb_ep0_ctl_callback(spb, data, data_len);
  110. case MSG_USB_SUSPEND:
  111. printf("--->MSG_USB_SUSPEND\n");
  112. #if USB_HID_EN
  113. usb_hid_suspend_callback(true);
  114. #endif
  115. break;
  116. case MSG_USB_RESUME:
  117. printf("--->MSG_USB_RESUME\n");
  118. #if USB_HID_EN
  119. usb_hid_suspend_callback(false);
  120. #endif
  121. break;
  122. case MSG_USB_EP1_IN:
  123. if (ep_cb.eptx_handler[1]) {
  124. ep_cb.eptx_handler[1]();
  125. }
  126. break;
  127. case MSG_USB_EP1_OUT:
  128. if (ep_cb.eprx_handler[1]) {
  129. ep_cb.eprx_handler[1]();
  130. }
  131. break;
  132. case MSG_USB_EP2_IN:
  133. if (ep_cb.eptx_handler[2]) {
  134. ep_cb.eptx_handler[2]();
  135. }
  136. break;
  137. case MSG_USB_EP2_OUT:
  138. if (ep_cb.eprx_handler[2]) {
  139. ep_cb.eprx_handler[2]();
  140. }
  141. break;
  142. case MSG_USB_EP3_IN:
  143. if (ep_cb.eptx_handler[3]) {
  144. ep_cb.eptx_handler[3]();
  145. }
  146. break;
  147. case MSG_USB_EP3_OUT:
  148. if (ep_cb.eprx_handler[3]) {
  149. ep_cb.eprx_handler[3]();
  150. }
  151. break;
  152. default:
  153. return false;
  154. }
  155. return true;
  156. }
  157. bool usb_ep_init_handler(void)
  158. {
  159. #if USB_HID_EN
  160. udh_init();
  161. #endif
  162. #if USB_AUDIO_EN
  163. uda_init();
  164. #endif
  165. #if USB_VENDOR_EN
  166. udv_init();
  167. #endif
  168. return true;
  169. }
  170. void usb_device_init(void)
  171. {
  172. usb_handler_cb.init = usb_ep_init_handler;
  173. usb_handler_cb.evt = usb_event_callback;
  174. usb_handler_register(&usb_handler_cb);
  175. usb_sof_hook_register(usb_sof_callback);
  176. thread_driver_user_callback_register(thread_driver_event_handle);
  177. usb_init();
  178. #if USB_AUDIO_EN
  179. usb_audio_init();
  180. #endif
  181. #if USB_HID_EN
  182. usb_hid_init();
  183. #endif // USB_HID_EN
  184. #if USB_VENDOR_EN
  185. usb_vendor_init();
  186. #endif // USB_VENDOR_EN
  187. }
  188. void usb_device_deinit(void)
  189. {
  190. #if USB_AUDIO_EN
  191. usb_audio_deinit();
  192. #endif
  193. #if USB_HID_EN
  194. usb_hid_deinit();
  195. #endif // USB_HID_EN
  196. #if USB_VENDOR_EN
  197. usb_vendor_deinit();
  198. #endif
  199. }