api_usb.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #ifndef _USB_API_H
  2. #define _USB_API_H
  3. #include "usb_define.h"
  4. #define EP_CNT 4
  5. #define USB_EP0_MAX_SIZE 64
  6. #define USB_EP1_MAX_SIZE 64
  7. #define USB_EP2_MAX_SIZE 256
  8. #define USB_EP3_MAX_SIZE 1023
  9. enum {
  10. USBCHK_ONLY_HOST,
  11. USBCHK_ONLY_DEVICE,
  12. USBCHK_OTG,
  13. };
  14. enum {
  15. USB_DISCONNECT,
  16. USB_DEV_CONNECTED,
  17. USB_HOST_CONNECTED,
  18. };
  19. enum {
  20. ///usb device message.
  21. MSG_USB_EP_RESET = 32, //usb device ep0 reset message.
  22. MSG_USB_EP0_CTL_FLOW, //usb device control flow message.
  23. MSG_USB_EP1_IN,
  24. MSG_USB_EP1_OUT,
  25. MSG_USB_EP2_IN,
  26. MSG_USB_EP2_OUT,
  27. MSG_USB_EP3_IN,
  28. MSG_USB_EP3_OUT,
  29. MSG_USB_SUSPEND,
  30. MSG_USB_RESUME,
  31. };
  32. typedef struct _epcb_t {
  33. psfr_t sfr;
  34. u8 type; //EP Type: Ctrl, Bulk, Int, ISOC
  35. u8 index; //EP Index
  36. volatile u8 error : 1, //USB通信出错
  37. dir : 1, //EP的方向
  38. halt : 1, //Halt标志
  39. first_pkt : 1; //Transfer first packet
  40. u8 *buf; //EP的BUF地址
  41. const u8 *xptr; //当前发送/接收指针
  42. u16 xlen; //当前发送/接收剩余
  43. u16 xcnt; //已发送/接收统计
  44. u16 epsize; //EP Size
  45. } epcb_t;
  46. typedef struct _spb_wrap_t {
  47. union {
  48. struct {
  49. u8 recipient : 5,
  50. type : 2,
  51. dir : 1;
  52. };
  53. u8 reqtype;
  54. };
  55. u8 req;
  56. u16 val;
  57. u16 index;
  58. u16 len;
  59. } spb_wrap_t;
  60. typedef bool (*usb_init_handler)(void);
  61. typedef bool (*usb_evt_handler)(uint8_t event, spb_wrap_t *spb, uint8_t *data, uint8_t data_len);
  62. typedef struct {
  63. usb_init_handler init;
  64. usb_evt_handler evt;
  65. }usb_handler_cb_t;
  66. void usb_handler_register(usb_handler_cb_t *handler);
  67. void usb_sof_hook_register(void (*callback)(void));
  68. u8 usbchk_connect(u8 mode);
  69. u8 usbchk_host_connect(void);
  70. void usb_init2(void);
  71. void usb_deinit2(void);
  72. // void usb_init(void);
  73. // void usb_disable(void);
  74. // void usb_device_enter(void);
  75. // void usb_device_exit(void);
  76. bool usb_device_is_suspend(void);
  77. void usb_device_suspend_enable(void);
  78. void usb_device_resume(void);
  79. void usb_ep_init(epcb_t *epcb);
  80. uint16_t usb_ep_get_max_len(uint8_t ep_index);
  81. uint16_t usb_ep_get_rx_len(epcb_t *epcb);
  82. void usb_ep_clear_rx_fifo(epcb_t *epcb);
  83. bool usb_ep_transfer(epcb_t *epcb);
  84. bool usb_ep_start_transfer(epcb_t *epcb, uint len);
  85. bool usb_ep0_start_transfer(void *buf, uint len);
  86. epcb_t * usb_ep_get_for_index(uint8_t index, uint8_t dir);
  87. void usb_ep_reset(epcb_t *epcb);
  88. void usb_ep_clear(epcb_t *epcb);
  89. void usb_ep_halt(epcb_t *epcb);
  90. #define usb_init(...) usb_init2(__VA_ARGS__)
  91. #define usb_device_exit(...) usb_deinit2(__VA_ARGS__)
  92. #endif // _USB_API_H