stm32h7xx_ll_usb.c 60 KB

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