Implement a secure ICS protocol targeting LoRa Node151 microcontroller for controlling irrigation.
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.
 
 
 
 
 
 

2772 lines
75 KiB

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