at32f403a_407_usb.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /**
  2. **************************************************************************
  3. * @file at32f403a_407_usb.c
  4. * @brief contains the functions for the usb firmware library
  5. **************************************************************************
  6. * Copyright notice & Disclaimer
  7. *
  8. * The software Board Support Package (BSP) that is made available to
  9. * download from Artery official website is the copyrighted work of Artery.
  10. * Artery authorizes customers to use, copy, and distribute the BSP
  11. * software and its related documentation for the purpose of design and
  12. * development in conjunction with Artery microcontrollers. Use of the
  13. * software is governed by this copyright notice and the following disclaimer.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  16. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  17. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  18. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  19. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  21. *
  22. **************************************************************************
  23. */
  24. /** @addtogroup AT32F403A_407_periph_driver
  25. * @{
  26. */
  27. /** @defgroup USB
  28. * @brief USB driver modules
  29. * @{
  30. */
  31. #include "at32f403a_407_conf.h"
  32. #ifdef USB_MODULE_ENABLED
  33. /** @defgroup USB_private_functions
  34. * @{
  35. */
  36. /**
  37. * @brief usb packet buffer start address
  38. */
  39. #define USB_ENDP_DESC_TABLE_OFFSET 0x40
  40. uint32_t g_usb_packet_address = USB_PACKET_BUFFER_ADDRESS;
  41. static uint16_t g_usb_offset_addr = USB_ENDP_DESC_TABLE_OFFSET;
  42. /**
  43. * @brief initialize usb peripheral controller register
  44. * @param usbx: to select the usb peripheral.
  45. * parameter as following values: USB
  46. * @retval none
  47. */
  48. void usb_dev_init(usbd_type *usbx)
  49. {
  50. /* clear usb core reset */
  51. usbx->ctrl_bit.csrst = 0;
  52. /* clear usb interrupt status */
  53. usbx->intsts = 0;
  54. /* set usb packet buffer descirption table address */
  55. usbx->buftbl = USB_BUFFER_TABLE_ADDRESS;
  56. /* enable usb core and set device address to 0 */
  57. usbx->devaddr = 0x80;
  58. usb_interrupt_enable(usbx, USB_SOF_INT | USB_RST_INT | USB_SP_INT | USB_WK_INT | USB_TC_INT, TRUE);
  59. }
  60. /**
  61. * @brief connect usb device
  62. * @param usbx: to select the usb peripheral.
  63. * parameter as following values: USB
  64. * @retval none
  65. */
  66. void usb_connect(usbd_type *usbx)
  67. {
  68. /* enable usb phy */
  69. usbx->ctrl_bit.disusb = 0;
  70. /* Dp 1.5k pull-up enable */
  71. usbx->cfg_bit.puo = 0;
  72. }
  73. /**
  74. * @brief disconnect usb device
  75. * @param usbx: to select the usb peripheral.
  76. * parameter as following values: USB
  77. * @retval none
  78. */
  79. void usb_disconnect(usbd_type *usbx)
  80. {
  81. /* disable usb phy */
  82. usbx->ctrl_bit.disusb = TRUE;
  83. /* D+ 1.5k pull-up disable */
  84. usbx->cfg_bit.puo = TRUE;
  85. }
  86. /**
  87. * @brief mapping usb packet buffer area
  88. * two mapping intervals are available for packet buffer area,
  89. * and are select by the usbbufs in the crm misc1 register.
  90. * when usbbufs is 0,sram size is 512 bytes, packet buffer start
  91. * address is 0x40006000.when usbbufs is 1, sram size is fixed to
  92. * 768~1280 bytes, and the packet buffer start address is fixed to
  93. * 0x40007800,packet buffer size decided by whether can1 and can2 are
  94. * enabled;when both can1 and can2 are disabled, usb packet buffer can be set to the
  95. * maximum of 1280 bytes; when either can1 or can2 is enabled, usb packet buffer can be set to the
  96. * maximum of 1024 bytes; when both CAN1 and CAN2 are enabled, usb packet buffer can be set to the
  97. * maximum of 768 bytes.
  98. * @param usbx: to select the usb peripheral.
  99. * parameter as following values: USB
  100. * @retval none
  101. */
  102. void usb_usbbufs_enable(usbd_type *usbx, confirm_state state)
  103. {
  104. if(state == TRUE)
  105. {
  106. /* enable usbbufs */
  107. g_usb_packet_address = USB_PACKET_BUFFER_ADDRESS_EX;
  108. CRM->misc1_bit.usbbufs = TRUE;
  109. }
  110. else
  111. {
  112. /* disable usbbufs */
  113. g_usb_packet_address = USB_PACKET_BUFFER_ADDRESS;
  114. CRM->misc1_bit.usbbufs = FALSE;
  115. }
  116. UNUSED(usbx);
  117. }
  118. /**
  119. * @brief open usb endpoint
  120. * @param usbx: to select the usb peripheral.
  121. * parameter as following values: USB
  122. * @param ept_info: endpoint information structure
  123. * @retval none
  124. */
  125. void usb_ept_open(usbd_type *usbx, usb_ept_info *ept_info)
  126. {
  127. uint16_t type = 0;
  128. /* set endpoint address */
  129. USB_SET_EPT_ADDRESS(ept_info->eptn, ept_info->ept_address);
  130. /* select endpoint transfer type */
  131. if(ept_info->trans_type == EPT_CONTROL_TYPE)
  132. {
  133. type = USB_EPT_CONTROL;
  134. }
  135. else if(ept_info->trans_type == EPT_BULK_TYPE)
  136. {
  137. type = USB_EPT_BULK;
  138. }
  139. else if(ept_info->trans_type == EPT_INT_TYPE)
  140. {
  141. type = USB_EPT_INT;
  142. }
  143. else if(ept_info->trans_type == EPT_ISO_TYPE)
  144. {
  145. type = USB_EPT_ISO;
  146. ept_info->is_double_buffer = TRUE;
  147. }
  148. /* configure endpoint transfer type (control, bulk, interrupt, isochronous) */
  149. USB_SET_TRANS_TYPE(ept_info->eptn, type);
  150. /* endpoint is in transfer */
  151. if(ept_info->inout == DATA_TRANS_IN)
  152. {
  153. if(ept_info->is_double_buffer == 0)
  154. {
  155. /* set in endpoint tx offset address */
  156. USB_SET_TX_ADDRESS(ept_info->eptn, ept_info->tx_addr);
  157. /* clear in endpoint data toggle */
  158. USB_CLEAR_TXDTS(ept_info->eptn);
  159. /* set endpoint transmission status: nak */
  160. USB_SET_TXSTS(ept_info->eptn, USB_TX_NAK);
  161. }
  162. else
  163. {
  164. /* set double buffer endpoint*/
  165. USB_SET_EPT_DOUBLE_BUFFER(ept_info->eptn);
  166. /* set in endpoint offset address0 and address1 */
  167. USB_SET_DOUBLE_BUFF0_ADDRESS(ept_info->eptn, ept_info->tx_addr);
  168. USB_SET_DOUBLE_BUFF1_ADDRESS(ept_info->eptn, ept_info->rx_addr);
  169. /* clear in and out data toggle */
  170. USB_CLEAR_TXDTS(ept_info->eptn);
  171. USB_CLEAR_RXDTS(ept_info->eptn);
  172. /* toggle rx data toggle flag */
  173. USB_TOGGLE_RXDTS(ept_info->eptn);
  174. /* set endpoint reception status: disable */
  175. USB_SET_RXSTS(ept_info->eptn, USB_RX_DISABLE);
  176. /* set endpoint transmision status: nak */
  177. USB_SET_TXSTS(ept_info->eptn, USB_TX_NAK);
  178. }
  179. }
  180. else
  181. {
  182. if(ept_info->is_double_buffer == 0)
  183. {
  184. /* set out endpoint rx offset address */
  185. USB_SET_RX_ADDRESS(ept_info->eptn, ept_info->rx_addr);
  186. /* clear out endpoint data toggle */
  187. USB_CLEAR_RXDTS(ept_info->eptn);
  188. /* set out endpoint max reception buffer size */
  189. USB_SET_RXLEN(ept_info->eptn, ept_info->maxpacket);
  190. /* set endpoint reception status: valid */
  191. USB_SET_RXSTS(ept_info->eptn, USB_RX_VALID);
  192. }
  193. else
  194. {
  195. /* set double buffer endpoint */
  196. USB_SET_EPT_DOUBLE_BUFFER(ept_info->eptn);
  197. /* set out endpoint offset address0 and address1 */
  198. USB_SET_DOUBLE_BUFF0_ADDRESS(ept_info->eptn, ept_info->tx_addr);
  199. USB_SET_DOUBLE_BUFF1_ADDRESS(ept_info->eptn, ept_info->rx_addr);
  200. /* set out endpoint max reception buffer size */
  201. USB_SET_EPT_DOUBLE_BUF0_LEN(ept_info->eptn, ept_info->maxpacket, DATA_TRANS_OUT);
  202. USB_SET_EPT_DOUBLE_BUF1_LEN(ept_info->eptn, ept_info->maxpacket, DATA_TRANS_OUT);
  203. /* clear in and out data toggle */
  204. USB_CLEAR_TXDTS(ept_info->eptn);
  205. USB_CLEAR_RXDTS(ept_info->eptn);
  206. /* toggle tx data toggle flag */
  207. USB_TOGGLE_TXDTS(ept_info->eptn);
  208. /* set endpoint reception status: valid */
  209. USB_SET_RXSTS(ept_info->eptn, USB_RX_VALID);
  210. /* set endpoint transmision status: disable */
  211. USB_SET_TXSTS(ept_info->eptn, USB_TX_DISABLE);
  212. }
  213. }
  214. UNUSED(usbx);
  215. }
  216. /**
  217. * @brief close usb endpoint
  218. * @param usbx: to select the usb peripheral.
  219. * parameter as following values: USB
  220. * @param ept_info: endpoint information structure
  221. * @retval none
  222. */
  223. void usb_ept_close(usbd_type *usbx, usb_ept_info *ept_info)
  224. {
  225. if(ept_info->is_double_buffer == 0)
  226. {
  227. if(ept_info->inout == DATA_TRANS_IN)
  228. {
  229. /*clear tx data toggle */
  230. USB_CLEAR_TXDTS(ept_info->eptn);
  231. /* set tx status: disable */
  232. USB_SET_TXSTS(ept_info->eptn, USB_TX_DISABLE);
  233. }
  234. else
  235. {
  236. /*clear rx data toggle */
  237. USB_CLEAR_RXDTS(ept_info->eptn);
  238. /* set rx status: disable */
  239. USB_SET_RXSTS(ept_info->eptn, USB_RX_DISABLE);
  240. }
  241. }
  242. else
  243. {
  244. /* double buffer */
  245. /*clear rx and tx data toggle */
  246. USB_CLEAR_TXDTS(ept_info->eptn);
  247. USB_CLEAR_RXDTS(ept_info->eptn);
  248. if(ept_info->inout == DATA_TRANS_IN)
  249. {
  250. /* toggle tx */
  251. USB_TOGGLE_TXDTS(ept_info->eptn);
  252. /* set tx and rx status: disable */
  253. USB_SET_TXSTS(ept_info->eptn, USB_TX_DISABLE);
  254. USB_SET_RXSTS(ept_info->eptn, USB_RX_DISABLE);
  255. }
  256. else
  257. {
  258. /* toggle rx */
  259. USB_TOGGLE_RXDTS(ept_info->eptn);
  260. /* set tx and rx status: disable */
  261. USB_SET_TXSTS(ept_info->eptn, USB_TX_DISABLE);
  262. USB_SET_RXSTS(ept_info->eptn, USB_RX_DISABLE);
  263. }
  264. }
  265. UNUSED(usbx);
  266. }
  267. /**
  268. * @brief write data from user memory to usb buffer
  269. * @param pusr_buf: point to user buffer
  270. * @param offset_addr: endpoint tx offset address
  271. * @param nbytes: number of bytes data write to usb buffer
  272. * @retval none
  273. */
  274. void usb_write_packet(uint8_t *pusr_buf, uint16_t offset_addr, uint16_t nbytes)
  275. {
  276. /* endpoint tx buffer address */
  277. __IO uint16_t *d_addr = (__IO uint16_t *)(offset_addr * 2 + g_usb_packet_address);
  278. uint32_t nhbytes = (nbytes + 1) >> 1;
  279. uint32_t n_index;
  280. uint16_t *pbuf = (uint16_t *)pusr_buf;
  281. for(n_index = 0; n_index < nhbytes; n_index ++)
  282. {
  283. #if defined (__ICCARM__) && (__VER__ < 7000000)
  284. *d_addr++ = *(__packed uint16_t *)pbuf;
  285. #else
  286. *d_addr++ = __UNALIGNED_UINT16_READ(pbuf);
  287. #endif
  288. d_addr ++;
  289. pbuf ++;
  290. }
  291. }
  292. /**
  293. * @brief read data from usb buffer to user buffer
  294. * @param pusr_buf: point to user buffer
  295. * @param offset_addr: endpoint rx offset address
  296. * @param nbytes: number of bytes data write to usb buffer
  297. * @retval none
  298. */
  299. void usb_read_packet(uint8_t *pusr_buf, uint16_t offset_addr, uint16_t nbytes)
  300. {
  301. __IO uint16_t *s_addr = (__IO uint16_t *)(offset_addr * 2 + g_usb_packet_address);
  302. uint32_t nhbytes = (nbytes + 1) >> 1;
  303. uint32_t n_index;
  304. uint16_t *pbuf = (uint16_t *)pusr_buf;
  305. for(n_index = 0; n_index < nhbytes; n_index ++)
  306. {
  307. #if defined (__ICCARM__) && (__VER__ < 7000000)
  308. *(__packed uint16_t *)pbuf = *(__IO uint16_t *)s_addr ++;
  309. #else
  310. __UNALIGNED_UINT16_WRITE(pbuf, *(__IO uint16_t *)s_addr ++);
  311. #endif
  312. s_addr ++;
  313. pbuf ++;
  314. }
  315. }
  316. /**
  317. * @brief usb interrupt enable
  318. * @param usbx: to select the usb peripheral.
  319. * parameter as following values: USB
  320. * @param interrupt:
  321. * this parameter can be any combination of the following values:
  322. * - USB_LSOF_INT
  323. * - USB_SOF_INT
  324. * - USB_RST_INT
  325. * - USB_SP_INT
  326. * - USB_WK_INT
  327. * - USB_BE_INT
  328. * - USB_UCFOR_INT
  329. * - USB_TC_INT
  330. * @param new_state (TRUE or FALSE)
  331. * @retval none
  332. */
  333. void usb_interrupt_enable(usbd_type *usbx, uint16_t interrupt, confirm_state new_state)
  334. {
  335. if(new_state == TRUE)
  336. {
  337. usbx->ctrl |= interrupt;
  338. }
  339. else
  340. {
  341. usbx->ctrl &= ~interrupt;
  342. }
  343. }
  344. /**
  345. * @brief set the host assignment address
  346. * @param usbx: to select the usb peripheral.
  347. * parameter as following values: USB
  348. * @param address: host assignment address
  349. * @retval none
  350. */
  351. void usb_set_address(usbd_type *usbx, uint8_t address)
  352. {
  353. usbx->devaddr_bit.addr = address;
  354. usbx->devaddr_bit.cen = TRUE;
  355. }
  356. /**
  357. * @brief set endpoint tx or rx status to stall
  358. * @param usbx: to select the usb peripheral.
  359. * parameter as following values: USB
  360. * @param ept_info: endpoint information structure
  361. * @retval none
  362. */
  363. void usb_ept_stall(usbd_type *usbx, usb_ept_info *ept_info)
  364. {
  365. if(ept_info->inout == DATA_TRANS_IN)
  366. {
  367. USB_SET_TXSTS(ept_info->eptn, USB_TX_STALL)
  368. }
  369. else
  370. {
  371. USB_SET_RXSTS(ept_info->eptn, USB_RX_STALL)
  372. }
  373. UNUSED(usbx);
  374. }
  375. /**
  376. * @brief usb device enter suspend mode
  377. * @param usbx: to select the usb peripheral.
  378. * parameter as following values: USB
  379. * @retval none
  380. */
  381. void usb_enter_suspend(usbd_type *usbx)
  382. {
  383. usbx->ctrl_bit.ssp = TRUE;
  384. usbx->ctrl_bit.lpm = TRUE;
  385. }
  386. /**
  387. * @brief usb device exit suspend mode
  388. * @param usbx: to select the usb peripheral.
  389. * parameter as following values: USB
  390. * @retval none
  391. */
  392. void usb_exit_suspend(usbd_type *usbx)
  393. {
  394. usbx->ctrl_bit.ssp = FALSE;
  395. usbx->ctrl_bit.lpm = FALSE;
  396. }
  397. /**
  398. * @brief usb remote wakeup set
  399. * @param usbx: to select the usb peripheral.
  400. * parameter as following values: USB
  401. * @retval none
  402. */
  403. void usb_remote_wkup_set(usbd_type *usbx)
  404. {
  405. usbx->ctrl_bit.gresume = TRUE;
  406. }
  407. /**
  408. * @brief usb remote wakeup clear
  409. * @param usbx: to select the usb peripheral.
  410. * parameter as following values: USB
  411. * @retval none
  412. */
  413. void usb_remote_wkup_clear(usbd_type *usbx)
  414. {
  415. usbx->ctrl_bit.gresume = FALSE;
  416. }
  417. /**
  418. * @brief usb auto malloc endpoint buffer
  419. * @param mapacket: endpoint support max packet size
  420. * @retval none
  421. */
  422. uint16_t usb_buffer_malloc(uint16_t maxpacket)
  423. {
  424. uint16_t offset = g_usb_offset_addr;
  425. g_usb_offset_addr += maxpacket;
  426. return offset;
  427. }
  428. /**
  429. * @brief free usb endpoint buffer
  430. * @param none
  431. * @retval none
  432. */
  433. void usb_buffer_free(void)
  434. {
  435. g_usb_offset_addr = USB_ENDP_DESC_TABLE_OFFSET;
  436. }
  437. /**
  438. * @brief get flag of usb.
  439. * @param usbx: select the usb peripheral
  440. * @param flag: select the usb flag
  441. * this parameter can be one of the following values:
  442. * - USB_INOUT_FLAG
  443. * - USB_LSOF_FLAG
  444. * - USB_SOF_FLAG
  445. * - USB_RST_FLAG
  446. * - USB_SP_FLAG
  447. * - USB_WK_FLAG
  448. * - USB_BE_FLAG
  449. * - USB_UCFOR_FLAG
  450. * - USB_TC_FLAG
  451. * @retval none
  452. */
  453. flag_status usb_flag_get(usbd_type *usbx, uint16_t flag)
  454. {
  455. flag_status status = RESET;
  456. if((usbx->intsts & flag) == RESET)
  457. {
  458. status = RESET;
  459. }
  460. else
  461. {
  462. status = SET;
  463. }
  464. return status;
  465. }
  466. /**
  467. * @brief get interrupt flag of usb.
  468. * @param usbx: select the usb peripheral
  469. * @param flag: select the usb flag
  470. * this parameter can be one of the following values:
  471. * - USB_LSOF_FLAG
  472. * - USB_SOF_FLAG
  473. * - USB_RST_FLAG
  474. * - USB_SP_FLAG
  475. * - USB_WK_FLAG
  476. * - USB_BE_FLAG
  477. * - USB_UCFOR_FLAG
  478. * - USB_TC_FLAG
  479. * @retval none
  480. */
  481. flag_status usb_interrupt_flag_get(usbd_type *usbx, uint16_t flag)
  482. {
  483. flag_status status = RESET;
  484. if(flag == USB_TC_FLAG)
  485. {
  486. if(usbx->intsts & USB_TC_FLAG)
  487. status = SET;
  488. }
  489. else
  490. {
  491. if((usbx->intsts & flag) && (usbx->ctrl & flag))
  492. {
  493. status = SET;
  494. }
  495. }
  496. return status;
  497. }
  498. /**
  499. * @brief clear flag of usb.
  500. * @param usbx: select the usb peripheral
  501. * @param flag: select the usb flag
  502. * this parameter can be one of the following values:
  503. * - USB_INOUT_FLAG
  504. * - USB_LSOF_FLAG
  505. * - USB_SOF_FLAG
  506. * - USB_RST_FLAG
  507. * - USB_SP_FLAG
  508. * - USB_WK_FLAG
  509. * - USB_BE_FLAG
  510. * - USB_UCFOR_FLAG
  511. * - USB_TC_FLAG
  512. * @retval none
  513. */
  514. void usb_flag_clear(usbd_type *usbx, uint16_t flag)
  515. {
  516. usbx->intsts = ~flag;
  517. }
  518. /**
  519. * @}
  520. */
  521. #endif
  522. /**
  523. * @}
  524. */
  525. /**
  526. * @}
  527. */