stm32f7xx_hal_pcd.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396
  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_pcd.c
  4. * @author MCD Application Team
  5. * @brief PCD HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the USB Peripheral Controller:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### How to use this driver #####
  16. ==============================================================================
  17. [..]
  18. The PCD HAL driver can be used as follows:
  19. (#) Declare a PCD_HandleTypeDef handle structure, for example:
  20. PCD_HandleTypeDef hpcd;
  21. (#) Fill parameters of Init structure in HCD handle
  22. (#) Call HAL_PCD_Init() API to initialize the PCD peripheral (Core, Device core, ...)
  23. (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
  24. (##) Enable the PCD/USB Low Level interface clock using
  25. (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
  26. (+++) __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); (For High Speed Mode)
  27. (##) Initialize the related GPIO clocks
  28. (##) Configure PCD pin-out
  29. (##) Configure PCD NVIC interrupt
  30. (#)Associate the Upper USB device stack to the HAL PCD Driver:
  31. (##) hpcd.pData = pdev;
  32. (#)Enable PCD transmission and reception:
  33. (##) HAL_PCD_Start();
  34. @endverbatim
  35. ******************************************************************************
  36. * @attention
  37. *
  38. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  39. *
  40. * Redistribution and use in source and binary forms, with or without modification,
  41. * are permitted provided that the following conditions are met:
  42. * 1. Redistributions of source code must retain the above copyright notice,
  43. * this list of conditions and the following disclaimer.
  44. * 2. Redistributions in binary form must reproduce the above copyright notice,
  45. * this list of conditions and the following disclaimer in the documentation
  46. * and/or other materials provided with the distribution.
  47. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  48. * may be used to endorse or promote products derived from this software
  49. * without specific prior written permission.
  50. *
  51. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  52. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  53. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  54. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  55. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  56. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  57. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  58. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  59. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. ******************************************************************************
  63. */
  64. /* Includes ------------------------------------------------------------------*/
  65. #include "stm32f7xx_hal.h"
  66. /** @addtogroup STM32F7xx_HAL_Driver
  67. * @{
  68. */
  69. /** @defgroup PCD
  70. * @brief PCD HAL module driver
  71. * @{
  72. */
  73. #ifdef HAL_PCD_MODULE_ENABLED
  74. /* Private types -------------------------------------------------------------*/
  75. /* Private variables ---------------------------------------------------------*/
  76. /* Private constants ---------------------------------------------------------*/
  77. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  78. /**
  79. * USB_OTG_CORE VERSION ID
  80. */
  81. #define USB_OTG_CORE_ID_300A 0x4F54300AU
  82. #define USB_OTG_CORE_ID_310A 0x4F54310AU
  83. #define USB_OTG_CORE_ID_320A 0x4F54320AU
  84. #endif /* USB_OTG_FS || USB_OTG_HS */
  85. /* Private macros ------------------------------------------------------------*/
  86. /** @defgroup PCD_Private_Macros PCD Private Macros
  87. * @{
  88. */
  89. #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
  90. #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
  91. /**
  92. * @}
  93. */
  94. /* Private functions prototypes ----------------------------------------------*/
  95. /** @defgroup PCD_Private_Functions PCD Private Functions
  96. * @{
  97. */
  98. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  99. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
  100. #endif /* USB_OTG_FS || USB_OTG_HS */
  101. /**
  102. * @}
  103. */
  104. /* Exported functions --------------------------------------------------------*/
  105. /** @defgroup PCD_Exported_Functions PCD Exported Functions
  106. * @{
  107. */
  108. /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
  109. * @brief Initialization and Configuration functions
  110. *
  111. @verbatim
  112. ===============================================================================
  113. ##### Initialization and de-initialization functions #####
  114. ===============================================================================
  115. [..] This section provides functions allowing to:
  116. @endverbatim
  117. * @{
  118. */
  119. /**
  120. * @brief Initializes the PCD according to the specified
  121. * parameters in the PCD_InitTypeDef and initialize the associated handle.
  122. * @param hpcd PCD handle
  123. * @retval HAL status
  124. */
  125. HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
  126. {
  127. uint8_t i;
  128. /* Check the PCD handle allocation */
  129. if(hpcd == NULL)
  130. {
  131. return HAL_ERROR;
  132. }
  133. /* Check the parameters */
  134. assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
  135. if(hpcd->State == HAL_PCD_STATE_RESET)
  136. {
  137. /* Allocate lock resource and initialize it */
  138. hpcd->Lock = HAL_UNLOCKED;
  139. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  140. HAL_PCD_MspInit(hpcd);
  141. }
  142. hpcd->State = HAL_PCD_STATE_BUSY;
  143. /* Disable the Interrupts */
  144. __HAL_PCD_DISABLE(hpcd);
  145. /*Init the Core (common init.) */
  146. (void)USB_CoreInit(hpcd->Instance, hpcd->Init);
  147. /* Force Device Mode*/
  148. (void)USB_SetCurrentMode(hpcd->Instance, USB_DEVICE_MODE);
  149. /* Init endpoints structures */
  150. for (i = 0U; i < 15U; i++)
  151. {
  152. /* Init ep structure */
  153. hpcd->IN_ep[i].is_in = 1U;
  154. hpcd->IN_ep[i].num = i;
  155. hpcd->IN_ep[i].tx_fifo_num = i;
  156. /* Control until ep is activated */
  157. hpcd->IN_ep[i].type = EP_TYPE_CTRL;
  158. hpcd->IN_ep[i].maxpacket = 0U;
  159. hpcd->IN_ep[i].xfer_buff = 0U;
  160. hpcd->IN_ep[i].xfer_len = 0U;
  161. }
  162. for (i = 0U; i < 15U; i++)
  163. {
  164. hpcd->OUT_ep[i].is_in = 0U;
  165. hpcd->OUT_ep[i].num = i;
  166. /* Control until ep is activated */
  167. hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
  168. hpcd->OUT_ep[i].maxpacket = 0U;
  169. hpcd->OUT_ep[i].xfer_buff = 0U;
  170. hpcd->OUT_ep[i].xfer_len = 0U;
  171. }
  172. /* Init Device */
  173. (void)USB_DevInit(hpcd->Instance, hpcd->Init);
  174. hpcd->USB_Address = 0U;
  175. hpcd->State = HAL_PCD_STATE_READY;
  176. /* Activate LPM */
  177. if (hpcd->Init.lpm_enable == 1U)
  178. {
  179. (void)HAL_PCDEx_ActivateLPM(hpcd);
  180. }
  181. (void)USB_DevDisconnect (hpcd->Instance);
  182. return HAL_OK;
  183. }
  184. /**
  185. * @brief DeInitializes the PCD peripheral.
  186. * @param hpcd PCD handle
  187. * @retval HAL status
  188. */
  189. HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
  190. {
  191. /* Check the PCD handle allocation */
  192. if(hpcd == NULL)
  193. {
  194. return HAL_ERROR;
  195. }
  196. hpcd->State = HAL_PCD_STATE_BUSY;
  197. /* Stop Device */
  198. (void)HAL_PCD_Stop(hpcd);
  199. /* DeInit the low level hardware */
  200. HAL_PCD_MspDeInit(hpcd);
  201. hpcd->State = HAL_PCD_STATE_RESET;
  202. return HAL_OK;
  203. }
  204. /**
  205. * @brief Initializes the PCD MSP.
  206. * @param hpcd PCD handle
  207. * @retval None
  208. */
  209. __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
  210. {
  211. /* Prevent unused argument(s) compilation warning */
  212. UNUSED(hpcd);
  213. /* NOTE : This function should not be modified, when the callback is needed,
  214. the HAL_PCD_MspInit could be implemented in the user file
  215. */
  216. }
  217. /**
  218. * @brief DeInitializes PCD MSP.
  219. * @param hpcd PCD handle
  220. * @retval None
  221. */
  222. __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
  223. {
  224. /* Prevent unused argument(s) compilation warning */
  225. UNUSED(hpcd);
  226. /* NOTE : This function should not be modified, when the callback is needed,
  227. the HAL_PCD_MspDeInit could be implemented in the user file
  228. */
  229. }
  230. /**
  231. * @}
  232. */
  233. /** @defgroup PCD_Exported_Functions_Group2 IO operation functions
  234. * @brief Data transfers functions
  235. *
  236. @verbatim
  237. ===============================================================================
  238. ##### IO operation functions #####
  239. ===============================================================================
  240. [..]
  241. This subsection provides a set of functions allowing to manage the PCD data
  242. transfers.
  243. @endverbatim
  244. * @{
  245. */
  246. /**
  247. * @brief Start the USB device
  248. * @param hpcd PCD handle
  249. * @retval HAL status
  250. */
  251. HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
  252. {
  253. __HAL_LOCK(hpcd);
  254. (void)USB_DevConnect (hpcd->Instance);
  255. __HAL_PCD_ENABLE(hpcd);
  256. __HAL_UNLOCK(hpcd);
  257. return HAL_OK;
  258. }
  259. /**
  260. * @brief Stop the USB device.
  261. * @param hpcd PCD handle
  262. * @retval HAL status
  263. */
  264. HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
  265. {
  266. __HAL_LOCK(hpcd);
  267. __HAL_PCD_DISABLE(hpcd);
  268. (void)USB_StopDevice(hpcd->Instance);
  269. (void)USB_DevDisconnect(hpcd->Instance);
  270. __HAL_UNLOCK(hpcd);
  271. return HAL_OK;
  272. }
  273. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  274. /**
  275. * @brief Handles PCD interrupt request.
  276. * @param hpcd PCD handle
  277. * @retval HAL status
  278. */
  279. void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
  280. {
  281. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  282. uint32_t USBx_BASE = (uint32_t)USBx;
  283. uint32_t TempReg = USBx_BASE + 0x40U;
  284. uint32_t gSNPSiD = *(uint32_t *) TempReg;
  285. uint32_t i, ep_intr, epint, epnum = 0U;
  286. uint32_t fifoemptymsk, temp;
  287. USB_OTG_EPTypeDef *ep;
  288. uint32_t hclk;
  289. /* ensure that we are in device mode */
  290. if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE)
  291. {
  292. /* avoid spurious interrupt */
  293. if(__HAL_PCD_IS_INVALID_INTERRUPT(hpcd))
  294. {
  295. return;
  296. }
  297. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS))
  298. {
  299. /* incorrect mode, acknowledge the interrupt */
  300. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
  301. }
  302. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT))
  303. {
  304. epnum = 0U;
  305. /* Read in the device interrupt bits */
  306. ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
  307. while (ep_intr != 0U)
  308. {
  309. if ((ep_intr & 0x1U) != 0U)
  310. {
  311. epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, (uint8_t)epnum);
  312. if ((epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
  313. {
  314. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
  315. if (gSNPSiD > USB_OTG_CORE_ID_300A)
  316. {
  317. /* setup/out transaction management for Core ID >= 310A */
  318. if (hpcd->Init.dma_enable == 1U)
  319. {
  320. if ((USBx_OUTEP(0U)->DOEPINT & (1U << 15)) != 0U)
  321. {
  322. CLEAR_OUT_EP_INTR(epnum, (1U << 15));
  323. }
  324. }
  325. }
  326. if(hpcd->Init.dma_enable == 1U)
  327. {
  328. hpcd->OUT_ep[epnum].xfer_count = hpcd->OUT_ep[epnum].maxpacket- (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
  329. hpcd->OUT_ep[epnum].xfer_buff += hpcd->OUT_ep[epnum].maxpacket;
  330. }
  331. if (gSNPSiD == USB_OTG_CORE_ID_310A)
  332. {
  333. if ((USBx_OUTEP(0U)->DOEPINT & (1U << 15)) != 0U)
  334. {
  335. CLEAR_OUT_EP_INTR(epnum, (1U << 15));
  336. }
  337. else
  338. {
  339. HAL_PCD_DataOutStageCallback(hpcd, epnum);
  340. }
  341. }
  342. else
  343. {
  344. HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
  345. }
  346. if(hpcd->Init.dma_enable == 1U)
  347. {
  348. if((epnum == 0U) && (hpcd->OUT_ep[epnum].xfer_len == 0U))
  349. {
  350. /* this is ZLP, so prepare EP0 for next setup */
  351. (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
  352. }
  353. }
  354. }
  355. if ((epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
  356. {
  357. if (gSNPSiD == USB_OTG_CORE_ID_310A)
  358. {
  359. if ((USBx_OUTEP(0U)->DOEPINT & (1U << 15)) != 0U)
  360. {
  361. CLEAR_OUT_EP_INTR(epnum, (1U << 15));
  362. }
  363. }
  364. if (gSNPSiD > USB_OTG_CORE_ID_300A)
  365. {
  366. /* setup/out transaction management for Core ID >= 310A */
  367. if (hpcd->Init.dma_enable == 1U)
  368. {
  369. if ((USBx_OUTEP(0U)->DOEPINT & (1U << 15)) != 0U)
  370. {
  371. CLEAR_OUT_EP_INTR(epnum, (1U << 15));
  372. }
  373. }
  374. }
  375. /* Inform the upper layer that a setup packet is available */
  376. HAL_PCD_SetupStageCallback(hpcd);
  377. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
  378. }
  379. if(( epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
  380. {
  381. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
  382. }
  383. #ifdef USB_OTG_DOEPINT_OTEPSPR
  384. /* Clear Status Phase Received interrupt */
  385. if(( epint & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
  386. {
  387. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
  388. }
  389. #endif /* USB_OTG_DOEPINT_OTEPSPR */
  390. }
  391. epnum++;
  392. ep_intr >>= 1U;
  393. }
  394. }
  395. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
  396. {
  397. /* Read in the device interrupt bits */
  398. ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
  399. epnum = 0U;
  400. while (ep_intr != 0U)
  401. {
  402. if ((ep_intr & 0x1U) != 0U) /* In ITR */
  403. {
  404. epint = USB_ReadDevInEPInterrupt(hpcd->Instance, (uint8_t)epnum);
  405. if ((epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
  406. {
  407. fifoemptymsk = (uint32_t)(0x1UL << (epnum & 0xFU));
  408. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  409. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
  410. if (hpcd->Init.dma_enable == 1U)
  411. {
  412. hpcd->IN_ep[epnum].xfer_buff += hpcd->IN_ep[epnum].maxpacket;
  413. }
  414. HAL_PCD_DataInStageCallback(hpcd, (uint8_t)epnum);
  415. if (hpcd->Init.dma_enable == 1U)
  416. {
  417. /* this is ZLP, so prepare EP0 for next setup */
  418. if ((epnum == 0U) && (hpcd->IN_ep[epnum].xfer_len == 0U))
  419. {
  420. /* prepare to rx more setup packets */
  421. (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
  422. }
  423. }
  424. }
  425. if ((epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
  426. {
  427. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
  428. }
  429. if ((epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
  430. {
  431. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
  432. }
  433. if ((epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
  434. {
  435. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
  436. }
  437. if ((epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
  438. {
  439. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
  440. }
  441. if ((epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
  442. {
  443. (void)PCD_WriteEmptyTxFifo(hpcd, epnum);
  444. }
  445. }
  446. epnum++;
  447. ep_intr >>= 1U;
  448. }
  449. }
  450. /* Handle Resume Interrupt */
  451. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
  452. {
  453. /* Clear the Remote Wake-up Signaling */
  454. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  455. if(hpcd->LPM_State == LPM_L1)
  456. {
  457. hpcd->LPM_State = LPM_L0;
  458. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
  459. }
  460. else
  461. {
  462. HAL_PCD_ResumeCallback(hpcd);
  463. }
  464. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
  465. }
  466. /* Handle Suspend Interrupt */
  467. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
  468. {
  469. if((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
  470. {
  471. HAL_PCD_SuspendCallback(hpcd);
  472. }
  473. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
  474. }
  475. /* Handle LPM Interrupt */
  476. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT))
  477. {
  478. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
  479. if( hpcd->LPM_State == LPM_L0)
  480. {
  481. hpcd->LPM_State = LPM_L1;
  482. hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >> 2U;
  483. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
  484. }
  485. else
  486. {
  487. HAL_PCD_SuspendCallback(hpcd);
  488. }
  489. }
  490. /* Handle Reset Interrupt */
  491. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
  492. {
  493. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  494. (void)USB_FlushTxFifo(hpcd->Instance, 0x10U);
  495. for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
  496. {
  497. USBx_INEP(i)->DIEPINT = 0xFB7FU;
  498. USBx_INEP(i)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
  499. USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
  500. USBx_OUTEP(i)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
  501. }
  502. USBx_DEVICE->DAINTMSK |= 0x10001U;
  503. if (hpcd->Init.use_dedicated_ep1 != 0U)
  504. {
  505. USBx_DEVICE->DOUTEP1MSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
  506. USBx_DEVICE->DINEP1MSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
  507. }
  508. else
  509. {
  510. #ifdef USB_OTG_DOEPINT_OTEPSPR
  511. USBx_DEVICE->DOEPMSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM | USB_OTG_DOEPMSK_OTEPSPRM);
  512. #else
  513. USBx_DEVICE->DOEPMSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
  514. #endif /* USB_OTG_DOEPINT_OTEPSPR */
  515. USBx_DEVICE->DIEPMSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
  516. }
  517. /* Set Default Address to 0 */
  518. USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
  519. /* setup EP0 to receive SETUP packets */
  520. (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t)hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
  521. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
  522. }
  523. /* Handle Enumeration done Interrupt */
  524. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
  525. {
  526. (void)USB_ActivateSetup(hpcd->Instance);
  527. hpcd->Instance->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
  528. if ( USB_GetDevSpeed(hpcd->Instance) == USB_OTG_SPEED_HIGH)
  529. {
  530. hpcd->Init.speed = USB_OTG_SPEED_HIGH;
  531. hpcd->Init.ep0_mps = USB_OTG_HS_MAX_PACKET_SIZE;
  532. hpcd->Instance->GUSBCFG |= (uint32_t)((USBD_HS_TRDT_VALUE << 10U) & USB_OTG_GUSBCFG_TRDT);
  533. }
  534. else
  535. {
  536. hpcd->Init.speed = USB_OTG_SPEED_FULL;
  537. hpcd->Init.ep0_mps = USB_OTG_FS_MAX_PACKET_SIZE;
  538. /* The USBTRD is configured according to the tables below, depending on AHB frequency
  539. used by application. In the low AHB frequency range it is used to stretch enough the USB response
  540. time to IN tokens, the USB turnaround time, so to compensate for the longer AHB read access
  541. latency to the Data FIFO */
  542. /* Get hclk frequency value */
  543. hclk = HAL_RCC_GetHCLKFreq();
  544. if((hclk >= 14200000U) && (hclk < 15000000U))
  545. {
  546. /* hclk Clock Range between 14.2-15 MHz */
  547. hpcd->Instance->GUSBCFG |= (uint32_t)((0xFU << 10) & USB_OTG_GUSBCFG_TRDT);
  548. }
  549. else if((hclk >= 15000000U) && (hclk < 16000000U))
  550. {
  551. /* hclk Clock Range between 15-16 MHz */
  552. hpcd->Instance->GUSBCFG |= (uint32_t)((0xEU << 10) & USB_OTG_GUSBCFG_TRDT);
  553. }
  554. else if((hclk >= 16000000U) && (hclk < 17200000U))
  555. {
  556. /* hclk Clock Range between 16-17.2 MHz */
  557. hpcd->Instance->GUSBCFG |= (uint32_t)((0xDU << 10) & USB_OTG_GUSBCFG_TRDT);
  558. }
  559. else if((hclk >= 17200000U) && (hclk < 18500000U))
  560. {
  561. /* hclk Clock Range between 17.2-18.5 MHz */
  562. hpcd->Instance->GUSBCFG |= (uint32_t)((0xCU << 10) & USB_OTG_GUSBCFG_TRDT);
  563. }
  564. else if((hclk >= 18500000U) && (hclk < 20000000U))
  565. {
  566. /* hclk Clock Range between 18.5-20 MHz */
  567. hpcd->Instance->GUSBCFG |= (uint32_t)((0xBU << 10) & USB_OTG_GUSBCFG_TRDT);
  568. }
  569. else if((hclk >= 20000000U) && (hclk < 21800000U))
  570. {
  571. /* hclk Clock Range between 20-21.8 MHz */
  572. hpcd->Instance->GUSBCFG |= (uint32_t)((0xAU << 10) & USB_OTG_GUSBCFG_TRDT);
  573. }
  574. else if((hclk >= 21800000U) && (hclk < 24000000U))
  575. {
  576. /* hclk Clock Range between 21.8-24 MHz */
  577. hpcd->Instance->GUSBCFG |= (uint32_t)((0x9U << 10) & USB_OTG_GUSBCFG_TRDT);
  578. }
  579. else if((hclk >= 24000000U) && (hclk < 27700000U))
  580. {
  581. /* hclk Clock Range between 24-27.7 MHz */
  582. hpcd->Instance->GUSBCFG |= (uint32_t)((0x8U << 10) & USB_OTG_GUSBCFG_TRDT);
  583. }
  584. else if((hclk >= 27700000U) && (hclk < 32000000U))
  585. {
  586. /* hclk Clock Range between 27.7-32 MHz */
  587. hpcd->Instance->GUSBCFG |= (uint32_t)((0x7U << 10) & USB_OTG_GUSBCFG_TRDT);
  588. }
  589. else /* if(hclk >= 32000000) */
  590. {
  591. /* hclk Clock Range between 32-200 MHz */
  592. hpcd->Instance->GUSBCFG |= (uint32_t)((0x6U << 10) & USB_OTG_GUSBCFG_TRDT);
  593. }
  594. }
  595. HAL_PCD_ResetCallback(hpcd);
  596. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
  597. }
  598. /* Handle RxQLevel Interrupt */
  599. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL))
  600. {
  601. USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  602. temp = USBx->GRXSTSP;
  603. ep = &hpcd->OUT_ep[temp & USB_OTG_GRXSTSP_EPNUM];
  604. if(((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT)
  605. {
  606. if((temp & USB_OTG_GRXSTSP_BCNT) != 0U)
  607. {
  608. (void)USB_ReadPacket(USBx, ep->xfer_buff, (uint16_t)((temp & USB_OTG_GRXSTSP_BCNT) >> 4));
  609. ep->xfer_buff += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  610. ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  611. }
  612. }
  613. else if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT)
  614. {
  615. (void)USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8U);
  616. ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  617. }
  618. else
  619. {
  620. /* ... */
  621. }
  622. USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  623. }
  624. /* Handle SOF Interrupt */
  625. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF))
  626. {
  627. HAL_PCD_SOFCallback(hpcd);
  628. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
  629. }
  630. /* Handle Incomplete ISO IN Interrupt */
  631. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR))
  632. {
  633. HAL_PCD_ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
  634. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
  635. }
  636. /* Handle Incomplete ISO OUT Interrupt */
  637. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
  638. {
  639. HAL_PCD_ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
  640. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
  641. }
  642. /* Handle Connection event Interrupt */
  643. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT))
  644. {
  645. HAL_PCD_ConnectCallback(hpcd);
  646. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
  647. }
  648. /* Handle Disconnection event Interrupt */
  649. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT))
  650. {
  651. temp = hpcd->Instance->GOTGINT;
  652. if((temp & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET)
  653. {
  654. HAL_PCD_DisconnectCallback(hpcd);
  655. }
  656. hpcd->Instance->GOTGINT |= temp;
  657. }
  658. }
  659. }
  660. #endif /* USB_OTG_FS || USB_OTG_HS */
  661. /**
  662. * @brief Data OUT stage callback.
  663. * @param hpcd PCD handle
  664. * @param epnum endpoint number
  665. * @retval None
  666. */
  667. __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  668. {
  669. /* Prevent unused argument(s) compilation warning */
  670. UNUSED(hpcd);
  671. UNUSED(epnum);
  672. /* NOTE : This function should not be modified, when the callback is needed,
  673. the HAL_PCD_DataOutStageCallback could be implemented in the user file
  674. */
  675. }
  676. /**
  677. * @brief Data IN stage callback
  678. * @param hpcd PCD handle
  679. * @param epnum endpoint number
  680. * @retval None
  681. */
  682. __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  683. {
  684. /* Prevent unused argument(s) compilation warning */
  685. UNUSED(hpcd);
  686. UNUSED(epnum);
  687. /* NOTE : This function should not be modified, when the callback is needed,
  688. the HAL_PCD_DataInStageCallback could be implemented in the user file
  689. */
  690. }
  691. /**
  692. * @brief Setup stage callback
  693. * @param hpcd PCD handle
  694. * @retval None
  695. */
  696. __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  697. {
  698. /* Prevent unused argument(s) compilation warning */
  699. UNUSED(hpcd);
  700. /* NOTE : This function should not be modified, when the callback is needed,
  701. the HAL_PCD_SetupStageCallback could be implemented in the user file
  702. */
  703. }
  704. /**
  705. * @brief USB Start Of Frame callback.
  706. * @param hpcd PCD handle
  707. * @retval None
  708. */
  709. __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  710. {
  711. /* Prevent unused argument(s) compilation warning */
  712. UNUSED(hpcd);
  713. /* NOTE : This function should not be modified, when the callback is needed,
  714. the HAL_PCD_SOFCallback could be implemented in the user file
  715. */
  716. }
  717. /**
  718. * @brief USB Reset callback.
  719. * @param hpcd PCD handle
  720. * @retval None
  721. */
  722. __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  723. {
  724. /* Prevent unused argument(s) compilation warning */
  725. UNUSED(hpcd);
  726. /* NOTE : This function should not be modified, when the callback is needed,
  727. the HAL_PCD_ResetCallback could be implemented in the user file
  728. */
  729. }
  730. /**
  731. * @brief Suspend event callback.
  732. * @param hpcd PCD handle
  733. * @retval None
  734. */
  735. __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  736. {
  737. /* Prevent unused argument(s) compilation warning */
  738. UNUSED(hpcd);
  739. /* NOTE : This function should not be modified, when the callback is needed,
  740. the HAL_PCD_SuspendCallback could be implemented in the user file
  741. */
  742. }
  743. /**
  744. * @brief Resume event callback.
  745. * @param hpcd PCD handle
  746. * @retval None
  747. */
  748. __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  749. {
  750. /* Prevent unused argument(s) compilation warning */
  751. UNUSED(hpcd);
  752. /* NOTE : This function should not be modified, when the callback is needed,
  753. the HAL_PCD_ResumeCallback could be implemented in the user file
  754. */
  755. }
  756. /**
  757. * @brief Incomplete ISO OUT callback.
  758. * @param hpcd PCD handle
  759. * @param epnum endpoint number
  760. * @retval None
  761. */
  762. __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  763. {
  764. /* Prevent unused argument(s) compilation warning */
  765. UNUSED(hpcd);
  766. UNUSED(epnum);
  767. /* NOTE : This function should not be modified, when the callback is needed,
  768. the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
  769. */
  770. }
  771. /**
  772. * @brief Incomplete ISO IN callback.
  773. * @param hpcd PCD handle
  774. * @param epnum endpoint number
  775. * @retval None
  776. */
  777. __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  778. {
  779. /* Prevent unused argument(s) compilation warning */
  780. UNUSED(hpcd);
  781. UNUSED(epnum);
  782. /* NOTE : This function should not be modified, when the callback is needed,
  783. the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
  784. */
  785. }
  786. /**
  787. * @brief Connection event callback.
  788. * @param hpcd PCD handle
  789. * @retval None
  790. */
  791. __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  792. {
  793. /* Prevent unused argument(s) compilation warning */
  794. UNUSED(hpcd);
  795. /* NOTE : This function should not be modified, when the callback is needed,
  796. the HAL_PCD_ConnectCallback could be implemented in the user file
  797. */
  798. }
  799. /**
  800. * @brief Disconnection event callback.
  801. * @param hpcd PCD handle
  802. * @retval None
  803. */
  804. __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  805. {
  806. /* Prevent unused argument(s) compilation warning */
  807. UNUSED(hpcd);
  808. /* NOTE : This function should not be modified, when the callback is needed,
  809. the HAL_PCD_DisconnectCallback could be implemented in the user file
  810. */
  811. }
  812. /**
  813. * @}
  814. */
  815. /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
  816. * @brief management functions
  817. *
  818. @verbatim
  819. ===============================================================================
  820. ##### Peripheral Control functions #####
  821. ===============================================================================
  822. [..]
  823. This subsection provides a set of functions allowing to control the PCD data
  824. transfers.
  825. @endverbatim
  826. * @{
  827. */
  828. /**
  829. * @brief Connect the USB device
  830. * @param hpcd PCD handle
  831. * @retval HAL status
  832. */
  833. HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
  834. {
  835. __HAL_LOCK(hpcd);
  836. (void)USB_DevConnect(hpcd->Instance);
  837. __HAL_UNLOCK(hpcd);
  838. return HAL_OK;
  839. }
  840. /**
  841. * @brief Disconnect the USB device.
  842. * @param hpcd PCD handle
  843. * @retval HAL status
  844. */
  845. HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
  846. {
  847. __HAL_LOCK(hpcd);
  848. (void)USB_DevDisconnect(hpcd->Instance);
  849. __HAL_UNLOCK(hpcd);
  850. return HAL_OK;
  851. }
  852. /**
  853. * @brief Set the USB Device address.
  854. * @param hpcd PCD handle
  855. * @param address new device address
  856. * @retval HAL status
  857. */
  858. HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
  859. {
  860. __HAL_LOCK(hpcd);
  861. hpcd->USB_Address = address;
  862. (void)USB_SetDevAddress(hpcd->Instance, address);
  863. __HAL_UNLOCK(hpcd);
  864. return HAL_OK;
  865. }
  866. /**
  867. * @brief Open and configure an endpoint.
  868. * @param hpcd PCD handle
  869. * @param ep_addr endpoint address
  870. * @param ep_mps endpoint max packet size
  871. * @param ep_type endpoint type
  872. * @retval HAL status
  873. */
  874. HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
  875. {
  876. HAL_StatusTypeDef ret = HAL_OK;
  877. PCD_EPTypeDef *ep;
  878. if ((ep_addr & 0x80U) == 0x80U)
  879. {
  880. ep = &hpcd->IN_ep[ep_addr & 0xFU];
  881. ep->is_in = 1U;
  882. }
  883. else
  884. {
  885. ep = &hpcd->OUT_ep[ep_addr & 0xFU];
  886. ep->is_in = 0U;
  887. }
  888. ep->num = ep_addr & 0xFU;
  889. ep->maxpacket = ep_mps;
  890. ep->type = ep_type;
  891. if (ep->is_in != 0U)
  892. {
  893. /* Assign a Tx FIFO */
  894. ep->tx_fifo_num = ep->num;
  895. }
  896. /* Set initial data PID. */
  897. if (ep_type == EP_TYPE_BULK)
  898. {
  899. ep->data_pid_start = 0U;
  900. }
  901. __HAL_LOCK(hpcd);
  902. (void)USB_ActivateEndpoint(hpcd->Instance, ep);
  903. __HAL_UNLOCK(hpcd);
  904. return ret;
  905. }
  906. /**
  907. * @brief Deactivate an endpoint.
  908. * @param hpcd PCD handle
  909. * @param ep_addr endpoint address
  910. * @retval HAL status
  911. */
  912. HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  913. {
  914. PCD_EPTypeDef *ep;
  915. if ((ep_addr & 0x80U) == 0x80U)
  916. {
  917. ep = &hpcd->IN_ep[ep_addr & 0xFU];
  918. ep->is_in = 1U;
  919. }
  920. else
  921. {
  922. ep = &hpcd->OUT_ep[ep_addr & 0xFU];
  923. ep->is_in = 0U;
  924. }
  925. ep->num = ep_addr & 0xFU;
  926. __HAL_LOCK(hpcd);
  927. (void)USB_DeactivateEndpoint(hpcd->Instance , ep);
  928. __HAL_UNLOCK(hpcd);
  929. return HAL_OK;
  930. }
  931. /**
  932. * @brief Receive an amount of data.
  933. * @param hpcd PCD handle
  934. * @param ep_addr endpoint address
  935. * @param pBuf pointer to the reception buffer
  936. * @param len amount of data to be received
  937. * @retval HAL status
  938. */
  939. HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  940. {
  941. PCD_EPTypeDef *ep;
  942. ep = &hpcd->OUT_ep[ep_addr & 0xFU];
  943. /*setup and start the Xfer */
  944. ep->xfer_buff = pBuf;
  945. ep->xfer_len = len;
  946. ep->xfer_count = 0U;
  947. ep->is_in = 0U;
  948. ep->num = ep_addr & 0xFU;
  949. if (hpcd->Init.dma_enable == 1U)
  950. {
  951. ep->dma_addr = (uint32_t)pBuf;
  952. }
  953. if ((ep_addr & 0xFU) == 0U)
  954. {
  955. (void)USB_EP0StartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
  956. }
  957. else
  958. {
  959. (void)USB_EPStartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
  960. }
  961. return HAL_OK;
  962. }
  963. /**
  964. * @brief Get Received Data Size
  965. * @param hpcd PCD handle
  966. * @param ep_addr endpoint address
  967. * @retval Data Size
  968. */
  969. uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  970. {
  971. return (uint16_t)hpcd->OUT_ep[ep_addr & 0xFU].xfer_count;
  972. }
  973. /**
  974. * @brief Send an amount of data
  975. * @param hpcd PCD handle
  976. * @param ep_addr endpoint address
  977. * @param pBuf pointer to the transmission buffer
  978. * @param len amount of data to be sent
  979. * @retval HAL status
  980. */
  981. HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  982. {
  983. PCD_EPTypeDef *ep;
  984. ep = &hpcd->IN_ep[ep_addr & 0xFU];
  985. /*setup and start the Xfer */
  986. ep->xfer_buff = pBuf;
  987. ep->xfer_len = len;
  988. ep->xfer_count = 0U;
  989. ep->is_in = 1U;
  990. ep->num = ep_addr & 0xFU;
  991. if (hpcd->Init.dma_enable == 1U)
  992. {
  993. ep->dma_addr = (uint32_t)pBuf;
  994. }
  995. if ((ep_addr & 0xFU) == 0U)
  996. {
  997. (void)USB_EP0StartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
  998. }
  999. else
  1000. {
  1001. (void)USB_EPStartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
  1002. }
  1003. return HAL_OK;
  1004. }
  1005. /**
  1006. * @brief Set a STALL condition over an endpoint
  1007. * @param hpcd PCD handle
  1008. * @param ep_addr endpoint address
  1009. * @retval HAL status
  1010. */
  1011. HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1012. {
  1013. PCD_EPTypeDef *ep;
  1014. if (((uint32_t)ep_addr & 0xFU) > hpcd->Init.dev_endpoints)
  1015. {
  1016. return HAL_ERROR;
  1017. }
  1018. if ((0x80U & ep_addr) == 0x80U)
  1019. {
  1020. ep = &hpcd->IN_ep[ep_addr & 0xFU];
  1021. ep->is_in = 1U;
  1022. }
  1023. else
  1024. {
  1025. ep = &hpcd->OUT_ep[ep_addr];
  1026. ep->is_in = 0U;
  1027. }
  1028. ep->is_stall = 1U;
  1029. ep->num = ep_addr & 0xFU;
  1030. __HAL_LOCK(hpcd);
  1031. (void)USB_EPSetStall(hpcd->Instance, ep);
  1032. if((ep_addr & 0xFU) == 0U)
  1033. {
  1034. (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t)hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
  1035. }
  1036. __HAL_UNLOCK(hpcd);
  1037. return HAL_OK;
  1038. }
  1039. /**
  1040. * @brief Clear a STALL condition over in an endpoint
  1041. * @param hpcd PCD handle
  1042. * @param ep_addr endpoint address
  1043. * @retval HAL status
  1044. */
  1045. HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1046. {
  1047. PCD_EPTypeDef *ep;
  1048. if (((uint32_t)ep_addr & 0x0FU) > hpcd->Init.dev_endpoints)
  1049. {
  1050. return HAL_ERROR;
  1051. }
  1052. if ((0x80U & ep_addr) == 0x80U)
  1053. {
  1054. ep = &hpcd->IN_ep[ep_addr & 0xFU];
  1055. ep->is_in = 1U;
  1056. }
  1057. else
  1058. {
  1059. ep = &hpcd->OUT_ep[ep_addr &0xFU];
  1060. ep->is_in = 0U;
  1061. }
  1062. ep->is_stall = 0U;
  1063. ep->num = ep_addr & 0xFU;
  1064. __HAL_LOCK(hpcd);
  1065. (void)USB_EPClearStall(hpcd->Instance, ep);
  1066. __HAL_UNLOCK(hpcd);
  1067. return HAL_OK;
  1068. }
  1069. /**
  1070. * @brief Flush an endpoint
  1071. * @param hpcd PCD handle
  1072. * @param ep_addr endpoint address
  1073. * @retval HAL status
  1074. */
  1075. HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1076. {
  1077. __HAL_LOCK(hpcd);
  1078. if ((ep_addr & 0x80U) == 0x80U)
  1079. {
  1080. (void)USB_FlushTxFifo(hpcd->Instance, (uint32_t)ep_addr & 0xFU);
  1081. }
  1082. else
  1083. {
  1084. (void)USB_FlushRxFifo(hpcd->Instance);
  1085. }
  1086. __HAL_UNLOCK(hpcd);
  1087. return HAL_OK;
  1088. }
  1089. /**
  1090. * @brief Activate remote wakeup signalling
  1091. * @param hpcd PCD handle
  1092. * @retval HAL status
  1093. */
  1094. HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1095. {
  1096. return(USB_ActivateRemoteWakeup(hpcd->Instance));
  1097. }
  1098. /**
  1099. * @brief De-activate remote wakeup signalling.
  1100. * @param hpcd PCD handle
  1101. * @retval HAL status
  1102. */
  1103. HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1104. {
  1105. return(USB_DeActivateRemoteWakeup(hpcd->Instance));
  1106. }
  1107. /**
  1108. * @}
  1109. */
  1110. /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
  1111. * @brief Peripheral State functions
  1112. *
  1113. @verbatim
  1114. ===============================================================================
  1115. ##### Peripheral State functions #####
  1116. ===============================================================================
  1117. [..]
  1118. This subsection permits to get in run-time the status of the peripheral
  1119. and the data flow.
  1120. @endverbatim
  1121. * @{
  1122. */
  1123. /**
  1124. * @brief Return the PCD handle state.
  1125. * @param hpcd PCD handle
  1126. * @retval HAL state
  1127. */
  1128. PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
  1129. {
  1130. return hpcd->State;
  1131. }
  1132. /**
  1133. * @}
  1134. */
  1135. /**
  1136. * @}
  1137. */
  1138. /* Private functions ---------------------------------------------------------*/
  1139. /** @addtogroup PCD_Private_Functions
  1140. * @{
  1141. */
  1142. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  1143. /**
  1144. * @brief Check FIFO for the next packet to be loaded.
  1145. * @param hpcd PCD handle
  1146. * @param epnum endpoint number
  1147. * @retval HAL status
  1148. */
  1149. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1150. {
  1151. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1152. uint32_t USBx_BASE = (uint32_t)USBx;
  1153. USB_OTG_EPTypeDef *ep;
  1154. uint32_t len;
  1155. uint32_t len32b;
  1156. uint32_t fifoemptymsk;
  1157. ep = &hpcd->IN_ep[epnum];
  1158. len = ep->xfer_len - ep->xfer_count;
  1159. if (len > ep->maxpacket)
  1160. {
  1161. len = ep->maxpacket;
  1162. }
  1163. len32b = (len + 3U) / 4U;
  1164. while (((USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) > len32b) &&
  1165. (ep->xfer_count < ep->xfer_len) && (ep->xfer_len != 0U))
  1166. {
  1167. /* Write the FIFO */
  1168. len = ep->xfer_len - ep->xfer_count;
  1169. if (len > ep->maxpacket)
  1170. {
  1171. len = ep->maxpacket;
  1172. }
  1173. len32b = (len + 3U) / 4U;
  1174. (void)USB_WritePacket(USBx, ep->xfer_buff, (uint8_t)epnum, (uint16_t)len, (uint8_t)hpcd->Init.dma_enable);
  1175. ep->xfer_buff += len;
  1176. ep->xfer_count += len;
  1177. }
  1178. if(len <= 0U)
  1179. {
  1180. fifoemptymsk = (uint32_t)(0x1UL << epnum);
  1181. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  1182. }
  1183. return HAL_OK;
  1184. }
  1185. #endif /* USB_OTG_FS || USB_OTG_HS */
  1186. /**
  1187. * @}
  1188. */
  1189. #endif /* HAL_PCD_MODULE_ENABLED */
  1190. /**
  1191. * @}
  1192. */
  1193. /**
  1194. * @}
  1195. */
  1196. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/