usb_dc_fsdev.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #include "usbd_core.h"
  2. #include "usb_fsdev_reg.h"
  3. #ifndef USBD_IRQHandler
  4. #define USBD_IRQHandler USB_LP_CAN1_RX0_IRQHandler //use actual usb irq name instead
  5. #endif
  6. #ifndef USB_BASE
  7. #define USB_BASE (0x40005C00UL) /*!< USB_IP Peripheral Registers base address */
  8. #endif
  9. #ifndef USB_NUM_BIDIR_ENDPOINTS
  10. #define USB_NUM_BIDIR_ENDPOINTS 8
  11. #endif
  12. #ifndef USB_RAM_SIZE
  13. #define USB_RAM_SIZE 512
  14. #endif
  15. #define USB ((USB_TypeDef *)USB_BASE)
  16. #define USB_BTABLE_SIZE (8 * USB_NUM_BIDIR_ENDPOINTS)
  17. static void fsdev_write_pma(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes);
  18. static void fsdev_read_pma(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes);
  19. /* Endpoint state */
  20. struct fsdev_ep_state {
  21. /** Endpoint max packet size */
  22. uint16_t ep_mps;
  23. /** Endpoint Transfer Type.
  24. * May be Bulk, Interrupt, Control or Isochronous
  25. */
  26. uint8_t ep_type;
  27. uint8_t ep_stalled; /** Endpoint stall flag */
  28. uint16_t ep_pma_buf_len; /** Previously allocated buffer size */
  29. uint16_t ep_pma_addr; /**ep pmd allocated addr*/
  30. };
  31. /* Driver state */
  32. struct fsdev_udc {
  33. volatile uint8_t dev_addr; /*!< USB Address */
  34. volatile uint32_t pma_offset; /*!< pma offset */
  35. struct fsdev_ep_state in_ep[USB_NUM_BIDIR_ENDPOINTS]; /*!< IN endpoint parameters*/
  36. struct fsdev_ep_state out_ep[USB_NUM_BIDIR_ENDPOINTS]; /*!< OUT endpoint parameters */
  37. } g_fsdev_udc;
  38. __WEAK void usb_dc_low_level_init(void)
  39. {
  40. }
  41. __WEAK void usb_dc_low_level_deinit(void)
  42. {
  43. }
  44. int usb_dc_init(void)
  45. {
  46. memset(&g_fsdev_udc, 0, sizeof(struct fsdev_udc));
  47. g_fsdev_udc.pma_offset = USB_BTABLE_SIZE;
  48. usb_dc_low_level_init();
  49. /* Init Device */
  50. /* CNTR_FRES = 1 */
  51. USB->CNTR = (uint16_t)USB_CNTR_FRES;
  52. /* CNTR_FRES = 0 */
  53. USB->CNTR = 0U;
  54. /* Clear pending interrupts */
  55. USB->ISTR = 0U;
  56. /*Set Btable Address*/
  57. USB->BTABLE = BTABLE_ADDRESS;
  58. uint32_t winterruptmask;
  59. /* Set winterruptmask variable */
  60. winterruptmask = USB_CNTR_CTRM | USB_CNTR_WKUPM |
  61. USB_CNTR_SUSPM | USB_CNTR_ERRM |
  62. USB_CNTR_SOFM | USB_CNTR_ESOFM |
  63. USB_CNTR_RESETM;
  64. /* Set interrupt mask */
  65. USB->CNTR = (uint16_t)winterruptmask;
  66. return 0;
  67. }
  68. int usb_dc_deinit(void)
  69. {
  70. /* disable all interrupts and force USB reset */
  71. USB->CNTR = (uint16_t)USB_CNTR_FRES;
  72. /* clear interrupt status register */
  73. USB->ISTR = 0U;
  74. /* switch-off device */
  75. USB->CNTR = (uint16_t)(USB_CNTR_FRES | USB_CNTR_PDWN);
  76. usb_dc_low_level_deinit();
  77. return 0;
  78. }
  79. int usbd_set_address(const uint8_t addr)
  80. {
  81. if (addr == 0U) {
  82. /* set device address and enable function */
  83. USB->DADDR = (uint16_t)USB_DADDR_EF;
  84. }
  85. g_fsdev_udc.dev_addr = addr;
  86. return 0;
  87. }
  88. int usbd_ep_open(const struct usbd_endpoint_cfg *ep_cfg)
  89. {
  90. uint8_t ep_idx = USB_EP_GET_IDX(ep_cfg->ep_addr);
  91. if (!ep_cfg) {
  92. return -1;
  93. }
  94. uint16_t wEpRegVal;
  95. /* initialize Endpoint */
  96. switch (ep_cfg->ep_type) {
  97. case USB_ENDPOINT_TYPE_CONTROL:
  98. wEpRegVal = USB_EP_CONTROL;
  99. break;
  100. case USB_ENDPOINT_TYPE_BULK:
  101. wEpRegVal = USB_EP_BULK;
  102. break;
  103. case USB_ENDPOINT_TYPE_INTERRUPT:
  104. wEpRegVal = USB_EP_INTERRUPT;
  105. break;
  106. case USB_ENDPOINT_TYPE_ISOCHRONOUS:
  107. wEpRegVal = USB_EP_ISOCHRONOUS;
  108. break;
  109. default:
  110. break;
  111. }
  112. PCD_SET_EPTYPE(USB, ep_idx, wEpRegVal);
  113. PCD_SET_EP_ADDRESS(USB, ep_idx, ep_idx);
  114. if (USB_EP_DIR_IS_OUT(ep_cfg->ep_addr)) {
  115. g_fsdev_udc.out_ep[ep_idx].ep_mps = ep_cfg->ep_mps;
  116. g_fsdev_udc.out_ep[ep_idx].ep_type = ep_cfg->ep_type;
  117. if (g_fsdev_udc.out_ep[ep_idx].ep_mps > g_fsdev_udc.out_ep[ep_idx].ep_pma_buf_len) {
  118. if (g_fsdev_udc.pma_offset + g_fsdev_udc.out_ep[ep_idx].ep_mps > USB_RAM_SIZE) {
  119. return -1;
  120. }
  121. g_fsdev_udc.out_ep[ep_idx].ep_pma_buf_len = ep_cfg->ep_mps;
  122. g_fsdev_udc.out_ep[ep_idx].ep_pma_addr = g_fsdev_udc.pma_offset;
  123. /*Set the endpoint Receive buffer address */
  124. PCD_SET_EP_RX_ADDRESS(USB, ep_idx, g_fsdev_udc.pma_offset);
  125. g_fsdev_udc.pma_offset += ep_cfg->ep_mps;
  126. }
  127. /*Set the endpoint Receive buffer counter*/
  128. PCD_SET_EP_RX_CNT(USB, ep_idx, ep_cfg->ep_mps);
  129. PCD_CLEAR_RX_DTOG(USB, ep_idx);
  130. /* Configure VALID status for the Endpoint*/
  131. PCD_SET_EP_RX_STATUS(USB, ep_idx, USB_EP_RX_VALID);
  132. } else {
  133. g_fsdev_udc.in_ep[ep_idx].ep_mps = ep_cfg->ep_mps;
  134. g_fsdev_udc.in_ep[ep_idx].ep_type = ep_cfg->ep_type;
  135. if (g_fsdev_udc.in_ep[ep_idx].ep_mps > g_fsdev_udc.in_ep[ep_idx].ep_pma_buf_len) {
  136. if (g_fsdev_udc.pma_offset + g_fsdev_udc.in_ep[ep_idx].ep_mps > USB_RAM_SIZE) {
  137. return -1;
  138. }
  139. g_fsdev_udc.in_ep[ep_idx].ep_pma_buf_len = ep_cfg->ep_mps;
  140. g_fsdev_udc.in_ep[ep_idx].ep_pma_addr = g_fsdev_udc.pma_offset;
  141. /*Set the endpoint Transmit buffer address */
  142. PCD_SET_EP_TX_ADDRESS(USB, ep_idx, g_fsdev_udc.pma_offset);
  143. g_fsdev_udc.pma_offset += ep_cfg->ep_mps;
  144. }
  145. PCD_CLEAR_TX_DTOG(USB, ep_idx);
  146. if (ep_cfg->ep_type != USB_ENDPOINT_TYPE_ISOCHRONOUS) {
  147. /* Configure NAK status for the Endpoint */
  148. PCD_SET_EP_TX_STATUS(USB, ep_idx, USB_EP_TX_NAK);
  149. } else {
  150. /* Configure TX Endpoint to disabled state */
  151. PCD_SET_EP_TX_STATUS(USB, ep_idx, USB_EP_TX_DIS);
  152. }
  153. }
  154. return 0;
  155. }
  156. int usbd_ep_close(const uint8_t ep)
  157. {
  158. uint8_t ep_idx = USB_EP_GET_IDX(ep);
  159. if (USB_EP_DIR_IS_OUT(ep)) {
  160. PCD_CLEAR_RX_DTOG(USB, ep_idx);
  161. /* Configure DISABLE status for the Endpoint*/
  162. PCD_SET_EP_RX_STATUS(USB, ep_idx, USB_EP_RX_DIS);
  163. } else {
  164. PCD_CLEAR_TX_DTOG(USB, ep_idx);
  165. /* Configure DISABLE status for the Endpoint*/
  166. PCD_SET_EP_TX_STATUS(USB, ep_idx, USB_EP_TX_DIS);
  167. }
  168. return 0;
  169. }
  170. int usbd_ep_set_stall(const uint8_t ep)
  171. {
  172. uint8_t ep_idx = USB_EP_GET_IDX(ep);
  173. if (USB_EP_DIR_IS_OUT(ep)) {
  174. PCD_SET_EP_RX_STATUS(USB, ep_idx, USB_EP_RX_STALL);
  175. } else {
  176. PCD_SET_EP_TX_STATUS(USB, ep_idx, USB_EP_TX_STALL);
  177. }
  178. return 0;
  179. }
  180. int usbd_ep_clear_stall(const uint8_t ep)
  181. {
  182. uint8_t ep_idx = USB_EP_GET_IDX(ep);
  183. if (USB_EP_DIR_IS_OUT(ep)) {
  184. PCD_CLEAR_RX_DTOG(USB, ep_idx);
  185. /* Configure VALID status for the Endpoint */
  186. PCD_SET_EP_RX_STATUS(USB, ep_idx, USB_EP_RX_VALID);
  187. } else {
  188. PCD_CLEAR_TX_DTOG(USB, ep_idx);
  189. if (g_fsdev_udc.in_ep[ep_idx].ep_type != USB_ENDPOINT_TYPE_ISOCHRONOUS) {
  190. /* Configure NAK status for the Endpoint */
  191. PCD_SET_EP_TX_STATUS(USB, ep_idx, USB_EP_TX_NAK);
  192. }
  193. }
  194. return 0;
  195. }
  196. int usbd_ep_is_stalled(const uint8_t ep, uint8_t *stalled)
  197. {
  198. if (USB_EP_DIR_IS_OUT(ep)) {
  199. } else {
  200. }
  201. return 0;
  202. }
  203. int usbd_ep_write(const uint8_t ep, const uint8_t *data, uint32_t data_len, uint32_t *ret_bytes)
  204. {
  205. uint8_t ep_idx = USB_EP_GET_IDX(ep);
  206. if (!data && data_len) {
  207. return -1;
  208. }
  209. while (PCD_GET_EP_TX_STATUS(USB, ep_idx) == USB_EP_TX_VALID) {
  210. }
  211. if (!data_len) {
  212. PCD_SET_EP_TX_CNT(USB, ep_idx, (uint16_t)0);
  213. PCD_SET_EP_TX_STATUS(USB, ep_idx, USB_EP_TX_VALID);
  214. return 0;
  215. }
  216. if (data_len > g_fsdev_udc.in_ep[ep_idx].ep_mps) {
  217. data_len = g_fsdev_udc.in_ep[ep_idx].ep_mps;
  218. }
  219. fsdev_write_pma(USB, (uint8_t *)data, g_fsdev_udc.in_ep[ep_idx].ep_pma_addr, (uint16_t)data_len);
  220. PCD_SET_EP_TX_CNT(USB, ep_idx, (uint16_t)data_len);
  221. PCD_SET_EP_TX_STATUS(USB, ep_idx, USB_EP_TX_VALID);
  222. if (ret_bytes) {
  223. *ret_bytes = data_len;
  224. }
  225. return 0;
  226. }
  227. int usbd_ep_read(const uint8_t ep, uint8_t *data, uint32_t max_data_len, uint32_t *read_bytes)
  228. {
  229. uint8_t ep_idx = USB_EP_GET_IDX(ep);
  230. uint32_t read_count;
  231. if (!data && max_data_len) {
  232. return -1;
  233. }
  234. if (!max_data_len) {
  235. if (ep_idx != 0x00) {
  236. PCD_SET_EP_RX_STATUS(USB, ep_idx, USB_EP_RX_VALID);
  237. }
  238. return 0;
  239. }
  240. read_count = PCD_GET_EP_RX_CNT(USB, ep_idx);
  241. read_count = MIN(read_count, max_data_len);
  242. fsdev_read_pma(USB, (uint8_t *)data, g_fsdev_udc.out_ep[ep_idx].ep_pma_addr, (uint16_t)read_count);
  243. if (read_bytes) {
  244. *read_bytes = read_count;
  245. }
  246. return 0;
  247. }
  248. void USBD_IRQHandler(void)
  249. {
  250. uint16_t wIstr, wEPVal;
  251. uint8_t epindex;
  252. wIstr = USB->ISTR;
  253. uint16_t store_ep[8];
  254. if (wIstr & USB_ISTR_CTR) {
  255. while ((USB->ISTR & USB_ISTR_CTR) != 0U) {
  256. wIstr = USB->ISTR;
  257. /* extract highest priority endpoint number */
  258. epindex = (uint8_t)(wIstr & USB_ISTR_EP_ID);
  259. if (epindex == 0U) {
  260. /* Decode and service control endpoint interrupt */
  261. /* DIR bit = origin of the interrupt */
  262. if ((wIstr & USB_ISTR_DIR) == 0U) {
  263. /* DIR = 0 */
  264. /* DIR = 0 => IN int */
  265. /* DIR = 0 implies that (EP_CTR_TX = 1) always */
  266. PCD_CLEAR_TX_EP_CTR(USB, 0);
  267. usbd_event_notify_handler(USBD_EVENT_EP0_IN_NOTIFY, NULL);
  268. if ((g_fsdev_udc.dev_addr > 0U) && (PCD_GET_EP_TX_CNT(USB, 0) == 0U)) {
  269. USB->DADDR = ((uint16_t)g_fsdev_udc.dev_addr | USB_DADDR_EF);
  270. g_fsdev_udc.dev_addr = 0U;
  271. }
  272. } else {
  273. /* DIR = 1 */
  274. /* DIR = 1 & CTR_RX => SETUP or OUT int */
  275. /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
  276. wEPVal = PCD_GET_ENDPOINT(USB, 0);
  277. if ((wEPVal & USB_EP_SETUP) != 0U) {
  278. /* SETUP bit kept frozen while CTR_RX = 1 */
  279. PCD_CLEAR_RX_EP_CTR(USB, 0);
  280. /* Process SETUP Packet*/
  281. usbd_event_notify_handler(USBD_EVENT_SETUP_NOTIFY, NULL);
  282. PCD_SET_EP_RX_STATUS(USB, 0, USB_EP_RX_VALID);
  283. } else if ((wEPVal & USB_EP_CTR_RX) != 0U) {
  284. PCD_CLEAR_RX_EP_CTR(USB, 0);
  285. /* Process Control Data OUT Packet */
  286. usbd_event_notify_handler(USBD_EVENT_EP0_OUT_NOTIFY, NULL);
  287. PCD_SET_EP_RX_STATUS(USB, 0, USB_EP_RX_VALID);
  288. }
  289. }
  290. } else {
  291. /* Decode and service non control endpoints interrupt */
  292. /* process related endpoint register */
  293. wEPVal = PCD_GET_ENDPOINT(USB, epindex);
  294. if ((wEPVal & USB_EP_CTR_RX) != 0U) {
  295. /* clear int flag */
  296. PCD_CLEAR_RX_EP_CTR(USB, epindex);
  297. usbd_event_notify_handler(USBD_EVENT_EP_OUT_NOTIFY, (void *)(epindex & 0x7f));
  298. }
  299. if ((wEPVal & USB_EP_CTR_TX) != 0U) {
  300. /* clear int flag */
  301. PCD_CLEAR_TX_EP_CTR(USB, epindex);
  302. usbd_event_notify_handler(USBD_EVENT_EP_IN_NOTIFY, (void *)(epindex | 0x80));
  303. }
  304. }
  305. }
  306. }
  307. if (wIstr & USB_ISTR_RESET) {
  308. memset(&g_fsdev_udc, 0, sizeof(struct fsdev_udc));
  309. g_fsdev_udc.pma_offset = USB_BTABLE_SIZE;
  310. usbd_event_notify_handler(USBD_EVENT_RESET, NULL);
  311. USB->ISTR &= (uint16_t)(~USB_ISTR_RESET);
  312. }
  313. if (wIstr & USB_ISTR_PMAOVR) {
  314. USB->ISTR &= (uint16_t)(~USB_ISTR_PMAOVR);
  315. }
  316. if (wIstr & USB_ISTR_ERR) {
  317. USB->ISTR &= (uint16_t)(~USB_ISTR_ERR);
  318. }
  319. if (wIstr & USB_ISTR_WKUP) {
  320. USB->CNTR &= (uint16_t) ~(USB_CNTR_LP_MODE);
  321. USB->CNTR &= (uint16_t) ~(USB_CNTR_FSUSP);
  322. USB->ISTR &= (uint16_t)(~USB_ISTR_WKUP);
  323. }
  324. if (wIstr & USB_ISTR_SUSP) {
  325. /* WA: To Clear Wakeup flag if raised with suspend signal */
  326. /* Store Endpoint register */
  327. for (uint8_t i = 0U; i < 8U; i++) {
  328. store_ep[i] = PCD_GET_ENDPOINT(USB, i);
  329. }
  330. /* FORCE RESET */
  331. USB->CNTR |= (uint16_t)(USB_CNTR_FRES);
  332. /* CLEAR RESET */
  333. USB->CNTR &= (uint16_t)(~USB_CNTR_FRES);
  334. /* wait for reset flag in ISTR */
  335. while ((USB->ISTR & USB_ISTR_RESET) == 0U) {
  336. }
  337. /* Clear Reset Flag */
  338. USB->ISTR &= (uint16_t)(~USB_ISTR_RESET);
  339. /* Restore Registre */
  340. for (uint8_t i = 0U; i < 8U; i++) {
  341. PCD_SET_ENDPOINT(USB, i, store_ep[i]);
  342. }
  343. /* Force low-power mode in the macrocell */
  344. USB->CNTR |= (uint16_t)USB_CNTR_FSUSP;
  345. /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
  346. USB->ISTR &= (uint16_t)(~USB_ISTR_SUSP);
  347. USB->CNTR |= (uint16_t)USB_CNTR_LP_MODE;
  348. }
  349. if (wIstr & USB_ISTR_SOF) {
  350. USB->ISTR &= (uint16_t)(~USB_ISTR_SOF);
  351. }
  352. if (wIstr & USB_ISTR_ESOF) {
  353. USB->ISTR &= (uint16_t)(~USB_ISTR_ESOF);
  354. }
  355. }
  356. static void fsdev_write_pma(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
  357. {
  358. uint32_t n = ((uint32_t)wNBytes + 1U) >> 1;
  359. uint32_t BaseAddr = (uint32_t)USBx;
  360. uint32_t i, temp1, temp2;
  361. __IO uint16_t *pdwVal;
  362. uint8_t *pBuf = pbUsrBuf;
  363. pdwVal = (__IO uint16_t *)(BaseAddr + 0x400U + ((uint32_t)wPMABufAddr * PMA_ACCESS));
  364. for (i = n; i != 0U; i--) {
  365. temp1 = *pBuf;
  366. pBuf++;
  367. temp2 = temp1 | ((uint16_t)((uint16_t)*pBuf << 8));
  368. *pdwVal = (uint16_t)temp2;
  369. pdwVal++;
  370. #if PMA_ACCESS > 1U
  371. pdwVal++;
  372. #endif
  373. pBuf++;
  374. }
  375. }
  376. /**
  377. * @brief Copy data from packet memory area (PMA) to user memory buffer
  378. * @param USBx USB peripheral instance register address.
  379. * @param pbUsrBuf pointer to user memory area.
  380. * @param wPMABufAddr address into PMA.
  381. * @param wNBytes no. of bytes to be copied.
  382. * @retval None
  383. */
  384. static void fsdev_read_pma(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
  385. {
  386. uint32_t n = (uint32_t)wNBytes >> 1;
  387. uint32_t BaseAddr = (uint32_t)USBx;
  388. uint32_t i, temp;
  389. __IO uint16_t *pdwVal;
  390. uint8_t *pBuf = pbUsrBuf;
  391. pdwVal = (__IO uint16_t *)(BaseAddr + 0x400U + ((uint32_t)wPMABufAddr * PMA_ACCESS));
  392. for (i = n; i != 0U; i--) {
  393. temp = *(__IO uint16_t *)pdwVal;
  394. pdwVal++;
  395. *pBuf = (uint8_t)((temp >> 0) & 0xFFU);
  396. pBuf++;
  397. *pBuf = (uint8_t)((temp >> 8) & 0xFFU);
  398. pBuf++;
  399. #if PMA_ACCESS > 1U
  400. pdwVal++;
  401. #endif
  402. }
  403. if ((wNBytes % 2U) != 0U) {
  404. temp = *pdwVal;
  405. *pBuf = (uint8_t)((temp >> 0) & 0xFFU);
  406. }
  407. }