usb_dc_fsdev.c 15 KB

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