stm32f1xx_ll_usb.c 70 KB

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