Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 

2259 řádky
59 KiB

  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) 2016 STMicroelectronics.
  39. * All rights reserved.</center></h2>
  40. *
  41. * This software component is licensed by ST under BSD 3-Clause license,
  42. * the "License"; You may not use this file except in compliance with the
  43. * License. You may obtain a copy of the License at:
  44. * opensource.org/licenses/BSD-3-Clause
  45. *
  46. ******************************************************************************
  47. */
  48. /* Includes ------------------------------------------------------------------*/
  49. #include "stm32f4xx_hal.h"
  50. /** @addtogroup STM32F4xx_HAL_Driver
  51. * @{
  52. */
  53. /** @defgroup PCD PCD
  54. * @brief PCD HAL module driver
  55. * @{
  56. */
  57. #ifdef HAL_PCD_MODULE_ENABLED
  58. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  59. /* Private types -------------------------------------------------------------*/
  60. /* Private variables ---------------------------------------------------------*/
  61. /* Private constants ---------------------------------------------------------*/
  62. /* Private macros ------------------------------------------------------------*/
  63. /** @defgroup PCD_Private_Macros PCD Private Macros
  64. * @{
  65. */
  66. #define PCD_MIN(a, b) (((a) < (b)) ? (a) : (b))
  67. #define PCD_MAX(a, b) (((a) > (b)) ? (a) : (b))
  68. /**
  69. * @}
  70. */
  71. /* Private functions prototypes ----------------------------------------------*/
  72. /** @defgroup PCD_Private_Functions PCD Private Functions
  73. * @{
  74. */
  75. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  76. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum);
  77. static HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum);
  78. static HAL_StatusTypeDef PCD_EP_OutSetupPacket_int(PCD_HandleTypeDef *hpcd, uint32_t epnum);
  79. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  80. /**
  81. * @}
  82. */
  83. /* Exported functions --------------------------------------------------------*/
  84. /** @defgroup PCD_Exported_Functions PCD Exported Functions
  85. * @{
  86. */
  87. /** @defgroup PCD_Exported_Functions_Group1 Initialization and de-initialization functions
  88. * @brief Initialization and Configuration functions
  89. *
  90. @verbatim
  91. ===============================================================================
  92. ##### Initialization and de-initialization functions #####
  93. ===============================================================================
  94. [..] This section provides functions allowing to:
  95. @endverbatim
  96. * @{
  97. */
  98. /**
  99. * @brief Initializes the PCD according to the specified
  100. * parameters in the PCD_InitTypeDef and initialize the associated handle.
  101. * @param hpcd PCD handle
  102. * @retval HAL status
  103. */
  104. HAL_StatusTypeDef HAL_PCD_Init(PCD_HandleTypeDef *hpcd)
  105. {
  106. USB_OTG_GlobalTypeDef *USBx;
  107. uint8_t i;
  108. /* Check the PCD handle allocation */
  109. if (hpcd == NULL)
  110. {
  111. return HAL_ERROR;
  112. }
  113. /* Check the parameters */
  114. assert_param(IS_PCD_ALL_INSTANCE(hpcd->Instance));
  115. USBx = hpcd->Instance;
  116. if (hpcd->State == HAL_PCD_STATE_RESET)
  117. {
  118. /* Allocate lock resource and initialize it */
  119. hpcd->Lock = HAL_UNLOCKED;
  120. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  121. hpcd->SOFCallback = HAL_PCD_SOFCallback;
  122. hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
  123. hpcd->ResetCallback = HAL_PCD_ResetCallback;
  124. hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
  125. hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
  126. hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
  127. hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
  128. hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback;
  129. hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback;
  130. hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback;
  131. hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback;
  132. hpcd->LPMCallback = HAL_PCDEx_LPM_Callback;
  133. hpcd->BCDCallback = HAL_PCDEx_BCD_Callback;
  134. if (hpcd->MspInitCallback == NULL)
  135. {
  136. hpcd->MspInitCallback = HAL_PCD_MspInit;
  137. }
  138. /* Init the low level hardware */
  139. hpcd->MspInitCallback(hpcd);
  140. #else
  141. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  142. HAL_PCD_MspInit(hpcd);
  143. #endif /* (USE_HAL_PCD_REGISTER_CALLBACKS) */
  144. }
  145. hpcd->State = HAL_PCD_STATE_BUSY;
  146. /* Disable DMA mode for FS instance */
  147. if ((USBx->CID & (0x1U << 8)) == 0U)
  148. {
  149. hpcd->Init.dma_enable = 0U;
  150. }
  151. /* Disable the Interrupts */
  152. __HAL_PCD_DISABLE(hpcd);
  153. /*Init the Core (common init.) */
  154. if (USB_CoreInit(hpcd->Instance, hpcd->Init) != HAL_OK)
  155. {
  156. hpcd->State = HAL_PCD_STATE_ERROR;
  157. return HAL_ERROR;
  158. }
  159. /* Force Device Mode*/
  160. (void)USB_SetCurrentMode(hpcd->Instance, USB_DEVICE_MODE);
  161. /* Init endpoints structures */
  162. for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
  163. {
  164. /* Init ep structure */
  165. hpcd->IN_ep[i].is_in = 1U;
  166. hpcd->IN_ep[i].num = i;
  167. hpcd->IN_ep[i].tx_fifo_num = i;
  168. /* Control until ep is activated */
  169. hpcd->IN_ep[i].type = EP_TYPE_CTRL;
  170. hpcd->IN_ep[i].maxpacket = 0U;
  171. hpcd->IN_ep[i].xfer_buff = 0U;
  172. hpcd->IN_ep[i].xfer_len = 0U;
  173. }
  174. for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
  175. {
  176. hpcd->OUT_ep[i].is_in = 0U;
  177. hpcd->OUT_ep[i].num = i;
  178. /* Control until ep is activated */
  179. hpcd->OUT_ep[i].type = EP_TYPE_CTRL;
  180. hpcd->OUT_ep[i].maxpacket = 0U;
  181. hpcd->OUT_ep[i].xfer_buff = 0U;
  182. hpcd->OUT_ep[i].xfer_len = 0U;
  183. }
  184. /* Init Device */
  185. if (USB_DevInit(hpcd->Instance, hpcd->Init) != HAL_OK)
  186. {
  187. hpcd->State = HAL_PCD_STATE_ERROR;
  188. return HAL_ERROR;
  189. }
  190. hpcd->USB_Address = 0U;
  191. hpcd->State = HAL_PCD_STATE_READY;
  192. #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  193. /* Activate LPM */
  194. if (hpcd->Init.lpm_enable == 1U)
  195. {
  196. (void)HAL_PCDEx_ActivateLPM(hpcd);
  197. }
  198. #endif /* defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx) */
  199. (void)USB_DevDisconnect(hpcd->Instance);
  200. return HAL_OK;
  201. }
  202. /**
  203. * @brief DeInitializes the PCD peripheral.
  204. * @param hpcd PCD handle
  205. * @retval HAL status
  206. */
  207. HAL_StatusTypeDef HAL_PCD_DeInit(PCD_HandleTypeDef *hpcd)
  208. {
  209. /* Check the PCD handle allocation */
  210. if (hpcd == NULL)
  211. {
  212. return HAL_ERROR;
  213. }
  214. hpcd->State = HAL_PCD_STATE_BUSY;
  215. /* Stop Device */
  216. if (USB_StopDevice(hpcd->Instance) != HAL_OK)
  217. {
  218. return HAL_ERROR;
  219. }
  220. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  221. if (hpcd->MspDeInitCallback == NULL)
  222. {
  223. hpcd->MspDeInitCallback = HAL_PCD_MspDeInit; /* Legacy weak MspDeInit */
  224. }
  225. /* DeInit the low level hardware */
  226. hpcd->MspDeInitCallback(hpcd);
  227. #else
  228. /* DeInit the low level hardware: CLOCK, NVIC.*/
  229. HAL_PCD_MspDeInit(hpcd);
  230. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  231. hpcd->State = HAL_PCD_STATE_RESET;
  232. return HAL_OK;
  233. }
  234. /**
  235. * @brief Initializes the PCD MSP.
  236. * @param hpcd PCD handle
  237. * @retval None
  238. */
  239. __weak void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd)
  240. {
  241. /* Prevent unused argument(s) compilation warning */
  242. UNUSED(hpcd);
  243. /* NOTE : This function should not be modified, when the callback is needed,
  244. the HAL_PCD_MspInit could be implemented in the user file
  245. */
  246. }
  247. /**
  248. * @brief DeInitializes PCD MSP.
  249. * @param hpcd PCD handle
  250. * @retval None
  251. */
  252. __weak void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd)
  253. {
  254. /* Prevent unused argument(s) compilation warning */
  255. UNUSED(hpcd);
  256. /* NOTE : This function should not be modified, when the callback is needed,
  257. the HAL_PCD_MspDeInit could be implemented in the user file
  258. */
  259. }
  260. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  261. /**
  262. * @brief Register a User USB PCD Callback
  263. * To be used instead of the weak predefined callback
  264. * @param hpcd USB PCD handle
  265. * @param CallbackID ID of the callback to be registered
  266. * This parameter can be one of the following values:
  267. * @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
  268. * @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
  269. * @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
  270. * @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
  271. * @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
  272. * @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
  273. * @arg @ref HAL_PCD_DISCONNECT_CB_ID OTG PCD Disconnect callback ID
  274. * @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
  275. * @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
  276. * @param pCallback pointer to the Callback function
  277. * @retval HAL status
  278. */
  279. HAL_StatusTypeDef HAL_PCD_RegisterCallback(PCD_HandleTypeDef *hpcd,
  280. HAL_PCD_CallbackIDTypeDef CallbackID,
  281. pPCD_CallbackTypeDef pCallback)
  282. {
  283. HAL_StatusTypeDef status = HAL_OK;
  284. if (pCallback == NULL)
  285. {
  286. /* Update the error code */
  287. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  288. return HAL_ERROR;
  289. }
  290. /* Process locked */
  291. __HAL_LOCK(hpcd);
  292. if (hpcd->State == HAL_PCD_STATE_READY)
  293. {
  294. switch (CallbackID)
  295. {
  296. case HAL_PCD_SOF_CB_ID :
  297. hpcd->SOFCallback = pCallback;
  298. break;
  299. case HAL_PCD_SETUPSTAGE_CB_ID :
  300. hpcd->SetupStageCallback = pCallback;
  301. break;
  302. case HAL_PCD_RESET_CB_ID :
  303. hpcd->ResetCallback = pCallback;
  304. break;
  305. case HAL_PCD_SUSPEND_CB_ID :
  306. hpcd->SuspendCallback = pCallback;
  307. break;
  308. case HAL_PCD_RESUME_CB_ID :
  309. hpcd->ResumeCallback = pCallback;
  310. break;
  311. case HAL_PCD_CONNECT_CB_ID :
  312. hpcd->ConnectCallback = pCallback;
  313. break;
  314. case HAL_PCD_DISCONNECT_CB_ID :
  315. hpcd->DisconnectCallback = pCallback;
  316. break;
  317. case HAL_PCD_MSPINIT_CB_ID :
  318. hpcd->MspInitCallback = pCallback;
  319. break;
  320. case HAL_PCD_MSPDEINIT_CB_ID :
  321. hpcd->MspDeInitCallback = pCallback;
  322. break;
  323. default :
  324. /* Update the error code */
  325. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  326. /* Return error status */
  327. status = HAL_ERROR;
  328. break;
  329. }
  330. }
  331. else if (hpcd->State == HAL_PCD_STATE_RESET)
  332. {
  333. switch (CallbackID)
  334. {
  335. case HAL_PCD_MSPINIT_CB_ID :
  336. hpcd->MspInitCallback = pCallback;
  337. break;
  338. case HAL_PCD_MSPDEINIT_CB_ID :
  339. hpcd->MspDeInitCallback = pCallback;
  340. break;
  341. default :
  342. /* Update the error code */
  343. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  344. /* Return error status */
  345. status = HAL_ERROR;
  346. break;
  347. }
  348. }
  349. else
  350. {
  351. /* Update the error code */
  352. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  353. /* Return error status */
  354. status = HAL_ERROR;
  355. }
  356. /* Release Lock */
  357. __HAL_UNLOCK(hpcd);
  358. return status;
  359. }
  360. /**
  361. * @brief Unregister an USB PCD Callback
  362. * USB PCD callabck is redirected to the weak predefined callback
  363. * @param hpcd USB PCD handle
  364. * @param CallbackID ID of the callback to be unregistered
  365. * This parameter can be one of the following values:
  366. * @arg @ref HAL_PCD_SOF_CB_ID USB PCD SOF callback ID
  367. * @arg @ref HAL_PCD_SETUPSTAGE_CB_ID USB PCD Setup callback ID
  368. * @arg @ref HAL_PCD_RESET_CB_ID USB PCD Reset callback ID
  369. * @arg @ref HAL_PCD_SUSPEND_CB_ID USB PCD Suspend callback ID
  370. * @arg @ref HAL_PCD_RESUME_CB_ID USB PCD Resume callback ID
  371. * @arg @ref HAL_PCD_CONNECT_CB_ID USB PCD Connect callback ID
  372. * @arg @ref HAL_PCD_DISCONNECT_CB_ID OTG PCD Disconnect callback ID
  373. * @arg @ref HAL_PCD_MSPINIT_CB_ID MspDeInit callback ID
  374. * @arg @ref HAL_PCD_MSPDEINIT_CB_ID MspDeInit callback ID
  375. * @retval HAL status
  376. */
  377. HAL_StatusTypeDef HAL_PCD_UnRegisterCallback(PCD_HandleTypeDef *hpcd, HAL_PCD_CallbackIDTypeDef CallbackID)
  378. {
  379. HAL_StatusTypeDef status = HAL_OK;
  380. /* Process locked */
  381. __HAL_LOCK(hpcd);
  382. /* Setup Legacy weak Callbacks */
  383. if (hpcd->State == HAL_PCD_STATE_READY)
  384. {
  385. switch (CallbackID)
  386. {
  387. case HAL_PCD_SOF_CB_ID :
  388. hpcd->SOFCallback = HAL_PCD_SOFCallback;
  389. break;
  390. case HAL_PCD_SETUPSTAGE_CB_ID :
  391. hpcd->SetupStageCallback = HAL_PCD_SetupStageCallback;
  392. break;
  393. case HAL_PCD_RESET_CB_ID :
  394. hpcd->ResetCallback = HAL_PCD_ResetCallback;
  395. break;
  396. case HAL_PCD_SUSPEND_CB_ID :
  397. hpcd->SuspendCallback = HAL_PCD_SuspendCallback;
  398. break;
  399. case HAL_PCD_RESUME_CB_ID :
  400. hpcd->ResumeCallback = HAL_PCD_ResumeCallback;
  401. break;
  402. case HAL_PCD_CONNECT_CB_ID :
  403. hpcd->ConnectCallback = HAL_PCD_ConnectCallback;
  404. break;
  405. case HAL_PCD_DISCONNECT_CB_ID :
  406. hpcd->DisconnectCallback = HAL_PCD_DisconnectCallback;
  407. break;
  408. case HAL_PCD_MSPINIT_CB_ID :
  409. hpcd->MspInitCallback = HAL_PCD_MspInit;
  410. break;
  411. case HAL_PCD_MSPDEINIT_CB_ID :
  412. hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
  413. break;
  414. default :
  415. /* Update the error code */
  416. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  417. /* Return error status */
  418. status = HAL_ERROR;
  419. break;
  420. }
  421. }
  422. else if (hpcd->State == HAL_PCD_STATE_RESET)
  423. {
  424. switch (CallbackID)
  425. {
  426. case HAL_PCD_MSPINIT_CB_ID :
  427. hpcd->MspInitCallback = HAL_PCD_MspInit;
  428. break;
  429. case HAL_PCD_MSPDEINIT_CB_ID :
  430. hpcd->MspDeInitCallback = HAL_PCD_MspDeInit;
  431. break;
  432. default :
  433. /* Update the error code */
  434. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  435. /* Return error status */
  436. status = HAL_ERROR;
  437. break;
  438. }
  439. }
  440. else
  441. {
  442. /* Update the error code */
  443. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  444. /* Return error status */
  445. status = HAL_ERROR;
  446. }
  447. /* Release Lock */
  448. __HAL_UNLOCK(hpcd);
  449. return status;
  450. }
  451. /**
  452. * @brief Register USB PCD Data OUT Stage Callback
  453. * To be used instead of the weak HAL_PCD_DataOutStageCallback() predefined callback
  454. * @param hpcd PCD handle
  455. * @param pCallback pointer to the USB PCD Data OUT Stage Callback function
  456. * @retval HAL status
  457. */
  458. HAL_StatusTypeDef HAL_PCD_RegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd,
  459. pPCD_DataOutStageCallbackTypeDef pCallback)
  460. {
  461. HAL_StatusTypeDef status = HAL_OK;
  462. if (pCallback == NULL)
  463. {
  464. /* Update the error code */
  465. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  466. return HAL_ERROR;
  467. }
  468. /* Process locked */
  469. __HAL_LOCK(hpcd);
  470. if (hpcd->State == HAL_PCD_STATE_READY)
  471. {
  472. hpcd->DataOutStageCallback = pCallback;
  473. }
  474. else
  475. {
  476. /* Update the error code */
  477. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  478. /* Return error status */
  479. status = HAL_ERROR;
  480. }
  481. /* Release Lock */
  482. __HAL_UNLOCK(hpcd);
  483. return status;
  484. }
  485. /**
  486. * @brief Unregister the USB PCD Data OUT Stage Callback
  487. * USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataOutStageCallback() predefined callback
  488. * @param hpcd PCD handle
  489. * @retval HAL status
  490. */
  491. HAL_StatusTypeDef HAL_PCD_UnRegisterDataOutStageCallback(PCD_HandleTypeDef *hpcd)
  492. {
  493. HAL_StatusTypeDef status = HAL_OK;
  494. /* Process locked */
  495. __HAL_LOCK(hpcd);
  496. if (hpcd->State == HAL_PCD_STATE_READY)
  497. {
  498. hpcd->DataOutStageCallback = HAL_PCD_DataOutStageCallback; /* Legacy weak DataOutStageCallback */
  499. }
  500. else
  501. {
  502. /* Update the error code */
  503. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  504. /* Return error status */
  505. status = HAL_ERROR;
  506. }
  507. /* Release Lock */
  508. __HAL_UNLOCK(hpcd);
  509. return status;
  510. }
  511. /**
  512. * @brief Register USB PCD Data IN Stage Callback
  513. * To be used instead of the weak HAL_PCD_DataInStageCallback() predefined callback
  514. * @param hpcd PCD handle
  515. * @param pCallback pointer to the USB PCD Data IN Stage Callback function
  516. * @retval HAL status
  517. */
  518. HAL_StatusTypeDef HAL_PCD_RegisterDataInStageCallback(PCD_HandleTypeDef *hpcd,
  519. pPCD_DataInStageCallbackTypeDef pCallback)
  520. {
  521. HAL_StatusTypeDef status = HAL_OK;
  522. if (pCallback == NULL)
  523. {
  524. /* Update the error code */
  525. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  526. return HAL_ERROR;
  527. }
  528. /* Process locked */
  529. __HAL_LOCK(hpcd);
  530. if (hpcd->State == HAL_PCD_STATE_READY)
  531. {
  532. hpcd->DataInStageCallback = pCallback;
  533. }
  534. else
  535. {
  536. /* Update the error code */
  537. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  538. /* Return error status */
  539. status = HAL_ERROR;
  540. }
  541. /* Release Lock */
  542. __HAL_UNLOCK(hpcd);
  543. return status;
  544. }
  545. /**
  546. * @brief Unregister the USB PCD Data IN Stage Callback
  547. * USB PCD Data OUT Stage Callback is redirected to the weak HAL_PCD_DataInStageCallback() predefined callback
  548. * @param hpcd PCD handle
  549. * @retval HAL status
  550. */
  551. HAL_StatusTypeDef HAL_PCD_UnRegisterDataInStageCallback(PCD_HandleTypeDef *hpcd)
  552. {
  553. HAL_StatusTypeDef status = HAL_OK;
  554. /* Process locked */
  555. __HAL_LOCK(hpcd);
  556. if (hpcd->State == HAL_PCD_STATE_READY)
  557. {
  558. hpcd->DataInStageCallback = HAL_PCD_DataInStageCallback; /* Legacy weak DataInStageCallback */
  559. }
  560. else
  561. {
  562. /* Update the error code */
  563. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  564. /* Return error status */
  565. status = HAL_ERROR;
  566. }
  567. /* Release Lock */
  568. __HAL_UNLOCK(hpcd);
  569. return status;
  570. }
  571. /**
  572. * @brief Register USB PCD Iso OUT incomplete Callback
  573. * To be used instead of the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
  574. * @param hpcd PCD handle
  575. * @param pCallback pointer to the USB PCD Iso OUT incomplete Callback function
  576. * @retval HAL status
  577. */
  578. HAL_StatusTypeDef HAL_PCD_RegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd,
  579. pPCD_IsoOutIncpltCallbackTypeDef pCallback)
  580. {
  581. HAL_StatusTypeDef status = HAL_OK;
  582. if (pCallback == NULL)
  583. {
  584. /* Update the error code */
  585. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  586. return HAL_ERROR;
  587. }
  588. /* Process locked */
  589. __HAL_LOCK(hpcd);
  590. if (hpcd->State == HAL_PCD_STATE_READY)
  591. {
  592. hpcd->ISOOUTIncompleteCallback = pCallback;
  593. }
  594. else
  595. {
  596. /* Update the error code */
  597. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  598. /* Return error status */
  599. status = HAL_ERROR;
  600. }
  601. /* Release Lock */
  602. __HAL_UNLOCK(hpcd);
  603. return status;
  604. }
  605. /**
  606. * @brief Unregister the USB PCD Iso OUT incomplete Callback
  607. * USB PCD Iso OUT incomplete Callback is redirected
  608. * to the weak HAL_PCD_ISOOUTIncompleteCallback() predefined callback
  609. * @param hpcd PCD handle
  610. * @retval HAL status
  611. */
  612. HAL_StatusTypeDef HAL_PCD_UnRegisterIsoOutIncpltCallback(PCD_HandleTypeDef *hpcd)
  613. {
  614. HAL_StatusTypeDef status = HAL_OK;
  615. /* Process locked */
  616. __HAL_LOCK(hpcd);
  617. if (hpcd->State == HAL_PCD_STATE_READY)
  618. {
  619. hpcd->ISOOUTIncompleteCallback = HAL_PCD_ISOOUTIncompleteCallback; /* Legacy weak ISOOUTIncompleteCallback */
  620. }
  621. else
  622. {
  623. /* Update the error code */
  624. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  625. /* Return error status */
  626. status = HAL_ERROR;
  627. }
  628. /* Release Lock */
  629. __HAL_UNLOCK(hpcd);
  630. return status;
  631. }
  632. /**
  633. * @brief Register USB PCD Iso IN incomplete Callback
  634. * To be used instead of the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
  635. * @param hpcd PCD handle
  636. * @param pCallback pointer to the USB PCD Iso IN incomplete Callback function
  637. * @retval HAL status
  638. */
  639. HAL_StatusTypeDef HAL_PCD_RegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd,
  640. pPCD_IsoInIncpltCallbackTypeDef pCallback)
  641. {
  642. HAL_StatusTypeDef status = HAL_OK;
  643. if (pCallback == NULL)
  644. {
  645. /* Update the error code */
  646. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  647. return HAL_ERROR;
  648. }
  649. /* Process locked */
  650. __HAL_LOCK(hpcd);
  651. if (hpcd->State == HAL_PCD_STATE_READY)
  652. {
  653. hpcd->ISOINIncompleteCallback = pCallback;
  654. }
  655. else
  656. {
  657. /* Update the error code */
  658. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  659. /* Return error status */
  660. status = HAL_ERROR;
  661. }
  662. /* Release Lock */
  663. __HAL_UNLOCK(hpcd);
  664. return status;
  665. }
  666. /**
  667. * @brief Unregister the USB PCD Iso IN incomplete Callback
  668. * USB PCD Iso IN incomplete Callback is redirected
  669. * to the weak HAL_PCD_ISOINIncompleteCallback() predefined callback
  670. * @param hpcd PCD handle
  671. * @retval HAL status
  672. */
  673. HAL_StatusTypeDef HAL_PCD_UnRegisterIsoInIncpltCallback(PCD_HandleTypeDef *hpcd)
  674. {
  675. HAL_StatusTypeDef status = HAL_OK;
  676. /* Process locked */
  677. __HAL_LOCK(hpcd);
  678. if (hpcd->State == HAL_PCD_STATE_READY)
  679. {
  680. hpcd->ISOINIncompleteCallback = HAL_PCD_ISOINIncompleteCallback; /* Legacy weak ISOINIncompleteCallback */
  681. }
  682. else
  683. {
  684. /* Update the error code */
  685. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  686. /* Return error status */
  687. status = HAL_ERROR;
  688. }
  689. /* Release Lock */
  690. __HAL_UNLOCK(hpcd);
  691. return status;
  692. }
  693. /**
  694. * @brief Register USB PCD BCD Callback
  695. * To be used instead of the weak HAL_PCDEx_BCD_Callback() predefined callback
  696. * @param hpcd PCD handle
  697. * @param pCallback pointer to the USB PCD BCD Callback function
  698. * @retval HAL status
  699. */
  700. HAL_StatusTypeDef HAL_PCD_RegisterBcdCallback(PCD_HandleTypeDef *hpcd, pPCD_BcdCallbackTypeDef pCallback)
  701. {
  702. HAL_StatusTypeDef status = HAL_OK;
  703. if (pCallback == NULL)
  704. {
  705. /* Update the error code */
  706. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  707. return HAL_ERROR;
  708. }
  709. /* Process locked */
  710. __HAL_LOCK(hpcd);
  711. if (hpcd->State == HAL_PCD_STATE_READY)
  712. {
  713. hpcd->BCDCallback = pCallback;
  714. }
  715. else
  716. {
  717. /* Update the error code */
  718. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  719. /* Return error status */
  720. status = HAL_ERROR;
  721. }
  722. /* Release Lock */
  723. __HAL_UNLOCK(hpcd);
  724. return status;
  725. }
  726. /**
  727. * @brief Unregister the USB PCD BCD Callback
  728. * USB BCD Callback is redirected to the weak HAL_PCDEx_BCD_Callback() predefined callback
  729. * @param hpcd PCD handle
  730. * @retval HAL status
  731. */
  732. HAL_StatusTypeDef HAL_PCD_UnRegisterBcdCallback(PCD_HandleTypeDef *hpcd)
  733. {
  734. HAL_StatusTypeDef status = HAL_OK;
  735. /* Process locked */
  736. __HAL_LOCK(hpcd);
  737. if (hpcd->State == HAL_PCD_STATE_READY)
  738. {
  739. hpcd->BCDCallback = HAL_PCDEx_BCD_Callback; /* Legacy weak HAL_PCDEx_BCD_Callback */
  740. }
  741. else
  742. {
  743. /* Update the error code */
  744. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  745. /* Return error status */
  746. status = HAL_ERROR;
  747. }
  748. /* Release Lock */
  749. __HAL_UNLOCK(hpcd);
  750. return status;
  751. }
  752. /**
  753. * @brief Register USB PCD LPM Callback
  754. * To be used instead of the weak HAL_PCDEx_LPM_Callback() predefined callback
  755. * @param hpcd PCD handle
  756. * @param pCallback pointer to the USB PCD LPM Callback function
  757. * @retval HAL status
  758. */
  759. HAL_StatusTypeDef HAL_PCD_RegisterLpmCallback(PCD_HandleTypeDef *hpcd, pPCD_LpmCallbackTypeDef pCallback)
  760. {
  761. HAL_StatusTypeDef status = HAL_OK;
  762. if (pCallback == NULL)
  763. {
  764. /* Update the error code */
  765. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  766. return HAL_ERROR;
  767. }
  768. /* Process locked */
  769. __HAL_LOCK(hpcd);
  770. if (hpcd->State == HAL_PCD_STATE_READY)
  771. {
  772. hpcd->LPMCallback = pCallback;
  773. }
  774. else
  775. {
  776. /* Update the error code */
  777. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  778. /* Return error status */
  779. status = HAL_ERROR;
  780. }
  781. /* Release Lock */
  782. __HAL_UNLOCK(hpcd);
  783. return status;
  784. }
  785. /**
  786. * @brief Unregister the USB PCD LPM Callback
  787. * USB LPM Callback is redirected to the weak HAL_PCDEx_LPM_Callback() predefined callback
  788. * @param hpcd PCD handle
  789. * @retval HAL status
  790. */
  791. HAL_StatusTypeDef HAL_PCD_UnRegisterLpmCallback(PCD_HandleTypeDef *hpcd)
  792. {
  793. HAL_StatusTypeDef status = HAL_OK;
  794. /* Process locked */
  795. __HAL_LOCK(hpcd);
  796. if (hpcd->State == HAL_PCD_STATE_READY)
  797. {
  798. hpcd->LPMCallback = HAL_PCDEx_LPM_Callback; /* Legacy weak HAL_PCDEx_LPM_Callback */
  799. }
  800. else
  801. {
  802. /* Update the error code */
  803. hpcd->ErrorCode |= HAL_PCD_ERROR_INVALID_CALLBACK;
  804. /* Return error status */
  805. status = HAL_ERROR;
  806. }
  807. /* Release Lock */
  808. __HAL_UNLOCK(hpcd);
  809. return status;
  810. }
  811. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  812. /**
  813. * @}
  814. */
  815. /** @defgroup PCD_Exported_Functions_Group2 Input and Output operation functions
  816. * @brief Data transfers functions
  817. *
  818. @verbatim
  819. ===============================================================================
  820. ##### IO operation functions #####
  821. ===============================================================================
  822. [..]
  823. This subsection provides a set of functions allowing to manage the PCD data
  824. transfers.
  825. @endverbatim
  826. * @{
  827. */
  828. /**
  829. * @brief Start the USB device
  830. * @param hpcd PCD handle
  831. * @retval HAL status
  832. */
  833. HAL_StatusTypeDef HAL_PCD_Start(PCD_HandleTypeDef *hpcd)
  834. {
  835. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  836. __HAL_LOCK(hpcd);
  837. if ((hpcd->Init.battery_charging_enable == 1U) &&
  838. (hpcd->Init.phy_itface != USB_OTG_ULPI_PHY))
  839. {
  840. /* Enable USB Transceiver */
  841. USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
  842. }
  843. __HAL_PCD_ENABLE(hpcd);
  844. (void)USB_DevConnect(hpcd->Instance);
  845. __HAL_UNLOCK(hpcd);
  846. return HAL_OK;
  847. }
  848. /**
  849. * @brief Stop the USB device.
  850. * @param hpcd PCD handle
  851. * @retval HAL status
  852. */
  853. HAL_StatusTypeDef HAL_PCD_Stop(PCD_HandleTypeDef *hpcd)
  854. {
  855. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  856. __HAL_LOCK(hpcd);
  857. __HAL_PCD_DISABLE(hpcd);
  858. (void)USB_DevDisconnect(hpcd->Instance);
  859. (void)USB_FlushTxFifo(hpcd->Instance, 0x10U);
  860. if ((hpcd->Init.battery_charging_enable == 1U) &&
  861. (hpcd->Init.phy_itface != USB_OTG_ULPI_PHY))
  862. {
  863. /* Disable USB Transceiver */
  864. USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
  865. }
  866. __HAL_UNLOCK(hpcd);
  867. return HAL_OK;
  868. }
  869. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  870. /**
  871. * @brief Handles PCD interrupt request.
  872. * @param hpcd PCD handle
  873. * @retval HAL status
  874. */
  875. void HAL_PCD_IRQHandler(PCD_HandleTypeDef *hpcd)
  876. {
  877. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  878. uint32_t USBx_BASE = (uint32_t)USBx;
  879. USB_OTG_EPTypeDef *ep;
  880. uint32_t i;
  881. uint32_t ep_intr;
  882. uint32_t epint;
  883. uint32_t epnum;
  884. uint32_t fifoemptymsk;
  885. uint32_t temp;
  886. /* ensure that we are in device mode */
  887. if (USB_GetMode(hpcd->Instance) == USB_OTG_MODE_DEVICE)
  888. {
  889. /* avoid spurious interrupt */
  890. if (__HAL_PCD_IS_INVALID_INTERRUPT(hpcd))
  891. {
  892. return;
  893. }
  894. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS))
  895. {
  896. /* incorrect mode, acknowledge the interrupt */
  897. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
  898. }
  899. /* Handle RxQLevel Interrupt */
  900. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL))
  901. {
  902. USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  903. temp = USBx->GRXSTSP;
  904. ep = &hpcd->OUT_ep[temp & USB_OTG_GRXSTSP_EPNUM];
  905. if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT)
  906. {
  907. if ((temp & USB_OTG_GRXSTSP_BCNT) != 0U)
  908. {
  909. (void)USB_ReadPacket(USBx, ep->xfer_buff,
  910. (uint16_t)((temp & USB_OTG_GRXSTSP_BCNT) >> 4));
  911. ep->xfer_buff += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  912. ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  913. }
  914. }
  915. else if (((temp & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT)
  916. {
  917. (void)USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8U);
  918. ep->xfer_count += (temp & USB_OTG_GRXSTSP_BCNT) >> 4;
  919. }
  920. else
  921. {
  922. /* ... */
  923. }
  924. USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  925. }
  926. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT))
  927. {
  928. epnum = 0U;
  929. /* Read in the device interrupt bits */
  930. ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
  931. while (ep_intr != 0U)
  932. {
  933. if ((ep_intr & 0x1U) != 0U)
  934. {
  935. epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, (uint8_t)epnum);
  936. if ((epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
  937. {
  938. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
  939. (void)PCD_EP_OutXfrComplete_int(hpcd, epnum);
  940. }
  941. if ((epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
  942. {
  943. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
  944. /* Class B setup phase done for previous decoded setup */
  945. (void)PCD_EP_OutSetupPacket_int(hpcd, epnum);
  946. }
  947. if ((epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
  948. {
  949. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
  950. }
  951. /* Clear Status Phase Received interrupt */
  952. if ((epint & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
  953. {
  954. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
  955. }
  956. /* Clear OUT NAK interrupt */
  957. if ((epint & USB_OTG_DOEPINT_NAK) == USB_OTG_DOEPINT_NAK)
  958. {
  959. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_NAK);
  960. }
  961. }
  962. epnum++;
  963. ep_intr >>= 1U;
  964. }
  965. }
  966. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
  967. {
  968. /* Read in the device interrupt bits */
  969. ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
  970. epnum = 0U;
  971. while (ep_intr != 0U)
  972. {
  973. if ((ep_intr & 0x1U) != 0U) /* In ITR */
  974. {
  975. epint = USB_ReadDevInEPInterrupt(hpcd->Instance, (uint8_t)epnum);
  976. if ((epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
  977. {
  978. fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));
  979. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  980. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
  981. if (hpcd->Init.dma_enable == 1U)
  982. {
  983. hpcd->IN_ep[epnum].xfer_buff += hpcd->IN_ep[epnum].maxpacket;
  984. /* this is ZLP, so prepare EP0 for next setup */
  985. if ((epnum == 0U) && (hpcd->IN_ep[epnum].xfer_len == 0U))
  986. {
  987. /* prepare to rx more setup packets */
  988. (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
  989. }
  990. }
  991. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  992. hpcd->DataInStageCallback(hpcd, (uint8_t)epnum);
  993. #else
  994. HAL_PCD_DataInStageCallback(hpcd, (uint8_t)epnum);
  995. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  996. }
  997. if ((epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
  998. {
  999. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
  1000. }
  1001. if ((epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
  1002. {
  1003. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
  1004. }
  1005. if ((epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
  1006. {
  1007. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
  1008. }
  1009. if ((epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
  1010. {
  1011. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
  1012. }
  1013. if ((epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
  1014. {
  1015. (void)PCD_WriteEmptyTxFifo(hpcd, epnum);
  1016. }
  1017. }
  1018. epnum++;
  1019. ep_intr >>= 1U;
  1020. }
  1021. }
  1022. /* Handle Resume Interrupt */
  1023. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
  1024. {
  1025. /* Clear the Remote Wake-up Signaling */
  1026. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  1027. if (hpcd->LPM_State == LPM_L1)
  1028. {
  1029. hpcd->LPM_State = LPM_L0;
  1030. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1031. hpcd->LPMCallback(hpcd, PCD_LPM_L0_ACTIVE);
  1032. #else
  1033. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
  1034. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1035. }
  1036. else
  1037. {
  1038. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1039. hpcd->ResumeCallback(hpcd);
  1040. #else
  1041. HAL_PCD_ResumeCallback(hpcd);
  1042. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1043. }
  1044. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
  1045. }
  1046. /* Handle Suspend Interrupt */
  1047. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
  1048. {
  1049. if ((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
  1050. {
  1051. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1052. hpcd->SuspendCallback(hpcd);
  1053. #else
  1054. HAL_PCD_SuspendCallback(hpcd);
  1055. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1056. }
  1057. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
  1058. }
  1059. #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  1060. /* Handle LPM Interrupt */
  1061. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT))
  1062. {
  1063. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
  1064. if (hpcd->LPM_State == LPM_L0)
  1065. {
  1066. hpcd->LPM_State = LPM_L1;
  1067. hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >> 2U;
  1068. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1069. hpcd->LPMCallback(hpcd, PCD_LPM_L1_ACTIVE);
  1070. #else
  1071. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
  1072. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1073. }
  1074. else
  1075. {
  1076. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1077. hpcd->SuspendCallback(hpcd);
  1078. #else
  1079. HAL_PCD_SuspendCallback(hpcd);
  1080. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1081. }
  1082. }
  1083. #endif /* defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx) */
  1084. /* Handle Reset Interrupt */
  1085. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
  1086. {
  1087. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  1088. (void)USB_FlushTxFifo(hpcd->Instance, 0x10U);
  1089. for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
  1090. {
  1091. USBx_INEP(i)->DIEPINT = 0xFB7FU;
  1092. USBx_INEP(i)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
  1093. USBx_INEP(i)->DIEPCTL |= USB_OTG_DIEPCTL_SNAK;
  1094. USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
  1095. USBx_OUTEP(i)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
  1096. USBx_OUTEP(i)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
  1097. }
  1098. USBx_DEVICE->DAINTMSK |= 0x10001U;
  1099. if (hpcd->Init.use_dedicated_ep1 != 0U)
  1100. {
  1101. USBx_DEVICE->DOUTEP1MSK |= USB_OTG_DOEPMSK_STUPM |
  1102. USB_OTG_DOEPMSK_XFRCM |
  1103. USB_OTG_DOEPMSK_EPDM;
  1104. USBx_DEVICE->DINEP1MSK |= USB_OTG_DIEPMSK_TOM |
  1105. USB_OTG_DIEPMSK_XFRCM |
  1106. USB_OTG_DIEPMSK_EPDM;
  1107. }
  1108. else
  1109. {
  1110. USBx_DEVICE->DOEPMSK |= USB_OTG_DOEPMSK_STUPM |
  1111. USB_OTG_DOEPMSK_XFRCM |
  1112. USB_OTG_DOEPMSK_EPDM |
  1113. USB_OTG_DOEPMSK_OTEPSPRM |
  1114. USB_OTG_DOEPMSK_NAKM;
  1115. USBx_DEVICE->DIEPMSK |= USB_OTG_DIEPMSK_TOM |
  1116. USB_OTG_DIEPMSK_XFRCM |
  1117. USB_OTG_DIEPMSK_EPDM;
  1118. }
  1119. /* Set Default Address to 0 */
  1120. USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
  1121. /* setup EP0 to receive SETUP packets */
  1122. (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t)hpcd->Init.dma_enable,
  1123. (uint8_t *)hpcd->Setup);
  1124. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
  1125. }
  1126. /* Handle Enumeration done Interrupt */
  1127. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
  1128. {
  1129. (void)USB_ActivateSetup(hpcd->Instance);
  1130. hpcd->Init.speed = USB_GetDevSpeed(hpcd->Instance);
  1131. /* Set USB Turnaround time */
  1132. (void)USB_SetTurnaroundTime(hpcd->Instance,
  1133. HAL_RCC_GetHCLKFreq(),
  1134. (uint8_t)hpcd->Init.speed);
  1135. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1136. hpcd->ResetCallback(hpcd);
  1137. #else
  1138. HAL_PCD_ResetCallback(hpcd);
  1139. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1140. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
  1141. }
  1142. /* Handle SOF Interrupt */
  1143. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF))
  1144. {
  1145. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1146. hpcd->SOFCallback(hpcd);
  1147. #else
  1148. HAL_PCD_SOFCallback(hpcd);
  1149. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1150. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
  1151. }
  1152. /* Handle Incomplete ISO IN Interrupt */
  1153. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR))
  1154. {
  1155. /* Keep application checking the corresponding Iso IN endpoint
  1156. causing the incomplete Interrupt */
  1157. epnum = 0U;
  1158. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1159. hpcd->ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
  1160. #else
  1161. HAL_PCD_ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
  1162. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1163. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
  1164. }
  1165. /* Handle Incomplete ISO OUT Interrupt */
  1166. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
  1167. {
  1168. /* Keep application checking the corresponding Iso OUT endpoint
  1169. causing the incomplete Interrupt */
  1170. epnum = 0U;
  1171. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1172. hpcd->ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
  1173. #else
  1174. HAL_PCD_ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
  1175. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1176. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
  1177. }
  1178. /* Handle Connection event Interrupt */
  1179. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT))
  1180. {
  1181. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1182. hpcd->ConnectCallback(hpcd);
  1183. #else
  1184. HAL_PCD_ConnectCallback(hpcd);
  1185. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1186. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
  1187. }
  1188. /* Handle Disconnection event Interrupt */
  1189. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT))
  1190. {
  1191. temp = hpcd->Instance->GOTGINT;
  1192. if ((temp & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET)
  1193. {
  1194. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1195. hpcd->DisconnectCallback(hpcd);
  1196. #else
  1197. HAL_PCD_DisconnectCallback(hpcd);
  1198. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1199. }
  1200. hpcd->Instance->GOTGINT |= temp;
  1201. }
  1202. }
  1203. }
  1204. /**
  1205. * @brief Handles PCD Wakeup interrupt request.
  1206. * @param hpcd PCD handle
  1207. * @retval HAL status
  1208. */
  1209. void HAL_PCD_WKUP_IRQHandler(PCD_HandleTypeDef *hpcd)
  1210. {
  1211. USB_OTG_GlobalTypeDef *USBx;
  1212. USBx = hpcd->Instance;
  1213. if ((USBx->CID & (0x1U << 8)) == 0U)
  1214. {
  1215. /* Clear EXTI pending Bit */
  1216. __HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG();
  1217. }
  1218. else
  1219. {
  1220. /* Clear EXTI pending Bit */
  1221. __HAL_USB_OTG_HS_WAKEUP_EXTI_CLEAR_FLAG();
  1222. }
  1223. }
  1224. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  1225. /**
  1226. * @brief Data OUT stage callback.
  1227. * @param hpcd PCD handle
  1228. * @param epnum endpoint number
  1229. * @retval None
  1230. */
  1231. __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1232. {
  1233. /* Prevent unused argument(s) compilation warning */
  1234. UNUSED(hpcd);
  1235. UNUSED(epnum);
  1236. /* NOTE : This function should not be modified, when the callback is needed,
  1237. the HAL_PCD_DataOutStageCallback could be implemented in the user file
  1238. */
  1239. }
  1240. /**
  1241. * @brief Data IN stage callback
  1242. * @param hpcd PCD handle
  1243. * @param epnum endpoint number
  1244. * @retval None
  1245. */
  1246. __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1247. {
  1248. /* Prevent unused argument(s) compilation warning */
  1249. UNUSED(hpcd);
  1250. UNUSED(epnum);
  1251. /* NOTE : This function should not be modified, when the callback is needed,
  1252. the HAL_PCD_DataInStageCallback could be implemented in the user file
  1253. */
  1254. }
  1255. /**
  1256. * @brief Setup stage callback
  1257. * @param hpcd PCD handle
  1258. * @retval None
  1259. */
  1260. __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  1261. {
  1262. /* Prevent unused argument(s) compilation warning */
  1263. UNUSED(hpcd);
  1264. /* NOTE : This function should not be modified, when the callback is needed,
  1265. the HAL_PCD_SetupStageCallback could be implemented in the user file
  1266. */
  1267. }
  1268. /**
  1269. * @brief USB Start Of Frame callback.
  1270. * @param hpcd PCD handle
  1271. * @retval None
  1272. */
  1273. __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  1274. {
  1275. /* Prevent unused argument(s) compilation warning */
  1276. UNUSED(hpcd);
  1277. /* NOTE : This function should not be modified, when the callback is needed,
  1278. the HAL_PCD_SOFCallback could be implemented in the user file
  1279. */
  1280. }
  1281. /**
  1282. * @brief USB Reset callback.
  1283. * @param hpcd PCD handle
  1284. * @retval None
  1285. */
  1286. __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  1287. {
  1288. /* Prevent unused argument(s) compilation warning */
  1289. UNUSED(hpcd);
  1290. /* NOTE : This function should not be modified, when the callback is needed,
  1291. the HAL_PCD_ResetCallback could be implemented in the user file
  1292. */
  1293. }
  1294. /**
  1295. * @brief Suspend event callback.
  1296. * @param hpcd PCD handle
  1297. * @retval None
  1298. */
  1299. __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  1300. {
  1301. /* Prevent unused argument(s) compilation warning */
  1302. UNUSED(hpcd);
  1303. /* NOTE : This function should not be modified, when the callback is needed,
  1304. the HAL_PCD_SuspendCallback could be implemented in the user file
  1305. */
  1306. }
  1307. /**
  1308. * @brief Resume event callback.
  1309. * @param hpcd PCD handle
  1310. * @retval None
  1311. */
  1312. __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  1313. {
  1314. /* Prevent unused argument(s) compilation warning */
  1315. UNUSED(hpcd);
  1316. /* NOTE : This function should not be modified, when the callback is needed,
  1317. the HAL_PCD_ResumeCallback could be implemented in the user file
  1318. */
  1319. }
  1320. /**
  1321. * @brief Incomplete ISO OUT callback.
  1322. * @param hpcd PCD handle
  1323. * @param epnum endpoint number
  1324. * @retval None
  1325. */
  1326. __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1327. {
  1328. /* Prevent unused argument(s) compilation warning */
  1329. UNUSED(hpcd);
  1330. UNUSED(epnum);
  1331. /* NOTE : This function should not be modified, when the callback is needed,
  1332. the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
  1333. */
  1334. }
  1335. /**
  1336. * @brief Incomplete ISO IN callback.
  1337. * @param hpcd PCD handle
  1338. * @param epnum endpoint number
  1339. * @retval None
  1340. */
  1341. __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1342. {
  1343. /* Prevent unused argument(s) compilation warning */
  1344. UNUSED(hpcd);
  1345. UNUSED(epnum);
  1346. /* NOTE : This function should not be modified, when the callback is needed,
  1347. the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
  1348. */
  1349. }
  1350. /**
  1351. * @brief Connection event callback.
  1352. * @param hpcd PCD handle
  1353. * @retval None
  1354. */
  1355. __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  1356. {
  1357. /* Prevent unused argument(s) compilation warning */
  1358. UNUSED(hpcd);
  1359. /* NOTE : This function should not be modified, when the callback is needed,
  1360. the HAL_PCD_ConnectCallback could be implemented in the user file
  1361. */
  1362. }
  1363. /**
  1364. * @brief Disconnection event callback.
  1365. * @param hpcd PCD handle
  1366. * @retval None
  1367. */
  1368. __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  1369. {
  1370. /* Prevent unused argument(s) compilation warning */
  1371. UNUSED(hpcd);
  1372. /* NOTE : This function should not be modified, when the callback is needed,
  1373. the HAL_PCD_DisconnectCallback could be implemented in the user file
  1374. */
  1375. }
  1376. /**
  1377. * @}
  1378. */
  1379. /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
  1380. * @brief management functions
  1381. *
  1382. @verbatim
  1383. ===============================================================================
  1384. ##### Peripheral Control functions #####
  1385. ===============================================================================
  1386. [..]
  1387. This subsection provides a set of functions allowing to control the PCD data
  1388. transfers.
  1389. @endverbatim
  1390. * @{
  1391. */
  1392. /**
  1393. * @brief Connect the USB device
  1394. * @param hpcd PCD handle
  1395. * @retval HAL status
  1396. */
  1397. HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
  1398. {
  1399. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1400. __HAL_LOCK(hpcd);
  1401. if ((hpcd->Init.battery_charging_enable == 1U) &&
  1402. (hpcd->Init.phy_itface != USB_OTG_ULPI_PHY))
  1403. {
  1404. /* Enable USB Transceiver */
  1405. USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
  1406. }
  1407. (void)USB_DevConnect(hpcd->Instance);
  1408. __HAL_UNLOCK(hpcd);
  1409. return HAL_OK;
  1410. }
  1411. /**
  1412. * @brief Disconnect the USB device.
  1413. * @param hpcd PCD handle
  1414. * @retval HAL status
  1415. */
  1416. HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
  1417. {
  1418. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1419. __HAL_LOCK(hpcd);
  1420. (void)USB_DevDisconnect(hpcd->Instance);
  1421. if ((hpcd->Init.battery_charging_enable == 1U) &&
  1422. (hpcd->Init.phy_itface != USB_OTG_ULPI_PHY))
  1423. {
  1424. /* Disable USB Transceiver */
  1425. USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
  1426. }
  1427. __HAL_UNLOCK(hpcd);
  1428. return HAL_OK;
  1429. }
  1430. /**
  1431. * @brief Set the USB Device address.
  1432. * @param hpcd PCD handle
  1433. * @param address new device address
  1434. * @retval HAL status
  1435. */
  1436. HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
  1437. {
  1438. __HAL_LOCK(hpcd);
  1439. hpcd->USB_Address = address;
  1440. (void)USB_SetDevAddress(hpcd->Instance, address);
  1441. __HAL_UNLOCK(hpcd);
  1442. return HAL_OK;
  1443. }
  1444. /**
  1445. * @brief Open and configure an endpoint.
  1446. * @param hpcd PCD handle
  1447. * @param ep_addr endpoint address
  1448. * @param ep_mps endpoint max packet size
  1449. * @param ep_type endpoint type
  1450. * @retval HAL status
  1451. */
  1452. HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
  1453. uint16_t ep_mps, uint8_t ep_type)
  1454. {
  1455. HAL_StatusTypeDef ret = HAL_OK;
  1456. PCD_EPTypeDef *ep;
  1457. if ((ep_addr & 0x80U) == 0x80U)
  1458. {
  1459. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1460. ep->is_in = 1U;
  1461. }
  1462. else
  1463. {
  1464. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1465. ep->is_in = 0U;
  1466. }
  1467. ep->num = ep_addr & EP_ADDR_MSK;
  1468. ep->maxpacket = ep_mps;
  1469. ep->type = ep_type;
  1470. if (ep->is_in != 0U)
  1471. {
  1472. /* Assign a Tx FIFO */
  1473. ep->tx_fifo_num = ep->num;
  1474. }
  1475. /* Set initial data PID. */
  1476. if (ep_type == EP_TYPE_BULK)
  1477. {
  1478. ep->data_pid_start = 0U;
  1479. }
  1480. __HAL_LOCK(hpcd);
  1481. (void)USB_ActivateEndpoint(hpcd->Instance, ep);
  1482. __HAL_UNLOCK(hpcd);
  1483. return ret;
  1484. }
  1485. /**
  1486. * @brief Deactivate an endpoint.
  1487. * @param hpcd PCD handle
  1488. * @param ep_addr endpoint address
  1489. * @retval HAL status
  1490. */
  1491. HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1492. {
  1493. PCD_EPTypeDef *ep;
  1494. if ((ep_addr & 0x80U) == 0x80U)
  1495. {
  1496. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1497. ep->is_in = 1U;
  1498. }
  1499. else
  1500. {
  1501. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1502. ep->is_in = 0U;
  1503. }
  1504. ep->num = ep_addr & EP_ADDR_MSK;
  1505. __HAL_LOCK(hpcd);
  1506. (void)USB_DeactivateEndpoint(hpcd->Instance, ep);
  1507. __HAL_UNLOCK(hpcd);
  1508. return HAL_OK;
  1509. }
  1510. /**
  1511. * @brief Receive an amount of data.
  1512. * @param hpcd PCD handle
  1513. * @param ep_addr endpoint address
  1514. * @param pBuf pointer to the reception buffer
  1515. * @param len amount of data to be received
  1516. * @retval HAL status
  1517. */
  1518. HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  1519. {
  1520. PCD_EPTypeDef *ep;
  1521. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1522. /*setup and start the Xfer */
  1523. ep->xfer_buff = pBuf;
  1524. ep->xfer_len = len;
  1525. ep->xfer_count = 0U;
  1526. ep->is_in = 0U;
  1527. ep->num = ep_addr & EP_ADDR_MSK;
  1528. if (hpcd->Init.dma_enable == 1U)
  1529. {
  1530. ep->dma_addr = (uint32_t)pBuf;
  1531. }
  1532. if ((ep_addr & EP_ADDR_MSK) == 0U)
  1533. {
  1534. (void)USB_EP0StartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
  1535. }
  1536. else
  1537. {
  1538. (void)USB_EPStartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
  1539. }
  1540. return HAL_OK;
  1541. }
  1542. /**
  1543. * @brief Get Received Data Size
  1544. * @param hpcd PCD handle
  1545. * @param ep_addr endpoint address
  1546. * @retval Data Size
  1547. */
  1548. uint32_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1549. {
  1550. return hpcd->OUT_ep[ep_addr & EP_ADDR_MSK].xfer_count;
  1551. }
  1552. /**
  1553. * @brief Send an amount of data
  1554. * @param hpcd PCD handle
  1555. * @param ep_addr endpoint address
  1556. * @param pBuf pointer to the transmission buffer
  1557. * @param len amount of data to be sent
  1558. * @retval HAL status
  1559. */
  1560. HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  1561. {
  1562. PCD_EPTypeDef *ep;
  1563. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1564. /*setup and start the Xfer */
  1565. ep->xfer_buff = pBuf;
  1566. ep->xfer_len = len;
  1567. ep->xfer_count = 0U;
  1568. ep->is_in = 1U;
  1569. ep->num = ep_addr & EP_ADDR_MSK;
  1570. if (hpcd->Init.dma_enable == 1U)
  1571. {
  1572. ep->dma_addr = (uint32_t)pBuf;
  1573. }
  1574. if ((ep_addr & EP_ADDR_MSK) == 0U)
  1575. {
  1576. (void)USB_EP0StartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
  1577. }
  1578. else
  1579. {
  1580. (void)USB_EPStartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
  1581. }
  1582. return HAL_OK;
  1583. }
  1584. /**
  1585. * @brief Set a STALL condition over an endpoint
  1586. * @param hpcd PCD handle
  1587. * @param ep_addr endpoint address
  1588. * @retval HAL status
  1589. */
  1590. HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1591. {
  1592. PCD_EPTypeDef *ep;
  1593. if (((uint32_t)ep_addr & EP_ADDR_MSK) > hpcd->Init.dev_endpoints)
  1594. {
  1595. return HAL_ERROR;
  1596. }
  1597. if ((0x80U & ep_addr) == 0x80U)
  1598. {
  1599. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1600. ep->is_in = 1U;
  1601. }
  1602. else
  1603. {
  1604. ep = &hpcd->OUT_ep[ep_addr];
  1605. ep->is_in = 0U;
  1606. }
  1607. ep->is_stall = 1U;
  1608. ep->num = ep_addr & EP_ADDR_MSK;
  1609. __HAL_LOCK(hpcd);
  1610. (void)USB_EPSetStall(hpcd->Instance, ep);
  1611. if ((ep_addr & EP_ADDR_MSK) == 0U)
  1612. {
  1613. (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t)hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
  1614. }
  1615. __HAL_UNLOCK(hpcd);
  1616. return HAL_OK;
  1617. }
  1618. /**
  1619. * @brief Clear a STALL condition over in an endpoint
  1620. * @param hpcd PCD handle
  1621. * @param ep_addr endpoint address
  1622. * @retval HAL status
  1623. */
  1624. HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1625. {
  1626. PCD_EPTypeDef *ep;
  1627. if (((uint32_t)ep_addr & 0x0FU) > hpcd->Init.dev_endpoints)
  1628. {
  1629. return HAL_ERROR;
  1630. }
  1631. if ((0x80U & ep_addr) == 0x80U)
  1632. {
  1633. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1634. ep->is_in = 1U;
  1635. }
  1636. else
  1637. {
  1638. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1639. ep->is_in = 0U;
  1640. }
  1641. ep->is_stall = 0U;
  1642. ep->num = ep_addr & EP_ADDR_MSK;
  1643. __HAL_LOCK(hpcd);
  1644. (void)USB_EPClearStall(hpcd->Instance, ep);
  1645. __HAL_UNLOCK(hpcd);
  1646. return HAL_OK;
  1647. }
  1648. /**
  1649. * @brief Flush an endpoint
  1650. * @param hpcd PCD handle
  1651. * @param ep_addr endpoint address
  1652. * @retval HAL status
  1653. */
  1654. HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1655. {
  1656. __HAL_LOCK(hpcd);
  1657. if ((ep_addr & 0x80U) == 0x80U)
  1658. {
  1659. (void)USB_FlushTxFifo(hpcd->Instance, (uint32_t)ep_addr & EP_ADDR_MSK);
  1660. }
  1661. else
  1662. {
  1663. (void)USB_FlushRxFifo(hpcd->Instance);
  1664. }
  1665. __HAL_UNLOCK(hpcd);
  1666. return HAL_OK;
  1667. }
  1668. /**
  1669. * @brief Activate remote wakeup signalling
  1670. * @param hpcd PCD handle
  1671. * @retval HAL status
  1672. */
  1673. HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1674. {
  1675. return (USB_ActivateRemoteWakeup(hpcd->Instance));
  1676. }
  1677. /**
  1678. * @brief De-activate remote wakeup signalling.
  1679. * @param hpcd PCD handle
  1680. * @retval HAL status
  1681. */
  1682. HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1683. {
  1684. return (USB_DeActivateRemoteWakeup(hpcd->Instance));
  1685. }
  1686. /**
  1687. * @}
  1688. */
  1689. /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
  1690. * @brief Peripheral State functions
  1691. *
  1692. @verbatim
  1693. ===============================================================================
  1694. ##### Peripheral State functions #####
  1695. ===============================================================================
  1696. [..]
  1697. This subsection permits to get in run-time the status of the peripheral
  1698. and the data flow.
  1699. @endverbatim
  1700. * @{
  1701. */
  1702. /**
  1703. * @brief Return the PCD handle state.
  1704. * @param hpcd PCD handle
  1705. * @retval HAL state
  1706. */
  1707. PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
  1708. {
  1709. return hpcd->State;
  1710. }
  1711. /**
  1712. * @}
  1713. */
  1714. /**
  1715. * @}
  1716. */
  1717. /* Private functions ---------------------------------------------------------*/
  1718. /** @addtogroup PCD_Private_Functions
  1719. * @{
  1720. */
  1721. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  1722. /**
  1723. * @brief Check FIFO for the next packet to be loaded.
  1724. * @param hpcd PCD handle
  1725. * @param epnum endpoint number
  1726. * @retval HAL status
  1727. */
  1728. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1729. {
  1730. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1731. uint32_t USBx_BASE = (uint32_t)USBx;
  1732. USB_OTG_EPTypeDef *ep;
  1733. uint32_t len;
  1734. uint32_t len32b;
  1735. uint32_t fifoemptymsk;
  1736. ep = &hpcd->IN_ep[epnum];
  1737. if (ep->xfer_count > ep->xfer_len)
  1738. {
  1739. return HAL_ERROR;
  1740. }
  1741. len = ep->xfer_len - ep->xfer_count;
  1742. if (len > ep->maxpacket)
  1743. {
  1744. len = ep->maxpacket;
  1745. }
  1746. len32b = (len + 3U) / 4U;
  1747. while (((USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) >= len32b) &&
  1748. (ep->xfer_count < ep->xfer_len) && (ep->xfer_len != 0U))
  1749. {
  1750. /* Write the FIFO */
  1751. len = ep->xfer_len - ep->xfer_count;
  1752. if (len > ep->maxpacket)
  1753. {
  1754. len = ep->maxpacket;
  1755. }
  1756. len32b = (len + 3U) / 4U;
  1757. (void)USB_WritePacket(USBx, ep->xfer_buff, (uint8_t)epnum, (uint16_t)len,
  1758. (uint8_t)hpcd->Init.dma_enable);
  1759. ep->xfer_buff += len;
  1760. ep->xfer_count += len;
  1761. }
  1762. if (ep->xfer_len <= ep->xfer_count)
  1763. {
  1764. fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));
  1765. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  1766. }
  1767. return HAL_OK;
  1768. }
  1769. /**
  1770. * @brief process EP OUT transfer complete interrupt.
  1771. * @param hpcd PCD handle
  1772. * @param epnum endpoint number
  1773. * @retval HAL status
  1774. */
  1775. static HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1776. {
  1777. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1778. uint32_t USBx_BASE = (uint32_t)USBx;
  1779. uint32_t gSNPSiD = *(__IO uint32_t *)(&USBx->CID + 0x1U);
  1780. uint32_t DoepintReg = USBx_OUTEP(epnum)->DOEPINT;
  1781. if (hpcd->Init.dma_enable == 1U)
  1782. {
  1783. if ((DoepintReg & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP) /* Class C */
  1784. {
  1785. /* StupPktRcvd = 1 this is a setup packet */
  1786. if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
  1787. ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX))
  1788. {
  1789. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
  1790. }
  1791. }
  1792. else if ((DoepintReg & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR) /* Class E */
  1793. {
  1794. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
  1795. }
  1796. else if ((DoepintReg & (USB_OTG_DOEPINT_STUP | USB_OTG_DOEPINT_OTEPSPR)) == 0U)
  1797. {
  1798. /* StupPktRcvd = 1 this is a setup packet */
  1799. if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
  1800. ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX))
  1801. {
  1802. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
  1803. }
  1804. else
  1805. {
  1806. /* out data packet received over EP0 */
  1807. hpcd->OUT_ep[epnum].xfer_count =
  1808. hpcd->OUT_ep[epnum].maxpacket -
  1809. (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
  1810. hpcd->OUT_ep[epnum].xfer_buff += hpcd->OUT_ep[epnum].maxpacket;
  1811. if ((epnum == 0U) && (hpcd->OUT_ep[epnum].xfer_len == 0U))
  1812. {
  1813. /* this is ZLP, so prepare EP0 for next setup */
  1814. (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
  1815. }
  1816. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1817. hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
  1818. #else
  1819. HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
  1820. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1821. }
  1822. }
  1823. else
  1824. {
  1825. /* ... */
  1826. }
  1827. }
  1828. else
  1829. {
  1830. if (gSNPSiD == USB_OTG_CORE_ID_310A)
  1831. {
  1832. /* StupPktRcvd = 1 this is a setup packet */
  1833. if ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX)
  1834. {
  1835. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
  1836. }
  1837. else
  1838. {
  1839. if ((DoepintReg & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
  1840. {
  1841. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
  1842. }
  1843. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1844. hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
  1845. #else
  1846. HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
  1847. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1848. }
  1849. }
  1850. else
  1851. {
  1852. if ((epnum == 0U) && (hpcd->OUT_ep[epnum].xfer_len == 0U))
  1853. {
  1854. /* this is ZLP, so prepare EP0 for next setup */
  1855. (void)USB_EP0_OutStart(hpcd->Instance, 0U, (uint8_t *)hpcd->Setup);
  1856. }
  1857. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1858. hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
  1859. #else
  1860. HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
  1861. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1862. }
  1863. }
  1864. return HAL_OK;
  1865. }
  1866. /**
  1867. * @brief process EP OUT setup packet received interrupt.
  1868. * @param hpcd PCD handle
  1869. * @param epnum endpoint number
  1870. * @retval HAL status
  1871. */
  1872. static HAL_StatusTypeDef PCD_EP_OutSetupPacket_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1873. {
  1874. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1875. uint32_t USBx_BASE = (uint32_t)USBx;
  1876. uint32_t gSNPSiD = *(__IO uint32_t *)(&USBx->CID + 0x1U);
  1877. uint32_t DoepintReg = USBx_OUTEP(epnum)->DOEPINT;
  1878. if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
  1879. ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX))
  1880. {
  1881. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
  1882. }
  1883. /* Inform the upper layer that a setup packet is available */
  1884. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1885. hpcd->SetupStageCallback(hpcd);
  1886. #else
  1887. HAL_PCD_SetupStageCallback(hpcd);
  1888. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1889. if ((gSNPSiD > USB_OTG_CORE_ID_300A) && (hpcd->Init.dma_enable == 1U))
  1890. {
  1891. (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
  1892. }
  1893. return HAL_OK;
  1894. }
  1895. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  1896. /**
  1897. * @}
  1898. */
  1899. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  1900. #endif /* HAL_PCD_MODULE_ENABLED */
  1901. /**
  1902. * @}
  1903. */
  1904. /**
  1905. * @}
  1906. */
  1907. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/