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.
 
 
 
 
 

2324 rivejä
63 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_ll_usb.c
  4. * @author MCD Application Team
  5. * @brief USB Low Layer HAL module driver.
  6. *
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the USB Peripheral Controller:
  9. * + Initialization/de-initialization functions
  10. * + I/O operation functions
  11. * + Peripheral Control functions
  12. * + Peripheral State functions
  13. *
  14. ******************************************************************************
  15. * @attention
  16. *
  17. * Copyright (c) 2016 STMicroelectronics.
  18. * All rights reserved.
  19. *
  20. * This software is licensed under terms that can be found in the LICENSE file
  21. * in the root directory of this software component.
  22. * If no LICENSE file comes with this software, it is provided AS-IS.
  23. *
  24. ******************************************************************************
  25. @verbatim
  26. ==============================================================================
  27. ##### How to use this driver #####
  28. ==============================================================================
  29. [..]
  30. (#) Fill parameters of Init structure in USB_CfgTypeDef structure.
  31. (#) Call USB_CoreInit() API to initialize the USB Core peripheral.
  32. (#) The upper HAL HCD/PCD driver will call the right routines for its internal processes.
  33. @endverbatim
  34. ******************************************************************************
  35. */
  36. /* Includes ------------------------------------------------------------------*/
  37. #include "stm32f4xx_hal.h"
  38. /** @addtogroup STM32F4xx_LL_USB_DRIVER
  39. * @{
  40. */
  41. #if defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED)
  42. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  43. /* Private typedef -----------------------------------------------------------*/
  44. /* Private define ------------------------------------------------------------*/
  45. /* Private macro -------------------------------------------------------------*/
  46. /* Private variables ---------------------------------------------------------*/
  47. /* Private function prototypes -----------------------------------------------*/
  48. /* Private functions ---------------------------------------------------------*/
  49. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  50. static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx);
  51. /* Exported functions --------------------------------------------------------*/
  52. /** @defgroup USB_LL_Exported_Functions USB Low Layer Exported Functions
  53. * @{
  54. */
  55. /** @defgroup USB_LL_Exported_Functions_Group1 Initialization/de-initialization functions
  56. * @brief Initialization and Configuration functions
  57. *
  58. @verbatim
  59. ===============================================================================
  60. ##### Initialization/de-initialization functions #####
  61. ===============================================================================
  62. @endverbatim
  63. * @{
  64. */
  65. /**
  66. * @brief Initializes the USB Core
  67. * @param USBx USB Instance
  68. * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
  69. * the configuration information for the specified USBx peripheral.
  70. * @retval HAL status
  71. */
  72. HAL_StatusTypeDef USB_CoreInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
  73. {
  74. HAL_StatusTypeDef ret;
  75. if (cfg.phy_itface == USB_OTG_ULPI_PHY)
  76. {
  77. USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
  78. /* Init The ULPI Interface */
  79. USBx->GUSBCFG &= ~(USB_OTG_GUSBCFG_TSDPS | USB_OTG_GUSBCFG_ULPIFSLS | USB_OTG_GUSBCFG_PHYSEL);
  80. /* Select vbus source */
  81. USBx->GUSBCFG &= ~(USB_OTG_GUSBCFG_ULPIEVBUSD | USB_OTG_GUSBCFG_ULPIEVBUSI);
  82. if (cfg.use_external_vbus == 1U)
  83. {
  84. USBx->GUSBCFG |= USB_OTG_GUSBCFG_ULPIEVBUSD;
  85. }
  86. /* Reset after a PHY select */
  87. ret = USB_CoreReset(USBx);
  88. }
  89. else /* FS interface (embedded Phy) */
  90. {
  91. /* Select FS Embedded PHY */
  92. USBx->GUSBCFG |= USB_OTG_GUSBCFG_PHYSEL;
  93. /* Reset after a PHY select */
  94. ret = USB_CoreReset(USBx);
  95. if (cfg.battery_charging_enable == 0U)
  96. {
  97. /* Activate the USB Transceiver */
  98. USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
  99. }
  100. else
  101. {
  102. /* Deactivate the USB Transceiver */
  103. USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
  104. }
  105. }
  106. if (cfg.dma_enable == 1U)
  107. {
  108. USBx->GAHBCFG |= USB_OTG_GAHBCFG_HBSTLEN_2;
  109. USBx->GAHBCFG |= USB_OTG_GAHBCFG_DMAEN;
  110. }
  111. return ret;
  112. }
  113. /**
  114. * @brief Set the USB turnaround time
  115. * @param USBx USB Instance
  116. * @param hclk: AHB clock frequency
  117. * @retval USB turnaround time In PHY Clocks number
  118. */
  119. HAL_StatusTypeDef USB_SetTurnaroundTime(USB_OTG_GlobalTypeDef *USBx,
  120. uint32_t hclk, uint8_t speed)
  121. {
  122. uint32_t UsbTrd;
  123. /* The USBTRD is configured according to the tables below, depending on AHB frequency
  124. used by application. In the low AHB frequency range it is used to stretch enough the USB response
  125. time to IN tokens, the USB turnaround time, so to compensate for the longer AHB read access
  126. latency to the Data FIFO */
  127. if (speed == USBD_FS_SPEED)
  128. {
  129. if ((hclk >= 14200000U) && (hclk < 15000000U))
  130. {
  131. /* hclk Clock Range between 14.2-15 MHz */
  132. UsbTrd = 0xFU;
  133. }
  134. else if ((hclk >= 15000000U) && (hclk < 16000000U))
  135. {
  136. /* hclk Clock Range between 15-16 MHz */
  137. UsbTrd = 0xEU;
  138. }
  139. else if ((hclk >= 16000000U) && (hclk < 17200000U))
  140. {
  141. /* hclk Clock Range between 16-17.2 MHz */
  142. UsbTrd = 0xDU;
  143. }
  144. else if ((hclk >= 17200000U) && (hclk < 18500000U))
  145. {
  146. /* hclk Clock Range between 17.2-18.5 MHz */
  147. UsbTrd = 0xCU;
  148. }
  149. else if ((hclk >= 18500000U) && (hclk < 20000000U))
  150. {
  151. /* hclk Clock Range between 18.5-20 MHz */
  152. UsbTrd = 0xBU;
  153. }
  154. else if ((hclk >= 20000000U) && (hclk < 21800000U))
  155. {
  156. /* hclk Clock Range between 20-21.8 MHz */
  157. UsbTrd = 0xAU;
  158. }
  159. else if ((hclk >= 21800000U) && (hclk < 24000000U))
  160. {
  161. /* hclk Clock Range between 21.8-24 MHz */
  162. UsbTrd = 0x9U;
  163. }
  164. else if ((hclk >= 24000000U) && (hclk < 27700000U))
  165. {
  166. /* hclk Clock Range between 24-27.7 MHz */
  167. UsbTrd = 0x8U;
  168. }
  169. else if ((hclk >= 27700000U) && (hclk < 32000000U))
  170. {
  171. /* hclk Clock Range between 27.7-32 MHz */
  172. UsbTrd = 0x7U;
  173. }
  174. else /* if(hclk >= 32000000) */
  175. {
  176. /* hclk Clock Range between 32-200 MHz */
  177. UsbTrd = 0x6U;
  178. }
  179. }
  180. else if (speed == USBD_HS_SPEED)
  181. {
  182. UsbTrd = USBD_HS_TRDT_VALUE;
  183. }
  184. else
  185. {
  186. UsbTrd = USBD_DEFAULT_TRDT_VALUE;
  187. }
  188. USBx->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
  189. USBx->GUSBCFG |= (uint32_t)((UsbTrd << 10) & USB_OTG_GUSBCFG_TRDT);
  190. return HAL_OK;
  191. }
  192. /**
  193. * @brief USB_EnableGlobalInt
  194. * Enables the controller's Global Int in the AHB Config reg
  195. * @param USBx Selected device
  196. * @retval HAL status
  197. */
  198. HAL_StatusTypeDef USB_EnableGlobalInt(USB_OTG_GlobalTypeDef *USBx)
  199. {
  200. USBx->GAHBCFG |= USB_OTG_GAHBCFG_GINT;
  201. return HAL_OK;
  202. }
  203. /**
  204. * @brief USB_DisableGlobalInt
  205. * Disable the controller's Global Int in the AHB Config reg
  206. * @param USBx Selected device
  207. * @retval HAL status
  208. */
  209. HAL_StatusTypeDef USB_DisableGlobalInt(USB_OTG_GlobalTypeDef *USBx)
  210. {
  211. USBx->GAHBCFG &= ~USB_OTG_GAHBCFG_GINT;
  212. return HAL_OK;
  213. }
  214. /**
  215. * @brief USB_SetCurrentMode Set functional mode
  216. * @param USBx Selected device
  217. * @param mode current core mode
  218. * This parameter can be one of these values:
  219. * @arg USB_DEVICE_MODE Peripheral mode
  220. * @arg USB_HOST_MODE Host mode
  221. * @retval HAL status
  222. */
  223. HAL_StatusTypeDef USB_SetCurrentMode(USB_OTG_GlobalTypeDef *USBx, USB_OTG_ModeTypeDef mode)
  224. {
  225. uint32_t ms = 0U;
  226. USBx->GUSBCFG &= ~(USB_OTG_GUSBCFG_FHMOD | USB_OTG_GUSBCFG_FDMOD);
  227. if (mode == USB_HOST_MODE)
  228. {
  229. USBx->GUSBCFG |= USB_OTG_GUSBCFG_FHMOD;
  230. do
  231. {
  232. HAL_Delay(10U);
  233. ms += 10U;
  234. } while ((USB_GetMode(USBx) != (uint32_t)USB_HOST_MODE) && (ms < HAL_USB_CURRENT_MODE_MAX_DELAY_MS));
  235. }
  236. else if (mode == USB_DEVICE_MODE)
  237. {
  238. USBx->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD;
  239. do
  240. {
  241. HAL_Delay(10U);
  242. ms += 10U;
  243. } while ((USB_GetMode(USBx) != (uint32_t)USB_DEVICE_MODE) && (ms < HAL_USB_CURRENT_MODE_MAX_DELAY_MS));
  244. }
  245. else
  246. {
  247. return HAL_ERROR;
  248. }
  249. if (ms == HAL_USB_CURRENT_MODE_MAX_DELAY_MS)
  250. {
  251. return HAL_ERROR;
  252. }
  253. return HAL_OK;
  254. }
  255. /**
  256. * @brief USB_DevInit Initializes the USB_OTG controller registers
  257. * for device mode
  258. * @param USBx Selected device
  259. * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
  260. * the configuration information for the specified USBx peripheral.
  261. * @retval HAL status
  262. */
  263. HAL_StatusTypeDef USB_DevInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
  264. {
  265. HAL_StatusTypeDef ret = HAL_OK;
  266. uint32_t USBx_BASE = (uint32_t)USBx;
  267. uint32_t i;
  268. for (i = 0U; i < 15U; i++)
  269. {
  270. USBx->DIEPTXF[i] = 0U;
  271. }
  272. #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) \
  273. || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) \
  274. || defined(STM32F423xx)
  275. /* VBUS Sensing setup */
  276. if (cfg.vbus_sensing_enable == 0U)
  277. {
  278. USBx_DEVICE->DCTL |= USB_OTG_DCTL_SDIS;
  279. /* Deactivate VBUS Sensing B */
  280. USBx->GCCFG &= ~USB_OTG_GCCFG_VBDEN;
  281. /* B-peripheral session valid override enable */
  282. USBx->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN;
  283. USBx->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL;
  284. }
  285. else
  286. {
  287. /* Enable HW VBUS sensing */
  288. USBx->GCCFG |= USB_OTG_GCCFG_VBDEN;
  289. }
  290. #else
  291. /* VBUS Sensing setup */
  292. if (cfg.vbus_sensing_enable == 0U)
  293. {
  294. /*
  295. * Disable HW VBUS sensing. VBUS is internally considered to be always
  296. * at VBUS-Valid level (5V).
  297. */
  298. USBx_DEVICE->DCTL |= USB_OTG_DCTL_SDIS;
  299. USBx->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS;
  300. USBx->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN;
  301. USBx->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN;
  302. }
  303. else
  304. {
  305. /* Enable HW VBUS sensing */
  306. USBx->GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS;
  307. USBx->GCCFG |= USB_OTG_GCCFG_VBUSBSEN;
  308. }
  309. #endif /* defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) ||
  310. defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) ||
  311. defined(STM32F423xx) */
  312. /* Restart the Phy Clock */
  313. USBx_PCGCCTL = 0U;
  314. if (cfg.phy_itface == USB_OTG_ULPI_PHY)
  315. {
  316. if (cfg.speed == USBD_HS_SPEED)
  317. {
  318. /* Set Core speed to High speed mode */
  319. (void)USB_SetDevSpeed(USBx, USB_OTG_SPEED_HIGH);
  320. }
  321. else
  322. {
  323. /* Set Core speed to Full speed mode */
  324. (void)USB_SetDevSpeed(USBx, USB_OTG_SPEED_HIGH_IN_FULL);
  325. }
  326. }
  327. else
  328. {
  329. /* Set Core speed to Full speed mode */
  330. (void)USB_SetDevSpeed(USBx, USB_OTG_SPEED_FULL);
  331. }
  332. /* Flush the FIFOs */
  333. if (USB_FlushTxFifo(USBx, 0x10U) != HAL_OK) /* all Tx FIFOs */
  334. {
  335. ret = HAL_ERROR;
  336. }
  337. if (USB_FlushRxFifo(USBx) != HAL_OK)
  338. {
  339. ret = HAL_ERROR;
  340. }
  341. /* Clear all pending Device Interrupts */
  342. USBx_DEVICE->DIEPMSK = 0U;
  343. USBx_DEVICE->DOEPMSK = 0U;
  344. USBx_DEVICE->DAINTMSK = 0U;
  345. for (i = 0U; i < cfg.dev_endpoints; i++)
  346. {
  347. if ((USBx_INEP(i)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
  348. {
  349. if (i == 0U)
  350. {
  351. USBx_INEP(i)->DIEPCTL = USB_OTG_DIEPCTL_SNAK;
  352. }
  353. else
  354. {
  355. USBx_INEP(i)->DIEPCTL = USB_OTG_DIEPCTL_EPDIS | USB_OTG_DIEPCTL_SNAK;
  356. }
  357. }
  358. else
  359. {
  360. USBx_INEP(i)->DIEPCTL = 0U;
  361. }
  362. USBx_INEP(i)->DIEPTSIZ = 0U;
  363. USBx_INEP(i)->DIEPINT = 0xFB7FU;
  364. }
  365. for (i = 0U; i < cfg.dev_endpoints; i++)
  366. {
  367. if ((USBx_OUTEP(i)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
  368. {
  369. if (i == 0U)
  370. {
  371. USBx_OUTEP(i)->DOEPCTL = USB_OTG_DOEPCTL_SNAK;
  372. }
  373. else
  374. {
  375. USBx_OUTEP(i)->DOEPCTL = USB_OTG_DOEPCTL_EPDIS | USB_OTG_DOEPCTL_SNAK;
  376. }
  377. }
  378. else
  379. {
  380. USBx_OUTEP(i)->DOEPCTL = 0U;
  381. }
  382. USBx_OUTEP(i)->DOEPTSIZ = 0U;
  383. USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
  384. }
  385. USBx_DEVICE->DIEPMSK &= ~(USB_OTG_DIEPMSK_TXFURM);
  386. /* Disable all interrupts. */
  387. USBx->GINTMSK = 0U;
  388. /* Clear any pending interrupts */
  389. USBx->GINTSTS = 0xBFFFFFFFU;
  390. /* Enable the common interrupts */
  391. if (cfg.dma_enable == 0U)
  392. {
  393. USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
  394. }
  395. /* Enable interrupts matching to the Device mode ONLY */
  396. USBx->GINTMSK |= USB_OTG_GINTMSK_USBSUSPM | USB_OTG_GINTMSK_USBRST |
  397. USB_OTG_GINTMSK_ENUMDNEM | USB_OTG_GINTMSK_IEPINT |
  398. USB_OTG_GINTMSK_OEPINT | USB_OTG_GINTMSK_IISOIXFRM |
  399. USB_OTG_GINTMSK_PXFRM_IISOOXFRM | USB_OTG_GINTMSK_WUIM;
  400. if (cfg.Sof_enable != 0U)
  401. {
  402. USBx->GINTMSK |= USB_OTG_GINTMSK_SOFM;
  403. }
  404. if (cfg.vbus_sensing_enable == 1U)
  405. {
  406. USBx->GINTMSK |= (USB_OTG_GINTMSK_SRQIM | USB_OTG_GINTMSK_OTGINT);
  407. }
  408. return ret;
  409. }
  410. /**
  411. * @brief USB_FlushTxFifo Flush a Tx FIFO
  412. * @param USBx Selected device
  413. * @param num FIFO number
  414. * This parameter can be a value from 1 to 15
  415. 15 means Flush all Tx FIFOs
  416. * @retval HAL status
  417. */
  418. HAL_StatusTypeDef USB_FlushTxFifo(USB_OTG_GlobalTypeDef *USBx, uint32_t num)
  419. {
  420. __IO uint32_t count = 0U;
  421. /* Wait for AHB master IDLE state. */
  422. do
  423. {
  424. count++;
  425. if (count > HAL_USB_TIMEOUT)
  426. {
  427. return HAL_TIMEOUT;
  428. }
  429. } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
  430. /* Flush TX Fifo */
  431. count = 0U;
  432. USBx->GRSTCTL = (USB_OTG_GRSTCTL_TXFFLSH | (num << 6));
  433. do
  434. {
  435. count++;
  436. if (count > HAL_USB_TIMEOUT)
  437. {
  438. return HAL_TIMEOUT;
  439. }
  440. } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_TXFFLSH) == USB_OTG_GRSTCTL_TXFFLSH);
  441. return HAL_OK;
  442. }
  443. /**
  444. * @brief USB_FlushRxFifo Flush Rx FIFO
  445. * @param USBx Selected device
  446. * @retval HAL status
  447. */
  448. HAL_StatusTypeDef USB_FlushRxFifo(USB_OTG_GlobalTypeDef *USBx)
  449. {
  450. __IO uint32_t count = 0U;
  451. /* Wait for AHB master IDLE state. */
  452. do
  453. {
  454. count++;
  455. if (count > HAL_USB_TIMEOUT)
  456. {
  457. return HAL_TIMEOUT;
  458. }
  459. } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
  460. /* Flush RX Fifo */
  461. count = 0U;
  462. USBx->GRSTCTL = USB_OTG_GRSTCTL_RXFFLSH;
  463. do
  464. {
  465. count++;
  466. if (count > HAL_USB_TIMEOUT)
  467. {
  468. return HAL_TIMEOUT;
  469. }
  470. } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_RXFFLSH) == USB_OTG_GRSTCTL_RXFFLSH);
  471. return HAL_OK;
  472. }
  473. /**
  474. * @brief USB_SetDevSpeed Initializes the DevSpd field of DCFG register
  475. * depending the PHY type and the enumeration speed of the device.
  476. * @param USBx Selected device
  477. * @param speed device speed
  478. * This parameter can be one of these values:
  479. * @arg USB_OTG_SPEED_HIGH: High speed mode
  480. * @arg USB_OTG_SPEED_HIGH_IN_FULL: High speed core in Full Speed mode
  481. * @arg USB_OTG_SPEED_FULL: Full speed mode
  482. * @retval Hal status
  483. */
  484. HAL_StatusTypeDef USB_SetDevSpeed(const USB_OTG_GlobalTypeDef *USBx, uint8_t speed)
  485. {
  486. uint32_t USBx_BASE = (uint32_t)USBx;
  487. USBx_DEVICE->DCFG |= speed;
  488. return HAL_OK;
  489. }
  490. /**
  491. * @brief USB_GetDevSpeed Return the Dev Speed
  492. * @param USBx Selected device
  493. * @retval speed device speed
  494. * This parameter can be one of these values:
  495. * @arg USBD_HS_SPEED: High speed mode
  496. * @arg USBD_FS_SPEED: Full speed mode
  497. */
  498. uint8_t USB_GetDevSpeed(const USB_OTG_GlobalTypeDef *USBx)
  499. {
  500. uint32_t USBx_BASE = (uint32_t)USBx;
  501. uint8_t speed;
  502. uint32_t DevEnumSpeed = USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD;
  503. if (DevEnumSpeed == DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ)
  504. {
  505. speed = USBD_HS_SPEED;
  506. }
  507. else if ((DevEnumSpeed == DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ) ||
  508. (DevEnumSpeed == DSTS_ENUMSPD_FS_PHY_48MHZ))
  509. {
  510. speed = USBD_FS_SPEED;
  511. }
  512. else
  513. {
  514. speed = 0xFU;
  515. }
  516. return speed;
  517. }
  518. /**
  519. * @brief Activate and configure an endpoint
  520. * @param USBx Selected device
  521. * @param ep pointer to endpoint structure
  522. * @retval HAL status
  523. */
  524. HAL_StatusTypeDef USB_ActivateEndpoint(const USB_OTG_GlobalTypeDef *USBx, const USB_OTG_EPTypeDef *ep)
  525. {
  526. uint32_t USBx_BASE = (uint32_t)USBx;
  527. uint32_t epnum = (uint32_t)ep->num;
  528. if (ep->is_in == 1U)
  529. {
  530. USBx_DEVICE->DAINTMSK |= USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK));
  531. if ((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_USBAEP) == 0U)
  532. {
  533. USBx_INEP(epnum)->DIEPCTL |= (ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ) |
  534. ((uint32_t)ep->type << 18) | (epnum << 22) |
  535. USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  536. USB_OTG_DIEPCTL_USBAEP;
  537. }
  538. }
  539. else
  540. {
  541. USBx_DEVICE->DAINTMSK |= USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16);
  542. if (((USBx_OUTEP(epnum)->DOEPCTL) & USB_OTG_DOEPCTL_USBAEP) == 0U)
  543. {
  544. USBx_OUTEP(epnum)->DOEPCTL |= (ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ) |
  545. ((uint32_t)ep->type << 18) |
  546. USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  547. USB_OTG_DOEPCTL_USBAEP;
  548. }
  549. }
  550. return HAL_OK;
  551. }
  552. /**
  553. * @brief Activate and configure a dedicated endpoint
  554. * @param USBx Selected device
  555. * @param ep pointer to endpoint structure
  556. * @retval HAL status
  557. */
  558. HAL_StatusTypeDef USB_ActivateDedicatedEndpoint(const USB_OTG_GlobalTypeDef *USBx, const USB_OTG_EPTypeDef *ep)
  559. {
  560. uint32_t USBx_BASE = (uint32_t)USBx;
  561. uint32_t epnum = (uint32_t)ep->num;
  562. /* Read DEPCTLn register */
  563. if (ep->is_in == 1U)
  564. {
  565. if (((USBx_INEP(epnum)->DIEPCTL) & USB_OTG_DIEPCTL_USBAEP) == 0U)
  566. {
  567. USBx_INEP(epnum)->DIEPCTL |= (ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ) |
  568. ((uint32_t)ep->type << 18) | (epnum << 22) |
  569. USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  570. USB_OTG_DIEPCTL_USBAEP;
  571. }
  572. USBx_DEVICE->DEACHMSK |= USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK));
  573. }
  574. else
  575. {
  576. if (((USBx_OUTEP(epnum)->DOEPCTL) & USB_OTG_DOEPCTL_USBAEP) == 0U)
  577. {
  578. USBx_OUTEP(epnum)->DOEPCTL |= (ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ) |
  579. ((uint32_t)ep->type << 18) | (epnum << 22) |
  580. USB_OTG_DOEPCTL_USBAEP;
  581. }
  582. USBx_DEVICE->DEACHMSK |= USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16);
  583. }
  584. return HAL_OK;
  585. }
  586. /**
  587. * @brief De-activate and de-initialize an endpoint
  588. * @param USBx Selected device
  589. * @param ep pointer to endpoint structure
  590. * @retval HAL status
  591. */
  592. HAL_StatusTypeDef USB_DeactivateEndpoint(const USB_OTG_GlobalTypeDef *USBx, const USB_OTG_EPTypeDef *ep)
  593. {
  594. uint32_t USBx_BASE = (uint32_t)USBx;
  595. uint32_t epnum = (uint32_t)ep->num;
  596. /* Read DEPCTLn register */
  597. if (ep->is_in == 1U)
  598. {
  599. if ((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
  600. {
  601. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SNAK;
  602. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_EPDIS;
  603. }
  604. USBx_DEVICE->DEACHMSK &= ~(USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK)));
  605. USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK)));
  606. USBx_INEP(epnum)->DIEPCTL &= ~(USB_OTG_DIEPCTL_USBAEP |
  607. USB_OTG_DIEPCTL_MPSIZ |
  608. USB_OTG_DIEPCTL_TXFNUM |
  609. USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  610. USB_OTG_DIEPCTL_EPTYP);
  611. }
  612. else
  613. {
  614. if ((USBx_OUTEP(epnum)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
  615. {
  616. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
  617. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_EPDIS;
  618. }
  619. USBx_DEVICE->DEACHMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16));
  620. USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16));
  621. USBx_OUTEP(epnum)->DOEPCTL &= ~(USB_OTG_DOEPCTL_USBAEP |
  622. USB_OTG_DOEPCTL_MPSIZ |
  623. USB_OTG_DOEPCTL_SD0PID_SEVNFRM |
  624. USB_OTG_DOEPCTL_EPTYP);
  625. }
  626. return HAL_OK;
  627. }
  628. /**
  629. * @brief De-activate and de-initialize a dedicated endpoint
  630. * @param USBx Selected device
  631. * @param ep pointer to endpoint structure
  632. * @retval HAL status
  633. */
  634. HAL_StatusTypeDef USB_DeactivateDedicatedEndpoint(const USB_OTG_GlobalTypeDef *USBx, const USB_OTG_EPTypeDef *ep)
  635. {
  636. uint32_t USBx_BASE = (uint32_t)USBx;
  637. uint32_t epnum = (uint32_t)ep->num;
  638. /* Read DEPCTLn register */
  639. if (ep->is_in == 1U)
  640. {
  641. if ((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
  642. {
  643. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SNAK;
  644. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_EPDIS;
  645. }
  646. USBx_INEP(epnum)->DIEPCTL &= ~ USB_OTG_DIEPCTL_USBAEP;
  647. USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK)));
  648. }
  649. else
  650. {
  651. if ((USBx_OUTEP(epnum)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
  652. {
  653. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
  654. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_EPDIS;
  655. }
  656. USBx_OUTEP(epnum)->DOEPCTL &= ~USB_OTG_DOEPCTL_USBAEP;
  657. USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16));
  658. }
  659. return HAL_OK;
  660. }
  661. /**
  662. * @brief USB_EPStartXfer : setup and starts a transfer over an EP
  663. * @param USBx Selected device
  664. * @param ep pointer to endpoint structure
  665. * @param dma USB dma enabled or disabled
  666. * This parameter can be one of these values:
  667. * 0 : DMA feature not used
  668. * 1 : DMA feature used
  669. * @retval HAL status
  670. */
  671. HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep, uint8_t dma)
  672. {
  673. uint32_t USBx_BASE = (uint32_t)USBx;
  674. uint32_t epnum = (uint32_t)ep->num;
  675. uint16_t pktcnt;
  676. /* IN endpoint */
  677. if (ep->is_in == 1U)
  678. {
  679. /* Zero Length Packet? */
  680. if (ep->xfer_len == 0U)
  681. {
  682. USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
  683. USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1U << 19));
  684. USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
  685. }
  686. else
  687. {
  688. /* Program the transfer size and packet count
  689. * as follows: xfersize = N * maxpacket +
  690. * short_packet pktcnt = N + (short_packet
  691. * exist ? 1 : 0)
  692. */
  693. USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
  694. USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
  695. if (epnum == 0U)
  696. {
  697. if (ep->xfer_len > ep->maxpacket)
  698. {
  699. ep->xfer_len = ep->maxpacket;
  700. }
  701. USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1U << 19));
  702. }
  703. else
  704. {
  705. pktcnt = (uint16_t)((ep->xfer_len + ep->maxpacket - 1U) / ep->maxpacket);
  706. USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & ((uint32_t)pktcnt << 19));
  707. if (ep->type == EP_TYPE_ISOC)
  708. {
  709. USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_MULCNT);
  710. USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_MULCNT & ((uint32_t)pktcnt << 29));
  711. }
  712. }
  713. USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_XFRSIZ & ep->xfer_len);
  714. }
  715. if (dma == 1U)
  716. {
  717. if ((uint32_t)ep->dma_addr != 0U)
  718. {
  719. USBx_INEP(epnum)->DIEPDMA = (uint32_t)(ep->dma_addr);
  720. }
  721. if (ep->type == EP_TYPE_ISOC)
  722. {
  723. if ((USBx_DEVICE->DSTS & (1U << 8)) == 0U)
  724. {
  725. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SODDFRM;
  726. }
  727. else
  728. {
  729. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM;
  730. }
  731. }
  732. /* EP enable, IN data in FIFO */
  733. USBx_INEP(epnum)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
  734. }
  735. else
  736. {
  737. /* EP enable, IN data in FIFO */
  738. USBx_INEP(epnum)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
  739. if (ep->type != EP_TYPE_ISOC)
  740. {
  741. /* Enable the Tx FIFO Empty Interrupt for this EP */
  742. if (ep->xfer_len > 0U)
  743. {
  744. USBx_DEVICE->DIEPEMPMSK |= 1UL << (ep->num & EP_ADDR_MSK);
  745. }
  746. }
  747. else
  748. {
  749. if ((USBx_DEVICE->DSTS & (1U << 8)) == 0U)
  750. {
  751. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SODDFRM;
  752. }
  753. else
  754. {
  755. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM;
  756. }
  757. (void)USB_WritePacket(USBx, ep->xfer_buff, ep->num, (uint16_t)ep->xfer_len, dma);
  758. }
  759. }
  760. }
  761. else /* OUT endpoint */
  762. {
  763. /* Program the transfer size and packet count as follows:
  764. * pktcnt = N
  765. * xfersize = N * maxpacket
  766. */
  767. USBx_OUTEP(epnum)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_XFRSIZ);
  768. USBx_OUTEP(epnum)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_PKTCNT);
  769. if (epnum == 0U)
  770. {
  771. if (ep->xfer_len > 0U)
  772. {
  773. ep->xfer_len = ep->maxpacket;
  774. }
  775. /* Store transfer size, for EP0 this is equal to endpoint max packet size */
  776. ep->xfer_size = ep->maxpacket;
  777. USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & ep->xfer_size);
  778. USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19));
  779. }
  780. else
  781. {
  782. if (ep->xfer_len == 0U)
  783. {
  784. USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & ep->maxpacket);
  785. USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19));
  786. }
  787. else
  788. {
  789. pktcnt = (uint16_t)((ep->xfer_len + ep->maxpacket - 1U) / ep->maxpacket);
  790. ep->xfer_size = ep->maxpacket * pktcnt;
  791. USBx_OUTEP(epnum)->DOEPTSIZ |= USB_OTG_DOEPTSIZ_PKTCNT & ((uint32_t)pktcnt << 19);
  792. USBx_OUTEP(epnum)->DOEPTSIZ |= USB_OTG_DOEPTSIZ_XFRSIZ & ep->xfer_size;
  793. }
  794. }
  795. if (dma == 1U)
  796. {
  797. if ((uint32_t)ep->xfer_buff != 0U)
  798. {
  799. USBx_OUTEP(epnum)->DOEPDMA = (uint32_t)(ep->xfer_buff);
  800. }
  801. }
  802. if (ep->type == EP_TYPE_ISOC)
  803. {
  804. if ((USBx_DEVICE->DSTS & (1U << 8)) == 0U)
  805. {
  806. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SODDFRM;
  807. }
  808. else
  809. {
  810. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM;
  811. }
  812. }
  813. /* EP enable */
  814. USBx_OUTEP(epnum)->DOEPCTL |= (USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
  815. }
  816. return HAL_OK;
  817. }
  818. /**
  819. * @brief USB_EPStoptXfer Stop transfer on an EP
  820. * @param USBx usb device instance
  821. * @param ep pointer to endpoint structure
  822. * @retval HAL status
  823. */
  824. HAL_StatusTypeDef USB_EPStopXfer(const USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
  825. {
  826. __IO uint32_t count = 0U;
  827. HAL_StatusTypeDef ret = HAL_OK;
  828. uint32_t USBx_BASE = (uint32_t)USBx;
  829. /* IN endpoint */
  830. if (ep->is_in == 1U)
  831. {
  832. /* EP enable, IN data in FIFO */
  833. if (((USBx_INEP(ep->num)->DIEPCTL) & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
  834. {
  835. USBx_INEP(ep->num)->DIEPCTL |= (USB_OTG_DIEPCTL_SNAK);
  836. USBx_INEP(ep->num)->DIEPCTL |= (USB_OTG_DIEPCTL_EPDIS);
  837. do
  838. {
  839. count++;
  840. if (count > 10000U)
  841. {
  842. ret = HAL_ERROR;
  843. break;
  844. }
  845. } while (((USBx_INEP(ep->num)->DIEPCTL) & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA);
  846. }
  847. }
  848. else /* OUT endpoint */
  849. {
  850. if (((USBx_OUTEP(ep->num)->DOEPCTL) & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
  851. {
  852. USBx_OUTEP(ep->num)->DOEPCTL |= (USB_OTG_DOEPCTL_SNAK);
  853. USBx_OUTEP(ep->num)->DOEPCTL |= (USB_OTG_DOEPCTL_EPDIS);
  854. do
  855. {
  856. count++;
  857. if (count > 10000U)
  858. {
  859. ret = HAL_ERROR;
  860. break;
  861. }
  862. } while (((USBx_OUTEP(ep->num)->DOEPCTL) & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA);
  863. }
  864. }
  865. return ret;
  866. }
  867. /**
  868. * @brief USB_WritePacket : Writes a packet into the Tx FIFO associated
  869. * with the EP/channel
  870. * @param USBx Selected device
  871. * @param src pointer to source buffer
  872. * @param ch_ep_num endpoint or host channel number
  873. * @param len Number of bytes to write
  874. * @param dma USB dma enabled or disabled
  875. * This parameter can be one of these values:
  876. * 0 : DMA feature not used
  877. * 1 : DMA feature used
  878. * @retval HAL status
  879. */
  880. HAL_StatusTypeDef USB_WritePacket(const USB_OTG_GlobalTypeDef *USBx, uint8_t *src,
  881. uint8_t ch_ep_num, uint16_t len, uint8_t dma)
  882. {
  883. uint32_t USBx_BASE = (uint32_t)USBx;
  884. uint8_t *pSrc = src;
  885. uint32_t count32b;
  886. uint32_t i;
  887. if (dma == 0U)
  888. {
  889. count32b = ((uint32_t)len + 3U) / 4U;
  890. for (i = 0U; i < count32b; i++)
  891. {
  892. USBx_DFIFO((uint32_t)ch_ep_num) = __UNALIGNED_UINT32_READ(pSrc);
  893. pSrc++;
  894. pSrc++;
  895. pSrc++;
  896. pSrc++;
  897. }
  898. }
  899. return HAL_OK;
  900. }
  901. /**
  902. * @brief USB_ReadPacket : read a packet from the RX FIFO
  903. * @param USBx Selected device
  904. * @param dest source pointer
  905. * @param len Number of bytes to read
  906. * @retval pointer to destination buffer
  907. */
  908. void *USB_ReadPacket(const USB_OTG_GlobalTypeDef *USBx, uint8_t *dest, uint16_t len)
  909. {
  910. uint32_t USBx_BASE = (uint32_t)USBx;
  911. uint8_t *pDest = dest;
  912. uint32_t pData;
  913. uint32_t i;
  914. uint32_t count32b = (uint32_t)len >> 2U;
  915. uint16_t remaining_bytes = len % 4U;
  916. for (i = 0U; i < count32b; i++)
  917. {
  918. __UNALIGNED_UINT32_WRITE(pDest, USBx_DFIFO(0U));
  919. pDest++;
  920. pDest++;
  921. pDest++;
  922. pDest++;
  923. }
  924. /* When Number of data is not word aligned, read the remaining byte */
  925. if (remaining_bytes != 0U)
  926. {
  927. i = 0U;
  928. __UNALIGNED_UINT32_WRITE(&pData, USBx_DFIFO(0U));
  929. do
  930. {
  931. *(uint8_t *)pDest = (uint8_t)(pData >> (8U * (uint8_t)(i)));
  932. i++;
  933. pDest++;
  934. remaining_bytes--;
  935. } while (remaining_bytes != 0U);
  936. }
  937. return ((void *)pDest);
  938. }
  939. /**
  940. * @brief USB_EPSetStall : set a stall condition over an EP
  941. * @param USBx Selected device
  942. * @param ep pointer to endpoint structure
  943. * @retval HAL status
  944. */
  945. HAL_StatusTypeDef USB_EPSetStall(const USB_OTG_GlobalTypeDef *USBx, const USB_OTG_EPTypeDef *ep)
  946. {
  947. uint32_t USBx_BASE = (uint32_t)USBx;
  948. uint32_t epnum = (uint32_t)ep->num;
  949. if (ep->is_in == 1U)
  950. {
  951. if (((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == 0U) && (epnum != 0U))
  952. {
  953. USBx_INEP(epnum)->DIEPCTL &= ~(USB_OTG_DIEPCTL_EPDIS);
  954. }
  955. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_STALL;
  956. }
  957. else
  958. {
  959. if (((USBx_OUTEP(epnum)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == 0U) && (epnum != 0U))
  960. {
  961. USBx_OUTEP(epnum)->DOEPCTL &= ~(USB_OTG_DOEPCTL_EPDIS);
  962. }
  963. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_STALL;
  964. }
  965. return HAL_OK;
  966. }
  967. /**
  968. * @brief USB_EPClearStall : Clear a stall condition over an EP
  969. * @param USBx Selected device
  970. * @param ep pointer to endpoint structure
  971. * @retval HAL status
  972. */
  973. HAL_StatusTypeDef USB_EPClearStall(const USB_OTG_GlobalTypeDef *USBx, const USB_OTG_EPTypeDef *ep)
  974. {
  975. uint32_t USBx_BASE = (uint32_t)USBx;
  976. uint32_t epnum = (uint32_t)ep->num;
  977. if (ep->is_in == 1U)
  978. {
  979. USBx_INEP(epnum)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
  980. if ((ep->type == EP_TYPE_INTR) || (ep->type == EP_TYPE_BULK))
  981. {
  982. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM; /* DATA0 */
  983. }
  984. }
  985. else
  986. {
  987. USBx_OUTEP(epnum)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
  988. if ((ep->type == EP_TYPE_INTR) || (ep->type == EP_TYPE_BULK))
  989. {
  990. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM; /* DATA0 */
  991. }
  992. }
  993. return HAL_OK;
  994. }
  995. /**
  996. * @brief USB_StopDevice : Stop the usb device mode
  997. * @param USBx Selected device
  998. * @retval HAL status
  999. */
  1000. HAL_StatusTypeDef USB_StopDevice(USB_OTG_GlobalTypeDef *USBx)
  1001. {
  1002. HAL_StatusTypeDef ret;
  1003. uint32_t USBx_BASE = (uint32_t)USBx;
  1004. uint32_t i;
  1005. /* Clear Pending interrupt */
  1006. for (i = 0U; i < 15U; i++)
  1007. {
  1008. USBx_INEP(i)->DIEPINT = 0xFB7FU;
  1009. USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
  1010. }
  1011. /* Clear interrupt masks */
  1012. USBx_DEVICE->DIEPMSK = 0U;
  1013. USBx_DEVICE->DOEPMSK = 0U;
  1014. USBx_DEVICE->DAINTMSK = 0U;
  1015. /* Flush the FIFO */
  1016. ret = USB_FlushRxFifo(USBx);
  1017. if (ret != HAL_OK)
  1018. {
  1019. return ret;
  1020. }
  1021. ret = USB_FlushTxFifo(USBx, 0x10U);
  1022. if (ret != HAL_OK)
  1023. {
  1024. return ret;
  1025. }
  1026. return ret;
  1027. }
  1028. /**
  1029. * @brief USB_SetDevAddress : Stop the usb device mode
  1030. * @param USBx Selected device
  1031. * @param address new device address to be assigned
  1032. * This parameter can be a value from 0 to 255
  1033. * @retval HAL status
  1034. */
  1035. HAL_StatusTypeDef USB_SetDevAddress(const USB_OTG_GlobalTypeDef *USBx, uint8_t address)
  1036. {
  1037. uint32_t USBx_BASE = (uint32_t)USBx;
  1038. USBx_DEVICE->DCFG &= ~(USB_OTG_DCFG_DAD);
  1039. USBx_DEVICE->DCFG |= ((uint32_t)address << 4) & USB_OTG_DCFG_DAD;
  1040. return HAL_OK;
  1041. }
  1042. /**
  1043. * @brief USB_DevConnect : Connect the USB device by enabling Rpu
  1044. * @param USBx Selected device
  1045. * @retval HAL status
  1046. */
  1047. HAL_StatusTypeDef USB_DevConnect(const USB_OTG_GlobalTypeDef *USBx)
  1048. {
  1049. uint32_t USBx_BASE = (uint32_t)USBx;
  1050. /* In case phy is stopped, ensure to ungate and restore the phy CLK */
  1051. USBx_PCGCCTL &= ~(USB_OTG_PCGCCTL_STOPCLK | USB_OTG_PCGCCTL_GATECLK);
  1052. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_SDIS;
  1053. return HAL_OK;
  1054. }
  1055. /**
  1056. * @brief USB_DevDisconnect : Disconnect the USB device by disabling Rpu
  1057. * @param USBx Selected device
  1058. * @retval HAL status
  1059. */
  1060. HAL_StatusTypeDef USB_DevDisconnect(const USB_OTG_GlobalTypeDef *USBx)
  1061. {
  1062. uint32_t USBx_BASE = (uint32_t)USBx;
  1063. /* In case phy is stopped, ensure to ungate and restore the phy CLK */
  1064. USBx_PCGCCTL &= ~(USB_OTG_PCGCCTL_STOPCLK | USB_OTG_PCGCCTL_GATECLK);
  1065. USBx_DEVICE->DCTL |= USB_OTG_DCTL_SDIS;
  1066. return HAL_OK;
  1067. }
  1068. /**
  1069. * @brief USB_ReadInterrupts: return the global USB interrupt status
  1070. * @param USBx Selected device
  1071. * @retval USB Global Interrupt status
  1072. */
  1073. uint32_t USB_ReadInterrupts(USB_OTG_GlobalTypeDef const *USBx)
  1074. {
  1075. uint32_t tmpreg;
  1076. tmpreg = USBx->GINTSTS;
  1077. tmpreg &= USBx->GINTMSK;
  1078. return tmpreg;
  1079. }
  1080. /**
  1081. * @brief USB_ReadChInterrupts: return USB channel interrupt status
  1082. * @param USBx Selected device
  1083. * @param chnum Channel number
  1084. * @retval USB Channel Interrupt status
  1085. */
  1086. uint32_t USB_ReadChInterrupts(const USB_OTG_GlobalTypeDef *USBx, uint8_t chnum)
  1087. {
  1088. uint32_t USBx_BASE = (uint32_t)USBx;
  1089. uint32_t tmpreg;
  1090. tmpreg = USBx_HC(chnum)->HCINT;
  1091. tmpreg &= USBx_HC(chnum)->HCINTMSK;
  1092. return tmpreg;
  1093. }
  1094. /**
  1095. * @brief USB_ReadDevAllOutEpInterrupt: return the USB device OUT endpoints interrupt status
  1096. * @param USBx Selected device
  1097. * @retval USB Device OUT EP interrupt status
  1098. */
  1099. uint32_t USB_ReadDevAllOutEpInterrupt(const USB_OTG_GlobalTypeDef *USBx)
  1100. {
  1101. uint32_t USBx_BASE = (uint32_t)USBx;
  1102. uint32_t tmpreg;
  1103. tmpreg = USBx_DEVICE->DAINT;
  1104. tmpreg &= USBx_DEVICE->DAINTMSK;
  1105. return ((tmpreg & 0xffff0000U) >> 16);
  1106. }
  1107. /**
  1108. * @brief USB_ReadDevAllInEpInterrupt: return the USB device IN endpoints interrupt status
  1109. * @param USBx Selected device
  1110. * @retval USB Device IN EP interrupt status
  1111. */
  1112. uint32_t USB_ReadDevAllInEpInterrupt(const USB_OTG_GlobalTypeDef *USBx)
  1113. {
  1114. uint32_t USBx_BASE = (uint32_t)USBx;
  1115. uint32_t tmpreg;
  1116. tmpreg = USBx_DEVICE->DAINT;
  1117. tmpreg &= USBx_DEVICE->DAINTMSK;
  1118. return ((tmpreg & 0xFFFFU));
  1119. }
  1120. /**
  1121. * @brief Returns Device OUT EP Interrupt register
  1122. * @param USBx Selected device
  1123. * @param epnum endpoint number
  1124. * This parameter can be a value from 0 to 15
  1125. * @retval Device OUT EP Interrupt register
  1126. */
  1127. uint32_t USB_ReadDevOutEPInterrupt(const USB_OTG_GlobalTypeDef *USBx, uint8_t epnum)
  1128. {
  1129. uint32_t USBx_BASE = (uint32_t)USBx;
  1130. uint32_t tmpreg;
  1131. tmpreg = USBx_OUTEP((uint32_t)epnum)->DOEPINT;
  1132. tmpreg &= USBx_DEVICE->DOEPMSK;
  1133. return tmpreg;
  1134. }
  1135. /**
  1136. * @brief Returns Device IN EP Interrupt register
  1137. * @param USBx Selected device
  1138. * @param epnum endpoint number
  1139. * This parameter can be a value from 0 to 15
  1140. * @retval Device IN EP Interrupt register
  1141. */
  1142. uint32_t USB_ReadDevInEPInterrupt(const USB_OTG_GlobalTypeDef *USBx, uint8_t epnum)
  1143. {
  1144. uint32_t USBx_BASE = (uint32_t)USBx;
  1145. uint32_t tmpreg;
  1146. uint32_t msk;
  1147. uint32_t emp;
  1148. msk = USBx_DEVICE->DIEPMSK;
  1149. emp = USBx_DEVICE->DIEPEMPMSK;
  1150. msk |= ((emp >> (epnum & EP_ADDR_MSK)) & 0x1U) << 7;
  1151. tmpreg = USBx_INEP((uint32_t)epnum)->DIEPINT & msk;
  1152. return tmpreg;
  1153. }
  1154. /**
  1155. * @brief USB_ClearInterrupts: clear a USB interrupt
  1156. * @param USBx Selected device
  1157. * @param interrupt flag
  1158. * @retval None
  1159. */
  1160. void USB_ClearInterrupts(USB_OTG_GlobalTypeDef *USBx, uint32_t interrupt)
  1161. {
  1162. USBx->GINTSTS &= interrupt;
  1163. }
  1164. /**
  1165. * @brief Returns USB core mode
  1166. * @param USBx Selected device
  1167. * @retval return core mode : Host or Device
  1168. * This parameter can be one of these values:
  1169. * 1 : Host
  1170. * 0 : Device
  1171. */
  1172. uint32_t USB_GetMode(const USB_OTG_GlobalTypeDef *USBx)
  1173. {
  1174. return ((USBx->GINTSTS) & 0x1U);
  1175. }
  1176. /**
  1177. * @brief Activate EP0 for Setup transactions
  1178. * @param USBx Selected device
  1179. * @retval HAL status
  1180. */
  1181. HAL_StatusTypeDef USB_ActivateSetup(const USB_OTG_GlobalTypeDef *USBx)
  1182. {
  1183. uint32_t USBx_BASE = (uint32_t)USBx;
  1184. /* Set the MPS of the IN EP0 to 64 bytes */
  1185. USBx_INEP(0U)->DIEPCTL &= ~USB_OTG_DIEPCTL_MPSIZ;
  1186. USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGINAK;
  1187. return HAL_OK;
  1188. }
  1189. /**
  1190. * @brief Prepare the EP0 to start the first control setup
  1191. * @param USBx Selected device
  1192. * @param dma USB dma enabled or disabled
  1193. * This parameter can be one of these values:
  1194. * 0 : DMA feature not used
  1195. * 1 : DMA feature used
  1196. * @param psetup pointer to setup packet
  1197. * @retval HAL status
  1198. */
  1199. HAL_StatusTypeDef USB_EP0_OutStart(const USB_OTG_GlobalTypeDef *USBx, uint8_t dma, const uint8_t *psetup)
  1200. {
  1201. uint32_t USBx_BASE = (uint32_t)USBx;
  1202. uint32_t gSNPSiD = *(__IO const uint32_t *)(&USBx->CID + 0x1U);
  1203. if (gSNPSiD > USB_OTG_CORE_ID_300A)
  1204. {
  1205. if ((USBx_OUTEP(0U)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
  1206. {
  1207. return HAL_OK;
  1208. }
  1209. }
  1210. USBx_OUTEP(0U)->DOEPTSIZ = 0U;
  1211. USBx_OUTEP(0U)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19));
  1212. USBx_OUTEP(0U)->DOEPTSIZ |= (3U * 8U);
  1213. USBx_OUTEP(0U)->DOEPTSIZ |= USB_OTG_DOEPTSIZ_STUPCNT;
  1214. if (dma == 1U)
  1215. {
  1216. USBx_OUTEP(0U)->DOEPDMA = (uint32_t)psetup;
  1217. /* EP enable */
  1218. USBx_OUTEP(0U)->DOEPCTL |= USB_OTG_DOEPCTL_EPENA | USB_OTG_DOEPCTL_USBAEP;
  1219. }
  1220. return HAL_OK;
  1221. }
  1222. /**
  1223. * @brief Reset the USB Core (needed after USB clock settings change)
  1224. * @param USBx Selected device
  1225. * @retval HAL status
  1226. */
  1227. static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx)
  1228. {
  1229. __IO uint32_t count = 0U;
  1230. /* Wait for AHB master IDLE state. */
  1231. do
  1232. {
  1233. count++;
  1234. if (count > HAL_USB_TIMEOUT)
  1235. {
  1236. return HAL_TIMEOUT;
  1237. }
  1238. } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
  1239. count = 10U;
  1240. /* few cycles before setting core reset */
  1241. while (count > 0U)
  1242. {
  1243. count--;
  1244. }
  1245. /* Core Soft Reset */
  1246. USBx->GRSTCTL |= USB_OTG_GRSTCTL_CSRST;
  1247. do
  1248. {
  1249. count++;
  1250. if (count > HAL_USB_TIMEOUT)
  1251. {
  1252. return HAL_TIMEOUT;
  1253. }
  1254. } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_CSRST) == USB_OTG_GRSTCTL_CSRST);
  1255. return HAL_OK;
  1256. }
  1257. /**
  1258. * @brief USB_HostInit : Initializes the USB OTG controller registers
  1259. * for Host mode
  1260. * @param USBx Selected device
  1261. * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
  1262. * the configuration information for the specified USBx peripheral.
  1263. * @retval HAL status
  1264. */
  1265. HAL_StatusTypeDef USB_HostInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
  1266. {
  1267. HAL_StatusTypeDef ret = HAL_OK;
  1268. uint32_t USBx_BASE = (uint32_t)USBx;
  1269. uint32_t i;
  1270. /* Restart the Phy Clock */
  1271. USBx_PCGCCTL = 0U;
  1272. #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) \
  1273. || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) \
  1274. || defined(STM32F423xx)
  1275. /* Disable HW VBUS sensing */
  1276. USBx->GCCFG &= ~(USB_OTG_GCCFG_VBDEN);
  1277. #else
  1278. /*
  1279. * Disable HW VBUS sensing. VBUS is internally considered to be always
  1280. * at VBUS-Valid level (5V).
  1281. */
  1282. USBx->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS;
  1283. USBx->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN;
  1284. USBx->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN;
  1285. #endif /* defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) ||
  1286. defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) ||
  1287. defined(STM32F423xx) */
  1288. #if defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) \
  1289. || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  1290. /* Disable Battery chargin detector */
  1291. USBx->GCCFG &= ~(USB_OTG_GCCFG_BCDEN);
  1292. #endif /* defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) ||
  1293. defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx) */
  1294. if ((USBx->GUSBCFG & USB_OTG_GUSBCFG_PHYSEL) == 0U)
  1295. {
  1296. if (cfg.speed == USBH_FSLS_SPEED)
  1297. {
  1298. /* Force Device Enumeration to FS/LS mode only */
  1299. USBx_HOST->HCFG |= USB_OTG_HCFG_FSLSS;
  1300. }
  1301. else
  1302. {
  1303. /* Set default Max speed support */
  1304. USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSS);
  1305. }
  1306. }
  1307. else
  1308. {
  1309. /* Set default Max speed support */
  1310. USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSS);
  1311. }
  1312. /* Make sure the FIFOs are flushed. */
  1313. if (USB_FlushTxFifo(USBx, 0x10U) != HAL_OK) /* all Tx FIFOs */
  1314. {
  1315. ret = HAL_ERROR;
  1316. }
  1317. if (USB_FlushRxFifo(USBx) != HAL_OK)
  1318. {
  1319. ret = HAL_ERROR;
  1320. }
  1321. /* Clear all pending HC Interrupts */
  1322. for (i = 0U; i < cfg.Host_channels; i++)
  1323. {
  1324. USBx_HC(i)->HCINT = CLEAR_INTERRUPT_MASK;
  1325. USBx_HC(i)->HCINTMSK = 0U;
  1326. }
  1327. /* Disable all interrupts. */
  1328. USBx->GINTMSK = 0U;
  1329. /* Clear any pending interrupts */
  1330. USBx->GINTSTS = CLEAR_INTERRUPT_MASK;
  1331. #if defined (USB_OTG_HS)
  1332. if (USBx == USB_OTG_HS)
  1333. {
  1334. /* set Rx FIFO size */
  1335. USBx->GRXFSIZ = 0x200U;
  1336. USBx->DIEPTXF0_HNPTXFSIZ = (uint32_t)(((0x100U << 16) & USB_OTG_NPTXFD) | 0x200U);
  1337. USBx->HPTXFSIZ = (uint32_t)(((0xE0U << 16) & USB_OTG_HPTXFSIZ_PTXFD) | 0x300U);
  1338. }
  1339. else
  1340. #endif /* defined (USB_OTG_HS) */
  1341. {
  1342. /* set Rx FIFO size */
  1343. USBx->GRXFSIZ = 0x80U;
  1344. USBx->DIEPTXF0_HNPTXFSIZ = (uint32_t)(((0x60U << 16) & USB_OTG_NPTXFD) | 0x80U);
  1345. USBx->HPTXFSIZ = (uint32_t)(((0x40U << 16)& USB_OTG_HPTXFSIZ_PTXFD) | 0xE0U);
  1346. }
  1347. /* Enable the common interrupts */
  1348. if (cfg.dma_enable == 0U)
  1349. {
  1350. USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
  1351. }
  1352. /* Enable interrupts matching to the Host mode ONLY */
  1353. USBx->GINTMSK |= (USB_OTG_GINTMSK_PRTIM | USB_OTG_GINTMSK_HCIM | \
  1354. USB_OTG_GINTMSK_SOFM | USB_OTG_GINTSTS_DISCINT | \
  1355. USB_OTG_GINTMSK_PXFRM_IISOOXFRM | USB_OTG_GINTMSK_WUIM);
  1356. return ret;
  1357. }
  1358. /**
  1359. * @brief USB_InitFSLSPClkSel : Initializes the FSLSPClkSel field of the
  1360. * HCFG register on the PHY type and set the right frame interval
  1361. * @param USBx Selected device
  1362. * @param freq clock frequency
  1363. * This parameter can be one of these values:
  1364. * HCFG_48_MHZ : Full Speed 48 MHz Clock
  1365. * HCFG_6_MHZ : Low Speed 6 MHz Clock
  1366. * @retval HAL status
  1367. */
  1368. HAL_StatusTypeDef USB_InitFSLSPClkSel(const USB_OTG_GlobalTypeDef *USBx, uint8_t freq)
  1369. {
  1370. uint32_t USBx_BASE = (uint32_t)USBx;
  1371. USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSPCS);
  1372. USBx_HOST->HCFG |= (uint32_t)freq & USB_OTG_HCFG_FSLSPCS;
  1373. if (freq == HCFG_48_MHZ)
  1374. {
  1375. USBx_HOST->HFIR = HFIR_48_MHZ;
  1376. }
  1377. else if (freq == HCFG_6_MHZ)
  1378. {
  1379. USBx_HOST->HFIR = HFIR_6_MHZ;
  1380. }
  1381. else
  1382. {
  1383. return HAL_ERROR;
  1384. }
  1385. return HAL_OK;
  1386. }
  1387. /**
  1388. * @brief USB_OTG_ResetPort : Reset Host Port
  1389. * @param USBx Selected device
  1390. * @retval HAL status
  1391. * @note (1)The application must wait at least 10 ms
  1392. * before clearing the reset bit.
  1393. */
  1394. HAL_StatusTypeDef USB_ResetPort(const USB_OTG_GlobalTypeDef *USBx)
  1395. {
  1396. uint32_t USBx_BASE = (uint32_t)USBx;
  1397. __IO uint32_t hprt0 = 0U;
  1398. hprt0 = USBx_HPRT0;
  1399. hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |
  1400. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
  1401. USBx_HPRT0 = (USB_OTG_HPRT_PRST | hprt0);
  1402. HAL_Delay(100U); /* See Note #1 */
  1403. USBx_HPRT0 = ((~USB_OTG_HPRT_PRST) & hprt0);
  1404. HAL_Delay(10U);
  1405. return HAL_OK;
  1406. }
  1407. /**
  1408. * @brief USB_DriveVbus : activate or de-activate vbus
  1409. * @param state VBUS state
  1410. * This parameter can be one of these values:
  1411. * 0 : Deactivate VBUS
  1412. * 1 : Activate VBUS
  1413. * @retval HAL status
  1414. */
  1415. HAL_StatusTypeDef USB_DriveVbus(const USB_OTG_GlobalTypeDef *USBx, uint8_t state)
  1416. {
  1417. uint32_t USBx_BASE = (uint32_t)USBx;
  1418. __IO uint32_t hprt0 = 0U;
  1419. hprt0 = USBx_HPRT0;
  1420. hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |
  1421. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
  1422. if (((hprt0 & USB_OTG_HPRT_PPWR) == 0U) && (state == 1U))
  1423. {
  1424. USBx_HPRT0 = (USB_OTG_HPRT_PPWR | hprt0);
  1425. }
  1426. if (((hprt0 & USB_OTG_HPRT_PPWR) == USB_OTG_HPRT_PPWR) && (state == 0U))
  1427. {
  1428. USBx_HPRT0 = ((~USB_OTG_HPRT_PPWR) & hprt0);
  1429. }
  1430. return HAL_OK;
  1431. }
  1432. /**
  1433. * @brief Return Host Port speed
  1434. * @param USBx Selected device
  1435. * @retval speed : Host port device speed
  1436. * This parameter can be one of these values:
  1437. * @arg HCD_DEVICE_SPEED_HIGH: High speed mode
  1438. * @arg HCD_DEVICE_SPEED_FULL: Full speed mode
  1439. * @arg HCD_DEVICE_SPEED_LOW: Low speed mode
  1440. */
  1441. uint32_t USB_GetHostSpeed(USB_OTG_GlobalTypeDef const *USBx)
  1442. {
  1443. uint32_t USBx_BASE = (uint32_t)USBx;
  1444. __IO uint32_t hprt0 = 0U;
  1445. hprt0 = USBx_HPRT0;
  1446. return ((hprt0 & USB_OTG_HPRT_PSPD) >> 17);
  1447. }
  1448. /**
  1449. * @brief Return Host Current Frame number
  1450. * @param USBx Selected device
  1451. * @retval current frame number
  1452. */
  1453. uint32_t USB_GetCurrentFrame(USB_OTG_GlobalTypeDef const *USBx)
  1454. {
  1455. uint32_t USBx_BASE = (uint32_t)USBx;
  1456. return (USBx_HOST->HFNUM & USB_OTG_HFNUM_FRNUM);
  1457. }
  1458. /**
  1459. * @brief Initialize a host channel
  1460. * @param USBx Selected device
  1461. * @param ch_num Channel number
  1462. * This parameter can be a value from 1 to 15
  1463. * @param epnum Endpoint number
  1464. * This parameter can be a value from 1 to 15
  1465. * @param dev_address Current device address
  1466. * This parameter can be a value from 0 to 255
  1467. * @param speed Current device speed
  1468. * This parameter can be one of these values:
  1469. * @arg USB_OTG_SPEED_HIGH: High speed mode
  1470. * @arg USB_OTG_SPEED_FULL: Full speed mode
  1471. * @arg USB_OTG_SPEED_LOW: Low speed mode
  1472. * @param ep_type Endpoint Type
  1473. * This parameter can be one of these values:
  1474. * @arg EP_TYPE_CTRL: Control type
  1475. * @arg EP_TYPE_ISOC: Isochronous type
  1476. * @arg EP_TYPE_BULK: Bulk type
  1477. * @arg EP_TYPE_INTR: Interrupt type
  1478. * @param mps Max Packet Size
  1479. * This parameter can be a value from 0 to 32K
  1480. * @retval HAL state
  1481. */
  1482. HAL_StatusTypeDef USB_HC_Init(USB_OTG_GlobalTypeDef *USBx, uint8_t ch_num,
  1483. uint8_t epnum, uint8_t dev_address, uint8_t speed,
  1484. uint8_t ep_type, uint16_t mps)
  1485. {
  1486. HAL_StatusTypeDef ret = HAL_OK;
  1487. uint32_t USBx_BASE = (uint32_t)USBx;
  1488. uint32_t HCcharEpDir;
  1489. uint32_t HCcharLowSpeed;
  1490. uint32_t HostCoreSpeed;
  1491. /* Clear old interrupt conditions for this host channel. */
  1492. USBx_HC((uint32_t)ch_num)->HCINT = CLEAR_INTERRUPT_MASK;
  1493. /* Enable channel interrupts required for this transfer. */
  1494. switch (ep_type)
  1495. {
  1496. case EP_TYPE_CTRL:
  1497. case EP_TYPE_BULK:
  1498. USBx_HC((uint32_t)ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |
  1499. USB_OTG_HCINTMSK_STALLM |
  1500. USB_OTG_HCINTMSK_TXERRM |
  1501. USB_OTG_HCINTMSK_DTERRM |
  1502. USB_OTG_HCINTMSK_AHBERR |
  1503. USB_OTG_HCINTMSK_NAKM;
  1504. if ((epnum & 0x80U) == 0x80U)
  1505. {
  1506. USBx_HC((uint32_t)ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
  1507. }
  1508. else
  1509. {
  1510. #if defined (USB_OTG_HS)
  1511. if (USBx == USB_OTG_HS)
  1512. {
  1513. USBx_HC((uint32_t)ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_NYET |
  1514. USB_OTG_HCINTMSK_ACKM;
  1515. }
  1516. #endif /* defined (USB_OTG_HS) */
  1517. }
  1518. break;
  1519. case EP_TYPE_INTR:
  1520. USBx_HC((uint32_t)ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |
  1521. USB_OTG_HCINTMSK_STALLM |
  1522. USB_OTG_HCINTMSK_TXERRM |
  1523. USB_OTG_HCINTMSK_DTERRM |
  1524. USB_OTG_HCINTMSK_NAKM |
  1525. USB_OTG_HCINTMSK_AHBERR |
  1526. USB_OTG_HCINTMSK_FRMORM;
  1527. if ((epnum & 0x80U) == 0x80U)
  1528. {
  1529. USBx_HC((uint32_t)ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
  1530. }
  1531. break;
  1532. case EP_TYPE_ISOC:
  1533. USBx_HC((uint32_t)ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |
  1534. USB_OTG_HCINTMSK_ACKM |
  1535. USB_OTG_HCINTMSK_AHBERR |
  1536. USB_OTG_HCINTMSK_FRMORM;
  1537. if ((epnum & 0x80U) == 0x80U)
  1538. {
  1539. USBx_HC((uint32_t)ch_num)->HCINTMSK |= (USB_OTG_HCINTMSK_TXERRM | USB_OTG_HCINTMSK_BBERRM);
  1540. }
  1541. break;
  1542. default:
  1543. ret = HAL_ERROR;
  1544. break;
  1545. }
  1546. /* Clear Hub Start Split transaction */
  1547. USBx_HC((uint32_t)ch_num)->HCSPLT = 0U;
  1548. /* Enable host channel Halt interrupt */
  1549. USBx_HC((uint32_t)ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_CHHM;
  1550. /* Enable the top level host channel interrupt. */
  1551. USBx_HOST->HAINTMSK |= 1UL << (ch_num & 0xFU);
  1552. /* Make sure host channel interrupts are enabled. */
  1553. USBx->GINTMSK |= USB_OTG_GINTMSK_HCIM;
  1554. /* Program the HCCHAR register */
  1555. if ((epnum & 0x80U) == 0x80U)
  1556. {
  1557. HCcharEpDir = (0x1U << 15) & USB_OTG_HCCHAR_EPDIR;
  1558. }
  1559. else
  1560. {
  1561. HCcharEpDir = 0U;
  1562. }
  1563. HostCoreSpeed = USB_GetHostSpeed(USBx);
  1564. /* LS device plugged to HUB */
  1565. if ((speed == HPRT0_PRTSPD_LOW_SPEED) && (HostCoreSpeed != HPRT0_PRTSPD_LOW_SPEED))
  1566. {
  1567. HCcharLowSpeed = (0x1U << 17) & USB_OTG_HCCHAR_LSDEV;
  1568. }
  1569. else
  1570. {
  1571. HCcharLowSpeed = 0U;
  1572. }
  1573. USBx_HC((uint32_t)ch_num)->HCCHAR = (((uint32_t)dev_address << 22) & USB_OTG_HCCHAR_DAD) |
  1574. ((((uint32_t)epnum & 0x7FU) << 11) & USB_OTG_HCCHAR_EPNUM) |
  1575. (((uint32_t)ep_type << 18) & USB_OTG_HCCHAR_EPTYP) |
  1576. ((uint32_t)mps & USB_OTG_HCCHAR_MPSIZ) |
  1577. USB_OTG_HCCHAR_MC_0 | HCcharEpDir | HCcharLowSpeed;
  1578. if ((ep_type == EP_TYPE_INTR) || (ep_type == EP_TYPE_ISOC))
  1579. {
  1580. USBx_HC((uint32_t)ch_num)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM;
  1581. }
  1582. return ret;
  1583. }
  1584. /**
  1585. * @brief Start a transfer over a host channel
  1586. * @param USBx Selected device
  1587. * @param hc pointer to host channel structure
  1588. * @param dma USB dma enabled or disabled
  1589. * This parameter can be one of these values:
  1590. * 0 : DMA feature not used
  1591. * 1 : DMA feature used
  1592. * @retval HAL state
  1593. */
  1594. HAL_StatusTypeDef USB_HC_StartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_HCTypeDef *hc, uint8_t dma)
  1595. {
  1596. uint32_t USBx_BASE = (uint32_t)USBx;
  1597. uint32_t ch_num = (uint32_t)hc->ch_num;
  1598. __IO uint32_t tmpreg;
  1599. uint8_t is_oddframe;
  1600. uint16_t len_words;
  1601. uint16_t num_packets;
  1602. uint16_t max_hc_pkt_count = HC_MAX_PKT_CNT;
  1603. #if defined (USB_OTG_HS)
  1604. if (USBx == USB_OTG_HS)
  1605. {
  1606. /* in DMA mode host Core automatically issues ping in case of NYET/NAK */
  1607. if (dma == 1U)
  1608. {
  1609. if (((hc->ep_type == EP_TYPE_CTRL) || (hc->ep_type == EP_TYPE_BULK)) && (hc->do_ssplit == 0U))
  1610. {
  1611. USBx_HC((uint32_t)ch_num)->HCINTMSK &= ~(USB_OTG_HCINTMSK_NYET |
  1612. USB_OTG_HCINTMSK_ACKM |
  1613. USB_OTG_HCINTMSK_NAKM);
  1614. }
  1615. }
  1616. else
  1617. {
  1618. if ((hc->speed == USBH_HS_SPEED) && (hc->do_ping == 1U))
  1619. {
  1620. (void)USB_DoPing(USBx, hc->ch_num);
  1621. return HAL_OK;
  1622. }
  1623. }
  1624. }
  1625. #endif /* defined (USB_OTG_HS) */
  1626. if (hc->do_ssplit == 1U)
  1627. {
  1628. /* Set number of packet to 1 for Split transaction */
  1629. num_packets = 1U;
  1630. if (hc->ep_is_in != 0U)
  1631. {
  1632. hc->XferSize = (uint32_t)num_packets * hc->max_packet;
  1633. }
  1634. else
  1635. {
  1636. if (hc->ep_type == EP_TYPE_ISOC)
  1637. {
  1638. if (hc->xfer_len > ISO_SPLT_MPS)
  1639. {
  1640. /* Isochrone Max Packet Size for Split mode */
  1641. hc->XferSize = hc->max_packet;
  1642. hc->xfer_len = hc->XferSize;
  1643. if ((hc->iso_splt_xactPos == HCSPLT_BEGIN) || (hc->iso_splt_xactPos == HCSPLT_MIDDLE))
  1644. {
  1645. hc->iso_splt_xactPos = HCSPLT_MIDDLE;
  1646. }
  1647. else
  1648. {
  1649. hc->iso_splt_xactPos = HCSPLT_BEGIN;
  1650. }
  1651. }
  1652. else
  1653. {
  1654. hc->XferSize = hc->xfer_len;
  1655. if ((hc->iso_splt_xactPos != HCSPLT_BEGIN) && (hc->iso_splt_xactPos != HCSPLT_MIDDLE))
  1656. {
  1657. hc->iso_splt_xactPos = HCSPLT_FULL;
  1658. }
  1659. else
  1660. {
  1661. hc->iso_splt_xactPos = HCSPLT_END;
  1662. }
  1663. }
  1664. }
  1665. else
  1666. {
  1667. if ((dma == 1U) && (hc->xfer_len > hc->max_packet))
  1668. {
  1669. hc->XferSize = (uint32_t)num_packets * hc->max_packet;
  1670. }
  1671. else
  1672. {
  1673. hc->XferSize = hc->xfer_len;
  1674. }
  1675. }
  1676. }
  1677. }
  1678. else
  1679. {
  1680. /* Compute the expected number of packets associated to the transfer */
  1681. if (hc->xfer_len > 0U)
  1682. {
  1683. num_packets = (uint16_t)((hc->xfer_len + hc->max_packet - 1U) / hc->max_packet);
  1684. if (num_packets > max_hc_pkt_count)
  1685. {
  1686. num_packets = max_hc_pkt_count;
  1687. hc->XferSize = (uint32_t)num_packets * hc->max_packet;
  1688. }
  1689. }
  1690. else
  1691. {
  1692. num_packets = 1U;
  1693. }
  1694. /*
  1695. * For IN channel HCTSIZ.XferSize is expected to be an integer multiple of
  1696. * max_packet size.
  1697. */
  1698. if (hc->ep_is_in != 0U)
  1699. {
  1700. hc->XferSize = (uint32_t)num_packets * hc->max_packet;
  1701. }
  1702. else
  1703. {
  1704. hc->XferSize = hc->xfer_len;
  1705. }
  1706. }
  1707. /* Initialize the HCTSIZn register */
  1708. USBx_HC(ch_num)->HCTSIZ = (hc->XferSize & USB_OTG_HCTSIZ_XFRSIZ) |
  1709. (((uint32_t)num_packets << 19) & USB_OTG_HCTSIZ_PKTCNT) |
  1710. (((uint32_t)hc->data_pid << 29) & USB_OTG_HCTSIZ_DPID);
  1711. if (dma != 0U)
  1712. {
  1713. /* xfer_buff MUST be 32-bits aligned */
  1714. USBx_HC(ch_num)->HCDMA = (uint32_t)hc->xfer_buff;
  1715. }
  1716. is_oddframe = (((uint32_t)USBx_HOST->HFNUM & 0x01U) != 0U) ? 0U : 1U;
  1717. USBx_HC(ch_num)->HCCHAR &= ~USB_OTG_HCCHAR_ODDFRM;
  1718. USBx_HC(ch_num)->HCCHAR |= (uint32_t)is_oddframe << 29;
  1719. if (hc->do_ssplit == 1U)
  1720. {
  1721. /* Set Hub start Split transaction */
  1722. USBx_HC((uint32_t)ch_num)->HCSPLT = ((uint32_t)hc->hub_addr << USB_OTG_HCSPLT_HUBADDR_Pos) |
  1723. (uint32_t)hc->hub_port_nbr | USB_OTG_HCSPLT_SPLITEN;
  1724. /* unmask ack & nyet for IN/OUT transactions */
  1725. USBx_HC((uint32_t)ch_num)->HCINTMSK |= (USB_OTG_HCINTMSK_ACKM |
  1726. USB_OTG_HCINTMSK_NYET);
  1727. if ((hc->do_csplit == 1U) && (hc->ep_is_in == 0U))
  1728. {
  1729. USBx_HC((uint32_t)ch_num)->HCSPLT |= USB_OTG_HCSPLT_COMPLSPLT;
  1730. USBx_HC((uint32_t)ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_NYET;
  1731. }
  1732. if (((hc->ep_type == EP_TYPE_ISOC) || (hc->ep_type == EP_TYPE_INTR)) &&
  1733. (hc->do_csplit == 1U) && (hc->ep_is_in == 1U))
  1734. {
  1735. USBx_HC((uint32_t)ch_num)->HCSPLT |= USB_OTG_HCSPLT_COMPLSPLT;
  1736. }
  1737. /* Position management for iso out transaction on split mode */
  1738. if ((hc->ep_type == EP_TYPE_ISOC) && (hc->ep_is_in == 0U))
  1739. {
  1740. /* Set data payload position */
  1741. switch (hc->iso_splt_xactPos)
  1742. {
  1743. case HCSPLT_BEGIN:
  1744. /* First data payload for OUT Transaction */
  1745. USBx_HC((uint32_t)ch_num)->HCSPLT |= USB_OTG_HCSPLT_XACTPOS_1;
  1746. break;
  1747. case HCSPLT_MIDDLE:
  1748. /* Middle data payload for OUT Transaction */
  1749. USBx_HC((uint32_t)ch_num)->HCSPLT |= USB_OTG_HCSPLT_XACTPOS_Pos;
  1750. break;
  1751. case HCSPLT_END:
  1752. /* End data payload for OUT Transaction */
  1753. USBx_HC((uint32_t)ch_num)->HCSPLT |= USB_OTG_HCSPLT_XACTPOS_0;
  1754. break;
  1755. case HCSPLT_FULL:
  1756. /* Entire data payload for OUT Transaction */
  1757. USBx_HC((uint32_t)ch_num)->HCSPLT |= USB_OTG_HCSPLT_XACTPOS;
  1758. break;
  1759. default:
  1760. break;
  1761. }
  1762. }
  1763. }
  1764. else
  1765. {
  1766. /* Clear Hub Start Split transaction */
  1767. USBx_HC((uint32_t)ch_num)->HCSPLT = 0U;
  1768. }
  1769. /* Set host channel enable */
  1770. tmpreg = USBx_HC(ch_num)->HCCHAR;
  1771. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  1772. /* make sure to set the correct ep direction */
  1773. if (hc->ep_is_in != 0U)
  1774. {
  1775. tmpreg |= USB_OTG_HCCHAR_EPDIR;
  1776. }
  1777. else
  1778. {
  1779. tmpreg &= ~USB_OTG_HCCHAR_EPDIR;
  1780. }
  1781. tmpreg |= USB_OTG_HCCHAR_CHENA;
  1782. USBx_HC(ch_num)->HCCHAR = tmpreg;
  1783. if (dma != 0U) /* dma mode */
  1784. {
  1785. return HAL_OK;
  1786. }
  1787. if ((hc->ep_is_in == 0U) && (hc->xfer_len > 0U) && (hc->do_csplit == 0U))
  1788. {
  1789. switch (hc->ep_type)
  1790. {
  1791. /* Non periodic transfer */
  1792. case EP_TYPE_CTRL:
  1793. case EP_TYPE_BULK:
  1794. len_words = (uint16_t)((hc->xfer_len + 3U) / 4U);
  1795. /* check if there is enough space in FIFO space */
  1796. if (len_words > (USBx->HNPTXSTS & 0xFFFFU))
  1797. {
  1798. /* need to process data in nptxfempty interrupt */
  1799. USBx->GINTMSK |= USB_OTG_GINTMSK_NPTXFEM;
  1800. }
  1801. break;
  1802. /* Periodic transfer */
  1803. case EP_TYPE_INTR:
  1804. case EP_TYPE_ISOC:
  1805. len_words = (uint16_t)((hc->xfer_len + 3U) / 4U);
  1806. /* check if there is enough space in FIFO space */
  1807. if (len_words > (USBx_HOST->HPTXSTS & 0xFFFFU)) /* split the transfer */
  1808. {
  1809. /* need to process data in ptxfempty interrupt */
  1810. USBx->GINTMSK |= USB_OTG_GINTMSK_PTXFEM;
  1811. }
  1812. break;
  1813. default:
  1814. break;
  1815. }
  1816. /* Write packet into the Tx FIFO. */
  1817. (void)USB_WritePacket(USBx, hc->xfer_buff, hc->ch_num, (uint16_t)hc->xfer_len, 0);
  1818. }
  1819. return HAL_OK;
  1820. }
  1821. /**
  1822. * @brief Read all host channel interrupts status
  1823. * @param USBx Selected device
  1824. * @retval HAL state
  1825. */
  1826. uint32_t USB_HC_ReadInterrupt(const USB_OTG_GlobalTypeDef *USBx)
  1827. {
  1828. uint32_t USBx_BASE = (uint32_t)USBx;
  1829. return ((USBx_HOST->HAINT) & 0xFFFFU);
  1830. }
  1831. /**
  1832. * @brief Halt a host channel
  1833. * @param USBx Selected device
  1834. * @param hc_num Host Channel number
  1835. * This parameter can be a value from 1 to 15
  1836. * @retval HAL state
  1837. */
  1838. HAL_StatusTypeDef USB_HC_Halt(const USB_OTG_GlobalTypeDef *USBx, uint8_t hc_num)
  1839. {
  1840. uint32_t USBx_BASE = (uint32_t)USBx;
  1841. uint32_t hcnum = (uint32_t)hc_num;
  1842. __IO uint32_t count = 0U;
  1843. uint32_t HcEpType = (USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_EPTYP) >> 18;
  1844. uint32_t ChannelEna = (USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_CHENA) >> 31;
  1845. uint32_t SplitEna = (USBx_HC(hcnum)->HCSPLT & USB_OTG_HCSPLT_SPLITEN) >> 31;
  1846. /* In buffer DMA, Channel disable must not be programmed for non-split periodic channels.
  1847. At the end of the next uframe/frame (in the worst case), the core generates a channel halted
  1848. and disables the channel automatically. */
  1849. if ((((USBx->GAHBCFG & USB_OTG_GAHBCFG_DMAEN) == USB_OTG_GAHBCFG_DMAEN) && (SplitEna == 0U)) &&
  1850. ((ChannelEna == 0U) || (((HcEpType == HCCHAR_ISOC) || (HcEpType == HCCHAR_INTR)))))
  1851. {
  1852. return HAL_OK;
  1853. }
  1854. /* Check for space in the request queue to issue the halt. */
  1855. if ((HcEpType == HCCHAR_CTRL) || (HcEpType == HCCHAR_BULK))
  1856. {
  1857. USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHDIS;
  1858. if ((USBx->GAHBCFG & USB_OTG_GAHBCFG_DMAEN) == 0U)
  1859. {
  1860. if ((USBx->HNPTXSTS & (0xFFU << 16)) == 0U)
  1861. {
  1862. USBx_HC(hcnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHENA;
  1863. USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  1864. do
  1865. {
  1866. count++;
  1867. if (count > 1000U)
  1868. {
  1869. break;
  1870. }
  1871. } while ((USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
  1872. }
  1873. else
  1874. {
  1875. USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  1876. }
  1877. }
  1878. else
  1879. {
  1880. USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  1881. }
  1882. }
  1883. else
  1884. {
  1885. USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHDIS;
  1886. if ((USBx_HOST->HPTXSTS & (0xFFU << 16)) == 0U)
  1887. {
  1888. USBx_HC(hcnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHENA;
  1889. USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  1890. do
  1891. {
  1892. count++;
  1893. if (count > 1000U)
  1894. {
  1895. break;
  1896. }
  1897. } while ((USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
  1898. }
  1899. else
  1900. {
  1901. USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  1902. }
  1903. }
  1904. return HAL_OK;
  1905. }
  1906. /**
  1907. * @brief Initiate Do Ping protocol
  1908. * @param USBx Selected device
  1909. * @param hc_num Host Channel number
  1910. * This parameter can be a value from 1 to 15
  1911. * @retval HAL state
  1912. */
  1913. HAL_StatusTypeDef USB_DoPing(const USB_OTG_GlobalTypeDef *USBx, uint8_t ch_num)
  1914. {
  1915. uint32_t USBx_BASE = (uint32_t)USBx;
  1916. uint32_t chnum = (uint32_t)ch_num;
  1917. uint32_t num_packets = 1U;
  1918. uint32_t tmpreg;
  1919. USBx_HC(chnum)->HCTSIZ = ((num_packets << 19) & USB_OTG_HCTSIZ_PKTCNT) |
  1920. USB_OTG_HCTSIZ_DOPING;
  1921. /* Set host channel enable */
  1922. tmpreg = USBx_HC(chnum)->HCCHAR;
  1923. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  1924. tmpreg |= USB_OTG_HCCHAR_CHENA;
  1925. USBx_HC(chnum)->HCCHAR = tmpreg;
  1926. return HAL_OK;
  1927. }
  1928. /**
  1929. * @brief Stop Host Core
  1930. * @param USBx Selected device
  1931. * @retval HAL state
  1932. */
  1933. HAL_StatusTypeDef USB_StopHost(USB_OTG_GlobalTypeDef *USBx)
  1934. {
  1935. HAL_StatusTypeDef ret = HAL_OK;
  1936. uint32_t USBx_BASE = (uint32_t)USBx;
  1937. __IO uint32_t count = 0U;
  1938. uint32_t value;
  1939. uint32_t i;
  1940. (void)USB_DisableGlobalInt(USBx);
  1941. /* Flush USB FIFO */
  1942. if (USB_FlushTxFifo(USBx, 0x10U) != HAL_OK) /* all Tx FIFOs */
  1943. {
  1944. ret = HAL_ERROR;
  1945. }
  1946. if (USB_FlushRxFifo(USBx) != HAL_OK)
  1947. {
  1948. ret = HAL_ERROR;
  1949. }
  1950. /* Flush out any leftover queued requests. */
  1951. for (i = 0U; i <= 15U; i++)
  1952. {
  1953. value = USBx_HC(i)->HCCHAR;
  1954. value |= USB_OTG_HCCHAR_CHDIS;
  1955. value &= ~USB_OTG_HCCHAR_CHENA;
  1956. value &= ~USB_OTG_HCCHAR_EPDIR;
  1957. USBx_HC(i)->HCCHAR = value;
  1958. }
  1959. /* Halt all channels to put them into a known state. */
  1960. for (i = 0U; i <= 15U; i++)
  1961. {
  1962. value = USBx_HC(i)->HCCHAR;
  1963. value |= USB_OTG_HCCHAR_CHDIS;
  1964. value |= USB_OTG_HCCHAR_CHENA;
  1965. value &= ~USB_OTG_HCCHAR_EPDIR;
  1966. USBx_HC(i)->HCCHAR = value;
  1967. do
  1968. {
  1969. count++;
  1970. if (count > 1000U)
  1971. {
  1972. break;
  1973. }
  1974. } while ((USBx_HC(i)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
  1975. }
  1976. /* Clear any pending Host interrupts */
  1977. USBx_HOST->HAINT = CLEAR_INTERRUPT_MASK;
  1978. USBx->GINTSTS = CLEAR_INTERRUPT_MASK;
  1979. (void)USB_EnableGlobalInt(USBx);
  1980. return ret;
  1981. }
  1982. /**
  1983. * @brief USB_ActivateRemoteWakeup active remote wakeup signalling
  1984. * @param USBx Selected device
  1985. * @retval HAL status
  1986. */
  1987. HAL_StatusTypeDef USB_ActivateRemoteWakeup(const USB_OTG_GlobalTypeDef *USBx)
  1988. {
  1989. uint32_t USBx_BASE = (uint32_t)USBx;
  1990. if ((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
  1991. {
  1992. /* active Remote wakeup signalling */
  1993. USBx_DEVICE->DCTL |= USB_OTG_DCTL_RWUSIG;
  1994. }
  1995. return HAL_OK;
  1996. }
  1997. /**
  1998. * @brief USB_DeActivateRemoteWakeup de-active remote wakeup signalling
  1999. * @param USBx Selected device
  2000. * @retval HAL status
  2001. */
  2002. HAL_StatusTypeDef USB_DeActivateRemoteWakeup(const USB_OTG_GlobalTypeDef *USBx)
  2003. {
  2004. uint32_t USBx_BASE = (uint32_t)USBx;
  2005. /* active Remote wakeup signalling */
  2006. USBx_DEVICE->DCTL &= ~(USB_OTG_DCTL_RWUSIG);
  2007. return HAL_OK;
  2008. }
  2009. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  2010. /**
  2011. * @}
  2012. */
  2013. /**
  2014. * @}
  2015. */
  2016. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  2017. #endif /* defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED) */
  2018. /**
  2019. * @}
  2020. */