stm32f1xx_ll_usb.c 65 KB

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