usbd_int.c 19 KB

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