stm32f7xx_ll_usb.c 54 KB

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