stm32f1xx_ll_usb.c 77 KB

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