ch32v20x_usb.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /********************************** (C) COPYRIGHT *******************************
  2. * File Name : ch32v20x_usb.h
  3. * Author : WCH
  4. * Version : V1.0.0
  5. * Date : 2024/01/30
  6. * Description : This file contains all the functions prototypes for the USB
  7. * firmware library.
  8. *********************************************************************************
  9. * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
  10. * Attention: This software (modified or not) and binary are used for
  11. * microcontroller manufactured by Nanjing Qinheng Microelectronics.
  12. *******************************************************************************/
  13. #ifndef __CH32V20X_USB_H
  14. #define __CH32V20X_USB_H
  15. #ifdef __cplusplus
  16. extern "C" {
  17. #endif
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /*******************************************************************************/
  22. /* Header File */
  23. #include "stdint.h"
  24. /*******************************************************************************/
  25. /* USB Communication Related Macro Definition */
  26. #ifndef DEFAULT_ENDP0_SIZE
  27. #define DEFAULT_ENDP0_SIZE 8 // default maximum packet size for endpoint 0
  28. #endif
  29. #ifndef MAX_PACKET_SIZE
  30. #define MAX_PACKET_SIZE 64 // maximum packet size
  31. #endif
  32. /* USB PID */
  33. #ifndef USB_PID_SETUP
  34. #define USB_PID_NULL 0x00
  35. #define USB_PID_SOF 0x05
  36. #define USB_PID_SETUP 0x0D
  37. #define USB_PID_IN 0x09
  38. #define USB_PID_OUT 0x01
  39. #define USB_PID_NYET 0x06
  40. #define USB_PID_ACK 0x02
  41. #define USB_PID_NAK 0x0A
  42. #define USB_PID_STALL 0x0E
  43. #define USB_PID_DATA0 0x03
  44. #define USB_PID_DATA1 0x0B
  45. #define USB_PID_PRE 0x0C
  46. #endif
  47. /* USB standard device request code */
  48. #ifndef USB_GET_DESCRIPTOR
  49. #define USB_GET_STATUS 0x00
  50. #define USB_CLEAR_FEATURE 0x01
  51. #define USB_SET_FEATURE 0x03
  52. #define USB_SET_ADDRESS 0x05
  53. #define USB_GET_DESCRIPTOR 0x06
  54. #define USB_SET_DESCRIPTOR 0x07
  55. #define USB_GET_CONFIGURATION 0x08
  56. #define USB_SET_CONFIGURATION 0x09
  57. #define USB_GET_INTERFACE 0x0A
  58. #define USB_SET_INTERFACE 0x0B
  59. #define USB_SYNCH_FRAME 0x0C
  60. #endif
  61. #define DEF_STRING_DESC_LANG 0x00
  62. #define DEF_STRING_DESC_MANU 0x01
  63. #define DEF_STRING_DESC_PROD 0x02
  64. #define DEF_STRING_DESC_SERN 0x03
  65. /* USB hub class request code */
  66. #ifndef HUB_GET_DESCRIPTOR
  67. #define HUB_GET_STATUS 0x00
  68. #define HUB_CLEAR_FEATURE 0x01
  69. #define HUB_GET_STATE 0x02
  70. #define HUB_SET_FEATURE 0x03
  71. #define HUB_GET_DESCRIPTOR 0x06
  72. #define HUB_SET_DESCRIPTOR 0x07
  73. #endif
  74. /* USB HID class request code */
  75. #ifndef HID_GET_REPORT
  76. #define HID_GET_REPORT 0x01
  77. #define HID_GET_IDLE 0x02
  78. #define HID_GET_PROTOCOL 0x03
  79. #define HID_SET_REPORT 0x09
  80. #define HID_SET_IDLE 0x0A
  81. #define HID_SET_PROTOCOL 0x0B
  82. #endif
  83. /* Bit Define for USB Request Type */
  84. #ifndef USB_REQ_TYP_MASK
  85. #define USB_REQ_TYP_IN 0x80
  86. #define USB_REQ_TYP_OUT 0x00
  87. #define USB_REQ_TYP_READ 0x80
  88. #define USB_REQ_TYP_WRITE 0x00
  89. #define USB_REQ_TYP_MASK 0x60
  90. #define USB_REQ_TYP_STANDARD 0x00
  91. #define USB_REQ_TYP_CLASS 0x20
  92. #define USB_REQ_TYP_VENDOR 0x40
  93. #define USB_REQ_TYP_RESERVED 0x60
  94. #define USB_REQ_RECIP_MASK 0x1F
  95. #define USB_REQ_RECIP_DEVICE 0x00
  96. #define USB_REQ_RECIP_INTERF 0x01
  97. #define USB_REQ_RECIP_ENDP 0x02
  98. #define USB_REQ_RECIP_OTHER 0x03
  99. #define USB_REQ_FEAT_REMOTE_WAKEUP 0x01
  100. #define USB_REQ_FEAT_ENDP_HALT 0x00
  101. #endif
  102. /* USB Descriptor Type */
  103. #ifndef USB_DESCR_TYP_DEVICE
  104. #define USB_DESCR_TYP_DEVICE 0x01
  105. #define USB_DESCR_TYP_CONFIG 0x02
  106. #define USB_DESCR_TYP_STRING 0x03
  107. #define USB_DESCR_TYP_INTERF 0x04
  108. #define USB_DESCR_TYP_ENDP 0x05
  109. #define USB_DESCR_TYP_QUALIF 0x06
  110. #define USB_DESCR_TYP_SPEED 0x07
  111. #define USB_DESCR_TYP_OTG 0x09
  112. #define USB_DESCR_TYP_BOS 0X0F
  113. #define USB_DESCR_TYP_HID 0x21
  114. #define USB_DESCR_TYP_REPORT 0x22
  115. #define USB_DESCR_TYP_PHYSIC 0x23
  116. #define USB_DESCR_TYP_CS_INTF 0x24
  117. #define USB_DESCR_TYP_CS_ENDP 0x25
  118. #define USB_DESCR_TYP_HUB 0x29
  119. #endif
  120. /* USB Device Class */
  121. #ifndef USB_DEV_CLASS_HUB
  122. #define USB_DEV_CLASS_RESERVED 0x00
  123. #define USB_DEV_CLASS_AUDIO 0x01
  124. #define USB_DEV_CLASS_COMMUNIC 0x02
  125. #define USB_DEV_CLASS_HID 0x03
  126. #define USB_DEV_CLASS_MONITOR 0x04
  127. #define USB_DEV_CLASS_PHYSIC_IF 0x05
  128. #define USB_DEV_CLASS_POWER 0x06
  129. #define USB_DEV_CLASS_IMAGE 0x06
  130. #define USB_DEV_CLASS_PRINTER 0x07
  131. #define USB_DEV_CLASS_STORAGE 0x08
  132. #define USB_DEV_CLASS_HUB 0x09
  133. #define USB_DEV_CLASS_VEN_SPEC 0xFF
  134. #endif
  135. /* USB Hub Class Request */
  136. #ifndef HUB_GET_HUB_DESCRIPTOR
  137. #define HUB_CLEAR_HUB_FEATURE 0x20
  138. #define HUB_CLEAR_PORT_FEATURE 0x23
  139. #define HUB_GET_BUS_STATE 0xA3
  140. #define HUB_GET_HUB_DESCRIPTOR 0xA0
  141. #define HUB_GET_HUB_STATUS 0xA0
  142. #define HUB_GET_PORT_STATUS 0xA3
  143. #define HUB_SET_HUB_DESCRIPTOR 0x20
  144. #define HUB_SET_HUB_FEATURE 0x20
  145. #define HUB_SET_PORT_FEATURE 0x23
  146. #endif
  147. /* Hub Class Feature Selectors */
  148. #ifndef HUB_PORT_RESET
  149. #define HUB_C_HUB_LOCAL_POWER 0
  150. #define HUB_C_HUB_OVER_CURRENT 1
  151. #define HUB_PORT_CONNECTION 0
  152. #define HUB_PORT_ENABLE 1
  153. #define HUB_PORT_SUSPEND 2
  154. #define HUB_PORT_OVER_CURRENT 3
  155. #define HUB_PORT_RESET 4
  156. #define HUB_PORT_POWER 8
  157. #define HUB_PORT_LOW_SPEED 9
  158. #define HUB_C_PORT_CONNECTION 16
  159. #define HUB_C_PORT_ENABLE 17
  160. #define HUB_C_PORT_SUSPEND 18
  161. #define HUB_C_PORT_OVER_CURRENT 19
  162. #define HUB_C_PORT_RESET 20
  163. #endif
  164. /* USB HID Class Request Code */
  165. #ifndef HID_GET_REPORT
  166. #define HID_GET_REPORT 0x01
  167. #define HID_GET_IDLE 0x02
  168. #define HID_GET_PROTOCOL 0x03
  169. #define HID_SET_REPORT 0x09
  170. #define HID_SET_IDLE 0x0A
  171. #define HID_SET_PROTOCOL 0x0B
  172. #endif
  173. /* USB CDC Class request code */
  174. #ifndef CDC_GET_LINE_CODING
  175. #define CDC_GET_LINE_CODING 0X21 /* This request allows the host to find out the currently configured line coding */
  176. #define CDC_SET_LINE_CODING 0x20 /* Configures DTE rate, stop-bits, parity, and number-of-character */
  177. #define CDC_SET_LINE_CTLSTE 0X22 /* This request generates RS-232/V.24 style control signals */
  178. #define CDC_SEND_BREAK 0X23 /* Sends special carrier modulation used to specify RS-232 style break */
  179. #endif
  180. /* USB UDisk */
  181. #ifndef USB_BO_CBW_SIZE
  182. #define USB_BO_CBW_SIZE 0x1F
  183. #define USB_BO_CSW_SIZE 0x0D
  184. #endif
  185. #ifndef USB_BO_CBW_SIG0
  186. #define USB_BO_CBW_SIG0 0x55
  187. #define USB_BO_CBW_SIG1 0x53
  188. #define USB_BO_CBW_SIG2 0x42
  189. #define USB_BO_CBW_SIG3 0x43
  190. #define USB_BO_CSW_SIG0 0x55
  191. #define USB_BO_CSW_SIG1 0x53
  192. #define USB_BO_CSW_SIG2 0x42
  193. #define USB_BO_CSW_SIG3 0x53
  194. #endif
  195. /*******************************************************************************/
  196. /* USBFS Related Register Macro Definition */
  197. /* R8_USB_CTRL */
  198. #define USBFS_UC_HOST_MODE 0x80
  199. #define USBFS_UC_LOW_SPEED 0x40
  200. #define USBFS_UC_DEV_PU_EN 0x20
  201. #define USBFS_UC_SYS_CTRL_MASK 0x30
  202. #define USBFS_UC_SYS_CTRL0 0x00
  203. #define USBFS_UC_SYS_CTRL1 0x10
  204. #define USBFS_UC_SYS_CTRL2 0x20
  205. #define USBFS_UC_SYS_CTRL3 0x30
  206. #define USBFS_UC_INT_BUSY 0x08
  207. #define USBFS_UC_RESET_SIE 0x04
  208. #define USBFS_UC_CLR_ALL 0x02
  209. #define USBFS_UC_DMA_EN 0x01
  210. /* R8_USB_INT_EN */
  211. #define USBFS_UIE_DEV_NAK 0x40
  212. #define USBFS_UIE_FIFO_OV 0x10
  213. #define USBFS_UIE_HST_SOF 0x08
  214. #define USBFS_UIE_SUSPEND 0x04
  215. #define USBFS_UIE_TRANSFER 0x02
  216. #define USBFS_UIE_DETECT 0x01
  217. #define USBFS_UIE_BUS_RST 0x01
  218. /* R8_USB_DEV_AD */
  219. #define USBFS_UDA_GP_BIT 0x80
  220. #define USBFS_USB_ADDR_MASK 0x7F
  221. /* R8_USB_MIS_ST */
  222. #define USBFS_UMS_SOF_PRES 0x80
  223. #define USBFS_UMS_SOF_ACT 0x40
  224. #define USBFS_UMS_SIE_FREE 0x20
  225. #define USBFS_UMS_R_FIFO_RDY 0x10
  226. #define USBFS_UMS_BUS_RESET 0x08
  227. #define USBFS_UMS_SUSPEND 0x04
  228. #define USBFS_UMS_DM_LEVEL 0x02
  229. #define USBFS_UMS_DEV_ATTACH 0x01
  230. /* R8_USB_INT_FG */
  231. #define USBFS_U_IS_NAK 0x80 // RO, indicate current USB transfer is NAK received
  232. #define USBFS_U_TOG_OK 0x40 // RO, indicate current USB transfer toggle is OK
  233. #define USBFS_U_SIE_FREE 0x20 // RO, indicate USB SIE free status
  234. #define USBFS_UIF_FIFO_OV 0x10 // FIFO overflow interrupt flag for USB, direct bit address clear or write 1 to clear
  235. #define USBFS_UIF_HST_SOF 0x08 // host SOF timer interrupt flag for USB host, direct bit address clear or write 1 to clear
  236. #define USBFS_UIF_SUSPEND 0x04 // USB suspend or resume event interrupt flag, direct bit address clear or write 1 to clear
  237. #define USBFS_UIF_TRANSFER 0x02 // USB transfer completion interrupt flag, direct bit address clear or write 1 to clear
  238. #define USBFS_UIF_DETECT 0x01 // device detected event interrupt flag for USB host mode, direct bit address clear or write 1 to clear
  239. #define USBFS_UIF_BUS_RST 0x01 // bus reset event interrupt flag for USB device mode, direct bit address clear or write 1 to clear
  240. /* R8_USB_INT_ST */
  241. #define USBFS_UIS_IS_NAK 0x80 // RO, indicate current USB transfer is NAK received for USB device mode
  242. #define USBFS_UIS_TOG_OK 0x40 // RO, indicate current USB transfer toggle is OK
  243. #define USBFS_UIS_TOKEN_MASK 0x30 // RO, bit mask of current token PID code received for USB device mode
  244. #define USBFS_UIS_TOKEN_OUT 0x00
  245. #define USBFS_UIS_TOKEN_IN 0x20
  246. #define USBFS_UIS_TOKEN_SETUP 0x30
  247. // bUIS_TOKEN1 & bUIS_TOKEN0: current token PID code received for USB device mode
  248. // 00: OUT token PID received
  249. // 10: IN token PID received
  250. // 11: SETUP token PID received
  251. #define USBFS_UIS_ENDP_MASK 0x0F // RO, bit mask of current transfer endpoint number for USB device mode
  252. #define USBFS_UIS_H_RES_MASK 0x0F // RO, bit mask of current transfer handshake response for USB host mode: 0000=no response, time out from device, others=handshake response PID received
  253. /* R32_USB_OTG_CR */
  254. #define USBFS_CR_SESS_VTH 0x20
  255. #define USBFS_CR_VBUS_VTH 0x10
  256. #define USBFS_CR_OTG_EN 0x08
  257. #define USBFS_CR_IDPU 0x04
  258. #define USBFS_CR_CHARGE_VBUS 0x02
  259. #define USBFS_CR_DISCHAR_VBUS 0x01
  260. /* R32_USB_OTG_SR */
  261. #define USBFS_SR_ID_DIG 0x08
  262. #define USBFS_SR_SESS_END 0x04
  263. #define USBFS_SR_SESS_VLD 0x02
  264. #define USBFS_SR_VBUS_VLD 0x01
  265. /* R8_UDEV_CTRL */
  266. #define USBFS_UD_PD_DIS 0x80 // disable USB UDP/UDM pulldown resistance: 0=enable pulldown, 1=disable
  267. #define USBFS_UD_DP_PIN 0x20 // ReadOnly: indicate current UDP pin level
  268. #define USBFS_UD_DM_PIN 0x10 // ReadOnly: indicate current UDM pin level
  269. #define USBFS_UD_LOW_SPEED 0x04 // enable USB physical port low speed: 0=full speed, 1=low speed
  270. #define USBFS_UD_GP_BIT 0x02 // general purpose bit
  271. #define USBFS_UD_PORT_EN 0x01 // enable USB physical port I/O: 0=disable, 1=enable
  272. /* R8_UEP4_1_MOD */
  273. #define USBFS_UEP1_RX_EN 0x80 // enable USB endpoint 1 receiving (OUT)
  274. #define USBFS_UEP1_TX_EN 0x40 // enable USB endpoint 1 transmittal (IN)
  275. #define USBFS_UEP1_BUF_MOD 0x10 // buffer mode of USB endpoint 1
  276. #define USBFS_UEP4_RX_EN 0x08 // enable USB endpoint 4 receiving (OUT)
  277. #define USBFS_UEP4_TX_EN 0x04 // enable USB endpoint 4 transmittal (IN)
  278. #define USBFS_UEP4_BUF_MOD 0x01
  279. /* R8_UEP2_3_MOD */
  280. #define USBFS_UEP3_RX_EN 0x80 // enable USB endpoint 3 receiving (OUT)
  281. #define USBFS_UEP3_TX_EN 0x40 // enable USB endpoint 3 transmittal (IN)
  282. #define USBFS_UEP3_BUF_MOD 0x10 // buffer mode of USB endpoint 3
  283. #define USBFS_UEP2_RX_EN 0x08 // enable USB endpoint 2 receiving (OUT)
  284. #define USBFS_UEP2_TX_EN 0x04 // enable USB endpoint 2 transmittal (IN)
  285. #define USBFS_UEP2_BUF_MOD 0x01 // buffer mode of USB endpoint 2
  286. /* R8_UEP5_6_MOD */
  287. #define USBFS_UEP6_RX_EN 0x80 // enable USB endpoint 6 receiving (OUT)
  288. #define USBFS_UEP6_TX_EN 0x40 // enable USB endpoint 6 transmittal (IN)
  289. #define USBFS_UEP6_BUF_MOD 0x10 // buffer mode of USB endpoint 6
  290. #define USBFS_UEP5_RX_EN 0x08 // enable USB endpoint 5 receiving (OUT)
  291. #define USBFS_UEP5_TX_EN 0x04 // enable USB endpoint 5 transmittal (IN)
  292. #define USBFS_UEP5_BUF_MOD 0x01 // buffer mode of USB endpoint 5
  293. /* R8_UEP7_MOD */
  294. #define USBFS_UEP7_RX_EN 0x08 // enable USB endpoint 7 receiving (OUT)
  295. #define USBFS_UEP7_TX_EN 0x04 // enable USB endpoint 7 transmittal (IN)
  296. #define USBFS_UEP7_BUF_MOD 0x01 // buffer mode of USB endpoint 7
  297. /* R8_UEPn_TX_CTRL */
  298. #define USBFS_UEP_T_AUTO_TOG 0x08 // enable automatic toggle after successful transfer completion on endpoint 1/2/3: 0=manual toggle, 1=automatic toggle
  299. #define USBFS_UEP_T_TOG 0x04 // prepared data toggle flag of USB endpoint X transmittal (IN): 0=DATA0, 1=DATA1
  300. #define USBFS_UEP_T_RES_MASK 0x03 // bit mask of handshake response type for USB endpoint X transmittal (IN)
  301. #define USBFS_UEP_T_RES_ACK 0x00
  302. #define USBFS_UEP_T_RES_NONE 0x01
  303. #define USBFS_UEP_T_RES_NAK 0x02
  304. #define USBFS_UEP_T_RES_STALL 0x03
  305. // bUEP_T_RES1 & bUEP_T_RES0: handshake response type for USB endpoint X transmittal (IN)
  306. // 00: DATA0 or DATA1 then expecting ACK (ready)
  307. // 01: DATA0 or DATA1 then expecting no response, time out from host, for non-zero endpoint isochronous transactions
  308. // 10: NAK (busy)
  309. // 11: STALL (error)
  310. // host aux setup
  311. /* R8_UEPn_RX_CTRL, n=0-7 */
  312. #define USBFS_UEP_R_AUTO_TOG 0x08 // enable automatic toggle after successful transfer completion on endpoint 1/2/3: 0=manual toggle, 1=automatic toggle
  313. #define USBFS_UEP_R_TOG 0x04 // expected data toggle flag of USB endpoint X receiving (OUT): 0=DATA0, 1=DATA1
  314. #define USBFS_UEP_R_RES_MASK 0x03 // bit mask of handshake response type for USB endpoint X receiving (OUT)
  315. #define USBFS_UEP_R_RES_ACK 0x00
  316. #define USBFS_UEP_R_RES_NONE 0x01
  317. #define USBFS_UEP_R_RES_NAK 0x02
  318. #define USBFS_UEP_R_RES_STALL 0x03
  319. // RB_UEP_R_RES1 & RB_UEP_R_RES0: handshake response type for USB endpoint X receiving (OUT)
  320. // 00: ACK (ready)
  321. // 01: no response, time out to host, for non-zero endpoint isochronous transactions
  322. // 10: NAK (busy)
  323. // 11: STALL (error)
  324. /* R8_UHOST_CTRL */
  325. #define USBFS_UH_PD_DIS 0x80 // disable USB UDP/UDM pulldown resistance: 0=enable pulldown, 1=disable
  326. #define USBFS_UH_DP_PIN 0x20 // ReadOnly: indicate current UDP pin level
  327. #define USBFS_UH_DM_PIN 0x10 // ReadOnly: indicate current UDM pin level
  328. #define USBFS_UH_LOW_SPEED 0x04 // enable USB port low speed: 0=full speed, 1=low speed
  329. #define USBFS_UH_BUS_RESET 0x02 // control USB bus reset: 0=normal, 1=force bus reset
  330. #define USBFS_UH_PORT_EN 0x01 // enable USB port: 0=disable, 1=enable port, automatic disabled if USB device detached
  331. /* R32_UH_EP_MOD */
  332. #define USBFS_UH_EP_TX_EN 0x40 // enable USB host OUT endpoint transmittal
  333. #define USBFS_UH_EP_TBUF_MOD 0x10 // buffer mode of USB host OUT endpoint
  334. // bUH_EP_TX_EN & bUH_EP_TBUF_MOD: USB host OUT endpoint buffer mode, buffer start address is UH_TX_DMA
  335. // 0 x: disable endpoint and disable buffer
  336. // 1 0: 64 bytes buffer for transmittal (OUT endpoint)
  337. // 1 1: dual 64 bytes buffer by toggle bit bUH_T_TOG selection for transmittal (OUT endpoint), total=128bytes
  338. #define USBFS_UH_EP_RX_EN 0x08 // enable USB host IN endpoint receiving
  339. #define USBFS_UH_EP_RBUF_MOD 0x01 // buffer mode of USB host IN endpoint
  340. // bUH_EP_RX_EN & bUH_EP_RBUF_MOD: USB host IN endpoint buffer mode, buffer start address is UH_RX_DMA
  341. // 0 x: disable endpoint and disable buffer
  342. // 1 0: 64 bytes buffer for receiving (IN endpoint)
  343. // 1 1: dual 64 bytes buffer by toggle bit bUH_R_TOG selection for receiving (IN endpoint), total=128bytes
  344. /* R16_UH_SETUP */
  345. #define USBFS_UH_PRE_PID_EN 0x0400 // USB host PRE PID enable for low speed device via hub
  346. #define USBFS_UH_SOF_EN 0x0004 // USB host automatic SOF enable
  347. /* R8_UH_EP_PID */
  348. #define USBFS_UH_TOKEN_MASK 0xF0 // bit mask of token PID for USB host transfer
  349. #define USBFS_UH_ENDP_MASK 0x0F // bit mask of endpoint number for USB host transfer
  350. /* R8_UH_RX_CTRL */
  351. #define USBFS_UH_R_AUTO_TOG 0x08 // enable automatic toggle after successful transfer completion: 0=manual toggle, 1=automatic toggle
  352. #define USBFS_UH_R_TOG 0x04 // expected data toggle flag of host receiving (IN): 0=DATA0, 1=DATA1
  353. #define USBFS_UH_R_RES 0x01 // prepared handshake response type for host receiving (IN): 0=ACK (ready), 1=no response, time out to device, for isochronous transactions
  354. /* R8_UH_TX_CTRL */
  355. #define USBFS_UH_T_AUTO_TOG 0x08 // enable automatic toggle after successful transfer completion: 0=manual toggle, 1=automatic toggle
  356. #define USBFS_UH_T_TOG 0x04 // prepared data toggle flag of host transmittal (SETUP/OUT): 0=DATA0, 1=DATA1
  357. #define USBFS_UH_T_RES 0x01 // expected handshake response type for host transmittal (SETUP/OUT): 0=ACK (ready), 1=no response, time out from device, for isochronous transactions
  358. /*******************************************************************************/
  359. /* Struct Definition */
  360. /* USB Setup Request */
  361. typedef struct __attribute__((packed)) _USB_SETUP_REQ
  362. {
  363. uint8_t bRequestType;
  364. uint8_t bRequest;
  365. uint16_t wValue;
  366. uint16_t wIndex;
  367. uint16_t wLength;
  368. } USB_SETUP_REQ, *PUSB_SETUP_REQ;
  369. /* USB Device Descriptor */
  370. typedef struct __attribute__((packed)) _USB_DEVICE_DESCR
  371. {
  372. uint8_t bLength;
  373. uint8_t bDescriptorType;
  374. uint16_t bcdUSB;
  375. uint8_t bDeviceClass;
  376. uint8_t bDeviceSubClass;
  377. uint8_t bDeviceProtocol;
  378. uint8_t bMaxPacketSize0;
  379. uint16_t idVendor;
  380. uint16_t idProduct;
  381. uint16_t bcdDevice;
  382. uint8_t iManufacturer;
  383. uint8_t iProduct;
  384. uint8_t iSerialNumber;
  385. uint8_t bNumConfigurations;
  386. } USB_DEV_DESCR, *PUSB_DEV_DESCR;
  387. /* USB Configuration Descriptor */
  388. typedef struct __attribute__((packed)) _USB_CONFIG_DESCR
  389. {
  390. uint8_t bLength;
  391. uint8_t bDescriptorType;
  392. uint16_t wTotalLength;
  393. uint8_t bNumInterfaces;
  394. uint8_t bConfigurationValue;
  395. uint8_t iConfiguration;
  396. uint8_t bmAttributes;
  397. uint8_t MaxPower;
  398. } USB_CFG_DESCR, *PUSB_CFG_DESCR;
  399. /* USB Interface Descriptor */
  400. typedef struct __attribute__((packed)) _USB_INTERF_DESCR
  401. {
  402. uint8_t bLength;
  403. uint8_t bDescriptorType;
  404. uint8_t bInterfaceNumber;
  405. uint8_t bAlternateSetting;
  406. uint8_t bNumEndpoints;
  407. uint8_t bInterfaceClass;
  408. uint8_t bInterfaceSubClass;
  409. uint8_t bInterfaceProtocol;
  410. uint8_t iInterface;
  411. } USB_ITF_DESCR, *PUSB_ITF_DESCR;
  412. /* USB Endpoint Descriptor */
  413. typedef struct __attribute__((packed)) _USB_ENDPOINT_DESCR
  414. {
  415. uint8_t bLength;
  416. uint8_t bDescriptorType;
  417. uint8_t bEndpointAddress;
  418. uint8_t bmAttributes;
  419. uint8_t wMaxPacketSizeL;
  420. uint8_t wMaxPacketSizeH;
  421. uint8_t bInterval;
  422. } USB_ENDP_DESCR, *PUSB_ENDP_DESCR;
  423. /* USB Configuration Descriptor Set */
  424. typedef struct __attribute__((packed)) _USB_CONFIG_DESCR_LONG
  425. {
  426. USB_CFG_DESCR cfg_descr;
  427. USB_ITF_DESCR itf_descr;
  428. USB_ENDP_DESCR endp_descr[ 1 ];
  429. } USB_CFG_DESCR_LONG, *PUSB_CFG_DESCR_LONG;
  430. /* USB HUB Descriptor */
  431. typedef struct __attribute__((packed)) _USB_HUB_DESCR
  432. {
  433. uint8_t bDescLength;
  434. uint8_t bDescriptorType;
  435. uint8_t bNbrPorts;
  436. uint8_t wHubCharacteristicsL;
  437. uint8_t wHubCharacteristicsH;
  438. uint8_t bPwrOn2PwrGood;
  439. uint8_t bHubContrCurrent;
  440. uint8_t DeviceRemovable;
  441. uint8_t PortPwrCtrlMask;
  442. } USB_HUB_DESCR, *PUSB_HUB_DESCR;
  443. /* USB HID Descriptor */
  444. typedef struct __attribute__((packed)) _USB_HID_DESCR
  445. {
  446. uint8_t bLength;
  447. uint8_t bDescriptorType;
  448. uint16_t bcdHID;
  449. uint8_t bCountryCode;
  450. uint8_t bNumDescriptors;
  451. uint8_t bDescriptorTypeX;
  452. uint8_t wDescriptorLengthL;
  453. uint8_t wDescriptorLengthH;
  454. } USB_HID_DESCR, *PUSB_HID_DESCR;
  455. /* USB UDisk */
  456. typedef struct __attribute__((packed)) _UDISK_BOC_CBW
  457. {
  458. uint32_t mCBW_Sig;
  459. uint32_t mCBW_Tag;
  460. uint32_t mCBW_DataLen;
  461. uint8_t mCBW_Flag;
  462. uint8_t mCBW_LUN;
  463. uint8_t mCBW_CB_Len;
  464. uint8_t mCBW_CB_Buf[ 16 ];
  465. } UDISK_BOC_CBW, *PXUDISK_BOC_CBW;
  466. /* USB UDisk */
  467. typedef struct __attribute__((packed)) _UDISK_BOC_CSW
  468. {
  469. uint32_t mCBW_Sig;
  470. uint32_t mCBW_Tag;
  471. uint32_t mCSW_Residue;
  472. uint8_t mCSW_Status;
  473. } UDISK_BOC_CSW, *PXUDISK_BOC_CSW;
  474. #ifdef __cplusplus
  475. }
  476. #endif
  477. #endif /*_CH32V20X_USB_H */