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.
 
 
 

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