stm32l4xx_ll_usb.c 78 KB

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