stm32f1xx_ll_usb.c 65 KB

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