stm32f4xx_hal_pcd.c 37 KB

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