stm32l4xx_ll_usb.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_ll_usb.c
  4. * @author MCD Application Team
  5. * @brief USB Low Layer HAL module driver.
  6. *
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the USB Peripheral Controller:
  9. * + Initialization/de-initialization functions
  10. * + I/O operation functions
  11. * + Peripheral Control functions
  12. * + Peripheral State functions
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### How to use this driver #####
  17. ==============================================================================
  18. [..]
  19. (#) Fill parameters of Init structure in USB_OTG_CfgTypeDef structure.
  20. (#) Call USB_CoreInit() API to initialize the USB Core peripheral.
  21. (#) The upper HAL HCD/PCD driver will call the right routines for its internal processes.
  22. @endverbatim
  23. ******************************************************************************
  24. * @attention
  25. *
  26. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  27. *
  28. * Redistribution and use in source and binary forms, with or without modification,
  29. * are permitted provided that the following conditions are met:
  30. * 1. Redistributions of source code must retain the above copyright notice,
  31. * this list of conditions and the following disclaimer.
  32. * 2. Redistributions in binary form must reproduce the above copyright notice,
  33. * this list of conditions and the following disclaimer in the documentation
  34. * and/or other materials provided with the distribution.
  35. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  36. * may be used to endorse or promote products derived from this software
  37. * without specific prior written permission.
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  40. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  42. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  43. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  44. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  45. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  46. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  47. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  48. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. *
  50. ******************************************************************************
  51. */
  52. /* Includes ------------------------------------------------------------------*/
  53. #include "stm32l4xx_hal.h"
  54. /** @defgroup USB_LL USB Low Layer
  55. * @brief Low layer module for USB_FS and USB_OTG_FS drivers
  56. * @{
  57. */
  58. #if defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED)
  59. #if defined(STM32L432xx) || defined(STM32L433xx) || defined(STM32L442xx) || defined(STM32L443xx) || \
  60. defined(STM32L452xx) || defined(STM32L462xx) || \
  61. defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || \
  62. defined(STM32L496xx) || defined(STM32L4A6xx) || \
  63. defined(STM32L4R5xx) || defined(STM32L4R7xx) || defined(STM32L4R9xx) || defined(STM32L4S5xx) || defined(STM32L4S7xx) || defined(STM32L4S9xx)
  64. /** @addtogroup STM32L4xx_LL_USB_DRIVER
  65. * @{
  66. */
  67. /* Private typedef -----------------------------------------------------------*/
  68. /* Private define ------------------------------------------------------------*/
  69. /* Private macro -------------------------------------------------------------*/
  70. /* Private variables ---------------------------------------------------------*/
  71. /* Private function prototypes -----------------------------------------------*/
  72. /* Private functions ---------------------------------------------------------*/
  73. #if defined (USB_OTG_FS)
  74. /** @defgroup USB_LL_Private_Functions USB Low Layer Private Functions
  75. * @{
  76. */
  77. static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx);
  78. /**
  79. * @}
  80. */
  81. #endif /* USB_OTG_FS */
  82. /* Exported functions --------------------------------------------------------*/
  83. /** @defgroup LL_USB_Exported_Functions USB Low Layer Exported Functions
  84. * @{
  85. */
  86. /** @defgroup LL_USB_Group1 Initialization/de-initialization functions
  87. * @brief Initialization and Configuration functions
  88. *
  89. @verbatim
  90. ===============================================================================
  91. ##### Initialization/de-initialization functions #####
  92. ===============================================================================
  93. [..] This section provides functions allowing to:
  94. @endverbatim
  95. * @{
  96. */
  97. /*==============================================================================
  98. USB OTG FS peripheral available on STM32L475xx, STM32L476xx, STM32L485xx and
  99. STM32L486xx devices
  100. ==============================================================================*/
  101. #if defined (USB_OTG_FS)
  102. /**
  103. * @brief Initializes the USB Core
  104. * @param USBx: USB Instance
  105. * @param cfg: pointer to a USB_OTG_CfgTypeDef structure that contains
  106. * the configuration information for the specified USBx peripheral.
  107. * @retval HAL status
  108. */
  109. HAL_StatusTypeDef USB_CoreInit(USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
  110. {
  111. /* Prevent unused argument(s) compilation warning */
  112. UNUSED(cfg);
  113. /* Select FS Embedded PHY */
  114. USBx->GUSBCFG |= USB_OTG_GUSBCFG_PHYSEL;
  115. /* Reset after a PHY select and set Host mode */
  116. USB_CoreReset(USBx);
  117. /* Deactivate the power down*/
  118. USBx->GCCFG = USB_OTG_GCCFG_PWRDWN;
  119. return HAL_OK;
  120. }
  121. /**
  122. * @brief USB_EnableGlobalInt
  123. * Enables the controller's Global Int in the AHB Config reg
  124. * @param USBx: Selected device
  125. * @retval HAL status
  126. */
  127. HAL_StatusTypeDef USB_EnableGlobalInt(USB_OTG_GlobalTypeDef *USBx)
  128. {
  129. USBx->GAHBCFG |= USB_OTG_GAHBCFG_GINT;
  130. return HAL_OK;
  131. }
  132. /**
  133. * @brief USB_DisableGlobalInt
  134. * Disable the controller's Global Int in the AHB Config reg
  135. * @param USBx: Selected device
  136. * @retval HAL status
  137. */
  138. HAL_StatusTypeDef USB_DisableGlobalInt(USB_OTG_GlobalTypeDef *USBx)
  139. {
  140. USBx->GAHBCFG &= ~USB_OTG_GAHBCFG_GINT;
  141. return HAL_OK;
  142. }
  143. /**
  144. * @brief USB_SetCurrentMode : Set functional mode
  145. * @param USBx: Selected device
  146. * @param mode: current core mode
  147. * This parameter can be one of these values:
  148. * @arg USB_OTG_DEVICE_MODE: Peripheral mode
  149. * @arg USB_OTG_HOST_MODE: Host mode
  150. * @arg USB_OTG_DRD_MODE: Dual Role Device mode
  151. * @retval HAL status
  152. */
  153. HAL_StatusTypeDef USB_SetCurrentMode(USB_OTG_GlobalTypeDef *USBx , USB_ModeTypeDef mode)
  154. {
  155. USBx->GUSBCFG &= ~(USB_OTG_GUSBCFG_FHMOD | USB_OTG_GUSBCFG_FDMOD);
  156. if ( mode == USB_HOST_MODE)
  157. {
  158. USBx->GUSBCFG |= USB_OTG_GUSBCFG_FHMOD;
  159. }
  160. else if ( mode == USB_DEVICE_MODE)
  161. {
  162. USBx->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD;
  163. }
  164. HAL_Delay(50);
  165. return HAL_OK;
  166. }
  167. /**
  168. * @brief USB_DevInit : Initializes the USB_OTG controller registers
  169. * for device mode
  170. * @param USBx: Selected device
  171. * @param cfg: pointer to a USB_OTG_CfgTypeDef structure that contains
  172. * the configuration information for the specified USBx peripheral.
  173. * @retval HAL status
  174. */
  175. HAL_StatusTypeDef USB_DevInit (USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
  176. {
  177. uint32_t index = 0;
  178. /*Activate VBUS Sensing B */
  179. USBx->GCCFG |= USB_OTG_GCCFG_VBDEN;
  180. if (cfg.vbus_sensing_enable == 0)
  181. {
  182. /* Deactivate VBUS Sensing B */
  183. USBx->GCCFG &= ~ USB_OTG_GCCFG_VBDEN;
  184. /* B-peripheral session valid override enable*/
  185. USBx->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN;
  186. USBx->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL;
  187. }
  188. /* Restart the Phy Clock */
  189. USBx_PCGCCTL = 0;
  190. /* Device mode configuration */
  191. USBx_DEVICE->DCFG |= DCFG_FRAME_INTERVAL_80;
  192. /* Set Full speed phy */
  193. USB_SetDevSpeed (USBx , USB_OTG_SPEED_FULL);
  194. /* Flush the FIFOs */
  195. USB_FlushTxFifo(USBx , 0x10); /* all Tx FIFOs */
  196. USB_FlushRxFifo(USBx);
  197. /* Clear all pending Device Interrupts */
  198. USBx_DEVICE->DIEPMSK = 0;
  199. USBx_DEVICE->DOEPMSK = 0;
  200. USBx_DEVICE->DAINT = 0xFFFFFFFF;
  201. USBx_DEVICE->DAINTMSK = 0;
  202. for (index = 0; index < cfg.dev_endpoints; index++)
  203. {
  204. if ((USBx_INEP(index)->DIEPCTL & USB_OTG_DIEPCTL_EPENA) == USB_OTG_DIEPCTL_EPENA)
  205. {
  206. USBx_INEP(index)->DIEPCTL = (USB_OTG_DIEPCTL_EPDIS | USB_OTG_DIEPCTL_SNAK);
  207. }
  208. else
  209. {
  210. USBx_INEP(index)->DIEPCTL = 0;
  211. }
  212. USBx_INEP(index)->DIEPTSIZ = 0;
  213. USBx_INEP(index)->DIEPINT = 0xFF;
  214. }
  215. for (index = 0; index < cfg.dev_endpoints; index++)
  216. {
  217. if ((USBx_OUTEP(index)->DOEPCTL & USB_OTG_DOEPCTL_EPENA) == USB_OTG_DOEPCTL_EPENA)
  218. {
  219. USBx_OUTEP(index)->DOEPCTL = (USB_OTG_DOEPCTL_EPDIS | USB_OTG_DOEPCTL_SNAK);
  220. }
  221. else
  222. {
  223. USBx_OUTEP(index)->DOEPCTL = 0;
  224. }
  225. USBx_OUTEP(index)->DOEPTSIZ = 0;
  226. USBx_OUTEP(index)->DOEPINT = 0xFF;
  227. }
  228. USBx_DEVICE->DIEPMSK &= ~(USB_OTG_DIEPMSK_TXFURM);
  229. if (cfg.dma_enable == 1)
  230. {
  231. /*Set threshold parameters */
  232. USBx_DEVICE->DTHRCTL = (USB_OTG_DTHRCTL_TXTHRLEN_6 | USB_OTG_DTHRCTL_RXTHRLEN_6);
  233. USBx_DEVICE->DTHRCTL |= (USB_OTG_DTHRCTL_RXTHREN | USB_OTG_DTHRCTL_ISOTHREN | USB_OTG_DTHRCTL_NONISOTHREN);
  234. index= USBx_DEVICE->DTHRCTL;
  235. }
  236. /* Disable all interrupts. */
  237. USBx->GINTMSK = 0;
  238. /* Clear any pending interrupts */
  239. USBx->GINTSTS = 0xBFFFFFFF;
  240. /* Enable the common interrupts */
  241. if (cfg.dma_enable == DISABLE)
  242. {
  243. USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
  244. }
  245. /* Enable interrupts matching to the Device mode ONLY */
  246. USBx->GINTMSK |= (USB_OTG_GINTMSK_USBSUSPM | USB_OTG_GINTMSK_USBRST |\
  247. USB_OTG_GINTMSK_ENUMDNEM | USB_OTG_GINTMSK_IEPINT |\
  248. USB_OTG_GINTMSK_OEPINT | USB_OTG_GINTMSK_IISOIXFRM|\
  249. USB_OTG_GINTMSK_PXFRM_IISOOXFRM | USB_OTG_GINTMSK_WUIM);
  250. if(cfg.Sof_enable)
  251. {
  252. USBx->GINTMSK |= USB_OTG_GINTMSK_SOFM;
  253. }
  254. if (cfg.vbus_sensing_enable == ENABLE)
  255. {
  256. USBx->GINTMSK |= (USB_OTG_GINTMSK_SRQIM | USB_OTG_GINTMSK_OTGINT);
  257. }
  258. return HAL_OK;
  259. }
  260. /**
  261. * @brief USB_OTG_FlushTxFifo : Flush a Tx FIFO
  262. * @param USBx: Selected device
  263. * @param num: FIFO number
  264. * This parameter can be a value from 1 to 15
  265. 15 means Flush all Tx FIFOs
  266. * @retval HAL status
  267. */
  268. HAL_StatusTypeDef USB_FlushTxFifo (USB_OTG_GlobalTypeDef *USBx, uint32_t num)
  269. {
  270. uint32_t count = 0;
  271. USBx->GRSTCTL = ( USB_OTG_GRSTCTL_TXFFLSH |(uint32_t)( num << 6));
  272. do
  273. {
  274. if (++count > 200000)
  275. {
  276. return HAL_TIMEOUT;
  277. }
  278. }
  279. while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_TXFFLSH) == USB_OTG_GRSTCTL_TXFFLSH);
  280. return HAL_OK;
  281. }
  282. /**
  283. * @brief USB_FlushRxFifo : Flush Rx FIFO
  284. * @param USBx: Selected device
  285. * @retval HAL status
  286. */
  287. HAL_StatusTypeDef USB_FlushRxFifo(USB_OTG_GlobalTypeDef *USBx)
  288. {
  289. uint32_t count = 0;
  290. USBx->GRSTCTL = USB_OTG_GRSTCTL_RXFFLSH;
  291. do
  292. {
  293. if (++count > 200000)
  294. {
  295. return HAL_TIMEOUT;
  296. }
  297. }
  298. while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_RXFFLSH) == USB_OTG_GRSTCTL_RXFFLSH);
  299. return HAL_OK;
  300. }
  301. /**
  302. * @brief USB_SetDevSpeed :Initializes the DevSpd field of DCFG register
  303. * depending the PHY type and the enumeration speed of the device.
  304. * @param USBx: Selected device
  305. * @param speed: device speed
  306. * This parameter can be one of these values:
  307. * @arg USB_OTG_SPEED_HIGH: High speed mode
  308. * @arg USB_OTG_SPEED_HIGH_IN_FULL: High speed core in Full Speed mode
  309. * @arg USB_OTG_SPEED_FULL: Full speed mode
  310. * @arg USB_OTG_SPEED_LOW: Low speed mode
  311. * @retval Hal status
  312. */
  313. HAL_StatusTypeDef USB_SetDevSpeed(USB_OTG_GlobalTypeDef *USBx , uint8_t speed)
  314. {
  315. USBx_DEVICE->DCFG |= speed;
  316. return HAL_OK;
  317. }
  318. /**
  319. * @brief USB_GetDevSpeed :Return the Dev Speed
  320. * @param USBx: Selected device
  321. * @retval speed : device speed
  322. * This parameter can be one of these values:
  323. * @arg USB_OTG_SPEED_HIGH: High speed mode
  324. * @arg USB_OTG_SPEED_FULL: Full speed mode
  325. * @arg USB_OTG_SPEED_LOW: Low speed mode
  326. */
  327. uint8_t USB_GetDevSpeed(USB_OTG_GlobalTypeDef *USBx)
  328. {
  329. uint8_t speed = 0;
  330. if((USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD) == DSTS_ENUMSPD_HS_PHY_30MHZ_OR_60MHZ)
  331. {
  332. speed = USB_OTG_SPEED_HIGH;
  333. }
  334. else if (((USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD) == DSTS_ENUMSPD_FS_PHY_30MHZ_OR_60MHZ)||
  335. ((USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD) == DSTS_ENUMSPD_FS_PHY_48MHZ))
  336. {
  337. speed = USB_OTG_SPEED_FULL;
  338. }
  339. else if((USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD) == DSTS_ENUMSPD_LS_PHY_6MHZ)
  340. {
  341. speed = USB_OTG_SPEED_LOW;
  342. }
  343. return speed;
  344. }
  345. /**
  346. * @brief Activate and configure an endpoint
  347. * @param USBx: Selected device
  348. * @param ep: pointer to endpoint structure
  349. * @retval HAL status
  350. */
  351. HAL_StatusTypeDef USB_ActivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
  352. {
  353. if (ep->is_in == 1)
  354. {
  355. USBx_DEVICE->DAINTMSK |= USB_OTG_DAINTMSK_IEPM & ((1 << (ep->num)));
  356. if (((USBx_INEP(ep->num)->DIEPCTL) & USB_OTG_DIEPCTL_USBAEP) == 0)
  357. {
  358. USBx_INEP(ep->num)->DIEPCTL |= ((ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ ) | (ep->type << 18 ) |\
  359. ((ep->num) << 22 ) | (USB_OTG_DIEPCTL_SD0PID_SEVNFRM) | (USB_OTG_DIEPCTL_USBAEP));
  360. }
  361. }
  362. else
  363. {
  364. USBx_DEVICE->DAINTMSK |= USB_OTG_DAINTMSK_OEPM & ((1 << (ep->num)) << 16);
  365. if (((USBx_OUTEP(ep->num)->DOEPCTL) & USB_OTG_DOEPCTL_USBAEP) == 0)
  366. {
  367. USBx_OUTEP(ep->num)->DOEPCTL |= ((ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ ) | (ep->type << 18 ) |\
  368. (USB_OTG_DIEPCTL_SD0PID_SEVNFRM)| (USB_OTG_DOEPCTL_USBAEP));
  369. }
  370. }
  371. return HAL_OK;
  372. }
  373. /**
  374. * @brief Activate and configure a dedicated endpoint
  375. * @param USBx: Selected device
  376. * @param ep: pointer to endpoint structure
  377. * @retval HAL status
  378. */
  379. HAL_StatusTypeDef USB_ActivateDedicatedEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
  380. {
  381. static __IO uint32_t debug = 0;
  382. /* Read DEPCTLn register */
  383. if (ep->is_in == 1)
  384. {
  385. if (((USBx_INEP(ep->num)->DIEPCTL) & USB_OTG_DIEPCTL_USBAEP) == 0)
  386. {
  387. USBx_INEP(ep->num)->DIEPCTL |= ((ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ ) | (ep->type << 18 ) |\
  388. ((ep->num) << 22 ) | (USB_OTG_DIEPCTL_SD0PID_SEVNFRM) | (USB_OTG_DIEPCTL_USBAEP));
  389. }
  390. debug |= ((ep->maxpacket & USB_OTG_DIEPCTL_MPSIZ ) | (ep->type << 18 ) |\
  391. ((ep->num) << 22 ) | (USB_OTG_DIEPCTL_SD0PID_SEVNFRM) | (USB_OTG_DIEPCTL_USBAEP));
  392. USBx_DEVICE->DEACHMSK |= USB_OTG_DAINTMSK_IEPM & ((1 << (ep->num)));
  393. }
  394. else
  395. {
  396. if (((USBx_OUTEP(ep->num)->DOEPCTL) & USB_OTG_DOEPCTL_USBAEP) == 0)
  397. {
  398. USBx_OUTEP(ep->num)->DOEPCTL |= ((ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ ) | (ep->type << 18 ) |\
  399. ((ep->num) << 22 ) | (USB_OTG_DOEPCTL_USBAEP));
  400. debug = (uint32_t)(((uint32_t )USBx) + USB_OTG_OUT_ENDPOINT_BASE + (0)*USB_OTG_EP_REG_SIZE);
  401. debug = (uint32_t )&USBx_OUTEP(ep->num)->DOEPCTL;
  402. debug |= ((ep->maxpacket & USB_OTG_DOEPCTL_MPSIZ ) | (ep->type << 18 ) |\
  403. ((ep->num) << 22 ) | (USB_OTG_DOEPCTL_USBAEP));
  404. }
  405. USBx_DEVICE->DEACHMSK |= USB_OTG_DAINTMSK_OEPM & ((1 << (ep->num)) << 16);
  406. }
  407. return HAL_OK;
  408. }
  409. /**
  410. * @brief De-activate and de-initialize an endpoint
  411. * @param USBx: Selected device
  412. * @param ep: pointer to endpoint structure
  413. * @retval HAL status
  414. */
  415. HAL_StatusTypeDef USB_DeactivateEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
  416. {
  417. /* Read DEPCTLn register */
  418. if (ep->is_in == 1)
  419. {
  420. USBx_DEVICE->DEACHMSK &= ~(USB_OTG_DAINTMSK_IEPM & ((1 << (ep->num))));
  421. USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_IEPM & ((1 << (ep->num))));
  422. USBx_INEP(ep->num)->DIEPCTL &= ~ USB_OTG_DIEPCTL_USBAEP;
  423. }
  424. else
  425. {
  426. USBx_DEVICE->DEACHMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((1 << (ep->num)) << 16));
  427. USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((1 << (ep->num)) << 16));
  428. USBx_OUTEP(ep->num)->DOEPCTL &= ~USB_OTG_DOEPCTL_USBAEP;
  429. }
  430. return HAL_OK;
  431. }
  432. /**
  433. * @brief De-activate and de-initialize a dedicated endpoint
  434. * @param USBx: Selected device
  435. * @param ep: pointer to endpoint structure
  436. * @retval HAL status
  437. */
  438. HAL_StatusTypeDef USB_DeactivateDedicatedEndpoint(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
  439. {
  440. /* Read DEPCTLn register */
  441. if (ep->is_in == 1)
  442. {
  443. USBx_INEP(ep->num)->DIEPCTL &= ~ USB_OTG_DIEPCTL_USBAEP;
  444. USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_IEPM & ((1 << (ep->num))));
  445. }
  446. else
  447. {
  448. USBx_OUTEP(ep->num)->DOEPCTL &= ~USB_OTG_DOEPCTL_USBAEP;
  449. USBx_DEVICE->DAINTMSK &= ~(USB_OTG_DAINTMSK_OEPM & ((1 << (ep->num)) << 16));
  450. }
  451. return HAL_OK;
  452. }
  453. /**
  454. * @brief USB_EPStartXfer : setup and starts a transfer over an EP
  455. * @param USBx: Selected device
  456. * @param ep: pointer to endpoint structure
  457. * @param dma: USB dma enabled or disabled
  458. * This parameter can be one of these values:
  459. * 0 : DMA feature not used
  460. * 1 : DMA feature used
  461. * @retval HAL status
  462. */
  463. HAL_StatusTypeDef USB_EPStartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDef *ep, uint8_t dma)
  464. {
  465. uint16_t pktcnt = 0;
  466. /* IN endpoint */
  467. if (ep->is_in == 1)
  468. {
  469. /* Zero Length Packet? */
  470. if (ep->xfer_len == 0)
  471. {
  472. USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
  473. USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1 << 19)) ;
  474. USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
  475. }
  476. else
  477. {
  478. /* Program the transfer size and packet count
  479. * as follows: xfersize = N * maxpacket +
  480. * short_packet pktcnt = N + (short_packet
  481. * exist ? 1 : 0)
  482. */
  483. USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
  484. USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
  485. USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (((ep->xfer_len + ep->maxpacket -1)/ ep->maxpacket) << 19)) ;
  486. USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_XFRSIZ & ep->xfer_len);
  487. if (ep->type == EP_TYPE_ISOC)
  488. {
  489. USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_MULCNT);
  490. USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_MULCNT & (1 << 29));
  491. }
  492. }
  493. if (ep->type != EP_TYPE_ISOC)
  494. {
  495. /* Enable the Tx FIFO Empty Interrupt for this EP */
  496. if (ep->xfer_len > 0)
  497. {
  498. USBx_DEVICE->DIEPEMPMSK |= 1 << ep->num;
  499. }
  500. }
  501. if (ep->type == EP_TYPE_ISOC)
  502. {
  503. if ((USBx_DEVICE->DSTS & ( 1 << 8 )) == 0)
  504. {
  505. USBx_INEP(ep->num)->DIEPCTL |= USB_OTG_DIEPCTL_SODDFRM;
  506. }
  507. else
  508. {
  509. USBx_INEP(ep->num)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM;
  510. }
  511. }
  512. /* EP enable, IN data in FIFO */
  513. USBx_INEP(ep->num)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
  514. if (ep->type == EP_TYPE_ISOC)
  515. {
  516. USB_WritePacket(USBx, ep->xfer_buff, ep->num, ep->xfer_len, dma);
  517. }
  518. }
  519. else /* OUT endpoint */
  520. {
  521. /* Program the transfer size and packet count as follows:
  522. * pktcnt = N
  523. * xfersize = N * maxpacket
  524. */
  525. USBx_OUTEP(ep->num)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_XFRSIZ);
  526. USBx_OUTEP(ep->num)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_PKTCNT);
  527. if (ep->xfer_len == 0)
  528. {
  529. USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & ep->maxpacket);
  530. USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1 << 19)) ;
  531. }
  532. else
  533. {
  534. pktcnt = (ep->xfer_len + ep->maxpacket -1)/ ep->maxpacket;
  535. USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (pktcnt << 19)); ;
  536. USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & (ep->maxpacket * pktcnt));
  537. }
  538. if (ep->type == EP_TYPE_ISOC)
  539. {
  540. if ((USBx_DEVICE->DSTS & ( 1 << 8 )) == 0)
  541. {
  542. USBx_OUTEP(ep->num)->DOEPCTL |= USB_OTG_DOEPCTL_SODDFRM;
  543. }
  544. else
  545. {
  546. USBx_OUTEP(ep->num)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM;
  547. }
  548. }
  549. /* EP enable */
  550. USBx_OUTEP(ep->num)->DOEPCTL |= (USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
  551. }
  552. return HAL_OK;
  553. }
  554. /**
  555. * @brief USB_EP0StartXfer : setup and starts a transfer over the EP 0
  556. * @param USBx: Selected device
  557. * @param ep: pointer to endpoint structure
  558. * @param dma: USB dma enabled or disabled
  559. * This parameter can be one of these values:
  560. * 0 : DMA feature not used
  561. * 1 : DMA feature used
  562. * @retval HAL status
  563. */
  564. HAL_StatusTypeDef USB_EP0StartXfer(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDef *ep, uint8_t dma)
  565. {
  566. /* Prevent unused argument(s) compilation warning */
  567. UNUSED(USBx);
  568. UNUSED(dma);
  569. /* IN endpoint */
  570. if (ep->is_in == 1)
  571. {
  572. /* Zero Length Packet? */
  573. if (ep->xfer_len == 0)
  574. {
  575. USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
  576. USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1 << 19)) ;
  577. USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
  578. }
  579. else
  580. {
  581. /* Program the transfer size and packet count
  582. * as follows: xfersize = N * maxpacket +
  583. * short_packet pktcnt = N + (short_packet
  584. * exist ? 1 : 0)
  585. */
  586. USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_XFRSIZ);
  587. USBx_INEP(ep->num)->DIEPTSIZ &= ~(USB_OTG_DIEPTSIZ_PKTCNT);
  588. if(ep->xfer_len > ep->maxpacket)
  589. {
  590. ep->xfer_len = ep->maxpacket;
  591. }
  592. USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_PKTCNT & (1 << 19)) ;
  593. USBx_INEP(ep->num)->DIEPTSIZ |= (USB_OTG_DIEPTSIZ_XFRSIZ & ep->xfer_len);
  594. }
  595. /* Enable the Tx FIFO Empty Interrupt for this EP */
  596. if (ep->xfer_len > 0)
  597. {
  598. USBx_DEVICE->DIEPEMPMSK |= 1 << (ep->num);
  599. }
  600. /* EP enable, IN data in FIFO */
  601. USBx_INEP(ep->num)->DIEPCTL |= (USB_OTG_DIEPCTL_CNAK | USB_OTG_DIEPCTL_EPENA);
  602. }
  603. else /* OUT endpoint */
  604. {
  605. /* Program the transfer size and packet count as follows:
  606. * pktcnt = N
  607. * xfersize = N * maxpacket
  608. */
  609. USBx_OUTEP(ep->num)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_XFRSIZ);
  610. USBx_OUTEP(ep->num)->DOEPTSIZ &= ~(USB_OTG_DOEPTSIZ_PKTCNT);
  611. if (ep->xfer_len > 0)
  612. {
  613. ep->xfer_len = ep->maxpacket;
  614. }
  615. USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1 << 19));
  616. USBx_OUTEP(ep->num)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_XFRSIZ & (ep->maxpacket));
  617. /* EP enable */
  618. USBx_OUTEP(ep->num)->DOEPCTL |= (USB_OTG_DOEPCTL_CNAK | USB_OTG_DOEPCTL_EPENA);
  619. }
  620. return HAL_OK;
  621. }
  622. /**
  623. * @brief USB_WritePacket : Writes a packet into the Tx FIFO associated
  624. * with the EP/channel
  625. * @param USBx: Selected device
  626. * @param src: pointer to source buffer
  627. * @param ch_ep_num: endpoint or host channel number
  628. * @param len: Number of bytes to write
  629. * @param dma: USB dma enabled or disabled
  630. * This parameter can be one of these values:
  631. * 0 : DMA feature not used
  632. * 1 : DMA feature used
  633. * @retval HAL status
  634. */
  635. HAL_StatusTypeDef USB_WritePacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *src, uint8_t ch_ep_num, uint16_t len, uint8_t dma)
  636. {
  637. /* Prevent unused argument(s) compilation warning */
  638. UNUSED(USBx);
  639. UNUSED(dma);
  640. uint32_t count32b= 0 , index= 0;
  641. count32b = (len + 3) / 4;
  642. for (index = 0; index < count32b; index++, src += 4)
  643. {
  644. USBx_DFIFO(ch_ep_num) = *((__packed uint32_t *)src);
  645. }
  646. return HAL_OK;
  647. }
  648. /**
  649. * @brief USB_ReadPacket : read a packet from the Tx FIFO associated
  650. * with the EP/channel
  651. * @param USBx: Selected device
  652. * @param src: source pointer
  653. * @param ch_ep_num: endpoint or host channel number
  654. * @param len: Number of bytes to read
  655. * @param dma: USB dma enabled or disabled
  656. * This parameter can be one of these values:
  657. * 0 : DMA feature not used
  658. * 1 : DMA feature used
  659. * @retval pointer to destination buffer
  660. */
  661. void *USB_ReadPacket(USB_OTG_GlobalTypeDef *USBx, uint8_t *dest, uint16_t len)
  662. {
  663. uint32_t index=0;
  664. uint32_t count32b = (len + 3) / 4;
  665. for ( index = 0; index < count32b; index++, dest += 4 )
  666. {
  667. *(__packed uint32_t *)dest = USBx_DFIFO(0);
  668. }
  669. return ((void *)dest);
  670. }
  671. /**
  672. * @brief USB_EPSetStall : set a stall condition over an EP
  673. * @param USBx: Selected device
  674. * @param ep: pointer to endpoint structure
  675. * @retval HAL status
  676. */
  677. HAL_StatusTypeDef USB_EPSetStall(USB_OTG_GlobalTypeDef *USBx , USB_OTG_EPTypeDef *ep)
  678. {
  679. if (ep->is_in == 1)
  680. {
  681. if (((USBx_INEP(ep->num)->DIEPCTL) & USB_OTG_DIEPCTL_EPENA) == 0)
  682. {
  683. USBx_INEP(ep->num)->DIEPCTL &= ~(USB_OTG_DIEPCTL_EPDIS);
  684. }
  685. USBx_INEP(ep->num)->DIEPCTL |= USB_OTG_DIEPCTL_STALL;
  686. }
  687. else
  688. {
  689. if (((USBx_OUTEP(ep->num)->DOEPCTL) & USB_OTG_DOEPCTL_EPENA) == 0)
  690. {
  691. USBx_OUTEP(ep->num)->DOEPCTL &= ~(USB_OTG_DOEPCTL_EPDIS);
  692. }
  693. USBx_OUTEP(ep->num)->DOEPCTL |= USB_OTG_DOEPCTL_STALL;
  694. }
  695. return HAL_OK;
  696. }
  697. /**
  698. * @brief USB_EPClearStall : Clear a stall condition over an EP
  699. * @param USBx: Selected device
  700. * @param ep: pointer to endpoint structure
  701. * @retval HAL status
  702. */
  703. HAL_StatusTypeDef USB_EPClearStall(USB_OTG_GlobalTypeDef *USBx, USB_OTG_EPTypeDef *ep)
  704. {
  705. if (ep->is_in == 1)
  706. {
  707. USBx_INEP(ep->num)->DIEPCTL &= ~USB_OTG_DIEPCTL_STALL;
  708. if (ep->type == EP_TYPE_INTR || ep->type == EP_TYPE_BULK)
  709. {
  710. USBx_INEP(ep->num)->DIEPCTL |= USB_OTG_DIEPCTL_SD0PID_SEVNFRM; /* DATA0 */
  711. }
  712. }
  713. else
  714. {
  715. USBx_OUTEP(ep->num)->DOEPCTL &= ~USB_OTG_DOEPCTL_STALL;
  716. if (ep->type == EP_TYPE_INTR || ep->type == EP_TYPE_BULK)
  717. {
  718. USBx_OUTEP(ep->num)->DOEPCTL |= USB_OTG_DOEPCTL_SD0PID_SEVNFRM; /* DATA0 */
  719. }
  720. }
  721. return HAL_OK;
  722. }
  723. /**
  724. * @brief USB_StopDevice : Stop the USB device mode
  725. * @param USBx: Selected device
  726. * @retval HAL status
  727. */
  728. HAL_StatusTypeDef USB_StopDevice(USB_OTG_GlobalTypeDef *USBx)
  729. {
  730. uint32_t index;
  731. /* Clear Pending interrupt */
  732. for (index = 0; index < 15 ; index++)
  733. {
  734. USBx_INEP(index)->DIEPINT = 0xFF;
  735. USBx_OUTEP(index)->DOEPINT = 0xFF;
  736. }
  737. USBx_DEVICE->DAINT = 0xFFFFFFFF;
  738. /* Clear interrupt masks */
  739. USBx_DEVICE->DIEPMSK = 0;
  740. USBx_DEVICE->DOEPMSK = 0;
  741. USBx_DEVICE->DAINTMSK = 0;
  742. /* Flush the FIFO */
  743. USB_FlushRxFifo(USBx);
  744. USB_FlushTxFifo(USBx , 0x10 );
  745. return HAL_OK;
  746. }
  747. /**
  748. * @brief USB_SetDevAddress : Stop the USB device mode
  749. * @param USBx: Selected device
  750. * @param address: new device address to be assigned
  751. * This parameter can be a value from 0 to 255
  752. * @retval HAL status
  753. */
  754. HAL_StatusTypeDef USB_SetDevAddress (USB_OTG_GlobalTypeDef *USBx, uint8_t address)
  755. {
  756. USBx_DEVICE->DCFG &= ~ (USB_OTG_DCFG_DAD);
  757. USBx_DEVICE->DCFG |= (address << 4) & USB_OTG_DCFG_DAD ;
  758. return HAL_OK;
  759. }
  760. /**
  761. * @brief USB_DevConnect : Connect the USB device by enabling the pull-up/pull-down
  762. * @param USBx: Selected device
  763. * @retval HAL status
  764. */
  765. HAL_StatusTypeDef USB_DevConnect (USB_OTG_GlobalTypeDef *USBx)
  766. {
  767. USBx_DEVICE->DCTL &= ~USB_OTG_DCTL_SDIS ;
  768. HAL_Delay(3);
  769. return HAL_OK;
  770. }
  771. /**
  772. * @brief USB_DevDisconnect : Disconnect the USB device by disabling the pull-up/pull-down
  773. * @param USBx: Selected device
  774. * @retval HAL status
  775. */
  776. HAL_StatusTypeDef USB_DevDisconnect (USB_OTG_GlobalTypeDef *USBx)
  777. {
  778. USBx_DEVICE->DCTL |= USB_OTG_DCTL_SDIS ;
  779. HAL_Delay(3);
  780. return HAL_OK;
  781. }
  782. /**
  783. * @brief USB_ReadInterrupts: return the global USB interrupt status
  784. * @param USBx: Selected device
  785. * @retval HAL status
  786. */
  787. uint32_t USB_ReadInterrupts (USB_OTG_GlobalTypeDef *USBx)
  788. {
  789. uint32_t tmpreg = 0;
  790. tmpreg = USBx->GINTSTS;
  791. tmpreg &= USBx->GINTMSK;
  792. return tmpreg;
  793. }
  794. /**
  795. * @brief USB_ReadDevAllOutEpInterrupt: return the USB device OUT endpoints interrupt status
  796. * @param USBx: Selected device
  797. * @retval HAL status
  798. */
  799. uint32_t USB_ReadDevAllOutEpInterrupt (USB_OTG_GlobalTypeDef *USBx)
  800. {
  801. uint32_t tmpreg;
  802. tmpreg = USBx_DEVICE->DAINT;
  803. tmpreg &= USBx_DEVICE->DAINTMSK;
  804. return ((tmpreg & 0xffff0000) >> 16);
  805. }
  806. /**
  807. * @brief USB_ReadDevAllInEpInterrupt: return the USB device IN endpoints interrupt status
  808. * @param USBx: Selected device
  809. * @retval HAL status
  810. */
  811. uint32_t USB_ReadDevAllInEpInterrupt (USB_OTG_GlobalTypeDef *USBx)
  812. {
  813. uint32_t tmpreg;
  814. tmpreg = USBx_DEVICE->DAINT;
  815. tmpreg &= USBx_DEVICE->DAINTMSK;
  816. return ((tmpreg & 0xFFFF));
  817. }
  818. /**
  819. * @brief Returns Device OUT EP Interrupt register
  820. * @param USBx: Selected device
  821. * @param epnum: endpoint number
  822. * This parameter can be a value from 0 to 15
  823. * @retval Device OUT EP Interrupt register
  824. */
  825. uint32_t USB_ReadDevOutEPInterrupt (USB_OTG_GlobalTypeDef *USBx , uint8_t epnum)
  826. {
  827. uint32_t tmpreg;
  828. tmpreg = USBx_OUTEP(epnum)->DOEPINT;
  829. tmpreg &= USBx_DEVICE->DOEPMSK;
  830. return tmpreg;
  831. }
  832. /**
  833. * @brief Returns Device IN EP Interrupt register
  834. * @param USBx: Selected device
  835. * @param epnum: endpoint number
  836. * This parameter can be a value from 0 to 15
  837. * @retval Device IN EP Interrupt register
  838. */
  839. uint32_t USB_ReadDevInEPInterrupt (USB_OTG_GlobalTypeDef *USBx , uint8_t epnum)
  840. {
  841. uint32_t tmpreg = 0, msk = 0, emp = 0;
  842. msk = USBx_DEVICE->DIEPMSK;
  843. emp = USBx_DEVICE->DIEPEMPMSK;
  844. msk |= ((emp >> epnum) & 0x1) << 7;
  845. tmpreg = USBx_INEP(epnum)->DIEPINT & msk;
  846. return tmpreg;
  847. }
  848. /**
  849. * @brief USB_ClearInterrupts: clear a USB interrupt
  850. * @param USBx: Selected device
  851. * @param interrupt: interrupt flag
  852. * @retval None
  853. */
  854. void USB_ClearInterrupts (USB_OTG_GlobalTypeDef *USBx, uint32_t interrupt)
  855. {
  856. USBx->GINTSTS |= interrupt;
  857. }
  858. /**
  859. * @brief Returns USB core mode
  860. * @param USBx: Selected device
  861. * @retval return core mode : Host or Device
  862. * This parameter can be one of these values:
  863. * 0 : Host
  864. * 1 : Device
  865. */
  866. uint32_t USB_GetMode(USB_OTG_GlobalTypeDef *USBx)
  867. {
  868. return ((USBx->GINTSTS ) & 0x1);
  869. }
  870. /**
  871. * @brief Activate EP0 for Setup transactions
  872. * @param USBx: Selected device
  873. * @retval HAL status
  874. */
  875. HAL_StatusTypeDef USB_ActivateSetup (USB_OTG_GlobalTypeDef *USBx)
  876. {
  877. /* Set the MPS of the IN EP based on the enumeration speed */
  878. USBx_INEP(0)->DIEPCTL &= ~USB_OTG_DIEPCTL_MPSIZ;
  879. if((USBx_DEVICE->DSTS & USB_OTG_DSTS_ENUMSPD) == DSTS_ENUMSPD_LS_PHY_6MHZ)
  880. {
  881. USBx_INEP(0)->DIEPCTL |= 3;
  882. }
  883. USBx_DEVICE->DCTL |= USB_OTG_DCTL_CGINAK;
  884. return HAL_OK;
  885. }
  886. /**
  887. * @brief Prepare the EP0 to start the first control setup
  888. * @param USBx: Selected device
  889. * @param dma: USB dma enabled or disabled
  890. * This parameter can be one of these values:
  891. * 0 : DMA feature not used
  892. * 1 : DMA feature used
  893. * @param psetup: pointer to setup packet
  894. * @retval HAL status
  895. */
  896. HAL_StatusTypeDef USB_EP0_OutStart(USB_OTG_GlobalTypeDef *USBx, uint8_t dma, uint8_t *psetup)
  897. {
  898. /* Prevent unused argument(s) compilation warning */
  899. UNUSED(psetup);
  900. USBx_OUTEP(0)->DOEPTSIZ = 0;
  901. USBx_OUTEP(0)->DOEPTSIZ |= (USB_OTG_DOEPTSIZ_PKTCNT & (1 << 19)) ;
  902. USBx_OUTEP(0)->DOEPTSIZ |= (3 * 8);
  903. USBx_OUTEP(0)->DOEPTSIZ |= USB_OTG_DOEPTSIZ_STUPCNT;
  904. return HAL_OK;
  905. }
  906. /**
  907. * @brief USB_HostInit : Initializes the USB OTG controller registers
  908. * for Host mode
  909. * @param USBx: Selected device
  910. * @param cfg: pointer to a USB_OTG_CfgTypeDef structure that contains
  911. * the configuration information for the specified USBx peripheral.
  912. * @retval HAL status
  913. */
  914. HAL_StatusTypeDef USB_HostInit (USB_OTG_GlobalTypeDef *USBx, USB_OTG_CfgTypeDef cfg)
  915. {
  916. uint32_t index = 0;
  917. /* Restart the Phy Clock */
  918. USBx_PCGCCTL = 0;
  919. /* Disable the FS/LS support mode only */
  920. if((cfg.speed == USB_OTG_SPEED_FULL)&&
  921. (USBx != USB_OTG_FS))
  922. {
  923. USBx_HOST->HCFG |= USB_OTG_HCFG_FSLSS;
  924. }
  925. else
  926. {
  927. USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSS);
  928. }
  929. /* Make sure the FIFOs are flushed. */
  930. USB_FlushTxFifo(USBx, 0x10 ); /* all Tx FIFOs */
  931. USB_FlushRxFifo(USBx);
  932. /* Clear all pending HC Interrupts */
  933. for (index = 0; index < cfg.Host_channels; index++)
  934. {
  935. USBx_HC(index)->HCINT = 0xFFFFFFFF;
  936. USBx_HC(index)->HCINTMSK = 0;
  937. }
  938. /* Enable VBUS driving */
  939. USB_DriveVbus(USBx, 1);
  940. HAL_Delay(200);
  941. /* Disable all interrupts. */
  942. USBx->GINTMSK = 0;
  943. /* Clear any pending interrupts */
  944. USBx->GINTSTS = 0xFFFFFFFF;
  945. /* set Rx FIFO size */
  946. USBx->GRXFSIZ = (uint32_t )0x80;
  947. USBx->DIEPTXF0_HNPTXFSIZ = (uint32_t )(((0x60 << 16)& USB_OTG_NPTXFD) | 0x80);
  948. USBx->HPTXFSIZ = (uint32_t )(((0x40 << 16)& USB_OTG_HPTXFSIZ_PTXFD) | 0xE0);
  949. /* Enable the common interrupts */
  950. if (cfg.dma_enable == DISABLE)
  951. {
  952. USBx->GINTMSK |= USB_OTG_GINTMSK_RXFLVLM;
  953. }
  954. /* Enable interrupts matching to the Host mode ONLY */
  955. USBx->GINTMSK |= (USB_OTG_GINTMSK_PRTIM | USB_OTG_GINTMSK_HCIM |\
  956. USB_OTG_GINTMSK_SOFM |USB_OTG_GINTSTS_DISCINT|\
  957. USB_OTG_GINTMSK_PXFRM_IISOOXFRM | USB_OTG_GINTMSK_WUIM);
  958. return HAL_OK;
  959. }
  960. /**
  961. * @brief USB_InitFSLSPClkSel : Initializes the FSLSPClkSel field of the
  962. * HCFG register on the PHY type and set the right frame interval
  963. * @param USBx: Selected device
  964. * @param freq: clock frequency
  965. * This parameter can be one of these values:
  966. * HCFG_48_MHZ : Full Speed 48 MHz Clock
  967. * HCFG_6_MHZ : Low Speed 6 MHz Clock
  968. * @retval HAL status
  969. */
  970. HAL_StatusTypeDef USB_InitFSLSPClkSel(USB_OTG_GlobalTypeDef *USBx , uint8_t freq)
  971. {
  972. USBx_HOST->HCFG &= ~(USB_OTG_HCFG_FSLSPCS);
  973. USBx_HOST->HCFG |= (freq & USB_OTG_HCFG_FSLSPCS);
  974. if (freq == HCFG_48_MHZ)
  975. {
  976. USBx_HOST->HFIR = (uint32_t)48000;
  977. }
  978. else if (freq == HCFG_6_MHZ)
  979. {
  980. USBx_HOST->HFIR = (uint32_t)6000;
  981. }
  982. return HAL_OK;
  983. }
  984. /**
  985. * @brief USB_OTG_ResetPort : Reset Host Port
  986. * @param USBx: Selected device
  987. * @retval HAL status
  988. * @note (1)The application must wait at least 10 ms
  989. * before clearing the reset bit.
  990. */
  991. HAL_StatusTypeDef USB_ResetPort(USB_OTG_GlobalTypeDef *USBx)
  992. {
  993. __IO uint32_t hprt0 = 0;
  994. hprt0 = USBx_HPRT0;
  995. hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
  996. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
  997. USBx_HPRT0 = (USB_OTG_HPRT_PRST | hprt0);
  998. HAL_Delay (10); /* See Note #1 */
  999. USBx_HPRT0 = ((~USB_OTG_HPRT_PRST) & hprt0);
  1000. return HAL_OK;
  1001. }
  1002. /**
  1003. * @brief USB_DriveVbus : activate or de-activate vbus
  1004. * @param state: VBUS state
  1005. * This parameter can be one of these values:
  1006. * 0 : VBUS Active
  1007. * 1 : VBUS Inactive
  1008. * @retval HAL status
  1009. */
  1010. HAL_StatusTypeDef USB_DriveVbus (USB_OTG_GlobalTypeDef *USBx, uint8_t state)
  1011. {
  1012. __IO uint32_t hprt0 = 0;
  1013. hprt0 = USBx_HPRT0;
  1014. hprt0 &= ~(USB_OTG_HPRT_PENA | USB_OTG_HPRT_PCDET |\
  1015. USB_OTG_HPRT_PENCHNG | USB_OTG_HPRT_POCCHNG );
  1016. if (((hprt0 & USB_OTG_HPRT_PPWR) == 0 ) && (state == 1 ))
  1017. {
  1018. USBx_HPRT0 = (USB_OTG_HPRT_PPWR | hprt0);
  1019. }
  1020. if (((hprt0 & USB_OTG_HPRT_PPWR) == USB_OTG_HPRT_PPWR) && (state == 0 ))
  1021. {
  1022. USBx_HPRT0 = ((~USB_OTG_HPRT_PPWR) & hprt0);
  1023. }
  1024. return HAL_OK;
  1025. }
  1026. /**
  1027. * @brief Return Host Core speed
  1028. * @param USBx: Selected device
  1029. * @retval speed : Host speed
  1030. * This parameter can be one of these values:
  1031. * @arg USB_OTG_SPEED_HIGH: High speed mode
  1032. * @arg USB_OTG_SPEED_FULL: Full speed mode
  1033. * @arg USB_OTG_SPEED_LOW: Low speed mode
  1034. */
  1035. uint32_t USB_GetHostSpeed (USB_OTG_GlobalTypeDef *USBx)
  1036. {
  1037. __IO uint32_t hprt0 = 0;
  1038. hprt0 = USBx_HPRT0;
  1039. return ((hprt0 & USB_OTG_HPRT_PSPD) >> 17);
  1040. }
  1041. /**
  1042. * @brief Return Host Current Frame number
  1043. * @param USBx: Selected device
  1044. * @retval current frame number
  1045. */
  1046. uint32_t USB_GetCurrentFrame (USB_OTG_GlobalTypeDef *USBx)
  1047. {
  1048. return (USBx_HOST->HFNUM & USB_OTG_HFNUM_FRNUM);
  1049. }
  1050. /**
  1051. * @brief Initialize a host channel
  1052. * @param USBx: Selected device
  1053. * @param ch_num : Channel number
  1054. * This parameter can be a value from 1 to 15
  1055. * @param epnum: Endpoint number
  1056. * This parameter can be a value from 1 to 15
  1057. * @param dev_address: Current device address
  1058. * This parameter can be a value from 0 to 255
  1059. * @param speed: Current device speed
  1060. * This parameter can be one of these values:
  1061. * @arg USB_OTG_SPEED_HIGH: High speed mode
  1062. * @arg USB_OTG_SPEED_FULL: Full speed mode
  1063. * @arg USB_OTG_SPEED_LOW: Low speed mode
  1064. * @param ep_type: Endpoint Type
  1065. * This parameter can be one of these values:
  1066. * @arg EP_TYPE_CTRL: Control type
  1067. * @arg EP_TYPE_ISOC: Isochronous type
  1068. * @arg EP_TYPE_BULK: Bulk type
  1069. * @arg EP_TYPE_INTR: Interrupt type
  1070. * @param mps: Max Packet Size
  1071. * This parameter can be a value from 0 to32K
  1072. * @retval HAL state
  1073. */
  1074. HAL_StatusTypeDef USB_HC_Init(USB_OTG_GlobalTypeDef *USBx,
  1075. uint8_t ch_num,
  1076. uint8_t epnum,
  1077. uint8_t dev_address,
  1078. uint8_t speed,
  1079. uint8_t ep_type,
  1080. uint16_t mps)
  1081. {
  1082. /* Clear old interrupt conditions for this host channel. */
  1083. USBx_HC(ch_num)->HCINT = 0xFFFFFFFF;
  1084. /* Enable channel interrupts required for this transfer. */
  1085. switch (ep_type)
  1086. {
  1087. case EP_TYPE_CTRL:
  1088. case EP_TYPE_BULK:
  1089. USBx_HC(ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |\
  1090. USB_OTG_HCINTMSK_STALLM |\
  1091. USB_OTG_HCINTMSK_TXERRM |\
  1092. USB_OTG_HCINTMSK_DTERRM |\
  1093. USB_OTG_HCINTMSK_AHBERR |\
  1094. USB_OTG_HCINTMSK_NAKM ;
  1095. if (epnum & 0x80)
  1096. {
  1097. USBx_HC(ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
  1098. }
  1099. break;
  1100. case EP_TYPE_INTR:
  1101. USBx_HC(ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |\
  1102. USB_OTG_HCINTMSK_STALLM |\
  1103. USB_OTG_HCINTMSK_TXERRM |\
  1104. USB_OTG_HCINTMSK_DTERRM |\
  1105. USB_OTG_HCINTMSK_NAKM |\
  1106. USB_OTG_HCINTMSK_AHBERR |\
  1107. USB_OTG_HCINTMSK_FRMORM ;
  1108. if (epnum & 0x80)
  1109. {
  1110. USBx_HC(ch_num)->HCINTMSK |= USB_OTG_HCINTMSK_BBERRM;
  1111. }
  1112. break;
  1113. case EP_TYPE_ISOC:
  1114. USBx_HC(ch_num)->HCINTMSK = USB_OTG_HCINTMSK_XFRCM |\
  1115. USB_OTG_HCINTMSK_ACKM |\
  1116. USB_OTG_HCINTMSK_AHBERR |\
  1117. USB_OTG_HCINTMSK_FRMORM ;
  1118. if (epnum & 0x80)
  1119. {
  1120. USBx_HC(ch_num)->HCINTMSK |= (USB_OTG_HCINTMSK_TXERRM | USB_OTG_HCINTMSK_BBERRM);
  1121. }
  1122. break;
  1123. }
  1124. /* Enable the top level host channel interrupt. */
  1125. USBx_HOST->HAINTMSK |= (1 << ch_num);
  1126. /* Make sure host channel interrupts are enabled. */
  1127. USBx->GINTMSK |= USB_OTG_GINTMSK_HCIM;
  1128. /* Program the HCCHAR register */
  1129. USBx_HC(ch_num)->HCCHAR = (((dev_address << 22) & USB_OTG_HCCHAR_DAD) |\
  1130. (((epnum & 0x7F)<< 11) & USB_OTG_HCCHAR_EPNUM)|\
  1131. ((((epnum & 0x80) == 0x80)<< 15) & USB_OTG_HCCHAR_EPDIR)|\
  1132. (((speed == HPRT0_PRTSPD_LOW_SPEED)<< 17) & USB_OTG_HCCHAR_LSDEV)|\
  1133. ((ep_type << 18) & USB_OTG_HCCHAR_EPTYP)|\
  1134. (mps & USB_OTG_HCCHAR_MPSIZ));
  1135. if (ep_type == EP_TYPE_INTR)
  1136. {
  1137. USBx_HC(ch_num)->HCCHAR |= USB_OTG_HCCHAR_ODDFRM ;
  1138. }
  1139. return HAL_OK;
  1140. }
  1141. /**
  1142. * @brief Start a transfer over a host channel
  1143. * @param USBx: Selected device
  1144. * @param hc: pointer to host channel structure
  1145. * @param dma: USB dma enabled or disabled
  1146. * This parameter can be one of these values:
  1147. * 0 : DMA feature not used
  1148. * 1 : DMA feature used
  1149. * @retval HAL state
  1150. */
  1151. #if defined (__CC_ARM) /*!< ARM Compiler */
  1152. #pragma O0
  1153. #elif defined (__GNUC__) /*!< GNU Compiler */
  1154. #pragma GCC optimize ("O0")
  1155. #endif /* __CC_ARM */
  1156. HAL_StatusTypeDef USB_HC_StartXfer(USB_OTG_GlobalTypeDef *USBx, USB_OTG_HCTypeDef *hc, uint8_t dma)
  1157. {
  1158. uint8_t is_oddframe = 0;
  1159. uint16_t len_words = 0;
  1160. uint16_t num_packets = 0;
  1161. uint16_t max_hc_pkt_count = 256;
  1162. uint32_t tmpreg = 0;
  1163. /* Compute the expected number of packets associated to the transfer */
  1164. if (hc->xfer_len > 0)
  1165. {
  1166. num_packets = (hc->xfer_len + hc->max_packet - 1) / hc->max_packet;
  1167. if (num_packets > max_hc_pkt_count)
  1168. {
  1169. num_packets = max_hc_pkt_count;
  1170. hc->xfer_len = num_packets * hc->max_packet;
  1171. }
  1172. }
  1173. else
  1174. {
  1175. num_packets = 1;
  1176. }
  1177. if (hc->ep_is_in)
  1178. {
  1179. hc->xfer_len = num_packets * hc->max_packet;
  1180. }
  1181. /* Initialize the HCTSIZn register */
  1182. USBx_HC(hc->ch_num)->HCTSIZ = (((hc->xfer_len) & USB_OTG_HCTSIZ_XFRSIZ)) |\
  1183. ((num_packets << 19) & USB_OTG_HCTSIZ_PKTCNT) |\
  1184. (((hc->data_pid) << 29) & USB_OTG_HCTSIZ_DPID);
  1185. if (dma)
  1186. {
  1187. /* xfer_buff MUST be 32-bits aligned */
  1188. USBx_HC(hc->ch_num)->HCDMA = (uint32_t)hc->xfer_buff;
  1189. }
  1190. is_oddframe = (USBx_HOST->HFNUM & 0x01) ? 0 : 1;
  1191. USBx_HC(hc->ch_num)->HCCHAR &= ~USB_OTG_HCCHAR_ODDFRM;
  1192. USBx_HC(hc->ch_num)->HCCHAR |= (is_oddframe << 29);
  1193. /* Set host channel enable */
  1194. tmpreg = USBx_HC(hc->ch_num)->HCCHAR;
  1195. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  1196. tmpreg |= USB_OTG_HCCHAR_CHENA;
  1197. USBx_HC(hc->ch_num)->HCCHAR = tmpreg;
  1198. if (dma == 0) /* Slave mode */
  1199. {
  1200. if((hc->ep_is_in == 0) && (hc->xfer_len > 0))
  1201. {
  1202. switch(hc->ep_type)
  1203. {
  1204. /* Non periodic transfer */
  1205. case EP_TYPE_CTRL:
  1206. case EP_TYPE_BULK:
  1207. len_words = (hc->xfer_len + 3) / 4;
  1208. /* check if there is enough space in FIFO space */
  1209. if(len_words > (USBx->HNPTXSTS & 0xFFFF))
  1210. {
  1211. /* need to process data in nptxfempty interrupt */
  1212. USBx->GINTMSK |= USB_OTG_GINTMSK_NPTXFEM;
  1213. }
  1214. break;
  1215. /* Periodic transfer */
  1216. case EP_TYPE_INTR:
  1217. case EP_TYPE_ISOC:
  1218. len_words = (hc->xfer_len + 3) / 4;
  1219. /* check if there is enough space in FIFO space */
  1220. if(len_words > (USBx_HOST->HPTXSTS & 0xFFFF)) /* split the transfer */
  1221. {
  1222. /* need to process data in ptxfempty interrupt */
  1223. USBx->GINTMSK |= USB_OTG_GINTMSK_PTXFEM;
  1224. }
  1225. break;
  1226. default:
  1227. break;
  1228. }
  1229. /* Write packet into the Tx FIFO. */
  1230. USB_WritePacket(USBx, hc->xfer_buff, hc->ch_num, hc->xfer_len, 0);
  1231. }
  1232. }
  1233. return HAL_OK;
  1234. }
  1235. /**
  1236. * @brief Read all host channel interrupts status
  1237. * @param USBx: Selected device
  1238. * @retval HAL state
  1239. */
  1240. uint32_t USB_HC_ReadInterrupt (USB_OTG_GlobalTypeDef *USBx)
  1241. {
  1242. return ((USBx_HOST->HAINT) & 0xFFFF);
  1243. }
  1244. /**
  1245. * @brief Halt a host channel
  1246. * @param USBx: Selected device
  1247. * @param hc_num: Host Channel number
  1248. * This parameter can be a value from 1 to 15
  1249. * @retval HAL state
  1250. */
  1251. HAL_StatusTypeDef USB_HC_Halt(USB_OTG_GlobalTypeDef *USBx , uint8_t hc_num)
  1252. {
  1253. uint32_t count = 0;
  1254. /* Check for space in the request queue to issue the halt. */
  1255. if (((USBx_HC(hc_num)->HCCHAR) & (HCCHAR_CTRL << 18)) || ((USBx_HC(hc_num)->HCCHAR) & (HCCHAR_BULK << 18)))
  1256. {
  1257. USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHDIS;
  1258. if ((USBx->HNPTXSTS & 0xFFFF) == 0)
  1259. {
  1260. USBx_HC(hc_num)->HCCHAR &= ~USB_OTG_HCCHAR_CHENA;
  1261. USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  1262. USBx_HC(hc_num)->HCCHAR &= ~USB_OTG_HCCHAR_EPDIR;
  1263. do
  1264. {
  1265. if (++count > 1000)
  1266. {
  1267. break;
  1268. }
  1269. }
  1270. while ((USBx_HC(hc_num)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
  1271. }
  1272. else
  1273. {
  1274. USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  1275. }
  1276. }
  1277. else
  1278. {
  1279. USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHDIS;
  1280. if ((USBx_HOST->HPTXSTS & 0xFFFF) == 0)
  1281. {
  1282. USBx_HC(hc_num)->HCCHAR &= ~USB_OTG_HCCHAR_CHENA;
  1283. USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  1284. USBx_HC(hc_num)->HCCHAR &= ~USB_OTG_HCCHAR_EPDIR;
  1285. do
  1286. {
  1287. if (++count > 1000)
  1288. {
  1289. break;
  1290. }
  1291. }
  1292. while ((USBx_HC(hc_num)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
  1293. }
  1294. else
  1295. {
  1296. USBx_HC(hc_num)->HCCHAR |= USB_OTG_HCCHAR_CHENA;
  1297. }
  1298. }
  1299. return HAL_OK;
  1300. }
  1301. /**
  1302. * @brief Initiate Do Ping protocol
  1303. * @param USBx: Selected device
  1304. * @param hc_num: Host Channel number
  1305. * This parameter can be a value from 1 to 15
  1306. * @retval HAL state
  1307. */
  1308. HAL_StatusTypeDef USB_DoPing(USB_OTG_GlobalTypeDef *USBx , uint8_t ch_num)
  1309. {
  1310. uint8_t num_packets = 1;
  1311. uint32_t tmpreg = 0;
  1312. USBx_HC(ch_num)->HCTSIZ = ((num_packets << 19) & USB_OTG_HCTSIZ_PKTCNT) |\
  1313. USB_OTG_HCTSIZ_DOPING;
  1314. /* Set host channel enable */
  1315. tmpreg = USBx_HC(ch_num)->HCCHAR;
  1316. tmpreg &= ~USB_OTG_HCCHAR_CHDIS;
  1317. tmpreg |= USB_OTG_HCCHAR_CHENA;
  1318. USBx_HC(ch_num)->HCCHAR = tmpreg;
  1319. return HAL_OK;
  1320. }
  1321. /**
  1322. * @brief Stop Host Core
  1323. * @param USBx: Selected device
  1324. * @retval HAL state
  1325. */
  1326. HAL_StatusTypeDef USB_StopHost(USB_OTG_GlobalTypeDef *USBx)
  1327. {
  1328. uint8_t index;
  1329. uint32_t count = 0;
  1330. uint32_t value = 0;
  1331. USB_DisableGlobalInt(USBx);
  1332. /* Flush FIFO */
  1333. USB_FlushTxFifo(USBx, 0x10);
  1334. USB_FlushRxFifo(USBx);
  1335. /* Flush out any leftover queued requests. */
  1336. for (index = 0; index <= 15; index++)
  1337. {
  1338. value = USBx_HC(index)->HCCHAR;
  1339. value |= USB_OTG_HCCHAR_CHDIS;
  1340. value &= ~USB_OTG_HCCHAR_CHENA;
  1341. value &= ~USB_OTG_HCCHAR_EPDIR;
  1342. USBx_HC(index)->HCCHAR = value;
  1343. }
  1344. /* Halt all channels to put them into a known state. */
  1345. for (index = 0; index <= 15; index++)
  1346. {
  1347. value = USBx_HC(index)->HCCHAR ;
  1348. value |= USB_OTG_HCCHAR_CHDIS;
  1349. value |= USB_OTG_HCCHAR_CHENA;
  1350. value &= ~USB_OTG_HCCHAR_EPDIR;
  1351. USBx_HC(index)->HCCHAR = value;
  1352. USBx_HC(index)->HCCHAR = value;
  1353. do
  1354. {
  1355. if (++count > 1000)
  1356. {
  1357. break;
  1358. }
  1359. }
  1360. while ((USBx_HC(index)->HCCHAR & USB_OTG_HCCHAR_CHENA) == USB_OTG_HCCHAR_CHENA);
  1361. }
  1362. /* Clear any pending Host interrupts */
  1363. USBx_HOST->HAINT = 0xFFFFFFFF;
  1364. USBx->GINTSTS = 0xFFFFFFFF;
  1365. USB_EnableGlobalInt(USBx);
  1366. return HAL_OK;
  1367. }
  1368. /**
  1369. * @brief USB_ActivateRemoteWakeup : active remote wakeup signalling
  1370. * @param USBx : Selected device
  1371. * @retval HAL status
  1372. */
  1373. HAL_StatusTypeDef USB_ActivateRemoteWakeup(USB_OTG_GlobalTypeDef *USBx)
  1374. {
  1375. if((USBx_DEVICE->DSTS & USB_OTG_DSTS_SUSPSTS) == USB_OTG_DSTS_SUSPSTS)
  1376. {
  1377. /* active Remote wakeup signalling */
  1378. USBx_DEVICE->DCTL |= USB_OTG_DCTL_RWUSIG;
  1379. }
  1380. return HAL_OK;
  1381. }
  1382. /**
  1383. * @brief USB_DeActivateRemoteWakeup : de-active remote wakeup signalling
  1384. * @param USBx : Selected device
  1385. * @retval HAL status
  1386. */
  1387. HAL_StatusTypeDef USB_DeActivateRemoteWakeup(USB_OTG_GlobalTypeDef *USBx)
  1388. {
  1389. /* active Remote wakeup signalling */
  1390. USBx_DEVICE->DCTL &= ~(USB_OTG_DCTL_RWUSIG);
  1391. return HAL_OK;
  1392. }
  1393. #endif /* USB_OTG_FS */
  1394. /*==============================================================================
  1395. USB Device FS peripheral available on STM32L432xx, STM32L433xx, STM32L442xx)
  1396. and STM32L443xx devices
  1397. ==============================================================================*/
  1398. #if defined (USB)
  1399. /**
  1400. * @brief Initializes the USB Core
  1401. * @param USBx: USB Instance
  1402. * @param cfg : pointer to a USB_CfgTypeDef structure that contains
  1403. * the configuration information for the specified USBx peripheral.
  1404. * @retval HAL status
  1405. */
  1406. HAL_StatusTypeDef USB_CoreInit(USB_TypeDef *USBx, USB_CfgTypeDef cfg)
  1407. {
  1408. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1409. only by USB OTG FS peripheral.
  1410. - This function is added to ensure compatibility across platforms.
  1411. */
  1412. /* Prevent unused argument(s) compilation warning */
  1413. UNUSED(USBx);
  1414. UNUSED(cfg);
  1415. return HAL_OK;
  1416. }
  1417. /**
  1418. * @brief USB_EnableGlobalInt
  1419. * Enables the controller's Global Int in the AHB Config reg
  1420. * @param USBx : Selected device
  1421. * @retval HAL status
  1422. */
  1423. HAL_StatusTypeDef USB_EnableGlobalInt(USB_TypeDef *USBx)
  1424. {
  1425. uint32_t winterruptmask = 0;
  1426. /* Set winterruptmask variable */
  1427. winterruptmask = USB_CNTR_CTRM | USB_CNTR_WKUPM | USB_CNTR_SUSPM | USB_CNTR_ERRM \
  1428. | USB_CNTR_ESOFM | USB_CNTR_RESETM;
  1429. /* Set interrupt mask */
  1430. USBx->CNTR |= winterruptmask;
  1431. return HAL_OK;
  1432. }
  1433. /**
  1434. * @brief USB_DisableGlobalInt
  1435. * Disable the controller's Global Int in the AHB Config reg
  1436. * @param USBx : Selected device
  1437. * @retval HAL status
  1438. */
  1439. HAL_StatusTypeDef USB_DisableGlobalInt(USB_TypeDef *USBx)
  1440. {
  1441. uint32_t winterruptmask = 0;
  1442. /* Set winterruptmask variable */
  1443. winterruptmask = USB_CNTR_CTRM | USB_CNTR_WKUPM | USB_CNTR_SUSPM | USB_CNTR_ERRM \
  1444. | USB_CNTR_ESOFM | USB_CNTR_RESETM;
  1445. /* Clear interrupt mask */
  1446. USBx->CNTR &= ~winterruptmask;
  1447. return HAL_OK;
  1448. }
  1449. /**
  1450. * @brief USB_SetCurrentMode : Set functional mode
  1451. * @param USBx : Selected device
  1452. * @param mode : current core mode
  1453. * This parameter can be one of the these values:
  1454. * @arg USB_DEVICE_MODE: Peripheral mode mode
  1455. * @retval HAL status
  1456. */
  1457. HAL_StatusTypeDef USB_SetCurrentMode(USB_TypeDef *USBx , USB_ModeTypeDef mode)
  1458. {
  1459. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1460. only by USB OTG FS peripheral.
  1461. - This function is added to ensure compatibility across platforms.
  1462. */
  1463. /* Prevent unused argument(s) compilation warning */
  1464. UNUSED(USBx);
  1465. UNUSED(mode);
  1466. return HAL_OK;
  1467. }
  1468. /**
  1469. * @brief USB_DevInit : Initializes the USB controller registers
  1470. * for device mode
  1471. * @param USBx : Selected device
  1472. * @param cfg : pointer to a USB_CfgTypeDef structure that contains
  1473. * the configuration information for the specified USBx peripheral.
  1474. * @retval HAL status
  1475. */
  1476. HAL_StatusTypeDef USB_DevInit (USB_TypeDef *USBx, USB_CfgTypeDef cfg)
  1477. {
  1478. /* Prevent unused argument(s) compilation warning */
  1479. UNUSED(cfg);
  1480. /* Init Device */
  1481. /*CNTR_FRES = 1*/
  1482. USBx->CNTR = USB_CNTR_FRES;
  1483. /*CNTR_FRES = 0*/
  1484. USBx->CNTR = 0;
  1485. /*Clear pending interrupts*/
  1486. USBx->ISTR = 0;
  1487. /*Set Btable Address*/
  1488. USBx->BTABLE = BTABLE_ADDRESS;
  1489. return HAL_OK;
  1490. }
  1491. /**
  1492. * @brief USB_FlushTxFifo : Flush a Tx FIFO
  1493. * @param USBx : Selected device
  1494. * @param num : FIFO number
  1495. * This parameter can be a value from 1 to 15
  1496. 15 means Flush all Tx FIFOs
  1497. * @retval HAL status
  1498. */
  1499. HAL_StatusTypeDef USB_FlushTxFifo (USB_TypeDef *USBx, uint32_t num )
  1500. {
  1501. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1502. only by USB OTG FS peripheral.
  1503. - This function is added to ensure compatibility across platforms.
  1504. */
  1505. /* Prevent unused argument(s) compilation warning */
  1506. UNUSED(USBx);
  1507. UNUSED(num);
  1508. return HAL_OK;
  1509. }
  1510. /**
  1511. * @brief USB_FlushRxFifo : Flush Rx FIFO
  1512. * @param USBx : Selected device
  1513. * @retval HAL status
  1514. */
  1515. HAL_StatusTypeDef USB_FlushRxFifo(USB_TypeDef *USBx)
  1516. {
  1517. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1518. only by USB OTG FS peripheral.
  1519. - This function is added to ensure compatibility across platforms.
  1520. */
  1521. /* Prevent unused argument(s) compilation warning */
  1522. UNUSED(USBx);
  1523. return HAL_OK;
  1524. }
  1525. /**
  1526. * @brief Activate and configure an endpoint
  1527. * @param USBx : Selected device
  1528. * @param ep: pointer to endpoint structure
  1529. * @retval HAL status
  1530. */
  1531. HAL_StatusTypeDef USB_ActivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep)
  1532. {
  1533. /* initialize Endpoint */
  1534. switch (ep->type)
  1535. {
  1536. case EP_TYPE_CTRL:
  1537. PCD_SET_EPTYPE(USBx, ep->num, USB_EP_CONTROL);
  1538. break;
  1539. case EP_TYPE_BULK:
  1540. PCD_SET_EPTYPE(USBx, ep->num, USB_EP_BULK);
  1541. break;
  1542. case EP_TYPE_INTR:
  1543. PCD_SET_EPTYPE(USBx, ep->num, USB_EP_INTERRUPT);
  1544. break;
  1545. case EP_TYPE_ISOC:
  1546. PCD_SET_EPTYPE(USBx, ep->num, USB_EP_ISOCHRONOUS);
  1547. break;
  1548. default:
  1549. break;
  1550. }
  1551. PCD_SET_EP_ADDRESS(USBx, ep->num, ep->num);
  1552. if (ep->doublebuffer == 0)
  1553. {
  1554. if (ep->is_in)
  1555. {
  1556. /*Set the endpoint Transmit buffer address */
  1557. PCD_SET_EP_TX_ADDRESS(USBx, ep->num, ep->pmaadress);
  1558. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  1559. /* Configure NAK status for the Endpoint*/
  1560. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_NAK);
  1561. }
  1562. else
  1563. {
  1564. /*Set the endpoint Receive buffer address */
  1565. PCD_SET_EP_RX_ADDRESS(USBx, ep->num, ep->pmaadress);
  1566. /*Set the endpoint Receive buffer counter*/
  1567. PCD_SET_EP_RX_CNT(USBx, ep->num, ep->maxpacket);
  1568. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  1569. /* Configure VALID status for the Endpoint*/
  1570. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
  1571. }
  1572. }
  1573. /*Double Buffer*/
  1574. else
  1575. {
  1576. /*Set the endpoint as double buffered*/
  1577. PCD_SET_EP_DBUF(USBx, ep->num);
  1578. /*Set buffer address for double buffered mode*/
  1579. PCD_SET_EP_DBUF_ADDR(USBx, ep->num,ep->pmaaddr0, ep->pmaaddr1);
  1580. if (ep->is_in==0)
  1581. {
  1582. /* Clear the data toggle bits for the endpoint IN/OUT*/
  1583. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  1584. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  1585. /* Reset value of the data toggle bits for the endpoint out*/
  1586. PCD_TX_DTOG(USBx, ep->num);
  1587. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
  1588. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  1589. }
  1590. else
  1591. {
  1592. /* Clear the data toggle bits for the endpoint IN/OUT*/
  1593. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  1594. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  1595. PCD_RX_DTOG(USBx, ep->num);
  1596. /* Configure DISABLE status for the Endpoint*/
  1597. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  1598. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
  1599. }
  1600. }
  1601. return HAL_OK;
  1602. }
  1603. /**
  1604. * @brief De-activate and de-initialize an endpoint
  1605. * @param USBx : Selected device
  1606. * @param ep: pointer to endpoint structure
  1607. * @retval HAL status
  1608. */
  1609. HAL_StatusTypeDef USB_DeactivateEndpoint(USB_TypeDef *USBx, USB_EPTypeDef *ep)
  1610. {
  1611. if (ep->doublebuffer == 0)
  1612. {
  1613. if (ep->is_in)
  1614. {
  1615. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  1616. /* Configure DISABLE status for the Endpoint*/
  1617. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  1618. }
  1619. else
  1620. {
  1621. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  1622. /* Configure DISABLE status for the Endpoint*/
  1623. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
  1624. }
  1625. }
  1626. /*Double Buffer*/
  1627. else
  1628. {
  1629. if (ep->is_in==0)
  1630. {
  1631. /* Clear the data toggle bits for the endpoint IN/OUT*/
  1632. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  1633. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  1634. /* Reset value of the data toggle bits for the endpoint out*/
  1635. PCD_TX_DTOG(USBx, ep->num);
  1636. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
  1637. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  1638. }
  1639. else
  1640. {
  1641. /* Clear the data toggle bits for the endpoint IN/OUT*/
  1642. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  1643. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  1644. PCD_RX_DTOG(USBx, ep->num);
  1645. /* Configure DISABLE status for the Endpoint*/
  1646. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_DIS);
  1647. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_DIS);
  1648. }
  1649. }
  1650. return HAL_OK;
  1651. }
  1652. /**
  1653. * @brief USB_EPStartXfer : setup and starts a transfer over an EP
  1654. * @param USBx : Selected device
  1655. * @param ep: pointer to endpoint structure
  1656. * @retval HAL status
  1657. */
  1658. HAL_StatusTypeDef USB_EPStartXfer(USB_TypeDef *USBx , USB_EPTypeDef *ep, uint8_t dma)
  1659. {
  1660. uint16_t pmabuffer = 0;
  1661. uint32_t len = ep->xfer_len;
  1662. /* IN endpoint */
  1663. if (ep->is_in == 1)
  1664. {
  1665. /*Multi packet transfer*/
  1666. if (ep->xfer_len > ep->maxpacket)
  1667. {
  1668. len=ep->maxpacket;
  1669. ep->xfer_len-=len;
  1670. }
  1671. else
  1672. {
  1673. len=ep->xfer_len;
  1674. ep->xfer_len =0;
  1675. }
  1676. /* configure and validate Tx endpoint */
  1677. if (ep->doublebuffer == 0)
  1678. {
  1679. USB_WritePMA(USBx, ep->xfer_buff, ep->pmaadress, len);
  1680. PCD_SET_EP_TX_CNT(USBx, ep->num, len);
  1681. }
  1682. else
  1683. {
  1684. /* Write the data to the USB endpoint */
  1685. if (PCD_GET_ENDPOINT(USBx, ep->num)& USB_EP_DTOG_TX)
  1686. {
  1687. /* Set the Double buffer counter for pmabuffer1 */
  1688. PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
  1689. pmabuffer = ep->pmaaddr1;
  1690. }
  1691. else
  1692. {
  1693. /* Set the Double buffer counter for pmabuffer0 */
  1694. PCD_SET_EP_DBUF0_CNT(USBx, ep->num, ep->is_in, len);
  1695. pmabuffer = ep->pmaaddr0;
  1696. }
  1697. USB_WritePMA(USBx, ep->xfer_buff, pmabuffer, len);
  1698. PCD_FreeUserBuffer(USBx, ep->num, ep->is_in);
  1699. }
  1700. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_VALID);
  1701. }
  1702. else /* OUT endpoint */
  1703. {
  1704. /* Multi packet transfer*/
  1705. if (ep->xfer_len > ep->maxpacket)
  1706. {
  1707. len=ep->maxpacket;
  1708. ep->xfer_len-=len;
  1709. }
  1710. else
  1711. {
  1712. len=ep->xfer_len;
  1713. ep->xfer_len =0;
  1714. }
  1715. /* configure and validate Rx endpoint */
  1716. if (ep->doublebuffer == 0)
  1717. {
  1718. /*Set RX buffer count*/
  1719. PCD_SET_EP_RX_CNT(USBx, ep->num, len);
  1720. }
  1721. else
  1722. {
  1723. /*Set the Double buffer counter*/
  1724. PCD_SET_EP_DBUF1_CNT(USBx, ep->num, ep->is_in, len);
  1725. }
  1726. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
  1727. }
  1728. return HAL_OK;
  1729. }
  1730. /**
  1731. * @brief USB_WritePacket : Writes a packet into the Tx FIFO associated
  1732. * with the EP/channel
  1733. * @param USBx : Selected device
  1734. * @param src : pointer to source buffer
  1735. * @param ch_ep_num : endpoint or host channel number
  1736. * @param len : Number of bytes to write
  1737. * @retval HAL status
  1738. */
  1739. HAL_StatusTypeDef USB_WritePacket(USB_TypeDef *USBx, uint8_t *src, uint8_t ch_ep_num, uint16_t len)
  1740. {
  1741. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1742. only by USB OTG FS peripheral.
  1743. - This function is added to ensure compatibility across platforms.
  1744. */
  1745. /* Prevent unused argument(s) compilation warning */
  1746. UNUSED(USBx);
  1747. UNUSED(src);
  1748. UNUSED(ch_ep_num);
  1749. UNUSED(len);
  1750. return HAL_OK;
  1751. }
  1752. /**
  1753. * @brief USB_ReadPacket : read a packet from the Tx FIFO associated
  1754. * with the EP/channel
  1755. * @param USBx : Selected device
  1756. * @param dest : destination pointer
  1757. * @param len : Number of bytes to read
  1758. * @retval pointer to destination buffer
  1759. */
  1760. void *USB_ReadPacket(USB_TypeDef *USBx, uint8_t *dest, uint16_t len)
  1761. {
  1762. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1763. only by USB OTG FS peripheral.
  1764. - This function is added to ensure compatibility across platforms.
  1765. */
  1766. /* Prevent unused argument(s) compilation warning */
  1767. UNUSED(USBx);
  1768. UNUSED(dest);
  1769. UNUSED(len);
  1770. return ((void *)NULL);
  1771. }
  1772. /**
  1773. * @brief USB_EPSetStall : set a stall condition over an EP
  1774. * @param USBx : Selected device
  1775. * @param ep: pointer to endpoint structure
  1776. * @retval HAL status
  1777. */
  1778. HAL_StatusTypeDef USB_EPSetStall(USB_TypeDef *USBx , USB_EPTypeDef *ep)
  1779. {
  1780. if (ep->num == 0)
  1781. {
  1782. /* This macro sets STALL status for RX & TX*/
  1783. PCD_SET_EP_TXRX_STATUS(USBx, ep->num, USB_EP_RX_STALL, USB_EP_TX_STALL);
  1784. }
  1785. else
  1786. {
  1787. if (ep->is_in)
  1788. {
  1789. PCD_SET_EP_TX_STATUS(USBx, ep->num , USB_EP_TX_STALL);
  1790. }
  1791. else
  1792. {
  1793. PCD_SET_EP_RX_STATUS(USBx, ep->num , USB_EP_RX_STALL);
  1794. }
  1795. }
  1796. return HAL_OK;
  1797. }
  1798. /**
  1799. * @brief USB_EPClearStall : Clear a stall condition over an EP
  1800. * @param USBx : Selected device
  1801. * @param ep: pointer to endpoint structure
  1802. * @retval HAL status
  1803. */
  1804. HAL_StatusTypeDef USB_EPClearStall(USB_TypeDef *USBx, USB_EPTypeDef *ep)
  1805. {
  1806. if (ep->is_in)
  1807. {
  1808. PCD_CLEAR_TX_DTOG(USBx, ep->num);
  1809. PCD_SET_EP_TX_STATUS(USBx, ep->num, USB_EP_TX_VALID);
  1810. }
  1811. else
  1812. {
  1813. PCD_CLEAR_RX_DTOG(USBx, ep->num);
  1814. PCD_SET_EP_RX_STATUS(USBx, ep->num, USB_EP_RX_VALID);
  1815. }
  1816. return HAL_OK;
  1817. }
  1818. /**
  1819. * @brief USB_StopDevice : Stop the usb device mode
  1820. * @param USBx : Selected device
  1821. * @retval HAL status
  1822. */
  1823. HAL_StatusTypeDef USB_StopDevice(USB_TypeDef *USBx)
  1824. {
  1825. /* disable all interrupts and force USB reset */
  1826. USBx->CNTR = USB_CNTR_FRES;
  1827. /* clear interrupt status register */
  1828. USBx->ISTR = 0;
  1829. /* switch-off device */
  1830. USBx->CNTR = (USB_CNTR_FRES | USB_CNTR_PDWN);
  1831. return HAL_OK;
  1832. }
  1833. /**
  1834. * @brief USB_SetDevAddress : Stop the usb device mode
  1835. * @param USBx : Selected device
  1836. * @param address : new device address to be assigned
  1837. * This parameter can be a value from 0 to 255
  1838. * @retval HAL status
  1839. */
  1840. HAL_StatusTypeDef USB_SetDevAddress (USB_TypeDef *USBx, uint8_t address)
  1841. {
  1842. if(address == 0)
  1843. {
  1844. /* set device address and enable function */
  1845. USBx->DADDR = USB_DADDR_EF;
  1846. }
  1847. return HAL_OK;
  1848. }
  1849. /**
  1850. * @brief USB_DevConnect : Connect the USB device by enabling the pull-up/pull-down
  1851. * @param USBx : Selected device
  1852. * @retval HAL status
  1853. */
  1854. HAL_StatusTypeDef USB_DevConnect (USB_TypeDef *USBx)
  1855. {
  1856. /* Enabling DP Pull-Down bit to Connect internal pull-up on USB DP line */
  1857. USB->BCDR |= USB_BCDR_DPPU;
  1858. return HAL_OK;
  1859. }
  1860. /**
  1861. * @brief USB_DevDisconnect : Disconnect the USB device by disabling the pull-up/pull-down
  1862. * @param USBx : Selected device
  1863. * @retval HAL status
  1864. */
  1865. HAL_StatusTypeDef USB_DevDisconnect (USB_TypeDef *USBx)
  1866. {
  1867. /* Disable DP Pull-Down bit*/
  1868. USB->BCDR &= ~(USB_BCDR_DPPU);
  1869. return HAL_OK;
  1870. }
  1871. /**
  1872. * @brief USB_ReadInterrupts: return the global USB interrupt status
  1873. * @param USBx : Selected device
  1874. * @retval HAL status
  1875. */
  1876. uint32_t USB_ReadInterrupts (USB_TypeDef *USBx)
  1877. {
  1878. uint32_t tmpreg = 0;
  1879. tmpreg = USBx->ISTR;
  1880. return tmpreg;
  1881. }
  1882. /**
  1883. * @brief USB_ReadDevAllOutEpInterrupt: return the USB device OUT endpoints interrupt status
  1884. * @param USBx : Selected device
  1885. * @retval HAL status
  1886. */
  1887. uint32_t USB_ReadDevAllOutEpInterrupt (USB_TypeDef *USBx)
  1888. {
  1889. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1890. only by USB OTG FS peripheral.
  1891. - This function is added to ensure compatibility across platforms.
  1892. */
  1893. /* Prevent unused argument(s) compilation warning */
  1894. UNUSED(USBx);
  1895. return (0);
  1896. }
  1897. /**
  1898. * @brief USB_ReadDevAllInEpInterrupt: return the USB device IN endpoints interrupt status
  1899. * @param USBx : Selected device
  1900. * @retval HAL status
  1901. */
  1902. uint32_t USB_ReadDevAllInEpInterrupt (USB_TypeDef *USBx)
  1903. {
  1904. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1905. only by USB OTG FS peripheral.
  1906. - This function is added to ensure compatibility across platforms.
  1907. */
  1908. /* Prevent unused argument(s) compilation warning */
  1909. UNUSED(USBx);
  1910. return (0);
  1911. }
  1912. /**
  1913. * @brief Returns Device OUT EP Interrupt register
  1914. * @param USBx : Selected device
  1915. * @param epnum : endpoint number
  1916. * This parameter can be a value from 0 to 15
  1917. * @retval Device OUT EP Interrupt register
  1918. */
  1919. uint32_t USB_ReadDevOutEPInterrupt (USB_TypeDef *USBx , uint8_t epnum)
  1920. {
  1921. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1922. only by USB OTG FS peripheral.
  1923. - This function is added to ensure compatibility across platforms.
  1924. */
  1925. /* Prevent unused argument(s) compilation warning */
  1926. UNUSED(USBx);
  1927. UNUSED(epnum);
  1928. return (0);
  1929. }
  1930. /**
  1931. * @brief Returns Device IN EP Interrupt register
  1932. * @param USBx : Selected device
  1933. * @param epnum : endpoint number
  1934. * This parameter can be a value from 0 to 15
  1935. * @retval Device IN EP Interrupt register
  1936. */
  1937. uint32_t USB_ReadDevInEPInterrupt (USB_TypeDef *USBx , uint8_t epnum)
  1938. {
  1939. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1940. only by USB OTG FS peripheral.
  1941. - This function is added to ensure compatibility across platforms.
  1942. */
  1943. /* Prevent unused argument(s) compilation warning */
  1944. UNUSED(USBx);
  1945. UNUSED(epnum);
  1946. return (0);
  1947. }
  1948. /**
  1949. * @brief USB_ClearInterrupts: clear a USB interrupt
  1950. * @param USBx : Selected device
  1951. * @param interrupt : interrupt flag
  1952. * @retval None
  1953. */
  1954. void USB_ClearInterrupts (USB_TypeDef *USBx, uint32_t interrupt)
  1955. {
  1956. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1957. only by USB OTG FS peripheral.
  1958. - This function is added to ensure compatibility across platforms.
  1959. */
  1960. /* Prevent unused argument(s) compilation warning */
  1961. UNUSED(USBx);
  1962. UNUSED(interrupt);
  1963. }
  1964. /**
  1965. * @brief Prepare the EP0 to start the first control setup
  1966. * @param USBx : Selected device
  1967. * @param psetup : pointer to setup packet
  1968. * @retval HAL status
  1969. */
  1970. HAL_StatusTypeDef USB_EP0_OutStart(USB_TypeDef *USBx, uint8_t dma ,uint8_t *psetup)
  1971. {
  1972. /* NOTE : - This function is not required by USB Device FS peripheral, it is used
  1973. only by USB OTG FS peripheral.
  1974. - This function is added to ensure compatibility across platforms.
  1975. */
  1976. /* Prevent unused argument(s) compilation warning */
  1977. UNUSED(USBx);
  1978. UNUSED(psetup);
  1979. UNUSED(dma);
  1980. return HAL_OK;
  1981. }
  1982. /**
  1983. * @brief USB_ActivateRemoteWakeup : active remote wakeup signalling
  1984. * @param USBx : Selected device
  1985. * @retval HAL status
  1986. */
  1987. HAL_StatusTypeDef USB_ActivateRemoteWakeup(USB_TypeDef *USBx)
  1988. {
  1989. USBx->CNTR |= USB_CNTR_RESUME;
  1990. return HAL_OK;
  1991. }
  1992. /**
  1993. * @brief USB_DeActivateRemoteWakeup : de-active remote wakeup signalling
  1994. * @param USBx : Selected device
  1995. * @retval HAL status
  1996. */
  1997. HAL_StatusTypeDef USB_DeActivateRemoteWakeup(USB_TypeDef *USBx)
  1998. {
  1999. USBx->CNTR &= ~(USB_CNTR_RESUME);
  2000. return HAL_OK;
  2001. }
  2002. /**
  2003. * @brief Copy a buffer from user memory area to packet memory area (PMA)
  2004. * @param USBx : pointer to USB register.
  2005. * @param pbUsrBuf : pointer to user memory area.
  2006. * @param wPMABufAddr : address into PMA.
  2007. * @param wNBytes : number of bytes to be copied.
  2008. * @retval None
  2009. */
  2010. void USB_WritePMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
  2011. {
  2012. uint32_t n = (wNBytes + 1) >> 1;
  2013. uint32_t i;
  2014. uint16_t temp1, temp2;
  2015. uint16_t *pdwVal;
  2016. pdwVal = (uint16_t *)(wPMABufAddr + (uint32_t)USBx + 0x400);
  2017. for (i = n; i != 0; i--)
  2018. {
  2019. temp1 = (uint16_t) * pbUsrBuf;
  2020. pbUsrBuf++;
  2021. temp2 = temp1 | (uint16_t) * pbUsrBuf << 8;
  2022. *pdwVal++ = temp2;
  2023. pbUsrBuf++;
  2024. }
  2025. }
  2026. /**
  2027. * @brief Copy a buffer from user memory area to packet memory area (PMA)
  2028. * @param USBx : pointer to USB register.
  2029. * @param pbUsrBuf : pointer to user memory area.
  2030. * @param wPMABufAddr : address into PMA.
  2031. * @param wNBytes : number of bytes to be copied.
  2032. * @retval None
  2033. */
  2034. void USB_ReadPMA(USB_TypeDef *USBx, uint8_t *pbUsrBuf, uint16_t wPMABufAddr, uint16_t wNBytes)
  2035. {
  2036. uint32_t n = (wNBytes + 1) >> 1;
  2037. uint32_t i;
  2038. uint16_t *pdwVal;
  2039. pdwVal = (uint16_t *)(wPMABufAddr + (uint32_t)USBx + 0x400);
  2040. for (i = n; i != 0; i--)
  2041. {
  2042. *(uint16_t*)pbUsrBuf++ = *pdwVal++;
  2043. pbUsrBuf++;
  2044. }
  2045. }
  2046. #endif /* USB */
  2047. /**
  2048. * @}
  2049. */
  2050. /**
  2051. * @}
  2052. */
  2053. #if defined (USB_OTG_FS)
  2054. /** @addtogroup USB_LL_Private_Functions
  2055. * @{
  2056. */
  2057. /**
  2058. * @brief Reset the USB Core (needed after USB clock settings change)
  2059. * @param USBx : Selected device
  2060. * @retval HAL status
  2061. */
  2062. static HAL_StatusTypeDef USB_CoreReset(USB_OTG_GlobalTypeDef *USBx)
  2063. {
  2064. uint32_t count = 0;
  2065. /* Wait for AHB master IDLE state. */
  2066. do
  2067. {
  2068. if (++count > 200000)
  2069. {
  2070. return HAL_TIMEOUT;
  2071. }
  2072. }
  2073. while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_AHBIDL) == 0);
  2074. /* Core Soft Reset */
  2075. count = 0;
  2076. USBx->GRSTCTL |= USB_OTG_GRSTCTL_CSRST;
  2077. do
  2078. {
  2079. if (++count > 200000)
  2080. {
  2081. return HAL_TIMEOUT;
  2082. }
  2083. }
  2084. while ((USBx->GRSTCTL & USB_OTG_GRSTCTL_CSRST) == USB_OTG_GRSTCTL_CSRST);
  2085. return HAL_OK;
  2086. }
  2087. /**
  2088. * @}
  2089. */
  2090. #endif /* USB_OTG_FS */
  2091. #endif /* STM32L432xx || STM32L433xx || STM32L442xx || STM32L443xx || */
  2092. /* STM32L452xx || STM32L462xx || */
  2093. /* STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || */
  2094. /* STM32L496xx || STM32L4A6xx || */
  2095. /* STM32L4R5xx || STM32L4R7xx || STM32L4R9xx || STM32L4S5xx || STM32L4S7xx || STM32L4S9xx */
  2096. #endif /* defined (HAL_PCD_MODULE_ENABLED) || defined (HAL_HCD_MODULE_ENABLED) */
  2097. /**
  2098. * @}
  2099. */
  2100. /**
  2101. * @}
  2102. */
  2103. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/