stm32l4xx_ll_usb.c 78 KB

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