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.
 
 
 

1997 lines
54 KiB

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