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.
 
 
 

1708 lines
46 KiB

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