stm32l4xx_ll_usb.c 68 KB

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