stm32f1xx_ll_usb.c 62 KB

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