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

1729 line
47 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_hcd.c
  4. * @author MCD Application Team
  5. * @brief HCD 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. (#)Declare a HCD_HandleTypeDef handle structure, for example:
  30. HCD_HandleTypeDef hhcd;
  31. (#)Fill parameters of Init structure in HCD handle
  32. (#)Call HAL_HCD_Init() API to initialize the HCD peripheral (Core, Host core, ...)
  33. (#)Initialize the HCD low level resources through the HAL_HCD_MspInit() API:
  34. (##) Enable the HCD/USB Low Level interface clock using the following macros
  35. (+++) __HAL_RCC_USB_OTG_FS_CLK_ENABLE();
  36. (+++) __HAL_RCC_USB_OTG_HS_CLK_ENABLE(); (For High Speed Mode)
  37. (+++) __HAL_RCC_USB_OTG_HS_ULPI_CLK_ENABLE(); (For High Speed Mode)
  38. (##) Initialize the related GPIO clocks
  39. (##) Configure HCD pin-out
  40. (##) Configure HCD NVIC interrupt
  41. (#)Associate the Upper USB Host stack to the HAL HCD Driver:
  42. (##) hhcd.pData = phost;
  43. (#)Enable HCD transmission and reception:
  44. (##) HAL_HCD_Start();
  45. @endverbatim
  46. ******************************************************************************
  47. */
  48. /* Includes ------------------------------------------------------------------*/
  49. #include "stm32f4xx_hal.h"
  50. /** @addtogroup STM32F4xx_HAL_Driver
  51. * @{
  52. */
  53. #ifdef HAL_HCD_MODULE_ENABLED
  54. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  55. /** @defgroup HCD HCD
  56. * @brief HCD HAL module driver
  57. * @{
  58. */
  59. /* Private typedef -----------------------------------------------------------*/
  60. /* Private define ------------------------------------------------------------*/
  61. /* Private macro -------------------------------------------------------------*/
  62. /* Private variables ---------------------------------------------------------*/
  63. /* Private function prototypes -----------------------------------------------*/
  64. /** @defgroup HCD_Private_Functions HCD Private Functions
  65. * @{
  66. */
  67. static void HCD_HC_IN_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum);
  68. static void HCD_HC_OUT_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum);
  69. static void HCD_RXQLVL_IRQHandler(HCD_HandleTypeDef *hhcd);
  70. static void HCD_Port_IRQHandler(HCD_HandleTypeDef *hhcd);
  71. /**
  72. * @}
  73. */
  74. /* Exported functions --------------------------------------------------------*/
  75. /** @defgroup HCD_Exported_Functions HCD Exported Functions
  76. * @{
  77. */
  78. /** @defgroup HCD_Exported_Functions_Group1 Initialization and de-initialization functions
  79. * @brief Initialization and Configuration functions
  80. *
  81. @verbatim
  82. ===============================================================================
  83. ##### Initialization and de-initialization functions #####
  84. ===============================================================================
  85. [..] This section provides functions allowing to:
  86. @endverbatim
  87. * @{
  88. */
  89. /**
  90. * @brief Initialize the host driver.
  91. * @param hhcd HCD handle
  92. * @retval HAL status
  93. */
  94. HAL_StatusTypeDef HAL_HCD_Init(HCD_HandleTypeDef *hhcd)
  95. {
  96. USB_OTG_GlobalTypeDef *USBx;
  97. /* Check the HCD handle allocation */
  98. if (hhcd == NULL)
  99. {
  100. return HAL_ERROR;
  101. }
  102. /* Check the parameters */
  103. assert_param(IS_HCD_ALL_INSTANCE(hhcd->Instance));
  104. USBx = hhcd->Instance;
  105. if (hhcd->State == HAL_HCD_STATE_RESET)
  106. {
  107. /* Allocate lock resource and initialize it */
  108. hhcd->Lock = HAL_UNLOCKED;
  109. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  110. hhcd->SOFCallback = HAL_HCD_SOF_Callback;
  111. hhcd->ConnectCallback = HAL_HCD_Connect_Callback;
  112. hhcd->DisconnectCallback = HAL_HCD_Disconnect_Callback;
  113. hhcd->PortEnabledCallback = HAL_HCD_PortEnabled_Callback;
  114. hhcd->PortDisabledCallback = HAL_HCD_PortDisabled_Callback;
  115. hhcd->HC_NotifyURBChangeCallback = HAL_HCD_HC_NotifyURBChange_Callback;
  116. if (hhcd->MspInitCallback == NULL)
  117. {
  118. hhcd->MspInitCallback = HAL_HCD_MspInit;
  119. }
  120. /* Init the low level hardware */
  121. hhcd->MspInitCallback(hhcd);
  122. #else
  123. /* Init the low level hardware : GPIO, CLOCK, NVIC... */
  124. HAL_HCD_MspInit(hhcd);
  125. #endif /* (USE_HAL_HCD_REGISTER_CALLBACKS) */
  126. }
  127. hhcd->State = HAL_HCD_STATE_BUSY;
  128. /* Disable DMA mode for FS instance */
  129. if ((USBx->CID & (0x1U << 8)) == 0U)
  130. {
  131. hhcd->Init.dma_enable = 0U;
  132. }
  133. /* Disable the Interrupts */
  134. __HAL_HCD_DISABLE(hhcd);
  135. /* Init the Core (common init.) */
  136. (void)USB_CoreInit(hhcd->Instance, hhcd->Init);
  137. /* Force Host Mode*/
  138. (void)USB_SetCurrentMode(hhcd->Instance, USB_HOST_MODE);
  139. /* Init Host */
  140. (void)USB_HostInit(hhcd->Instance, hhcd->Init);
  141. hhcd->State = HAL_HCD_STATE_READY;
  142. return HAL_OK;
  143. }
  144. /**
  145. * @brief Initialize a host channel.
  146. * @param hhcd HCD handle
  147. * @param ch_num Channel number.
  148. * This parameter can be a value from 1 to 15
  149. * @param epnum Endpoint number.
  150. * This parameter can be a value from 1 to 15
  151. * @param dev_address Current device address
  152. * This parameter can be a value from 0 to 255
  153. * @param speed Current device speed.
  154. * This parameter can be one of these values:
  155. * HCD_DEVICE_SPEED_HIGH: High speed mode,
  156. * HCD_DEVICE_SPEED_FULL: Full speed mode,
  157. * HCD_DEVICE_SPEED_LOW: Low speed mode
  158. * @param ep_type Endpoint Type.
  159. * This parameter can be one of these values:
  160. * EP_TYPE_CTRL: Control type,
  161. * EP_TYPE_ISOC: Isochronous type,
  162. * EP_TYPE_BULK: Bulk type,
  163. * EP_TYPE_INTR: Interrupt type
  164. * @param mps Max Packet Size.
  165. * This parameter can be a value from 0 to32K
  166. * @retval HAL status
  167. */
  168. HAL_StatusTypeDef HAL_HCD_HC_Init(HCD_HandleTypeDef *hhcd,
  169. uint8_t ch_num,
  170. uint8_t epnum,
  171. uint8_t dev_address,
  172. uint8_t speed,
  173. uint8_t ep_type,
  174. uint16_t mps)
  175. {
  176. HAL_StatusTypeDef status;
  177. __HAL_LOCK(hhcd);
  178. hhcd->hc[ch_num].do_ping = 0U;
  179. hhcd->hc[ch_num].dev_addr = dev_address;
  180. hhcd->hc[ch_num].max_packet = mps;
  181. hhcd->hc[ch_num].ch_num = ch_num;
  182. hhcd->hc[ch_num].ep_type = ep_type;
  183. hhcd->hc[ch_num].ep_num = epnum & 0x7FU;
  184. if ((epnum & 0x80U) == 0x80U)
  185. {
  186. hhcd->hc[ch_num].ep_is_in = 1U;
  187. }
  188. else
  189. {
  190. hhcd->hc[ch_num].ep_is_in = 0U;
  191. }
  192. hhcd->hc[ch_num].speed = speed;
  193. status = USB_HC_Init(hhcd->Instance,
  194. ch_num,
  195. epnum,
  196. dev_address,
  197. speed,
  198. ep_type,
  199. mps);
  200. __HAL_UNLOCK(hhcd);
  201. return status;
  202. }
  203. /**
  204. * @brief Halt a host channel.
  205. * @param hhcd HCD handle
  206. * @param ch_num Channel number.
  207. * This parameter can be a value from 1 to 15
  208. * @retval HAL status
  209. */
  210. HAL_StatusTypeDef HAL_HCD_HC_Halt(HCD_HandleTypeDef *hhcd, uint8_t ch_num)
  211. {
  212. HAL_StatusTypeDef status = HAL_OK;
  213. __HAL_LOCK(hhcd);
  214. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  215. __HAL_UNLOCK(hhcd);
  216. return status;
  217. }
  218. /**
  219. * @brief DeInitialize the host driver.
  220. * @param hhcd HCD handle
  221. * @retval HAL status
  222. */
  223. HAL_StatusTypeDef HAL_HCD_DeInit(HCD_HandleTypeDef *hhcd)
  224. {
  225. /* Check the HCD handle allocation */
  226. if (hhcd == NULL)
  227. {
  228. return HAL_ERROR;
  229. }
  230. hhcd->State = HAL_HCD_STATE_BUSY;
  231. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  232. if (hhcd->MspDeInitCallback == NULL)
  233. {
  234. hhcd->MspDeInitCallback = HAL_HCD_MspDeInit; /* Legacy weak MspDeInit */
  235. }
  236. /* DeInit the low level hardware */
  237. hhcd->MspDeInitCallback(hhcd);
  238. #else
  239. /* DeInit the low level hardware: CLOCK, NVIC.*/
  240. HAL_HCD_MspDeInit(hhcd);
  241. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  242. __HAL_HCD_DISABLE(hhcd);
  243. hhcd->State = HAL_HCD_STATE_RESET;
  244. return HAL_OK;
  245. }
  246. /**
  247. * @brief Initialize the HCD MSP.
  248. * @param hhcd HCD handle
  249. * @retval None
  250. */
  251. __weak void HAL_HCD_MspInit(HCD_HandleTypeDef *hhcd)
  252. {
  253. /* Prevent unused argument(s) compilation warning */
  254. UNUSED(hhcd);
  255. /* NOTE : This function should not be modified, when the callback is needed,
  256. the HAL_HCD_MspInit could be implemented in the user file
  257. */
  258. }
  259. /**
  260. * @brief DeInitialize the HCD MSP.
  261. * @param hhcd HCD handle
  262. * @retval None
  263. */
  264. __weak void HAL_HCD_MspDeInit(HCD_HandleTypeDef *hhcd)
  265. {
  266. /* Prevent unused argument(s) compilation warning */
  267. UNUSED(hhcd);
  268. /* NOTE : This function should not be modified, when the callback is needed,
  269. the HAL_HCD_MspDeInit could be implemented in the user file
  270. */
  271. }
  272. /**
  273. * @}
  274. */
  275. /** @defgroup HCD_Exported_Functions_Group2 Input and Output operation functions
  276. * @brief HCD IO operation functions
  277. *
  278. @verbatim
  279. ===============================================================================
  280. ##### IO operation functions #####
  281. ===============================================================================
  282. [..] This subsection provides a set of functions allowing to manage the USB Host Data
  283. Transfer
  284. @endverbatim
  285. * @{
  286. */
  287. /**
  288. * @brief Submit a new URB for processing.
  289. * @param hhcd HCD handle
  290. * @param ch_num Channel number.
  291. * This parameter can be a value from 1 to 15
  292. * @param direction Channel number.
  293. * This parameter can be one of these values:
  294. * 0 : Output / 1 : Input
  295. * @param ep_type Endpoint Type.
  296. * This parameter can be one of these values:
  297. * EP_TYPE_CTRL: Control type/
  298. * EP_TYPE_ISOC: Isochronous type/
  299. * EP_TYPE_BULK: Bulk type/
  300. * EP_TYPE_INTR: Interrupt type/
  301. * @param token Endpoint Type.
  302. * This parameter can be one of these values:
  303. * 0: HC_PID_SETUP / 1: HC_PID_DATA1
  304. * @param pbuff pointer to URB data
  305. * @param length Length of URB data
  306. * @param do_ping activate do ping protocol (for high speed only).
  307. * This parameter can be one of these values:
  308. * 0 : do ping inactive / 1 : do ping active
  309. * @retval HAL status
  310. */
  311. HAL_StatusTypeDef HAL_HCD_HC_SubmitRequest(HCD_HandleTypeDef *hhcd,
  312. uint8_t ch_num,
  313. uint8_t direction,
  314. uint8_t ep_type,
  315. uint8_t token,
  316. uint8_t *pbuff,
  317. uint16_t length,
  318. uint8_t do_ping)
  319. {
  320. hhcd->hc[ch_num].ep_is_in = direction;
  321. hhcd->hc[ch_num].ep_type = ep_type;
  322. if (token == 0U)
  323. {
  324. hhcd->hc[ch_num].data_pid = HC_PID_SETUP;
  325. hhcd->hc[ch_num].do_ping = do_ping;
  326. }
  327. else
  328. {
  329. hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
  330. }
  331. /* Manage Data Toggle */
  332. switch (ep_type)
  333. {
  334. case EP_TYPE_CTRL:
  335. if ((token == 1U) && (direction == 0U)) /*send data */
  336. {
  337. if (length == 0U)
  338. {
  339. /* For Status OUT stage, Length==0, Status Out PID = 1 */
  340. hhcd->hc[ch_num].toggle_out = 1U;
  341. }
  342. /* Set the Data Toggle bit as per the Flag */
  343. if (hhcd->hc[ch_num].toggle_out == 0U)
  344. {
  345. /* Put the PID 0 */
  346. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  347. }
  348. else
  349. {
  350. /* Put the PID 1 */
  351. hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
  352. }
  353. }
  354. break;
  355. case EP_TYPE_BULK:
  356. if (direction == 0U)
  357. {
  358. /* Set the Data Toggle bit as per the Flag */
  359. if (hhcd->hc[ch_num].toggle_out == 0U)
  360. {
  361. /* Put the PID 0 */
  362. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  363. }
  364. else
  365. {
  366. /* Put the PID 1 */
  367. hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
  368. }
  369. }
  370. else
  371. {
  372. if (hhcd->hc[ch_num].toggle_in == 0U)
  373. {
  374. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  375. }
  376. else
  377. {
  378. hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
  379. }
  380. }
  381. break;
  382. case EP_TYPE_INTR:
  383. if (direction == 0U)
  384. {
  385. /* Set the Data Toggle bit as per the Flag */
  386. if (hhcd->hc[ch_num].toggle_out == 0U)
  387. {
  388. /* Put the PID 0 */
  389. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  390. }
  391. else
  392. {
  393. /* Put the PID 1 */
  394. hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
  395. }
  396. }
  397. else
  398. {
  399. if (hhcd->hc[ch_num].toggle_in == 0U)
  400. {
  401. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  402. }
  403. else
  404. {
  405. hhcd->hc[ch_num].data_pid = HC_PID_DATA1;
  406. }
  407. }
  408. break;
  409. case EP_TYPE_ISOC:
  410. hhcd->hc[ch_num].data_pid = HC_PID_DATA0;
  411. break;
  412. default:
  413. break;
  414. }
  415. hhcd->hc[ch_num].xfer_buff = pbuff;
  416. hhcd->hc[ch_num].xfer_len = length;
  417. hhcd->hc[ch_num].urb_state = URB_IDLE;
  418. hhcd->hc[ch_num].xfer_count = 0U;
  419. hhcd->hc[ch_num].ch_num = ch_num;
  420. hhcd->hc[ch_num].state = HC_IDLE;
  421. return USB_HC_StartXfer(hhcd->Instance, &hhcd->hc[ch_num], (uint8_t)hhcd->Init.dma_enable);
  422. }
  423. /**
  424. * @brief Handle HCD interrupt request.
  425. * @param hhcd HCD handle
  426. * @retval None
  427. */
  428. void HAL_HCD_IRQHandler(HCD_HandleTypeDef *hhcd)
  429. {
  430. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  431. uint32_t USBx_BASE = (uint32_t)USBx;
  432. uint32_t i;
  433. uint32_t interrupt;
  434. /* Ensure that we are in device mode */
  435. if (USB_GetMode(hhcd->Instance) == USB_OTG_MODE_HOST)
  436. {
  437. /* Avoid spurious interrupt */
  438. if (__HAL_HCD_IS_INVALID_INTERRUPT(hhcd))
  439. {
  440. return;
  441. }
  442. if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT))
  443. {
  444. /* Incorrect mode, acknowledge the interrupt */
  445. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_PXFR_INCOMPISOOUT);
  446. }
  447. if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_IISOIXFR))
  448. {
  449. /* Incorrect mode, acknowledge the interrupt */
  450. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_IISOIXFR);
  451. }
  452. if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_PTXFE))
  453. {
  454. /* Incorrect mode, acknowledge the interrupt */
  455. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_PTXFE);
  456. }
  457. if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_MMIS))
  458. {
  459. /* Incorrect mode, acknowledge the interrupt */
  460. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_MMIS);
  461. }
  462. /* Handle Host Disconnect Interrupts */
  463. if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_DISCINT))
  464. {
  465. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_DISCINT);
  466. if ((USBx_HPRT0 & USB_OTG_HPRT_PCSTS) == 0U)
  467. {
  468. /* Flush USB Fifo */
  469. (void)USB_FlushTxFifo(USBx, 0x10U);
  470. (void)USB_FlushRxFifo(USBx);
  471. /* Restore FS Clock */
  472. (void)USB_InitFSLSPClkSel(hhcd->Instance, HCFG_48_MHZ);
  473. /* Handle Host Port Disconnect Interrupt */
  474. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  475. hhcd->DisconnectCallback(hhcd);
  476. #else
  477. HAL_HCD_Disconnect_Callback(hhcd);
  478. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  479. }
  480. }
  481. /* Handle Host Port Interrupts */
  482. if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_HPRTINT))
  483. {
  484. HCD_Port_IRQHandler(hhcd);
  485. }
  486. /* Handle Host SOF Interrupt */
  487. if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_SOF))
  488. {
  489. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  490. hhcd->SOFCallback(hhcd);
  491. #else
  492. HAL_HCD_SOF_Callback(hhcd);
  493. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  494. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_SOF);
  495. }
  496. /* Handle Rx Queue Level Interrupts */
  497. if ((__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_RXFLVL)) != 0U)
  498. {
  499. USB_MASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  500. HCD_RXQLVL_IRQHandler(hhcd);
  501. USB_UNMASK_INTERRUPT(hhcd->Instance, USB_OTG_GINTSTS_RXFLVL);
  502. }
  503. /* Handle Host channel Interrupt */
  504. if (__HAL_HCD_GET_FLAG(hhcd, USB_OTG_GINTSTS_HCINT))
  505. {
  506. interrupt = USB_HC_ReadInterrupt(hhcd->Instance);
  507. for (i = 0U; i < hhcd->Init.Host_channels; i++)
  508. {
  509. if ((interrupt & (1UL << (i & 0xFU))) != 0U)
  510. {
  511. if ((USBx_HC(i)->HCCHAR & USB_OTG_HCCHAR_EPDIR) == USB_OTG_HCCHAR_EPDIR)
  512. {
  513. HCD_HC_IN_IRQHandler(hhcd, (uint8_t)i);
  514. }
  515. else
  516. {
  517. HCD_HC_OUT_IRQHandler(hhcd, (uint8_t)i);
  518. }
  519. }
  520. }
  521. __HAL_HCD_CLEAR_FLAG(hhcd, USB_OTG_GINTSTS_HCINT);
  522. }
  523. }
  524. }
  525. /**
  526. * @brief Handles HCD Wakeup interrupt request.
  527. * @param hhcd HCD handle
  528. * @retval HAL status
  529. */
  530. void HAL_HCD_WKUP_IRQHandler(HCD_HandleTypeDef *hhcd)
  531. {
  532. UNUSED(hhcd);
  533. }
  534. /**
  535. * @brief SOF callback.
  536. * @param hhcd HCD handle
  537. * @retval None
  538. */
  539. __weak void HAL_HCD_SOF_Callback(HCD_HandleTypeDef *hhcd)
  540. {
  541. /* Prevent unused argument(s) compilation warning */
  542. UNUSED(hhcd);
  543. /* NOTE : This function should not be modified, when the callback is needed,
  544. the HAL_HCD_SOF_Callback could be implemented in the user file
  545. */
  546. }
  547. /**
  548. * @brief Connection Event callback.
  549. * @param hhcd HCD handle
  550. * @retval None
  551. */
  552. __weak void HAL_HCD_Connect_Callback(HCD_HandleTypeDef *hhcd)
  553. {
  554. /* Prevent unused argument(s) compilation warning */
  555. UNUSED(hhcd);
  556. /* NOTE : This function should not be modified, when the callback is needed,
  557. the HAL_HCD_Connect_Callback could be implemented in the user file
  558. */
  559. }
  560. /**
  561. * @brief Disconnection Event callback.
  562. * @param hhcd HCD handle
  563. * @retval None
  564. */
  565. __weak void HAL_HCD_Disconnect_Callback(HCD_HandleTypeDef *hhcd)
  566. {
  567. /* Prevent unused argument(s) compilation warning */
  568. UNUSED(hhcd);
  569. /* NOTE : This function should not be modified, when the callback is needed,
  570. the HAL_HCD_Disconnect_Callback could be implemented in the user file
  571. */
  572. }
  573. /**
  574. * @brief Port Enabled Event callback.
  575. * @param hhcd HCD handle
  576. * @retval None
  577. */
  578. __weak void HAL_HCD_PortEnabled_Callback(HCD_HandleTypeDef *hhcd)
  579. {
  580. /* Prevent unused argument(s) compilation warning */
  581. UNUSED(hhcd);
  582. /* NOTE : This function should not be modified, when the callback is needed,
  583. the HAL_HCD_Disconnect_Callback could be implemented in the user file
  584. */
  585. }
  586. /**
  587. * @brief Port Disabled Event callback.
  588. * @param hhcd HCD handle
  589. * @retval None
  590. */
  591. __weak void HAL_HCD_PortDisabled_Callback(HCD_HandleTypeDef *hhcd)
  592. {
  593. /* Prevent unused argument(s) compilation warning */
  594. UNUSED(hhcd);
  595. /* NOTE : This function should not be modified, when the callback is needed,
  596. the HAL_HCD_Disconnect_Callback could be implemented in the user file
  597. */
  598. }
  599. /**
  600. * @brief Notify URB state change callback.
  601. * @param hhcd HCD handle
  602. * @param chnum Channel number.
  603. * This parameter can be a value from 1 to 15
  604. * @param urb_state:
  605. * This parameter can be one of these values:
  606. * URB_IDLE/
  607. * URB_DONE/
  608. * URB_NOTREADY/
  609. * URB_NYET/
  610. * URB_ERROR/
  611. * URB_STALL/
  612. * @retval None
  613. */
  614. __weak void HAL_HCD_HC_NotifyURBChange_Callback(HCD_HandleTypeDef *hhcd, uint8_t chnum, HCD_URBStateTypeDef urb_state)
  615. {
  616. /* Prevent unused argument(s) compilation warning */
  617. UNUSED(hhcd);
  618. UNUSED(chnum);
  619. UNUSED(urb_state);
  620. /* NOTE : This function should not be modified, when the callback is needed,
  621. the HAL_HCD_HC_NotifyURBChange_Callback could be implemented in the user file
  622. */
  623. }
  624. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  625. /**
  626. * @brief Register a User USB HCD Callback
  627. * To be used instead of the weak predefined callback
  628. * @param hhcd USB HCD handle
  629. * @param CallbackID ID of the callback to be registered
  630. * This parameter can be one of the following values:
  631. * @arg @ref HAL_HCD_SOF_CB_ID USB HCD SOF callback ID
  632. * @arg @ref HAL_HCD_CONNECT_CB_ID USB HCD Connect callback ID
  633. * @arg @ref HAL_HCD_DISCONNECT_CB_ID OTG HCD Disconnect callback ID
  634. * @arg @ref HAL_HCD_PORT_ENABLED_CB_ID USB HCD Port Enable callback ID
  635. * @arg @ref HAL_HCD_PORT_DISABLED_CB_ID USB HCD Port Disable callback ID
  636. * @arg @ref HAL_HCD_MSPINIT_CB_ID MspDeInit callback ID
  637. * @arg @ref HAL_HCD_MSPDEINIT_CB_ID MspDeInit callback ID
  638. * @param pCallback pointer to the Callback function
  639. * @retval HAL status
  640. */
  641. HAL_StatusTypeDef HAL_HCD_RegisterCallback(HCD_HandleTypeDef *hhcd,
  642. HAL_HCD_CallbackIDTypeDef CallbackID,
  643. pHCD_CallbackTypeDef pCallback)
  644. {
  645. HAL_StatusTypeDef status = HAL_OK;
  646. if (pCallback == NULL)
  647. {
  648. /* Update the error code */
  649. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  650. return HAL_ERROR;
  651. }
  652. /* Process locked */
  653. __HAL_LOCK(hhcd);
  654. if (hhcd->State == HAL_HCD_STATE_READY)
  655. {
  656. switch (CallbackID)
  657. {
  658. case HAL_HCD_SOF_CB_ID :
  659. hhcd->SOFCallback = pCallback;
  660. break;
  661. case HAL_HCD_CONNECT_CB_ID :
  662. hhcd->ConnectCallback = pCallback;
  663. break;
  664. case HAL_HCD_DISCONNECT_CB_ID :
  665. hhcd->DisconnectCallback = pCallback;
  666. break;
  667. case HAL_HCD_PORT_ENABLED_CB_ID :
  668. hhcd->PortEnabledCallback = pCallback;
  669. break;
  670. case HAL_HCD_PORT_DISABLED_CB_ID :
  671. hhcd->PortDisabledCallback = pCallback;
  672. break;
  673. case HAL_HCD_MSPINIT_CB_ID :
  674. hhcd->MspInitCallback = pCallback;
  675. break;
  676. case HAL_HCD_MSPDEINIT_CB_ID :
  677. hhcd->MspDeInitCallback = pCallback;
  678. break;
  679. default :
  680. /* Update the error code */
  681. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  682. /* Return error status */
  683. status = HAL_ERROR;
  684. break;
  685. }
  686. }
  687. else if (hhcd->State == HAL_HCD_STATE_RESET)
  688. {
  689. switch (CallbackID)
  690. {
  691. case HAL_HCD_MSPINIT_CB_ID :
  692. hhcd->MspInitCallback = pCallback;
  693. break;
  694. case HAL_HCD_MSPDEINIT_CB_ID :
  695. hhcd->MspDeInitCallback = pCallback;
  696. break;
  697. default :
  698. /* Update the error code */
  699. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  700. /* Return error status */
  701. status = HAL_ERROR;
  702. break;
  703. }
  704. }
  705. else
  706. {
  707. /* Update the error code */
  708. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  709. /* Return error status */
  710. status = HAL_ERROR;
  711. }
  712. /* Release Lock */
  713. __HAL_UNLOCK(hhcd);
  714. return status;
  715. }
  716. /**
  717. * @brief Unregister an USB HCD Callback
  718. * USB HCD callback is redirected to the weak predefined callback
  719. * @param hhcd USB HCD handle
  720. * @param CallbackID ID of the callback to be unregistered
  721. * This parameter can be one of the following values:
  722. * @arg @ref HAL_HCD_SOF_CB_ID USB HCD SOF callback ID
  723. * @arg @ref HAL_HCD_CONNECT_CB_ID USB HCD Connect callback ID
  724. * @arg @ref HAL_HCD_DISCONNECT_CB_ID OTG HCD Disconnect callback ID
  725. * @arg @ref HAL_HCD_PORT_ENABLED_CB_ID USB HCD Port Enabled callback ID
  726. * @arg @ref HAL_HCD_PORT_DISABLED_CB_ID USB HCD Port Disabled callback ID
  727. * @arg @ref HAL_HCD_MSPINIT_CB_ID MspDeInit callback ID
  728. * @arg @ref HAL_HCD_MSPDEINIT_CB_ID MspDeInit callback ID
  729. * @retval HAL status
  730. */
  731. HAL_StatusTypeDef HAL_HCD_UnRegisterCallback(HCD_HandleTypeDef *hhcd, HAL_HCD_CallbackIDTypeDef CallbackID)
  732. {
  733. HAL_StatusTypeDef status = HAL_OK;
  734. /* Process locked */
  735. __HAL_LOCK(hhcd);
  736. /* Setup Legacy weak Callbacks */
  737. if (hhcd->State == HAL_HCD_STATE_READY)
  738. {
  739. switch (CallbackID)
  740. {
  741. case HAL_HCD_SOF_CB_ID :
  742. hhcd->SOFCallback = HAL_HCD_SOF_Callback;
  743. break;
  744. case HAL_HCD_CONNECT_CB_ID :
  745. hhcd->ConnectCallback = HAL_HCD_Connect_Callback;
  746. break;
  747. case HAL_HCD_DISCONNECT_CB_ID :
  748. hhcd->DisconnectCallback = HAL_HCD_Disconnect_Callback;
  749. break;
  750. case HAL_HCD_PORT_ENABLED_CB_ID :
  751. hhcd->PortEnabledCallback = HAL_HCD_PortEnabled_Callback;
  752. break;
  753. case HAL_HCD_PORT_DISABLED_CB_ID :
  754. hhcd->PortDisabledCallback = HAL_HCD_PortDisabled_Callback;
  755. break;
  756. case HAL_HCD_MSPINIT_CB_ID :
  757. hhcd->MspInitCallback = HAL_HCD_MspInit;
  758. break;
  759. case HAL_HCD_MSPDEINIT_CB_ID :
  760. hhcd->MspDeInitCallback = HAL_HCD_MspDeInit;
  761. break;
  762. default :
  763. /* Update the error code */
  764. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  765. /* Return error status */
  766. status = HAL_ERROR;
  767. break;
  768. }
  769. }
  770. else if (hhcd->State == HAL_HCD_STATE_RESET)
  771. {
  772. switch (CallbackID)
  773. {
  774. case HAL_HCD_MSPINIT_CB_ID :
  775. hhcd->MspInitCallback = HAL_HCD_MspInit;
  776. break;
  777. case HAL_HCD_MSPDEINIT_CB_ID :
  778. hhcd->MspDeInitCallback = HAL_HCD_MspDeInit;
  779. break;
  780. default :
  781. /* Update the error code */
  782. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  783. /* Return error status */
  784. status = HAL_ERROR;
  785. break;
  786. }
  787. }
  788. else
  789. {
  790. /* Update the error code */
  791. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  792. /* Return error status */
  793. status = HAL_ERROR;
  794. }
  795. /* Release Lock */
  796. __HAL_UNLOCK(hhcd);
  797. return status;
  798. }
  799. /**
  800. * @brief Register USB HCD Host Channel Notify URB Change Callback
  801. * To be used instead of the weak HAL_HCD_HC_NotifyURBChange_Callback() predefined callback
  802. * @param hhcd HCD handle
  803. * @param pCallback pointer to the USB HCD Host Channel Notify URB Change Callback function
  804. * @retval HAL status
  805. */
  806. HAL_StatusTypeDef HAL_HCD_RegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef *hhcd,
  807. pHCD_HC_NotifyURBChangeCallbackTypeDef pCallback)
  808. {
  809. HAL_StatusTypeDef status = HAL_OK;
  810. if (pCallback == NULL)
  811. {
  812. /* Update the error code */
  813. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  814. return HAL_ERROR;
  815. }
  816. /* Process locked */
  817. __HAL_LOCK(hhcd);
  818. if (hhcd->State == HAL_HCD_STATE_READY)
  819. {
  820. hhcd->HC_NotifyURBChangeCallback = pCallback;
  821. }
  822. else
  823. {
  824. /* Update the error code */
  825. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  826. /* Return error status */
  827. status = HAL_ERROR;
  828. }
  829. /* Release Lock */
  830. __HAL_UNLOCK(hhcd);
  831. return status;
  832. }
  833. /**
  834. * @brief Unregister the USB HCD Host Channel Notify URB Change Callback
  835. * USB HCD Host Channel Notify URB Change Callback is redirected
  836. * to the weak HAL_HCD_HC_NotifyURBChange_Callback() predefined callback
  837. * @param hhcd HCD handle
  838. * @retval HAL status
  839. */
  840. HAL_StatusTypeDef HAL_HCD_UnRegisterHC_NotifyURBChangeCallback(HCD_HandleTypeDef *hhcd)
  841. {
  842. HAL_StatusTypeDef status = HAL_OK;
  843. /* Process locked */
  844. __HAL_LOCK(hhcd);
  845. if (hhcd->State == HAL_HCD_STATE_READY)
  846. {
  847. hhcd->HC_NotifyURBChangeCallback = HAL_HCD_HC_NotifyURBChange_Callback; /* Legacy weak DataOutStageCallback */
  848. }
  849. else
  850. {
  851. /* Update the error code */
  852. hhcd->ErrorCode |= HAL_HCD_ERROR_INVALID_CALLBACK;
  853. /* Return error status */
  854. status = HAL_ERROR;
  855. }
  856. /* Release Lock */
  857. __HAL_UNLOCK(hhcd);
  858. return status;
  859. }
  860. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  861. /**
  862. * @}
  863. */
  864. /** @defgroup HCD_Exported_Functions_Group3 Peripheral Control functions
  865. * @brief Management functions
  866. *
  867. @verbatim
  868. ===============================================================================
  869. ##### Peripheral Control functions #####
  870. ===============================================================================
  871. [..]
  872. This subsection provides a set of functions allowing to control the HCD data
  873. transfers.
  874. @endverbatim
  875. * @{
  876. */
  877. /**
  878. * @brief Start the host driver.
  879. * @param hhcd HCD handle
  880. * @retval HAL status
  881. */
  882. HAL_StatusTypeDef HAL_HCD_Start(HCD_HandleTypeDef *hhcd)
  883. {
  884. __HAL_LOCK(hhcd);
  885. /* Enable port power */
  886. (void)USB_DriveVbus(hhcd->Instance, 1U);
  887. /* Enable global interrupt */
  888. __HAL_HCD_ENABLE(hhcd);
  889. __HAL_UNLOCK(hhcd);
  890. return HAL_OK;
  891. }
  892. /**
  893. * @brief Stop the host driver.
  894. * @param hhcd HCD handle
  895. * @retval HAL status
  896. */
  897. HAL_StatusTypeDef HAL_HCD_Stop(HCD_HandleTypeDef *hhcd)
  898. {
  899. __HAL_LOCK(hhcd);
  900. (void)USB_StopHost(hhcd->Instance);
  901. __HAL_UNLOCK(hhcd);
  902. return HAL_OK;
  903. }
  904. /**
  905. * @brief Reset the host port.
  906. * @param hhcd HCD handle
  907. * @retval HAL status
  908. */
  909. HAL_StatusTypeDef HAL_HCD_ResetPort(HCD_HandleTypeDef *hhcd)
  910. {
  911. return (USB_ResetPort(hhcd->Instance));
  912. }
  913. /**
  914. * @}
  915. */
  916. /** @defgroup HCD_Exported_Functions_Group4 Peripheral State functions
  917. * @brief Peripheral State functions
  918. *
  919. @verbatim
  920. ===============================================================================
  921. ##### Peripheral State functions #####
  922. ===============================================================================
  923. [..]
  924. This subsection permits to get in run-time the status of the peripheral
  925. and the data flow.
  926. @endverbatim
  927. * @{
  928. */
  929. /**
  930. * @brief Return the HCD handle state.
  931. * @param hhcd HCD handle
  932. * @retval HAL state
  933. */
  934. HCD_StateTypeDef HAL_HCD_GetState(HCD_HandleTypeDef *hhcd)
  935. {
  936. return hhcd->State;
  937. }
  938. /**
  939. * @brief Return URB state for a channel.
  940. * @param hhcd HCD handle
  941. * @param chnum Channel number.
  942. * This parameter can be a value from 1 to 15
  943. * @retval URB state.
  944. * This parameter can be one of these values:
  945. * URB_IDLE/
  946. * URB_DONE/
  947. * URB_NOTREADY/
  948. * URB_NYET/
  949. * URB_ERROR/
  950. * URB_STALL
  951. */
  952. HCD_URBStateTypeDef HAL_HCD_HC_GetURBState(HCD_HandleTypeDef *hhcd, uint8_t chnum)
  953. {
  954. return hhcd->hc[chnum].urb_state;
  955. }
  956. /**
  957. * @brief Return the last host transfer size.
  958. * @param hhcd HCD handle
  959. * @param chnum Channel number.
  960. * This parameter can be a value from 1 to 15
  961. * @retval last transfer size in byte
  962. */
  963. uint32_t HAL_HCD_HC_GetXferCount(HCD_HandleTypeDef *hhcd, uint8_t chnum)
  964. {
  965. return hhcd->hc[chnum].xfer_count;
  966. }
  967. /**
  968. * @brief Return the Host Channel state.
  969. * @param hhcd HCD handle
  970. * @param chnum Channel number.
  971. * This parameter can be a value from 1 to 15
  972. * @retval Host channel state
  973. * This parameter can be one of these values:
  974. * HC_IDLE/
  975. * HC_XFRC/
  976. * HC_HALTED/
  977. * HC_NYET/
  978. * HC_NAK/
  979. * HC_STALL/
  980. * HC_XACTERR/
  981. * HC_BBLERR/
  982. * HC_DATATGLERR
  983. */
  984. HCD_HCStateTypeDef HAL_HCD_HC_GetState(HCD_HandleTypeDef *hhcd, uint8_t chnum)
  985. {
  986. return hhcd->hc[chnum].state;
  987. }
  988. /**
  989. * @brief Return the current Host frame number.
  990. * @param hhcd HCD handle
  991. * @retval Current Host frame number
  992. */
  993. uint32_t HAL_HCD_GetCurrentFrame(HCD_HandleTypeDef *hhcd)
  994. {
  995. return (USB_GetCurrentFrame(hhcd->Instance));
  996. }
  997. /**
  998. * @brief Return the Host enumeration speed.
  999. * @param hhcd HCD handle
  1000. * @retval Enumeration speed
  1001. */
  1002. uint32_t HAL_HCD_GetCurrentSpeed(HCD_HandleTypeDef *hhcd)
  1003. {
  1004. return (USB_GetHostSpeed(hhcd->Instance));
  1005. }
  1006. /**
  1007. * @}
  1008. */
  1009. /**
  1010. * @}
  1011. */
  1012. /** @addtogroup HCD_Private_Functions
  1013. * @{
  1014. */
  1015. /**
  1016. * @brief Handle Host Channel IN interrupt requests.
  1017. * @param hhcd HCD handle
  1018. * @param chnum Channel number.
  1019. * This parameter can be a value from 1 to 15
  1020. * @retval none
  1021. */
  1022. static void HCD_HC_IN_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum)
  1023. {
  1024. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  1025. uint32_t USBx_BASE = (uint32_t)USBx;
  1026. uint32_t ch_num = (uint32_t)chnum;
  1027. uint32_t tmpreg;
  1028. if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_AHBERR) == USB_OTG_HCINT_AHBERR)
  1029. {
  1030. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_AHBERR);
  1031. hhcd->hc[ch_num].state = HC_XACTERR;
  1032. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1033. }
  1034. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_BBERR) == USB_OTG_HCINT_BBERR)
  1035. {
  1036. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_BBERR);
  1037. hhcd->hc[ch_num].state = HC_BBLERR;
  1038. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1039. }
  1040. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_ACK) == USB_OTG_HCINT_ACK)
  1041. {
  1042. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_ACK);
  1043. }
  1044. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_STALL) == USB_OTG_HCINT_STALL)
  1045. {
  1046. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_STALL);
  1047. hhcd->hc[ch_num].state = HC_STALL;
  1048. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1049. }
  1050. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_DTERR) == USB_OTG_HCINT_DTERR)
  1051. {
  1052. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_DTERR);
  1053. hhcd->hc[ch_num].state = HC_DATATGLERR;
  1054. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1055. }
  1056. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_TXERR) == USB_OTG_HCINT_TXERR)
  1057. {
  1058. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_TXERR);
  1059. hhcd->hc[ch_num].state = HC_XACTERR;
  1060. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1061. }
  1062. else
  1063. {
  1064. /* ... */
  1065. }
  1066. if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_FRMOR) == USB_OTG_HCINT_FRMOR)
  1067. {
  1068. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1069. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_FRMOR);
  1070. }
  1071. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_XFRC) == USB_OTG_HCINT_XFRC)
  1072. {
  1073. if (hhcd->Init.dma_enable != 0U)
  1074. {
  1075. hhcd->hc[ch_num].xfer_count = hhcd->hc[ch_num].XferSize - \
  1076. (USBx_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_XFRSIZ);
  1077. }
  1078. hhcd->hc[ch_num].state = HC_XFRC;
  1079. hhcd->hc[ch_num].ErrCnt = 0U;
  1080. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_XFRC);
  1081. if ((hhcd->hc[ch_num].ep_type == EP_TYPE_CTRL) ||
  1082. (hhcd->hc[ch_num].ep_type == EP_TYPE_BULK))
  1083. {
  1084. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1085. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_NAK);
  1086. }
  1087. else if ((hhcd->hc[ch_num].ep_type == EP_TYPE_INTR) ||
  1088. (hhcd->hc[ch_num].ep_type == EP_TYPE_ISOC))
  1089. {
  1090. USBx_HC(ch_num)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM;
  1091. hhcd->hc[ch_num].urb_state = URB_DONE;
  1092. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1093. hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
  1094. #else
  1095. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
  1096. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1097. }
  1098. else
  1099. {
  1100. /* ... */
  1101. }
  1102. if (hhcd->Init.dma_enable == 1U)
  1103. {
  1104. if (((hhcd->hc[ch_num].XferSize / hhcd->hc[ch_num].max_packet) & 1U) != 0U)
  1105. {
  1106. hhcd->hc[ch_num].toggle_in ^= 1U;
  1107. }
  1108. }
  1109. else
  1110. {
  1111. hhcd->hc[ch_num].toggle_in ^= 1U;
  1112. }
  1113. }
  1114. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_CHH) == USB_OTG_HCINT_CHH)
  1115. {
  1116. if (hhcd->hc[ch_num].state == HC_XFRC)
  1117. {
  1118. hhcd->hc[ch_num].urb_state = URB_DONE;
  1119. }
  1120. else if (hhcd->hc[ch_num].state == HC_STALL)
  1121. {
  1122. hhcd->hc[ch_num].urb_state = URB_STALL;
  1123. }
  1124. else if ((hhcd->hc[ch_num].state == HC_XACTERR) ||
  1125. (hhcd->hc[ch_num].state == HC_DATATGLERR))
  1126. {
  1127. hhcd->hc[ch_num].ErrCnt++;
  1128. if (hhcd->hc[ch_num].ErrCnt > 2U)
  1129. {
  1130. hhcd->hc[ch_num].ErrCnt = 0U;
  1131. hhcd->hc[ch_num].urb_state = URB_ERROR;
  1132. }
  1133. else
  1134. {
  1135. hhcd->hc[ch_num].urb_state = URB_NOTREADY;
  1136. /* re-activate the channel */
  1137. tmpreg = USBx_HC(ch_num)->HCCHAR;
  1138. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  1139. tmpreg |= USB_OTG_HCCHAR_CHENA;
  1140. USBx_HC(ch_num)->HCCHAR = tmpreg;
  1141. }
  1142. }
  1143. else if (hhcd->hc[ch_num].state == HC_NAK)
  1144. {
  1145. hhcd->hc[ch_num].urb_state = URB_NOTREADY;
  1146. /* re-activate the channel */
  1147. tmpreg = USBx_HC(ch_num)->HCCHAR;
  1148. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  1149. tmpreg |= USB_OTG_HCCHAR_CHENA;
  1150. USBx_HC(ch_num)->HCCHAR = tmpreg;
  1151. }
  1152. else if (hhcd->hc[ch_num].state == HC_BBLERR)
  1153. {
  1154. hhcd->hc[ch_num].ErrCnt++;
  1155. hhcd->hc[ch_num].urb_state = URB_ERROR;
  1156. }
  1157. else
  1158. {
  1159. /* ... */
  1160. }
  1161. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_CHH);
  1162. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1163. hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
  1164. #else
  1165. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
  1166. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1167. }
  1168. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_NAK) == USB_OTG_HCINT_NAK)
  1169. {
  1170. if (hhcd->hc[ch_num].ep_type == EP_TYPE_INTR)
  1171. {
  1172. hhcd->hc[ch_num].ErrCnt = 0U;
  1173. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1174. }
  1175. else if ((hhcd->hc[ch_num].ep_type == EP_TYPE_CTRL) ||
  1176. (hhcd->hc[ch_num].ep_type == EP_TYPE_BULK))
  1177. {
  1178. hhcd->hc[ch_num].ErrCnt = 0U;
  1179. if (hhcd->Init.dma_enable == 0U)
  1180. {
  1181. hhcd->hc[ch_num].state = HC_NAK;
  1182. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1183. }
  1184. }
  1185. else
  1186. {
  1187. /* ... */
  1188. }
  1189. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_NAK);
  1190. }
  1191. else
  1192. {
  1193. /* ... */
  1194. }
  1195. }
  1196. /**
  1197. * @brief Handle Host Channel OUT interrupt requests.
  1198. * @param hhcd HCD handle
  1199. * @param chnum Channel number.
  1200. * This parameter can be a value from 1 to 15
  1201. * @retval none
  1202. */
  1203. static void HCD_HC_OUT_IRQHandler(HCD_HandleTypeDef *hhcd, uint8_t chnum)
  1204. {
  1205. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  1206. uint32_t USBx_BASE = (uint32_t)USBx;
  1207. uint32_t ch_num = (uint32_t)chnum;
  1208. uint32_t tmpreg;
  1209. uint32_t num_packets;
  1210. if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_AHBERR) == USB_OTG_HCINT_AHBERR)
  1211. {
  1212. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_AHBERR);
  1213. hhcd->hc[ch_num].state = HC_XACTERR;
  1214. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1215. }
  1216. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_ACK) == USB_OTG_HCINT_ACK)
  1217. {
  1218. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_ACK);
  1219. if (hhcd->hc[ch_num].do_ping == 1U)
  1220. {
  1221. hhcd->hc[ch_num].do_ping = 0U;
  1222. hhcd->hc[ch_num].urb_state = URB_NOTREADY;
  1223. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1224. }
  1225. }
  1226. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_FRMOR) == USB_OTG_HCINT_FRMOR)
  1227. {
  1228. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_FRMOR);
  1229. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1230. }
  1231. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_XFRC) == USB_OTG_HCINT_XFRC)
  1232. {
  1233. hhcd->hc[ch_num].ErrCnt = 0U;
  1234. /* transaction completed with NYET state, update do ping state */
  1235. if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_NYET) == USB_OTG_HCINT_NYET)
  1236. {
  1237. hhcd->hc[ch_num].do_ping = 1U;
  1238. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_NYET);
  1239. }
  1240. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_XFRC);
  1241. hhcd->hc[ch_num].state = HC_XFRC;
  1242. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1243. }
  1244. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_NYET) == USB_OTG_HCINT_NYET)
  1245. {
  1246. hhcd->hc[ch_num].state = HC_NYET;
  1247. hhcd->hc[ch_num].do_ping = 1U;
  1248. hhcd->hc[ch_num].ErrCnt = 0U;
  1249. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1250. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_NYET);
  1251. }
  1252. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_STALL) == USB_OTG_HCINT_STALL)
  1253. {
  1254. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_STALL);
  1255. hhcd->hc[ch_num].state = HC_STALL;
  1256. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1257. }
  1258. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_NAK) == USB_OTG_HCINT_NAK)
  1259. {
  1260. hhcd->hc[ch_num].ErrCnt = 0U;
  1261. hhcd->hc[ch_num].state = HC_NAK;
  1262. if (hhcd->hc[ch_num].do_ping == 0U)
  1263. {
  1264. if (hhcd->hc[ch_num].speed == HCD_DEVICE_SPEED_HIGH)
  1265. {
  1266. hhcd->hc[ch_num].do_ping = 1U;
  1267. }
  1268. }
  1269. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1270. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_NAK);
  1271. }
  1272. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_TXERR) == USB_OTG_HCINT_TXERR)
  1273. {
  1274. if (hhcd->Init.dma_enable == 0U)
  1275. {
  1276. hhcd->hc[ch_num].state = HC_XACTERR;
  1277. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1278. }
  1279. else
  1280. {
  1281. hhcd->hc[ch_num].ErrCnt++;
  1282. if (hhcd->hc[ch_num].ErrCnt > 2U)
  1283. {
  1284. hhcd->hc[ch_num].ErrCnt = 0U;
  1285. hhcd->hc[ch_num].urb_state = URB_ERROR;
  1286. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1287. hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
  1288. #else
  1289. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
  1290. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1291. }
  1292. else
  1293. {
  1294. hhcd->hc[ch_num].urb_state = URB_NOTREADY;
  1295. }
  1296. }
  1297. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_TXERR);
  1298. }
  1299. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_DTERR) == USB_OTG_HCINT_DTERR)
  1300. {
  1301. hhcd->hc[ch_num].state = HC_DATATGLERR;
  1302. (void)USB_HC_Halt(hhcd->Instance, (uint8_t)ch_num);
  1303. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_DTERR);
  1304. }
  1305. else if ((USBx_HC(ch_num)->HCINT & USB_OTG_HCINT_CHH) == USB_OTG_HCINT_CHH)
  1306. {
  1307. if (hhcd->hc[ch_num].state == HC_XFRC)
  1308. {
  1309. hhcd->hc[ch_num].urb_state = URB_DONE;
  1310. if ((hhcd->hc[ch_num].ep_type == EP_TYPE_BULK) ||
  1311. (hhcd->hc[ch_num].ep_type == EP_TYPE_INTR))
  1312. {
  1313. if (hhcd->Init.dma_enable == 0U)
  1314. {
  1315. hhcd->hc[ch_num].toggle_out ^= 1U;
  1316. }
  1317. if ((hhcd->Init.dma_enable == 1U) && (hhcd->hc[ch_num].xfer_len > 0U))
  1318. {
  1319. num_packets = (hhcd->hc[ch_num].xfer_len + hhcd->hc[ch_num].max_packet - 1U) / hhcd->hc[ch_num].max_packet;
  1320. if ((num_packets & 1U) != 0U)
  1321. {
  1322. hhcd->hc[ch_num].toggle_out ^= 1U;
  1323. }
  1324. }
  1325. }
  1326. }
  1327. else if (hhcd->hc[ch_num].state == HC_NAK)
  1328. {
  1329. hhcd->hc[ch_num].urb_state = URB_NOTREADY;
  1330. }
  1331. else if (hhcd->hc[ch_num].state == HC_NYET)
  1332. {
  1333. hhcd->hc[ch_num].urb_state = URB_NOTREADY;
  1334. }
  1335. else if (hhcd->hc[ch_num].state == HC_STALL)
  1336. {
  1337. hhcd->hc[ch_num].urb_state = URB_STALL;
  1338. }
  1339. else if ((hhcd->hc[ch_num].state == HC_XACTERR) ||
  1340. (hhcd->hc[ch_num].state == HC_DATATGLERR))
  1341. {
  1342. hhcd->hc[ch_num].ErrCnt++;
  1343. if (hhcd->hc[ch_num].ErrCnt > 2U)
  1344. {
  1345. hhcd->hc[ch_num].ErrCnt = 0U;
  1346. hhcd->hc[ch_num].urb_state = URB_ERROR;
  1347. }
  1348. else
  1349. {
  1350. hhcd->hc[ch_num].urb_state = URB_NOTREADY;
  1351. /* re-activate the channel */
  1352. tmpreg = USBx_HC(ch_num)->HCCHAR;
  1353. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  1354. tmpreg |= USB_OTG_HCCHAR_CHENA;
  1355. USBx_HC(ch_num)->HCCHAR = tmpreg;
  1356. }
  1357. }
  1358. else
  1359. {
  1360. /* ... */
  1361. }
  1362. __HAL_HCD_CLEAR_HC_INT(ch_num, USB_OTG_HCINT_CHH);
  1363. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1364. hhcd->HC_NotifyURBChangeCallback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
  1365. #else
  1366. HAL_HCD_HC_NotifyURBChange_Callback(hhcd, (uint8_t)ch_num, hhcd->hc[ch_num].urb_state);
  1367. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1368. }
  1369. else
  1370. {
  1371. /* ... */
  1372. }
  1373. }
  1374. /**
  1375. * @brief Handle Rx Queue Level interrupt requests.
  1376. * @param hhcd HCD handle
  1377. * @retval none
  1378. */
  1379. static void HCD_RXQLVL_IRQHandler(HCD_HandleTypeDef *hhcd)
  1380. {
  1381. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  1382. uint32_t USBx_BASE = (uint32_t)USBx;
  1383. uint32_t pktsts;
  1384. uint32_t pktcnt;
  1385. uint32_t GrxstspReg;
  1386. uint32_t xferSizePktCnt;
  1387. uint32_t tmpreg;
  1388. uint32_t ch_num;
  1389. GrxstspReg = hhcd->Instance->GRXSTSP;
  1390. ch_num = GrxstspReg & USB_OTG_GRXSTSP_EPNUM;
  1391. pktsts = (GrxstspReg & USB_OTG_GRXSTSP_PKTSTS) >> 17;
  1392. pktcnt = (GrxstspReg & USB_OTG_GRXSTSP_BCNT) >> 4;
  1393. switch (pktsts)
  1394. {
  1395. case GRXSTS_PKTSTS_IN:
  1396. /* Read the data into the host buffer. */
  1397. if ((pktcnt > 0U) && (hhcd->hc[ch_num].xfer_buff != (void *)0))
  1398. {
  1399. if ((hhcd->hc[ch_num].xfer_count + pktcnt) <= hhcd->hc[ch_num].xfer_len)
  1400. {
  1401. (void)USB_ReadPacket(hhcd->Instance,
  1402. hhcd->hc[ch_num].xfer_buff, (uint16_t)pktcnt);
  1403. /* manage multiple Xfer */
  1404. hhcd->hc[ch_num].xfer_buff += pktcnt;
  1405. hhcd->hc[ch_num].xfer_count += pktcnt;
  1406. /* get transfer size packet count */
  1407. xferSizePktCnt = (USBx_HC(ch_num)->HCTSIZ & USB_OTG_HCTSIZ_PKTCNT) >> 19;
  1408. if ((hhcd->hc[ch_num].max_packet == pktcnt) && (xferSizePktCnt > 0U))
  1409. {
  1410. /* re-activate the channel when more packets are expected */
  1411. tmpreg = USBx_HC(ch_num)->HCCHAR;
  1412. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  1413. tmpreg |= USB_OTG_HCCHAR_CHENA;
  1414. USBx_HC(ch_num)->HCCHAR = tmpreg;
  1415. hhcd->hc[ch_num].toggle_in ^= 1U;
  1416. }
  1417. }
  1418. else
  1419. {
  1420. hhcd->hc[ch_num].urb_state = URB_ERROR;
  1421. }
  1422. }
  1423. break;
  1424. case GRXSTS_PKTSTS_DATA_TOGGLE_ERR:
  1425. break;
  1426. case GRXSTS_PKTSTS_IN_XFER_COMP:
  1427. case GRXSTS_PKTSTS_CH_HALTED:
  1428. default:
  1429. break;
  1430. }
  1431. }
  1432. /**
  1433. * @brief Handle Host Port interrupt requests.
  1434. * @param hhcd HCD handle
  1435. * @retval None
  1436. */
  1437. static void HCD_Port_IRQHandler(HCD_HandleTypeDef *hhcd)
  1438. {
  1439. USB_OTG_GlobalTypeDef *USBx = hhcd->Instance;
  1440. uint32_t USBx_BASE = (uint32_t)USBx;
  1441. __IO uint32_t hprt0;
  1442. __IO uint32_t hprt0_dup;
  1443. /* Handle Host Port Interrupts */
  1444. hprt0 = USBx_HPRT0;
  1445. hprt0_dup = USBx_HPRT0;
  1446. hprt0_dup &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET | \
  1447. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
  1448. /* Check whether Port Connect detected */
  1449. if ((hprt0 & USB_OTG_HPRT_PCDET) == USB_OTG_HPRT_PCDET)
  1450. {
  1451. if ((hprt0 & USB_OTG_HPRT_PCSTS) == USB_OTG_HPRT_PCSTS)
  1452. {
  1453. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1454. hhcd->ConnectCallback(hhcd);
  1455. #else
  1456. HAL_HCD_Connect_Callback(hhcd);
  1457. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1458. }
  1459. hprt0_dup |= USB_OTG_HPRT_PCDET;
  1460. }
  1461. /* Check whether Port Enable Changed */
  1462. if ((hprt0 & USB_OTG_HPRT_PENCHNG) == USB_OTG_HPRT_PENCHNG)
  1463. {
  1464. hprt0_dup |= USB_OTG_HPRT_PENCHNG;
  1465. if ((hprt0 & USB_OTG_HPRT_PENA) == USB_OTG_HPRT_PENA)
  1466. {
  1467. if (hhcd->Init.phy_itface == USB_OTG_EMBEDDED_PHY)
  1468. {
  1469. if ((hprt0 & USB_OTG_HPRT_PSPD) == (HPRT0_PRTSPD_LOW_SPEED << 17))
  1470. {
  1471. (void)USB_InitFSLSPClkSel(hhcd->Instance, HCFG_6_MHZ);
  1472. }
  1473. else
  1474. {
  1475. (void)USB_InitFSLSPClkSel(hhcd->Instance, HCFG_48_MHZ);
  1476. }
  1477. }
  1478. else
  1479. {
  1480. if (hhcd->Init.speed == HCD_SPEED_FULL)
  1481. {
  1482. USBx_HOST->HFIR = 60000U;
  1483. }
  1484. }
  1485. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1486. hhcd->PortEnabledCallback(hhcd);
  1487. #else
  1488. HAL_HCD_PortEnabled_Callback(hhcd);
  1489. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1490. }
  1491. else
  1492. {
  1493. #if (USE_HAL_HCD_REGISTER_CALLBACKS == 1U)
  1494. hhcd->PortDisabledCallback(hhcd);
  1495. #else
  1496. HAL_HCD_PortDisabled_Callback(hhcd);
  1497. #endif /* USE_HAL_HCD_REGISTER_CALLBACKS */
  1498. }
  1499. }
  1500. /* Check for an overcurrent */
  1501. if ((hprt0 & USB_OTG_HPRT_POCCHNG) == USB_OTG_HPRT_POCCHNG)
  1502. {
  1503. hprt0_dup |= USB_OTG_HPRT_POCCHNG;
  1504. }
  1505. /* Clear Port Interrupts */
  1506. USBx_HPRT0 = hprt0_dup;
  1507. }
  1508. /**
  1509. * @}
  1510. */
  1511. /**
  1512. * @}
  1513. */
  1514. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  1515. #endif /* HAL_HCD_MODULE_ENABLED */
  1516. /**
  1517. * @}
  1518. */
  1519. /**
  1520. * @}
  1521. */