drv_usbd.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (C) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-02-14 CDT first version
  9. */
  10. #ifndef __DRV_USBD_H__
  11. #define __DRV_USBD_H__
  12. /*******************************************************************************
  13. * Include files
  14. ******************************************************************************/
  15. #include <rtthread.h>
  16. #include "rtdevice.h"
  17. #include "hc32_ll_usb.h"
  18. /* C binding of definitions if building with C++ compiler */
  19. #ifdef __cplusplus
  20. extern "C"
  21. {
  22. #endif
  23. /*******************************************************************************
  24. * Global pre-processor symbols/macros ('#define')
  25. ******************************************************************************/
  26. /* The bit of the diepint/doepint */
  27. #define XFER_COMPL (1UL)
  28. #define EPDISABLED (1UL<<1)
  29. #define TIME_OUT (1UL<<3)
  30. #define SETUP_BIT (1UL<<3)
  31. #define INTKNTXFEMP (1UL<<4)
  32. #define INEPNAKEFF (1UL<<6)
  33. #define TXFEMP (1UL<<7)
  34. /* The bit of the GINTSTS */
  35. #define MODEMIS_INT (1UL<<1)
  36. #define SOF_INT (1UL<<3)
  37. #define RXFLVL_INT (1UL<<4)
  38. #define USBSUSP_INT (1UL<<11)
  39. #define USBRST_INT (1UL<<12)
  40. #define ENUMDONE_INT (1UL<<13)
  41. #define INEP_INT (1UL<<18)
  42. #define OUTEP_INT (1UL<<19)
  43. #define INCOMPLSOIN (1UL<<20)
  44. #define INCOMPLSOOUT (1UL<<21)
  45. #define VBUSV_INT (1UL<<30)
  46. #define WAKEUP_INT (1UL<<31)
  47. /* Data packet status for device mode */
  48. #define STS_GOUT_NAK (1U)
  49. #define STS_DATA_UPDT (2U)
  50. #define STS_XFER_COMP (3U)
  51. #define STS_SETUP_COMP (4U)
  52. #define STS_SETUP_UPDT (6U)
  53. /* USB EP0 state */
  54. #define USB_EP0_IDLE (0U)
  55. #define USB_EP0_SETUP (1U)
  56. #define USB_EP0_DATA_IN (2U)
  57. #define USB_EP0_DATA_OUT (3U)
  58. #define USB_EP0_STATUS_IN (4U)
  59. #define USB_EP0_STATUS_OUT (5U)
  60. #define USB_EP0_STALL (6U)
  61. /*******************************************************************************
  62. * Global type definitions ('typedef')
  63. ******************************************************************************/
  64. typedef enum
  65. {
  66. CTRL_START = 0U,
  67. CTRL_XFRC,
  68. CTRL_HALTED,
  69. CTRL_NAK,
  70. CTRL_STALL,
  71. CTRL_XACTERR,
  72. CTRL_BBLERR,
  73. CTRL_DATATGLERR,
  74. CTRL_FAIL
  75. } CTRL_HANDLE_STATUS;
  76. typedef struct
  77. {
  78. uint8_t bmRequest;
  79. uint8_t bRequest;
  80. uint16_t wValue;
  81. uint16_t wIndex;
  82. uint16_t wLength;
  83. } USB_SETUP_REQ;
  84. typedef struct
  85. {
  86. uint8_t *(*get_dev_desc)(uint16_t *length);
  87. uint8_t *(*get_dev_langiddesc)(uint16_t *length);
  88. uint8_t *(*get_dev_manufacturerstr)(uint16_t *length);
  89. uint8_t *(*get_dev_productstr)(uint16_t *length);
  90. uint8_t *(*get_dev_serialstr)(uint16_t *length);
  91. uint8_t *(*get_dev_configstr)(uint16_t *length);
  92. uint8_t *(*get_dev_interfacestr)(uint16_t *length);
  93. } usb_dev_desc_func;
  94. typedef struct
  95. {
  96. void (*class_init)(void *pdev);
  97. void (*class_deinit)(void *pdev);
  98. uint8_t (*ep0_setup)(void *pdev, USB_SETUP_REQ *req);
  99. void (*ep0_datain)(void *pdev);
  100. void (*ep0_dataout)(void *pdev);
  101. uint8_t *(*class_getconfigdesc)(uint16_t *length);
  102. uint8_t (*class_sof)(void *pdev);
  103. void (*class_datain)(void *pdev, uint8_t epnum);
  104. void (*class_dataout)(void *pdev, uint8_t epnum);
  105. void (*class_syn_in_incomplt)(void *pdev);
  106. void (*class_syn_out_incomplt)(void *pdev);
  107. } usb_dev_class_func;
  108. typedef struct
  109. {
  110. void (*user_init)(void);
  111. void (*user_devrst)(void);
  112. void (*user_devconfig)(void);
  113. void (*user_devsusp)(void);
  114. void (*user_devresume)(void);
  115. void (*user_devconn)(void);
  116. void (*user_devdisconn)(void);
  117. } usb_dev_user_func;
  118. typedef struct
  119. {
  120. __IO uint8_t device_config;
  121. __IO uint8_t device_address;
  122. __IO uint8_t device_state;
  123. __IO uint8_t device_old_status;
  124. __IO uint8_t device_cur_status;
  125. __IO uint8_t connection_status;
  126. __IO uint8_t device_remote_wakeup;
  127. __IO uint8_t test_mode;
  128. USB_DEV_EP in_ep[USB_MAX_TX_FIFOS];
  129. USB_DEV_EP out_ep[USB_MAX_TX_FIFOS];
  130. uint8_t setup_pkt_buf[24];
  131. usb_dev_class_func *class_callback;
  132. usb_dev_user_func *user_callback;
  133. usb_dev_desc_func *desc_callback;
  134. } USB_DEV_PARAM;
  135. typedef struct
  136. {
  137. USB_CORE_BASIC_CFGS basic_cfgs;
  138. LL_USB_TypeDef regs;
  139. #ifdef USE_DEVICE_MODE
  140. USB_DEV_PARAM dev;
  141. #endif
  142. } usb_core_instance;
  143. typedef struct
  144. {
  145. void (* Reset)(usb_core_instance *pdev);
  146. void (* devctrlconnect)(usb_core_instance *pdev, uint8_t conn);
  147. void (* Suspend)(usb_core_instance *pdev);
  148. void (* Resume)(usb_core_instance *pdev);
  149. void (* SOF)(usb_core_instance *pdev);
  150. void (* SetupStage)(usb_core_instance *pdev);
  151. void (* DataOutStage)(usb_core_instance *pdev, uint8_t epnum);
  152. void (* DataInStage)(usb_core_instance *pdev, uint8_t epnum);
  153. void (* IsoINIncomplete)(usb_core_instance *pdev);
  154. void (* IsoOUTIncomplete)(usb_core_instance *pdev);
  155. } usb_dev_int_cbk_typedef;
  156. /*******************************************************************************
  157. Global function prototypes (definition in C source)
  158. ******************************************************************************/
  159. /**
  160. * @brief get the IN Endpoint TxFIFO avail space
  161. * @param [in] pdev device instance
  162. * @param [in] epnum endpoint number
  163. * @retval the avail space in 32-bit words
  164. */
  165. __STATIC_INLINE uint16_t usb_rdineptxfspcavail(usb_core_instance *pdev, uint32_t epnum)
  166. {
  167. return (uint16_t)(READ_REG32(pdev->regs.INEP_REGS[epnum]->DTXFSTS) & USBFS_DTXFSTS_INEPTFSAV);
  168. }
  169. /**
  170. * @brief set the USB turn around time
  171. * @param [in] pdev device instance
  172. * @retval None
  173. */
  174. __STATIC_INLINE void usb_setaroundtim(usb_core_instance *pdev)
  175. {
  176. SET_REG32_BIT(pdev->regs.GREGS->GUSBCFG, USBFS_GUSBCFG_TRDT);
  177. }
  178. #ifdef __cplusplus
  179. }
  180. #endif
  181. #endif /* __DRV_USBD_H__ */
  182. /*******************************************************************************
  183. * EOF (not truncated)
  184. ******************************************************************************/