训练营PLSR题目
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

2388 satır
62 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. ******************************************************************************
  14. * @attention
  15. *
  16. * Copyright (c) 2016 STMicroelectronics.
  17. * All rights reserved.
  18. *
  19. * This software is licensed under terms that can be found in the LICENSE file
  20. * in the root directory of this software component.
  21. * If no LICENSE file comes with this software, it is provided AS-IS.
  22. *
  23. ******************************************************************************
  24. @verbatim
  25. ==============================================================================
  26. ##### How to use this driver #####
  27. ==============================================================================
  28. [..]
  29. The PCD HAL driver can be used as follows:
  30. (#) Declare a PCD_HandleTypeDef handle structure, for example:
  31. PCD_HandleTypeDef hpcd;
  32. (#) Fill parameters of Init structure in HCD handle
  33. (#) Call HAL_PCD_Init() API to initialize the PCD peripheral (Core, Device core, ...)
  34. (#) Initialize the PCD low level resources through the HAL_PCD_MspInit() API:
  35. (##) Enable the PCD/USB Low Level interface clock using
  36. (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
  37. (+++) __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); (For High Speed Mode)
  38. (##) Initialize the related GPIO clocks
  39. (##) Configure PCD pin-out
  40. (##) Configure PCD NVIC interrupt
  41. (#)Associate the Upper USB device stack to the HAL PCD Driver:
  42. (##) hpcd.pData = pdev;
  43. (#)Enable PCD transmission and reception:
  44. (##) HAL_PCD_Start();
  45. @endverbatim
  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 callback 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 RegVal;
  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. /* store current frame number */
  895. hpcd->FrameNumber = (USBx_DEVICE->DSTS & USB_OTG_DSTS_FNSOF_Msk) >> USB_OTG_DSTS_FNSOF_Pos;
  896. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_MMIS))
  897. {
  898. /* incorrect mode, acknowledge the interrupt */
  899. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_MMIS);
  900. }
  901. /* Handle RxQLevel Interrupt */
  902. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_RXFLVL))
  903. {
  904. USB_MASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  905. RegVal = USBx->GRXSTSP;
  906. ep = &hpcd->OUT_ep[RegVal & USB_OTG_GRXSTSP_EPNUM];
  907. if (((RegVal & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_DATA_UPDT)
  908. {
  909. if ((RegVal & USB_OTG_GRXSTSP_BCNT) != 0U)
  910. {
  911. (void)USB_ReadPacket(USBx, ep->xfer_buff,
  912. (uint16_t)((RegVal & USB_OTG_GRXSTSP_BCNT) >> 4));
  913. ep->xfer_buff += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
  914. ep->xfer_count += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
  915. }
  916. }
  917. else if (((RegVal & USB_OTG_GRXSTSP_PKTSTS) >> 17) == STS_SETUP_UPDT)
  918. {
  919. (void)USB_ReadPacket(USBx, (uint8_t *)hpcd->Setup, 8U);
  920. ep->xfer_count += (RegVal & USB_OTG_GRXSTSP_BCNT) >> 4;
  921. }
  922. else
  923. {
  924. /* ... */
  925. }
  926. USB_UNMASK_INTERRUPT(hpcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  927. }
  928. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OEPINT))
  929. {
  930. epnum = 0U;
  931. /* Read in the device interrupt bits */
  932. ep_intr = USB_ReadDevAllOutEpInterrupt(hpcd->Instance);
  933. while (ep_intr != 0U)
  934. {
  935. if ((ep_intr & 0x1U) != 0U)
  936. {
  937. epint = USB_ReadDevOutEPInterrupt(hpcd->Instance, (uint8_t)epnum);
  938. if ((epint & USB_OTG_DOEPINT_XFRC) == USB_OTG_DOEPINT_XFRC)
  939. {
  940. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_XFRC);
  941. (void)PCD_EP_OutXfrComplete_int(hpcd, epnum);
  942. }
  943. if ((epint & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP)
  944. {
  945. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STUP);
  946. /* Class B setup phase done for previous decoded setup */
  947. (void)PCD_EP_OutSetupPacket_int(hpcd, epnum);
  948. }
  949. if ((epint & USB_OTG_DOEPINT_OTEPDIS) == USB_OTG_DOEPINT_OTEPDIS)
  950. {
  951. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPDIS);
  952. }
  953. /* Clear OUT Endpoint disable interrupt */
  954. if ((epint & USB_OTG_DOEPINT_EPDISD) == USB_OTG_DOEPINT_EPDISD)
  955. {
  956. if ((USBx->GINTSTS & USB_OTG_GINTSTS_BOUTNAKEFF) == USB_OTG_GINTSTS_BOUTNAKEFF)
  957. {
  958. USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGONAK;
  959. }
  960. ep = &hpcd->OUT_ep[epnum];
  961. if (ep->is_iso_incomplete == 1U)
  962. {
  963. ep->is_iso_incomplete = 0U;
  964. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  965. hpcd->ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
  966. #else
  967. HAL_PCD_ISOOUTIncompleteCallback(hpcd, (uint8_t)epnum);
  968. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  969. }
  970. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_EPDISD);
  971. }
  972. /* Clear Status Phase Received interrupt */
  973. if ((epint & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
  974. {
  975. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
  976. }
  977. /* Clear OUT NAK interrupt */
  978. if ((epint & USB_OTG_DOEPINT_NAK) == USB_OTG_DOEPINT_NAK)
  979. {
  980. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_NAK);
  981. }
  982. }
  983. epnum++;
  984. ep_intr >>= 1U;
  985. }
  986. }
  987. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IEPINT))
  988. {
  989. /* Read in the device interrupt bits */
  990. ep_intr = USB_ReadDevAllInEpInterrupt(hpcd->Instance);
  991. epnum = 0U;
  992. while (ep_intr != 0U)
  993. {
  994. if ((ep_intr & 0x1U) != 0U) /* In ITR */
  995. {
  996. epint = USB_ReadDevInEPInterrupt(hpcd->Instance, (uint8_t)epnum);
  997. if ((epint & USB_OTG_DIEPINT_XFRC) == USB_OTG_DIEPINT_XFRC)
  998. {
  999. fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));
  1000. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  1001. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_XFRC);
  1002. if (hpcd->Init.dma_enable == 1U)
  1003. {
  1004. hpcd->IN_ep[epnum].xfer_buff += hpcd->IN_ep[epnum].maxpacket;
  1005. /* this is ZLP, so prepare EP0 for next setup */
  1006. if ((epnum == 0U) && (hpcd->IN_ep[epnum].xfer_len == 0U))
  1007. {
  1008. /* prepare to rx more setup packets */
  1009. (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
  1010. }
  1011. }
  1012. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1013. hpcd->DataInStageCallback(hpcd, (uint8_t)epnum);
  1014. #else
  1015. HAL_PCD_DataInStageCallback(hpcd, (uint8_t)epnum);
  1016. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1017. }
  1018. if ((epint & USB_OTG_DIEPINT_TOC) == USB_OTG_DIEPINT_TOC)
  1019. {
  1020. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_TOC);
  1021. }
  1022. if ((epint & USB_OTG_DIEPINT_ITTXFE) == USB_OTG_DIEPINT_ITTXFE)
  1023. {
  1024. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_ITTXFE);
  1025. }
  1026. if ((epint & USB_OTG_DIEPINT_INEPNE) == USB_OTG_DIEPINT_INEPNE)
  1027. {
  1028. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_INEPNE);
  1029. }
  1030. if ((epint & USB_OTG_DIEPINT_EPDISD) == USB_OTG_DIEPINT_EPDISD)
  1031. {
  1032. (void)USB_FlushTxFifo(USBx, epnum);
  1033. ep = &hpcd->IN_ep[epnum];
  1034. if (ep->is_iso_incomplete == 1U)
  1035. {
  1036. ep->is_iso_incomplete = 0U;
  1037. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1038. hpcd->ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
  1039. #else
  1040. HAL_PCD_ISOINIncompleteCallback(hpcd, (uint8_t)epnum);
  1041. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1042. }
  1043. CLEAR_IN_EP_INTR(epnum, USB_OTG_DIEPINT_EPDISD);
  1044. }
  1045. if ((epint & USB_OTG_DIEPINT_TXFE) == USB_OTG_DIEPINT_TXFE)
  1046. {
  1047. (void)PCD_WriteEmptyTxFifo(hpcd, epnum);
  1048. }
  1049. }
  1050. epnum++;
  1051. ep_intr >>= 1U;
  1052. }
  1053. }
  1054. /* Handle Resume Interrupt */
  1055. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT))
  1056. {
  1057. /* Clear the Remote Wake-up Signaling */
  1058. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  1059. if (hpcd->LPM_State == LPM_L1)
  1060. {
  1061. hpcd->LPM_State = LPM_L0;
  1062. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1063. hpcd->LPMCallback(hpcd, PCD_LPM_L0_ACTIVE);
  1064. #else
  1065. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L0_ACTIVE);
  1066. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1067. }
  1068. else
  1069. {
  1070. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1071. hpcd->ResumeCallback(hpcd);
  1072. #else
  1073. HAL_PCD_ResumeCallback(hpcd);
  1074. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1075. }
  1076. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_WKUINT);
  1077. }
  1078. /* Handle Suspend Interrupt */
  1079. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP))
  1080. {
  1081. if ((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
  1082. {
  1083. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1084. hpcd->SuspendCallback(hpcd);
  1085. #else
  1086. HAL_PCD_SuspendCallback(hpcd);
  1087. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1088. }
  1089. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBSUSP);
  1090. }
  1091. #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  1092. /* Handle LPM Interrupt */
  1093. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT))
  1094. {
  1095. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_LPMINT);
  1096. if (hpcd->LPM_State == LPM_L0)
  1097. {
  1098. hpcd->LPM_State = LPM_L1;
  1099. hpcd->BESL = (hpcd->Instance->GLPMCFG & USB_OTG_GLPMCFG_BESL) >> 2U;
  1100. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1101. hpcd->LPMCallback(hpcd, PCD_LPM_L1_ACTIVE);
  1102. #else
  1103. HAL_PCDEx_LPM_Callback(hpcd, PCD_LPM_L1_ACTIVE);
  1104. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1105. }
  1106. else
  1107. {
  1108. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1109. hpcd->SuspendCallback(hpcd);
  1110. #else
  1111. HAL_PCD_SuspendCallback(hpcd);
  1112. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1113. }
  1114. }
  1115. #endif /* defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx) */
  1116. /* Handle Reset Interrupt */
  1117. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_USBRST))
  1118. {
  1119. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_RWUSIG;
  1120. (void)USB_FlushTxFifo(hpcd->Instance, 0x10U);
  1121. for (i = 0U; i < hpcd->Init.dev_endpoints; i++)
  1122. {
  1123. USBx_INEP(i)->DIEPINT = 0xFB7FU;
  1124. USBx_INEP(i)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
  1125. USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
  1126. USBx_OUTEP(i)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
  1127. USBx_OUTEP(i)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
  1128. }
  1129. USBx_DEVICE->DAINTMSK |= 0x10001U;
  1130. if (hpcd->Init.use_dedicated_ep1 != 0U)
  1131. {
  1132. USBx_DEVICE->DOUTEP1MSK |= USB_OTG_DOEPMSK_STUPM |
  1133. USB_OTG_DOEPMSK_XFRCM |
  1134. USB_OTG_DOEPMSK_EPDM;
  1135. USBx_DEVICE->DINEP1MSK |= USB_OTG_DIEPMSK_TOM |
  1136. USB_OTG_DIEPMSK_XFRCM |
  1137. USB_OTG_DIEPMSK_EPDM;
  1138. }
  1139. else
  1140. {
  1141. USBx_DEVICE->DOEPMSK |= USB_OTG_DOEPMSK_STUPM |
  1142. USB_OTG_DOEPMSK_XFRCM |
  1143. USB_OTG_DOEPMSK_EPDM |
  1144. USB_OTG_DOEPMSK_OTEPSPRM |
  1145. USB_OTG_DOEPMSK_NAKM;
  1146. USBx_DEVICE->DIEPMSK |= USB_OTG_DIEPMSK_TOM |
  1147. USB_OTG_DIEPMSK_XFRCM |
  1148. USB_OTG_DIEPMSK_EPDM;
  1149. }
  1150. /* Set Default Address to 0 */
  1151. USBx_DEVICE->DCFG &= ~USB_OTG_DCFG_DAD;
  1152. /* setup EP0 to receive SETUP packets */
  1153. (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t)hpcd->Init.dma_enable,
  1154. (uint8_t *)hpcd->Setup);
  1155. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_USBRST);
  1156. }
  1157. /* Handle Enumeration done Interrupt */
  1158. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE))
  1159. {
  1160. (void)USB_ActivateSetup(hpcd->Instance);
  1161. hpcd->Init.speed = USB_GetDevSpeed(hpcd->Instance);
  1162. /* Set USB Turnaround time */
  1163. (void)USB_SetTurnaroundTime(hpcd->Instance,
  1164. HAL_RCC_GetHCLKFreq(),
  1165. (uint8_t)hpcd->Init.speed);
  1166. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1167. hpcd->ResetCallback(hpcd);
  1168. #else
  1169. HAL_PCD_ResetCallback(hpcd);
  1170. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1171. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_ENUMDNE);
  1172. }
  1173. /* Handle SOF Interrupt */
  1174. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SOF))
  1175. {
  1176. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1177. hpcd->SOFCallback(hpcd);
  1178. #else
  1179. HAL_PCD_SOFCallback(hpcd);
  1180. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1181. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SOF);
  1182. }
  1183. /* Handle Global OUT NAK effective Interrupt */
  1184. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_BOUTNAKEFF))
  1185. {
  1186. USBx->GINTMSK &= ~USB_OTG_GINTMSK_GONAKEFFM;
  1187. for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
  1188. {
  1189. if (hpcd->OUT_ep[epnum].is_iso_incomplete == 1U)
  1190. {
  1191. /* Abort current transaction and disable the EP */
  1192. (void)HAL_PCD_EP_Abort(hpcd, (uint8_t)epnum);
  1193. }
  1194. }
  1195. }
  1196. /* Handle Incomplete ISO IN Interrupt */
  1197. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR))
  1198. {
  1199. for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
  1200. {
  1201. RegVal = USBx_INEP(epnum)->DIEPCTL;
  1202. if ((hpcd->IN_ep[epnum].type == EP_TYPE_ISOC) &&
  1203. ((RegVal & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA))
  1204. {
  1205. hpcd->IN_ep[epnum].is_iso_incomplete = 1U;
  1206. /* Abort current transaction and disable the EP */
  1207. (void)HAL_PCD_EP_Abort(hpcd, (uint8_t)(epnum | 0x80U));
  1208. }
  1209. }
  1210. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_IISOIXFR);
  1211. }
  1212. /* Handle Incomplete ISO OUT Interrupt */
  1213. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
  1214. {
  1215. for (epnum = 1U; epnum < hpcd->Init.dev_endpoints; epnum++)
  1216. {
  1217. RegVal = USBx_OUTEP(epnum)->DOEPCTL;
  1218. if ((hpcd->OUT_ep[epnum].type == EP_TYPE_ISOC) &&
  1219. ((RegVal & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA) &&
  1220. ((RegVal & (0x1U << 16)) == (hpcd->FrameNumber & 0x1U)))
  1221. {
  1222. hpcd->OUT_ep[epnum].is_iso_incomplete = 1U;
  1223. USBx->GINTMSK |= USB_OTG_GINTMSK_GONAKEFFM;
  1224. if ((USBx->GINTSTS & USB_OTG_GINTSTS_BOUTNAKEFF) == 0U)
  1225. {
  1226. USBx_DEVICE->DCTL |= USB_OTG_DCTL_SGONAK;
  1227. break;
  1228. }
  1229. }
  1230. }
  1231. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
  1232. }
  1233. /* Handle Connection event Interrupt */
  1234. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT))
  1235. {
  1236. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1237. hpcd->ConnectCallback(hpcd);
  1238. #else
  1239. HAL_PCD_ConnectCallback(hpcd);
  1240. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1241. __HAL_PCD_CLEAR_FLAG(hpcd, USB_OTG_GINTSTS_SRQINT);
  1242. }
  1243. /* Handle Disconnection event Interrupt */
  1244. if (__HAL_PCD_GET_FLAG(hpcd, USB_OTG_GINTSTS_OTGINT))
  1245. {
  1246. RegVal = hpcd->Instance->GOTGINT;
  1247. if ((RegVal & USB_OTG_GOTGINT_SEDET) == USB_OTG_GOTGINT_SEDET)
  1248. {
  1249. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1250. hpcd->DisconnectCallback(hpcd);
  1251. #else
  1252. HAL_PCD_DisconnectCallback(hpcd);
  1253. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1254. }
  1255. hpcd->Instance->GOTGINT |= RegVal;
  1256. }
  1257. }
  1258. }
  1259. /**
  1260. * @brief Handles PCD Wakeup interrupt request.
  1261. * @param hpcd PCD handle
  1262. * @retval HAL status
  1263. */
  1264. void HAL_PCD_WKUP_IRQHandler(PCD_HandleTypeDef *hpcd)
  1265. {
  1266. USB_OTG_GlobalTypeDef *USBx;
  1267. USBx = hpcd->Instance;
  1268. if ((USBx->CID & (0x1U << 8)) == 0U)
  1269. {
  1270. /* Clear EXTI pending Bit */
  1271. __HAL_USB_OTG_FS_WAKEUP_EXTI_CLEAR_FLAG();
  1272. }
  1273. else
  1274. {
  1275. /* Clear EXTI pending Bit */
  1276. __HAL_USB_OTG_HS_WAKEUP_EXTI_CLEAR_FLAG();
  1277. }
  1278. }
  1279. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  1280. /**
  1281. * @brief Data OUT stage callback.
  1282. * @param hpcd PCD handle
  1283. * @param epnum endpoint number
  1284. * @retval None
  1285. */
  1286. __weak void HAL_PCD_DataOutStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1287. {
  1288. /* Prevent unused argument(s) compilation warning */
  1289. UNUSED(hpcd);
  1290. UNUSED(epnum);
  1291. /* NOTE : This function should not be modified, when the callback is needed,
  1292. the HAL_PCD_DataOutStageCallback could be implemented in the user file
  1293. */
  1294. }
  1295. /**
  1296. * @brief Data IN stage callback
  1297. * @param hpcd PCD handle
  1298. * @param epnum endpoint number
  1299. * @retval None
  1300. */
  1301. __weak void HAL_PCD_DataInStageCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1302. {
  1303. /* Prevent unused argument(s) compilation warning */
  1304. UNUSED(hpcd);
  1305. UNUSED(epnum);
  1306. /* NOTE : This function should not be modified, when the callback is needed,
  1307. the HAL_PCD_DataInStageCallback could be implemented in the user file
  1308. */
  1309. }
  1310. /**
  1311. * @brief Setup stage callback
  1312. * @param hpcd PCD handle
  1313. * @retval None
  1314. */
  1315. __weak void HAL_PCD_SetupStageCallback(PCD_HandleTypeDef *hpcd)
  1316. {
  1317. /* Prevent unused argument(s) compilation warning */
  1318. UNUSED(hpcd);
  1319. /* NOTE : This function should not be modified, when the callback is needed,
  1320. the HAL_PCD_SetupStageCallback could be implemented in the user file
  1321. */
  1322. }
  1323. /**
  1324. * @brief USB Start Of Frame callback.
  1325. * @param hpcd PCD handle
  1326. * @retval None
  1327. */
  1328. __weak void HAL_PCD_SOFCallback(PCD_HandleTypeDef *hpcd)
  1329. {
  1330. /* Prevent unused argument(s) compilation warning */
  1331. UNUSED(hpcd);
  1332. /* NOTE : This function should not be modified, when the callback is needed,
  1333. the HAL_PCD_SOFCallback could be implemented in the user file
  1334. */
  1335. }
  1336. /**
  1337. * @brief USB Reset callback.
  1338. * @param hpcd PCD handle
  1339. * @retval None
  1340. */
  1341. __weak void HAL_PCD_ResetCallback(PCD_HandleTypeDef *hpcd)
  1342. {
  1343. /* Prevent unused argument(s) compilation warning */
  1344. UNUSED(hpcd);
  1345. /* NOTE : This function should not be modified, when the callback is needed,
  1346. the HAL_PCD_ResetCallback could be implemented in the user file
  1347. */
  1348. }
  1349. /**
  1350. * @brief Suspend event callback.
  1351. * @param hpcd PCD handle
  1352. * @retval None
  1353. */
  1354. __weak void HAL_PCD_SuspendCallback(PCD_HandleTypeDef *hpcd)
  1355. {
  1356. /* Prevent unused argument(s) compilation warning */
  1357. UNUSED(hpcd);
  1358. /* NOTE : This function should not be modified, when the callback is needed,
  1359. the HAL_PCD_SuspendCallback could be implemented in the user file
  1360. */
  1361. }
  1362. /**
  1363. * @brief Resume event callback.
  1364. * @param hpcd PCD handle
  1365. * @retval None
  1366. */
  1367. __weak void HAL_PCD_ResumeCallback(PCD_HandleTypeDef *hpcd)
  1368. {
  1369. /* Prevent unused argument(s) compilation warning */
  1370. UNUSED(hpcd);
  1371. /* NOTE : This function should not be modified, when the callback is needed,
  1372. the HAL_PCD_ResumeCallback could be implemented in the user file
  1373. */
  1374. }
  1375. /**
  1376. * @brief Incomplete ISO OUT callback.
  1377. * @param hpcd PCD handle
  1378. * @param epnum endpoint number
  1379. * @retval None
  1380. */
  1381. __weak void HAL_PCD_ISOOUTIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1382. {
  1383. /* Prevent unused argument(s) compilation warning */
  1384. UNUSED(hpcd);
  1385. UNUSED(epnum);
  1386. /* NOTE : This function should not be modified, when the callback is needed,
  1387. the HAL_PCD_ISOOUTIncompleteCallback could be implemented in the user file
  1388. */
  1389. }
  1390. /**
  1391. * @brief Incomplete ISO IN callback.
  1392. * @param hpcd PCD handle
  1393. * @param epnum endpoint number
  1394. * @retval None
  1395. */
  1396. __weak void HAL_PCD_ISOINIncompleteCallback(PCD_HandleTypeDef *hpcd, uint8_t epnum)
  1397. {
  1398. /* Prevent unused argument(s) compilation warning */
  1399. UNUSED(hpcd);
  1400. UNUSED(epnum);
  1401. /* NOTE : This function should not be modified, when the callback is needed,
  1402. the HAL_PCD_ISOINIncompleteCallback could be implemented in the user file
  1403. */
  1404. }
  1405. /**
  1406. * @brief Connection event callback.
  1407. * @param hpcd PCD handle
  1408. * @retval None
  1409. */
  1410. __weak void HAL_PCD_ConnectCallback(PCD_HandleTypeDef *hpcd)
  1411. {
  1412. /* Prevent unused argument(s) compilation warning */
  1413. UNUSED(hpcd);
  1414. /* NOTE : This function should not be modified, when the callback is needed,
  1415. the HAL_PCD_ConnectCallback could be implemented in the user file
  1416. */
  1417. }
  1418. /**
  1419. * @brief Disconnection event callback.
  1420. * @param hpcd PCD handle
  1421. * @retval None
  1422. */
  1423. __weak void HAL_PCD_DisconnectCallback(PCD_HandleTypeDef *hpcd)
  1424. {
  1425. /* Prevent unused argument(s) compilation warning */
  1426. UNUSED(hpcd);
  1427. /* NOTE : This function should not be modified, when the callback is needed,
  1428. the HAL_PCD_DisconnectCallback could be implemented in the user file
  1429. */
  1430. }
  1431. /**
  1432. * @}
  1433. */
  1434. /** @defgroup PCD_Exported_Functions_Group3 Peripheral Control functions
  1435. * @brief management functions
  1436. *
  1437. @verbatim
  1438. ===============================================================================
  1439. ##### Peripheral Control functions #####
  1440. ===============================================================================
  1441. [..]
  1442. This subsection provides a set of functions allowing to control the PCD data
  1443. transfers.
  1444. @endverbatim
  1445. * @{
  1446. */
  1447. /**
  1448. * @brief Connect the USB device
  1449. * @param hpcd PCD handle
  1450. * @retval HAL status
  1451. */
  1452. HAL_StatusTypeDef HAL_PCD_DevConnect(PCD_HandleTypeDef *hpcd)
  1453. {
  1454. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1455. __HAL_LOCK(hpcd);
  1456. if ((hpcd->Init.battery_charging_enable == 1U) &&
  1457. (hpcd->Init.phy_itface != USB_OTG_ULPI_PHY))
  1458. {
  1459. /* Enable USB Transceiver */
  1460. USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
  1461. }
  1462. (void)USB_DevConnect(hpcd->Instance);
  1463. __HAL_UNLOCK(hpcd);
  1464. return HAL_OK;
  1465. }
  1466. /**
  1467. * @brief Disconnect the USB device.
  1468. * @param hpcd PCD handle
  1469. * @retval HAL status
  1470. */
  1471. HAL_StatusTypeDef HAL_PCD_DevDisconnect(PCD_HandleTypeDef *hpcd)
  1472. {
  1473. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1474. __HAL_LOCK(hpcd);
  1475. (void)USB_DevDisconnect(hpcd->Instance);
  1476. if ((hpcd->Init.battery_charging_enable == 1U) &&
  1477. (hpcd->Init.phy_itface != USB_OTG_ULPI_PHY))
  1478. {
  1479. /* Disable USB Transceiver */
  1480. USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
  1481. }
  1482. __HAL_UNLOCK(hpcd);
  1483. return HAL_OK;
  1484. }
  1485. /**
  1486. * @brief Set the USB Device address.
  1487. * @param hpcd PCD handle
  1488. * @param address new device address
  1489. * @retval HAL status
  1490. */
  1491. HAL_StatusTypeDef HAL_PCD_SetAddress(PCD_HandleTypeDef *hpcd, uint8_t address)
  1492. {
  1493. __HAL_LOCK(hpcd);
  1494. hpcd->USB_Address = address;
  1495. (void)USB_SetDevAddress(hpcd->Instance, address);
  1496. __HAL_UNLOCK(hpcd);
  1497. return HAL_OK;
  1498. }
  1499. /**
  1500. * @brief Open and configure an endpoint.
  1501. * @param hpcd PCD handle
  1502. * @param ep_addr endpoint address
  1503. * @param ep_mps endpoint max packet size
  1504. * @param ep_type endpoint type
  1505. * @retval HAL status
  1506. */
  1507. HAL_StatusTypeDef HAL_PCD_EP_Open(PCD_HandleTypeDef *hpcd, uint8_t ep_addr,
  1508. uint16_t ep_mps, uint8_t ep_type)
  1509. {
  1510. HAL_StatusTypeDef ret = HAL_OK;
  1511. PCD_EPTypeDef *ep;
  1512. if ((ep_addr & 0x80U) == 0x80U)
  1513. {
  1514. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1515. ep->is_in = 1U;
  1516. }
  1517. else
  1518. {
  1519. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1520. ep->is_in = 0U;
  1521. }
  1522. ep->num = ep_addr & EP_ADDR_MSK;
  1523. ep->maxpacket = ep_mps;
  1524. ep->type = ep_type;
  1525. if (ep->is_in != 0U)
  1526. {
  1527. /* Assign a Tx FIFO */
  1528. ep->tx_fifo_num = ep->num;
  1529. }
  1530. /* Set initial data PID. */
  1531. if (ep_type == EP_TYPE_BULK)
  1532. {
  1533. ep->data_pid_start = 0U;
  1534. }
  1535. __HAL_LOCK(hpcd);
  1536. (void)USB_ActivateEndpoint(hpcd->Instance, ep);
  1537. __HAL_UNLOCK(hpcd);
  1538. return ret;
  1539. }
  1540. /**
  1541. * @brief Deactivate an endpoint.
  1542. * @param hpcd PCD handle
  1543. * @param ep_addr endpoint address
  1544. * @retval HAL status
  1545. */
  1546. HAL_StatusTypeDef HAL_PCD_EP_Close(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1547. {
  1548. PCD_EPTypeDef *ep;
  1549. if ((ep_addr & 0x80U) == 0x80U)
  1550. {
  1551. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1552. ep->is_in = 1U;
  1553. }
  1554. else
  1555. {
  1556. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1557. ep->is_in = 0U;
  1558. }
  1559. ep->num = ep_addr & EP_ADDR_MSK;
  1560. __HAL_LOCK(hpcd);
  1561. (void)USB_DeactivateEndpoint(hpcd->Instance, ep);
  1562. __HAL_UNLOCK(hpcd);
  1563. return HAL_OK;
  1564. }
  1565. /**
  1566. * @brief Receive an amount of data.
  1567. * @param hpcd PCD handle
  1568. * @param ep_addr endpoint address
  1569. * @param pBuf pointer to the reception buffer
  1570. * @param len amount of data to be received
  1571. * @retval HAL status
  1572. */
  1573. HAL_StatusTypeDef HAL_PCD_EP_Receive(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  1574. {
  1575. PCD_EPTypeDef *ep;
  1576. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1577. /*setup and start the Xfer */
  1578. ep->xfer_buff = pBuf;
  1579. ep->xfer_len = len;
  1580. ep->xfer_count = 0U;
  1581. ep->is_in = 0U;
  1582. ep->num = ep_addr & EP_ADDR_MSK;
  1583. if (hpcd->Init.dma_enable == 1U)
  1584. {
  1585. ep->dma_addr = (uint32_t)pBuf;
  1586. }
  1587. if ((ep_addr & EP_ADDR_MSK) == 0U)
  1588. {
  1589. (void)USB_EP0StartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
  1590. }
  1591. else
  1592. {
  1593. (void)USB_EPStartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
  1594. }
  1595. return HAL_OK;
  1596. }
  1597. /**
  1598. * @brief Get Received Data Size
  1599. * @param hpcd PCD handle
  1600. * @param ep_addr endpoint address
  1601. * @retval Data Size
  1602. */
  1603. uint32_t HAL_PCD_EP_GetRxCount(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1604. {
  1605. return hpcd->OUT_ep[ep_addr & EP_ADDR_MSK].xfer_count;
  1606. }
  1607. /**
  1608. * @brief Send an amount of data
  1609. * @param hpcd PCD handle
  1610. * @param ep_addr endpoint address
  1611. * @param pBuf pointer to the transmission buffer
  1612. * @param len amount of data to be sent
  1613. * @retval HAL status
  1614. */
  1615. HAL_StatusTypeDef HAL_PCD_EP_Transmit(PCD_HandleTypeDef *hpcd, uint8_t ep_addr, uint8_t *pBuf, uint32_t len)
  1616. {
  1617. PCD_EPTypeDef *ep;
  1618. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1619. /*setup and start the Xfer */
  1620. ep->xfer_buff = pBuf;
  1621. ep->xfer_len = len;
  1622. ep->xfer_count = 0U;
  1623. ep->is_in = 1U;
  1624. ep->num = ep_addr & EP_ADDR_MSK;
  1625. if (hpcd->Init.dma_enable == 1U)
  1626. {
  1627. ep->dma_addr = (uint32_t)pBuf;
  1628. }
  1629. if ((ep_addr & EP_ADDR_MSK) == 0U)
  1630. {
  1631. (void)USB_EP0StartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
  1632. }
  1633. else
  1634. {
  1635. (void)USB_EPStartXfer(hpcd->Instance, ep, (uint8_t)hpcd->Init.dma_enable);
  1636. }
  1637. return HAL_OK;
  1638. }
  1639. /**
  1640. * @brief Set a STALL condition over an endpoint
  1641. * @param hpcd PCD handle
  1642. * @param ep_addr endpoint address
  1643. * @retval HAL status
  1644. */
  1645. HAL_StatusTypeDef HAL_PCD_EP_SetStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1646. {
  1647. PCD_EPTypeDef *ep;
  1648. if (((uint32_t)ep_addr & EP_ADDR_MSK) > hpcd->Init.dev_endpoints)
  1649. {
  1650. return HAL_ERROR;
  1651. }
  1652. if ((0x80U & ep_addr) == 0x80U)
  1653. {
  1654. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1655. ep->is_in = 1U;
  1656. }
  1657. else
  1658. {
  1659. ep = &hpcd->OUT_ep[ep_addr];
  1660. ep->is_in = 0U;
  1661. }
  1662. ep->is_stall = 1U;
  1663. ep->num = ep_addr & EP_ADDR_MSK;
  1664. __HAL_LOCK(hpcd);
  1665. (void)USB_EPSetStall(hpcd->Instance, ep);
  1666. if ((ep_addr & EP_ADDR_MSK) == 0U)
  1667. {
  1668. (void)USB_EP0_OutStart(hpcd->Instance, (uint8_t)hpcd->Init.dma_enable, (uint8_t *)hpcd->Setup);
  1669. }
  1670. __HAL_UNLOCK(hpcd);
  1671. return HAL_OK;
  1672. }
  1673. /**
  1674. * @brief Clear a STALL condition over in an endpoint
  1675. * @param hpcd PCD handle
  1676. * @param ep_addr endpoint address
  1677. * @retval HAL status
  1678. */
  1679. HAL_StatusTypeDef HAL_PCD_EP_ClrStall(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1680. {
  1681. PCD_EPTypeDef *ep;
  1682. if (((uint32_t)ep_addr & 0x0FU) > hpcd->Init.dev_endpoints)
  1683. {
  1684. return HAL_ERROR;
  1685. }
  1686. if ((0x80U & ep_addr) == 0x80U)
  1687. {
  1688. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1689. ep->is_in = 1U;
  1690. }
  1691. else
  1692. {
  1693. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1694. ep->is_in = 0U;
  1695. }
  1696. ep->is_stall = 0U;
  1697. ep->num = ep_addr & EP_ADDR_MSK;
  1698. __HAL_LOCK(hpcd);
  1699. (void)USB_EPClearStall(hpcd->Instance, ep);
  1700. __HAL_UNLOCK(hpcd);
  1701. return HAL_OK;
  1702. }
  1703. /**
  1704. * @brief Abort an USB EP transaction.
  1705. * @param hpcd PCD handle
  1706. * @param ep_addr endpoint address
  1707. * @retval HAL status
  1708. */
  1709. HAL_StatusTypeDef HAL_PCD_EP_Abort(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1710. {
  1711. HAL_StatusTypeDef ret;
  1712. PCD_EPTypeDef *ep;
  1713. if ((0x80U & ep_addr) == 0x80U)
  1714. {
  1715. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  1716. }
  1717. else
  1718. {
  1719. ep = &hpcd->OUT_ep[ep_addr & EP_ADDR_MSK];
  1720. }
  1721. /* Stop Xfer */
  1722. ret = USB_EPStopXfer(hpcd->Instance, ep);
  1723. return ret;
  1724. }
  1725. /**
  1726. * @brief Flush an endpoint
  1727. * @param hpcd PCD handle
  1728. * @param ep_addr endpoint address
  1729. * @retval HAL status
  1730. */
  1731. HAL_StatusTypeDef HAL_PCD_EP_Flush(PCD_HandleTypeDef *hpcd, uint8_t ep_addr)
  1732. {
  1733. __HAL_LOCK(hpcd);
  1734. if ((ep_addr & 0x80U) == 0x80U)
  1735. {
  1736. (void)USB_FlushTxFifo(hpcd->Instance, (uint32_t)ep_addr & EP_ADDR_MSK);
  1737. }
  1738. else
  1739. {
  1740. (void)USB_FlushRxFifo(hpcd->Instance);
  1741. }
  1742. __HAL_UNLOCK(hpcd);
  1743. return HAL_OK;
  1744. }
  1745. /**
  1746. * @brief Activate remote wakeup signalling
  1747. * @param hpcd PCD handle
  1748. * @retval HAL status
  1749. */
  1750. HAL_StatusTypeDef HAL_PCD_ActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1751. {
  1752. return (USB_ActivateRemoteWakeup(hpcd->Instance));
  1753. }
  1754. /**
  1755. * @brief De-activate remote wakeup signalling.
  1756. * @param hpcd PCD handle
  1757. * @retval HAL status
  1758. */
  1759. HAL_StatusTypeDef HAL_PCD_DeActivateRemoteWakeup(PCD_HandleTypeDef *hpcd)
  1760. {
  1761. return (USB_DeActivateRemoteWakeup(hpcd->Instance));
  1762. }
  1763. /**
  1764. * @}
  1765. */
  1766. /** @defgroup PCD_Exported_Functions_Group4 Peripheral State functions
  1767. * @brief Peripheral State functions
  1768. *
  1769. @verbatim
  1770. ===============================================================================
  1771. ##### Peripheral State functions #####
  1772. ===============================================================================
  1773. [..]
  1774. This subsection permits to get in run-time the status of the peripheral
  1775. and the data flow.
  1776. @endverbatim
  1777. * @{
  1778. */
  1779. /**
  1780. * @brief Return the PCD handle state.
  1781. * @param hpcd PCD handle
  1782. * @retval HAL state
  1783. */
  1784. PCD_StateTypeDef HAL_PCD_GetState(PCD_HandleTypeDef *hpcd)
  1785. {
  1786. return hpcd->State;
  1787. }
  1788. /**
  1789. * @brief Set the USB Device high speed test mode.
  1790. * @param hpcd PCD handle
  1791. * @param testmode USB Device high speed test mode
  1792. * @retval HAL status
  1793. */
  1794. HAL_StatusTypeDef HAL_PCD_SetTestMode(PCD_HandleTypeDef *hpcd, uint8_t testmode)
  1795. {
  1796. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1797. uint32_t USBx_BASE = (uint32_t)USBx;
  1798. switch (testmode)
  1799. {
  1800. case TEST_J:
  1801. case TEST_K:
  1802. case TEST_SE0_NAK:
  1803. case TEST_PACKET:
  1804. case TEST_FORCE_EN:
  1805. USBx_DEVICE->DCTL |= (uint32_t)testmode << 4;
  1806. break;
  1807. default:
  1808. break;
  1809. }
  1810. return HAL_OK;
  1811. }
  1812. /**
  1813. * @}
  1814. */
  1815. /**
  1816. * @}
  1817. */
  1818. /* Private functions ---------------------------------------------------------*/
  1819. /** @addtogroup PCD_Private_Functions
  1820. * @{
  1821. */
  1822. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  1823. /**
  1824. * @brief Check FIFO for the next packet to be loaded.
  1825. * @param hpcd PCD handle
  1826. * @param epnum endpoint number
  1827. * @retval HAL status
  1828. */
  1829. static HAL_StatusTypeDef PCD_WriteEmptyTxFifo(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1830. {
  1831. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1832. uint32_t USBx_BASE = (uint32_t)USBx;
  1833. USB_OTG_EPTypeDef *ep;
  1834. uint32_t len;
  1835. uint32_t len32b;
  1836. uint32_t fifoemptymsk;
  1837. ep = &hpcd->IN_ep[epnum];
  1838. if (ep->xfer_count > ep->xfer_len)
  1839. {
  1840. return HAL_ERROR;
  1841. }
  1842. len = ep->xfer_len - ep->xfer_count;
  1843. if (len > ep->maxpacket)
  1844. {
  1845. len = ep->maxpacket;
  1846. }
  1847. len32b = (len + 3U) / 4U;
  1848. while (((USBx_INEP(epnum)->DTXFSTS & USB_OTG_DTXFSTS_INEPTFSAV) >= len32b) &&
  1849. (ep->xfer_count < ep->xfer_len) && (ep->xfer_len != 0U))
  1850. {
  1851. /* Write the FIFO */
  1852. len = ep->xfer_len - ep->xfer_count;
  1853. if (len > ep->maxpacket)
  1854. {
  1855. len = ep->maxpacket;
  1856. }
  1857. len32b = (len + 3U) / 4U;
  1858. (void)USB_WritePacket(USBx, ep->xfer_buff, (uint8_t)epnum, (uint16_t)len,
  1859. (uint8_t)hpcd->Init.dma_enable);
  1860. ep->xfer_buff += len;
  1861. ep->xfer_count += len;
  1862. }
  1863. if (ep->xfer_len <= ep->xfer_count)
  1864. {
  1865. fifoemptymsk = (uint32_t)(0x1UL << (epnum & EP_ADDR_MSK));
  1866. USBx_DEVICE->DIEPEMPMSK &= ~fifoemptymsk;
  1867. }
  1868. return HAL_OK;
  1869. }
  1870. /**
  1871. * @brief process EP OUT transfer complete interrupt.
  1872. * @param hpcd PCD handle
  1873. * @param epnum endpoint number
  1874. * @retval HAL status
  1875. */
  1876. static HAL_StatusTypeDef PCD_EP_OutXfrComplete_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1877. {
  1878. USB_OTG_EPTypeDef *ep;
  1879. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1880. uint32_t USBx_BASE = (uint32_t)USBx;
  1881. uint32_t gSNPSiD = *(__IO uint32_t *)(&USBx->CID + 0x1U);
  1882. uint32_t DoepintReg = USBx_OUTEP(epnum)->DOEPINT;
  1883. if (hpcd->Init.dma_enable == 1U)
  1884. {
  1885. if ((DoepintReg & USB_OTG_DOEPINT_STUP) == USB_OTG_DOEPINT_STUP) /* Class C */
  1886. {
  1887. /* StupPktRcvd = 1 this is a setup packet */
  1888. if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
  1889. ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX))
  1890. {
  1891. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
  1892. }
  1893. }
  1894. else if ((DoepintReg & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR) /* Class E */
  1895. {
  1896. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
  1897. }
  1898. else if ((DoepintReg & (USB_OTG_DOEPINT_STUP | USB_OTG_DOEPINT_OTEPSPR)) == 0U)
  1899. {
  1900. /* StupPktRcvd = 1 this is a setup packet */
  1901. if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
  1902. ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX))
  1903. {
  1904. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
  1905. }
  1906. else
  1907. {
  1908. ep = &hpcd->OUT_ep[epnum];
  1909. /* out data packet received over EP */
  1910. ep->xfer_count = ep->xfer_size - (USBx_OUTEP(epnum)->DOEPTSIZ & USB_OTG_DOEPTSIZ_XFRSIZ);
  1911. if (epnum == 0U)
  1912. {
  1913. if (ep->xfer_len == 0U)
  1914. {
  1915. /* this is ZLP, so prepare EP0 for next setup */
  1916. (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
  1917. }
  1918. else
  1919. {
  1920. ep->xfer_buff += ep->xfer_count;
  1921. }
  1922. }
  1923. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1924. hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
  1925. #else
  1926. HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
  1927. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1928. }
  1929. }
  1930. else
  1931. {
  1932. /* ... */
  1933. }
  1934. }
  1935. else
  1936. {
  1937. if (gSNPSiD == USB_OTG_CORE_ID_310A)
  1938. {
  1939. /* StupPktRcvd = 1 this is a setup packet */
  1940. if ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX)
  1941. {
  1942. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
  1943. }
  1944. else
  1945. {
  1946. if ((DoepintReg & USB_OTG_DOEPINT_OTEPSPR) == USB_OTG_DOEPINT_OTEPSPR)
  1947. {
  1948. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_OTEPSPR);
  1949. }
  1950. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1951. hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
  1952. #else
  1953. HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
  1954. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1955. }
  1956. }
  1957. else
  1958. {
  1959. if ((epnum == 0U) && (hpcd->OUT_ep[epnum].xfer_len == 0U))
  1960. {
  1961. /* this is ZLP, so prepare EP0 for next setup */
  1962. (void)USB_EP0_OutStart(hpcd->Instance, 0U, (uint8_t *)hpcd->Setup);
  1963. }
  1964. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1965. hpcd->DataOutStageCallback(hpcd, (uint8_t)epnum);
  1966. #else
  1967. HAL_PCD_DataOutStageCallback(hpcd, (uint8_t)epnum);
  1968. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1969. }
  1970. }
  1971. return HAL_OK;
  1972. }
  1973. /**
  1974. * @brief process EP OUT setup packet received interrupt.
  1975. * @param hpcd PCD handle
  1976. * @param epnum endpoint number
  1977. * @retval HAL status
  1978. */
  1979. static HAL_StatusTypeDef PCD_EP_OutSetupPacket_int(PCD_HandleTypeDef *hpcd, uint32_t epnum)
  1980. {
  1981. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  1982. uint32_t USBx_BASE = (uint32_t)USBx;
  1983. uint32_t gSNPSiD = *(__IO uint32_t *)(&USBx->CID + 0x1U);
  1984. uint32_t DoepintReg = USBx_OUTEP(epnum)->DOEPINT;
  1985. if ((gSNPSiD > USB_OTG_CORE_ID_300A) &&
  1986. ((DoepintReg & USB_OTG_DOEPINT_STPKTRX) == USB_OTG_DOEPINT_STPKTRX))
  1987. {
  1988. CLEAR_OUT_EP_INTR(epnum, USB_OTG_DOEPINT_STPKTRX);
  1989. }
  1990. /* Inform the upper layer that a setup packet is available */
  1991. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  1992. hpcd->SetupStageCallback(hpcd);
  1993. #else
  1994. HAL_PCD_SetupStageCallback(hpcd);
  1995. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  1996. if ((gSNPSiD > USB_OTG_CORE_ID_300A) && (hpcd->Init.dma_enable == 1U))
  1997. {
  1998. (void)USB_EP0_OutStart(hpcd->Instance, 1U, (uint8_t *)hpcd->Setup);
  1999. }
  2000. return HAL_OK;
  2001. }
  2002. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  2003. /**
  2004. * @}
  2005. */
  2006. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  2007. #endif /* HAL_PCD_MODULE_ENABLED */
  2008. /**
  2009. * @}
  2010. */
  2011. /**
  2012. * @}
  2013. */