usbd_int.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*!
  2. \file usbd_int.c
  3. \brief USB device power interrupt routines
  4. */
  5. /*
  6. Copyright (C) 2017 GigaDevice
  7. 2017-02-10, V1.0.0, firmware for GD32F30x
  8. */
  9. #include "usbd_int.h"
  10. extern uint32_t g_interrupt_mask;
  11. extern __IO uint8_t g_suspend_enabled;
  12. extern __IO uint8_t g_remote_wakeup_on;
  13. extern __IO uint8_t g_ESOF_count;
  14. #ifdef LPM_ENABLED
  15. __IO uint32_t L1_remote_wakeup = 0U;
  16. __IO uint32_t L1_resume = 0U;
  17. __IO uint32_t besl = 0U;
  18. #endif /* LPM_ENABLED */
  19. static uint8_t usbd_intf_lpst (usbd_core_handle_struct *pudev);
  20. static uint8_t usbd_intf_sof (usbd_core_handle_struct *pudev);
  21. static uint8_t usbd_intf_esof (usbd_core_handle_struct *pudev);
  22. static uint8_t usbd_intf_reset (usbd_core_handle_struct *pudev);
  23. static uint8_t usbd_intf_suspend (usbd_core_handle_struct *pudev);
  24. static uint8_t usbd_intf_wakeup (usbd_core_handle_struct *pudev);
  25. /*!
  26. \brief USB interrupt events service routine
  27. \param[in] none
  28. \param[out] none
  29. \retval none
  30. */
  31. void usbd_isr (void)
  32. {
  33. __IO uint16_t interrupt_flag = 0U;
  34. __IO uint16_t ctlr = 0U;
  35. interrupt_flag = USBD_REG_GET(USBD_INTF);
  36. if (g_interrupt_mask & INTF_STIF & interrupt_flag) {
  37. /* the endpoint successful transfer interrupt service */
  38. usbd_intf_lpst(&usb_device_dev);
  39. }
  40. if (g_interrupt_mask & INTF_WKUPIF & interrupt_flag) {
  41. /* clear wakeup interrupt flag in INTF */
  42. USBD_REG_SET(USBD_INTF, (uint16_t)CLR_WKUPIF);
  43. /* USB wakeup interrupt handle */
  44. usbd_intf_wakeup(&usb_device_dev);
  45. #ifdef LPM_ENABLED
  46. /* clear L1 remote wakeup flag */
  47. L1_remote_wakeup = 0;
  48. #endif /* LPM_ENABLED */
  49. }
  50. if (g_interrupt_mask & INTF_SPSIF & interrupt_flag) {
  51. if(!(USBD_REG_GET(USBD_CTL) & CTL_RSREQ)) {
  52. /* process library core layer suspend routine*/
  53. usbd_intf_suspend(&usb_device_dev);
  54. /* clear of suspend interrupt flag bit must be done after setting of CTLR_SETSPS */
  55. USBD_REG_SET(USBD_INTF, (uint16_t)CLR_SPSIF);
  56. }
  57. }
  58. if (g_interrupt_mask & INTF_SOFIF & interrupt_flag) {
  59. /* clear SOF interrupt flag in INTF */
  60. USBD_REG_SET(USBD_INTF, (uint16_t)CLR_SOFIF);
  61. /* USB SOF interrupt handle */
  62. usbd_intf_sof(&usb_device_dev);
  63. }
  64. if (g_interrupt_mask & INTF_ESOFIF & interrupt_flag) {
  65. /* clear ESOF interrupt flag in INTF */
  66. USBD_REG_SET(USBD_INTF, (uint16_t)CLR_ESOFIF);
  67. /* USB ESOF interrupt handle */
  68. usbd_intf_esof(&usb_device_dev);
  69. }
  70. if (g_interrupt_mask & INTF_RSTIF & interrupt_flag) {
  71. /* clear reset interrupt flag in INTF */
  72. USBD_REG_SET(USBD_INTF, (uint16_t)CLR_RSTIF);
  73. /* USB reset interrupt handle */
  74. usbd_intf_reset(&usb_device_dev);
  75. }
  76. #ifdef LPM_ENABLED
  77. if (g_interrupt_mask & INTF_L1REQ & interrupt_flag) {
  78. /* clear L1 ST bit in LPM INTF */
  79. USBD_REG_SET(USBD_INTF, CLR_L1REQ);
  80. /* read BESL field from subendpoint0 register which coressponds to HIRD parameter in LPM spec */
  81. besl = (USBD_REG_GET(USBD_LPMCS) & LPMCS_BLSTAT) >> 4;
  82. /* read BREMOTEWAKE bit from subendpoint0 register which corresponding to bRemoteWake bit in LPM request */
  83. L1_remote_wakeup = (USBD_REG_GET(USBD_LPMCS) & LPMCS_REMWK) >> 8;
  84. /* process USB device core layer suspend routine */
  85. /* enter USB model in suspend and system in low power mode (DEEP_SLEEP mode) */
  86. usbd_intf_suspend(&usb_device_dev);
  87. }
  88. #endif /* LPM_ENABLED */
  89. }
  90. /*!
  91. \brief handle USB low priority successful transfer event
  92. \param[in] pudev: pointer to USB device instance
  93. \param[out] none
  94. \retval USB device operation status
  95. */
  96. static uint8_t usbd_intf_lpst (usbd_core_handle_struct *pudev)
  97. {
  98. uint8_t ep_num = 0U;
  99. __IO uint16_t int_status = 0U;
  100. __IO uint16_t ep_value = 0U;
  101. usb_ep_struct *ep = NULL;
  102. /* wait till interrupts are not pending */
  103. while (0U != ((int_status = USBD_REG_GET(USBD_INTF)) & (uint16_t)INTF_STIF)) {
  104. /* get endpoint number and the value of control and state register */
  105. ep_num = (uint8_t)(int_status & INTF_EPNUM);
  106. ep_value = USBD_REG_GET(USBD_EPxCS(ep_num));
  107. if (0U == (int_status & INTF_DIR)) {
  108. /* handle the in direction transaction */
  109. ep = &(pudev->in_ep[ep_num]);
  110. if (0U != (ep_value & EPxCS_TX_ST)) {
  111. /* clear successful transmit interrupt flag */
  112. USBD_ENDP_TX_STAT_CLEAR(ep_num);
  113. /* just handle single buffer situation */
  114. ep->trs_count = (pbuf_reg + ep_num)->tx_count & EPTCNT_CNT;
  115. /* maybe mutiple packets */
  116. ep->trs_buf += ep->trs_count;
  117. usbd_in_transaction(pudev, ep_num);
  118. }
  119. } else {
  120. /* handle the out direction transaction */
  121. uint16_t count = 0U;
  122. ep = &(pudev->out_ep[ep_num]);
  123. if (0U != (ep_value & EPxCS_RX_ST)) {
  124. /* clear successful receive interrupt flag */
  125. USBD_ENDP_RX_STAT_CLEAR(ep_num);
  126. count = (pbuf_reg + ep_num)->rx_count & (uint16_t)EPRCNT_CNT;
  127. if (0U != count) {
  128. if (0U != (ep_value & EPxCS_SETUP)) {
  129. /* handle setup packet */
  130. usbd_ep_data_read(&(pudev->setup_packet[0]), pbuf_reg->rx_addr, count);
  131. /* enter setup status */
  132. usbd_setup_transaction(pudev);
  133. return USBD_OK;
  134. } else {
  135. usbd_ep_data_read(ep->trs_buf, (pbuf_reg + ep_num)->rx_addr, count);
  136. }
  137. }
  138. /* maybe mutiple packets */
  139. ep->trs_count += count;
  140. ep->trs_buf += count;
  141. ep->trs_len -= count;
  142. if ((0U == ep->trs_len) || (count < ep->maxpacket)) {
  143. /* enter data OUT status */
  144. usbd_out_transaction(pudev, ep_num);
  145. ep->trs_count = 0U;
  146. } else {
  147. usbd_ep_rx(pudev, ep_num, ep->trs_buf, (uint16_t)ep->trs_len);
  148. }
  149. }
  150. }
  151. }
  152. return USBD_OK;
  153. }
  154. /*!
  155. \brief handle USB SOF event
  156. \param[in] pudev: pointer to USB device instance
  157. \param[out] none
  158. \retval USB device operation status
  159. */
  160. static uint8_t usbd_intf_sof (usbd_core_handle_struct *pudev)
  161. {
  162. /* if necessary, user can add code here */
  163. if (NULL != usbd_int_fops) {
  164. usbd_int_fops->SOF(pudev);
  165. }
  166. return USBD_OK;
  167. }
  168. /*!
  169. \brief handle USB expect SOF event
  170. \param[in] pudev: pointer to USB device instance
  171. \param[out] none
  172. \retval USB device operation status
  173. */
  174. static uint8_t usbd_intf_esof (usbd_core_handle_struct *pudev)
  175. {
  176. /* control resume time by ESOFs */
  177. if (g_ESOF_count > 0U) {
  178. g_ESOF_count--;
  179. if (0U == g_ESOF_count) {
  180. if (1U == g_remote_wakeup_on) {
  181. USBD_REG_SET(USBD_CTL, USBD_REG_GET(USBD_CTL) & ~CTL_RSREQ);
  182. g_remote_wakeup_on = 0U;
  183. } else {
  184. USBD_REG_SET(USBD_CTL, USBD_REG_GET(USBD_CTL) | CTL_RSREQ);
  185. g_ESOF_count = 3U;
  186. g_remote_wakeup_on = 1U;
  187. }
  188. }
  189. }
  190. return USBD_OK;
  191. }
  192. /*!
  193. \brief handle USB reset event
  194. \param[in] pudev: pointer to USB device instance
  195. \param[out] none
  196. \retval USB device operation status
  197. */
  198. static uint8_t usbd_intf_reset (usbd_core_handle_struct *pudev)
  199. {
  200. // uint8_t i;
  201. g_free_buf_addr = ENDP_BUF_ADDR;
  202. /* configure endpoint 0 buffer */
  203. pbuf_reg->tx_addr = (uint16_t)g_free_buf_addr;
  204. g_free_buf_addr += USBD_EP0_MAX_SIZE;
  205. pbuf_reg->rx_addr = (uint16_t)g_free_buf_addr;
  206. g_free_buf_addr += USBD_EP0_MAX_SIZE;
  207. /* configure endpoint 0 Rx count */
  208. pbuf_reg->rx_count = ((USBD_EP0_MAX_SIZE << 5) - 1) | 0x8000;
  209. pudev->in_ep[EP0].maxpacket = USBD_EP0_MAX_SIZE;
  210. pudev->out_ep[EP0].maxpacket = USBD_EP0_MAX_SIZE;
  211. USBD_REG_SET(USBD_EPxCS(EP0), EP_CONTROL | EPRX_VALID | EPTX_NAK);
  212. /* set device address as default address 0 */
  213. USBD_REG_SET(USBD_DADDR, DADDR_USBEN);
  214. pudev->status = USBD_DEFAULT;
  215. return USBD_OK;
  216. }
  217. /*!
  218. \brief handle USB suspend event
  219. \param[in] pudev: pointer to USB device instance
  220. \param[out] none
  221. \retval USB device operation status
  222. */
  223. static uint8_t usbd_intf_suspend (usbd_core_handle_struct *pudev)
  224. {
  225. /* store the device current status */
  226. pudev->prev_status = pudev->status;
  227. /* set device in suspended state */
  228. pudev->status = USBD_SUSPENDED;
  229. /* enter USB in suspend and mcu system in low power mode */
  230. if (g_suspend_enabled) {
  231. usbd_suspend();
  232. } else {
  233. /* if not possible then resume after xx ms */
  234. g_ESOF_count = 3U;
  235. }
  236. return USBD_OK;
  237. }
  238. /*!
  239. \brief handle USB wakeup event
  240. \param[in] pudev: pointer to USB device instance
  241. \param[out] none
  242. \retval USB device operation status
  243. */
  244. static uint8_t usbd_intf_wakeup (usbd_core_handle_struct *pudev)
  245. {
  246. /* restore the old status */
  247. pudev->status = pudev->prev_status;
  248. #ifdef LPM_ENABLED
  249. if ((0U == g_remote_wakeup_on) && (0U == L1_resume)) {
  250. resume_mcu();
  251. } else if (1U == g_remote_wakeup_on) {
  252. /* no operation */
  253. } else {
  254. L1_resume = 0U;
  255. }
  256. #else
  257. if (0U == g_remote_wakeup_on) {
  258. resume_mcu();
  259. }
  260. #endif /* LPM_ENABLED */
  261. return USBD_OK;
  262. }