You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1344 lines
36 KiB

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