stm32f4xx_hal_pcd.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_pcd.c
  4. * @author MCD Application Team
  5. * @brief PCD HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the USB Peripheral Controller:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State functions
  12. *
  13. @verbatim
  14. ==============================================================================
  15. ##### How to use this driver #####
  16. ==============================================================================
  17. [..]
  18. The PCD HAL driver can be used as follows:
  19. (#) Declare a PCD_HandleTypeDef handle structure, for example:
  20. PCD_HandleTypeDef hpcd;
  21. (#) Fill parameters of Init structure in HCD handle
  22. (#) Call HAL_PCD_Init() API to initialize the PCD peripheral (Core, Device core, ...)
  23. (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
  24. (##) Enable the PCD/USB Low Level interface clock using
  25. (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
  26. (+++) __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); (For High Speed Mode)
  27. (##) Initialize the related GPIO clocks
  28. (##) Configure PCD pin-out
  29. (##) Configure PCD NVIC interrupt
  30. (#)Associate the Upper USB device stack to the HAL PCD Driver:
  31. (##) hpcd.pData = pdev;
  32. (#)Enable PCD transmission and reception:
  33. (##) HAL_PCD_Start();
  34. @endverbatim
  35. ******************************************************************************
  36. * @attention
  37. *
  38. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  39. *
  40. * Redistribution and use in source and binary forms, with or without modification,
  41. * are permitted provided that the following conditions are met:
  42. * 1. Redistributions of source code must retain the above copyright notice,
  43. * this list of conditions and the following disclaimer.
  44. * 2. Redistributions in binary form must reproduce the above copyright notice,
  45. * this list of conditions and the following disclaimer in the documentation
  46. * and/or other materials provided with the distribution.
  47. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  48. * may be used to endorse or promote products derived from this software
  49. * without specific prior written permission.
  50. *
  51. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  52. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  53. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  54. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  55. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  56. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  57. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  58. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  59. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  60. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  61. *
  62. ******************************************************************************
  63. */
  64. /* Includes ------------------------------------------------------------------*/
  65. #include "stm32f4xx_hal.h"
  66. /** @addtogroup STM32F4xx_HAL_Driver
  67. * @{
  68. */
  69. /** @defgroup PCD PCD
  70. * @brief PCD HAL module driver
  71. * @{
  72. */
  73. #ifdef HAL_PCD_MODULE_ENABLED
  74. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || \
  75. defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
  76. defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE) || defined(STM32F446xx) || \
  77. defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
  78. defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  79. /* Private types -------------------------------------------------------------*/
  80. /* Private variables ---------------------------------------------------------*/
  81. /* Private constants ---------------------------------------------------------*/
  82. /* Private macros ------------------------------------------------------------*/
  83. /** @defgroup PCD_Private_Macros PCD Private Macros
  84. * @{
  85. */
  86. #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
  87. #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
  88. /**
  89. * @}
  90. */
  91. /* Private functions prototypes ----------------------------------------------*/
  92. /** @defgroup PCD_Private_Functions PCD Private Functions
  93. * @{
  94. */
  95. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
  96. /**
  97. * @}
  98. */
  99. /* Exported functions --------------------------------------------------------*/
  100. /** @defgroup PCD_Exported_Functions PCD Exported Functions
  101. * @{
  102. */
  103. /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
  104. * @brief Initialization and Configuration functions
  105. *
  106. @verbatim
  107. ===============================================================================
  108. ##### Initialization and de-initialization functions #####
  109. ===============================================================================
  110. [..] This section provides functions allowing to:
  111. @endverbatim
  112. * @{
  113. */
  114. /**
  115. * @brief Initializes the PCD according to the specified
  116. * parameters in the PCD_InitTypeDef and initialize the associated handle.
  117. * @param hpcd PCD handle
  118. * @retval HAL status
  119. */
  120. HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
  121. {
  122. uint32_t i = 0U;
  123. /* Check the PCD handle allocation */
  124. if(hpcd == NULL)
  125. {
  126. return HAL_ERROR;
  127. }
  128. /* Check the parameters */
  129. assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
  130. hpcd->State = HAL_PCD_STATE_BUSY;
  131. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  132. HAL_PCD_MspInit(hpcd);
  133. /* Disable the Interrupts */
  134. __HAL_PCD_DISABLE(hpcd);
  135. /*Init the Core (common init.) */
  136. USB_CoreInit(hpcd->Instance, hpcd->Init);
  137. /* Force Device Mode*/
  138. USB_SetCurrentMode(hpcd->Instance , USB_OTG_DEVICE_MODE);
  139. /* Init endpoints structures */
  140. for (i = 0U; i < 15U; i++)
  141. {
  142. /* Init ep structure */
  143. hpcd->IN_ep[i].is_in = 1U;
  144. hpcd->IN_ep[i].num = i;
  145. hpcd->IN_ep[i].tx_fifo_num = i;
  146. /* Control until ep is activated */
  147. hpcd->IN_ep[i].type = EP_TYPE_CTRL;
  148. hpcd->IN_ep[i].maxpacket = 0U;
  149. hpcd->IN_ep[i].xfer_buff = 0U;
  150. hpcd->IN_ep[i].xfer_len = 0U;
  151. }
  152. for (i = 0U; i < 15U; i++)
  153. {
  154. hpcd->OUT_ep[i].is_in = 0U;
  155. hpcd->OUT_ep[i].num = i;
  156. hpcd->IN_ep[i].tx_fifo_num = i;
  157. /* Control until ep is activated */
  158. hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
  159. hpcd->OUT_ep[i].maxpacket = 0U;
  160. hpcd->OUT_ep[i].xfer_buff = 0U;
  161. hpcd->OUT_ep[i].xfer_len = 0U;
  162. hpcd->Instance->DIEPTXF[i] = 0U;
  163. }
  164. /* Init Device */
  165. USB_DevInit(hpcd->Instance, hpcd->Init);
  166. hpcd->State= HAL_PCD_STATE_READY;
  167. #ifdef USB_OTG_GLPMCFG_LPMEN
  168. /* Activate LPM */
  169. if (hpcd->Init.lpm_enable == 1U)
  170. {
  171. HAL_PCDEx_ActivateLPM(hpcd);
  172. }
  173. #endif /* USB_OTG_GLPMCFG_LPMEN */
  174. #ifdef USB_OTG_GCCFG_BCDEN
  175. /* Activate Battery charging */
  176. if (hpcd->Init.battery_charging_enable == 1U)
  177. {
  178. HAL_PCDEx_ActivateBCD(hpcd);
  179. }
  180. #endif /* USB_OTG_GCCFG_BCDEN */
  181. USB_DevDisconnect (hpcd->Instance);
  182. return HAL_OK;
  183. }
  184. /**
  185. * @brief DeInitializes the PCD peripheral.
  186. * @param hpcd PCD handle
  187. * @retval HAL status
  188. */
  189. HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
  190. {
  191. /* Check the PCD handle allocation */
  192. if(hpcd == NULL)
  193. {
  194. return HAL_ERROR;
  195. }
  196. hpcd->State = HAL_PCD_STATE_BUSY;
  197. /* Stop Device */
  198. HAL_PCD_Stop(hpcd);
  199. /* DeInit the low level hardware */
  200. HAL_PCD_MspDeInit(hpcd);
  201. hpcd->State = HAL_PCD_STATE_RESET;
  202. return HAL_OK;
  203. }
  204. /**
  205. * @brief Initializes the PCD MSP.
  206. * @param hpcd PCD handle
  207. * @retval None
  208. */
  209. __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
  210. {
  211. /* Prevent unused argument(s) compilation warning */
  212. UNUSED(hpcd);
  213. /* NOTE : This function Should not be modified, when the callback is needed,
  214. the HAL_PCD_MspInit could be implemented in the user file
  215. */
  216. }
  217. /**
  218. * @brief DeInitializes PCD MSP.
  219. * @param hpcd PCD handle
  220. * @retval None
  221. */
  222. __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
  223. {
  224. /* Prevent unused argument(s) compilation warning */
  225. UNUSED(hpcd);
  226. /* NOTE : This function Should not be modified, when the callback is needed,
  227. the HAL_PCD_MspDeInit could be implemented in the user file
  228. */
  229. }
  230. /**
  231. * @}
  232. */
  233. /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
  234. * @brief Data transfers functions
  235. *
  236. @verbatim
  237. ===============================================================================
  238. ##### IO operation functions #####
  239. ===============================================================================
  240. [..]
  241. This subsection provides a set of functions allowing to manage the PCD data
  242. transfers.
  243. @endverbatim
  244. * @{
  245. */
  246. /**
  247. * @brief Start The USB OTG Device.
  248. * @param hpcd PCD handle
  249. * @retval HAL status
  250. */
  251. HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
  252. {
  253. __HAL_LOCK(hpcd);
  254. USB_DevConnect (hpcd->Instance);
  255. __HAL_PCD_ENABLE(hpcd);
  256. __HAL_UNLOCK(hpcd);
  257. return HAL_OK;
  258. }
  259. /**
  260. * @brief Stop The USB OTG 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. /**
  274. * @brief Handles PCD interrupt request.
  275. * @param hpcd PCD handle
  276. * @retval HAL status
  277. */
  278. void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
  279. {
  280. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  281. uint32_t i = 0U, ep_intr = 0U, epint = 0U, epnum = 0U;
  282. uint32_t fifoemptymsk = 0U, temp = 0U;
  283. USB_OTG_EPTypeDef *ep;
  284. uint32_t hclk = 180000000U;
  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. if(hpcd->Init.dma_enable == 1U)
  312. {
  313. hpcd->OUT_ep[epnum].xfer_count = hpcd->OUT_ep[epnum].maxpacket- (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
  314. hpcd->OUT_ep[epnum].xfer_buff += hpcd->OUT_ep[epnum].maxpacket;
  315. }
  316. HAL_PCD_DataOutStageCallback(hpcd, epnum);
  317. if(hpcd->Init.dma_enable == 1U)
  318. {
  319. if((epnum == 0U) && (hpcd->OUT_ep[epnum].xfer_len == 0U))
  320. {
  321. /* this is ZLP, so prepare EP0 for next setup */
  322. USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
  323. }
  324. }
  325. }
  326. if(( epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
  327. {
  328. /* Inform the upper layer that a setup packet is available */
  329. HAL_PCD_SetupStageCallback(hpcd);
  330. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
  331. }
  332. if(( epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
  333. {
  334. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
  335. }
  336. #ifdef USB_OTG_DOEPINT_OTEPSPR
  337. /* Clear Status Phase Received interrupt */
  338. if(( epint & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
  339. {
  340. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
  341. }
  342. #endif /* USB_OTG_DOEPINT_OTEPSPR */
  343. }
  344. epnum++;
  345. ep_intr >>= 1U;
  346. }
  347. }
  348. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
  349. {
  350. /* Read in the device interrupt bits */
  351. ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
  352. epnum = 0U;
  353. while ( ep_intr )
  354. {
  355. if (ep_intr & 0x1U) /* In ITR */
  356. {
  357. epint = USB_ReadDevInEPInterrupt(hpcd->Instance, epnum);
  358. if(( epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
  359. {
  360. fifoemptymsk = 0x1U << epnum;
  361. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  362. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
  363. if (hpcd->Init.dma_enable == 1U)
  364. {
  365. hpcd->IN_ep[epnum].xfer_buff += hpcd->IN_ep[epnum].maxpacket;
  366. }
  367. HAL_PCD_DataInStageCallback(hpcd, epnum);
  368. if (hpcd->Init.dma_enable == 1U)
  369. {
  370. /* this is ZLP, so prepare EP0 for next setup */
  371. if((epnum == 0U) && (hpcd->IN_ep[epnum].xfer_len == 0U))
  372. {
  373. /* prepare to rx more setup packets */
  374. USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
  375. }
  376. }
  377. }
  378. if(( epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
  379. {
  380. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
  381. }
  382. if(( epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
  383. {
  384. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
  385. }
  386. if(( epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
  387. {
  388. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
  389. }
  390. if(( epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
  391. {
  392. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
  393. }
  394. if(( epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
  395. {
  396. PCD_WriteEmptyTxFifo(hpcd , epnum);
  397. }
  398. }
  399. epnum++;
  400. ep_intr >>= 1U;
  401. }
  402. }
  403. /* Handle Resume Interrupt */
  404. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
  405. {
  406. /* Clear the Remote Wake-up Signaling */
  407. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  408. #ifdef USB_OTG_GLPMCFG_LPMEN
  409. if(hpcd->LPM_State == LPM_L1)
  410. {
  411. hpcd->LPM_State = LPM_L0;
  412. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
  413. }
  414. else
  415. #endif /* USB_OTG_GLPMCFG_LPMEN */
  416. {
  417. HAL_PCD_ResumeCallback(hpcd);
  418. }
  419. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
  420. }
  421. /* Handle Suspend Interrupt */
  422. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
  423. {
  424. if((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
  425. {
  426. HAL_PCD_SuspendCallback(hpcd);
  427. }
  428. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
  429. }
  430. #ifdef USB_OTG_GLPMCFG_LPMEN
  431. /* Handle LPM Interrupt */
  432. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT))
  433. {
  434. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
  435. if( hpcd->LPM_State == LPM_L0)
  436. {
  437. hpcd->LPM_State = LPM_L1;
  438. hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >> 2U;
  439. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
  440. }
  441. else
  442. {
  443. HAL_PCD_SuspendCallback(hpcd);
  444. }
  445. }
  446. #endif /* USB_OTG_GLPMCFG_LPMEN */
  447. /* Handle Reset Interrupt */
  448. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
  449. {
  450. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  451. USB_FlushTxFifo(hpcd->Instance , 0x10U);
  452. for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
  453. {
  454. USBx_INEP(i)->DIEPINT = 0xFFU;
  455. USBx_OUTEP(i)->DOEPINT = 0xFFU;
  456. }
  457. USBx_DEVICE->DAINT = 0xFFFFFFFFU;
  458. USBx_DEVICE->DAINTMSK |= 0x10001U;
  459. if(hpcd->Init.use_dedicated_ep1)
  460. {
  461. USBx_DEVICE->DOUTEP1MSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
  462. USBx_DEVICE->DINEP1MSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
  463. }
  464. else
  465. {
  466. #ifdef USB_OTG_DOEPINT_OTEPSPR
  467. USBx_DEVICE->DOEPMSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM | USB_OTG_DOEPMSK_OTEPSPRM);
  468. #else
  469. USBx_DEVICE->DOEPMSK |= (USB_OTG_DOEPMSK_STUPM | USB_OTG_DOEPMSK_XFRCM | USB_OTG_DOEPMSK_EPDM);
  470. #endif /* USB_OTG_DOEPINT_OTEPSPR */
  471. USBx_DEVICE->DIEPMSK |= (USB_OTG_DIEPMSK_TOM | USB_OTG_DIEPMSK_XFRCM | USB_OTG_DIEPMSK_EPDM);
  472. }
  473. /* Set Default Address to 0 */
  474. USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
  475. /* setup EP0 to receive SETUP packets */
  476. USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
  477. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
  478. }
  479. /* Handle Enumeration done Interrupt */
  480. if(__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
  481. {
  482. USB_ActivateSetup(hpcd->Instance);
  483. hpcd->Instance->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
  484. if ( USB_GetDevSpeed(hpcd->Instance) == USB_OTG_SPEED_HIGH)
  485. {
  486. hpcd->Init.speed = USB_OTG_SPEED_HIGH;
  487. hpcd->Init.ep0_mps = USB_OTG_HS_MAX_PACKET_SIZE ;
  488. hpcd->Instance->GUSBCFG |= (uint32_t)((USBD_HS_TRDT_VALUE << 10U) & USB_OTG_GUSBCFG_TRDT);
  489. }
  490. else
  491. {
  492. hpcd->Init.speed = USB_OTG_SPEED_FULL;
  493. hpcd->Init.ep0_mps = USB_OTG_FS_MAX_PACKET_SIZE ;
  494. /* The USBTRD is configured according to the tables below, depending on AHB frequency
  495. used by application. In the low AHB frequency range it is used to stretch enough the USB response
  496. time to IN tokens, the USB turnaround time, so to compensate for the longer AHB read access
  497. latency to the Data FIFO */
  498. /* Get hclk frequency value */
  499. hclk = HAL_RCC_GetHCLKFreq();
  500. if((hclk >= 14200000U)&&(hclk < 15000000U))
  501. {
  502. /* hclk Clock Range between 14.2-15 MHz */
  503. hpcd->Instance->GUSBCFG |= (uint32_t)((0xFU << 10U) & USB_OTG_GUSBCFG_TRDT);
  504. }
  505. else if((hclk >= 15000000U)&&(hclk < 16000000U))
  506. {
  507. /* hclk Clock Range between 15-16 MHz */
  508. hpcd->Instance->GUSBCFG |= (uint32_t)((0xEU << 10U) & USB_OTG_GUSBCFG_TRDT);
  509. }
  510. else if((hclk >= 16000000U)&&(hclk < 17200000U))
  511. {
  512. /* hclk Clock Range between 16-17.2 MHz */
  513. hpcd->Instance->GUSBCFG |= (uint32_t)((0xDU << 10U) & USB_OTG_GUSBCFG_TRDT);
  514. }
  515. else if((hclk >= 17200000U)&&(hclk < 18500000U))
  516. {
  517. /* hclk Clock Range between 17.2-18.5 MHz */
  518. hpcd->Instance->GUSBCFG |= (uint32_t)((0xCU << 10U) & USB_OTG_GUSBCFG_TRDT);
  519. }
  520. else if((hclk >= 18500000U)&&(hclk < 20000000U))
  521. {
  522. /* hclk Clock Range between 18.5-20 MHz */
  523. hpcd->Instance->GUSBCFG |= (uint32_t)((0xBU << 10U) & USB_OTG_GUSBCFG_TRDT);
  524. }
  525. else if((hclk >= 20000000U)&&(hclk < 21800000U))
  526. {
  527. /* hclk Clock Range between 20-21.8 MHz */
  528. hpcd->Instance->GUSBCFG |= (uint32_t)((0xAU << 10U) & USB_OTG_GUSBCFG_TRDT);
  529. }
  530. else if((hclk >= 21800000U)&&(hclk < 24000000U))
  531. {
  532. /* hclk Clock Range between 21.8-24 MHz */
  533. hpcd->Instance->GUSBCFG |= (uint32_t)((0x9U << 10U) & USB_OTG_GUSBCFG_TRDT);
  534. }
  535. else if((hclk >= 24000000U)&&(hclk < 27700000U))
  536. {
  537. /* hclk Clock Range between 24-27.7 MHz */
  538. hpcd->Instance->GUSBCFG |= (uint32_t)((0x8U << 10U) & USB_OTG_GUSBCFG_TRDT);
  539. }
  540. else if((hclk >= 27700000U)&&(hclk < 32000000U))
  541. {
  542. /* hclk Clock Range between 27.7-32 MHz */
  543. hpcd->Instance->GUSBCFG |= (uint32_t)((0x7U << 10U) & 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)((0x6U << 10U) & 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 & 0x80) == 0x80)
  829. {
  830. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  831. }
  832. else
  833. {
  834. ep = &hpcd->OUT_ep[ep_addr & 0x7F];
  835. }
  836. ep->num = ep_addr & 0x7F;
  837. ep->is_in = (0x80 & ep_addr) != 0;
  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 & 0x80) == 0x80)
  865. {
  866. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  867. }
  868. else
  869. {
  870. ep = &hpcd->OUT_ep[ep_addr & 0x7F];
  871. }
  872. ep->num = ep_addr & 0x7F;
  873. ep->is_in = (0x80 & ep_addr) != 0;
  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 & 0x7F];
  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 & 0x7F;
  897. if (hpcd->Init.dma_enable == 1U)
  898. {
  899. ep->dma_addr = (uint32_t)pBuf;
  900. }
  901. if ((ep_addr & 0x7F) == 0)
  902. {
  903. USB_EP0StartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
  904. }
  905. else
  906. {
  907. USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
  908. }
  909. return HAL_OK;
  910. }
  911. /**
  912. * @brief Get Received Data Size.
  913. * @param hpcd PCD handle
  914. * @param ep_addr endpoint address
  915. * @retval Data Size
  916. */
  917. uint16_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  918. {
  919. return hpcd->OUT_ep[ep_addr & 0xF].xfer_count;
  920. }
  921. /**
  922. * @brief Send an amount of data.
  923. * @param hpcd PCD handle
  924. * @param ep_addr endpoint address
  925. * @param pBuf pointer to the transmission buffer
  926. * @param len amount of data to be sent
  927. * @retval HAL status
  928. */
  929. HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  930. {
  931. USB_OTG_EPTypeDef *ep;
  932. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  933. /*setup and start the Xfer */
  934. ep->xfer_buff = pBuf;
  935. ep->xfer_len = len;
  936. ep->xfer_count = 0U;
  937. ep->is_in = 1U;
  938. ep->num = ep_addr & 0x7F;
  939. if (hpcd->Init.dma_enable == 1U)
  940. {
  941. ep->dma_addr = (uint32_t)pBuf;
  942. }
  943. if ((ep_addr & 0x7F) == 0)
  944. {
  945. USB_EP0StartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
  946. }
  947. else
  948. {
  949. USB_EPStartXfer(hpcd->Instance , ep, hpcd->Init.dma_enable);
  950. }
  951. return HAL_OK;
  952. }
  953. /**
  954. * @brief Set a STALL condition over an endpoint.
  955. * @param hpcd PCD handle
  956. * @param ep_addr endpoint address
  957. * @retval HAL status
  958. */
  959. HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  960. {
  961. USB_OTG_EPTypeDef *ep;
  962. if ((0x80 & ep_addr) == 0x80)
  963. {
  964. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  965. }
  966. else
  967. {
  968. ep = &hpcd->OUT_ep[ep_addr];
  969. }
  970. ep->is_stall = 1U;
  971. ep->num = ep_addr & 0x7F;
  972. ep->is_in = ((ep_addr & 0x80) == 0x80);
  973. __HAL_LOCK(hpcd);
  974. USB_EPSetStall(hpcd->Instance , ep);
  975. if((ep_addr & 0x7F) == 0)
  976. {
  977. USB_EP0_OutStart(hpcd->Instance, hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
  978. }
  979. __HAL_UNLOCK(hpcd);
  980. return HAL_OK;
  981. }
  982. /**
  983. * @brief Clear a STALL condition over in an endpoint.
  984. * @param hpcd PCD handle
  985. * @param ep_addr endpoint address
  986. * @retval HAL status
  987. */
  988. HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  989. {
  990. USB_OTG_EPTypeDef *ep;
  991. if ((0x80 & ep_addr) == 0x80)
  992. {
  993. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  994. }
  995. else
  996. {
  997. ep = &hpcd->OUT_ep[ep_addr];
  998. }
  999. ep->is_stall = 0U;
  1000. ep->num = ep_addr & 0x7F;
  1001. ep->is_in = ((ep_addr & 0x80) == 0x80);
  1002. __HAL_LOCK(hpcd);
  1003. USB_EPClearStall(hpcd->Instance , ep);
  1004. __HAL_UNLOCK(hpcd);
  1005. return HAL_OK;
  1006. }
  1007. /**
  1008. * @brief Flush an endpoint.
  1009. * @param hpcd PCD handle
  1010. * @param ep_addr endpoint address
  1011. * @retval HAL status
  1012. */
  1013. HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1014. {
  1015. __HAL_LOCK(hpcd);
  1016. if ((ep_addr & 0x80) == 0x80)
  1017. {
  1018. USB_FlushTxFifo(hpcd->Instance, ep_addr & 0x7F);
  1019. }
  1020. else
  1021. {
  1022. USB_FlushRxFifo(hpcd->Instance);
  1023. }
  1024. __HAL_UNLOCK(hpcd);
  1025. return HAL_OK;
  1026. }
  1027. /**
  1028. * @brief Activate remote wakeup signalling.
  1029. * @param hpcd PCD handle
  1030. * @retval HAL status
  1031. */
  1032. HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1033. {
  1034. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1035. if((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
  1036. {
  1037. /* Activate Remote wakeup signaling */
  1038. USBx_DEVICE->DCTL |= USB_OTG_DCTL_RWUSIG;
  1039. }
  1040. return HAL_OK;
  1041. }
  1042. /**
  1043. * @brief De-activate remote wakeup signalling.
  1044. * @param hpcd PCD handle
  1045. * @retval HAL status
  1046. */
  1047. HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1048. {
  1049. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1050. /* De-activate Remote wakeup signaling */
  1051. USBx_DEVICE->DCTL &= ~(USB_OTG_DCTL_RWUSIG);
  1052. return HAL_OK;
  1053. }
  1054. /**
  1055. * @}
  1056. */
  1057. /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
  1058. * @brief Peripheral State functions
  1059. *
  1060. @verbatim
  1061. ===============================================================================
  1062. ##### Peripheral State functions #####
  1063. ===============================================================================
  1064. [..]
  1065. This subsection permits to get in run-time the status of the peripheral
  1066. and the data flow.
  1067. @endverbatim
  1068. * @{
  1069. */
  1070. /**
  1071. * @brief Return the PCD handle state.
  1072. * @param hpcd PCD handle
  1073. * @retval HAL state
  1074. */
  1075. PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
  1076. {
  1077. return hpcd->State;
  1078. }
  1079. /**
  1080. * @}
  1081. */
  1082. /**
  1083. * @}
  1084. */
  1085. /* Private functions ---------------------------------------------------------*/
  1086. /** @addtogroup PCD_Private_Functions
  1087. * @{
  1088. */
  1089. /**
  1090. * @brief Check FIFO for the next packet to be loaded.
  1091. * @param hpcd PCD handle
  1092. * @param epnum endpoint number
  1093. * @retval HAL status
  1094. */
  1095. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1096. {
  1097. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1098. USB_OTG_EPTypeDef *ep;
  1099. int32_t len = 0U;
  1100. uint32_t len32b;
  1101. uint32_t fifoemptymsk = 0U;
  1102. ep = &hpcd->IN_ep[epnum];
  1103. len = ep->xfer_len - ep->xfer_count;
  1104. if (len > ep->maxpacket)
  1105. {
  1106. len = ep->maxpacket;
  1107. }
  1108. len32b = (len + 3U) / 4U;
  1109. while (((USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) > len32b) &&
  1110. (ep->xfer_count < ep->xfer_len) &&
  1111. (ep->xfer_len != 0U))
  1112. {
  1113. /* Write the FIFO */
  1114. len = ep->xfer_len - ep->xfer_count;
  1115. if (len > ep->maxpacket)
  1116. {
  1117. len = ep->maxpacket;
  1118. }
  1119. len32b = (len + 3U) / 4U;
  1120. USB_WritePacket(USBx, ep->xfer_buff, epnum, len, hpcd->Init.dma_enable);
  1121. ep->xfer_buff += len;
  1122. ep->xfer_count += len;
  1123. }
  1124. if(len <= 0U)
  1125. {
  1126. fifoemptymsk = 0x1U << epnum;
  1127. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  1128. }
  1129. return HAL_OK;
  1130. }
  1131. /**
  1132. * @}
  1133. */
  1134. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||
  1135. STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Rx ||
  1136. STM32F412Vx || STM32F412Cx || STM32F413xx || STM32F423xx */
  1137. #endif /* HAL_PCD_MODULE_ENABLED */
  1138. /**
  1139. * @}
  1140. */
  1141. /**
  1142. * @}
  1143. */
  1144. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/