stm32f7xx_hal_pcd.c 36 KB

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