stm32f7xx_ll_usb.c 65 KB

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