hpm_usb_drv.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  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. */
  13. #include "hpm_common.h"
  14. #include "hpm_usb_regs.h"
  15. #include "hpm_soc_feature.h"
  16. /**
  17. * @brief USB driver APIs
  18. * @defgroup usb_interface USB driver APIs
  19. * @ingroup communication_interfaces
  20. * @{
  21. */
  22. /*---------------------------------------------------------------------
  23. * Macro Constant Declarations
  24. *---------------------------------------------------------------------
  25. */
  26. #define USB_PHY_INIT_DELAY_COUNT (16U) /**< a delay count for USB phy initialization */
  27. #define USB_HOST_FRAMELIST_SIZE (8U) /**< a frame list size in USB host mode */
  28. /*---------------------------------------------------------------------
  29. * Macro Enum Declarations
  30. *---------------------------------------------------------------------
  31. */
  32. /**
  33. * @brief USB transfer direction types
  34. */
  35. typedef enum {
  36. usb_dir_out = 0,
  37. usb_dir_in = 1,
  38. usb_dir_in_mask = 0x80
  39. } usb_dir_t; /**< usb_dir_t */
  40. /**
  41. * @brief USB transfer types
  42. */
  43. typedef enum {
  44. usb_xfer_control = 0,
  45. usb_xfer_isochronous,
  46. usb_xfer_bulk,
  47. usb_xfer_interrupt
  48. } usb_xfer_type_t; /**< usb_xfer_type_t */
  49. /**
  50. * @brief USB controller work modes
  51. */
  52. typedef enum {
  53. usb_ctrl_mode_otg = 0,
  54. usb_ctrl_mode_device = 2,
  55. usb_ctrl_mode_host = 3
  56. } usb_controller_mode_t; /**< usb_controller_mode_t */
  57. /**
  58. * @brief USB line state
  59. */
  60. typedef enum {
  61. usb_line_state0 = 0,
  62. usb_line_state1 = 1,
  63. usb_line_state2 = 2
  64. } usb_line_state_t; /**< usb_line_state_t */
  65. /**
  66. * @brief USB transceiver
  67. */
  68. typedef enum {
  69. usb_tran_parallel = 0,
  70. usb_tran_serial = 1
  71. } usb_transceiver_t; /**< usb_transceiver_t */
  72. /**
  73. * @brief USB test modes
  74. */
  75. typedef enum {
  76. usb_test_mode_disable = 0,
  77. usb_test_j_state,
  78. usb_test_k_state,
  79. usb_test_se0_nak,
  80. usb_test_packet,
  81. usb_test_force_hs,
  82. usb_test_force_fs,
  83. usb_test_force_ls,
  84. } usb_test_mode_t; /**< usb_test_mode_t */
  85. /**
  86. * @brief USB vbus wakeup source
  87. */
  88. typedef enum {
  89. usb_vbus_wakeup_vbus_valid = 0,
  90. usb_vbus_wakeup_session_valid,
  91. } usb_vbus_wakeup_source_t; /**< usb_vbus_wakeup_source_t */
  92. /*---------------------------------------------------------------------
  93. * Structure Declarations
  94. *---------------------------------------------------------------------
  95. */
  96. /**
  97. * @brief Control request structure
  98. */
  99. typedef struct __attribute__ ((packed)) {
  100. union {
  101. struct __attribute__ ((packed)) {
  102. uint8_t recipient : 5;
  103. uint8_t type : 2;
  104. uint8_t direction : 1;
  105. } bmRequestType_bit;
  106. uint8_t bmRequestType;
  107. };
  108. uint8_t bRequest;
  109. uint16_t wValue;
  110. uint16_t wIndex;
  111. uint16_t wLength;
  112. } usb_control_request_t;
  113. /**
  114. * @brief Endpoint config structure
  115. */
  116. typedef struct {
  117. uint8_t xfer;
  118. uint8_t ep_addr;
  119. uint16_t max_packet_size;
  120. } usb_endpoint_config_t;
  121. #if defined __cplusplus
  122. extern "C" {
  123. #endif /* __cplusplus */
  124. /*---------------------------------------------------------------------
  125. * Common API
  126. *---------------------------------------------------------------------
  127. */
  128. /**
  129. * @brief Get the mask of all enabled interrupts
  130. *
  131. * @param[in] ptr A USB peripheral base address.
  132. * @retval Mask of all enabled interrupts.
  133. */
  134. static inline uint32_t usb_get_interrupts(USB_Type *ptr)
  135. {
  136. return ptr->USBINTR;
  137. }
  138. /**
  139. * @brief Enable interrupts
  140. *
  141. * @param[in] ptr A USB peripheral base address
  142. * @param[in] mask Mask value for interrupt events
  143. */
  144. static inline void usb_enable_interrupts(USB_Type *ptr, uint32_t mask)
  145. {
  146. ptr->USBINTR |= mask;
  147. }
  148. /**
  149. * @brief Get all USB status flags
  150. *
  151. * @param[in] ptr A USB peripheral base address
  152. * @retval The USB interrupt status flags
  153. */
  154. static inline uint32_t usb_get_status_flags(USB_Type *ptr)
  155. {
  156. return ptr->USBSTS;
  157. }
  158. /**
  159. * @brief Clear status flags
  160. *
  161. * Only the specified flags can be cleared by writing USBSTS register.
  162. *
  163. * @param[in] ptr A USB peripheral base address
  164. * @param[in] mask Mask value for flags to be cleared.
  165. */
  166. static inline void usb_clear_status_flags(USB_Type *ptr, uint32_t mask)
  167. {
  168. ptr->USBSTS = mask;
  169. }
  170. /**
  171. * @brief Enable otg vbus wakeup
  172. *
  173. * @param[in] ptr A USB peripheral base address
  174. */
  175. static inline void usb_otg_enable_vbus_wakeup(USB_Type *ptr)
  176. {
  177. ptr->OTG_CTRL0 |= USB_OTG_CTRL0_OTG_VBUS_WAKEUP_EN_MASK;
  178. }
  179. /**
  180. * @brief Disbable otg vbus wakeup
  181. *
  182. * @param[in] ptr A USB peripheral base address
  183. */
  184. static inline void usb_otg_disable_vbus_wakeup(USB_Type *ptr)
  185. {
  186. ptr->OTG_CTRL0 &= ~USB_OTG_CTRL0_OTG_VBUS_WAKEUP_EN_MASK;
  187. }
  188. /**
  189. * @brief Set otg vbus wakeup source
  190. *
  191. * @param[in] ptr A USB peripheral base address
  192. * @param[in] src wakeup source, @ref usb_vbus_wakeup_source_t
  193. */
  194. static inline void usb_otg_set_vbus_wakeup_source(USB_Type *ptr, usb_vbus_wakeup_source_t src)
  195. {
  196. ptr->OTG_CTRL0 = (ptr->OTG_CTRL0 & ~USB_OTG_CTRL0_OTG_VBUS_SOURCE_SEL_MASK) | USB_OTG_CTRL0_OTG_VBUS_SOURCE_SEL_SET(src);
  197. }
  198. /**
  199. * @brief Enable otg wakeup interrupt
  200. *
  201. * @param[in] ptr A USB peripheral base address
  202. */
  203. static inline void usb_otg_enable_wakeup_int(USB_Type *ptr)
  204. {
  205. ptr->OTG_CTRL0 |= USB_OTG_CTRL0_OTG_WAKEUP_INT_ENABLE_MASK;
  206. }
  207. /**
  208. * @brief Disable otg wakeup interrupt
  209. *
  210. * @param[in] ptr A USB peripheral base address
  211. */
  212. static inline void usb_otg_disable_wakeup_int(USB_Type *ptr)
  213. {
  214. ptr->OTG_CTRL0 &= ~USB_OTG_CTRL0_OTG_WAKEUP_INT_ENABLE_MASK;
  215. }
  216. /**
  217. * @brief Get otg wakeup status flags
  218. *
  219. * @param[in] ptr A USB peripheral base address
  220. * @retval The USB otg wakeup interrupt status flag
  221. */
  222. static inline bool usb_get_otg_wakeup_int_flag(USB_Type *ptr)
  223. {
  224. return (USB_TOP_STATUS_WAKEUP_INT_STATUS_GET(ptr->TOP_STATUS) != 0) ? true : false;
  225. }
  226. /**
  227. * @brief Get USB suspend status
  228. *
  229. * @param[in] ptr A USB peripheral base address
  230. * @retval The USB controller suspend status
  231. */
  232. static inline uint8_t usb_get_suspend_status(USB_Type *ptr)
  233. {
  234. return USB_PORTSC1_SUSP_GET(ptr->PORTSC1);
  235. }
  236. /**
  237. * @brief Get USB reset status
  238. *
  239. * @param[in] ptr A USB peripheral base address
  240. * @retval The USB controller reset status
  241. */
  242. static inline bool usb_get_port_reset_status(USB_Type *ptr)
  243. {
  244. return USB_PORTSC1_PR_GET(ptr->PORTSC1);
  245. }
  246. /**
  247. * @brief Get USB current connect status
  248. *
  249. * @param[in] ptr A USB peripheral base address
  250. * @retval The USB controller reset status
  251. */
  252. static inline bool usb_get_port_ccs(USB_Type *ptr)
  253. {
  254. return USB_PORTSC1_CCS_GET(ptr->PORTSC1);
  255. }
  256. /**
  257. * @brief Get USB port speed status
  258. *
  259. * @param[in] ptr A USB peripheral base address
  260. * @retval The USB controller port speed status
  261. */
  262. static inline uint8_t usb_get_port_speed(USB_Type *ptr)
  263. {
  264. return USB_PORTSC1_PSPD_GET(ptr->PORTSC1);
  265. }
  266. /**
  267. * @brief Set port test control mode
  268. *
  269. * @param[in] ptr A USB peripheral base address
  270. * @param[in] test_mode usb test mode, @ref usb_test_mode_t
  271. */
  272. static inline void usb_set_port_test_mode(USB_Type *ptr, usb_test_mode_t test_mode)
  273. {
  274. ptr->PORTSC1 = (ptr->PORTSC1 & ~USB_PORTSC1_PTC_MASK) | USB_PORTSC1_PTC_SET(test_mode);
  275. }
  276. /**
  277. * @brief USB set port suspend
  278. *
  279. * @param[in] ptr A USB peripheral base address
  280. * @param[in] suspend true - suspend, false - not suspend
  281. */
  282. static inline void usb_set_port_suspend(USB_Type *ptr, bool suspend)
  283. {
  284. ptr->PORTSC1 = (ptr->PORTSC1 & ~USB_PORTSC1_SUSP_MASK) | USB_PORTSC1_SUSP_SET(suspend);
  285. }
  286. /**
  287. * @brief USB force port resume
  288. *
  289. * @param[in] ptr A USB peripheral base address
  290. */
  291. static inline void usb_force_port_resume(USB_Type *ptr)
  292. {
  293. ptr->PORTSC1 |= USB_PORTSC1_FPR_MASK;
  294. }
  295. /**
  296. * @brief USB phy enter low power suspend
  297. *
  298. * @param[in] ptr A USB peripheral base address
  299. */
  300. static inline void usb_phy_enter_low_power_suspend(USB_Type *ptr)
  301. {
  302. ptr->PORTSC1 |= USB_PORTSC1_PHCD_MASK;
  303. }
  304. /**
  305. * @brief USB phy exit low power suspend
  306. *
  307. * @param[in] ptr A USB peripheral base address
  308. */
  309. static inline void usb_phy_exit_low_power_suspend(USB_Type *ptr)
  310. {
  311. ptr->PORTSC1 &= ~USB_PORTSC1_PHCD_MASK;
  312. /* otg utmi clock detection */
  313. ptr->PHY_STATUS |= USB_PHY_STATUS_UTMI_CLK_VALID_MASK; /* write 1 to clear valid status */
  314. while (USB_PHY_STATUS_UTMI_CLK_VALID_GET(ptr->PHY_STATUS) == 0) { /* get utmi clock status */
  315. ;
  316. }
  317. }
  318. /**
  319. * @brief Get phy session valid flag
  320. *
  321. * @param[in] ptr A USB peripheral base address
  322. * @retval The phy session valid flag
  323. */
  324. static inline bool usb_phy_get_session_valid_flag(USB_Type *ptr)
  325. {
  326. return (USB_PHY_STATUS_UTMI_SESS_VALID_GET(ptr->PHY_STATUS) != 0) ? true : false;
  327. }
  328. /**
  329. * @brief enable otgsc session valid change interrupt
  330. *
  331. * @param[in] ptr A USB peripheral base address
  332. */
  333. static inline void usb_otgsc_enable_session_valid_chg_int(USB_Type *ptr)
  334. {
  335. ptr->OTGSC |= USB_OTGSC_ASVIE_MASK;
  336. }
  337. /**
  338. * @brief disable otgsc session valid change interrupt
  339. *
  340. * @param[in] ptr A USB peripheral base address
  341. */
  342. static inline void usb_otgsc_disable_session_valid_chg_int(USB_Type *ptr)
  343. {
  344. ptr->OTGSC &= ~USB_OTGSC_ASVIE_MASK;
  345. }
  346. /**
  347. * @brief get otgsc session valid change flag
  348. *
  349. * @param[in] ptr A USB peripheral base address
  350. * @retval The otgsc session valid flag
  351. */
  352. static inline bool usb_otgsc_get_session_valid_chg_flag(USB_Type *ptr)
  353. {
  354. return (USB_OTGSC_ASVIS_SET(ptr->OTGSC) != 0) ? true : false;
  355. }
  356. /**
  357. * @brief clear otgsc session valid change flag
  358. *
  359. * @param[in] ptr A USB peripheral base address
  360. */
  361. static inline void usb_otgsc_clear_session_valid_chg_flag(USB_Type *ptr)
  362. {
  363. ptr->OTGSC |= USB_OTGSC_ASVIS_MASK;
  364. }
  365. /**
  366. * @brief Get otgsc session valid flag
  367. *
  368. * @param[in] ptr A USB peripheral base address
  369. * @retval The otgsc session valid flag
  370. */
  371. static inline bool usb_otgsc_get_session_valid_flag(USB_Type *ptr)
  372. {
  373. return (USB_OTGSC_ASV_GET(ptr->OTGSC) != 0) ? true : false;
  374. }
  375. /**
  376. * @brief Initialize USB phy
  377. *
  378. * @param[in] ptr A USB peripheral base address
  379. */
  380. void usb_phy_init(USB_Type *ptr);
  381. /**
  382. * @brief USB phy get line status
  383. *
  384. * @param[in] ptr A USB peripheral base address
  385. */
  386. static inline uint8_t usb_phy_get_line_state(USB_Type *ptr)
  387. {
  388. return USB_PHY_STATUS_LINE_STATE_GET(ptr->PHY_STATUS);
  389. }
  390. /**
  391. * @brief USB phy using internal vbus
  392. *
  393. * @param[in] ptr A USB peripheral base address
  394. */
  395. static inline void usb_phy_using_internal_vbus(USB_Type *ptr)
  396. {
  397. ptr->PHY_CTRL0 |= (USB_PHY_CTRL0_VBUS_VALID_OVERRIDE_MASK | USB_PHY_CTRL0_SESS_VALID_OVERRIDE_MASK)
  398. | (USB_PHY_CTRL0_VBUS_VALID_OVERRIDE_EN_MASK | USB_PHY_CTRL0_SESS_VALID_OVERRIDE_EN_MASK);
  399. }
  400. /**
  401. * @brief USB phy using external vbus
  402. *
  403. * @param[in] ptr A USB peripheral base address
  404. */
  405. static inline void usb_phy_using_external_vbus(USB_Type *ptr)
  406. {
  407. ptr->PHY_CTRL0 &= ~((USB_PHY_CTRL0_VBUS_VALID_OVERRIDE_MASK | USB_PHY_CTRL0_SESS_VALID_OVERRIDE_MASK)
  408. | (USB_PHY_CTRL0_VBUS_VALID_OVERRIDE_EN_MASK | USB_PHY_CTRL0_SESS_VALID_OVERRIDE_EN_MASK));
  409. }
  410. /**
  411. * @brief USB phy disconnect dp/dm pins pulldown resistance
  412. *
  413. * @param[in] ptr A USB peripheral base address
  414. */
  415. static inline void usb_phy_disable_dp_dm_pulldown(USB_Type *ptr)
  416. {
  417. ptr->PHY_CTRL0 |= 0x001000E0u;
  418. }
  419. /**
  420. * @brief USB phy connect dp/dm pins pulldown resistance
  421. *
  422. * @param[in] ptr A USB peripheral base address
  423. */
  424. static inline void usb_phy_enable_dp_dm_pulldown(USB_Type *ptr)
  425. {
  426. ptr->PHY_CTRL0 &= ~0x001000E0u;
  427. }
  428. /**
  429. * @brief Set phyctrl1 not utmi suspend
  430. *
  431. * @param[in] ptr A USB peripheral base address
  432. */
  433. static inline void usb_phyctrl1_set_not_utmi_suspend(USB_Type *ptr)
  434. {
  435. ptr->PHY_CTRL1 |= USB_PHY_CTRL1_UTMI_OTG_SUSPENDM_MASK;
  436. }
  437. /*---------------------------------------------------------------------
  438. * Device API
  439. *---------------------------------------------------------------------
  440. */
  441. /**
  442. * @brief USB device bus reset
  443. *
  444. * @param[in] ptr A USB peripheral base address
  445. * @param[in] ep0_max_packet_size The maximum packet size of endpoint 0
  446. */
  447. void usb_dcd_bus_reset(USB_Type *ptr, uint16_t ep0_max_packet_size);
  448. /**
  449. * @brief Initialize controller to device mode
  450. *
  451. * @param[in] ptr A USB peripheral base address
  452. */
  453. void usb_dcd_init(USB_Type *ptr);
  454. /**
  455. * @brief Deinitialize controller to device
  456. *
  457. * @param[in] ptr A USB peripheral base address
  458. */
  459. void usb_dcd_deinit(USB_Type *ptr);
  460. /**
  461. * @brief Wakeup from host
  462. *
  463. * @param[in] ptr A USB peripheral base address
  464. */
  465. void usb_dcd_remote_wakeup(USB_Type *ptr);
  466. /**
  467. * @brief Open an endpoint
  468. *
  469. * @param[in] ptr A USB peripheral base address
  470. * @param[in] config A pointer to the specified endpoint config struct
  471. */
  472. void usb_dcd_edpt_open(USB_Type *ptr, usb_endpoint_config_t *config);
  473. /**
  474. * @brief get a specified endpoint type
  475. *
  476. * @param[in] ptr A USB peripheral base address
  477. * @param[in] ep_addr Endpoint address
  478. */
  479. uint8_t usb_dcd_edpt_get_type(USB_Type *ptr, uint8_t ep_addr);
  480. /**
  481. * @brief Submit a transfer
  482. *
  483. * @param[in] ptr A USB peripheral base address
  484. * @param[in] ep_idx An index of the specified endpoint
  485. */
  486. void usb_dcd_edpt_xfer(USB_Type *ptr, uint8_t ep_idx);
  487. /**
  488. * @brief Stall endpoint
  489. *
  490. * @param[in] ptr A USB peripheral base address
  491. * @param[in] ep_addr An address of the specified endpoint
  492. */
  493. void usb_dcd_edpt_stall(USB_Type *ptr, uint8_t ep_addr);
  494. /**
  495. * @brief Clear stall
  496. *
  497. * @param[in] ptr A USB peripheral base address
  498. * @param[in] ep_addr An address of the specified endpoint
  499. */
  500. void usb_dcd_edpt_clear_stall(USB_Type *ptr, uint8_t ep_addr);
  501. /**
  502. * @brief Clear stall
  503. *
  504. * @param[in] ptr A USB peripheral base address
  505. * @param[in] ep_addr An address of the specified endpoint
  506. * @retval The status of endpoint stall, true is stall, false is not stall
  507. */
  508. bool usb_dcd_edpt_check_stall(USB_Type *ptr, uint8_t ep_addr);
  509. /**
  510. * @brief Close a specified endpoint
  511. *
  512. * @param[in] ptr A USB peripheral base address
  513. * @param[in] ep_addr An address of the specified endpoint
  514. */
  515. void usb_dcd_edpt_close(USB_Type *ptr, uint8_t ep_addr);
  516. /**
  517. * @brief Connect by enabling internal pull-up resistor on D+/D-
  518. *
  519. * @param[in] ptr A USB peripheral base address
  520. */
  521. void usb_dcd_connect(USB_Type *ptr);
  522. /**
  523. * @brief Disconnect by disabling internal pull-up resistor on D+/D-
  524. *
  525. * @param[in] ptr A USB peripheral base address
  526. */
  527. void usb_dcd_disconnect(USB_Type *ptr);
  528. /**
  529. * @brief Get setup status of endpoint
  530. *
  531. * @param[in] ptr A USB peripheral base address
  532. * @retval The status of setup endpoint
  533. */
  534. static inline uint32_t usb_dcd_get_edpt_setup_status(USB_Type *ptr)
  535. {
  536. return ptr->ENDPTSETUPSTAT;
  537. }
  538. /**
  539. * @brief Clear the setup status of all specified endpoints
  540. *
  541. * @param[in] ptr A USB peripheral base address
  542. * @param[in] mask A mask of all specified endpoints
  543. */
  544. static inline void usb_dcd_clear_edpt_setup_status(USB_Type *ptr, uint32_t mask)
  545. {
  546. ptr->ENDPTSETUPSTAT = mask;
  547. }
  548. /**
  549. * @brief Set address
  550. *
  551. * @param[in] ptr A USB peripheral base address
  552. * @param[in] dev_addr An assigned endpoint address from USB host
  553. */
  554. static inline void usb_dcd_set_address(USB_Type *ptr, uint8_t dev_addr)
  555. {
  556. ptr->DEVICEADDR = USB_DEVICEADDR_USBADR_SET(dev_addr) | USB_DEVICEADDR_USBADRA_MASK;
  557. }
  558. /**
  559. * @brief Set endpoint list address
  560. *
  561. * @param[in] ptr A USB peripheral base address
  562. * @param[in] addr A start address of the endpoint qtd list
  563. */
  564. static inline void usb_dcd_set_edpt_list_addr(USB_Type *ptr, uint32_t addr)
  565. {
  566. ptr->ENDPTLISTADDR = addr & USB_ENDPTLISTADDR_EPBASE_MASK;
  567. }
  568. /**
  569. * @brief Get device address
  570. *
  571. * @param[in] ptr A USB peripheral base address
  572. * @retval The endpoint address
  573. */
  574. static inline uint8_t usb_dcd_get_device_addr(USB_Type *ptr)
  575. {
  576. return USB_DEVICEADDR_USBADR_GET(ptr->DEVICEADDR);
  577. }
  578. /**
  579. * @brief Get complete status of endpoint
  580. *
  581. * @param[in] ptr A USB peripheral base address
  582. * @retval The complete status od endpoint
  583. */
  584. static inline uint32_t usb_dcd_get_edpt_complete_status(USB_Type *ptr)
  585. {
  586. return ptr->ENDPTCOMPLETE;
  587. }
  588. /**
  589. * @brief Clear complete status of endpoint
  590. *
  591. * @param[in] ptr A USB peripheral base address
  592. * @param[in] mask A mask of the specified endpoints
  593. */
  594. static inline void usb_dcd_clear_edpt_complete_status(USB_Type *ptr, uint32_t mask)
  595. {
  596. ptr->ENDPTCOMPLETE = mask;
  597. }
  598. /*---------------------------------------------------------------------
  599. * Host API
  600. *---------------------------------------------------------------------
  601. */
  602. /**
  603. * @brief Initialize controller to host mode
  604. *
  605. * @param[in] ptr A USB peripheral base address
  606. * @param[in] int_mask A mask of all required interrupts
  607. * @param[in] framelist_size A size of the frame list
  608. */
  609. bool usb_hcd_init(USB_Type *ptr, uint32_t int_mask, uint16_t framelist_size);
  610. /**
  611. * @brief Initialize controller to host modeHost Reset port
  612. *
  613. * @param[in] ptr A USB peripheral base address
  614. */
  615. void usb_hcd_port_reset(USB_Type *ptr);
  616. /**
  617. * @brief Initialize controller to host modeHost set command register
  618. *
  619. * @param[in] ptr A USB peripheral base address
  620. * @param[in] mask A mask of all required commands
  621. */
  622. static inline void usb_hcd_set_command(USB_Type *ptr, uint32_t mask)
  623. {
  624. ptr->USBCMD |= mask;
  625. }
  626. /**
  627. * @brief Get frame index
  628. *
  629. * @param[in] ptr A USB peripheral base address
  630. * @retval A index of the current frame list
  631. */
  632. static inline uint32_t usb_hcd_get_frame_index(USB_Type *ptr)
  633. {
  634. return ptr->FRINDEX;
  635. }
  636. /**
  637. * @brief Get port connect status change
  638. *
  639. * @param[in] ptr A USB peripheral base address
  640. * @retval A connect status change
  641. */
  642. static inline bool usb_hcd_get_port_csc(USB_Type *ptr)
  643. {
  644. return USB_PORTSC1_CSC_GET(ptr->PORTSC1);
  645. }
  646. /**
  647. * @brief Set power ctrl polarity
  648. *
  649. * @param[in] ptr A USB peripheral base address
  650. * @param[in] high true - vbus high level enable, false - vbus low level enable
  651. */
  652. static inline void usb_hcd_set_power_ctrl_polarity(USB_Type *ptr, bool high)
  653. {
  654. if (high) {
  655. ptr->OTG_CTRL0 |= USB_OTG_CTRL0_OTG_POWER_MASK_MASK;
  656. } else {
  657. ptr->OTG_CTRL0 &= ~USB_OTG_CTRL0_OTG_POWER_MASK_MASK;
  658. }
  659. }
  660. /**
  661. * @brief Enable port power
  662. *
  663. * @param[in] ptr A USB peripheral base address
  664. */
  665. static inline void usb_hcd_enable_port_power(USB_Type *ptr)
  666. {
  667. ptr->PORTSC1 |= USB_PORTSC1_PP_MASK;
  668. }
  669. /**
  670. * @brief Get port connect status changeSet async list address
  671. *
  672. * @param[in] ptr A USB peripheral base address
  673. * @param[in] addr An the start address of the async endpoint list
  674. */
  675. static inline void usb_hcd_set_async_list_addr(USB_Type *ptr, uint32_t addr)
  676. {
  677. ptr->ASYNCLISTADDR = addr & USB_ASYNCLISTADDR_ASYBASE_MASK;
  678. }
  679. /**
  680. * @brief Set periodic list address
  681. *
  682. * @param[in] ptr A USB peripheral base address
  683. * @param[in] addr An start address of the periodic endpoint list
  684. */
  685. static inline void usb_hcd_set_periodic_list_addr(USB_Type *ptr, uint32_t addr)
  686. {
  687. ptr->PERIODICLISTBASE = addr & USB_PERIODICLISTBASE_BASEADR_MASK;
  688. }
  689. /**
  690. * @brief Start hcd controller
  691. *
  692. * @param[in] ptr A USB peripheral base address
  693. */
  694. static inline void usb_hcd_run(USB_Type *ptr)
  695. {
  696. ptr->USBCMD |= USB_USBCMD_RS_MASK;
  697. }
  698. /**
  699. * @brief Stop hcd controller
  700. *
  701. * @param[in] ptr A USB peripheral base address
  702. */
  703. static inline void usb_hcd_stop(USB_Type *ptr)
  704. {
  705. ptr->USBCMD &= ~USB_USBCMD_RS_MASK;
  706. }
  707. #if defined __cplusplus
  708. }
  709. #endif /* __cplusplus */
  710. /** @} */
  711. #endif /* HPM_USB_DRV_H */