usbd_core.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*!
  2. \file usbd_core.c
  3. \brief USB device 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. uint32_t g_interrupt_mask = 0U;
  35. uint32_t g_free_buf_addr = ENDP_BUF_ADDR;
  36. usbd_ep_buf_struct *pbuf_reg = (usbd_ep_buf_struct *)USBD_RAM;
  37. /*!
  38. \brief device core register initialization
  39. \param[in] none
  40. \param[out] none
  41. \retval none
  42. */
  43. void usbd_core_init (usbd_core_handle_struct *pudev)
  44. {
  45. /* disable remote wakeup feature */
  46. pudev->remote_wakeup = 0U;
  47. /* just reset the CLOSE bit */
  48. USBD_REG_SET(USBD_CTL, CTL_SETRST);
  49. /* may be need wait some time(tSTARTUP) ... */
  50. /* clear SETRST bit in USBD_CTL register */
  51. USBD_REG_SET(USBD_CTL, 0U);
  52. /* clear all pending interrupts */
  53. USBD_REG_SET(USBD_INTF, 0U);
  54. /* set allocation buffer address */
  55. USBD_REG_SET(USBD_BADDR, BUFFER_ADDRESS & 0xFFF8U);
  56. g_interrupt_mask = IER_MASK;
  57. #ifdef LPM_ENABLED
  58. /* enable L1REQ interrupt */
  59. USBD_REG_SET(USBD_LPMCS, LPMCS_LPMACK | LPMCS_LPMEN);
  60. #endif /* LPM_ENABLED */
  61. /* enable all interrupts mask bits */
  62. USBD_REG_SET(USBD_CTL, g_interrupt_mask);
  63. }
  64. /*!
  65. \brief free buffer used from application by toggling the SW_BUF byte
  66. \param[in] ep_num: endpoint identifier (0..7)
  67. \param[in] dir: endpoint direction which can be OUT(0) or IN(1)
  68. \param[out] none
  69. \retval None
  70. */
  71. void user_buffer_free (uint8_t ep_num, uint8_t dir)
  72. {
  73. if (DBUF_EP_OUT == dir) {
  74. USBD_SWBUF_RX_TOGGLE(ep_num);
  75. } else if (DBUF_EP_IN == dir) {
  76. USBD_SWBUF_TX_TOGGLE(ep_num);
  77. } else {
  78. /* no operation */
  79. }
  80. }
  81. /*!
  82. \brief device core register configure when stop device
  83. \param[in] none
  84. \param[out] none
  85. \retval none
  86. */
  87. void usbd_core_deinit (void)
  88. {
  89. /* disable all interrupts and set USB reset */
  90. USBD_REG_SET(USBD_CTL, CTL_SETRST);
  91. /* clear all interrupt flags */
  92. USBD_REG_SET(USBD_INTF, 0U);
  93. /* close device */
  94. USBD_REG_SET(USBD_CTL, CTL_SETRST | CTL_CLOSE);
  95. }
  96. /*!
  97. \brief endpoint initialization
  98. \param[in] pudev: pointer to USB core instance
  99. \param[in] buf_kind: kind of buffer
  100. \param[in] pep_desc: pointer to endpoint descriptor
  101. \param[out] none
  102. \retval none
  103. */
  104. void usbd_ep_init (usbd_core_handle_struct *pudev, usbd_epkind_enum buf_kind, void *ep_desc)
  105. {
  106. usb_descriptor_endpoint_struct *desc_ep = (usb_descriptor_endpoint_struct *)ep_desc;
  107. uint8_t ep_num = desc_ep->bEndpointAddress & 0x0FU;
  108. uint32_t reg_value = 0;
  109. /* set the endpoint type */
  110. switch (desc_ep->bmAttributes & USB_EPTYPE_MASK) {
  111. case ENDP_CONTROL:
  112. reg_value = EP_CONTROL;
  113. break;
  114. case ENDP_BULK:
  115. reg_value = EP_BULK;
  116. break;
  117. case ENDP_INT:
  118. reg_value = EP_INTERRUPT;
  119. break;
  120. case ENDP_ISOC:
  121. reg_value = EP_ISO;
  122. break;
  123. default:
  124. break;
  125. }
  126. USBD_REG_SET(USBD_EPxCS(ep_num), reg_value | ep_num);
  127. reg_value = desc_ep->wMaxPacketSize;
  128. if (desc_ep->bEndpointAddress >> 7U) {
  129. usb_ep_struct *ep = &pudev->in_ep[ep_num];
  130. ep->maxpacket = reg_value;
  131. /* set the endpoint transmit buffer address */
  132. (pbuf_reg + ep_num)->tx_addr = (uint16_t)g_free_buf_addr;
  133. reg_value = (reg_value + 1U) & ~1U;
  134. g_free_buf_addr += reg_value;
  135. if (ENDP_DBL_BUF == buf_kind) {
  136. USBD_ENDP_DOUBLE_BUF_SET(ep_num);
  137. (pbuf_reg + ep_num)->rx_addr = (uint16_t)g_free_buf_addr;
  138. g_free_buf_addr += reg_value;
  139. USBD_ENDP_TX_STATUS_SET(ep_num, EPTX_VALID);
  140. USBD_ENDP_RX_STATUS_SET(ep_num, EPRX_DISABLED);
  141. } else {
  142. /* configure the endpoint status as NAK status */
  143. USBD_ENDP_TX_STATUS_SET(ep_num, EPTX_NAK);
  144. }
  145. } else {
  146. usb_ep_struct *ep = &pudev->out_ep[ep_num];
  147. ep->maxpacket = reg_value;
  148. if (ENDP_DBL_BUF == buf_kind) {
  149. USBD_ENDP_DOUBLE_BUF_SET(ep_num);
  150. USBD_DTG_TX_TOGGLE(ep_num);
  151. /* set the endpoint transmit buffer address */
  152. (pbuf_reg + ep_num)->tx_addr = (uint16_t)g_free_buf_addr;
  153. if (reg_value > 62U) {
  154. reg_value = (reg_value + 31U) & ~31U;
  155. (pbuf_reg + ep_num)->tx_count = (uint16_t)(((reg_value << 5U) - 1U) | 0x8000U);
  156. } else {
  157. reg_value = (reg_value + 1U) & ~1U;
  158. (pbuf_reg + ep_num)->tx_count = (uint16_t)(reg_value << 9U);
  159. }
  160. g_free_buf_addr += reg_value;
  161. }
  162. reg_value = desc_ep->wMaxPacketSize;
  163. /* set the endpoint receive buffer address */
  164. (pbuf_reg + ep_num)->rx_addr = (uint16_t)g_free_buf_addr;
  165. if (reg_value > 62U) {
  166. reg_value = (reg_value + 31U) & ~31U;
  167. (pbuf_reg + ep_num)->rx_count = (uint16_t)(((reg_value << 5U) - 1U) | 0x8000U);
  168. } else {
  169. reg_value = (reg_value + 1U) & ~1U;
  170. (pbuf_reg + ep_num)->rx_count = (uint16_t)(reg_value << 9U);
  171. }
  172. if (ENDP_DBL_BUF == buf_kind) {
  173. USBD_ENDP_RX_STATUS_SET(ep_num, EPRX_DISABLED);
  174. USBD_ENDP_TX_STATUS_SET(ep_num, EPTX_NAK);
  175. } else {
  176. /* configure the endpoint status as NAK status */
  177. USBD_ENDP_RX_STATUS_SET(ep_num, EPRX_NAK);
  178. }
  179. }
  180. }
  181. /*!
  182. \brief configure the endpoint when it is disabled
  183. \param[in] pudev: pointer to USB core instance
  184. \param[in] ep_addr: endpoint address
  185. in this parameter:
  186. bit0..bit6: endpoint number (0..7)
  187. bit7: endpoint direction which can be IN(1) or OUT(0)
  188. \param[out] none
  189. \retval none
  190. */
  191. void usbd_ep_deinit (usbd_core_handle_struct *pudev, uint8_t ep_addr)
  192. {
  193. uint8_t ep_num = ep_addr & 0x7F;
  194. if (ep_addr >> 7) {
  195. USBD_DTG_TX_CLEAR(ep_num);
  196. /* configure the endpoint status as DISABLED */
  197. USBD_ENDP_TX_STATUS_SET(ep_num, EPTX_DISABLED);
  198. } else {
  199. USBD_DTG_RX_CLEAR(ep_num);
  200. /* configure the endpoint status as DISABLED */
  201. USBD_ENDP_RX_STATUS_SET(ep_num, EPRX_DISABLED);
  202. }
  203. }
  204. /*!
  205. \brief endpoint prepare to receive data
  206. \param[in] pudev: pointer to usb core instance
  207. \param[in] ep_addr: endpoint address
  208. in this parameter:
  209. bit0..bit6: endpoint number (0..7)
  210. bit7: endpoint direction which can be IN(1) or OUT(0)
  211. \param[in] pbuf: user buffer address pointer
  212. \param[in] buf_len: buffer length
  213. \param[out] none
  214. \retval none
  215. */
  216. void usbd_ep_rx (usbd_core_handle_struct *pudev, uint8_t ep_addr, uint8_t *pbuf, uint16_t buf_len)
  217. {
  218. usb_ep_struct *ep;
  219. uint8_t ep_num = ep_addr & 0x7FU;
  220. ep = &pudev->out_ep[ep_num];
  221. /* configure the transaction level parameters */
  222. ep->trs_buf = pbuf;
  223. ep->trs_len = buf_len;
  224. /* enable endpoint to receive */
  225. USBD_ENDP_RX_STATUS_SET(ep_num, EPRX_VALID);
  226. }
  227. /*!
  228. \brief endpoint prepare to transmit data
  229. \param[in] pudev: pointer to USB core instance
  230. \param[in] ep_addr: endpoint address
  231. in this parameter:
  232. bit0..bit6: endpoint number (0..7)
  233. bit7: endpoint direction which can be IN(1) or OUT(0)
  234. \param[in] pbuf: transmit buffer address pointer
  235. \param[in] buf_len: buffer length
  236. \param[out] none
  237. \retval none
  238. */
  239. void usbd_ep_tx (usbd_core_handle_struct *pudev, uint8_t ep_addr, uint8_t *pbuf, uint16_t buf_len)
  240. {
  241. __IO uint32_t len = 0U;
  242. uint8_t ep_num = ep_addr & 0x7FU;
  243. usb_ep_struct *ep = &pudev->in_ep[ep_num];
  244. /* configure the transaction level parameters */
  245. ep->trs_buf = pbuf;
  246. ep->trs_len = buf_len;
  247. ep->trs_count = 0U;
  248. /* transmit length is more than one packet */
  249. if (ep->trs_len > ep->maxpacket) {
  250. len = ep->maxpacket;
  251. } else {
  252. len = ep->trs_len;
  253. }
  254. usbd_ep_data_write(ep->trs_buf, (pbuf_reg + ep_num)->tx_addr, (uint16_t)len);
  255. (pbuf_reg + ep_num)->tx_count = (uint16_t)len;
  256. /* enable endpoint to transmit */
  257. USBD_ENDP_TX_STATUS_SET(ep_num, EPTX_VALID);
  258. }
  259. /*!
  260. \brief set an endpoint to stall status
  261. \param[in] pudev: pointer to usb core instance
  262. \param[in] ep_addr: endpoint address
  263. in this parameter:
  264. bit0..bit6: endpoint number (0..7)
  265. bit7: endpoint direction which can be IN(1) or OUT(0)
  266. \param[out] none
  267. \retval none
  268. */
  269. void usbd_ep_stall (usbd_core_handle_struct *pudev, uint8_t ep_addr)
  270. {
  271. uint8_t ep_num = ep_addr & 0x7FU;
  272. usb_ep_struct *ep;
  273. if (ep_addr >> 7U) {
  274. ep = &pudev->in_ep[ep_num];
  275. USBD_ENDP_TX_STATUS_SET(ep_num, EPTX_STALL);
  276. } else {
  277. ep = &pudev->out_ep[ep_num];
  278. USBD_ENDP_RX_STATUS_SET(ep_num, EPRX_STALL);
  279. }
  280. ep->stall = 1U;
  281. if (0U == ep_num) {
  282. /* control endpoint need to be stalled in two directions */
  283. USBD_ENDP_RX_TX_STATUS_SET(ep_num, EPRX_STALL, EPTX_STALL);
  284. }
  285. }
  286. /*!
  287. \brief clear endpoint stalled status
  288. \param[in] pudev: pointer to usb core instance
  289. \param[in] ep_addr: endpoint address
  290. in this parameter:
  291. bit0..bit6: endpoint number (0..7)
  292. bit7: endpoint direction which can be IN(1) or OUT(0)
  293. \param[out] none
  294. \retval none
  295. */
  296. void usbd_ep_clear_stall (usbd_core_handle_struct *pudev, uint8_t ep_addr)
  297. {
  298. uint8_t ep_num = ep_addr & 0x7FU;
  299. usb_ep_struct *ep;
  300. if (ep_addr >> 7U) {
  301. ep = &pudev->in_ep[ep_num];
  302. /* clear endpoint data toggle bit */
  303. USBD_DTG_TX_CLEAR(ep_num);
  304. /* clear endpoint stall status */
  305. USBD_ENDP_TX_STATUS_SET(ep_num, EPTX_VALID);
  306. } else {
  307. ep = &pudev->out_ep[ep_num];
  308. /* clear endpoint data toggle bit */
  309. USBD_DTG_RX_CLEAR(ep_num);
  310. /* clear endpoint stall status */
  311. USBD_ENDP_RX_STATUS_SET(ep_num, EPRX_VALID);
  312. }
  313. ep->stall = 0U;
  314. }
  315. /*!
  316. \brief get the endpoint status
  317. \param[in] pudev: pointer to usb core instance
  318. \param[in] ep_addr: endpoint address
  319. in this parameter:
  320. bit0..bit6: endpoint number (0..7)
  321. bit7: endpoint direction which can be IN(1) or OUT(0)
  322. \param[out] none
  323. \retval endpoint status
  324. */
  325. uint8_t usbd_ep_status_get (usbd_core_handle_struct *pudev, uint8_t ep_addr)
  326. {
  327. if (ep_addr >> 7U) {
  328. return (uint8_t)USBD_ENDP_TX_STATUS_GET((ep_addr & 0x7FU));
  329. } else {
  330. return (uint8_t)USBD_ENDP_RX_STATUS_GET(ep_addr);
  331. }
  332. }
  333. /*!
  334. \brief write datas from user fifo to USBRAM
  335. \param[in] user_fifo: pointer to user fifo
  336. \param[in] usbram_addr: the allocation buffer address of the endpoint
  337. \param[in] bytes: the bytes count of the write datas
  338. \param[out] none
  339. \retval none
  340. */
  341. void usbd_ep_data_write(uint8_t *user_fifo, uint16_t usbram_addr, uint16_t bytes)
  342. {
  343. uint32_t n;
  344. uint32_t *write_addr = (uint32_t *)((uint32_t)(usbram_addr * 2U + USBD_RAM));
  345. for (n = 0U; n < (bytes + 1U) / 2U; n++) {
  346. *write_addr++ = *((__packed uint16_t*)user_fifo);
  347. user_fifo += 2U;
  348. }
  349. }
  350. /*!
  351. \brief read datas from USBRAM to user fifo
  352. \param[in] user_fifo: pointer to user fifo
  353. \param[in] usbram_addr: the allocation buffer address of the endpoint
  354. \param[in] bytes: the bytes count of the read datas
  355. \param[out] none
  356. \retval none
  357. */
  358. void usbd_ep_data_read(uint8_t *user_fifo, uint16_t usbram_addr, uint16_t bytes)
  359. {
  360. uint32_t n;
  361. uint32_t *read_addr = (uint32_t *)((uint32_t)(usbram_addr * 2U + USBD_RAM));
  362. for (n = 0U; n < (bytes + 1U) / 2U; n++) {
  363. *((__packed uint16_t*)user_fifo) = (uint16_t)*read_addr++;
  364. user_fifo += 2U;
  365. }
  366. }
  367. /*!
  368. \brief get the received data length
  369. \param[in] pudev: pointer to USB core instance
  370. \param[in] ep_num: endpoint identifier which is in (0..7)
  371. \param[out] none
  372. \retval received data length
  373. */
  374. uint16_t usbd_rx_count_get (usbd_core_handle_struct *pudev, uint8_t ep_num)
  375. {
  376. return (uint16_t)pudev->out_ep[ep_num].trs_count;
  377. }