stm32l4xx_hal_pcd.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_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. (##) Initialize the related GPIO clocks
  27. (##) Configure PCD pin-out
  28. (##) Configure PCD NVIC interrupt
  29. (#)Associate the Upper USB device stack to the HAL PCD Driver:
  30. (##) hpcd.pData = pdev;
  31. (#)Enable PCD transmission and reception:
  32. (##) HAL_PCD_Start();
  33. @endverbatim
  34. ******************************************************************************
  35. * @attention
  36. *
  37. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  38. *
  39. * Redistribution and use in source and binary forms, with or without modification,
  40. * are permitted provided that the following conditions are met:
  41. * 1. Redistributions of source code must retain the above copyright notice,
  42. * this list of conditions and the following disclaimer.
  43. * 2. Redistributions in binary form must reproduce the above copyright notice,
  44. * this list of conditions and the following disclaimer in the documentation
  45. * and/or other materials provided with the distribution.
  46. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  47. * may be used to endorse or promote products derived from this software
  48. * without specific prior written permission.
  49. *
  50. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  51. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  52. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  53. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  54. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  55. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  56. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  57. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  58. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  59. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  60. *
  61. ******************************************************************************
  62. */
  63. /* Includes ------------------------------------------------------------------*/
  64. #include "stm32l4xx_hal.h"
  65. /** @addtogroup STM32L4xx_HAL_Driver
  66. * @{
  67. */
  68. /** @defgroup PCD PCD
  69. * @brief PCD HAL module driver
  70. * @{
  71. */
  72. #ifdef HAL_PCD_MODULE_ENABLED
  73. #if defined(STM32L432xx) || defined(STM32L433xx) || defined(STM32L442xx) || defined(STM32L443xx) || \
  74. defined(STM32L452xx) || defined(STM32L462xx) || \
  75. defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || \
  76. defined(STM32L496xx) || defined(STM32L4A6xx) || \
  77. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  78. /* Private types -------------------------------------------------------------*/
  79. /* Private variables ---------------------------------------------------------*/
  80. /* Private constants ---------------------------------------------------------*/
  81. /**
  82. * USB_OTG_CORE VERSION ID
  83. */
  84. #define USB_OTG_CORE_ID_310A 0x4F54310A
  85. #define USB_OTG_CORE_ID_320A 0x4F54320A
  86. /* Private macros ------------------------------------------------------------*/
  87. /** @defgroup PCD_Private_Macros PCD Private Macros
  88. * @{
  89. */
  90. #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
  91. #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
  92. /**
  93. * @}
  94. */
  95. /* Private functions prototypes ----------------------------------------------*/
  96. /** @defgroup PCD_Private_Functions PCD Private Functions
  97. * @{
  98. */
  99. #if defined (USB_OTG_FS)
  100. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
  101. #endif /* USB_OTG_FS */
  102. #if defined (USB)
  103. static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd);
  104. #endif /* USB */
  105. /**
  106. * @}
  107. */
  108. /* Exported functions --------------------------------------------------------*/
  109. /** @defgroup PCD_Exported_Functions PCD Exported Functions
  110. * @{
  111. */
  112. /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
  113. * @brief Initialization and Configuration functions
  114. *
  115. @verbatim
  116. ===============================================================================
  117. ##### Initialization and de-initialization functions #####
  118. ===============================================================================
  119. [..] This section provides functions allowing to:
  120. @endverbatim
  121. * @{
  122. */
  123. /**
  124. * @brief Initializes the PCD according to the specified
  125. * parameters in the PCD_InitTypeDef and initialize the associated handle.
  126. * @param hpcd: PCD handle
  127. * @retval HAL status
  128. */
  129. HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
  130. {
  131. uint32_t index = 0U;
  132. /* Check the PCD handle allocation */
  133. if(hpcd == NULL)
  134. {
  135. return HAL_ERROR;
  136. }
  137. /* Check the parameters */
  138. assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
  139. if(hpcd->State == HAL_PCD_STATE_RESET)
  140. {
  141. /* Allocate lock resource and initialize it */
  142. hpcd->Lock = HAL_UNLOCKED;
  143. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  144. HAL_PCD_MspInit(hpcd);
  145. }
  146. hpcd->State = HAL_PCD_STATE_BUSY;
  147. /* Disable the Interrupts */
  148. __HAL_PCD_DISABLE(hpcd);
  149. /*Init the Core (common init.) */
  150. USB_CoreInit(hpcd->Instance, hpcd->Init);
  151. /* Force Device Mode*/
  152. USB_SetCurrentMode(hpcd->Instance , USB_DEVICE_MODE);
  153. /* Init endpoints structures */
  154. for (index = 0; index < hpcd->Init.dev_endpoints ; index++)
  155. {
  156. /* Init ep structure */
  157. hpcd->IN_ep[index].is_in = 1;
  158. hpcd->IN_ep[index].num = index;
  159. hpcd->IN_ep[index].tx_fifo_num = index;
  160. /* Control until ep is activated */
  161. hpcd->IN_ep[index].type = EP_TYPE_CTRL;
  162. hpcd->IN_ep[index].maxpacket = 0;
  163. hpcd->IN_ep[index].xfer_buff = 0;
  164. hpcd->IN_ep[index].xfer_len = 0;
  165. }
  166. for (index = 0; index < 15 ; index++)
  167. {
  168. hpcd->OUT_ep[index].is_in = 0;
  169. hpcd->OUT_ep[index].num = index;
  170. hpcd->IN_ep[index].tx_fifo_num = index;
  171. /* Control until ep is activated */
  172. hpcd->OUT_ep[index].type = EP_TYPE_CTRL;
  173. hpcd->OUT_ep[index].maxpacket = 0;
  174. hpcd->OUT_ep[index].xfer_buff = 0;
  175. hpcd->OUT_ep[index].xfer_len = 0;
  176. }
  177. /* Init Device */
  178. USB_DevInit(hpcd->Instance, hpcd->Init);
  179. hpcd->USB_Address = 0;
  180. hpcd->State= HAL_PCD_STATE_READY;
  181. /* Activate LPM */
  182. if (hpcd->Init.lpm_enable ==1)
  183. {
  184. HAL_PCDEx_ActivateLPM(hpcd);
  185. }
  186. /* Activate Battery charging */
  187. if (hpcd->Init.battery_charging_enable ==1)
  188. {
  189. HAL_PCDEx_ActivateBCD(hpcd);
  190. }
  191. USB_DevDisconnect (hpcd->Instance);
  192. return HAL_OK;
  193. }
  194. /**
  195. * @brief DeInitializes the PCD peripheral.
  196. * @param hpcd: PCD handle
  197. * @retval HAL status
  198. */
  199. HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
  200. {
  201. /* Check the PCD handle allocation */
  202. if(hpcd == NULL)
  203. {
  204. return HAL_ERROR;
  205. }
  206. hpcd->State = HAL_PCD_STATE_BUSY;
  207. /* Stop Device */
  208. HAL_PCD_Stop(hpcd);
  209. /* DeInit the low level hardware */
  210. HAL_PCD_MspDeInit(hpcd);
  211. hpcd->State = HAL_PCD_STATE_RESET;
  212. return HAL_OK;
  213. }
  214. /**
  215. * @brief Initializes the PCD MSP.
  216. * @param hpcd: PCD handle
  217. * @retval None
  218. */
  219. __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
  220. {
  221. /* Prevent unused argument(s) compilation warning */
  222. UNUSED(hpcd);
  223. /* NOTE : This function should not be modified, when the callback is needed,
  224. the HAL_PCD_MspInit could be implemented in the user file
  225. */
  226. }
  227. /**
  228. * @brief DeInitializes PCD MSP.
  229. * @param hpcd: PCD handle
  230. * @retval None
  231. */
  232. __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
  233. {
  234. /* Prevent unused argument(s) compilation warning */
  235. UNUSED(hpcd);
  236. /* NOTE : This function should not be modified, when the callback is needed,
  237. the HAL_PCD_MspDeInit could be implemented in the user file
  238. */
  239. }
  240. /**
  241. * @}
  242. */
  243. /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
  244. * @brief Data transfers functions
  245. *
  246. @verbatim
  247. ===============================================================================
  248. ##### IO operation functions #####
  249. ===============================================================================
  250. [..]
  251. This subsection provides a set of functions allowing to manage the PCD data
  252. transfers.
  253. @endverbatim
  254. * @{
  255. */
  256. /**
  257. * @brief Start The USB OTG Device.
  258. * @param hpcd: PCD handle
  259. * @retval HAL status
  260. */
  261. HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
  262. {
  263. __HAL_LOCK(hpcd);
  264. USB_DevConnect (hpcd->Instance);
  265. __HAL_PCD_ENABLE(hpcd);
  266. __HAL_UNLOCK(hpcd);
  267. return HAL_OK;
  268. }
  269. /**
  270. * @brief Stop The USB OTG Device.
  271. * @param hpcd: PCD handle
  272. * @retval HAL status
  273. */
  274. HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
  275. {
  276. __HAL_LOCK(hpcd);
  277. __HAL_PCD_DISABLE(hpcd);
  278. USB_StopDevice(hpcd->Instance);
  279. USB_DevDisconnect (hpcd->Instance);
  280. __HAL_UNLOCK(hpcd);
  281. return HAL_OK;
  282. }
  283. #if defined (USB_OTG_FS)
  284. /**
  285. * @brief Handles PCD interrupt request.
  286. * @param hpcd: PCD handle
  287. * @retval HAL status
  288. */
  289. void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
  290. {
  291. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  292. uint32_t index = 0U, ep_intr = 0U, epint = 0U, epnum = 0U;
  293. uint32_t fifoemptymsk = 0U, temp = 0U;
  294. USB_OTG_EPTypeDef *ep = NULL;
  295. uint32_t hclk = 80000000;
  296. /* ensure that we are in device mode */
  297. if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE)
  298. {
  299. /* avoid spurious interrupt */
  300. if(__HAL_PCD_IS_INVALID_INTERRUPT(hpcd))
  301. {
  302. return;
  303. }
  304. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS))
  305. {
  306. /* incorrect mode, acknowledge the interrupt */
  307. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
  308. }
  309. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT))
  310. {
  311. epnum = 0;
  312. /* Read in the device interrupt bits */
  313. ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
  314. while (ep_intr)
  315. {
  316. if (ep_intr & 0x1)
  317. {
  318. epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, epnum);
  319. if (( epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
  320. {
  321. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
  322. /* setup/out transaction management for Core ID 310A */
  323. if (USBx->GSNPSID == USB_OTG_CORE_ID_310A)
  324. {
  325. if (!(USBx_OUTEP(0)->DOEPINT & (0x1 << 15)))
  326. {
  327. if (hpcd->Init.dma_enable == 1)
  328. {
  329. hpcd->OUT_ep[epnum].xfer_count =
  330. hpcd->OUT_ep[epnum].maxpacket -
  331. (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
  332. hpcd->OUT_ep[epnum].xfer_buff +=
  333. hpcd->OUT_ep[epnum].maxpacket;
  334. }
  335. HAL_PCD_DataOutStageCallback(hpcd, epnum);
  336. if (hpcd->Init.dma_enable == 1)
  337. {
  338. if (!epnum && !hpcd->OUT_ep[epnum].xfer_len)
  339. {
  340. /* this is ZLP, so prepare EP0 for next setup */
  341. USB_EP0_OutStart(hpcd->Instance, 1, (uint8_t *)hpcd->Setup);
  342. }
  343. }
  344. }
  345. /* Clear the SetPktRcvd flag*/
  346. USBx_OUTEP(0)->DOEPINT |= (0x1 << 15) | (0x1 << 5);
  347. }
  348. else
  349. {
  350. if (hpcd->Init.dma_enable == 1)
  351. {
  352. hpcd->OUT_ep[epnum].xfer_count =
  353. hpcd->OUT_ep[epnum].maxpacket -
  354. (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
  355. hpcd->OUT_ep[epnum].xfer_buff += hpcd->OUT_ep[epnum].maxpacket;
  356. }
  357. HAL_PCD_DataOutStageCallback(hpcd, epnum);
  358. if (hpcd->Init.dma_enable == 1)
  359. {
  360. if (!epnum && !hpcd->OUT_ep[epnum].xfer_len)
  361. {
  362. /* this is ZLP, so prepare EP0 for next setup */
  363. USB_EP0_OutStart(hpcd->Instance, 1, (uint8_t *)hpcd->Setup);
  364. }
  365. }
  366. }
  367. }
  368. if(( epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
  369. {
  370. /* Inform the upper layer that a setup packet is available */
  371. HAL_PCD_SetupStageCallback(hpcd);
  372. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
  373. }
  374. if(( epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
  375. {
  376. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
  377. }
  378. #ifdef USB_OTG_DOEPINT_OTEPSPR
  379. /* Clear Status Phase Received interrupt */
  380. if(( epint & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
  381. {
  382. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
  383. }
  384. #endif /* USB_OTG_DOEPINT_OTEPSPR */
  385. }
  386. epnum++;
  387. ep_intr >>= 1;
  388. }
  389. }
  390. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
  391. {
  392. /* Read in the device interrupt bits */
  393. ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
  394. epnum = 0;
  395. while ( ep_intr )
  396. {
  397. if (ep_intr & 0x1) /* In ITR */
  398. {
  399. epint = USB_ReadDevInEPInterrupt(hpcd->Instance, epnum);
  400. if(( epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
  401. {
  402. fifoemptymsk = 0x1 << epnum;
  403. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  404. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
  405. if (hpcd->Init.dma_enable == 1)
  406. {
  407. hpcd->IN_ep[epnum].xfer_buff += hpcd->IN_ep[epnum].maxpacket;
  408. }
  409. HAL_PCD_DataInStageCallback(hpcd, epnum);
  410. if (hpcd->Init.dma_enable == 1)
  411. {
  412. /* this is ZLP, so prepare EP0 for next setup */
  413. if((epnum == 0) && (hpcd->IN_ep[epnum].xfer_len == 0))
  414. {
  415. /* prepare to rx more setup packets */
  416. USB_EP0_OutStart(hpcd->Instance, 1, (uint8_t *)hpcd->Setup);
  417. }
  418. }
  419. }
  420. if(( epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
  421. {
  422. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
  423. }
  424. if(( epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
  425. {
  426. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
  427. }
  428. if(( epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
  429. {
  430. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
  431. }
  432. if(( epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
  433. {
  434. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
  435. }
  436. if(( epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
  437. {
  438. PCD_WriteEmptyTxFifo(hpcd , epnum);
  439. }
  440. }
  441. epnum++;
  442. ep_intr >>= 1;
  443. }
  444. }
  445. /* Handle Resume Interrupt */
  446. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
  447. {
  448. /* Clear the Remote Wake-up Signaling */
  449. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  450. if(hpcd->LPM_State == LPM_L1)
  451. {
  452. hpcd->LPM_State = LPM_L0;
  453. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
  454. }
  455. else
  456. {
  457. HAL_PCD_ResumeCallback(hpcd);
  458. }
  459. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
  460. }
  461. /* Handle Suspend Interrupt */
  462. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
  463. {
  464. if((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
  465. {
  466. HAL_PCD_SuspendCallback(hpcd);
  467. }
  468. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
  469. }
  470. /* Handle LPM Interrupt */
  471. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT))
  472. {
  473. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
  474. if( hpcd->LPM_State == LPM_L0)
  475. {
  476. hpcd->LPM_State = LPM_L1;
  477. hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >>2 ;
  478. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
  479. }
  480. else
  481. {
  482. HAL_PCD_SuspendCallback(hpcd);
  483. }
  484. }
  485. /* Handle Reset Interrupt */
  486. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
  487. {
  488. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  489. USB_FlushTxFifo(hpcd->Instance , 0x10);
  490. for (index = 0; index < hpcd->Init.dev_endpoints ; index++)
  491. {
  492. USBx_INEP(index)->DIEPINT = 0xFF;
  493. USBx_OUTEP(index)->DOEPINT = 0xFF;
  494. }
  495. USBx_DEVICE->DAINT = 0xFFFFFFFF;
  496. USBx_DEVICE->DAINTMSK |= 0x10001;
  497. if(hpcd->Init.use_dedicated_ep1)
  498. {
  499. USBx_DEVICE->DOUTEP1MSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
  500. USBx_DEVICE->DINEP1MSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
  501. }
  502. else
  503. {
  504. #ifdef USB_OTG_DOEPINT_OTEPSPR
  505. USBx_DEVICE->DOEPMSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM | USB_OTG_DOEPMSK_OTEPSPRM);
  506. #else
  507. USBx_DEVICE->DOEPMSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
  508. #endif /* USB_OTG_DOEPINT_OTEPSPR */
  509. USBx_DEVICE->DIEPMSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
  510. }
  511. /* Set Default Address to 0 */
  512. USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
  513. /* setup EP0 to receive SETUP packets */
  514. USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
  515. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
  516. }
  517. /* Handle Enumeration done Interrupt */
  518. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
  519. {
  520. USB_ActivateSetup(hpcd->Instance);
  521. hpcd->Instance->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
  522. hpcd->Init.speed = USB_OTG_SPEED_FULL;
  523. hpcd->Init.ep0_mps = USB_OTG_FS_MAX_PACKET_SIZE ;
  524. /* The USBTRD is configured according to the tables below, depending on AHB frequency
  525. used by application. In the low AHB frequency range it is used to stretch enough the USB response
  526. time to IN tokens, the USB turnaround time, so to compensate for the longer AHB read access
  527. latency to the Data FIFO */
  528. /* Get hclk frequency value */
  529. hclk = HAL_RCC_GetHCLKFreq();
  530. if((hclk >= 14200000)&&(hclk < 15000000))
  531. {
  532. /* hclk Clock Range between 14.2-15 MHz */
  533. hpcd->Instance->GUSBCFG |= (uint32_t)((0xF << 10) & USB_OTG_GUSBCFG_TRDT);
  534. }
  535. else if((hclk >= 15000000)&&(hclk < 16000000))
  536. {
  537. /* hclk Clock Range between 15-16 MHz */
  538. hpcd->Instance->GUSBCFG |= (uint32_t)((0xE << 10) & USB_OTG_GUSBCFG_TRDT);
  539. }
  540. else if((hclk >= 16000000)&&(hclk < 17200000))
  541. {
  542. /* hclk Clock Range between 16-17.2 MHz */
  543. hpcd->Instance->GUSBCFG |= (uint32_t)((0xD << 10) & USB_OTG_GUSBCFG_TRDT);
  544. }
  545. else if((hclk >= 17200000)&&(hclk < 18500000))
  546. {
  547. /* hclk Clock Range between 17.2-18.5 MHz */
  548. hpcd->Instance->GUSBCFG |= (uint32_t)((0xC << 10) & USB_OTG_GUSBCFG_TRDT);
  549. }
  550. else if((hclk >= 18500000)&&(hclk < 20000000))
  551. {
  552. /* hclk Clock Range between 18.5-20 MHz */
  553. hpcd->Instance->GUSBCFG |= (uint32_t)((0xB << 10) & USB_OTG_GUSBCFG_TRDT);
  554. }
  555. else if((hclk >= 20000000)&&(hclk < 21800000))
  556. {
  557. /* hclk Clock Range between 20-21.8 MHz */
  558. hpcd->Instance->GUSBCFG |= (uint32_t)((0xA << 10) & USB_OTG_GUSBCFG_TRDT);
  559. }
  560. else if((hclk >= 21800000)&&(hclk < 24000000))
  561. {
  562. /* hclk Clock Range between 21.8-24 MHz */
  563. hpcd->Instance->GUSBCFG |= (uint32_t)((0x9 << 10) & USB_OTG_GUSBCFG_TRDT);
  564. }
  565. else if((hclk >= 24000000)&&(hclk < 27700000))
  566. {
  567. /* hclk Clock Range between 24-27.7 MHz */
  568. hpcd->Instance->GUSBCFG |= (uint32_t)((0x8 << 10) & USB_OTG_GUSBCFG_TRDT);
  569. }
  570. else if((hclk >= 27700000)&&(hclk < 32000000))
  571. {
  572. /* hclk Clock Range between 27.7-32 MHz */
  573. hpcd->Instance->GUSBCFG |= (uint32_t)((0x7 << 10) & USB_OTG_GUSBCFG_TRDT);
  574. }
  575. else /* if(hclk >= 32000000) */
  576. {
  577. /* hclk Clock Range between 32-80 MHz */
  578. hpcd->Instance->GUSBCFG |= (uint32_t)((0x6 << 10) & USB_OTG_GUSBCFG_TRDT);
  579. }
  580. HAL_PCD_ResetCallback(hpcd);
  581. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
  582. }
  583. /* Handle RxQLevel Interrupt */
  584. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL))
  585. {
  586. USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  587. temp = USBx->GRXSTSP;
  588. ep = &hpcd->OUT_ep[temp & USB_OTG_GRXSTSP_EPNUM];
  589. if(((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT)
  590. {
  591. if((temp & USB_OTG_GRXSTSP_BCNT) != 0)
  592. {
  593. USB_ReadPacket(USBx, ep->xfer_buff, (temp & USB_OTG_GRXSTSP_BCNT) >> 4);
  594. ep->xfer_buff += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  595. ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  596. }
  597. }
  598. else if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT)
  599. {
  600. USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8);
  601. ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  602. }
  603. USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  604. }
  605. /* Handle SOF Interrupt */
  606. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF))
  607. {
  608. HAL_PCD_SOFCallback(hpcd);
  609. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
  610. }
  611. /* Handle Incomplete ISO IN Interrupt */
  612. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR))
  613. {
  614. HAL_PCD_ISOINIncompleteCallback(hpcd, epnum);
  615. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
  616. }
  617. /* Handle Incomplete ISO OUT Interrupt */
  618. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
  619. {
  620. HAL_PCD_ISOOUTIncompleteCallback(hpcd, epnum);
  621. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
  622. }
  623. /* Handle Connection event Interrupt */
  624. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT))
  625. {
  626. HAL_PCD_ConnectCallback(hpcd);
  627. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
  628. }
  629. /* Handle Disconnection event Interrupt */
  630. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT))
  631. {
  632. temp = hpcd->Instance->GOTGINT;
  633. if((temp & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET)
  634. {
  635. HAL_PCD_DisconnectCallback(hpcd);
  636. }
  637. hpcd->Instance->GOTGINT |= temp;
  638. }
  639. }
  640. }
  641. #endif /* USB_OTG_FS */
  642. #if defined (USB)
  643. /**
  644. * @brief This function handles PCD interrupt request.
  645. * @param hpcd: PCD handle
  646. * @retval HAL status
  647. */
  648. void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
  649. {
  650. uint32_t wInterrupt_Mask = 0;
  651. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_CTR))
  652. {
  653. /* servicing of the endpoint correct transfer interrupt */
  654. /* clear of the CTR flag into the sub */
  655. PCD_EP_ISR_Handler(hpcd);
  656. }
  657. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_RESET))
  658. {
  659. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_RESET);
  660. HAL_PCD_ResetCallback(hpcd);
  661. HAL_PCD_SetAddress(hpcd, 0);
  662. }
  663. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_PMAOVR))
  664. {
  665. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_PMAOVR);
  666. }
  667. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_ERR))
  668. {
  669. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ERR);
  670. }
  671. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_WKUP))
  672. {
  673. hpcd->Instance->CNTR &= ~(USB_CNTR_LPMODE);
  674. /*set wInterrupt_Mask global variable*/
  675. wInterrupt_Mask = USB_CNTR_CTRM | USB_CNTR_WKUPM | USB_CNTR_SUSPM | USB_CNTR_ERRM \
  676. | USB_CNTR_SOFM | USB_CNTR_ESOFM | USB_CNTR_RESETM;
  677. /*Set interrupt mask*/
  678. hpcd->Instance->CNTR = wInterrupt_Mask;
  679. /* enable L1REQ interrupt */
  680. if (hpcd->Init.lpm_enable ==1)
  681. {
  682. wInterrupt_Mask |= USB_CNTR_L1REQM;
  683. /* Enable LPM support and enable ACK answer to LPM request*/
  684. USB_TypeDef *USBx = hpcd->Instance;
  685. hpcd->lpm_active = ENABLE;
  686. hpcd->LPM_State = LPM_L0;
  687. USBx->LPMCSR |= (USB_LPMCSR_LMPEN);
  688. USBx->LPMCSR |= (USB_LPMCSR_LPMACK);
  689. }
  690. if(hpcd->LPM_State == LPM_L1)
  691. {
  692. hpcd->LPM_State = LPM_L0;
  693. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
  694. }
  695. HAL_PCD_ResumeCallback(hpcd);
  696. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_WKUP);
  697. }
  698. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_SUSP))
  699. {
  700. /* clear of the ISTR bit must be done after setting of CNTR_FSUSP */
  701. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SUSP);
  702. /* Force low-power mode in the macrocell */
  703. hpcd->Instance->CNTR |= USB_CNTR_FSUSP;
  704. hpcd->Instance->CNTR |= USB_CNTR_LPMODE;
  705. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_WKUP) == 0)
  706. {
  707. HAL_PCD_SuspendCallback(hpcd);
  708. }
  709. }
  710. /* Handle LPM Interrupt */
  711. if(__HAL_PCD_GET_FLAG(hpcd, USB_ISTR_L1REQ))
  712. {
  713. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_L1REQ);
  714. if( hpcd->LPM_State == LPM_L0)
  715. {
  716. /* Force suspend and low-power mode before going to L1 state*/
  717. hpcd->Instance->CNTR |= USB_CNTR_LPMODE;
  718. hpcd->Instance->CNTR |= USB_CNTR_FSUSP;
  719. hpcd->LPM_State = LPM_L1;
  720. hpcd->BESL = (hpcd->Instance->LPMCSR & USB_LPMCSR_BESL) >>2 ;
  721. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
  722. }
  723. else
  724. {
  725. HAL_PCD_SuspendCallback(hpcd);
  726. }
  727. }
  728. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_SOF))
  729. {
  730. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_SOF);
  731. HAL_PCD_SOFCallback(hpcd);
  732. }
  733. if (__HAL_PCD_GET_FLAG (hpcd, USB_ISTR_ESOF))
  734. {
  735. /* clear ESOF flag in ISTR */
  736. __HAL_PCD_CLEAR_FLAG(hpcd, USB_ISTR_ESOF);
  737. }
  738. }
  739. #endif /* USB */
  740. /**
  741. * @brief Data OUT stage callback.
  742. * @param hpcd: PCD handle
  743. * @param epnum: endpoint number
  744. * @retval None
  745. */
  746. __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  747. {
  748. /* Prevent unused argument(s) compilation warning */
  749. UNUSED(hpcd);
  750. UNUSED(epnum);
  751. /* NOTE : This function should not be modified, when the callback is needed,
  752. the HAL_PCD_DataOutStageCallback could be implemented in the user file
  753. */
  754. }
  755. /**
  756. * @brief Data IN stage callback.
  757. * @param hpcd: PCD handle
  758. * @param epnum: endpoint number
  759. * @retval None
  760. */
  761. __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  762. {
  763. /* Prevent unused argument(s) compilation warning */
  764. UNUSED(hpcd);
  765. UNUSED(epnum);
  766. /* NOTE : This function should not be modified, when the callback is needed,
  767. the HAL_PCD_DataInStageCallback could be implemented in the user file
  768. */
  769. }
  770. /**
  771. * @brief Setup stage callback.
  772. * @param hpcd: PCD handle
  773. * @retval None
  774. */
  775. __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  776. {
  777. /* Prevent unused argument(s) compilation warning */
  778. UNUSED(hpcd);
  779. /* NOTE : This function should not be modified, when the callback is needed,
  780. the HAL_PCD_SetupStageCallback could be implemented in the user file
  781. */
  782. }
  783. /**
  784. * @brief USB Start Of Frame callback.
  785. * @param hpcd: PCD handle
  786. * @retval None
  787. */
  788. __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  789. {
  790. /* Prevent unused argument(s) compilation warning */
  791. UNUSED(hpcd);
  792. /* NOTE : This function should not be modified, when the callback is needed,
  793. the HAL_PCD_SOFCallback could be implemented in the user file
  794. */
  795. }
  796. /**
  797. * @brief USB Reset callback.
  798. * @param hpcd: PCD handle
  799. * @retval None
  800. */
  801. __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  802. {
  803. /* Prevent unused argument(s) compilation warning */
  804. UNUSED(hpcd);
  805. /* NOTE : This function should not be modified, when the callback is needed,
  806. the HAL_PCD_ResetCallback could be implemented in the user file
  807. */
  808. }
  809. /**
  810. * @brief Suspend event callback.
  811. * @param hpcd: PCD handle
  812. * @retval None
  813. */
  814. __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  815. {
  816. /* Prevent unused argument(s) compilation warning */
  817. UNUSED(hpcd);
  818. /* NOTE : This function should not be modified, when the callback is needed,
  819. the HAL_PCD_SuspendCallback could be implemented in the user file
  820. */
  821. }
  822. /**
  823. * @brief Resume event callback.
  824. * @param hpcd: PCD handle
  825. * @retval None
  826. */
  827. __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  828. {
  829. /* Prevent unused argument(s) compilation warning */
  830. UNUSED(hpcd);
  831. /* NOTE : This function should not be modified, when the callback is needed,
  832. the HAL_PCD_ResumeCallback could be implemented in the user file
  833. */
  834. }
  835. /**
  836. * @brief Incomplete ISO OUT callback.
  837. * @param hpcd: PCD handle
  838. * @param epnum: endpoint number
  839. * @retval None
  840. */
  841. __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  842. {
  843. /* Prevent unused argument(s) compilation warning */
  844. UNUSED(hpcd);
  845. UNUSED(epnum);
  846. /* NOTE : This function should not be modified, when the callback is needed,
  847. the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
  848. */
  849. }
  850. /**
  851. * @brief Incomplete ISO IN callback.
  852. * @param hpcd: PCD handle
  853. * @param epnum: endpoint number
  854. * @retval None
  855. */
  856. __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  857. {
  858. /* Prevent unused argument(s) compilation warning */
  859. UNUSED(hpcd);
  860. UNUSED(epnum);
  861. /* NOTE : This function should not be modified, when the callback is needed,
  862. the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
  863. */
  864. }
  865. /**
  866. * @brief Connection event callback.
  867. * @param hpcd: PCD handle
  868. * @retval None
  869. */
  870. __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  871. {
  872. /* Prevent unused argument(s) compilation warning */
  873. UNUSED(hpcd);
  874. /* NOTE : This function should not be modified, when the callback is needed,
  875. the HAL_PCD_ConnectCallback could be implemented in the user file
  876. */
  877. }
  878. /**
  879. * @brief Disconnection event callback.
  880. * @param hpcd: PCD handle
  881. * @retval None
  882. */
  883. __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  884. {
  885. /* Prevent unused argument(s) compilation warning */
  886. UNUSED(hpcd);
  887. /* NOTE : This function should not be modified, when the callback is needed,
  888. the HAL_PCD_DisconnectCallback could be implemented in the user file
  889. */
  890. }
  891. /**
  892. * @}
  893. */
  894. /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
  895. * @brief management functions
  896. *
  897. @verbatim
  898. ===============================================================================
  899. ##### Peripheral Control functions #####
  900. ===============================================================================
  901. [..]
  902. This subsection provides a set of functions allowing to control the PCD data
  903. transfers.
  904. @endverbatim
  905. * @{
  906. */
  907. /**
  908. * @brief Connect the USB device.
  909. * @param hpcd: PCD handle
  910. * @retval HAL status
  911. */
  912. HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
  913. {
  914. __HAL_LOCK(hpcd);
  915. USB_DevConnect(hpcd->Instance);
  916. __HAL_UNLOCK(hpcd);
  917. return HAL_OK;
  918. }
  919. /**
  920. * @brief Disconnect the USB device.
  921. * @param hpcd: PCD handle
  922. * @retval HAL status
  923. */
  924. HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
  925. {
  926. __HAL_LOCK(hpcd);
  927. USB_DevDisconnect(hpcd->Instance);
  928. __HAL_UNLOCK(hpcd);
  929. return HAL_OK;
  930. }
  931. /**
  932. * @brief Set the USB Device address.
  933. * @param hpcd: PCD handle
  934. * @param address: new device address
  935. * @retval HAL status
  936. */
  937. HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
  938. {
  939. __HAL_LOCK(hpcd);
  940. hpcd->USB_Address = address;
  941. USB_SetDevAddress(hpcd->Instance, address);
  942. __HAL_UNLOCK(hpcd);
  943. return HAL_OK;
  944. }
  945. /**
  946. * @brief Open and configure an endpoint.
  947. * @param hpcd: PCD handle
  948. * @param ep_addr: endpoint address
  949. * @param ep_mps: endpoint max packet size
  950. * @param ep_type: endpoint type
  951. * @retval HAL status
  952. */
  953. HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint16_t ep_mps, uint8_t ep_type)
  954. {
  955. HAL_StatusTypeDef ret = HAL_OK;
  956. PCD_EPTypeDef *ep = NULL;
  957. if ((ep_addr & 0x80) == 0x80)
  958. {
  959. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  960. }
  961. else
  962. {
  963. ep = &hpcd->OUT_ep[ep_addr & 0x7F];
  964. }
  965. ep->num = ep_addr & 0x7F;
  966. ep->is_in = (0x80 & ep_addr) != 0;
  967. ep->maxpacket = ep_mps;
  968. ep->type = ep_type;
  969. __HAL_LOCK(hpcd);
  970. USB_ActivateEndpoint(hpcd->Instance , ep);
  971. __HAL_UNLOCK(hpcd);
  972. return ret;
  973. }
  974. /**
  975. * @brief Deactivate an endpoint.
  976. * @param hpcd: PCD handle
  977. * @param ep_addr: endpoint address
  978. * @retval HAL status
  979. */
  980. HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  981. {
  982. PCD_EPTypeDef *ep = NULL;
  983. if ((ep_addr & 0x80) == 0x80)
  984. {
  985. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  986. }
  987. else
  988. {
  989. ep = &hpcd->OUT_ep[ep_addr & 0x7F];
  990. }
  991. ep->num = ep_addr & 0x7F;
  992. ep->is_in = (0x80 & ep_addr) != 0;
  993. __HAL_LOCK(hpcd);
  994. USB_DeactivateEndpoint(hpcd->Instance , ep);
  995. __HAL_UNLOCK(hpcd);
  996. return HAL_OK;
  997. }
  998. /**
  999. * @brief Receive an amount of data.
  1000. * @param hpcd: PCD handle
  1001. * @param ep_addr: endpoint address
  1002. * @param pBuf: pointer to the reception buffer
  1003. * @param len: amount of data to be received
  1004. * @retval HAL status
  1005. */
  1006. HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  1007. {
  1008. PCD_EPTypeDef *ep = NULL;
  1009. ep = &hpcd->OUT_ep[ep_addr & 0x7F];
  1010. /*setup and start the Xfer */
  1011. ep->xfer_buff = pBuf;
  1012. ep->xfer_len = len;
  1013. ep->xfer_count = 0;
  1014. ep->is_in = 0;
  1015. ep->num = ep_addr & 0x7F;
  1016. if ((ep_addr & 0x7F) == 0 )
  1017. {
  1018. USB_EP0StartXfer(hpcd->Instance, ep, hpcd->Init.dma_enable);
  1019. }
  1020. else
  1021. {
  1022. USB_EPStartXfer(hpcd->Instance, ep, hpcd->Init.dma_enable);
  1023. }
  1024. return HAL_OK;
  1025. }
  1026. /**
  1027. * @brief Get Received Data Size.
  1028. * @param hpcd: PCD handle
  1029. * @param ep_addr: endpoint address
  1030. * @retval Data Size
  1031. */
  1032. uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1033. {
  1034. return hpcd->OUT_ep[ep_addr & 0x7F].xfer_count;
  1035. }
  1036. /**
  1037. * @brief Send an amount of data.
  1038. * @param hpcd: PCD handle
  1039. * @param ep_addr: endpoint address
  1040. * @param pBuf: pointer to the transmission buffer
  1041. * @param len: amount of data to be sent
  1042. * @retval HAL status
  1043. */
  1044. HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  1045. {
  1046. PCD_EPTypeDef *ep = NULL;
  1047. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  1048. /*setup and start the Xfer */
  1049. ep->xfer_buff = pBuf;
  1050. ep->xfer_len = len;
  1051. ep->xfer_count = 0;
  1052. ep->is_in = 1;
  1053. ep->num = ep_addr & 0x7F;
  1054. if ((ep_addr & 0x7F) == 0 )
  1055. {
  1056. USB_EP0StartXfer(hpcd->Instance,ep, hpcd->Init.dma_enable);
  1057. }
  1058. else
  1059. {
  1060. USB_EPStartXfer(hpcd->Instance, ep, hpcd->Init.dma_enable);
  1061. }
  1062. return HAL_OK;
  1063. }
  1064. /**
  1065. * @brief Set a STALL condition over an endpoint.
  1066. * @param hpcd: PCD handle
  1067. * @param ep_addr: endpoint address
  1068. * @retval HAL status
  1069. */
  1070. HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1071. {
  1072. PCD_EPTypeDef *ep = NULL;
  1073. if ((0x80 & ep_addr) == 0x80)
  1074. {
  1075. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  1076. }
  1077. else
  1078. {
  1079. ep = &hpcd->OUT_ep[ep_addr];
  1080. }
  1081. ep->is_stall = 1;
  1082. ep->num = ep_addr & 0x7F;
  1083. ep->is_in = ((ep_addr & 0x80) == 0x80);
  1084. __HAL_LOCK(hpcd);
  1085. USB_EPSetStall(hpcd->Instance , ep);
  1086. if((ep_addr & 0x7F) == 0)
  1087. {
  1088. USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
  1089. }
  1090. __HAL_UNLOCK(hpcd);
  1091. return HAL_OK;
  1092. }
  1093. /**
  1094. * @brief Clear a STALL condition over in an endpoint.
  1095. * @param hpcd: PCD handle
  1096. * @param ep_addr: endpoint address
  1097. * @retval HAL status
  1098. */
  1099. HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1100. {
  1101. PCD_EPTypeDef *ep = NULL;
  1102. if ((0x80 & ep_addr) == 0x80)
  1103. {
  1104. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  1105. }
  1106. else
  1107. {
  1108. ep = &hpcd->OUT_ep[ep_addr];
  1109. }
  1110. ep->is_stall = 0;
  1111. ep->num = ep_addr & 0x7F;
  1112. ep->is_in = ((ep_addr & 0x80) == 0x80);
  1113. __HAL_LOCK(hpcd);
  1114. USB_EPClearStall(hpcd->Instance , ep);
  1115. __HAL_UNLOCK(hpcd);
  1116. return HAL_OK;
  1117. }
  1118. /**
  1119. * @brief Flush an endpoint.
  1120. * @param hpcd: PCD handle
  1121. * @param ep_addr: endpoint address
  1122. * @retval HAL status
  1123. */
  1124. HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1125. {
  1126. __HAL_LOCK(hpcd);
  1127. if ((ep_addr & 0x80) == 0x80)
  1128. {
  1129. USB_FlushTxFifo(hpcd->Instance, ep_addr & 0x7F);
  1130. }
  1131. else
  1132. {
  1133. USB_FlushRxFifo(hpcd->Instance);
  1134. }
  1135. __HAL_UNLOCK(hpcd);
  1136. return HAL_OK;
  1137. }
  1138. /**
  1139. * @brief Activate remote wakeup signalling.
  1140. * @param hpcd: PCD handle
  1141. * @retval HAL status
  1142. */
  1143. HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1144. {
  1145. return(USB_ActivateRemoteWakeup(hpcd->Instance));
  1146. }
  1147. /**
  1148. * @brief De-activate remote wakeup signalling.
  1149. * @param hpcd: PCD handle
  1150. * @retval HAL status
  1151. */
  1152. HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1153. {
  1154. return(USB_DeActivateRemoteWakeup(hpcd->Instance));
  1155. }
  1156. /**
  1157. * @}
  1158. */
  1159. /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
  1160. * @brief Peripheral State functions
  1161. *
  1162. @verbatim
  1163. ===============================================================================
  1164. ##### Peripheral State functions #####
  1165. ===============================================================================
  1166. [..]
  1167. This subsection permits to get in run-time the status of the peripheral
  1168. and the data flow.
  1169. @endverbatim
  1170. * @{
  1171. */
  1172. /**
  1173. * @brief Return the PCD handle state.
  1174. * @param hpcd: PCD handle
  1175. * @retval HAL state
  1176. */
  1177. PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
  1178. {
  1179. return hpcd->State;
  1180. }
  1181. /**
  1182. * @}
  1183. */
  1184. /**
  1185. * @}
  1186. */
  1187. /* Private functions ---------------------------------------------------------*/
  1188. /** @addtogroup PCD_Private_Functions
  1189. * @{
  1190. */
  1191. #if defined (USB_OTG_FS)
  1192. /**
  1193. * @brief Check FIFO for the next packet to be loaded.
  1194. * @param hpcd: PCD handle
  1195. * @param epnum: endpoint number
  1196. * @retval HAL status
  1197. */
  1198. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1199. {
  1200. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1201. USB_OTG_EPTypeDef *ep = NULL;
  1202. int32_t len = 0U;
  1203. uint32_t len32b = 0;
  1204. uint32_t fifoemptymsk = 0;
  1205. ep = &hpcd->IN_ep[epnum];
  1206. len = ep->xfer_len - ep->xfer_count;
  1207. if (len > ep->maxpacket)
  1208. {
  1209. len = ep->maxpacket;
  1210. }
  1211. len32b = (len + 3) / 4;
  1212. while ( (USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) > len32b &&
  1213. ep->xfer_count < ep->xfer_len &&
  1214. ep->xfer_len != 0)
  1215. {
  1216. /* Write the FIFO */
  1217. len = ep->xfer_len - ep->xfer_count;
  1218. if (len > ep->maxpacket)
  1219. {
  1220. len = ep->maxpacket;
  1221. }
  1222. len32b = (len + 3) / 4;
  1223. USB_WritePacket(USBx, ep->xfer_buff, epnum, len, hpcd->Init.dma_enable);
  1224. ep->xfer_buff += len;
  1225. ep->xfer_count += len;
  1226. }
  1227. if(len <= 0)
  1228. {
  1229. fifoemptymsk = 0x1 << epnum;
  1230. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  1231. }
  1232. return HAL_OK;
  1233. }
  1234. #endif /* USB_OTG_FS */
  1235. #if defined (USB)
  1236. /**
  1237. * @brief This function handles PCD Endpoint interrupt request.
  1238. * @param hpcd: PCD handle
  1239. * @retval HAL status
  1240. */
  1241. static HAL_StatusTypeDef PCD_EP_ISR_Handler(PCD_HandleTypeDef *hpcd)
  1242. {
  1243. PCD_EPTypeDef *ep = NULL;
  1244. uint16_t count = 0;
  1245. uint8_t epindex = 0;
  1246. __IO uint16_t wIstr = 0;
  1247. __IO uint16_t wEPVal = 0;
  1248. /* stay in loop while pending interrupts */
  1249. while (((wIstr = hpcd->Instance->ISTR) & USB_ISTR_CTR) != 0)
  1250. {
  1251. /* extract highest priority endpoint number */
  1252. epindex = (uint8_t)(wIstr & USB_ISTR_EP_ID);
  1253. if (epindex == 0)
  1254. {
  1255. /* Decode and service control endpoint interrupt */
  1256. /* DIR bit = origin of the interrupt */
  1257. if ((wIstr & USB_ISTR_DIR) == 0)
  1258. {
  1259. /* DIR = 0 */
  1260. /* DIR = 0 => IN int */
  1261. /* DIR = 0 implies that (EP_CTR_TX = 1) always */
  1262. PCD_CLEAR_TX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1263. ep = &hpcd->IN_ep[0];
  1264. ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
  1265. ep->xfer_buff += ep->xfer_count;
  1266. /* TX COMPLETE */
  1267. HAL_PCD_DataInStageCallback(hpcd, 0);
  1268. if((hpcd->USB_Address > 0)&& ( ep->xfer_len == 0))
  1269. {
  1270. hpcd->Instance->DADDR = (hpcd->USB_Address | USB_DADDR_EF);
  1271. hpcd->USB_Address = 0;
  1272. }
  1273. }
  1274. else
  1275. {
  1276. /* DIR = 1 */
  1277. /* DIR = 1 & CTR_RX => SETUP or OUT int */
  1278. /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */
  1279. ep = &hpcd->OUT_ep[0];
  1280. wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, PCD_ENDP0);
  1281. if ((wEPVal & USB_EP_SETUP) != 0)
  1282. {
  1283. /* Get SETUP Packet*/
  1284. ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1285. USB_ReadPMA(hpcd->Instance, (uint8_t*)hpcd->Setup ,ep->pmaadress , ep->xfer_count);
  1286. /* SETUP bit kept frozen while CTR_RX = 1*/
  1287. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1288. /* Process SETUP Packet*/
  1289. HAL_PCD_SetupStageCallback(hpcd);
  1290. }
  1291. else if ((wEPVal & USB_EP_CTR_RX) != 0)
  1292. {
  1293. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, PCD_ENDP0);
  1294. /* Get Control Data OUT Packet*/
  1295. ep->xfer_count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1296. if (ep->xfer_count != 0)
  1297. {
  1298. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, ep->xfer_count);
  1299. ep->xfer_buff+=ep->xfer_count;
  1300. }
  1301. /* Process Control Data OUT Packet*/
  1302. HAL_PCD_DataOutStageCallback(hpcd, 0);
  1303. PCD_SET_EP_RX_CNT(hpcd->Instance, PCD_ENDP0, ep->maxpacket);
  1304. PCD_SET_EP_RX_STATUS(hpcd->Instance, PCD_ENDP0, USB_EP_RX_VALID);
  1305. }
  1306. }
  1307. }
  1308. else
  1309. {
  1310. /* Decode and service non control endpoints interrupt */
  1311. /* process related endpoint register */
  1312. wEPVal = PCD_GET_ENDPOINT(hpcd->Instance, epindex);
  1313. if ((wEPVal & USB_EP_CTR_RX) != 0)
  1314. {
  1315. /* clear int flag */
  1316. PCD_CLEAR_RX_EP_CTR(hpcd->Instance, epindex);
  1317. ep = &hpcd->OUT_ep[epindex];
  1318. /* OUT double Buffering*/
  1319. if (ep->doublebuffer == 0)
  1320. {
  1321. count = PCD_GET_EP_RX_CNT(hpcd->Instance, ep->num);
  1322. if (count != 0)
  1323. {
  1324. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, count);
  1325. }
  1326. }
  1327. else
  1328. {
  1329. if (PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_RX)
  1330. {
  1331. /*read from endpoint BUF0Addr buffer*/
  1332. count = PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  1333. if (count != 0)
  1334. {
  1335. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, count);
  1336. }
  1337. }
  1338. else
  1339. {
  1340. /*read from endpoint BUF1Addr buffer*/
  1341. count = PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  1342. if (count != 0)
  1343. {
  1344. USB_ReadPMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, count);
  1345. }
  1346. }
  1347. PCD_FreeUserBuffer(hpcd->Instance, ep->num, PCD_EP_DBUF_OUT);
  1348. }
  1349. /*multi-packet on the NON control OUT endpoint*/
  1350. ep->xfer_count+=count;
  1351. ep->xfer_buff+=count;
  1352. if ((ep->xfer_len == 0) || (count < ep->maxpacket))
  1353. {
  1354. /* RX COMPLETE */
  1355. HAL_PCD_DataOutStageCallback(hpcd, ep->num);
  1356. }
  1357. else
  1358. {
  1359. HAL_PCD_EP_Receive(hpcd, ep->num, ep->xfer_buff, ep->xfer_len);
  1360. }
  1361. } /* if((wEPVal & EP_CTR_RX) */
  1362. if ((wEPVal & USB_EP_CTR_TX) != 0)
  1363. {
  1364. ep = &hpcd->IN_ep[epindex];
  1365. /* clear int flag */
  1366. PCD_CLEAR_TX_EP_CTR(hpcd->Instance, epindex);
  1367. /* IN double Buffering*/
  1368. if (ep->doublebuffer == 0)
  1369. {
  1370. ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
  1371. if (ep->xfer_count != 0)
  1372. {
  1373. USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaadress, ep->xfer_count);
  1374. }
  1375. }
  1376. else
  1377. {
  1378. if (PCD_GET_ENDPOINT(hpcd->Instance, ep->num) & USB_EP_DTOG_TX)
  1379. {
  1380. /*read from endpoint BUF0Addr buffer*/
  1381. ep->xfer_count = PCD_GET_EP_DBUF0_CNT(hpcd->Instance, ep->num);
  1382. if (ep->xfer_count != 0)
  1383. {
  1384. USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr0, ep->xfer_count);
  1385. }
  1386. }
  1387. else
  1388. {
  1389. /*read from endpoint BUF1Addr buffer*/
  1390. ep->xfer_count = PCD_GET_EP_DBUF1_CNT(hpcd->Instance, ep->num);
  1391. if (ep->xfer_count != 0)
  1392. {
  1393. USB_WritePMA(hpcd->Instance, ep->xfer_buff, ep->pmaaddr1, ep->xfer_count);
  1394. }
  1395. }
  1396. PCD_FreeUserBuffer(hpcd->Instance, ep->num, PCD_EP_DBUF_IN);
  1397. }
  1398. /*multi-packet on the NON control IN endpoint*/
  1399. ep->xfer_count = PCD_GET_EP_TX_CNT(hpcd->Instance, ep->num);
  1400. ep->xfer_buff+=ep->xfer_count;
  1401. /* Zero Length Packet? */
  1402. if (ep->xfer_len == 0)
  1403. {
  1404. /* TX COMPLETE */
  1405. HAL_PCD_DataInStageCallback(hpcd, ep->num);
  1406. }
  1407. else
  1408. {
  1409. HAL_PCD_EP_Transmit(hpcd, ep->num, ep->xfer_buff, ep->xfer_len);
  1410. }
  1411. }
  1412. }
  1413. }
  1414. return HAL_OK;
  1415. }
  1416. #endif /* USB */
  1417. /**
  1418. * @}
  1419. */
  1420. #endif /* STM32L432xx || STM32L433xx || STM32L442xx || STM32L443xx || */
  1421. /* STM32L452xx || STM32L462xx || */
  1422. /* STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || */
  1423. /* STM32L496xx || STM32L4A6xx || */
  1424. /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  1425. #endif /* HAL_PCD_MODULE_ENABLED */
  1426. /**
  1427. * @}
  1428. */
  1429. /**
  1430. * @}
  1431. */
  1432. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/