usbd_int.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. /*!
  2. \file usbd_int.c
  3. \brief USB device mode interrupt routines
  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 "usbd_int.h"
  33. #include "usbd_std.h"
  34. /* interrupt handlers */
  35. static uint32_t usbd_intf_outep (usb_core_handle_struct *pudev);
  36. static uint32_t usbd_intf_inep (usb_core_handle_struct *pudev);
  37. static uint32_t usbd_intf_earlysuspend (usb_core_handle_struct *pudev);
  38. static uint32_t usbd_intf_suspend (usb_core_handle_struct *pudev);
  39. static uint32_t usbd_intf_resume (usb_core_handle_struct *pudev);
  40. static uint32_t usbd_intf_sof (usb_core_handle_struct *pudev);
  41. static uint32_t usbd_intf_rxfifo (usb_core_handle_struct *pudev);
  42. static uint32_t usbd_intf_reset (usb_core_handle_struct *pudev);
  43. static uint32_t usbd_intf_enumfinish (usb_core_handle_struct *pudev);
  44. static uint32_t usbd_intf_isoinincomplete (usb_core_handle_struct *pudev);
  45. static uint32_t usbd_intf_isooutincomplete (usb_core_handle_struct *pudev);
  46. static uint32_t usbd_emptytxfifo_write (usb_core_handle_struct *pudev, uint8_t ep_num);
  47. #ifdef VBUS_SENSING_ENABLED
  48. static uint32_t usbd_intf_otg (usb_core_handle_struct *pudev);
  49. static uint32_t usbd_intf_sessionrequest (usb_core_handle_struct *pudev);
  50. #endif /* VBUS_SENSING_ENABLED */
  51. static usb_speed_enum USB_SPEED[4] = {
  52. [DSTAT_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ] = USB_SPEED_HIGH,
  53. [DSTAT_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ] = USB_SPEED_FULL,
  54. [DSTAT_ENUMSPD_FS_PHY_48MHZ] = USB_SPEED_FULL,
  55. [DSTAT_ENUMSPD_LS_PHY_6MHZ] = USB_SPEED_LOW
  56. };
  57. static const uint8_t EP0_MAXLEN[4] = {
  58. [DSTAT_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ] = EP0MPL_64,
  59. [DSTAT_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ] = EP0MPL_64,
  60. [DSTAT_ENUMSPD_FS_PHY_48MHZ] = EP0MPL_64,
  61. [DSTAT_ENUMSPD_LS_PHY_6MHZ] = EP0MPL_8
  62. };
  63. /*!
  64. \brief USB device-mode interrupts global service routine handler
  65. \param[in] pudev: pointer to usb device instance
  66. \param[out] none
  67. \retval operation status
  68. */
  69. uint32_t usbd_isr (usb_core_handle_struct *pudev)
  70. {
  71. uint32_t retval = 0U;
  72. uint32_t int_status = 0U, gintf = USB_GINTF, ginten = USB_GINTEN;
  73. /* ensure the core is in device mode */
  74. if (DEVICE_MODE == USB_CURRENT_MODE_GET()) {
  75. int_status = gintf & ginten;
  76. /* there are no interrupts, avoid spurious interrupt */
  77. if (!int_status) {
  78. return 0U;
  79. }
  80. /* OUT endpoints interrupts */
  81. if (int_status & GINTF_OEPIF) {
  82. retval |= usbd_intf_outep(pudev);
  83. }
  84. /* IN endpoints interrupts */
  85. if (int_status & GINTF_IEPIF) {
  86. retval |= usbd_intf_inep(pudev);
  87. }
  88. /* mode mismatch interrupt */
  89. if (int_status & GINTF_MFIF) {
  90. /* clear interrupt */
  91. USB_GINTF = GINTF_MFIF;
  92. }
  93. /* early suspend interrupt */
  94. if (int_status & GINTF_ESP) {
  95. retval |= usbd_intf_earlysuspend(pudev);
  96. }
  97. /* suspend interrupt */
  98. if (int_status & GINTF_SP) {
  99. retval |= usbd_intf_suspend(pudev);
  100. }
  101. /* wakeup interrupt */
  102. if (int_status & GINTF_WKUPIF) {
  103. retval |= usbd_intf_resume(pudev);
  104. }
  105. /* start of frame interrupt */
  106. if (int_status & GINTF_SOF) {
  107. retval |= usbd_intf_sof(pudev);
  108. }
  109. /* reveive fifo not empty interrupt */
  110. if (int_status & GINTF_RXFNEIF) {
  111. retval |= usbd_intf_rxfifo(pudev);
  112. }
  113. /* USB reset interrupt */
  114. if (int_status & GINTF_RST) {
  115. retval |= usbd_intf_reset(pudev);
  116. }
  117. /* enumeration has been finished interrupt */
  118. if (int_status & GINTF_ENUMF) {
  119. retval |= usbd_intf_enumfinish(pudev);
  120. }
  121. /* incomplete synchronization in transfer interrupt*/
  122. if (int_status & GINTF_ISOINCIF) {
  123. retval |= usbd_intf_isoinincomplete(pudev);
  124. }
  125. /* incomplete synchronization out transfer interrupt*/
  126. if (int_status & GINTF_ISOONCIF) {
  127. retval |= usbd_intf_isooutincomplete(pudev);
  128. }
  129. #ifdef VBUS_SENSING_ENABLED
  130. /* session request interrupt */
  131. if (int_status & GINTF_SESIF) {
  132. retval |= usbd_intf_sessionrequest(pudev);
  133. }
  134. /* OTG mode interrupt */
  135. if (int_status & GINTF_OTGIF) {
  136. retval |= usbd_intf_otg(pudev);
  137. }
  138. #endif /* VBUS_SENSING_ENABLED */
  139. }
  140. return retval;
  141. }
  142. /*!
  143. \brief indicates that an OUT endpoint has a pending interrupt
  144. \param[in] pudev: pointer to usb device instance
  145. \param[out] none
  146. \retval operation status
  147. */
  148. static uint32_t usbd_intf_outep (usb_core_handle_struct *pudev)
  149. {
  150. uint8_t endp_num = 0U;
  151. uint32_t endp_intr = 0U;
  152. __IO uint32_t out_endp_intr = 0U;
  153. /* read in the device interrupt bits */
  154. USB_DAOEP_INTR_READ(endp_intr);
  155. while (endp_intr) {
  156. if (endp_intr & 0x1U) {
  157. USB_DOEP_INTR_READ(out_endp_intr, (uint16_t)endp_num);
  158. /* transfer complete interrupt */
  159. if (out_endp_intr & DOEPINTF_TF) {
  160. USB_DOEPxINTF((uint16_t)endp_num) = DOEPINTF_TF;
  161. /* data receive is completed */
  162. usbd_out_transaction(pudev, endp_num);
  163. }
  164. /* endpoint disable interrupt */
  165. if (out_endp_intr & DOEPINTF_EPDIS) {
  166. USB_DOEPxINTF((uint16_t)endp_num) = DOEPINTF_EPDIS;
  167. }
  168. /* setup phase finished interrupt (just for control endpoints) */
  169. if (out_endp_intr & DOEPINTF_STPF) {
  170. /* setup phase is completed */
  171. usbd_setup_transaction(pudev);
  172. USB_DOEPxINTF((uint16_t)endp_num) = DOEPINTF_STPF;
  173. }
  174. /* back to back setup packets received */
  175. if (out_endp_intr & DOEPINTF_BTBSTP) {
  176. USB_DOEPxINTF((uint16_t)endp_num) = DOEPINTF_BTBSTP;
  177. }
  178. }
  179. endp_num ++;
  180. endp_intr >>= 1;
  181. }
  182. return 1U;
  183. }
  184. /*!
  185. \brief indicates that an in endpoint has a pending interrupt
  186. \param[in] pudev: pointer to usb device instance
  187. \param[out] none
  188. \retval operation status
  189. */
  190. static uint32_t usbd_intf_inep(usb_core_handle_struct *pudev)
  191. {
  192. uint8_t endp_num = 0U;
  193. uint32_t endp_intr = 0U;
  194. __IO uint32_t in_endp_intr = 0U;
  195. /* get all in endpoints which have interrupts */
  196. USB_DAIEP_INTR_READ(endp_intr);
  197. while (endp_intr) {
  198. if (endp_intr & 0x1U) {
  199. USB_DIEP_INTR_READ(in_endp_intr, (uint16_t)endp_num);
  200. if (in_endp_intr & DIEPINTF_TF) {
  201. /* disable the fifo empty interrupt for the endpoint */
  202. USB_DIEPFEINTEN &= ~(0x1U << endp_num);
  203. USB_DIEPxINTF((uint16_t)endp_num) = DIEPINTF_TF;
  204. /* data transmittion is completed */
  205. usbd_in_transaction(pudev, endp_num);
  206. }
  207. if (in_endp_intr & DIEPINTF_CITO) {
  208. USB_DIEPxINTF((uint16_t)endp_num) = DIEPINTF_CITO;
  209. }
  210. if (in_endp_intr & DIEPINTF_IEPNE) {
  211. USB_DIEPxINTF((uint16_t)endp_num) = DIEPINTF_IEPNE;
  212. }
  213. if (in_endp_intr & DIEPINTF_EPDIS) {
  214. USB_DIEPxINTF((uint16_t)endp_num) = DIEPINTF_EPDIS;
  215. }
  216. if (in_endp_intr & DIEPINTF_TXFE) {
  217. usbd_emptytxfifo_write(pudev, endp_num);
  218. USB_DIEPxINTF((uint16_t)endp_num) = DIEPINTF_TXFE;
  219. }
  220. }
  221. endp_num ++;
  222. endp_intr >>= 1;
  223. }
  224. return 1U;
  225. }
  226. /*!
  227. \brief indicates that early suspend state has been detected on the USB
  228. \param[in] pudev: pointer to usb device instance
  229. \param[out] none
  230. \retval operation status
  231. */
  232. static uint32_t usbd_intf_earlysuspend (usb_core_handle_struct *pudev)
  233. {
  234. USB_GINTEN &= ~GINTEN_ESPIE;
  235. USB_GINTF = GINTF_ESP;
  236. return 1U;
  237. }
  238. /*!
  239. \brief indicates that suspend state has been detected on the USB
  240. \param[in] pudev: pointer to usb device instance
  241. \param[out] none
  242. \retval operation status
  243. */
  244. static uint32_t usbd_intf_suspend(usb_core_handle_struct *pudev)
  245. {
  246. __IO uint8_t low_power = pudev->cfg.low_power;
  247. __IO uint8_t suspend = (uint8_t)(USB_DSTAT & DSTAT_SPST);
  248. __IO uint8_t is_configured = (pudev->dev.status == USB_STATUS_CONFIGURED)? 1U : 0U;
  249. pudev->dev.prev_status = pudev->dev.status;
  250. pudev->dev.status = USB_STATUS_SUSPENDED;
  251. if (low_power && suspend && is_configured) {
  252. /* switch-off the otg clocks */
  253. USB_PWRCLKCTL |= PWRCLKCTL_SUCLK | PWRCLKCTL_SHCLK;
  254. /* enter DEEP_SLEEP mode with LDO in low power mode */
  255. pmu_to_deepsleepmode(PMU_LDO_LOWPOWER, WFI_CMD);
  256. }
  257. /* clear interrupt */
  258. USB_GINTF = GINTF_SP;
  259. return 1U;
  260. }
  261. /*!
  262. \brief indicates that the USB controller has detected a resume or remote Wake-up sequence
  263. \param[in] pudev: pointer to usb device instance
  264. \param[out] none
  265. \retval operation status
  266. */
  267. static uint32_t usbd_intf_resume (usb_core_handle_struct *pudev)
  268. {
  269. pudev->dev.status = pudev->dev.prev_status;
  270. pudev->dev.status = USB_STATUS_CONFIGURED;
  271. /* clear interrupt */
  272. USB_GINTF = GINTF_WKUPIF;
  273. return 1U;
  274. }
  275. /*!
  276. \brief handle the SOF interrupts
  277. \param[in] pudev: pointer to usb device instance
  278. \param[out] none
  279. \retval operation status
  280. */
  281. static uint32_t usbd_intf_sof(usb_core_handle_struct *pudev)
  282. {
  283. if (NULL != usbd_int_fops) {
  284. usbd_int_fops->SOF(pudev);
  285. }
  286. USB_GINTF = GINTF_SOF;
  287. return 1U;
  288. }
  289. /*!
  290. \brief handle the rx status queue level interrupt
  291. \param[in] pudev: pointer to usb device instance
  292. \param[out] none
  293. \retval operation status
  294. */
  295. static uint32_t usbd_intf_rxfifo (usb_core_handle_struct *pudev)
  296. {
  297. usb_ep_struct *ep;
  298. uint8_t data_pid = 0U, endp_num = 0U;
  299. uint32_t bcount = 0U, packet_num = 0U;
  300. /* get the status from the top of the fifo (must be read to a variable) */
  301. __IO uint32_t rx_status = USB_GRSTATP;
  302. /* disable the rx fifo non-empty interrupt */
  303. USB_GINTEN &= ~GINTEN_RXFNEIE;
  304. endp_num = (uint8_t)(rx_status & GRSTATRP_EPNUM);
  305. bcount = (rx_status & GRSTATRP_BCOUNT) >> 4U;
  306. data_pid = (uint8_t)((rx_status & GRSTATRP_DPID) >> 15U);
  307. /* ensure no-DMA mode can work */
  308. packet_num = USB_DOEPxLEN((uint16_t)endp_num) & DEPLEN_PCNT;
  309. if ((1U == endp_num) && (0U == packet_num)) {
  310. uint32_t devepctl = USB_DOEPxCTL((uint16_t)endp_num);
  311. devepctl |= DEPCTL_SNAK;
  312. devepctl &= ~DEPCTL_EPEN;
  313. devepctl &= ~DEPCTL_EPD;
  314. USB_DOEPxCTL((uint16_t)endp_num) = devepctl;
  315. }
  316. ep = &pudev->dev.out_ep[endp_num];
  317. switch ((rx_status & GRSTATRP_RPCKST) >> 17U) {
  318. case RXSTAT_GOUT_NAK:
  319. break;
  320. case RXSTAT_DATA_UPDT:
  321. if (bcount > 0U) {
  322. usb_fifo_read(ep->xfer_buff, (uint16_t)bcount);
  323. ep->xfer_buff += bcount;
  324. ep->xfer_count += bcount;
  325. }
  326. break;
  327. case RXSTAT_XFER_COMP:
  328. break;
  329. case RXSTAT_SETUP_COMP:
  330. break;
  331. case RXSTAT_SETUP_UPDT:
  332. *(uint32_t *)0x5000081CU |= 0x00020000U;
  333. if ((0U == endp_num) && (8U == bcount) && (DPID_DATA0 == data_pid)) {
  334. /* copy the setup packet received in fifo into the setup buffer in ram */
  335. usb_fifo_read(pudev->dev.setup_packet, 8U);
  336. ep->xfer_count += bcount;
  337. }
  338. break;
  339. default:
  340. break;
  341. }
  342. /* enable the rx fifo non-empty interrupt */
  343. USB_GINTEN |= GINTEN_RXFNEIE;
  344. return 1U;
  345. }
  346. /*!
  347. \brief handle USB reset interrupt
  348. \param[in] pudev: pointer to usb device instance
  349. \param[out] none
  350. \retval status
  351. */
  352. static uint32_t usbd_intf_reset(usb_core_handle_struct *pudev)
  353. {
  354. uint8_t i = 0U;
  355. usb_ep_struct *ep;
  356. /* clear the remote wakeup signaling */
  357. USB_DCTL &= ~DCTL_RWKUP;
  358. /* flush the tx fifo */
  359. usb_txfifo_flush(pudev, 0U);
  360. for (i = 0U; i < pudev->cfg.dev_endp_num; i++) {
  361. USB_DIEPxINTF((uint16_t)i) = 0xFFU;
  362. USB_DOEPxINTF((uint16_t)i) = 0xFFU;
  363. }
  364. /* clear all pending device endpoint interrupts */
  365. USB_DAEPINT = 0xFFFFFFFFU;
  366. /* enable endpoint 0 interrupts */
  367. USB_DAEPINTEN &= ~DAEPINTEN_OEPIE;
  368. USB_DAEPINTEN &= ~DAEPINTEN_IEPIE;
  369. USB_DAEPINTEN = (1U << 16) | 1U;
  370. /* enable out endpoint interrupts */
  371. USB_DOEPINTEN = DOEPINTEN_STPFEN | DOEPINTEN_TFEN | DOEPINTEN_EPDISEN;
  372. /* enable in endpoint interrupts */
  373. USB_DIEPINTEN = DIEPINTEN_TFEN | DIEPINTEN_CITOEN | DIEPINTEN_EPDISEN;
  374. /* reset device address */
  375. USB_DCFG &= ~DCFG_DAR;
  376. USB_DCFG |= 0U << 4U;
  377. /* configure endpoint 0 to receive setup packets */
  378. usb_ep0_startout(pudev);
  379. /* clear usb reset interrupt */
  380. USB_GINTF = GINTF_RST;
  381. /* open EP0 IN */
  382. ep = &pudev->dev.in_ep[0];
  383. USB_DIEPxCTL(0U) &= ~DEP0CTL_MPL;
  384. USB_DIEPxCTL(0U) &= ~DEPCTL_EPTYPE;
  385. USB_DIEPxCTL(0U) &= ~DIEPCTL_TXFNUM;
  386. if (!(USB_DIEPxCTL(0U) & DEP0CTL_EPACT)) {
  387. USB_DIEPxCTL(0U) |= USB_MAX_EP0_SIZE;
  388. USB_DIEPxCTL(0U) |= (USB_EPTYPE_CTRL << 18U);
  389. USB_DIEPxCTL(0U) |= DEP0CTL_EPACT;
  390. }
  391. ep->endp_mps = USB_MAX_EP0_SIZE;
  392. ep->endp_type = USB_EPTYPE_CTRL;
  393. /* open EP0 OUT */
  394. ep = &pudev->dev.out_ep[0];
  395. USB_DOEPxCTL(0U) &= ~DEP0CTL_MPL;
  396. USB_DOEPxCTL(0U) &= ~DEPCTL_EPTYPE;
  397. if (!(USB_DOEPxCTL(0U) & DEP0CTL_EPACT)) {
  398. USB_DOEPxCTL(0U) |= USB_MAX_EP0_SIZE;
  399. USB_DOEPxCTL(0U) |= (USB_EPTYPE_CTRL << 18U);
  400. USB_DOEPxCTL(0U) |= DEP0CTL_EPACT;
  401. }
  402. ep->endp_mps = USB_MAX_EP0_SIZE;
  403. ep->endp_type = USB_EPTYPE_CTRL;
  404. pudev->dev.status = USB_STATUS_DEFAULT;
  405. return 1U;
  406. }
  407. /*!
  408. \brief handle enumeration finish interrupt
  409. \param[in] pudev: pointer to usb device instance
  410. \param[out] none
  411. \retval status
  412. */
  413. static uint32_t usbd_intf_enumfinish(usb_core_handle_struct *pudev)
  414. {
  415. uint8_t enum_speed = (uint8_t)((USB_DSTAT & DSTAT_ES) >> 1U);
  416. /* set the max packet size of devie in endpoint based on the enumeration speed */
  417. USB_DIEPxCTL(0U) |= EP0_MAXLEN[enum_speed];
  418. /* clear global in NAK */
  419. USB_DCTL &= ~DCTL_CGINAK;
  420. USB_DCTL |= DCTL_CGINAK;
  421. /* set USB turn-around time based on device speed and PHY interface */
  422. if (USB_SPEED_HIGH == USB_SPEED[enum_speed]) {
  423. pudev->cfg.core_speed = USB_CORE_SPEED_HIGH;
  424. pudev->cfg.max_packet_size = USBHS_MAX_PACKET_SIZE;
  425. USB_GUSBCS &= ~GUSBCS_UTT;
  426. USB_GUSBCS |= 0x09U << 10;
  427. } else {
  428. pudev->cfg.core_speed = USB_CORE_SPEED_FULL;
  429. pudev->cfg.max_packet_size = USBFS_MAX_PACKET_SIZE;
  430. USB_GUSBCS &= ~GUSBCS_UTT;
  431. USB_GUSBCS |= 0x05U << 10;
  432. }
  433. /* clear interrupt */
  434. USB_GINTF = GINTF_ENUMF;
  435. return 1U;
  436. }
  437. /*!
  438. \brief handle the ISO in incomplete interrupt
  439. \param[in] pudev: pointer to usb device instance
  440. \param[out] none
  441. \retval status
  442. */
  443. static uint32_t usbd_intf_isoinincomplete(usb_core_handle_struct *pudev)
  444. {
  445. /* clear interrupt */
  446. USB_GINTF = GINTF_ISOINCIF;
  447. return 1U;
  448. }
  449. /*!
  450. \brief handle the ISO OUT incomplete interrupt
  451. \param[in] pudev: pointer to usb device instance
  452. \param[out] none
  453. \retval status
  454. */
  455. static uint32_t usbd_intf_isooutincomplete(usb_core_handle_struct *pudev)
  456. {
  457. /* clear interrupt */
  458. USB_GINTF = GINTF_ISOONCIF;
  459. return 1U;
  460. }
  461. /*!
  462. \brief check FIFO for the next packet to be loaded
  463. \param[in] pudev: pointer to usb device instance
  464. \param[in] ep_id: endpoint identifier which is in (0..3)
  465. \param[out] none
  466. \retval status
  467. */
  468. static uint32_t usbd_emptytxfifo_write(usb_core_handle_struct *pudev, uint8_t ep_num)
  469. {
  470. uint32_t len = 0U, word_len = 0U, fifo_empty_mask = 0U;
  471. usb_ep_struct *ep;
  472. ep = &pudev->dev.in_ep[ep_num];
  473. len = ep->xfer_len - ep->xfer_count;
  474. if (len > ep->endp_mps) {
  475. len = ep->endp_mps;
  476. }
  477. word_len = (len + 3U) / 4U;
  478. while (((USB_DIEPxTFSTAT((uint16_t)ep_num) & DIEPTFSTAT_IEPTFS) > word_len) &&
  479. (ep->xfer_count < ep->xfer_len)) {
  480. /* write the FIFO */
  481. len = ep->xfer_len - ep->xfer_count;
  482. if (len > ep->endp_mps) {
  483. len = ep->endp_mps;
  484. }
  485. word_len = (len + 3U) / 4U;
  486. usb_fifo_write (ep->xfer_buff, ep_num, (uint16_t)len);
  487. ep->xfer_buff += len;
  488. ep->xfer_count += len;
  489. if(ep->xfer_len == ep->xfer_count) {
  490. fifo_empty_mask = 0x1U << ep_num;
  491. USB_DIEPFEINTEN &= ~fifo_empty_mask;
  492. }
  493. }
  494. return 1U;
  495. }
  496. #ifdef VBUS_SENSING_ENABLED
  497. /*!
  498. \brief indicates that the USB_OTG controller has detected a connection
  499. \param[in] pudev: pointer to usb device instance
  500. \param[out] none
  501. \retval status
  502. */
  503. static uint32_t usbd_intf_sessionrequest(usb_core_handle_struct *pudev)
  504. {
  505. pudev->dev.connection_status = 1U;
  506. /* clear the interrupt bit */
  507. USB_GINTF = GINTF_SESIF;
  508. return 1;
  509. }
  510. /*!
  511. \brief indicates that the USB_OTG controller has detected an OTG event
  512. \param[in] pudev: pointer to usb device instance
  513. \param[out] none
  514. \retval status
  515. */
  516. static uint32_t usbd_intf_otg(usb_core_handle_struct *pudev)
  517. {
  518. if (USB_GOTGINTF & GOTGINTF_SESEND) {
  519. pudev->dev.class_deinit(pudev, 0);
  520. pudev->dev.connection_status = 0;
  521. }
  522. /* clear OTG interrupt */
  523. USB_GOTGINTF |= GOTGINTF_SESEND;
  524. return 1;
  525. }
  526. #endif /* VBUS_SENSING_ENABLED */