You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

2405 lines
67 KiB

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