stm32f1xx_ll_usb.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_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) 2016 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_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 "stm32f1xx_hal.h"
  38. /** @addtogroup STM32F1xx_LL_USB_DRIVER
  39. * @{
  40. */
  41. #if defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED)
  42. #if defined (USB) || defined (USB_OTG_FS)
  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)
  50. static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx);
  51. /* Exported functions --------------------------------------------------------*/
  52. /** @defgroup USB_LL_Exported_Functions USB Low Layer Exported Functions
  53. * @{
  54. */
  55. /** @defgroup USB_LL_Exported_Functions_Group1 Initialization/de-initialization functions
  56. * @brief Initialization and Configuration functions
  57. *
  58. @verbatim
  59. ===============================================================================
  60. ##### Initialization/de-initialization functions #####
  61. ===============================================================================
  62. @endverbatim
  63. * @{
  64. */
  65. /**
  66. * @brief Initializes the USB Core
  67. * @param USBx USB Instance
  68. * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
  69. * the configuration information for the specified USBx peripheral.
  70. * @retval HAL status
  71. */
  72. HAL_StatusTypeDef USB_CoreInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
  73. {
  74. HAL_StatusTypeDef ret;
  75. /* Select FS Embedded PHY */
  76. USBx->GUSBCFG |= USB_OTG_GUSBCFG_PHYSEL;
  77. /* Reset after a PHY select */
  78. ret = USB_CoreReset(USBx);
  79. /* Activate the USB Transceiver */
  80. USBx->GCCFG |= USB_OTG_GCCFG_PWRDWN;
  81. return ret;
  82. }
  83. /**
  84. * @brief Set the USB turnaround time
  85. * @param USBx USB Instance
  86. * @param hclk: AHB clock frequency
  87. * @retval USB turnaround time In PHY Clocks number
  88. */
  89. HAL_StatusTypeDef USB_SetTurnaroundTime(USB_OTG_GlobalTypeDef *USBx,
  90. uint32_t hclk, uint8_t speed)
  91. {
  92. uint32_t UsbTrd;
  93. /* The USBTRD is configured according to the tables below, depending on AHB frequency
  94. used by application. In the low AHB frequency range it is used to stretch enough the USB response
  95. time to IN tokens, the USB turnaround time, so to compensate for the longer AHB read access
  96. latency to the Data FIFO */
  97. if (speed == USBD_FS_SPEED)
  98. {
  99. if ((hclk >= 14200000U) && (hclk < 15000000U))
  100. {
  101. /* hclk Clock Range between 14.2-15 MHz */
  102. UsbTrd = 0xFU;
  103. }
  104. else if ((hclk >= 15000000U) && (hclk < 16000000U))
  105. {
  106. /* hclk Clock Range between 15-16 MHz */
  107. UsbTrd = 0xEU;
  108. }
  109. else if ((hclk >= 16000000U) && (hclk < 17200000U))
  110. {
  111. /* hclk Clock Range between 16-17.2 MHz */
  112. UsbTrd = 0xDU;
  113. }
  114. else if ((hclk >= 17200000U) && (hclk < 18500000U))
  115. {
  116. /* hclk Clock Range between 17.2-18.5 MHz */
  117. UsbTrd = 0xCU;
  118. }
  119. else if ((hclk >= 18500000U) && (hclk < 20000000U))
  120. {
  121. /* hclk Clock Range between 18.5-20 MHz */
  122. UsbTrd = 0xBU;
  123. }
  124. else if ((hclk >= 20000000U) && (hclk < 21800000U))
  125. {
  126. /* hclk Clock Range between 20-21.8 MHz */
  127. UsbTrd = 0xAU;
  128. }
  129. else if ((hclk >= 21800000U) && (hclk < 24000000U))
  130. {
  131. /* hclk Clock Range between 21.8-24 MHz */
  132. UsbTrd = 0x9U;
  133. }
  134. else if ((hclk >= 24000000U) && (hclk < 27700000U))
  135. {
  136. /* hclk Clock Range between 24-27.7 MHz */
  137. UsbTrd = 0x8U;
  138. }
  139. else if ((hclk >= 27700000U) && (hclk < 32000000U))
  140. {
  141. /* hclk Clock Range between 27.7-32 MHz */
  142. UsbTrd = 0x7U;
  143. }
  144. else /* if(hclk >= 32000000) */
  145. {
  146. /* hclk Clock Range between 32-200 MHz */
  147. UsbTrd = 0x6U;
  148. }
  149. }
  150. else
  151. {
  152. UsbTrd = USBD_DEFAULT_TRDT_VALUE;
  153. }
  154. USBx->GUSBCFG &= ~USB_OTG_GUSBCFG_TRDT;
  155. USBx->GUSBCFG |= (uint32_t)((UsbTrd << 10) & USB_OTG_GUSBCFG_TRDT);
  156. return HAL_OK;
  157. }
  158. /**
  159. * @brief USB_EnableGlobalInt
  160. * Enables the controller's Global Int in the AHB Config reg
  161. * @param USBx Selected device
  162. * @retval HAL status
  163. */
  164. HAL_StatusTypeDef USB_EnableGlobalInt(USB_OTG_GlobalTypeDef *USBx)
  165. {
  166. USBx->GAHBCFG |= USB_OTG_GAHBCFG_GINT;
  167. return HAL_OK;
  168. }
  169. /**
  170. * @brief USB_DisableGlobalInt
  171. * Disable the controller's Global Int in the AHB Config reg
  172. * @param USBx Selected device
  173. * @retval HAL status
  174. */
  175. HAL_StatusTypeDef USB_DisableGlobalInt(USB_OTG_GlobalTypeDef *USBx)
  176. {
  177. USBx->GAHBCFG &= ~USB_OTG_GAHBCFG_GINT;
  178. return HAL_OK;
  179. }
  180. /**
  181. * @brief USB_SetCurrentMode Set functional mode
  182. * @param USBx Selected device
  183. * @param mode current core mode
  184. * This parameter can be one of these values:
  185. * @arg USB_DEVICE_MODE Peripheral mode
  186. * @arg USB_HOST_MODE Host mode
  187. * @retval HAL status
  188. */
  189. HAL_StatusTypeDef USB_SetCurrentMode(USB_OTG_GlobalTypeDef *USBx, USB_ModeTypeDef mode)
  190. {
  191. uint32_t ms = 0U;
  192. USBx->GUSBCFG &= ~(USB_OTG_GUSBCFG_FHMOD | USB_OTG_GUSBCFG_FDMOD);
  193. if (mode == USB_HOST_MODE)
  194. {
  195. USBx->GUSBCFG |= USB_OTG_GUSBCFG_FHMOD;
  196. do
  197. {
  198. HAL_Delay(1U);
  199. ms++;
  200. } while ((USB_GetMode(USBx) != (uint32_t)USB_HOST_MODE) && (ms < 50U));
  201. }
  202. else if (mode == USB_DEVICE_MODE)
  203. {
  204. USBx->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD;
  205. do
  206. {
  207. HAL_Delay(1U);
  208. ms++;
  209. } while ((USB_GetMode(USBx) != (uint32_t)USB_DEVICE_MODE) && (ms < 50U));
  210. }
  211. else
  212. {
  213. return HAL_ERROR;
  214. }
  215. if (ms == 50U)
  216. {
  217. return HAL_ERROR;
  218. }
  219. return HAL_OK;
  220. }
  221. /**
  222. * @brief USB_DevInit Initializes the USB_OTG controller registers
  223. * for device mode
  224. * @param USBx Selected device
  225. * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
  226. * the configuration information for the specified USBx peripheral.
  227. * @retval HAL status
  228. */
  229. HAL_StatusTypeDef USB_DevInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
  230. {
  231. HAL_StatusTypeDef ret = HAL_OK;
  232. uint32_t USBx_BASE = (uint32_t)USBx;
  233. uint32_t i;
  234. for (i = 0U; i < 15U; i++)
  235. {
  236. USBx->DIEPTXF[i] = 0U;
  237. }
  238. /* Enable HW VBUS sensing */
  239. USBx->GCCFG |= USB_OTG_GCCFG_VBUSBSEN;
  240. /* Restart the Phy Clock */
  241. USBx_PCGCCTL = 0U;
  242. /* Set Core speed to Full speed mode */
  243. (void)USB_SetDevSpeed(USBx, USB_OTG_SPEED_FULL);
  244. /* Flush the FIFOs */
  245. if (USB_FlushTxFifo(USBx, 0x10U) != HAL_OK) /* all Tx FIFOs */
  246. {
  247. ret = HAL_ERROR;
  248. }
  249. if (USB_FlushRxFifo(USBx) != HAL_OK)
  250. {
  251. ret = HAL_ERROR;
  252. }
  253. /* Clear all pending Device Interrupts */
  254. USBx_DEVICE->DIEPMSK = 0U;
  255. USBx_DEVICE->DOEPMSK = 0U;
  256. USBx_DEVICE->DAINTMSK = 0U;
  257. for (i = 0U; i < cfg.dev_endpoints; i++)
  258. {
  259. if ((USBx_INEP(i)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
  260. {
  261. if (i == 0U)
  262. {
  263. USBx_INEP(i)->DIEPCTL = USB_OTG_DIEPCTL_SNAK;
  264. }
  265. else
  266. {
  267. USBx_INEP(i)->DIEPCTL = USB_OTG_DIEPCTL_EPDIS | USB_OTG_DIEPCTL_SNAK;
  268. }
  269. }
  270. else
  271. {
  272. USBx_INEP(i)->DIEPCTL = 0U;
  273. }
  274. USBx_INEP(i)->DIEPTSIZ = 0U;
  275. USBx_INEP(i)->DIEPINT = 0xFB7FU;
  276. }
  277. for (i = 0U; i < cfg.dev_endpoints; i++)
  278. {
  279. if ((USBx_OUTEP(i)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
  280. {
  281. if (i == 0U)
  282. {
  283. USBx_OUTEP(i)->DOEPCTL = USB_OTG_DOEPCTL_SNAK;
  284. }
  285. else
  286. {
  287. USBx_OUTEP(i)->DOEPCTL = USB_OTG_DOEPCTL_EPDIS | USB_OTG_DOEPCTL_SNAK;
  288. }
  289. }
  290. else
  291. {
  292. USBx_OUTEP(i)->DOEPCTL = 0U;
  293. }
  294. USBx_OUTEP(i)->DOEPTSIZ = 0U;
  295. USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
  296. }
  297. USBx_DEVICE->DIEPMSK &= ~(USB_OTG_DIEPMSK_TXFURM);
  298. /* Disable all interrupts. */
  299. USBx->GINTMSK = 0U;
  300. /* Clear any pending interrupts */
  301. USBx->GINTSTS = 0xBFFFFFFFU;
  302. /* Enable the common interrupts */
  303. USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
  304. /* Enable interrupts matching to the Device mode ONLY */
  305. USBx->GINTMSK |= USB_OTG_GINTMSK_USBSUSPM | USB_OTG_GINTMSK_USBRST |
  306. USB_OTG_GINTMSK_ENUMDNEM | USB_OTG_GINTMSK_IEPINT |
  307. USB_OTG_GINTMSK_OEPINT | USB_OTG_GINTMSK_IISOIXFRM |
  308. USB_OTG_GINTMSK_PXFRM_IISOOXFRM | USB_OTG_GINTMSK_WUIM;
  309. if (cfg.Sof_enable != 0U)
  310. {
  311. USBx->GINTMSK |= USB_OTG_GINTMSK_SOFM;
  312. }
  313. if (cfg.vbus_sensing_enable == 1U)
  314. {
  315. USBx->GINTMSK |= (USB_OTG_GINTMSK_SRQIM | USB_OTG_GINTMSK_OTGINT);
  316. }
  317. return ret;
  318. }
  319. /**
  320. * @brief USB_FlushTxFifo Flush a Tx FIFO
  321. * @param USBx Selected device
  322. * @param num FIFO number
  323. * This parameter can be a value from 1 to 15
  324. 15 means Flush all Tx FIFOs
  325. * @retval HAL status
  326. */
  327. HAL_StatusTypeDef USB_FlushTxFifo(USB_OTG_GlobalTypeDef *USBx, uint32_t num)
  328. {
  329. __IO uint32_t count = 0U;
  330. /* Wait for AHB master IDLE state. */
  331. do
  332. {
  333. count++;
  334. if (count > 200000U)
  335. {
  336. return HAL_TIMEOUT;
  337. }
  338. } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
  339. /* Flush TX Fifo */
  340. count = 0U;
  341. USBx->GRSTCTL = (USB_OTG_GRSTCTL_TXFFLSH | (num << 6));
  342. do
  343. {
  344. count++;
  345. if (count > 200000U)
  346. {
  347. return HAL_TIMEOUT;
  348. }
  349. } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_TXFFLSH) == USB_OTG_GRSTCTL_TXFFLSH);
  350. return HAL_OK;
  351. }
  352. /**
  353. * @brief USB_FlushRxFifo Flush Rx FIFO
  354. * @param USBx Selected device
  355. * @retval HAL status
  356. */
  357. HAL_StatusTypeDef USB_FlushRxFifo(USB_OTG_GlobalTypeDef *USBx)
  358. {
  359. __IO uint32_t count = 0U;
  360. /* Wait for AHB master IDLE state. */
  361. do
  362. {
  363. count++;
  364. if (count > 200000U)
  365. {
  366. return HAL_TIMEOUT;
  367. }
  368. } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
  369. /* Flush RX Fifo */
  370. count = 0U;
  371. USBx->GRSTCTL = USB_OTG_GRSTCTL_RXFFLSH;
  372. do
  373. {
  374. count++;
  375. if (count > 200000U)
  376. {
  377. return HAL_TIMEOUT;
  378. }
  379. } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_RXFFLSH) == USB_OTG_GRSTCTL_RXFFLSH);
  380. return HAL_OK;
  381. }
  382. /**
  383. * @brief USB_SetDevSpeed Initializes the DevSpd field of DCFG register
  384. * depending the PHY type and the enumeration speed of the device.
  385. * @param USBx Selected device
  386. * @param speed device speed
  387. * This parameter can be one of these values:
  388. * @arg USB_OTG_SPEED_FULL: Full speed mode
  389. * @retval Hal status
  390. */
  391. HAL_StatusTypeDef USB_SetDevSpeed(USB_OTG_GlobalTypeDef *USBx, uint8_t speed)
  392. {
  393. uint32_t USBx_BASE = (uint32_t)USBx;
  394. USBx_DEVICE->DCFG |= speed;
  395. return HAL_OK;
  396. }
  397. /**
  398. * @brief USB_GetDevSpeed Return the Dev Speed
  399. * @param USBx Selected device
  400. * @retval speed device speed
  401. * This parameter can be one of these values:
  402. * @arg USBD_FS_SPEED: Full speed mode
  403. */
  404. uint8_t USB_GetDevSpeed(USB_OTG_GlobalTypeDef *USBx)
  405. {
  406. uint32_t USBx_BASE = (uint32_t)USBx;
  407. uint8_t speed;
  408. uint32_t DevEnumSpeed = USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD;
  409. if ((DevEnumSpeed == DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ) ||
  410. (DevEnumSpeed == DSTS_ENUMSPD_FS_PHY_48MHZ))
  411. {
  412. speed = USBD_FS_SPEED;
  413. }
  414. else
  415. {
  416. speed = 0xFU;
  417. }
  418. return speed;
  419. }
  420. /**
  421. * @brief Activate and configure an endpoint
  422. * @param USBx Selected device
  423. * @param ep pointer to endpoint structure
  424. * @retval HAL status
  425. */
  426. HAL_StatusTypeDef USB_ActivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
  427. {
  428. uint32_t USBx_BASE = (uint32_t)USBx;
  429. uint32_t epnum = (uint32_t)ep->num;
  430. if (ep->is_in == 1U)
  431. {
  432. USBx_DEVICE->DAINTMSK |= USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK));
  433. if ((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_USBAEP) == 0U)
  434. {
  435. USBx_INEP(epnum)->DIEPCTL |= (ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ) |
  436. ((uint32_t)ep->type << 18) | (epnum << 22) |
  437. USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  438. USB_OTG_DIEPCTL_USBAEP;
  439. }
  440. }
  441. else
  442. {
  443. USBx_DEVICE->DAINTMSK |= USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16);
  444. if (((USBx_OUTEP(epnum)->DOEPCTL) & USB_OTG_DOEPCTL_USBAEP) == 0U)
  445. {
  446. USBx_OUTEP(epnum)->DOEPCTL |= (ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ) |
  447. ((uint32_t)ep->type << 18) |
  448. USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  449. USB_OTG_DOEPCTL_USBAEP;
  450. }
  451. }
  452. return HAL_OK;
  453. }
  454. /**
  455. * @brief Activate and configure a dedicated endpoint
  456. * @param USBx Selected device
  457. * @param ep pointer to endpoint structure
  458. * @retval HAL status
  459. */
  460. HAL_StatusTypeDef USB_ActivateDedicatedEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
  461. {
  462. uint32_t USBx_BASE = (uint32_t)USBx;
  463. uint32_t epnum = (uint32_t)ep->num;
  464. /* Read DEPCTLn register */
  465. if (ep->is_in == 1U)
  466. {
  467. if (((USBx_INEP(epnum)->DIEPCTL) & USB_OTG_DIEPCTL_USBAEP) == 0U)
  468. {
  469. USBx_INEP(epnum)->DIEPCTL |= (ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ) |
  470. ((uint32_t)ep->type << 18) | (epnum << 22) |
  471. USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  472. USB_OTG_DIEPCTL_USBAEP;
  473. }
  474. USBx_DEVICE->DEACHMSK |= USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK));
  475. }
  476. else
  477. {
  478. if (((USBx_OUTEP(epnum)->DOEPCTL) & USB_OTG_DOEPCTL_USBAEP) == 0U)
  479. {
  480. USBx_OUTEP(epnum)->DOEPCTL |= (ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ) |
  481. ((uint32_t)ep->type << 18) | (epnum << 22) |
  482. USB_OTG_DOEPCTL_USBAEP;
  483. }
  484. USBx_DEVICE->DEACHMSK |= USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16);
  485. }
  486. return HAL_OK;
  487. }
  488. /**
  489. * @brief De-activate and de-initialize an endpoint
  490. * @param USBx Selected device
  491. * @param ep pointer to endpoint structure
  492. * @retval HAL status
  493. */
  494. HAL_StatusTypeDef USB_DeactivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
  495. {
  496. uint32_t USBx_BASE = (uint32_t)USBx;
  497. uint32_t epnum = (uint32_t)ep->num;
  498. /* Read DEPCTLn register */
  499. if (ep->is_in == 1U)
  500. {
  501. if ((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
  502. {
  503. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SNAK;
  504. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_EPDIS;
  505. }
  506. USBx_DEVICE->DEACHMSK &= ~(USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK)));
  507. USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK)));
  508. USBx_INEP(epnum)->DIEPCTL &= ~(USB_OTG_DIEPCTL_USBAEP |
  509. USB_OTG_DIEPCTL_MPSIZ |
  510. USB_OTG_DIEPCTL_TXFNUM |
  511. USB_OTG_DIEPCTL_SD0PID_SEVNFRM |
  512. USB_OTG_DIEPCTL_EPTYP);
  513. }
  514. else
  515. {
  516. if ((USBx_OUTEP(epnum)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
  517. {
  518. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
  519. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_EPDIS;
  520. }
  521. USBx_DEVICE->DEACHMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16));
  522. USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16));
  523. USBx_OUTEP(epnum)->DOEPCTL &= ~(USB_OTG_DOEPCTL_USBAEP |
  524. USB_OTG_DOEPCTL_MPSIZ |
  525. USB_OTG_DOEPCTL_SD0PID_SEVNFRM |
  526. USB_OTG_DOEPCTL_EPTYP);
  527. }
  528. return HAL_OK;
  529. }
  530. /**
  531. * @brief De-activate and de-initialize a dedicated endpoint
  532. * @param USBx Selected device
  533. * @param ep pointer to endpoint structure
  534. * @retval HAL status
  535. */
  536. HAL_StatusTypeDef USB_DeactivateDedicatedEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
  537. {
  538. uint32_t USBx_BASE = (uint32_t)USBx;
  539. uint32_t epnum = (uint32_t)ep->num;
  540. /* Read DEPCTLn register */
  541. if (ep->is_in == 1U)
  542. {
  543. if ((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
  544. {
  545. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SNAK;
  546. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_EPDIS;
  547. }
  548. USBx_INEP(epnum)->DIEPCTL &= ~ USB_OTG_DIEPCTL_USBAEP;
  549. USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_IEPM & (uint32_t)(1UL << (ep->num & EP_ADDR_MSK)));
  550. }
  551. else
  552. {
  553. if ((USBx_OUTEP(epnum)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
  554. {
  555. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SNAK;
  556. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_EPDIS;
  557. }
  558. USBx_OUTEP(epnum)->DOEPCTL &= ~USB_OTG_DOEPCTL_USBAEP;
  559. USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((uint32_t)(1UL << (ep->num & EP_ADDR_MSK)) << 16));
  560. }
  561. return HAL_OK;
  562. }
  563. /**
  564. * @brief USB_EPStartXfer : setup and starts a transfer over an EP
  565. * @param USBx Selected device
  566. * @param ep pointer to endpoint structure
  567. * @retval HAL status
  568. */
  569. HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
  570. {
  571. uint32_t USBx_BASE = (uint32_t)USBx;
  572. uint32_t epnum = (uint32_t)ep->num;
  573. uint16_t pktcnt;
  574. /* IN endpoint */
  575. if (ep->is_in == 1U)
  576. {
  577. /* Zero Length Packet? */
  578. if (ep->xfer_len == 0U)
  579. {
  580. USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
  581. USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1U << 19));
  582. USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
  583. }
  584. else
  585. {
  586. /* Program the transfer size and packet count
  587. * as follows: xfersize = N * maxpacket +
  588. * short_packet pktcnt = N + (short_packet
  589. * exist ? 1 : 0)
  590. */
  591. USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
  592. USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
  593. if (epnum == 0U)
  594. {
  595. if (ep->xfer_len > ep->maxpacket)
  596. {
  597. ep->xfer_len = ep->maxpacket;
  598. }
  599. USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1U << 19));
  600. }
  601. else
  602. {
  603. USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT &
  604. (((ep->xfer_len + ep->maxpacket - 1U) / ep->maxpacket) << 19));
  605. }
  606. USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_XFRSIZ & ep->xfer_len);
  607. if (ep->type == EP_TYPE_ISOC)
  608. {
  609. USBx_INEP(epnum)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_MULCNT);
  610. USBx_INEP(epnum)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_MULCNT & (1U << 29));
  611. }
  612. }
  613. /* EP enable, IN data in FIFO */
  614. USBx_INEP(epnum)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
  615. if (ep->type != EP_TYPE_ISOC)
  616. {
  617. /* Enable the Tx FIFO Empty Interrupt for this EP */
  618. if (ep->xfer_len > 0U)
  619. {
  620. USBx_DEVICE->DIEPEMPMSK |= 1UL << (ep->num & EP_ADDR_MSK);
  621. }
  622. }
  623. else
  624. {
  625. if ((USBx_DEVICE->DSTS & (1U << 8)) == 0U)
  626. {
  627. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SODDFRM;
  628. }
  629. else
  630. {
  631. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM;
  632. }
  633. (void)USB_WritePacket(USBx, ep->xfer_buff, ep->num, (uint16_t)ep->xfer_len);
  634. }
  635. }
  636. else /* OUT endpoint */
  637. {
  638. /* Program the transfer size and packet count as follows:
  639. * pktcnt = N
  640. * xfersize = N * maxpacket
  641. */
  642. USBx_OUTEP(epnum)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_XFRSIZ);
  643. USBx_OUTEP(epnum)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_PKTCNT);
  644. if (epnum == 0U)
  645. {
  646. if (ep->xfer_len > 0U)
  647. {
  648. ep->xfer_len = ep->maxpacket;
  649. }
  650. /* Store transfer size, for EP0 this is equal to endpoint max packet size */
  651. ep->xfer_size = ep->maxpacket;
  652. USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & ep->xfer_size);
  653. USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19));
  654. }
  655. else
  656. {
  657. if (ep->xfer_len == 0U)
  658. {
  659. USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & ep->maxpacket);
  660. USBx_OUTEP(epnum)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19));
  661. }
  662. else
  663. {
  664. pktcnt = (uint16_t)((ep->xfer_len + ep->maxpacket - 1U) / ep->maxpacket);
  665. ep->xfer_size = ep->maxpacket * pktcnt;
  666. USBx_OUTEP(epnum)->DOEPTSIZ |= USB_OTG_DOEPTSIZ_PKTCNT & ((uint32_t)pktcnt << 19);
  667. USBx_OUTEP(epnum)->DOEPTSIZ |= USB_OTG_DOEPTSIZ_XFRSIZ & ep->xfer_size;
  668. }
  669. }
  670. if (ep->type == EP_TYPE_ISOC)
  671. {
  672. if ((USBx_DEVICE->DSTS & (1U << 8)) == 0U)
  673. {
  674. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SODDFRM;
  675. }
  676. else
  677. {
  678. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM;
  679. }
  680. }
  681. /* EP enable */
  682. USBx_OUTEP(epnum)->DOEPCTL |= (USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
  683. }
  684. return HAL_OK;
  685. }
  686. /**
  687. * @brief USB_EPStoptXfer Stop transfer on an EP
  688. * @param USBx usb device instance
  689. * @param ep pointer to endpoint structure
  690. * @retval HAL status
  691. */
  692. HAL_StatusTypeDef USB_EPStopXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
  693. {
  694. __IO uint32_t count = 0U;
  695. HAL_StatusTypeDef ret = HAL_OK;
  696. uint32_t USBx_BASE = (uint32_t)USBx;
  697. /* IN endpoint */
  698. if (ep->is_in == 1U)
  699. {
  700. /* EP enable, IN data in FIFO */
  701. if (((USBx_INEP(ep->num)->DIEPCTL) & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
  702. {
  703. USBx_INEP(ep->num)->DIEPCTL |= (USB_OTG_DIEPCTL_SNAK);
  704. USBx_INEP(ep->num)->DIEPCTL |= (USB_OTG_DIEPCTL_EPDIS);
  705. do
  706. {
  707. count++;
  708. if (count > 10000U)
  709. {
  710. ret = HAL_ERROR;
  711. break;
  712. }
  713. } while (((USBx_INEP(ep->num)->DIEPCTL) & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA);
  714. }
  715. }
  716. else /* OUT endpoint */
  717. {
  718. if (((USBx_OUTEP(ep->num)->DOEPCTL) & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
  719. {
  720. USBx_OUTEP(ep->num)->DOEPCTL |= (USB_OTG_DOEPCTL_SNAK);
  721. USBx_OUTEP(ep->num)->DOEPCTL |= (USB_OTG_DOEPCTL_EPDIS);
  722. do
  723. {
  724. count++;
  725. if (count > 10000U)
  726. {
  727. ret = HAL_ERROR;
  728. break;
  729. }
  730. } while (((USBx_OUTEP(ep->num)->DOEPCTL) & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA);
  731. }
  732. }
  733. return ret;
  734. }
  735. /**
  736. * @brief USB_WritePacket : Writes a packet into the Tx FIFO associated
  737. * with the EP/channel
  738. * @param USBx Selected device
  739. * @param src pointer to source buffer
  740. * @param ch_ep_num endpoint or host channel number
  741. * @param len Number of bytes to write
  742. * @retval HAL status
  743. */
  744. HAL_StatusTypeDef USB_WritePacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *src,
  745. uint8_t ch_ep_num, uint16_t len)
  746. {
  747. uint32_t USBx_BASE = (uint32_t)USBx;
  748. uint8_t *pSrc = src;
  749. uint32_t count32b;
  750. uint32_t i;
  751. count32b = ((uint32_t)len + 3U) / 4U;
  752. for (i = 0U; i < count32b; i++)
  753. {
  754. USBx_DFIFO((uint32_t)ch_ep_num) = __UNALIGNED_UINT32_READ(pSrc);
  755. pSrc++;
  756. pSrc++;
  757. pSrc++;
  758. pSrc++;
  759. }
  760. return HAL_OK;
  761. }
  762. /**
  763. * @brief USB_ReadPacket : read a packet from the RX FIFO
  764. * @param USBx Selected device
  765. * @param dest source pointer
  766. * @param len Number of bytes to read
  767. * @retval pointer to destination buffer
  768. */
  769. void *USB_ReadPacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *dest, uint16_t len)
  770. {
  771. uint32_t USBx_BASE = (uint32_t)USBx;
  772. uint8_t *pDest = dest;
  773. uint32_t pData;
  774. uint32_t i;
  775. uint32_t count32b = (uint32_t)len >> 2U;
  776. uint16_t remaining_bytes = len % 4U;
  777. for (i = 0U; i < count32b; i++)
  778. {
  779. __UNALIGNED_UINT32_WRITE(pDest, USBx_DFIFO(0U));
  780. pDest++;
  781. pDest++;
  782. pDest++;
  783. pDest++;
  784. }
  785. /* When Number of data is not word aligned, read the remaining byte */
  786. if (remaining_bytes != 0U)
  787. {
  788. i = 0U;
  789. __UNALIGNED_UINT32_WRITE(&pData, USBx_DFIFO(0U));
  790. do
  791. {
  792. *(uint8_t *)pDest = (uint8_t)(pData >> (8U * (uint8_t)(i)));
  793. i++;
  794. pDest++;
  795. remaining_bytes--;
  796. } while (remaining_bytes != 0U);
  797. }
  798. return ((void *)pDest);
  799. }
  800. /**
  801. * @brief USB_EPSetStall : set a stall condition over an EP
  802. * @param USBx Selected device
  803. * @param ep pointer to endpoint structure
  804. * @retval HAL status
  805. */
  806. HAL_StatusTypeDef USB_EPSetStall(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
  807. {
  808. uint32_t USBx_BASE = (uint32_t)USBx;
  809. uint32_t epnum = (uint32_t)ep->num;
  810. if (ep->is_in == 1U)
  811. {
  812. if (((USBx_INEP(epnum)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == 0U) && (epnum != 0U))
  813. {
  814. USBx_INEP(epnum)->DIEPCTL &= ~(USB_OTG_DIEPCTL_EPDIS);
  815. }
  816. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_STALL;
  817. }
  818. else
  819. {
  820. if (((USBx_OUTEP(epnum)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == 0U) && (epnum != 0U))
  821. {
  822. USBx_OUTEP(epnum)->DOEPCTL &= ~(USB_OTG_DOEPCTL_EPDIS);
  823. }
  824. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_STALL;
  825. }
  826. return HAL_OK;
  827. }
  828. /**
  829. * @brief USB_EPClearStall : Clear a stall condition over an EP
  830. * @param USBx Selected device
  831. * @param ep pointer to endpoint structure
  832. * @retval HAL status
  833. */
  834. HAL_StatusTypeDef USB_EPClearStall(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
  835. {
  836. uint32_t USBx_BASE = (uint32_t)USBx;
  837. uint32_t epnum = (uint32_t)ep->num;
  838. if (ep->is_in == 1U)
  839. {
  840. USBx_INEP(epnum)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
  841. if ((ep->type == EP_TYPE_INTR) || (ep->type == EP_TYPE_BULK))
  842. {
  843. USBx_INEP(epnum)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM; /* DATA0 */
  844. }
  845. }
  846. else
  847. {
  848. USBx_OUTEP(epnum)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
  849. if ((ep->type == EP_TYPE_INTR) || (ep->type == EP_TYPE_BULK))
  850. {
  851. USBx_OUTEP(epnum)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM; /* DATA0 */
  852. }
  853. }
  854. return HAL_OK;
  855. }
  856. /**
  857. * @brief USB_StopDevice : Stop the usb device mode
  858. * @param USBx Selected device
  859. * @retval HAL status
  860. */
  861. HAL_StatusTypeDef USB_StopDevice(USB_OTG_GlobalTypeDef *USBx)
  862. {
  863. HAL_StatusTypeDef ret;
  864. uint32_t USBx_BASE = (uint32_t)USBx;
  865. uint32_t i;
  866. /* Clear Pending interrupt */
  867. for (i = 0U; i < 15U; i++)
  868. {
  869. USBx_INEP(i)->DIEPINT = 0xFB7FU;
  870. USBx_OUTEP(i)->DOEPINT = 0xFB7FU;
  871. }
  872. /* Clear interrupt masks */
  873. USBx_DEVICE->DIEPMSK = 0U;
  874. USBx_DEVICE->DOEPMSK = 0U;
  875. USBx_DEVICE->DAINTMSK = 0U;
  876. /* Flush the FIFO */
  877. ret = USB_FlushRxFifo(USBx);
  878. if (ret != HAL_OK)
  879. {
  880. return ret;
  881. }
  882. ret = USB_FlushTxFifo(USBx, 0x10U);
  883. if (ret != HAL_OK)
  884. {
  885. return ret;
  886. }
  887. return ret;
  888. }
  889. /**
  890. * @brief USB_SetDevAddress : Stop the usb device mode
  891. * @param USBx Selected device
  892. * @param address new device address to be assigned
  893. * This parameter can be a value from 0 to 255
  894. * @retval HAL status
  895. */
  896. HAL_StatusTypeDef USB_SetDevAddress(USB_OTG_GlobalTypeDef *USBx, uint8_t address)
  897. {
  898. uint32_t USBx_BASE = (uint32_t)USBx;
  899. USBx_DEVICE->DCFG &= ~(USB_OTG_DCFG_DAD);
  900. USBx_DEVICE->DCFG |= ((uint32_t)address << 4) & USB_OTG_DCFG_DAD;
  901. return HAL_OK;
  902. }
  903. /**
  904. * @brief USB_DevConnect : Connect the USB device by enabling Rpu
  905. * @param USBx Selected device
  906. * @retval HAL status
  907. */
  908. HAL_StatusTypeDef USB_DevConnect(USB_OTG_GlobalTypeDef *USBx)
  909. {
  910. uint32_t USBx_BASE = (uint32_t)USBx;
  911. /* In case phy is stopped, ensure to ungate and restore the phy CLK */
  912. USBx_PCGCCTL &= ~(USB_OTG_PCGCCTL_STOPCLK | USB_OTG_PCGCCTL_GATECLK);
  913. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_SDIS;
  914. return HAL_OK;
  915. }
  916. /**
  917. * @brief USB_DevDisconnect : Disconnect the USB device by disabling Rpu
  918. * @param USBx Selected device
  919. * @retval HAL status
  920. */
  921. HAL_StatusTypeDef USB_DevDisconnect(USB_OTG_GlobalTypeDef *USBx)
  922. {
  923. uint32_t USBx_BASE = (uint32_t)USBx;
  924. /* In case phy is stopped, ensure to ungate and restore the phy CLK */
  925. USBx_PCGCCTL &= ~(USB_OTG_PCGCCTL_STOPCLK | USB_OTG_PCGCCTL_GATECLK);
  926. USBx_DEVICE->DCTL |= USB_OTG_DCTL_SDIS;
  927. return HAL_OK;
  928. }
  929. /**
  930. * @brief USB_ReadInterrupts: return the global USB interrupt status
  931. * @param USBx Selected device
  932. * @retval USB Global Interrupt status
  933. */
  934. uint32_t USB_ReadInterrupts(USB_OTG_GlobalTypeDef const *USBx)
  935. {
  936. uint32_t tmpreg;
  937. tmpreg = USBx->GINTSTS;
  938. tmpreg &= USBx->GINTMSK;
  939. return tmpreg;
  940. }
  941. /**
  942. * @brief USB_ReadChInterrupts: return USB channel interrupt status
  943. * @param USBx Selected device
  944. * @param chnum Channel number
  945. * @retval USB Channel Interrupt status
  946. */
  947. uint32_t USB_ReadChInterrupts(USB_OTG_GlobalTypeDef *USBx, uint8_t chnum)
  948. {
  949. uint32_t USBx_BASE = (uint32_t)USBx;
  950. uint32_t tmpreg;
  951. tmpreg = USBx_HC(chnum)->HCINT;
  952. tmpreg &= USBx_HC(chnum)->HCINTMSK;
  953. return tmpreg;
  954. }
  955. /**
  956. * @brief USB_ReadDevAllOutEpInterrupt: return the USB device OUT endpoints interrupt status
  957. * @param USBx Selected device
  958. * @retval USB Device OUT EP interrupt status
  959. */
  960. uint32_t USB_ReadDevAllOutEpInterrupt(USB_OTG_GlobalTypeDef *USBx)
  961. {
  962. uint32_t USBx_BASE = (uint32_t)USBx;
  963. uint32_t tmpreg;
  964. tmpreg = USBx_DEVICE->DAINT;
  965. tmpreg &= USBx_DEVICE->DAINTMSK;
  966. return ((tmpreg & 0xffff0000U) >> 16);
  967. }
  968. /**
  969. * @brief USB_ReadDevAllInEpInterrupt: return the USB device IN endpoints interrupt status
  970. * @param USBx Selected device
  971. * @retval USB Device IN EP interrupt status
  972. */
  973. uint32_t USB_ReadDevAllInEpInterrupt(USB_OTG_GlobalTypeDef *USBx)
  974. {
  975. uint32_t USBx_BASE = (uint32_t)USBx;
  976. uint32_t tmpreg;
  977. tmpreg = USBx_DEVICE->DAINT;
  978. tmpreg &= USBx_DEVICE->DAINTMSK;
  979. return ((tmpreg & 0xFFFFU));
  980. }
  981. /**
  982. * @brief Returns Device OUT EP Interrupt register
  983. * @param USBx Selected device
  984. * @param epnum endpoint number
  985. * This parameter can be a value from 0 to 15
  986. * @retval Device OUT EP Interrupt register
  987. */
  988. uint32_t USB_ReadDevOutEPInterrupt(USB_OTG_GlobalTypeDef *USBx, uint8_t epnum)
  989. {
  990. uint32_t USBx_BASE = (uint32_t)USBx;
  991. uint32_t tmpreg;
  992. tmpreg = USBx_OUTEP((uint32_t)epnum)->DOEPINT;
  993. tmpreg &= USBx_DEVICE->DOEPMSK;
  994. return tmpreg;
  995. }
  996. /**
  997. * @brief Returns Device IN EP Interrupt register
  998. * @param USBx Selected device
  999. * @param epnum endpoint number
  1000. * This parameter can be a value from 0 to 15
  1001. * @retval Device IN EP Interrupt register
  1002. */
  1003. uint32_t USB_ReadDevInEPInterrupt(USB_OTG_GlobalTypeDef *USBx, uint8_t epnum)
  1004. {
  1005. uint32_t USBx_BASE = (uint32_t)USBx;
  1006. uint32_t tmpreg;
  1007. uint32_t msk;
  1008. uint32_t emp;
  1009. msk = USBx_DEVICE->DIEPMSK;
  1010. emp = USBx_DEVICE->DIEPEMPMSK;
  1011. msk |= ((emp >> (epnum & EP_ADDR_MSK)) & 0x1U) << 7;
  1012. tmpreg = USBx_INEP((uint32_t)epnum)->DIEPINT & msk;
  1013. return tmpreg;
  1014. }
  1015. /**
  1016. * @brief USB_ClearInterrupts: clear a USB interrupt
  1017. * @param USBx Selected device
  1018. * @param interrupt flag
  1019. * @retval None
  1020. */
  1021. void USB_ClearInterrupts(USB_OTG_GlobalTypeDef *USBx, uint32_t interrupt)
  1022. {
  1023. USBx->GINTSTS &= interrupt;
  1024. }
  1025. /**
  1026. * @brief Returns USB core mode
  1027. * @param USBx Selected device
  1028. * @retval return core mode : Host or Device
  1029. * This parameter can be one of these values:
  1030. * 0 : Host
  1031. * 1 : Device
  1032. */
  1033. uint32_t USB_GetMode(USB_OTG_GlobalTypeDef *USBx)
  1034. {
  1035. return ((USBx->GINTSTS) & 0x1U);
  1036. }
  1037. /**
  1038. * @brief Activate EP0 for Setup transactions
  1039. * @param USBx Selected device
  1040. * @retval HAL status
  1041. */
  1042. HAL_StatusTypeDef USB_ActivateSetup(USB_OTG_GlobalTypeDef *USBx)
  1043. {
  1044. uint32_t USBx_BASE = (uint32_t)USBx;
  1045. /* Set the MPS of the IN EP0 to 64 bytes */
  1046. USBx_INEP(0U)->DIEPCTL &= ~USB_OTG_DIEPCTL_MPSIZ;
  1047. USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGINAK;
  1048. return HAL_OK;
  1049. }
  1050. /**
  1051. * @brief Prepare the EP0 to start the first control setup
  1052. * @param USBx Selected device
  1053. * @param psetup pointer to setup packet
  1054. * @retval HAL status
  1055. */
  1056. HAL_StatusTypeDef USB_EP0_OutStart(USB_OTG_GlobalTypeDef *USBx, uint8_t *psetup)
  1057. {
  1058. uint32_t USBx_BASE = (uint32_t)USBx;
  1059. uint32_t gSNPSiD = *(__IO uint32_t *)(&USBx->CID + 0x1U);
  1060. UNUSED(psetup);
  1061. if (gSNPSiD > USB_OTG_CORE_ID_300A)
  1062. {
  1063. if ((USBx_OUTEP(0U)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
  1064. {
  1065. return HAL_OK;
  1066. }
  1067. }
  1068. USBx_OUTEP(0U)->DOEPTSIZ = 0U;
  1069. USBx_OUTEP(0U)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1U << 19));
  1070. USBx_OUTEP(0U)->DOEPTSIZ |= (3U * 8U);
  1071. USBx_OUTEP(0U)->DOEPTSIZ |= USB_OTG_DOEPTSIZ_STUPCNT;
  1072. return HAL_OK;
  1073. }
  1074. /**
  1075. * @brief Reset the USB Core (needed after USB clock settings change)
  1076. * @param USBx Selected device
  1077. * @retval HAL status
  1078. */
  1079. static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx)
  1080. {
  1081. __IO uint32_t count = 0U;
  1082. /* Wait for AHB master IDLE state. */
  1083. do
  1084. {
  1085. count++;
  1086. if (count > 200000U)
  1087. {
  1088. return HAL_TIMEOUT;
  1089. }
  1090. } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0U);
  1091. /* Core Soft Reset */
  1092. count = 0U;
  1093. USBx->GRSTCTL |= USB_OTG_GRSTCTL_CSRST;
  1094. do
  1095. {
  1096. count++;
  1097. if (count > 200000U)
  1098. {
  1099. return HAL_TIMEOUT;
  1100. }
  1101. } while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_CSRST) == USB_OTG_GRSTCTL_CSRST);
  1102. return HAL_OK;
  1103. }
  1104. /**
  1105. * @brief USB_HostInit : Initializes the USB OTG controller registers
  1106. * for Host mode
  1107. * @param USBx Selected device
  1108. * @param cfg pointer to a USB_OTG_CfgTypeDef structure that contains
  1109. * the configuration information for the specified USBx peripheral.
  1110. * @retval HAL status
  1111. */
  1112. HAL_StatusTypeDef USB_HostInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
  1113. {
  1114. HAL_StatusTypeDef ret = HAL_OK;
  1115. uint32_t USBx_BASE = (uint32_t)USBx;
  1116. uint32_t i;
  1117. /* Restart the Phy Clock */
  1118. USBx_PCGCCTL = 0U;
  1119. /* Disable VBUS sensing */
  1120. USBx->GCCFG &= ~(USB_OTG_GCCFG_VBUSASEN);
  1121. USBx->GCCFG &= ~(USB_OTG_GCCFG_VBUSBSEN);
  1122. /* Set default Max speed support */
  1123. USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSS);
  1124. /* Make sure the FIFOs are flushed. */
  1125. if (USB_FlushTxFifo(USBx, 0x10U) != HAL_OK) /* all Tx FIFOs */
  1126. {
  1127. ret = HAL_ERROR;
  1128. }
  1129. if (USB_FlushRxFifo(USBx) != HAL_OK)
  1130. {
  1131. ret = HAL_ERROR;
  1132. }
  1133. /* Clear all pending HC Interrupts */
  1134. for (i = 0U; i < cfg.Host_channels; i++)
  1135. {
  1136. USBx_HC(i)->HCINT = CLEAR_INTERRUPT_MASK;
  1137. USBx_HC(i)->HCINTMSK = 0U;
  1138. }
  1139. /* Disable all interrupts. */
  1140. USBx->GINTMSK = 0U;
  1141. /* Clear any pending interrupts */
  1142. USBx->GINTSTS = CLEAR_INTERRUPT_MASK;
  1143. /* set Rx FIFO size */
  1144. USBx->GRXFSIZ = 0x80U;
  1145. USBx->DIEPTXF0_HNPTXFSIZ = (uint32_t)(((0x60U << 16) & USB_OTG_NPTXFD) | 0x80U);
  1146. USBx->HPTXFSIZ = (uint32_t)(((0x40U << 16)& USB_OTG_HPTXFSIZ_PTXFD) | 0xE0U);
  1147. /* Enable the common interrupts */
  1148. USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
  1149. /* Enable interrupts matching to the Host mode ONLY */
  1150. USBx->GINTMSK |= (USB_OTG_GINTMSK_PRTIM | USB_OTG_GINTMSK_HCIM | \
  1151. USB_OTG_GINTMSK_SOFM | USB_OTG_GINTSTS_DISCINT | \
  1152. USB_OTG_GINTMSK_PXFRM_IISOOXFRM | USB_OTG_GINTMSK_WUIM);
  1153. return ret;
  1154. }
  1155. /**
  1156. * @brief USB_InitFSLSPClkSel : Initializes the FSLSPClkSel field of the
  1157. * HCFG register on the PHY type and set the right frame interval
  1158. * @param USBx Selected device
  1159. * @param freq clock frequency
  1160. * This parameter can be one of these values:
  1161. * HCFG_48_MHZ : Full Speed 48 MHz Clock
  1162. * HCFG_6_MHZ : Low Speed 6 MHz Clock
  1163. * @retval HAL status
  1164. */
  1165. HAL_StatusTypeDef USB_InitFSLSPClkSel(USB_OTG_GlobalTypeDef *USBx, uint8_t freq)
  1166. {
  1167. uint32_t USBx_BASE = (uint32_t)USBx;
  1168. USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSPCS);
  1169. USBx_HOST->HCFG |= (uint32_t)freq & USB_OTG_HCFG_FSLSPCS;
  1170. if (freq == HCFG_48_MHZ)
  1171. {
  1172. USBx_HOST->HFIR = HFIR_48_MHZ;
  1173. }
  1174. else if (freq == HCFG_6_MHZ)
  1175. {
  1176. USBx_HOST->HFIR = HFIR_6_MHZ;
  1177. }
  1178. else
  1179. {
  1180. return HAL_ERROR;
  1181. }
  1182. return HAL_OK;
  1183. }
  1184. /**
  1185. * @brief USB_OTG_ResetPort : Reset Host Port
  1186. * @param USBx Selected device
  1187. * @retval HAL status
  1188. * @note (1)The application must wait at least 10 ms
  1189. * before clearing the reset bit.
  1190. */
  1191. HAL_StatusTypeDef USB_ResetPort(USB_OTG_GlobalTypeDef *USBx)
  1192. {
  1193. uint32_t USBx_BASE = (uint32_t)USBx;
  1194. __IO uint32_t hprt0 = 0U;
  1195. hprt0 = USBx_HPRT0;
  1196. hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |
  1197. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
  1198. USBx_HPRT0 = (USB_OTG_HPRT_PRST | hprt0);
  1199. HAL_Delay(100U); /* See Note #1 */
  1200. USBx_HPRT0 = ((~USB_OTG_HPRT_PRST) & hprt0);
  1201. HAL_Delay(10U);
  1202. return HAL_OK;
  1203. }
  1204. /**
  1205. * @brief USB_DriveVbus : activate or de-activate vbus
  1206. * @param state VBUS state
  1207. * This parameter can be one of these values:
  1208. * 0 : Deactivate VBUS
  1209. * 1 : Activate VBUS
  1210. * @retval HAL status
  1211. */
  1212. HAL_StatusTypeDef USB_DriveVbus(USB_OTG_GlobalTypeDef *USBx, uint8_t state)
  1213. {
  1214. uint32_t USBx_BASE = (uint32_t)USBx;
  1215. __IO uint32_t hprt0 = 0U;
  1216. hprt0 = USBx_HPRT0;
  1217. hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |
  1218. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG);
  1219. if (((hprt0 & USB_OTG_HPRT_PPWR) == 0U) && (state == 1U))
  1220. {
  1221. USBx_HPRT0 = (USB_OTG_HPRT_PPWR | hprt0);
  1222. }
  1223. if (((hprt0 & USB_OTG_HPRT_PPWR) == USB_OTG_HPRT_PPWR) && (state == 0U))
  1224. {
  1225. USBx_HPRT0 = ((~USB_OTG_HPRT_PPWR) & hprt0);
  1226. }
  1227. return HAL_OK;
  1228. }
  1229. /**
  1230. * @brief Return Host Core speed
  1231. * @param USBx Selected device
  1232. * @retval speed : Host speed
  1233. * This parameter can be one of these values:
  1234. * @arg HCD_SPEED_FULL: Full speed mode
  1235. * @arg HCD_SPEED_LOW: Low speed mode
  1236. */
  1237. uint32_t USB_GetHostSpeed(USB_OTG_GlobalTypeDef const *USBx)
  1238. {
  1239. uint32_t USBx_BASE = (uint32_t)USBx;
  1240. __IO uint32_t hprt0 = 0U;
  1241. hprt0 = USBx_HPRT0;
  1242. return ((hprt0 & USB_OTG_HPRT_PSPD) >> 17);
  1243. }
  1244. /**
  1245. * @brief Return Host Current Frame number
  1246. * @param USBx Selected device
  1247. * @retval current frame number
  1248. */
  1249. uint32_t USB_GetCurrentFrame(USB_OTG_GlobalTypeDef const *USBx)
  1250. {
  1251. uint32_t USBx_BASE = (uint32_t)USBx;
  1252. return (USBx_HOST->HFNUM & USB_OTG_HFNUM_FRNUM);
  1253. }
  1254. /**
  1255. * @brief Initialize a host channel
  1256. * @param USBx Selected device
  1257. * @param ch_num Channel number
  1258. * This parameter can be a value from 1 to 15
  1259. * @param epnum Endpoint number
  1260. * This parameter can be a value from 1 to 15
  1261. * @param dev_address Current device address
  1262. * This parameter can be a value from 0 to 255
  1263. * @param speed Current device speed
  1264. * This parameter can be one of these values:
  1265. * @arg USB_OTG_SPEED_FULL: Full speed mode
  1266. * @arg USB_OTG_SPEED_LOW: Low speed mode
  1267. * @param ep_type Endpoint Type
  1268. * This parameter can be one of these values:
  1269. * @arg EP_TYPE_CTRL: Control type
  1270. * @arg EP_TYPE_ISOC: Isochronous type
  1271. * @arg EP_TYPE_BULK: Bulk type
  1272. * @arg EP_TYPE_INTR: Interrupt type
  1273. * @param mps Max Packet Size
  1274. * This parameter can be a value from 0 to 32K
  1275. * @retval HAL state
  1276. */
  1277. HAL_StatusTypeDef USB_HC_Init(USB_OTG_GlobalTypeDef *USBx, uint8_t ch_num,
  1278. uint8_t epnum, uint8_t dev_address, uint8_t speed,
  1279. uint8_t ep_type, uint16_t mps)
  1280. {
  1281. HAL_StatusTypeDef ret = HAL_OK;
  1282. uint32_t USBx_BASE = (uint32_t)USBx;
  1283. uint32_t HCcharEpDir;
  1284. uint32_t HCcharLowSpeed;
  1285. uint32_t HostCoreSpeed;
  1286. /* Clear old interrupt conditions for this host channel. */
  1287. USBx_HC((uint32_t)ch_num)->HCINT = CLEAR_INTERRUPT_MASK;
  1288. /* Enable channel interrupts required for this transfer. */
  1289. switch (ep_type)
  1290. {
  1291. case EP_TYPE_CTRL:
  1292. case EP_TYPE_BULK:
  1293. USBx_HC((uint32_t)ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |
  1294. USB_OTG_HCINTMSK_STALLM |
  1295. USB_OTG_HCINTMSK_TXERRM |
  1296. USB_OTG_HCINTMSK_DTERRM |
  1297. USB_OTG_HCINTMSK_AHBERR |
  1298. USB_OTG_HCINTMSK_NAKM;
  1299. if ((epnum & 0x80U) == 0x80U)
  1300. {
  1301. USBx_HC((uint32_t)ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
  1302. }
  1303. break;
  1304. case EP_TYPE_INTR:
  1305. USBx_HC((uint32_t)ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |
  1306. USB_OTG_HCINTMSK_STALLM |
  1307. USB_OTG_HCINTMSK_TXERRM |
  1308. USB_OTG_HCINTMSK_DTERRM |
  1309. USB_OTG_HCINTMSK_NAKM |
  1310. USB_OTG_HCINTMSK_AHBERR |
  1311. USB_OTG_HCINTMSK_FRMORM;
  1312. if ((epnum & 0x80U) == 0x80U)
  1313. {
  1314. USBx_HC((uint32_t)ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
  1315. }
  1316. break;
  1317. case EP_TYPE_ISOC:
  1318. USBx_HC((uint32_t)ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |
  1319. USB_OTG_HCINTMSK_ACKM |
  1320. USB_OTG_HCINTMSK_AHBERR |
  1321. USB_OTG_HCINTMSK_FRMORM;
  1322. if ((epnum & 0x80U) == 0x80U)
  1323. {
  1324. USBx_HC((uint32_t)ch_num)->HCINTMSK |= (USB_OTG_HCINTMSK_TXERRM | USB_OTG_HCINTMSK_BBERRM);
  1325. }
  1326. break;
  1327. default:
  1328. ret = HAL_ERROR;
  1329. break;
  1330. }
  1331. /* Enable host channel Halt interrupt */
  1332. USBx_HC((uint32_t)ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_CHHM;
  1333. /* Enable the top level host channel interrupt. */
  1334. USBx_HOST->HAINTMSK |= 1UL << (ch_num & 0xFU);
  1335. /* Make sure host channel interrupts are enabled. */
  1336. USBx->GINTMSK |= USB_OTG_GINTMSK_HCIM;
  1337. /* Program the HCCHAR register */
  1338. if ((epnum & 0x80U) == 0x80U)
  1339. {
  1340. HCcharEpDir = (0x1U << 15) & USB_OTG_HCCHAR_EPDIR;
  1341. }
  1342. else
  1343. {
  1344. HCcharEpDir = 0U;
  1345. }
  1346. HostCoreSpeed = USB_GetHostSpeed(USBx);
  1347. /* LS device plugged to HUB */
  1348. if ((speed == HPRT0_PRTSPD_LOW_SPEED) && (HostCoreSpeed != HPRT0_PRTSPD_LOW_SPEED))
  1349. {
  1350. HCcharLowSpeed = (0x1U << 17) & USB_OTG_HCCHAR_LSDEV;
  1351. }
  1352. else
  1353. {
  1354. HCcharLowSpeed = 0U;
  1355. }
  1356. USBx_HC((uint32_t)ch_num)->HCCHAR = (((uint32_t)dev_address << 22) & USB_OTG_HCCHAR_DAD) |
  1357. ((((uint32_t)epnum & 0x7FU) << 11) & USB_OTG_HCCHAR_EPNUM) |
  1358. (((uint32_t)ep_type << 18) & USB_OTG_HCCHAR_EPTYP) |
  1359. ((uint32_t)mps & USB_OTG_HCCHAR_MPSIZ) |
  1360. USB_OTG_HCCHAR_MC_0 | HCcharEpDir | HCcharLowSpeed;
  1361. if ((ep_type == EP_TYPE_INTR) || (ep_type == EP_TYPE_ISOC))
  1362. {
  1363. USBx_HC((uint32_t)ch_num)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM;
  1364. }
  1365. return ret;
  1366. }
  1367. /**
  1368. * @brief Start a transfer over a host channel
  1369. * @param USBx Selected device
  1370. * @param hc pointer to host channel structure
  1371. * @retval HAL state
  1372. */
  1373. HAL_StatusTypeDef USB_HC_StartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_HCTypeDef *hc)
  1374. {
  1375. uint32_t USBx_BASE = (uint32_t)USBx;
  1376. uint32_t ch_num = (uint32_t)hc->ch_num;
  1377. __IO uint32_t tmpreg;
  1378. uint8_t is_oddframe;
  1379. uint16_t len_words;
  1380. uint16_t num_packets;
  1381. uint16_t max_hc_pkt_count = HC_MAX_PKT_CNT;
  1382. /* Compute the expected number of packets associated to the transfer */
  1383. if (hc->xfer_len > 0U)
  1384. {
  1385. num_packets = (uint16_t)((hc->xfer_len + hc->max_packet - 1U) / hc->max_packet);
  1386. if (num_packets > max_hc_pkt_count)
  1387. {
  1388. num_packets = max_hc_pkt_count;
  1389. hc->XferSize = (uint32_t)num_packets * hc->max_packet;
  1390. }
  1391. }
  1392. else
  1393. {
  1394. num_packets = 1U;
  1395. }
  1396. /*
  1397. * For IN channel HCTSIZ.XferSize is expected to be an integer multiple of
  1398. * max_packet size.
  1399. */
  1400. if (hc->ep_is_in != 0U)
  1401. {
  1402. hc->XferSize = (uint32_t)num_packets * hc->max_packet;
  1403. }
  1404. else
  1405. {
  1406. hc->XferSize = hc->xfer_len;
  1407. }
  1408. /* Initialize the HCTSIZn register */
  1409. USBx_HC(ch_num)->HCTSIZ = (hc->XferSize & USB_OTG_HCTSIZ_XFRSIZ) |
  1410. (((uint32_t)num_packets << 19) & USB_OTG_HCTSIZ_PKTCNT) |
  1411. (((uint32_t)hc->data_pid << 29) & USB_OTG_HCTSIZ_DPID);
  1412. is_oddframe = (((uint32_t)USBx_HOST->HFNUM & 0x01U) != 0U) ? 0U : 1U;
  1413. USBx_HC(ch_num)->HCCHAR &= ~USB_OTG_HCCHAR_ODDFRM;
  1414. USBx_HC(ch_num)->HCCHAR |= (uint32_t)is_oddframe << 29;
  1415. /* Set host channel enable */
  1416. tmpreg = USBx_HC(ch_num)->HCCHAR;
  1417. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  1418. /* make sure to set the correct ep direction */
  1419. if (hc->ep_is_in != 0U)
  1420. {
  1421. tmpreg |= USB_OTG_HCCHAR_EPDIR;
  1422. }
  1423. else
  1424. {
  1425. tmpreg &= ~USB_OTG_HCCHAR_EPDIR;
  1426. }
  1427. tmpreg |= USB_OTG_HCCHAR_CHENA;
  1428. USBx_HC(ch_num)->HCCHAR = tmpreg;
  1429. if ((hc->ep_is_in == 0U) && (hc->xfer_len > 0U))
  1430. {
  1431. switch (hc->ep_type)
  1432. {
  1433. /* Non periodic transfer */
  1434. case EP_TYPE_CTRL:
  1435. case EP_TYPE_BULK:
  1436. len_words = (uint16_t)((hc->xfer_len + 3U) / 4U);
  1437. /* check if there is enough space in FIFO space */
  1438. if (len_words > (USBx->HNPTXSTS & 0xFFFFU))
  1439. {
  1440. /* need to process data in nptxfempty interrupt */
  1441. USBx->GINTMSK |= USB_OTG_GINTMSK_NPTXFEM;
  1442. }
  1443. break;
  1444. /* Periodic transfer */
  1445. case EP_TYPE_INTR:
  1446. case EP_TYPE_ISOC:
  1447. len_words = (uint16_t)((hc->xfer_len + 3U) / 4U);
  1448. /* check if there is enough space in FIFO space */
  1449. if (len_words > (USBx_HOST->HPTXSTS & 0xFFFFU)) /* split the transfer */
  1450. {
  1451. /* need to process data in ptxfempty interrupt */
  1452. USBx->GINTMSK |= USB_OTG_GINTMSK_PTXFEM;
  1453. }
  1454. break;
  1455. default:
  1456. break;
  1457. }
  1458. /* Write packet into the Tx FIFO. */
  1459. (void)USB_WritePacket(USBx, hc->xfer_buff, hc->ch_num, (uint16_t)hc->xfer_len);
  1460. }
  1461. return HAL_OK;
  1462. }
  1463. /**
  1464. * @brief Read all host channel interrupts status
  1465. * @param USBx Selected device
  1466. * @retval HAL state
  1467. */
  1468. uint32_t USB_HC_ReadInterrupt(USB_OTG_GlobalTypeDef *USBx)
  1469. {
  1470. uint32_t USBx_BASE = (uint32_t)USBx;
  1471. return ((USBx_HOST->HAINT) & 0xFFFFU);
  1472. }
  1473. /**
  1474. * @brief Halt a host channel
  1475. * @param USBx Selected device
  1476. * @param hc_num Host Channel number
  1477. * This parameter can be a value from 1 to 15
  1478. * @retval HAL state
  1479. */
  1480. HAL_StatusTypeDef USB_HC_Halt(USB_OTG_GlobalTypeDef *USBx, uint8_t hc_num)
  1481. {
  1482. uint32_t USBx_BASE = (uint32_t)USBx;
  1483. uint32_t hcnum = (uint32_t)hc_num;
  1484. __IO uint32_t count = 0U;
  1485. uint32_t HcEpType = (USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_EPTYP) >> 18;
  1486. uint32_t ChannelEna = (USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_CHENA) >> 31;
  1487. uint32_t SplitEna = (USBx_HC(hcnum)->HCSPLT & USB_OTG_HCSPLT_SPLITEN) >> 31;
  1488. /* In buffer DMA, Channel disable must not be programmed for non-split periodic channels.
  1489. At the end of the next uframe/frame (in the worst case), the core generates a channel halted
  1490. and disables the channel automatically. */
  1491. if ((((USBx->GAHBCFG & USB_OTG_GAHBCFG_DMAEN) == USB_OTG_GAHBCFG_DMAEN) && (SplitEna == 0U)) &&
  1492. ((ChannelEna == 0U) || (((HcEpType == HCCHAR_ISOC) || (HcEpType == HCCHAR_INTR)))))
  1493. {
  1494. return HAL_OK;
  1495. }
  1496. /* Check for space in the request queue to issue the halt. */
  1497. if ((HcEpType == HCCHAR_CTRL) || (HcEpType == HCCHAR_BULK))
  1498. {
  1499. USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHDIS;
  1500. if ((USBx->GAHBCFG & USB_OTG_GAHBCFG_DMAEN) == 0U)
  1501. {
  1502. if ((USBx->HNPTXSTS & (0xFFU << 16)) == 0U)
  1503. {
  1504. USBx_HC(hcnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHENA;
  1505. USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  1506. do
  1507. {
  1508. count++;
  1509. if (count > 1000U)
  1510. {
  1511. break;
  1512. }
  1513. } while ((USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
  1514. }
  1515. else
  1516. {
  1517. USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  1518. }
  1519. }
  1520. else
  1521. {
  1522. USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  1523. }
  1524. }
  1525. else
  1526. {
  1527. USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHDIS;
  1528. if ((USBx_HOST->HPTXSTS & (0xFFU << 16)) == 0U)
  1529. {
  1530. USBx_HC(hcnum)->HCCHAR &= ~USB_OTG_HCCHAR_CHENA;
  1531. USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  1532. do
  1533. {
  1534. count++;
  1535. if (count > 1000U)
  1536. {
  1537. break;
  1538. }
  1539. } while ((USBx_HC(hcnum)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
  1540. }
  1541. else
  1542. {
  1543. USBx_HC(hcnum)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  1544. }
  1545. }
  1546. return HAL_OK;
  1547. }
  1548. /**
  1549. * @brief Initiate Do Ping protocol
  1550. * @param USBx Selected device
  1551. * @param hc_num Host Channel number
  1552. * This parameter can be a value from 1 to 15
  1553. * @retval HAL state
  1554. */
  1555. HAL_StatusTypeDef USB_DoPing(USB_OTG_GlobalTypeDef *USBx, uint8_t ch_num)
  1556. {
  1557. uint32_t USBx_BASE = (uint32_t)USBx;
  1558. uint32_t chnum = (uint32_t)ch_num;
  1559. uint32_t num_packets = 1U;
  1560. uint32_t tmpreg;
  1561. USBx_HC(chnum)->HCTSIZ = ((num_packets << 19) & USB_OTG_HCTSIZ_PKTCNT) |
  1562. USB_OTG_HCTSIZ_DOPING;
  1563. /* Set host channel enable */
  1564. tmpreg = USBx_HC(chnum)->HCCHAR;
  1565. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  1566. tmpreg |= USB_OTG_HCCHAR_CHENA;
  1567. USBx_HC(chnum)->HCCHAR = tmpreg;
  1568. return HAL_OK;
  1569. }
  1570. /**
  1571. * @brief Stop Host Core
  1572. * @param USBx Selected device
  1573. * @retval HAL state
  1574. */
  1575. HAL_StatusTypeDef USB_StopHost(USB_OTG_GlobalTypeDef *USBx)
  1576. {
  1577. HAL_StatusTypeDef ret = HAL_OK;
  1578. uint32_t USBx_BASE = (uint32_t)USBx;
  1579. __IO uint32_t count = 0U;
  1580. uint32_t value;
  1581. uint32_t i;
  1582. (void)USB_DisableGlobalInt(USBx);
  1583. /* Flush USB FIFO */
  1584. if (USB_FlushTxFifo(USBx, 0x10U) != HAL_OK) /* all Tx FIFOs */
  1585. {
  1586. ret = HAL_ERROR;
  1587. }
  1588. if (USB_FlushRxFifo(USBx) != HAL_OK)
  1589. {
  1590. ret = HAL_ERROR;
  1591. }
  1592. /* Flush out any leftover queued requests. */
  1593. for (i = 0U; i <= 15U; i++)
  1594. {
  1595. value = USBx_HC(i)->HCCHAR;
  1596. value |= USB_OTG_HCCHAR_CHDIS;
  1597. value &= ~USB_OTG_HCCHAR_CHENA;
  1598. value &= ~USB_OTG_HCCHAR_EPDIR;
  1599. USBx_HC(i)->HCCHAR = value;
  1600. }
  1601. /* Halt all channels to put them into a known state. */
  1602. for (i = 0U; i <= 15U; i++)
  1603. {
  1604. value = USBx_HC(i)->HCCHAR;
  1605. value |= USB_OTG_HCCHAR_CHDIS;
  1606. value |= USB_OTG_HCCHAR_CHENA;
  1607. value &= ~USB_OTG_HCCHAR_EPDIR;
  1608. USBx_HC(i)->HCCHAR = value;
  1609. do
  1610. {
  1611. count++;
  1612. if (count > 1000U)
  1613. {
  1614. break;
  1615. }
  1616. } while ((USBx_HC(i)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
  1617. }
  1618. /* Clear any pending Host interrupts */
  1619. USBx_HOST->HAINT = CLEAR_INTERRUPT_MASK;
  1620. USBx->GINTSTS = CLEAR_INTERRUPT_MASK;
  1621. (void)USB_EnableGlobalInt(USBx);
  1622. return ret;
  1623. }
  1624. /**
  1625. * @brief USB_ActivateRemoteWakeup active remote wakeup signalling
  1626. * @param USBx Selected device
  1627. * @retval HAL status
  1628. */
  1629. HAL_StatusTypeDef USB_ActivateRemoteWakeup(USB_OTG_GlobalTypeDef *USBx)
  1630. {
  1631. uint32_t USBx_BASE = (uint32_t)USBx;
  1632. if ((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
  1633. {
  1634. /* active Remote wakeup signalling */
  1635. USBx_DEVICE->DCTL |= USB_OTG_DCTL_RWUSIG;
  1636. }
  1637. return HAL_OK;
  1638. }
  1639. /**
  1640. * @brief USB_DeActivateRemoteWakeup de-active remote wakeup signalling
  1641. * @param USBx Selected device
  1642. * @retval HAL status
  1643. */
  1644. HAL_StatusTypeDef USB_DeActivateRemoteWakeup(USB_OTG_GlobalTypeDef *USBx)
  1645. {
  1646. uint32_t USBx_BASE = (uint32_t)USBx;
  1647. /* active Remote wakeup signalling */
  1648. USBx_DEVICE->DCTL &= ~(USB_OTG_DCTL_RWUSIG);
  1649. return HAL_OK;
  1650. }
  1651. #endif /* defined (USB_OTG_FS) */
  1652. #if defined (USB)
  1653. /**
  1654. * @brief Initializes the USB Core
  1655. * @param USBx USB Instance
  1656. * @param cfg pointer to a USB_CfgTypeDef structure that contains
  1657. * the configuration information for the specified USBx peripheral.
  1658. * @retval HAL status
  1659. */
  1660. HAL_StatusTypeDef USB_CoreInit(USB_TypeDef *USBx, USB_CfgTypeDef cfg)
  1661. {
  1662. /* Prevent unused argument(s) compilation warning */
  1663. UNUSED(USBx);
  1664. UNUSED(cfg);
  1665. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1666. only by USB OTG FS peripheral.
  1667. - This function is added to ensure compatibility across platforms.
  1668. */
  1669. return HAL_OK;
  1670. }
  1671. /**
  1672. * @brief USB_EnableGlobalInt
  1673. * Enables the controller's Global Int in the AHB Config reg
  1674. * @param USBx Selected device
  1675. * @retval HAL status
  1676. */
  1677. HAL_StatusTypeDef USB_EnableGlobalInt(USB_TypeDef *USBx)
  1678. {
  1679. uint32_t winterruptmask;
  1680. /* Clear pending interrupts */
  1681. USBx->ISTR = 0U;
  1682. /* Set winterruptmask variable */
  1683. winterruptmask = USB_CNTR_CTRM | USB_CNTR_WKUPM |
  1684. USB_CNTR_SUSPM | USB_CNTR_ERRM |
  1685. USB_CNTR_SOFM | USB_CNTR_ESOFM |
  1686. USB_CNTR_RESETM;
  1687. /* Set interrupt mask */
  1688. USBx->CNTR = (uint16_t)winterruptmask;
  1689. return HAL_OK;
  1690. }
  1691. /**
  1692. * @brief USB_DisableGlobalInt
  1693. * Disable the controller's Global Int in the AHB Config reg
  1694. * @param USBx Selected device
  1695. * @retval HAL status
  1696. */
  1697. HAL_StatusTypeDef USB_DisableGlobalInt(USB_TypeDef *USBx)
  1698. {
  1699. uint32_t winterruptmask;
  1700. /* Set winterruptmask variable */
  1701. winterruptmask = USB_CNTR_CTRM | USB_CNTR_WKUPM |
  1702. USB_CNTR_SUSPM | USB_CNTR_ERRM |
  1703. USB_CNTR_SOFM | USB_CNTR_ESOFM |
  1704. USB_CNTR_RESETM;
  1705. /* Clear interrupt mask */
  1706. USBx->CNTR &= (uint16_t)(~winterruptmask);
  1707. return HAL_OK;
  1708. }
  1709. /**
  1710. * @brief USB_SetCurrentMode Set functional mode
  1711. * @param USBx Selected device
  1712. * @param mode current core mode
  1713. * This parameter can be one of the these values:
  1714. * @arg USB_DEVICE_MODE Peripheral mode
  1715. * @retval HAL status
  1716. */
  1717. HAL_StatusTypeDef USB_SetCurrentMode(USB_TypeDef *USBx, USB_ModeTypeDef mode)
  1718. {
  1719. /* Prevent unused argument(s) compilation warning */
  1720. UNUSED(USBx);
  1721. UNUSED(mode);
  1722. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1723. only by USB OTG FS peripheral.
  1724. - This function is added to ensure compatibility across platforms.
  1725. */
  1726. return HAL_OK;
  1727. }
  1728. /**
  1729. * @brief USB_DevInit Initializes the USB controller registers
  1730. * for device mode
  1731. * @param USBx Selected device
  1732. * @param cfg pointer to a USB_CfgTypeDef structure that contains
  1733. * the configuration information for the specified USBx peripheral.
  1734. * @retval HAL status
  1735. */
  1736. HAL_StatusTypeDef USB_DevInit(USB_TypeDef *USBx, USB_CfgTypeDef cfg)
  1737. {
  1738. /* Prevent unused argument(s) compilation warning */
  1739. UNUSED(cfg);
  1740. /* Init Device */
  1741. /* CNTR_FRES = 1 */
  1742. USBx->CNTR = (uint16_t)USB_CNTR_FRES;
  1743. /* CNTR_FRES = 0 */
  1744. USBx->CNTR = 0U;
  1745. /* Clear pending interrupts */
  1746. USBx->ISTR = 0U;
  1747. /*Set Btable Address*/
  1748. USBx->BTABLE = BTABLE_ADDRESS;
  1749. return HAL_OK;
  1750. }
  1751. /**
  1752. * @brief USB_FlushTxFifo : Flush a Tx FIFO
  1753. * @param USBx : Selected device
  1754. * @param num : FIFO number
  1755. * This parameter can be a value from 1 to 15
  1756. 15 means Flush all Tx FIFOs
  1757. * @retval HAL status
  1758. */
  1759. HAL_StatusTypeDef USB_FlushTxFifo(USB_TypeDef const *USBx, uint32_t num)
  1760. {
  1761. /* Prevent unused argument(s) compilation warning */
  1762. UNUSED(USBx);
  1763. UNUSED(num);
  1764. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1765. only by USB OTG FS peripheral.
  1766. - This function is added to ensure compatibility across platforms.
  1767. */
  1768. return HAL_OK;
  1769. }
  1770. /**
  1771. * @brief USB_FlushRxFifo : Flush Rx FIFO
  1772. * @param USBx : Selected device
  1773. * @retval HAL status
  1774. */
  1775. HAL_StatusTypeDef USB_FlushRxFifo(USB_TypeDef const *USBx)
  1776. {
  1777. /* Prevent unused argument(s) compilation warning */
  1778. UNUSED(USBx);
  1779. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1780. only by USB OTG FS peripheral.
  1781. - This function is added to ensure compatibility across platforms.
  1782. */
  1783. return HAL_OK;
  1784. }
  1785. #if defined (HAL_PCD_MODULE_ENABLED)
  1786. /**
  1787. * @brief Activate and configure an endpoint
  1788. * @param USBx Selected device
  1789. * @param ep pointer to endpoint structure
  1790. * @retval HAL status
  1791. */
  1792. HAL_StatusTypeDef USB_ActivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep)
  1793. {
  1794. HAL_StatusTypeDef ret = HAL_OK;
  1795. uint16_t wEpRegVal;
  1796. wEpRegVal = PCD_GET_ENDPOINT(USBx, ep->num) & USB_EP_T_MASK;
  1797. /* initialize Endpoint */
  1798. switch (ep->type)
  1799. {
  1800. case EP_TYPE_CTRL:
  1801. wEpRegVal |= USB_EP_CONTROL;
  1802. break;
  1803. case EP_TYPE_BULK:
  1804. wEpRegVal |= USB_EP_BULK;
  1805. break;
  1806. case EP_TYPE_INTR:
  1807. wEpRegVal |= USB_EP_INTERRUPT;
  1808. break;
  1809. case EP_TYPE_ISOC:
  1810. wEpRegVal |= USB_EP_ISOCHRONOUS;
  1811. break;
  1812. default:
  1813. ret = HAL_ERROR;
  1814. break;
  1815. }
  1816. PCD_SET_ENDPOINT(USBx, ep->num, (wEpRegVal | USB_EP_CTR_RX | USB_EP_CTR_TX));
  1817. PCD_SET_EP_ADDRESS(USBx, ep->num, ep->num);
  1818. if (ep->doublebuffer == 0U)
  1819. {
  1820. if (ep->is_in != 0U)
  1821. {
  1822. /*Set the endpoint Transmit buffer address */
  1823. PCD_SET_EP_TX_ADDRESS(USBx, ep->num, ep->pmaadress);
  1824. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  1825. if (ep->type != EP_TYPE_ISOC)
  1826. {
  1827. /* Configure NAK status for the Endpoint */
  1828. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
  1829. }
  1830. else
  1831. {
  1832. /* Configure TX Endpoint to disabled state */
  1833. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  1834. }
  1835. }
  1836. else
  1837. {
  1838. /* Set the endpoint Receive buffer address */
  1839. PCD_SET_EP_RX_ADDRESS(USBx, ep->num, ep->pmaadress);
  1840. /* Set the endpoint Receive buffer counter */
  1841. PCD_SET_EP_RX_CNT(USBx, ep->num, ep->maxpacket);
  1842. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  1843. if (ep->num == 0U)
  1844. {
  1845. /* Configure VALID status for EP0 */
  1846. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
  1847. }
  1848. else
  1849. {
  1850. /* Configure NAK status for OUT Endpoint */
  1851. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_NAK);
  1852. }
  1853. }
  1854. }
  1855. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1856. /* Double Buffer */
  1857. else
  1858. {
  1859. if (ep->type == EP_TYPE_BULK)
  1860. {
  1861. /* Set bulk endpoint as double buffered */
  1862. PCD_SET_BULK_EP_DBUF(USBx, ep->num);
  1863. }
  1864. else
  1865. {
  1866. /* Set the ISOC endpoint in double buffer mode */
  1867. PCD_CLEAR_EP_KIND(USBx, ep->num);
  1868. }
  1869. /* Set buffer address for double buffered mode */
  1870. PCD_SET_EP_DBUF_ADDR(USBx, ep->num, ep->pmaaddr0, ep->pmaaddr1);
  1871. if (ep->is_in == 0U)
  1872. {
  1873. /* Clear the data toggle bits for the endpoint IN/OUT */
  1874. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  1875. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  1876. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
  1877. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  1878. }
  1879. else
  1880. {
  1881. /* Clear the data toggle bits for the endpoint IN/OUT */
  1882. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  1883. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  1884. if (ep->type != EP_TYPE_ISOC)
  1885. {
  1886. /* Configure NAK status for the Endpoint */
  1887. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
  1888. }
  1889. else
  1890. {
  1891. /* Configure TX Endpoint to disabled state */
  1892. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  1893. }
  1894. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
  1895. }
  1896. }
  1897. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1898. return ret;
  1899. }
  1900. /**
  1901. * @brief De-activate and de-initialize an endpoint
  1902. * @param USBx Selected device
  1903. * @param ep pointer to endpoint structure
  1904. * @retval HAL status
  1905. */
  1906. HAL_StatusTypeDef USB_DeactivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep)
  1907. {
  1908. if (ep->doublebuffer == 0U)
  1909. {
  1910. if (ep->is_in != 0U)
  1911. {
  1912. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  1913. /* Configure DISABLE status for the Endpoint */
  1914. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  1915. }
  1916. else
  1917. {
  1918. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  1919. /* Configure DISABLE status for the Endpoint */
  1920. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
  1921. }
  1922. }
  1923. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1924. /* Double Buffer */
  1925. else
  1926. {
  1927. if (ep->is_in == 0U)
  1928. {
  1929. /* Clear the data toggle bits for the endpoint IN/OUT*/
  1930. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  1931. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  1932. /* Reset value of the data toggle bits for the endpoint out*/
  1933. PCD_TX_DTOG(USBx, ep->num);
  1934. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
  1935. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  1936. }
  1937. else
  1938. {
  1939. /* Clear the data toggle bits for the endpoint IN/OUT*/
  1940. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  1941. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  1942. PCD_RX_DTOG(USBx, ep->num);
  1943. /* Configure DISABLE status for the Endpoint*/
  1944. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  1945. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
  1946. }
  1947. }
  1948. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1949. return HAL_OK;
  1950. }
  1951. /**
  1952. * @brief USB_EPStartXfer setup and starts a transfer over an EP
  1953. * @param USBx Selected device
  1954. * @param ep pointer to endpoint structure
  1955. * @retval HAL status
  1956. */
  1957. HAL_StatusTypeDef USB_EPStartXfer(USB_TypeDef *USBx, USB_EPTypeDef *ep)
  1958. {
  1959. uint32_t len;
  1960. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1961. uint16_t pmabuffer;
  1962. uint16_t wEPVal;
  1963. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  1964. /* IN endpoint */
  1965. if (ep->is_in == 1U)
  1966. {
  1967. /*Multi packet transfer*/
  1968. if (ep->xfer_len > ep->maxpacket)
  1969. {
  1970. len = ep->maxpacket;
  1971. }
  1972. else
  1973. {
  1974. len = ep->xfer_len;
  1975. }
  1976. /* configure and validate Tx endpoint */
  1977. if (ep->doublebuffer == 0U)
  1978. {
  1979. USB_WritePMA(USBx, ep->xfer_buff, ep->pmaadress, (uint16_t)len);
  1980. PCD_SET_EP_TX_CNT(USBx, ep->num, len);
  1981. }
  1982. #if (USE_USB_DOUBLE_BUFFER == 1U)
  1983. else
  1984. {
  1985. /* double buffer bulk management */
  1986. if (ep->type == EP_TYPE_BULK)
  1987. {
  1988. if (ep->xfer_len_db > ep->maxpacket)
  1989. {
  1990. /* enable double buffer */
  1991. PCD_SET_BULK_EP_DBUF(USBx, ep->num);
  1992. /* each Time to write in PMA xfer_len_db will */
  1993. ep->xfer_len_db -= len;
  1994. /* Fill the two first buffer in the Buffer0 & Buffer1 */
  1995. if ((PCD_GET_ENDPOINT(USBx, ep->num) & USB_EP_DTOG_TX) != 0U)
  1996. {
  1997. /* Set the Double buffer counter for pmabuffer1 */
  1998. PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
  1999. pmabuffer = ep->pmaaddr1;
  2000. /* Write the user buffer to USB PMA */
  2001. USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
  2002. ep->xfer_buff += len;
  2003. if (ep->xfer_len_db > ep->maxpacket)
  2004. {
  2005. ep->xfer_len_db -= len;
  2006. }
  2007. else
  2008. {
  2009. len = ep->xfer_len_db;
  2010. ep->xfer_len_db = 0U;
  2011. }
  2012. /* Set the Double buffer counter for pmabuffer0 */
  2013. PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len);
  2014. pmabuffer = ep->pmaaddr0;
  2015. /* Write the user buffer to USB PMA */
  2016. USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
  2017. }
  2018. else
  2019. {
  2020. /* Set the Double buffer counter for pmabuffer0 */
  2021. PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len);
  2022. pmabuffer = ep->pmaaddr0;
  2023. /* Write the user buffer to USB PMA */
  2024. USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
  2025. ep->xfer_buff += len;
  2026. if (ep->xfer_len_db > ep->maxpacket)
  2027. {
  2028. ep->xfer_len_db -= len;
  2029. }
  2030. else
  2031. {
  2032. len = ep->xfer_len_db;
  2033. ep->xfer_len_db = 0U;
  2034. }
  2035. /* Set the Double buffer counter for pmabuffer1 */
  2036. PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
  2037. pmabuffer = ep->pmaaddr1;
  2038. /* Write the user buffer to USB PMA */
  2039. USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
  2040. }
  2041. }
  2042. /* auto Switch to single buffer mode when transfer <Mps no need to manage in double buffer */
  2043. else
  2044. {
  2045. len = ep->xfer_len_db;
  2046. /* disable double buffer mode for Bulk endpoint */
  2047. PCD_CLEAR_BULK_EP_DBUF(USBx, ep->num);
  2048. /* Set Tx count with nbre of byte to be transmitted */
  2049. PCD_SET_EP_TX_CNT(USBx, ep->num, len);
  2050. pmabuffer = ep->pmaaddr0;
  2051. /* Write the user buffer to USB PMA */
  2052. USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
  2053. }
  2054. }
  2055. else /* manage isochronous double buffer IN mode */
  2056. {
  2057. /* each Time to write in PMA xfer_len_db will */
  2058. ep->xfer_len_db -= len;
  2059. /* Fill the data buffer */
  2060. if ((PCD_GET_ENDPOINT(USBx, ep->num) & USB_EP_DTOG_TX) != 0U)
  2061. {
  2062. /* Set the Double buffer counter for pmabuffer1 */
  2063. PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
  2064. pmabuffer = ep->pmaaddr1;
  2065. /* Write the user buffer to USB PMA */
  2066. USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
  2067. }
  2068. else
  2069. {
  2070. /* Set the Double buffer counter for pmabuffer0 */
  2071. PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len);
  2072. pmabuffer = ep->pmaaddr0;
  2073. /* Write the user buffer to USB PMA */
  2074. USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, (uint16_t)len);
  2075. }
  2076. }
  2077. }
  2078. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  2079. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_VALID);
  2080. }
  2081. else /* OUT endpoint */
  2082. {
  2083. if (ep->doublebuffer == 0U)
  2084. {
  2085. /* Multi packet transfer */
  2086. if (ep->xfer_len > ep->maxpacket)
  2087. {
  2088. len = ep->maxpacket;
  2089. ep->xfer_len -= len;
  2090. }
  2091. else
  2092. {
  2093. len = ep->xfer_len;
  2094. ep->xfer_len = 0U;
  2095. }
  2096. /* configure and validate Rx endpoint */
  2097. PCD_SET_EP_RX_CNT(USBx, ep->num, len);
  2098. }
  2099. #if (USE_USB_DOUBLE_BUFFER == 1U)
  2100. else
  2101. {
  2102. /* First Transfer Coming From HAL_PCD_EP_Receive & From ISR */
  2103. /* Set the Double buffer counter */
  2104. if (ep->type == EP_TYPE_BULK)
  2105. {
  2106. PCD_SET_EP_DBUF_CNT(USBx, ep->num, ep->is_in, ep->maxpacket);
  2107. /* Coming from ISR */
  2108. if (ep->xfer_count != 0U)
  2109. {
  2110. /* update last value to check if there is blocking state */
  2111. wEPVal = PCD_GET_ENDPOINT(USBx, ep->num);
  2112. /*Blocking State */
  2113. if ((((wEPVal & USB_EP_DTOG_RX) != 0U) && ((wEPVal & USB_EP_DTOG_TX) != 0U)) ||
  2114. (((wEPVal & USB_EP_DTOG_RX) == 0U) && ((wEPVal & USB_EP_DTOG_TX) == 0U)))
  2115. {
  2116. PCD_FREE_USER_BUFFER(USBx, ep->num, 0U);
  2117. }
  2118. }
  2119. }
  2120. /* iso out double */
  2121. else if (ep->type == EP_TYPE_ISOC)
  2122. {
  2123. /* Multi packet transfer */
  2124. if (ep->xfer_len > ep->maxpacket)
  2125. {
  2126. len = ep->maxpacket;
  2127. ep->xfer_len -= len;
  2128. }
  2129. else
  2130. {
  2131. len = ep->xfer_len;
  2132. ep->xfer_len = 0U;
  2133. }
  2134. PCD_SET_EP_DBUF_CNT(USBx, ep->num, ep->is_in, len);
  2135. }
  2136. else
  2137. {
  2138. return HAL_ERROR;
  2139. }
  2140. }
  2141. #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
  2142. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
  2143. }
  2144. return HAL_OK;
  2145. }
  2146. /**
  2147. * @brief USB_EPSetStall set a stall condition over an EP
  2148. * @param USBx Selected device
  2149. * @param ep pointer to endpoint structure
  2150. * @retval HAL status
  2151. */
  2152. HAL_StatusTypeDef USB_EPSetStall(USB_TypeDef *USBx, USB_EPTypeDef *ep)
  2153. {
  2154. if (ep->is_in != 0U)
  2155. {
  2156. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_STALL);
  2157. }
  2158. else
  2159. {
  2160. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_STALL);
  2161. }
  2162. return HAL_OK;
  2163. }
  2164. /**
  2165. * @brief USB_EPClearStall Clear a stall condition over an EP
  2166. * @param USBx Selected device
  2167. * @param ep pointer to endpoint structure
  2168. * @retval HAL status
  2169. */
  2170. HAL_StatusTypeDef USB_EPClearStall(USB_TypeDef *USBx, USB_EPTypeDef *ep)
  2171. {
  2172. if (ep->doublebuffer == 0U)
  2173. {
  2174. if (ep->is_in != 0U)
  2175. {
  2176. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  2177. if (ep->type != EP_TYPE_ISOC)
  2178. {
  2179. /* Configure NAK status for the Endpoint */
  2180. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
  2181. }
  2182. }
  2183. else
  2184. {
  2185. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  2186. /* Configure VALID status for the Endpoint */
  2187. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
  2188. }
  2189. }
  2190. return HAL_OK;
  2191. }
  2192. /**
  2193. * @brief USB_EPStoptXfer Stop transfer on an EP
  2194. * @param USBx usb device instance
  2195. * @param ep pointer to endpoint structure
  2196. * @retval HAL status
  2197. */
  2198. HAL_StatusTypeDef USB_EPStopXfer(USB_TypeDef *USBx, USB_EPTypeDef *ep)
  2199. {
  2200. /* IN endpoint */
  2201. if (ep->is_in == 1U)
  2202. {
  2203. if (ep->doublebuffer == 0U)
  2204. {
  2205. if (ep->type != EP_TYPE_ISOC)
  2206. {
  2207. /* Configure NAK status for the Endpoint */
  2208. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
  2209. }
  2210. else
  2211. {
  2212. /* Configure TX Endpoint to disabled state */
  2213. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  2214. }
  2215. }
  2216. }
  2217. else /* OUT endpoint */
  2218. {
  2219. if (ep->doublebuffer == 0U)
  2220. {
  2221. if (ep->type != EP_TYPE_ISOC)
  2222. {
  2223. /* Configure NAK status for the Endpoint */
  2224. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_NAK);
  2225. }
  2226. else
  2227. {
  2228. /* Configure RX Endpoint to disabled state */
  2229. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
  2230. }
  2231. }
  2232. }
  2233. return HAL_OK;
  2234. }
  2235. #endif /* defined (HAL_PCD_MODULE_ENABLED) */
  2236. /**
  2237. * @brief USB_StopDevice Stop the usb device mode
  2238. * @param USBx Selected device
  2239. * @retval HAL status
  2240. */
  2241. HAL_StatusTypeDef USB_StopDevice(USB_TypeDef *USBx)
  2242. {
  2243. /* disable all interrupts and force USB reset */
  2244. USBx->CNTR = (uint16_t)USB_CNTR_FRES;
  2245. /* clear interrupt status register */
  2246. USBx->ISTR = 0U;
  2247. /* switch-off device */
  2248. USBx->CNTR = (uint16_t)(USB_CNTR_FRES | USB_CNTR_PDWN);
  2249. return HAL_OK;
  2250. }
  2251. /**
  2252. * @brief USB_SetDevAddress Stop the usb device mode
  2253. * @param USBx Selected device
  2254. * @param address new device address to be assigned
  2255. * This parameter can be a value from 0 to 255
  2256. * @retval HAL status
  2257. */
  2258. HAL_StatusTypeDef USB_SetDevAddress(USB_TypeDef *USBx, uint8_t address)
  2259. {
  2260. if (address == 0U)
  2261. {
  2262. /* set device address and enable function */
  2263. USBx->DADDR = (uint16_t)USB_DADDR_EF;
  2264. }
  2265. return HAL_OK;
  2266. }
  2267. /**
  2268. * @brief USB_DevConnect Connect the USB device by enabling the pull-up/pull-down
  2269. * @param USBx Selected device
  2270. * @retval HAL status
  2271. */
  2272. HAL_StatusTypeDef USB_DevConnect(USB_TypeDef *USBx)
  2273. {
  2274. /* Prevent unused argument(s) compilation warning */
  2275. UNUSED(USBx);
  2276. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  2277. only by USB OTG FS peripheral.
  2278. - This function is added to ensure compatibility across platforms.
  2279. */
  2280. return HAL_OK;
  2281. }
  2282. /**
  2283. * @brief USB_DevDisconnect Disconnect the USB device by disabling the pull-up/pull-down
  2284. * @param USBx Selected device
  2285. * @retval HAL status
  2286. */
  2287. HAL_StatusTypeDef USB_DevDisconnect(USB_TypeDef *USBx)
  2288. {
  2289. /* Prevent unused argument(s) compilation warning */
  2290. UNUSED(USBx);
  2291. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  2292. only by USB OTG FS peripheral.
  2293. - This function is added to ensure compatibility across platforms.
  2294. */
  2295. return HAL_OK;
  2296. }
  2297. /**
  2298. * @brief USB_ReadInterrupts return the global USB interrupt status
  2299. * @param USBx Selected device
  2300. * @retval USB Global Interrupt status
  2301. */
  2302. uint32_t USB_ReadInterrupts(USB_TypeDef const *USBx)
  2303. {
  2304. uint32_t tmpreg;
  2305. tmpreg = USBx->ISTR;
  2306. return tmpreg;
  2307. }
  2308. /**
  2309. * @brief USB_ReadDevAllOutEpInterrupt return the USB device OUT endpoints interrupt status
  2310. * @param USBx Selected device
  2311. * @retval HAL status
  2312. */
  2313. uint32_t USB_ReadDevAllOutEpInterrupt(USB_TypeDef *USBx)
  2314. {
  2315. /* Prevent unused argument(s) compilation warning */
  2316. UNUSED(USBx);
  2317. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  2318. only by USB OTG FS peripheral.
  2319. - This function is added to ensure compatibility across platforms.
  2320. */
  2321. return (0);
  2322. }
  2323. /**
  2324. * @brief USB_ReadDevAllInEpInterrupt return the USB device IN endpoints interrupt status
  2325. * @param USBx Selected device
  2326. * @retval HAL status
  2327. */
  2328. uint32_t USB_ReadDevAllInEpInterrupt(USB_TypeDef *USBx)
  2329. {
  2330. /* Prevent unused argument(s) compilation warning */
  2331. UNUSED(USBx);
  2332. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  2333. only by USB OTG FS peripheral.
  2334. - This function is added to ensure compatibility across platforms.
  2335. */
  2336. return (0);
  2337. }
  2338. /**
  2339. * @brief Returns Device OUT EP Interrupt register
  2340. * @param USBx Selected device
  2341. * @param epnum endpoint number
  2342. * This parameter can be a value from 0 to 15
  2343. * @retval Device OUT EP Interrupt register
  2344. */
  2345. uint32_t USB_ReadDevOutEPInterrupt(USB_TypeDef *USBx, uint8_t epnum)
  2346. {
  2347. /* Prevent unused argument(s) compilation warning */
  2348. UNUSED(USBx);
  2349. UNUSED(epnum);
  2350. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  2351. only by USB OTG FS peripheral.
  2352. - This function is added to ensure compatibility across platforms.
  2353. */
  2354. return (0);
  2355. }
  2356. /**
  2357. * @brief Returns Device IN EP Interrupt register
  2358. * @param USBx Selected device
  2359. * @param epnum endpoint number
  2360. * This parameter can be a value from 0 to 15
  2361. * @retval Device IN EP Interrupt register
  2362. */
  2363. uint32_t USB_ReadDevInEPInterrupt(USB_TypeDef *USBx, uint8_t epnum)
  2364. {
  2365. /* Prevent unused argument(s) compilation warning */
  2366. UNUSED(USBx);
  2367. UNUSED(epnum);
  2368. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  2369. only by USB OTG FS peripheral.
  2370. - This function is added to ensure compatibility across platforms.
  2371. */
  2372. return (0);
  2373. }
  2374. /**
  2375. * @brief USB_ClearInterrupts: clear a USB interrupt
  2376. * @param USBx Selected device
  2377. * @param interrupt flag
  2378. * @retval None
  2379. */
  2380. void USB_ClearInterrupts(USB_TypeDef *USBx, uint32_t interrupt)
  2381. {
  2382. /* Prevent unused argument(s) compilation warning */
  2383. UNUSED(USBx);
  2384. UNUSED(interrupt);
  2385. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  2386. only by USB OTG FS peripheral.
  2387. - This function is added to ensure compatibility across platforms.
  2388. */
  2389. }
  2390. /**
  2391. * @brief Prepare the EP0 to start the first control setup
  2392. * @param USBx Selected device
  2393. * @param psetup pointer to setup packet
  2394. * @retval HAL status
  2395. */
  2396. HAL_StatusTypeDef USB_EP0_OutStart(USB_TypeDef *USBx, uint8_t *psetup)
  2397. {
  2398. /* Prevent unused argument(s) compilation warning */
  2399. UNUSED(USBx);
  2400. UNUSED(psetup);
  2401. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  2402. only by USB OTG FS peripheral.
  2403. - This function is added to ensure compatibility across platforms.
  2404. */
  2405. return HAL_OK;
  2406. }
  2407. /**
  2408. * @brief USB_ActivateRemoteWakeup : active remote wakeup signalling
  2409. * @param USBx Selected device
  2410. * @retval HAL status
  2411. */
  2412. HAL_StatusTypeDef USB_ActivateRemoteWakeup(USB_TypeDef *USBx)
  2413. {
  2414. USBx->CNTR |= (uint16_t)USB_CNTR_RESUME;
  2415. return HAL_OK;
  2416. }
  2417. /**
  2418. * @brief USB_DeActivateRemoteWakeup de-active remote wakeup signalling
  2419. * @param USBx Selected device
  2420. * @retval HAL status
  2421. */
  2422. HAL_StatusTypeDef USB_DeActivateRemoteWakeup(USB_TypeDef *USBx)
  2423. {
  2424. USBx->CNTR &= (uint16_t)(~USB_CNTR_RESUME);
  2425. return HAL_OK;
  2426. }
  2427. /**
  2428. * @brief Copy a buffer from user memory area to packet memory area (PMA)
  2429. * @param USBx USB peripheral instance register address.
  2430. * @param pbUsrBuf pointer to user memory area.
  2431. * @param wPMABufAddr address into PMA.
  2432. * @param wNBytes no. of bytes to be copied.
  2433. * @retval None
  2434. */
  2435. void USB_WritePMA(USB_TypeDef const *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
  2436. {
  2437. uint32_t n = ((uint32_t)wNBytes + 1U) >> 1;
  2438. uint32_t BaseAddr = (uint32_t)USBx;
  2439. uint32_t count;
  2440. uint16_t WrVal;
  2441. __IO uint16_t *pdwVal;
  2442. uint8_t *pBuf = pbUsrBuf;
  2443. pdwVal = (__IO uint16_t *)(BaseAddr + 0x400U + ((uint32_t)wPMABufAddr * PMA_ACCESS));
  2444. for (count = n; count != 0U; count--)
  2445. {
  2446. WrVal = pBuf[0];
  2447. WrVal |= (uint16_t)pBuf[1] << 8;
  2448. *pdwVal = (WrVal & 0xFFFFU);
  2449. pdwVal++;
  2450. #if PMA_ACCESS > 1U
  2451. pdwVal++;
  2452. #endif /* PMA_ACCESS */
  2453. pBuf++;
  2454. pBuf++;
  2455. }
  2456. }
  2457. /**
  2458. * @brief Copy data from packet memory area (PMA) to user memory buffer
  2459. * @param USBx USB peripheral instance register address.
  2460. * @param pbUsrBuf pointer to user memory area.
  2461. * @param wPMABufAddr address into PMA.
  2462. * @param wNBytes no. of bytes to be copied.
  2463. * @retval None
  2464. */
  2465. void USB_ReadPMA(USB_TypeDef const *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
  2466. {
  2467. uint32_t n = (uint32_t)wNBytes >> 1;
  2468. uint32_t BaseAddr = (uint32_t)USBx;
  2469. uint32_t count;
  2470. uint32_t RdVal;
  2471. __IO uint16_t *pdwVal;
  2472. uint8_t *pBuf = pbUsrBuf;
  2473. pdwVal = (__IO uint16_t *)(BaseAddr + 0x400U + ((uint32_t)wPMABufAddr * PMA_ACCESS));
  2474. for (count = n; count != 0U; count--)
  2475. {
  2476. RdVal = *(__IO uint16_t *)pdwVal;
  2477. pdwVal++;
  2478. *pBuf = (uint8_t)((RdVal >> 0) & 0xFFU);
  2479. pBuf++;
  2480. *pBuf = (uint8_t)((RdVal >> 8) & 0xFFU);
  2481. pBuf++;
  2482. #if PMA_ACCESS > 1U
  2483. pdwVal++;
  2484. #endif /* PMA_ACCESS */
  2485. }
  2486. if ((wNBytes % 2U) != 0U)
  2487. {
  2488. RdVal = *pdwVal;
  2489. *pBuf = (uint8_t)((RdVal >> 0) & 0xFFU);
  2490. }
  2491. }
  2492. #endif /* defined (USB) */
  2493. /**
  2494. * @}
  2495. */
  2496. /**
  2497. * @}
  2498. */
  2499. #endif /* defined (USB) || defined (USB_OTG_FS) */
  2500. #endif /* defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED) */
  2501. /**
  2502. * @}
  2503. */