usbd_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /*!
  2. \file usbd_core.c
  3. \brief USB device mode core driver
  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_core.h"
  33. #include "usbd_std.h"
  34. /*!
  35. \brief initailizes the USB device-mode handler stack
  36. \param[in] pudev: pointer to usb device instance
  37. \param[in] core_id: USB core ID
  38. \param[out] none
  39. \retval none
  40. */
  41. void usbd_init (usb_core_handle_struct *pudev, usb_core_id_enum core_id)
  42. {
  43. /* select USB core */
  44. usb_core_select (pudev, core_id);
  45. pudev->dev.status = USB_STATUS_DEFAULT;
  46. /* disable USB global interrupt */
  47. USB_GLOBAL_INT_DISABLE();
  48. /* init the core (common init.) */
  49. usb_core_init(pudev);
  50. /* force device mode*/
  51. usb_mode_set(pudev, DEVICE_MODE);
  52. /* set device disconnect */
  53. USB_SOFT_DISCONNECT_ENABLE();
  54. if ((void *)0 != pudev->mdelay) {
  55. pudev->mdelay(3U);
  56. }
  57. /* init device */
  58. usb_devcore_init(pudev);
  59. /* set device Connect */
  60. USB_SOFT_DISCONNECT_DISABLE();
  61. if ((void *)0 != pudev->mdelay) {
  62. pudev->mdelay(3U);
  63. }
  64. /* enable USB global interrupt */
  65. USB_GLOBAL_INT_ENABLE();
  66. }
  67. /*!
  68. \brief endpoint initialization
  69. \param[in] pudev: pointer to usb device instance
  70. \param[in] ep_desc: pointer to usb endpoint descriptor
  71. \param[out] none
  72. \retval none
  73. */
  74. void usbd_ep_init (usb_core_handle_struct *pudev, const usb_descriptor_endpoint_struct *ep_desc)
  75. {
  76. usb_ep_struct *ep;
  77. usb_dir_enum ep_dir;
  78. uint32_t devepinten = 0U;
  79. uint32_t devepctl = 0U;
  80. uint8_t ep_num = ep_desc->bEndpointAddress & 0x7FU;
  81. uint8_t ep_type = ep_desc->bmAttributes & USB_EPTYPE_MASK;
  82. uint16_t ep_mps = ep_desc->wMaxPacketSize;
  83. if (ep_desc->bEndpointAddress >> 7U) {
  84. ep = &pudev->dev.in_ep[ep_num];
  85. devepinten |= 1U << ep_num;
  86. devepctl = USB_DIEPxCTL((uint16_t)ep_num);
  87. ep_dir = USB_TX;
  88. } else {
  89. ep = &pudev->dev.out_ep[ep_num];
  90. devepinten |= (1U << ep_num) << 16U;
  91. devepctl = USB_DOEPxCTL((uint16_t)ep_num);
  92. ep_dir = USB_RX;
  93. }
  94. /* if the endpoint is not active, need change the endpoint control register */
  95. if (!(devepctl & DEPCTL_EPACT)) {
  96. devepctl &= ~DEPCTL_MPL;
  97. devepctl |= ep_mps;
  98. devepctl &= ~DEPCTL_EPTYPE;
  99. devepctl |= (uint32_t)ep_type << 18U;
  100. if (USB_TX == ep_dir) {
  101. devepctl &= ~DIEPCTL_TXFNUM;
  102. devepctl |= (uint32_t)ep_num << 22U;
  103. }
  104. devepctl |= DEPCTL_SD0PID;
  105. devepctl |= DEPCTL_EPACT;
  106. }
  107. if (USB_TX == ep_dir) {
  108. USB_DIEPxCTL((uint16_t)ep_num) = devepctl;
  109. } else if (USB_RX == ep_dir) {
  110. USB_DOEPxCTL((uint16_t)ep_num) = devepctl;
  111. } else {
  112. /* no operation */
  113. }
  114. ep->endp_mps = ep_mps;
  115. ep->endp_type = ep_type;
  116. /* enable the interrupts for this endpoint */
  117. USB_DAEPINTEN |= devepinten;
  118. }
  119. /*!
  120. \brief endpoint deinitialize
  121. \param[in] pudev: pointer to usb device instance
  122. \param[in] ep_addr: endpoint address
  123. \param[out] none
  124. \retval none
  125. */
  126. void usbd_ep_deinit (usb_core_handle_struct *pudev, uint8_t ep_addr)
  127. {
  128. uint32_t devepinten = 0U;
  129. uint8_t ep_num = ep_addr & 0x7FU;
  130. if (ep_addr >> 7U) {
  131. devepinten |= 1U << ep_num;
  132. USB_DIEPxCTL((uint16_t)ep_num) &= ~DEPCTL_EPACT;
  133. } else {
  134. devepinten |= (1U << ep_num) << 16U;
  135. USB_DOEPxCTL((uint16_t)ep_num) &= ~DEPCTL_EPACT;
  136. }
  137. /* disable the interrupts for this endpoint */
  138. USB_DAEPINTEN &= ~devepinten;
  139. }
  140. /*!
  141. \brief endpoint prepare to receive data
  142. \param[in] pudev: pointer to usb device instance
  143. \param[in] ep_addr: endpoint address
  144. \param[in] pbuf: pointer to buffer
  145. \param[in] buf_len: buffer length
  146. \param[out] none
  147. \retval none
  148. */
  149. void usbd_ep_rx (usb_core_handle_struct *pudev, uint8_t ep_addr, uint8_t *pbuf, uint16_t buf_len)
  150. {
  151. usb_ep_struct *ep;
  152. uint8_t ep_num = ep_addr & 0x7FU;
  153. uint32_t devepctl = 0U, devepxlen = 0U;
  154. ep = &pudev->dev.out_ep[ep_num];
  155. /* setup and start the Xfer */
  156. ep->xfer_buff = pbuf;
  157. ep->xfer_len = buf_len;
  158. ep->xfer_count = 0U;
  159. devepctl = USB_DOEPxCTL((uint16_t)ep_num);
  160. devepxlen = USB_DOEPxLEN((uint16_t)ep_num);
  161. devepxlen &= ~DEPLEN_TLEN;
  162. devepxlen &= ~DEPLEN_PCNT;
  163. /* zero length packet */
  164. if (0U == ep->xfer_len) {
  165. /* set the transfer length to max packet size */
  166. devepxlen |= ep->endp_mps;
  167. /* set the transfer packet count to 1 */
  168. devepxlen |= 1U << 19U;
  169. } else {
  170. if (0U == ep_num) {
  171. /* set the transfer length to max packet size */
  172. devepxlen |= ep->endp_mps;
  173. /* set the transfer packet count to 1 */
  174. devepxlen |= 1U << 19U;
  175. } else {
  176. /* configure the transfer size and packet count as follows:
  177. * pktcnt = N
  178. * xfersize = N * maxpacket
  179. */
  180. devepxlen |= ((ep->xfer_len + ep->endp_mps - 1U) / ep->endp_mps) << 19U;
  181. devepxlen |= ((devepxlen & DEPLEN_PCNT) >> 19U) * ep->endp_mps;
  182. }
  183. }
  184. USB_DOEPxLEN((uint16_t)ep_num) = devepxlen;
  185. if (USB_EPTYPE_ISOC == ep->endp_type) {
  186. if (ep->endp_frame) {
  187. devepctl |= DEPCTL_SODDFRM;
  188. } else {
  189. devepctl |= DEPCTL_SEVNFRM;
  190. }
  191. }
  192. /* enable the endpoint and clear the NAK */
  193. devepctl |= DEPCTL_EPEN | DEPCTL_CNAK;
  194. USB_DOEPxCTL((uint16_t)ep_num) = devepctl;
  195. }
  196. /*!
  197. \brief endpoint prepare to transmit data
  198. \param[in] pudev: pointer to usb device instance
  199. \param[in] ep_addr: endpoint address
  200. \param[in] pbuf: pointer to buffer
  201. \param[in] len: buffer length
  202. \param[out] none
  203. \retval none
  204. */
  205. void usbd_ep_tx (usb_core_handle_struct *pudev, uint8_t ep_addr, uint8_t *pbuf, uint32_t buf_len)
  206. {
  207. usb_ep_struct *ep;
  208. uint8_t ep_num = ep_addr & 0x7FU;
  209. __IO uint32_t devepctl = 0U;
  210. __IO uint32_t deveplen = 0U;
  211. ep = &pudev->dev.in_ep[ep_num];
  212. /* setup and start the transfer */
  213. ep->xfer_buff = pbuf;
  214. ep->xfer_len = buf_len;
  215. ep->xfer_count = 0U;
  216. devepctl = USB_DIEPxCTL((uint16_t)ep_num);
  217. deveplen = USB_DIEPxLEN((uint16_t)ep_num);
  218. /* clear transfer length to 0 */
  219. deveplen &= ~DEPLEN_TLEN;
  220. /* clear transfer packet to 0 */
  221. deveplen &= ~DEPLEN_PCNT;
  222. /* zero length packet */
  223. if (0U == ep->xfer_len) {
  224. /* set transfer packet count to 1 */
  225. deveplen |= 1U << 19U;
  226. } else {
  227. if (0U == ep_num) {
  228. if (ep->xfer_len > ep->endp_mps) {
  229. ep->xfer_len = ep->endp_mps;
  230. }
  231. deveplen |= 1U << 19U;
  232. } else {
  233. deveplen |= ((ep->xfer_len - 1U + ep->endp_mps) / ep->endp_mps) << 19U;
  234. }
  235. /* configure the transfer size and packet count as follows:
  236. * xfersize = N * maxpacket + short_packet
  237. * pktcnt = N + (short_packet exist ? 1 : 0)
  238. */
  239. deveplen |= ep->xfer_len;
  240. if (USB_EPTYPE_ISOC == ep->endp_type) {
  241. deveplen |= DIEPLEN_MCNT & (1U << 29U);
  242. }
  243. }
  244. USB_DIEPxLEN((uint16_t)ep_num) = deveplen;
  245. if (USB_EPTYPE_ISOC == ep->endp_type) {
  246. if (0U == (((USB_DSTAT & DSTAT_FNRSOF) >> 8U) & 0x1U)) {
  247. devepctl |= DEPCTL_SODDFRM;
  248. } else {
  249. devepctl |= DEPCTL_SEVNFRM;
  250. }
  251. }
  252. /* enable the endpoint and clear the NAK */
  253. devepctl |= DEPCTL_EPEN | DEPCTL_CNAK;
  254. USB_DIEPxCTL((uint16_t)ep_num) = devepctl;
  255. if (USB_EPTYPE_ISOC != ep->endp_type) {
  256. /* enable the Tx FIFO empty interrupt for this endpoint */
  257. if (ep->xfer_len > 0U) {
  258. USB_DIEPFEINTEN |= 1U << ep_num;
  259. }
  260. } else {
  261. usb_fifo_write(ep->xfer_buff, ep_num, (uint16_t)ep->xfer_len);
  262. }
  263. }
  264. /*!
  265. \brief transmit data on the control channel
  266. \param[in] pudev: pointer to usb device instance
  267. \param[in] pbuf: pointer to buffer
  268. \param[in] len: buffer length
  269. \param[out] none
  270. \retval usb device operation status
  271. */
  272. usbd_status_enum usbd_ctltx (usb_core_handle_struct *pudev, uint8_t *pbuf, uint16_t len)
  273. {
  274. usbd_status_enum ret = USBD_OK;
  275. pudev->dev.sum_len = len;
  276. pudev->dev.remain_len = len;
  277. pudev->dev.ctl_status = USB_CTRL_DATA_IN;
  278. usbd_ep_tx (pudev, 0U, pbuf, (uint32_t)len);
  279. return ret;
  280. }
  281. /*!
  282. \brief receive data on the control channel
  283. \param[in] pudev: pointer to usb device instance
  284. \param[in] pbuf: pointer to buffer
  285. \param[in] len: buffer length
  286. \param[out] none
  287. \retval usb device operation status
  288. */
  289. usbd_status_enum usbd_ctlrx (usb_core_handle_struct *pudev, uint8_t *pbuf, uint16_t len)
  290. {
  291. pudev->dev.sum_len = len;
  292. pudev->dev.remain_len = len;
  293. pudev->dev.ctl_status = USB_CTRL_DATA_OUT;
  294. usbd_ep_rx (pudev, 0U, pbuf, len);
  295. return USBD_OK;
  296. }
  297. /*!
  298. \brief transmit status on the control channel
  299. \param[in] pudev: pointer to usb device instance
  300. \param[out] none
  301. \retval usb device operation status
  302. */
  303. usbd_status_enum usbd_ctlstatus_tx (usb_core_handle_struct *pudev)
  304. {
  305. pudev->dev.ctl_status = USB_CTRL_STATUS_IN;
  306. usbd_ep_tx (pudev, 0U, NULL, 0U);
  307. usb_ep0_startout(pudev);
  308. return USBD_OK;
  309. }
  310. /*!
  311. \brief receive status on the control channel
  312. \param[in] pudev: pointer to usb device instance
  313. \param[out] none
  314. \retval usb device operation status
  315. */
  316. usbd_status_enum usbd_ctlstatus_rx (usb_core_handle_struct *pudev)
  317. {
  318. pudev->dev.ctl_status = USB_CTRL_STATUS_OUT;
  319. usbd_ep_rx (pudev, 0U, NULL, 0U);
  320. usb_ep0_startout(pudev);
  321. return USBD_OK;
  322. }
  323. /*!
  324. \brief set an endpoint to STALL status
  325. \param[in] pudev: pointer to usb device instance
  326. \param[in] ep_addr: endpoint address
  327. \param[out] none
  328. \retval none
  329. */
  330. void usbd_ep_stall (usb_core_handle_struct *pudev, uint8_t ep_addr)
  331. {
  332. uint8_t ep_num = ep_addr & 0x7FU;
  333. __IO uint32_t devepctl = 0U;
  334. if (ep_addr >> 7U) {
  335. devepctl = USB_DIEPxCTL((uint16_t)ep_num);
  336. /* set the endpoint disable bit */
  337. if (devepctl & DEPCTL_EPEN) {
  338. devepctl |= DEPCTL_EPD;
  339. }
  340. /* set the endpoint stall bit */
  341. devepctl |= DEPCTL_STALL;
  342. USB_DIEPxCTL((uint16_t)ep_num) = devepctl;
  343. } else {
  344. /* set the endpoint stall bit */
  345. USB_DOEPxCTL((uint16_t)ep_num) |= DEPCTL_STALL;
  346. }
  347. }
  348. /*!
  349. \brief clear endpoint stalled status
  350. \param[in] pudev: pointer to usb device instance
  351. \param[in] ep_addr: endpoint address
  352. \param[out] none
  353. \retval none
  354. */
  355. void usbd_ep_clear_stall (usb_core_handle_struct *pudev, uint8_t ep_addr)
  356. {
  357. usb_ep_struct *ep;
  358. uint8_t ep_num = ep_addr & 0x7FU;
  359. __IO uint32_t devepctl = 0U;
  360. if(ep_addr >> 7U){
  361. ep = &pudev->dev.in_ep[ep_num];
  362. devepctl = USB_DIEPxCTL((uint16_t)ep_num);
  363. /* clear the in endpoint stall bits */
  364. devepctl &= ~DEPCTL_STALL;
  365. if ((USB_EPTYPE_INTR == ep->endp_type) || (USB_EPTYPE_BULK == ep->endp_type)) {
  366. devepctl |= DEPCTL_SEVNFRM;
  367. }
  368. USB_DIEPxCTL((uint16_t)ep_num) = devepctl;
  369. } else {
  370. ep = &pudev->dev.out_ep[ep_num];
  371. devepctl = USB_DOEPxCTL((uint16_t)ep_num);
  372. /* clear the out endpoint stall bits */
  373. devepctl &= ~DEPCTL_STALL;
  374. if ((USB_EPTYPE_INTR == ep->endp_type) || (USB_EPTYPE_BULK == ep->endp_type)) {
  375. devepctl |= DEPCTL_SEVNFRM;
  376. }
  377. USB_DOEPxCTL((uint16_t)ep_num) = devepctl;
  378. }
  379. }
  380. /*!
  381. \brief flushes the fifos
  382. \param[in] pudev: pointer to usb device instance
  383. \param[in] ep_addr: endpoint address
  384. \param[out] none
  385. \retval none
  386. */
  387. void usbd_ep_fifo_flush (usb_core_handle_struct *pudev, uint8_t ep_addr)
  388. {
  389. if (ep_addr >> 7U) {
  390. usb_txfifo_flush(pudev, ep_addr & 0x7FU);
  391. } else {
  392. usb_rxfifo_flush(pudev);
  393. }
  394. }
  395. /*!
  396. \brief get the received data length
  397. \param[in] pudev: pointer to usb device instance
  398. \param[in] ep_num: endpoint identifier which is in (0..3)
  399. \param[out] none
  400. \retval received data length
  401. */
  402. uint16_t usbd_rxcount_get (usb_core_handle_struct *pudev, uint8_t ep_num)
  403. {
  404. return (uint16_t)pudev->dev.out_ep[ep_num].xfer_count;
  405. }