usbd_int.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*!
  2. \file usbd_int.c
  3. \brief USB device power 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. extern uint32_t g_interrupt_mask;
  34. extern __IO uint8_t g_suspend_enabled;
  35. extern __IO uint8_t g_remote_wakeup_on;
  36. extern __IO uint8_t g_ESOF_count;
  37. #ifdef LPM_ENABLED
  38. __IO uint32_t L1_remote_wakeup = 0U;
  39. __IO uint32_t L1_resume = 0U;
  40. __IO uint32_t besl = 0U;
  41. #endif /* LPM_ENABLED */
  42. static uint8_t usbd_intf_lpst (usbd_core_handle_struct *pudev);
  43. static uint8_t usbd_intf_sof (usbd_core_handle_struct *pudev);
  44. static uint8_t usbd_intf_esof (usbd_core_handle_struct *pudev);
  45. static uint8_t usbd_intf_reset (usbd_core_handle_struct *pudev);
  46. static uint8_t usbd_intf_suspend (usbd_core_handle_struct *pudev);
  47. static uint8_t usbd_intf_wakeup (usbd_core_handle_struct *pudev);
  48. /*!
  49. \brief USB interrupt events service routine
  50. \param[in] none
  51. \param[out] none
  52. \retval none
  53. */
  54. void usbd_isr (void)
  55. {
  56. __IO uint16_t interrupt_flag = 0U;
  57. __IO uint16_t ctlr = 0U;
  58. interrupt_flag = USBD_REG_GET(USBD_INTF);
  59. if (g_interrupt_mask & INTF_STIF & interrupt_flag) {
  60. /* the endpoint successful transfer interrupt service */
  61. usbd_intf_lpst(&usb_device_dev);
  62. }
  63. if (g_interrupt_mask & INTF_WKUPIF & interrupt_flag) {
  64. /* clear wakeup interrupt flag in INTF */
  65. USBD_REG_SET(USBD_INTF, (uint16_t)CLR_WKUPIF);
  66. /* USB wakeup interrupt handle */
  67. usbd_intf_wakeup(&usb_device_dev);
  68. #ifdef LPM_ENABLED
  69. /* clear L1 remote wakeup flag */
  70. L1_remote_wakeup = 0;
  71. #endif /* LPM_ENABLED */
  72. }
  73. if (g_interrupt_mask & INTF_SPSIF & interrupt_flag) {
  74. if(!(USBD_REG_GET(USBD_CTL) & CTL_RSREQ)) {
  75. /* process library core layer suspend routine*/
  76. usbd_intf_suspend(&usb_device_dev);
  77. /* clear of suspend interrupt flag bit must be done after setting of CTLR_SETSPS */
  78. USBD_REG_SET(USBD_INTF, (uint16_t)CLR_SPSIF);
  79. }
  80. }
  81. if (g_interrupt_mask & INTF_SOFIF & interrupt_flag) {
  82. /* clear SOF interrupt flag in INTF */
  83. USBD_REG_SET(USBD_INTF, (uint16_t)CLR_SOFIF);
  84. /* USB SOF interrupt handle */
  85. usbd_intf_sof(&usb_device_dev);
  86. }
  87. if (g_interrupt_mask & INTF_ESOFIF & interrupt_flag) {
  88. /* clear ESOF interrupt flag in INTF */
  89. USBD_REG_SET(USBD_INTF, (uint16_t)CLR_ESOFIF);
  90. /* USB ESOF interrupt handle */
  91. usbd_intf_esof(&usb_device_dev);
  92. }
  93. if (g_interrupt_mask & INTF_RSTIF & interrupt_flag) {
  94. /* clear reset interrupt flag in INTF */
  95. USBD_REG_SET(USBD_INTF, (uint16_t)CLR_RSTIF);
  96. /* USB reset interrupt handle */
  97. usbd_intf_reset(&usb_device_dev);
  98. }
  99. #ifdef LPM_ENABLED
  100. if (g_interrupt_mask & INTF_L1REQ & interrupt_flag) {
  101. /* clear L1 ST bit in LPM INTF */
  102. USBD_REG_SET(USBD_INTF, CLR_L1REQ);
  103. /* read BESL field from subendpoint0 register which coressponds to HIRD parameter in LPM spec */
  104. besl = (USBD_REG_GET(USBD_LPMCS) & LPMCS_BLSTAT) >> 4;
  105. /* read BREMOTEWAKE bit from subendpoint0 register which corresponding to bRemoteWake bit in LPM request */
  106. L1_remote_wakeup = (USBD_REG_GET(USBD_LPMCS) & LPMCS_REMWK) >> 8;
  107. /* process USB device core layer suspend routine */
  108. /* enter USB model in suspend and system in low power mode (DEEP_SLEEP mode) */
  109. usbd_intf_suspend(&usb_device_dev);
  110. }
  111. #endif /* LPM_ENABLED */
  112. }
  113. /*!
  114. \brief handle USB high priority successful transfer event
  115. \param[in] pudev: pointer to USB device instance
  116. \param[out] none
  117. \retval USB device operation status
  118. */
  119. uint8_t usbd_intf_hpst (usbd_core_handle_struct *pudev)
  120. {
  121. uint8_t ep_num = 0U;
  122. __IO uint16_t int_status = 0U;
  123. __IO uint16_t ep_value = 0U;
  124. usb_ep_struct *ep = NULL;
  125. /* wait till interrupts are not pending */
  126. while (0U != ((int_status = USBD_INTF) & (uint16_t)INTF_STIF)) {
  127. /* get endpoint number and the value of control and state register */
  128. ep_num = (uint8_t)(int_status & INTF_EPNUM);
  129. ep_value = USBD_EPxCS(ep_num);
  130. if (0U == (int_status & INTF_DIR)) {
  131. /* handle the in direction transaction */
  132. ep = &(pudev->in_ep[ep_num]);
  133. if (0U != (ep_value & EPxCS_TX_ST)) {
  134. /* clear successful transmit interrupt flag */
  135. USBD_ENDP_TX_STAT_CLEAR(ep_num);
  136. if (ep_value & EPxCS_TX_DTG) {
  137. /* just handle single buffer situation */
  138. ep->trs_count = (pbuf_reg + ep_num)->tx_count & EPTCNT_CNT;
  139. }
  140. /* maybe mutiple packets */
  141. ep->trs_buf += ep->trs_count;
  142. usbd_in_transaction(pudev, ep_num);
  143. }
  144. } else {
  145. /* handle the out direction transaction */
  146. uint16_t count = 0U;
  147. ep = &(pudev->out_ep[ep_num]);
  148. if (0U != (ep_value & EPxCS_RX_ST)) {
  149. /* clear successful receive interrupt flag */
  150. USBD_ENDP_RX_STAT_CLEAR(ep_num);
  151. if (ep_value & EPxCS_TX_DTG) {
  152. count = (pbuf_reg + ep_num)->tx_count & (uint16_t)EPRCNT_CNT;
  153. if (0U != count) {
  154. usbd_ep_data_read(ep->trs_buf, (pbuf_reg + ep_num)->tx_addr, count);
  155. }
  156. } else {
  157. count = (pbuf_reg + ep_num)->rx_count & (uint16_t)EPRCNT_CNT;
  158. if (0U != count) {
  159. usbd_ep_data_read(ep->trs_buf, (pbuf_reg + ep_num)->rx_addr, count);
  160. }
  161. }
  162. user_buffer_free(ep_num, DBUF_EP_OUT);
  163. /* maybe mutiple packets */
  164. ep->trs_count += count;
  165. ep->trs_buf += count;
  166. ep->trs_len -= count;
  167. if ((0U == ep->trs_len) || (count < ep->maxpacket)) {
  168. USBD_ENDP_TX_STATUS_SET(ep_num, EPRX_NAK);
  169. /* enter data OUT status */
  170. usbd_out_transaction(pudev, ep_num);
  171. ep->trs_count = 0U;
  172. }
  173. }
  174. }
  175. }
  176. return USBD_OK;
  177. }
  178. /*!
  179. \brief handle USB low priority successful transfer event
  180. \param[in] pudev: pointer to USB device instance
  181. \param[out] none
  182. \retval USB device operation status
  183. */
  184. static uint8_t usbd_intf_lpst (usbd_core_handle_struct *pudev)
  185. {
  186. uint8_t ep_num = 0U;
  187. __IO uint16_t int_status = 0U;
  188. __IO uint16_t ep_value = 0U;
  189. usb_ep_struct *ep = NULL;
  190. /* wait till interrupts are not pending */
  191. while (0U != ((int_status = USBD_REG_GET(USBD_INTF)) & (uint16_t)INTF_STIF)) {
  192. /* get endpoint number and the value of control and state register */
  193. ep_num = (uint8_t)(int_status & INTF_EPNUM);
  194. ep_value = USBD_REG_GET(USBD_EPxCS(ep_num));
  195. if (0U == (int_status & INTF_DIR)) {
  196. /* handle the in direction transaction */
  197. ep = &(pudev->in_ep[ep_num]);
  198. if (0U != (ep_value & EPxCS_TX_ST)) {
  199. /* clear successful transmit interrupt flag */
  200. USBD_ENDP_TX_STAT_CLEAR(ep_num);
  201. /* just handle single buffer situation */
  202. ep->trs_count = (pbuf_reg + ep_num)->tx_count & EPTCNT_CNT;
  203. /* maybe mutiple packets */
  204. ep->trs_buf += ep->trs_count;
  205. usbd_in_transaction(pudev, ep_num);
  206. }
  207. } else {
  208. /* handle the out direction transaction */
  209. uint16_t count = 0U;
  210. ep = &(pudev->out_ep[ep_num]);
  211. if (0U != (ep_value & EPxCS_RX_ST)) {
  212. /* clear successful receive interrupt flag */
  213. USBD_ENDP_RX_STAT_CLEAR(ep_num);
  214. count = (pbuf_reg + ep_num)->rx_count & (uint16_t)EPRCNT_CNT;
  215. if (0U != count) {
  216. if (0U != (ep_value & EPxCS_SETUP)) {
  217. /* handle setup packet */
  218. usbd_ep_data_read(&(pudev->setup_packet[0]), pbuf_reg->rx_addr, count);
  219. /* enter setup status */
  220. usbd_setup_transaction(pudev);
  221. return USBD_OK;
  222. } else {
  223. usbd_ep_data_read(ep->trs_buf, (pbuf_reg + ep_num)->rx_addr, count);
  224. }
  225. }
  226. /* maybe mutiple packets */
  227. ep->trs_count += count;
  228. ep->trs_buf += count;
  229. ep->trs_len -= count;
  230. if ((0U == ep->trs_len) || (count < ep->maxpacket)) {
  231. /* enter data OUT status */
  232. usbd_out_transaction(pudev, ep_num);
  233. ep->trs_count = 0U;
  234. } else {
  235. usbd_ep_rx(pudev, ep_num, ep->trs_buf, (uint16_t)ep->trs_len);
  236. }
  237. }
  238. }
  239. }
  240. return USBD_OK;
  241. }
  242. /*!
  243. \brief handle USB SOF event
  244. \param[in] pudev: pointer to USB device instance
  245. \param[out] none
  246. \retval USB device operation status
  247. */
  248. static uint8_t usbd_intf_sof (usbd_core_handle_struct *pudev)
  249. {
  250. /* if necessary, user can add code here */
  251. if (NULL != usbd_int_fops) {
  252. usbd_int_fops->SOF(pudev);
  253. }
  254. return USBD_OK;
  255. }
  256. /*!
  257. \brief handle USB expect SOF event
  258. \param[in] pudev: pointer to USB device instance
  259. \param[out] none
  260. \retval USB device operation status
  261. */
  262. static uint8_t usbd_intf_esof (usbd_core_handle_struct *pudev)
  263. {
  264. /* control resume time by ESOFs */
  265. if (g_ESOF_count > 0U) {
  266. g_ESOF_count--;
  267. if (0U == g_ESOF_count) {
  268. if (1U == g_remote_wakeup_on) {
  269. USBD_REG_SET(USBD_CTL, USBD_REG_GET(USBD_CTL) & ~CTL_RSREQ);
  270. g_remote_wakeup_on = 0U;
  271. } else {
  272. USBD_REG_SET(USBD_CTL, USBD_REG_GET(USBD_CTL) | CTL_RSREQ);
  273. g_ESOF_count = 3U;
  274. g_remote_wakeup_on = 1U;
  275. }
  276. }
  277. }
  278. return USBD_OK;
  279. }
  280. /*!
  281. \brief handle USB reset event
  282. \param[in] pudev: pointer to USB device instance
  283. \param[out] none
  284. \retval USB device operation status
  285. */
  286. static uint8_t usbd_intf_reset (usbd_core_handle_struct *pudev)
  287. {
  288. uint8_t i;
  289. g_free_buf_addr = ENDP_BUF_ADDR;
  290. /* configure endpoint 0 buffer */
  291. pbuf_reg->tx_addr = (uint16_t)g_free_buf_addr;
  292. g_free_buf_addr += USBD_EP0_MAX_SIZE;
  293. pbuf_reg->rx_addr = (uint16_t)g_free_buf_addr;
  294. g_free_buf_addr += USBD_EP0_MAX_SIZE;
  295. /* configure endpoint 0 Rx count */
  296. if (USBD_EP0_MAX_SIZE > 62U) {
  297. pbuf_reg->rx_count = ((USBD_EP0_MAX_SIZE << 5U) - 1U) | 0x8000U;
  298. } else {
  299. pbuf_reg->rx_count = ((USBD_EP0_MAX_SIZE + 1U) & ~1U) << 9U;
  300. }
  301. pudev->in_ep[EP0].maxpacket = USBD_EP0_MAX_SIZE;
  302. pudev->out_ep[EP0].maxpacket = USBD_EP0_MAX_SIZE;
  303. /* set endpoints address */
  304. for (i = 0U; i < EP_COUNT; i++)
  305. {
  306. _EP_ADDR_SET(i, i);
  307. }
  308. /* set device address as default address 0 */
  309. USBD_REG_SET(USBD_DADDR, DADDR_USBEN);
  310. /* clear endpoint 0 register */
  311. USBD_REG_SET(USBD_EPxCS(EP0), USBD_EPxCS(EP0));
  312. USBD_REG_SET(USBD_EPxCS(EP0), EP_CONTROL | EPRX_VALID | EPTX_NAK);
  313. pudev->status = USBD_DEFAULT;
  314. return USBD_OK;
  315. }
  316. /*!
  317. \brief handle USB suspend event
  318. \param[in] pudev: pointer to USB device instance
  319. \param[out] none
  320. \retval USB device operation status
  321. */
  322. static uint8_t usbd_intf_suspend (usbd_core_handle_struct *pudev)
  323. {
  324. /* store the device current status */
  325. pudev->prev_status = pudev->status;
  326. /* set device in suspended state */
  327. pudev->status = USBD_SUSPENDED;
  328. /* enter USB in suspend and mcu system in low power mode */
  329. if (g_suspend_enabled) {
  330. usbd_suspend();
  331. } else {
  332. /* if not possible then resume after xx ms */
  333. g_ESOF_count = 3U;
  334. }
  335. return USBD_OK;
  336. }
  337. /*!
  338. \brief handle USB wakeup event
  339. \param[in] pudev: pointer to USB device instance
  340. \param[out] none
  341. \retval USB device operation status
  342. */
  343. static uint8_t usbd_intf_wakeup (usbd_core_handle_struct *pudev)
  344. {
  345. /* restore the old status */
  346. pudev->status = pudev->prev_status;
  347. #ifdef LPM_ENABLED
  348. if ((0U == g_remote_wakeup_on) && (0U == L1_resume)) {
  349. resume_mcu();
  350. } else if (1U == g_remote_wakeup_on) {
  351. /* no operation */
  352. } else {
  353. L1_resume = 0U;
  354. }
  355. #else
  356. if (0U == g_remote_wakeup_on) {
  357. resume_mcu();
  358. }
  359. #endif /* LPM_ENABLED */
  360. return USBD_OK;
  361. }