hpm_usb_drv.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*
  2. * Copyright (c) 2021 hpmicro
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. *
  6. */
  7. #ifndef HPM_USB_DRV_H
  8. #define HPM_USB_DRV_H
  9. /*---------------------------------------------------------------------*
  10. * Includes
  11. *---------------------------------------------------------------------*/
  12. #include "hpm_common.h"
  13. #include "hpm_usb_regs.h"
  14. #include "hpm_soc_feature.h"
  15. /**
  16. * @brief USB driver APIs
  17. * @defgroup usb_interface USB driver APIs
  18. * @ingroup communication_interfaces
  19. * @{
  20. */
  21. /*---------------------------------------------------------------------*
  22. * Macro Constant Declarations
  23. *---------------------------------------------------------------------*/
  24. #define USB_PHY_INIT_DELAY_COUNT (16U) /**< a delay count for USB phy initialization */
  25. #define USB_HOST_FRAMELIST_SIZE (8U) /**< a frame list size in USB host mode */
  26. /*---------------------------------------------------------------------*
  27. * Macro Enum Declarations
  28. *---------------------------------------------------------------------*/
  29. /**
  30. * @brief USB transfer direction types
  31. */
  32. typedef enum {
  33. usb_dir_out = 0,
  34. usb_dir_in = 1,
  35. usb_dir_in_mask = 0x80
  36. } usb_dir_t;
  37. /**
  38. * @brief USB transfer types
  39. */
  40. typedef enum {
  41. usb_xfer_control = 0,
  42. usb_xfer_isochronous,
  43. usb_xfer_bulk,
  44. usb_xfer_interrupt
  45. } usb_xfer_type_t;
  46. /**
  47. * @brief USB controller work modes
  48. */
  49. typedef enum {
  50. usb_ctrl_mode_otg = 0,
  51. usb_ctrl_mode_device = 2,
  52. usb_ctrl_mode_host = 3
  53. } usb_controller_mode_t;
  54. /**
  55. * @brief USB line state
  56. */
  57. typedef enum {
  58. usb_line_state0 = 0,
  59. usb_line_state1 = 1,
  60. usb_line_state2 = 2
  61. } usb_line_state_t;
  62. /**
  63. * @brief USB transceiver
  64. */
  65. typedef enum {
  66. usb_tran_parallel = 0,
  67. usb_tran_serial = 1
  68. } usb_transceiver_t;
  69. /*---------------------------------------------------------------------*
  70. * Structure Declarations
  71. *---------------------------------------------------------------------*/
  72. /**
  73. * @brief Control request structure
  74. */
  75. typedef struct __attribute__ ((packed)){
  76. union {
  77. struct __attribute__ ((packed)) {
  78. uint8_t recipient : 5;
  79. uint8_t type : 2;
  80. uint8_t direction : 1;
  81. } bmRequestType_bit;
  82. uint8_t bmRequestType;
  83. };
  84. uint8_t bRequest;
  85. uint16_t wValue;
  86. uint16_t wIndex;
  87. uint16_t wLength;
  88. } usb_control_request_t;
  89. /**
  90. * @brief Endpoint config structure
  91. */
  92. typedef struct {
  93. uint8_t xfer;
  94. uint8_t ep_addr;
  95. uint16_t max_packet_size;
  96. } usb_endpoint_config_t;
  97. #if defined __cplusplus
  98. extern "C" {
  99. #endif /* __cplusplus */
  100. /*---------------------------------------------------------------------*
  101. * Common API
  102. *---------------------------------------------------------------------*/
  103. /**
  104. * @brief Get the mask of all enabled interrupts
  105. *
  106. * @param[in] ptr A USB peripheral base address.
  107. * @retval Mask of all enabled interrupts.
  108. */
  109. static inline uint32_t usb_get_interrupts(USB_Type *ptr)
  110. {
  111. return ptr->USBINTR;
  112. }
  113. /**
  114. * @brief Enable interrupts
  115. *
  116. * @param[in] ptr A USB peripheral base address
  117. * @param[in] mask Mask value for interrupt events
  118. */
  119. static inline void usb_enable_interrupts(USB_Type *ptr, uint32_t mask)
  120. {
  121. ptr->USBINTR |= mask;
  122. }
  123. /**
  124. * @brief Get all USB status flags
  125. *
  126. * @param[in] ptr A USB peripheral base address
  127. * @retval The USB interrupt status flags
  128. */
  129. static inline uint32_t usb_get_status_flags(USB_Type *ptr)
  130. {
  131. return ptr->USBSTS;
  132. }
  133. /**
  134. * @brief Clear status flags
  135. *
  136. * Only the specified flags can be cleared by writing USBSTS register.
  137. *
  138. * @param[in] ptr A USB peripheral base address
  139. * @param[in] mask Mask value for flags to be cleared.
  140. */
  141. static inline void usb_clear_status_flags(USB_Type *ptr, uint32_t mask)
  142. {
  143. ptr->USBSTS |= mask;
  144. }
  145. /**
  146. * @brief Get USB suspend status
  147. *
  148. * @param[in] ptr A USB peripheral base address
  149. * @retval The USB controller suspend status
  150. */
  151. static inline uint8_t usb_get_suspend_status(USB_Type *ptr)
  152. {
  153. return USB_PORTSC1_SUSP_GET(ptr->PORTSC1);
  154. }
  155. /**
  156. * @brief Get USB reset status
  157. *
  158. * @param[in] ptr A USB peripheral base address
  159. * @retval The USB controller reset status
  160. */
  161. static inline bool usb_get_port_reset_status(USB_Type *ptr)
  162. {
  163. return USB_PORTSC1_PR_GET(ptr->PORTSC1);
  164. }
  165. /**
  166. * @brief Get USB current connect status
  167. *
  168. * @param[in] ptr A USB peripheral base address
  169. * @retval The USB controller reset status
  170. */
  171. static inline bool usb_get_port_ccs(USB_Type *ptr)
  172. {
  173. return USB_PORTSC1_CCS_GET(ptr->PORTSC1);
  174. }
  175. /**
  176. * @brief Get USB port speed status
  177. *
  178. * @param[in] ptr A USB peripheral base address
  179. * @retval The USB controller port speed status
  180. */
  181. static inline uint8_t usb_get_port_speed(USB_Type *ptr)
  182. {
  183. return USB_PORTSC1_PSPD_GET(ptr->PORTSC1);
  184. }
  185. /*---------------------------------------------------------------------*
  186. * Device API
  187. *---------------------------------------------------------------------*/
  188. /**
  189. * @brief USB device bus reset
  190. *
  191. * @param[in] ptr A USB peripheral base address
  192. * @param[in] ep0_max_packet_size The maximum packet size of endpoint 0
  193. */
  194. void usb_dcd_bus_reset(USB_Type *ptr, uint16_t ep0_max_packet_size);
  195. /**
  196. * @brief Initialize controller to device mode
  197. *
  198. * @param[in] ptr A USB peripheral base address
  199. */
  200. void usb_dcd_init(USB_Type *ptr);
  201. /**
  202. * @brief Deinitialize controller to device
  203. *
  204. * @param[in] ptr A USB peripheral base address
  205. */
  206. void usb_dcd_deinit(USB_Type *ptr);
  207. /**
  208. * @brief Wakeup from host
  209. *
  210. * @param[in] ptr A USB peripheral base address
  211. */
  212. void usb_dcd_remote_wakeup(USB_Type *ptr);
  213. /**
  214. * @brief Open an endpoint
  215. *
  216. * @param[in] ptr A USB peripheral base address
  217. * @param[in] config A pointer to the specified endpoint config struct
  218. */
  219. void usb_dcd_edpt_open(USB_Type *ptr, usb_endpoint_config_t *config);
  220. /**
  221. * @brief Submit a transfer
  222. *
  223. * @param[in] ptr A USB peripheral base address
  224. * @param[in] ep_idx An index of the specified endpoint
  225. */
  226. void usb_dcd_edpt_xfer(USB_Type *ptr, uint8_t ep_idx);
  227. /**
  228. * @brief Stall endpoint
  229. *
  230. * @param[in] ptr A USB peripheral base address
  231. * @param[in] ep_addr An address of the specified endpoint
  232. */
  233. void usb_dcd_edpt_stall(USB_Type *ptr, uint8_t ep_addr);
  234. /**
  235. * @brief Clear stall
  236. *
  237. * @param[in] ptr A USB peripheral base address
  238. * @param[in] ep_addr An address of the specified endpoint
  239. */
  240. void usb_dcd_edpt_clear_stall(USB_Type *ptr, uint8_t ep_addr);
  241. /**
  242. * @brief Close a specified endpoint
  243. *
  244. * @param[in] ptr A USB peripheral base address
  245. * @param[in] ep_addr An address of the specified endpoint
  246. */
  247. void usb_dcd_edpt_close(USB_Type *ptr, uint8_t ep_addr);
  248. /**
  249. * @brief Connect by enabling internal pull-up resistor on D+/D-
  250. *
  251. * @param[in] ptr A USB peripheral base address
  252. */
  253. void usb_dcd_connect(USB_Type *ptr);
  254. /**
  255. * @brief Disconnect by disabling internal pull-up resistor on D+/D-
  256. *
  257. * @param[in] ptr A USB peripheral base address
  258. */
  259. void usb_dcd_disconnect(USB_Type *ptr);
  260. /**
  261. * @brief Get setup status of endpoint
  262. *
  263. * @param[in] ptr A USB peripheral base address
  264. * @retval The status of setup endpoint
  265. */
  266. static inline uint32_t usb_dcd_get_edpt_setup_status(USB_Type *ptr)
  267. {
  268. return ptr->ENDPTSETUPSTAT;
  269. }
  270. /**
  271. * @brief Clear the setup status of all specified endpoints
  272. *
  273. * @param[in] ptr A USB peripheral base address
  274. * @param[in] mask A mask of all specified endpoints
  275. */
  276. static inline void usb_dcd_clear_edpt_setup_status(USB_Type *ptr, uint32_t mask)
  277. {
  278. ptr->ENDPTSETUPSTAT |= mask;
  279. }
  280. /**
  281. * @brief Set address
  282. *
  283. * @param[in] ptr A USB peripheral base address
  284. * @param[in] dev_addr An assigned endpoint address from USB host
  285. */
  286. static inline void usb_dcd_set_address(USB_Type *ptr, uint8_t dev_addr)
  287. {
  288. ptr->DEVICEADDR = USB_DEVICEADDR_USBADR_SET(dev_addr) | USB_DEVICEADDR_USBADRA_MASK;
  289. }
  290. /**
  291. * @brief Set endpoint list address
  292. *
  293. * @param[in] ptr A USB peripheral base address
  294. * @param[in] addr A start address of the endpoint qtd list
  295. */
  296. static inline void usb_dcd_set_edpt_list_addr(USB_Type *ptr, uint32_t addr)
  297. {
  298. ptr->ENDPTLISTADDR = addr & USB_ENDPTLISTADDR_EPBASE_MASK;
  299. }
  300. /**
  301. * @brief Get device address
  302. *
  303. * @param[in] ptr A USB peripheral base address
  304. * @retval The endpoint address
  305. */
  306. static inline uint8_t usb_dcd_get_device_addr(USB_Type *ptr)
  307. {
  308. return USB_DEVICEADDR_USBADR_GET(ptr->DEVICEADDR);
  309. }
  310. /**
  311. * @brief Get complete status of endpoint
  312. *
  313. * @param[in] ptr A USB peripheral base address
  314. * @retval The complete status od endpoint
  315. */
  316. static inline uint32_t usb_dcd_get_edpt_complete_status(USB_Type *ptr)
  317. {
  318. return ptr->ENDPTCOMPLETE;
  319. }
  320. /**
  321. * @brief Clear complete status of endpoint
  322. *
  323. * @param[in] ptr A USB peripheral base address
  324. * @param[in] mask A mask of the specified endpoints
  325. */
  326. static inline void usb_dcd_clear_edpt_complete_status(USB_Type *ptr, uint32_t mask)
  327. {
  328. ptr->ENDPTCOMPLETE |= mask;
  329. }
  330. /*---------------------------------------------------------------------*
  331. * Host API
  332. *---------------------------------------------------------------------*/
  333. /**
  334. * @brief Initialize controller to host mode
  335. *
  336. * @param[in] ptr A USB peripheral base address
  337. * @param[in] int_mask A mask of all required interrupts
  338. * @param[in] framelist_size A size of the frame list
  339. */
  340. bool usb_hcd_init(USB_Type *ptr, uint32_t int_mask, uint16_t framelist_size);
  341. /**
  342. * @brief Initialize controller to host modeHost Reset port
  343. *
  344. * @param[in] ptr A USB peripheral base address
  345. */
  346. void usb_hcd_port_reset(USB_Type *ptr);
  347. /**
  348. * @brief Initialize controller to host modeHost set command register
  349. *
  350. * @param[in] ptr A USB peripheral base address
  351. * @param[in] mask A mask of all required commands
  352. */
  353. static inline void usb_hcd_set_command(USB_Type *ptr ,uint32_t mask)
  354. {
  355. ptr->USBCMD |= mask;
  356. }
  357. /**
  358. * @brief Get frame index
  359. *
  360. * @param[in] ptr A USB peripheral base address
  361. * @retval A index of the current frame list
  362. */
  363. static inline uint32_t usb_hcd_get_frame_index(USB_Type *ptr)
  364. {
  365. return ptr->FRINDEX;
  366. }
  367. /**
  368. * @brief Get port connect status change
  369. *
  370. * @param[in] ptr A USB peripheral base address
  371. * @retval A connect status change
  372. */
  373. static inline bool usb_hcd_get_port_csc(USB_Type *ptr)
  374. {
  375. return USB_PORTSC1_CSC_GET(ptr->PORTSC1);
  376. }
  377. /**
  378. * @brief Get port connect status changeSet async list address
  379. *
  380. * @param[in] ptr A USB peripheral base address
  381. * @param[in] addr An the start address of the async endpoint list
  382. */
  383. static inline void usb_hcd_set_async_list_addr(USB_Type *ptr, uint32_t addr)
  384. {
  385. ptr->ASYNCLISTADDR = addr & USB_ASYNCLISTADDR_ASYBASE_MASK;
  386. }
  387. /**
  388. * @brief Set periodic list address
  389. *
  390. * @param[in] ptr A USB peripheral base address
  391. * @param[in] addr An start address of the periodic endpoint list
  392. */
  393. static inline void usb_hcd_set_periodic_list_addr(USB_Type *ptr, uint32_t addr)
  394. {
  395. ptr->PERIODICLISTBASE = addr & USB_PERIODICLISTBASE_BASEADR_MASK;
  396. }
  397. #if defined __cplusplus
  398. }
  399. #endif /* __cplusplus */
  400. /** @} */
  401. #endif /* HPM_USB_DRV_H */