stm32f1xx_hal_pcd.c 39 KB

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