usb_core.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /*!
  2. \file usb_core.c
  3. \brief USB core driver which can operate in host-mode and device-mode
  4. \version 2014-12-26, V1.0.0, firmware for GD32F10x
  5. \version 2017-06-20, V2.0.0, firmware for GD32F10x
  6. \version 2018-07-31, V2.1.0, firmware for GD32F10x
  7. */
  8. /*
  9. Copyright (c) 2018, GigaDevice Semiconductor Inc.
  10. All rights reserved.
  11. Redistribution and use in source and binary forms, with or without modification,
  12. are permitted provided that the following conditions are met:
  13. 1. Redistributions of source code must retain the above copyright notice, this
  14. list of conditions and the following disclaimer.
  15. 2. Redistributions in binary form must reproduce the above copyright notice,
  16. this list of conditions and the following disclaimer in the documentation
  17. and/or other materials provided with the distribution.
  18. 3. Neither the name of the copyright holder nor the names of its contributors
  19. may be used to endorse or promote products derived from this software without
  20. specific prior written permission.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  23. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  24. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  25. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  26. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  30. OF SUCH DAMAGE.
  31. */
  32. #include "usb_core.h"
  33. static void usb_commonint_enable (usb_core_handle_struct *pudev);
  34. static usb_status_enum usb_core_reset (usb_core_handle_struct *pudev);
  35. /*!
  36. \brief enable the commmon interrupts which are used in both device and host modes
  37. \param[in] pudev: pointer to selected usb device
  38. \param[out] none
  39. \retval none
  40. */
  41. static void usb_commonint_enable (usb_core_handle_struct *pudev)
  42. {
  43. #ifndef USE_OTG_MODE
  44. /* clear any pending USB interrupts */
  45. USB_GOTGINTF = 0xFFFFFFFFU;
  46. #endif /* USE_OTG_MODE */
  47. USB_GINTF = 0xBFFFFFFF;
  48. /* enable the usb wakeup and suspend interrupts */
  49. USB_GINTEN = GINTEN_WKUPIE | GINTEN_SPIE;
  50. #ifdef USE_OTG_MODE
  51. /* enable the OTG interrupts, session interrrupts and connector ID pin interrupt */
  52. USB_GINTEN |= GINTEN_OTGIE | GINTEN_SESIE | GINTEN_CIDPSCIE;
  53. #endif /* USE_OTG_MODE */
  54. }
  55. /*!
  56. \brief soft reset of the OTG_FS core
  57. \param[in] pudev: pointer to usb device
  58. \param[out] none
  59. \retval operation status
  60. */
  61. static usb_status_enum usb_core_reset (usb_core_handle_struct *pudev)
  62. {
  63. uint32_t count = 0U;
  64. /* enable core soft reset */
  65. USB_GRSTCTL |= GRSTCTL_CSRST;
  66. /* wait for the core to be soft reset */
  67. do {
  68. if (++count > 200000U) {
  69. break;
  70. }
  71. } while (1U == (USB_GRSTCTL & GRSTCTL_CSRST));
  72. /* wait for addtional 3 PHY clocks */
  73. if (NULL != pudev->udelay) {
  74. pudev->udelay(3U);
  75. }
  76. return USB_OK;
  77. }
  78. /*!
  79. \brief write a packet into the tx fifo associated with the endpoint
  80. \param[in] src: pointer to source buffer
  81. \param[in] chep_num: channel or endpoint identifier which is in (0..3)
  82. \param[in] len: packet length
  83. \param[out] none
  84. \retval operation status
  85. */
  86. usb_status_enum usb_fifo_write (uint8_t *src, uint8_t chep_num, uint16_t len)
  87. {
  88. uint32_t count32b = 0U, i = 0U;
  89. __IO uint32_t *fifo = USB_FIFO(chep_num);
  90. count32b = (len + 3U) / 4U;
  91. for (i = 0U; i < count32b; i++) {
  92. *fifo = *((__packed uint32_t *)src);
  93. src += 4U;
  94. }
  95. return USB_OK;
  96. }
  97. /*!
  98. \brief read a packet from the rx fifo associated with the endpoint
  99. \param[in] dest: pointer to destination buffer
  100. \param[in] len: packet length
  101. \param[out] none
  102. \retval void type pointer
  103. */
  104. void *usb_fifo_read (uint8_t *dest, uint16_t len)
  105. {
  106. uint32_t i = 0U;
  107. uint32_t count32b = (len + 3U) / 4U;
  108. __IO uint32_t *fifo = USB_FIFO(0U);
  109. for (i = 0U; i < count32b; i++) {
  110. *(__packed uint32_t *)dest = *fifo;
  111. dest += 4U;
  112. }
  113. return ((void *)dest);
  114. }
  115. /*!
  116. \brief initialize core parameters
  117. \param[in] pudev: pointer to usb device
  118. \param[in] core_id: USB core id
  119. \param[out] none
  120. \retval operation status
  121. */
  122. usb_status_enum usb_core_select (usb_core_handle_struct *pudev, usb_core_id_enum core_id)
  123. {
  124. /* at startup the core is in FS mode */
  125. pudev->cfg.core_speed = USB_CORE_SPEED_FULL;
  126. pudev->cfg.max_packet_size = USBFS_MAX_PACKET_SIZE;
  127. /* initialize the core parameters */
  128. if (USB_FS_CORE_ID == core_id) {
  129. pudev->cfg.core_id = USB_FS_CORE_ID;
  130. /* set the host channel numbers */
  131. pudev->cfg.host_channel_num = USBFS_MAX_HOST_CHANNELCOUNT;
  132. /* set the device endpoint numbers */
  133. pudev->cfg.dev_endp_num = USBFS_MAX_DEV_EPCOUNT;
  134. /* fifo size is in terms of DWORD */
  135. pudev->cfg.max_fifo_size = USBFS_MAX_FIFO_WORDLEN;
  136. /* OTG_FS core use embedded physical layer */
  137. pudev->cfg.phy_interface = USB_CORE_EMBEDDED_PHY;
  138. #ifdef USBFS_SOF_OUTPUT_ENABLED
  139. pudev->cfg.sof_output = 1U;
  140. #endif /* USBFS_SOF_OUTPUT_ENABLED */
  141. #ifdef USBFS_LOW_PWR_MGMT_SUPPORT
  142. pudev->cfg.low_power = 1U;
  143. #endif /* USBFS_LOW_PWR_MGMT_SUPPORT */
  144. }
  145. return USB_OK;
  146. }
  147. /*!
  148. \brief initializes the USB controller registers and
  149. prepares the core device mode or host mode operation
  150. \param[in] pudev: pointer to usb device
  151. \param[out] none
  152. \retval operation status
  153. */
  154. usb_status_enum usb_core_init (usb_core_handle_struct *pudev)
  155. {
  156. /* soft reset the core */
  157. usb_core_reset(pudev);
  158. /* active the transceiver and enable vbus sensing */
  159. USB_GCCFG |= GCCFG_PWRON | GCCFG_VBUSACEN | GCCFG_VBUSBCEN;
  160. /* set tx fifo empty level to half empty mode */
  161. USB_GAHBCS &= ~GAHBCS_TXFTH | TXFIFO_EMPTY_HALF;
  162. #ifndef VBUS_SENSING_ENABLED
  163. USB_GCCFG |= GCCFG_VBUSIG;
  164. #endif /* VBUS_SENSING_ENABLED */
  165. if(pudev->cfg.sof_output){
  166. USB_GCCFG |= GCCFG_SOFOEN;
  167. }
  168. if (NULL != pudev->mdelay) {
  169. pudev->mdelay(20U);
  170. }
  171. #ifdef USE_OTG_MODE
  172. /* enable OTG features */
  173. USB_GUSBCS |= GUSBCS_HNPCAP | GUSBCS_SRPCAP;
  174. USB_OTG_EnableCommonInt(pudev);
  175. #endif /* USE_OTG_MODE */
  176. return USB_OK;
  177. }
  178. /*!
  179. \brief flush a tx fifo or all tx fifos
  180. \param[in] pudev: pointer to usb device
  181. \param[in] fifo_num: fifo number which is in (0..3)
  182. \param[out] none
  183. \retval operation status
  184. */
  185. usb_status_enum usb_txfifo_flush (usb_core_handle_struct *pudev, uint8_t fifo_num)
  186. {
  187. uint32_t count = 0U;
  188. USB_GRSTCTL = ((uint32_t)fifo_num << 6U) | GRSTCTL_TXFF;
  189. /* wait for tx fifo flush bit is set */
  190. do {
  191. if (++count > 200000U) {
  192. break;
  193. }
  194. } while (USB_GRSTCTL & GRSTCTL_TXFF);
  195. /* wait for 3 PHY clocks */
  196. if (NULL != pudev->udelay) {
  197. pudev->udelay(3U);
  198. }
  199. return USB_OK;
  200. }
  201. /*!
  202. \brief flush the entire rx fifo
  203. \param[in] pudev: pointer to usb device
  204. \param[out] none
  205. \retval operation status
  206. */
  207. usb_status_enum usb_rxfifo_flush (usb_core_handle_struct *pudev)
  208. {
  209. uint32_t count = 0U;
  210. USB_GRSTCTL = GRSTCTL_RXFF;
  211. /* wait for rx fifo flush bit is set */
  212. do {
  213. if (++count > 200000U) {
  214. break;
  215. }
  216. } while (USB_GRSTCTL & GRSTCTL_RXFF);
  217. /* wait for 3 PHY clocks */
  218. if (NULL != pudev->udelay) {
  219. pudev->udelay(3U);
  220. }
  221. return USB_OK;
  222. }
  223. /*!
  224. \brief set operation mode (host or device)
  225. \param[in] pudev: pointer to usb device
  226. \param[in] mode: operation mode which need to set
  227. \arg HOST_MODE
  228. \arg DEVICE_MODE
  229. \param[out] none
  230. \retval operation status
  231. */
  232. usb_status_enum usb_mode_set (usb_core_handle_struct *pudev, uint8_t mode)
  233. {
  234. if (HOST_MODE == mode) {
  235. USB_GUSBCS &= ~GUSBCS_FDM;
  236. USB_GUSBCS |= GUSBCS_FHM;
  237. } else if (DEVICE_MODE == mode) {
  238. USB_GUSBCS &= ~GUSBCS_FHM;
  239. USB_GUSBCS |= GUSBCS_FDM;
  240. } else {
  241. /* no operation */
  242. }
  243. if (NULL != pudev->mdelay) {
  244. pudev->mdelay(50U);
  245. }
  246. return USB_OK;
  247. }
  248. #ifdef USE_HOST_MODE
  249. /*!
  250. \brief initializes USB core for host mode
  251. \param[in] pudev: pointer to selected usb host
  252. \param[out] none
  253. \retval operation status
  254. */
  255. usb_status_enum usb_hostcore_init (usb_core_handle_struct *pudev)
  256. {
  257. uint32_t i = 0U;
  258. __IO uint32_t nptxfifolen = 0U;
  259. __IO uint32_t ptxfifolen = 0U;
  260. #ifdef USE_OTG_MODE
  261. __IO uint32_t otgctl = 0;
  262. #endif /* USE_OTG_MODE */
  263. /* restart the PHY clock */
  264. USB_PWRCLKCTL = 0U;
  265. /* initialize host configuration register */
  266. if (USB_CORE_ULPI_PHY == pudev->cfg.phy_interface) {
  267. USB_FSLSCLOCK_INIT(HCTLR_30_60_MHZ);
  268. } else {
  269. USB_FSLSCLOCK_INIT(HCTLR_48_MHZ);
  270. }
  271. /* reset USB port */
  272. usb_port_reset(pudev);
  273. /* configure data fifo sizes */
  274. if (USB_FS_CORE_ID == pudev->cfg.core_id) {
  275. /* set rx fifo size */
  276. USB_GRFLEN = USBFS_RX_FIFO_SIZE;
  277. /* set non-periodic tx fifo size and address */
  278. nptxfifolen &= ~HNPTFLEN_HNPTXRSAR;
  279. nptxfifolen |= USBFS_RX_FIFO_SIZE;
  280. nptxfifolen &= ~HNPTFLEN_HNPTXFD;
  281. nptxfifolen |= USBFS_HTX_NPFIFO_SIZE << 16;
  282. USB_HNPTFLEN = nptxfifolen;
  283. /* set periodic tx fifo size and address */
  284. ptxfifolen &= ~HPTFLEN_HPTXFSAR;
  285. ptxfifolen |= USBFS_RX_FIFO_SIZE + USBFS_HTX_PFIFO_SIZE;
  286. ptxfifolen &= ~HPTFLEN_HPTXFD;
  287. ptxfifolen |= USBFS_HTX_PFIFO_SIZE << 16;
  288. USB_HPTFLEN = ptxfifolen;
  289. }
  290. #ifdef USE_OTG_MODE
  291. /* clear host set HNP enable bit in the USB OTG control register */
  292. otgctl |= GOTGCS_HHNPEN;
  293. USB_GOTGCS &= ~otgctl;
  294. USB_GOTGCS |= 0;
  295. #endif /* USE_OTG_MODE */
  296. /* make sure the fifos are flushed */
  297. /* flush all tx fifos in device or host mode */
  298. usb_txfifo_flush(pudev, 0x10U);
  299. /* flush the entire rx fifo */
  300. usb_rxfifo_flush(pudev);
  301. /* clear all pending host channel interrupts */
  302. USB_HACHINTEN &= ~HACHINTEN_CINTEN;
  303. for (i = 0U; i < pudev->cfg.host_channel_num; i++) {
  304. USB_HCHxINTEN(i) = 0U;
  305. USB_HCHxINTF(i) = 0xFFFFFFFFU;
  306. }
  307. #ifndef USE_OTG_MODE
  308. usb_vbus_drive(pudev, 1U);
  309. #endif /* USE_OTG_MODE */
  310. usb_hostint_enable(pudev);
  311. return USB_OK;
  312. }
  313. /*!
  314. \brief control the VBUS to power
  315. \param[in] pudev: pointer to selected usb host
  316. \param[in] state: VBUS state
  317. \param[out] none
  318. \retval none
  319. */
  320. void usb_vbus_drive (usb_core_handle_struct *pudev, uint8_t state)
  321. {
  322. __IO uint32_t host_port = 0U;
  323. /* enable or disable the external charge pump */
  324. if ((void *)0 != pudev->host.vbus_drive) {
  325. pudev->host.vbus_drive(pudev, state);
  326. }
  327. /* turn on the host port power. */
  328. host_port = USB_PORT_READ();
  329. if ((0U == (host_port & HPCS_PP)) && (1U == state)) {
  330. host_port |= HPCS_PP;
  331. } else if ((1U == (host_port & HPCS_PP)) && (0U == state)) {
  332. host_port &= ~HPCS_PP;
  333. } else {
  334. /* no operation */
  335. }
  336. USB_HPCS = host_port;
  337. if (NULL != pudev->mdelay) {
  338. pudev->mdelay(200U);
  339. }
  340. }
  341. /*!
  342. \brief enables the host mode interrupts
  343. \param[in] pudev: pointer to selected usb host
  344. \param[out] none
  345. \retval operation status
  346. */
  347. usb_status_enum usb_hostint_enable (usb_core_handle_struct *pudev)
  348. {
  349. uint32_t gintf = 0U;
  350. /* disable all interrupts */
  351. USB_GINTEN = 0U;
  352. /* clear any pending interrupts */
  353. USB_GINTF = 0xFFFFFFFFU;
  354. /* enable the common interrupts */
  355. usb_commonint_enable(pudev);
  356. gintf |= GINTF_RXFNEIF;
  357. /* enable host_mode-related interrupts */
  358. gintf |= GINTF_HPIF | GINTF_HCIF | GINTF_DISCIF | GINTF_SOF | GINTF_ISOONCIF;
  359. USB_GINTEN &= ~gintf;
  360. USB_GINTEN |= gintf;
  361. return USB_OK;
  362. }
  363. /*!
  364. \brief reset host port
  365. \param[in] pudev: pointer to usb device
  366. \param[out] none
  367. \retval operation status
  368. */
  369. uint32_t usb_port_reset (usb_core_handle_struct *pudev)
  370. {
  371. __IO uint32_t hpcs;
  372. hpcs = USB_PORT_READ();
  373. hpcs |= HPCS_PRST;
  374. USB_HPCS = hpcs;
  375. if (NULL != pudev->mdelay) {
  376. pudev->mdelay(100U);
  377. }
  378. hpcs &= ~HPCS_PRST;
  379. USB_HPCS = hpcs;
  380. if (NULL != pudev->mdelay) {
  381. pudev->mdelay(20U);
  382. }
  383. return USB_OK;
  384. }
  385. /*!
  386. \brief initialize host channel
  387. \param[in] pudev: pointer to usb device
  388. \param[in] hc_num: host channel number which is in (0..7)
  389. \param[out] none
  390. \retval operation status
  391. */
  392. usb_status_enum usb_hostchannel_init(usb_core_handle_struct *pudev, uint8_t hc_num)
  393. {
  394. uint8_t is_low_speed = 0U;
  395. __IO uint32_t chinten = 0U;
  396. __IO uint32_t chctl = 0U;
  397. usb_hostchannel_struct *puhc = &pudev->host.host_channel[hc_num];
  398. /* clear old interrupt conditions for this host channel */
  399. USB_HCHxINTF((uint16_t)hc_num) = 0xFFFFFFFFU;
  400. /* enable channel interrupts required for this transfer */
  401. switch (puhc->endp_type) {
  402. case USB_EPTYPE_CTRL:
  403. case USB_EPTYPE_BULK:
  404. chinten |= HCHINTEN_TFIE | HCHINTEN_STALLIE | HCHINTEN_USBERIE \
  405. | HCHINTEN_DTERIE | HCHINTEN_NAKIE;
  406. if (puhc->endp_in) {
  407. chinten |= HCHINTEN_BBERIE;
  408. } else {
  409. chinten |= HCHINTEN_NYETIE;
  410. }
  411. break;
  412. case USB_EPTYPE_INTR:
  413. chinten |= HCHINTEN_TFIE | HCHINTEN_STALLIE | HCHINTEN_USBERIE | HCHINTEN_DTERIE \
  414. | HCHINTEN_NAKIE | HCHINTEN_REQOVRIE;
  415. if (puhc->endp_in) {
  416. chinten |= HCHINTEN_BBERIE;
  417. }
  418. break;
  419. case USB_EPTYPE_ISOC:
  420. chinten |= HCHINTEN_TFIE | HCHINTEN_REQOVRIE | HCHINTEN_ACKIE;
  421. if (puhc->endp_in) {
  422. chinten |= HCHINTEN_USBERIE | HCHINTEN_BBERIE;
  423. }
  424. break;
  425. default:
  426. break;
  427. }
  428. USB_HCHxINTEN((uint16_t)hc_num) = chinten;
  429. /* enable the top level host channel interrupt */
  430. USB_HACHINTEN |= 1U << hc_num;
  431. /* make sure host channel interrupts are enabled */
  432. USB_GINTEN |= GINTEN_HCIE;
  433. /* program the hcctlr register */
  434. chctl = 0U;
  435. if (HPRT_PRTSPD_LOW_SPEED == puhc->dev_speed) {
  436. is_low_speed = 1U;
  437. }
  438. chctl |= (uint32_t)puhc->dev_addr << 22U;
  439. chctl |= (uint32_t)puhc->endp_type << 18U;
  440. chctl |= (uint32_t)puhc->endp_id << 11U;
  441. chctl |= (uint32_t)puhc->endp_in << 15U;
  442. chctl |= (uint32_t)is_low_speed << 17U;
  443. chctl |= puhc->endp_mps;
  444. if (HCCHAR_INTR == puhc->endp_type) {
  445. chctl |= HCHCTL_ODDFRM;
  446. }
  447. USB_HCHxCTL((uint16_t)hc_num) = chctl;
  448. return USB_OK;
  449. }
  450. /*!
  451. \brief prepare host channel for transferring packets
  452. \param[in] pudev: pointer to usb device
  453. \param[in] hc_num: host channel number which is in (0..7)
  454. \param[out] none
  455. \retval operation status
  456. */
  457. usb_status_enum usb_hostchannel_startxfer(usb_core_handle_struct *pudev, uint8_t hc_num)
  458. {
  459. uint16_t dword_len = 0U;
  460. uint16_t packet_num = 0U;
  461. __IO uint32_t chxlen = 0U;
  462. __IO uint32_t chctl = 0U;
  463. usb_hostchannel_struct *puhc = &pudev->host.host_channel[hc_num];
  464. /* compute the expected number of packets associated to the transfer */
  465. if (puhc->xfer_len > 0U) {
  466. packet_num = ((uint16_t)puhc->xfer_len + puhc->endp_mps - 1U) / puhc->endp_mps;
  467. if (packet_num > HC_MAX_PACKET_COUNT) {
  468. packet_num = HC_MAX_PACKET_COUNT;
  469. puhc->xfer_len = (uint32_t)(packet_num) * (uint32_t)(puhc->endp_mps);
  470. }
  471. } else {
  472. packet_num = 1U;
  473. }
  474. if (puhc->endp_in) {
  475. puhc->xfer_len = (uint32_t)(packet_num) * (uint32_t)(puhc->endp_mps);
  476. }
  477. /* initialize the host channel length register */
  478. chxlen &= ~HCHLEN_TLEN;
  479. chxlen |= puhc->xfer_len;
  480. chxlen &= ~HCHLEN_PCNT;
  481. chxlen |= (uint32_t)packet_num << 19U;
  482. chxlen &= ~HCHLEN_DPID;
  483. chxlen |= (uint32_t)(puhc->DPID) << 29U;
  484. USB_HCHxLEN((uint16_t)hc_num) = (uint32_t)chxlen;
  485. /* set host channel enable */
  486. chctl = USB_HCHxCTL((uint16_t)hc_num);
  487. if (1U == USB_EVEN_FRAME()) {
  488. chctl |= HCHCTL_ODDFRM;
  489. } else {
  490. chctl &= ~HCHCTL_ODDFRM;
  491. }
  492. chctl |= HCHCTL_CEN;
  493. chctl &= ~HCHCTL_CDIS;
  494. USB_HCHxCTL((uint16_t)hc_num) = chctl;
  495. if ((0U == puhc->endp_in) && (puhc->xfer_len > 0U)) {
  496. dword_len = (uint16_t)(puhc->xfer_len + 3U) / 4U;
  497. switch (puhc->endp_type) {
  498. /* non-periodic transfer */
  499. case USB_EPTYPE_CTRL:
  500. case USB_EPTYPE_BULK:
  501. /* check if there is enough space in fifo space */
  502. if (dword_len > (USB_HNPTFQSTAT & HNPTFQSTAT_NPTXFS)) {
  503. /* need to process data in non-periodic transfer fifo empty interrupt */
  504. USB_GINTEN |= GINTEN_NPTXFEIE;
  505. }
  506. break;
  507. /* periodic transfer */
  508. case USB_EPTYPE_INTR:
  509. case USB_EPTYPE_ISOC:
  510. /* check if there is enough space in FIFO space */
  511. if (dword_len > (USB_HPTFQSTAT & HPTFQSTAT_PTXFS)) {
  512. /* need to process data in periodic transfer fifo empty interrupt */
  513. USB_GINTEN |= GINTEN_PTXFEIE;
  514. }
  515. break;
  516. default:
  517. break;
  518. }
  519. /* write packet into the Tx FIFO. */
  520. usb_fifo_write(puhc->xfer_buff, hc_num, (uint16_t)puhc->xfer_len);
  521. }
  522. return USB_OK;
  523. }
  524. /*!
  525. \brief halt channel
  526. \param[in] pudev: pointer to usb device
  527. \param[in] hc_num: host channel number which is in (0..7)
  528. \param[out] none
  529. \retval operation status
  530. */
  531. usb_status_enum usb_hostchannel_halt(usb_core_handle_struct *pudev, uint8_t hc_num)
  532. {
  533. uint8_t endp_type = 0U;
  534. __IO uint32_t chctl = USB_HCHxCTL((uint16_t)hc_num);
  535. chctl |= HCHCTL_CEN | HCHCTL_CDIS;
  536. endp_type = (uint8_t)((chctl & HCHCTL_EPTYPE) >> 18U);
  537. /* check for space in the request queue to issue the halt. */
  538. if ((HCCHAR_CTRL == endp_type) || (HCCHAR_BULK == endp_type)) {
  539. if (0U == (USB_HNPTFQSTAT & HNPTFQSTAT_NPTXFS)) {
  540. chctl &= ~HCHCTL_CEN;
  541. }
  542. } else {
  543. if (0U == (USB_HPTFQSTAT & HPTFQSTAT_PTXFS)) {
  544. chctl &= ~HCHCTL_CEN;
  545. }
  546. }
  547. USB_HCHxCTL((uint16_t)hc_num) = chctl;
  548. return USB_OK;
  549. }
  550. /*!
  551. \brief stop the USB host and clean up fifos
  552. \param[in] none
  553. \param[out] none
  554. \retval none
  555. */
  556. void usb_host_stop(usb_core_handle_struct *pudev)
  557. {
  558. uint32_t i;
  559. /* disable all host channel interrupt */
  560. USB_HACHINTEN = 0U;
  561. USB_HACHINT = 0xFFFFFFFFU;
  562. /* flush out any leftover queued requests */
  563. for (i = 0U; i < pudev->cfg.host_channel_num; i++) {
  564. USB_HCHxCTL(i) |= HCHCTL_CEN | HCHCTL_CDIS | HCHCTL_EPDIR;
  565. }
  566. /* flush the FIFO */
  567. usb_rxfifo_flush(pudev);
  568. usb_txfifo_flush(pudev, 0x10U);
  569. }
  570. #endif /* USE_HOST_MODE */
  571. #ifdef USE_DEVICE_MODE
  572. /* USB endpoint Tx FIFO size */
  573. static uint16_t USBFS_TX_FIFO_SIZE[USBFS_MAX_DEV_EPCOUNT] =
  574. {
  575. (uint16_t)TX0_FIFO_FS_SIZE,
  576. (uint16_t)TX1_FIFO_FS_SIZE,
  577. (uint16_t)TX2_FIFO_FS_SIZE,
  578. (uint16_t)TX3_FIFO_FS_SIZE
  579. };
  580. static usb_status_enum usb_devint_enable(usb_core_handle_struct *pudev);
  581. /*!
  582. \brief initialize USB core registers for device mode
  583. \param[in] pudev: pointer to usb device
  584. \param[out] none
  585. \retval operation status
  586. */
  587. usb_status_enum usb_devcore_init (usb_core_handle_struct *pudev)
  588. {
  589. uint32_t i, ram_address = 0U;
  590. __IO uint32_t devinep0intf = USB_DIEP0TFLEN;
  591. __IO uint32_t devinepintf = 0U;
  592. /* restart the phy clock (maybe don't need to...) */
  593. USB_PWRCLKCTL = 0U;
  594. /* config periodic frmae interval to default */
  595. USB_DCFG &= ~DCFG_EOPFT;
  596. USB_DCFG |= FRAME_INTERVAL_80;
  597. if (USB_FS_CORE_ID == pudev->cfg.core_id) {
  598. /* set full speed PHY */
  599. USB_DCFG &= ~DCFG_DS;
  600. USB_DCFG |= USB_SPEED_INP_FULL;
  601. /* set rx fifo size */
  602. USB_GRFLEN &= ~GRFLEN_RXFD;
  603. USB_GRFLEN |= (uint32_t)RX_FIFO_FS_SIZE;
  604. /* set endpoint 0 tx fifo length and RAM address */
  605. devinep0intf &= ~DIEP0TFLEN_IEP0TXFD;
  606. devinep0intf |= (uint32_t)TX0_FIFO_FS_SIZE << 16;
  607. devinep0intf &= ~DIEP0TFLEN_IEP0TXRSAR;
  608. devinep0intf |= (uint32_t)RX_FIFO_FS_SIZE;
  609. USB_DIEP0TFLEN = devinep0intf;
  610. ram_address = (uint32_t)RX_FIFO_FS_SIZE;
  611. /* set endpoint 1 to 3's tx fifo length and RAM address */
  612. for (i = 1U; i < USBFS_MAX_DEV_EPCOUNT; i++) {
  613. ram_address += USBFS_TX_FIFO_SIZE[i - 1U];
  614. devinepintf &= ~DIEPTFLEN_IEPTXFD;
  615. devinepintf |= (uint32_t)USBFS_TX_FIFO_SIZE[i] << 16U;
  616. devinepintf &= ~DIEPTFLEN_IEPTXRSAR;
  617. devinepintf |= ram_address;
  618. USB_DIEPxTFLEN(i) = devinepintf;
  619. }
  620. }
  621. /* make sure all fifos are flushed */
  622. /* flush all tx fifos */
  623. usb_txfifo_flush(pudev, 0x10U);
  624. /* flush entire rx fifo */
  625. usb_rxfifo_flush(pudev);
  626. /* clear all pending device interrupts */
  627. USB_DIEPINTEN = 0U;
  628. USB_DOEPINTEN = 0U;
  629. USB_DAEPINT = 0xFFFFFFFFU;
  630. USB_DAEPINTEN = 0U;
  631. /* configure all in/out endpoints */
  632. for (i = 0U; i < pudev->cfg.dev_endp_num; i++) {
  633. if (USB_DIEPxCTL(i) & DEPCTL_EPEN) {
  634. USB_DIEPxCTL(i) |= DEPCTL_EPD | DEPCTL_SNAK;
  635. } else {
  636. USB_DIEPxCTL(i) = 0U;
  637. }
  638. if (USB_DOEPxCTL(i) & DEPCTL_EPEN) {
  639. USB_DOEPxCTL(i) |= DEPCTL_EPD | DEPCTL_SNAK;
  640. } else {
  641. USB_DOEPxCTL(i) = 0U;
  642. }
  643. /* set in/out endpoint transfer length to 0 */
  644. USB_DIEPxLEN(i) = 0U;
  645. USB_DOEPxLEN(i) = 0U;
  646. /* clear all pending in/out endpoints interrupts */
  647. USB_DIEPxINTF(i) = 0xFFU;
  648. USB_DOEPxINTF(i) = 0xFFU;
  649. }
  650. usb_devint_enable(pudev);
  651. return USB_OK;
  652. }
  653. /*!
  654. \brief enable the device mode interrupts
  655. \param[in] pudev: pointer to usb device
  656. \param[out] none
  657. \retval status
  658. */
  659. static usb_status_enum usb_devint_enable(usb_core_handle_struct *pudev)
  660. {
  661. uint32_t int_mask = 0U;
  662. /* disable all interrupts */
  663. USB_GINTEN = 0U;
  664. /* clear any pending interrupts */
  665. USB_GINTF = 0xBFFFFFFFU;
  666. /* enable the common interrupts */
  667. usb_commonint_enable(pudev);
  668. int_mask = GINTEN_RXFNEIE;
  669. /* enable device_mode-related interrupts */
  670. int_mask |= GINTEN_SPIE | GINTEN_RSTIE | GINTEN_ENUMFIE \
  671. | GINTEN_IEPIE | GINTEN_OEPIE | GINTEN_SOFIE | GINTEN_ISOONCIE \
  672. | GINTEN_ISOINCIE;
  673. #ifdef VBUS_SENSING_ENABLED
  674. int_mask |= GINTEN_SESIE | GINTEN_OTGIE;
  675. #endif /* VBUS_SENSING_ENABLED */
  676. USB_GINTEN &= ~int_mask;
  677. USB_GINTEN |= int_mask;
  678. return USB_OK;
  679. }
  680. /*!
  681. \brief configures endpoint 0 to receive setup packets
  682. \param[in] pudev: pointer to usb device
  683. \param[out] none
  684. \retval none
  685. */
  686. void usb_ep0_startout(usb_core_handle_struct *pudev)
  687. {
  688. __IO uint32_t ep0len = 0U;
  689. /* set out endpoint 0 receive length to 24 bytes */
  690. ep0len &= ~DOEP0LEN_TLEN;
  691. ep0len |= 8U * 3U;
  692. /* set out endpoint 0 receive length to 1 packet */
  693. ep0len &= ~DOEP0LEN_PCNT;
  694. ep0len |= 1U << 19;
  695. /* set setup packet count to 3 */
  696. ep0len &= ~DOEP0LEN_STPCNT;
  697. ep0len |= 3U << 29;
  698. USB_DOEPxLEN(0U) = ep0len;
  699. }
  700. /*!
  701. \brief active remote wakeup signalling
  702. \param[in] pudev: pointer to usb device
  703. \param[out] none
  704. \retval none
  705. */
  706. void usb_remotewakeup_active(usb_core_handle_struct *pudev)
  707. {
  708. __IO uint32_t power_clock;
  709. if (pudev->dev.remote_wakeup) {
  710. if (1U == (USB_DSTAT & DSTAT_SPST)) {
  711. if (pudev->cfg.low_power) {
  712. /* ungate USB core clock */
  713. power_clock = USB_PWRCLKCTL;
  714. power_clock &= ~PWRCLKCTL_SHCLK;
  715. power_clock &= ~PWRCLKCTL_SUCLK;
  716. USB_PWRCLKCTL = power_clock;
  717. }
  718. /* active remote wakeup signaling */
  719. USB_DCTL |= DCTL_RWKUP;
  720. if (pudev->mdelay != (void *)0) {
  721. pudev->mdelay(5U);
  722. }
  723. USB_DCTL &= ~DCTL_RWKUP;
  724. }
  725. }
  726. }
  727. /*!
  728. \brief active USB core clock
  729. \param[in] pudev: pointer to usb device
  730. \param[out] none
  731. \retval none
  732. */
  733. void usb_clock_ungate(usb_core_handle_struct *pudev)
  734. {
  735. if (pudev->cfg.low_power) {
  736. __IO uint32_t power_clock;
  737. if (1U == (USB_DSTAT & DSTAT_SPST)) {
  738. /* un-gate USB core clock */
  739. power_clock = USB_PWRCLKCTL;
  740. power_clock &= ~PWRCLKCTL_SHCLK;
  741. power_clock &= ~PWRCLKCTL_SUCLK;
  742. USB_PWRCLKCTL = power_clock;
  743. }
  744. }
  745. }
  746. /*!
  747. \brief stop the device and clean up fifos
  748. \param[in] pudev: pointer to usb device
  749. \param[out] none
  750. \retval none
  751. */
  752. void usb_device_stop (usb_core_handle_struct *pudev)
  753. {
  754. uint32_t i;
  755. pudev->dev.status = 1U;
  756. for (i = 0U; i < pudev->cfg.dev_endp_num; i++) {
  757. USB_DIEPxINTF(i) = 0xFFU;
  758. USB_DOEPxINTF(i) = 0xFFU;
  759. }
  760. USB_DIEPINTEN = 0U;
  761. USB_DOEPINTEN = 0U;
  762. USB_DAEPINTEN = 0U;
  763. USB_DAEPINT = 0xFFFFFFFFU;
  764. /* flush the FIFO */
  765. usb_rxfifo_flush(pudev);
  766. usb_txfifo_flush(pudev, 0x10U);
  767. }
  768. #endif /* USE_DEVICE_MODE */