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.
 
 
 

349 lines
9.7 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32h7xx_hal_pcd_ex.c
  4. * @author MCD Application Team
  5. * @brief PCD Extended HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the USB Peripheral Controller:
  8. * + Extended features functions
  9. *
  10. ******************************************************************************
  11. * @attention
  12. *
  13. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  14. * All rights reserved.</center></h2>
  15. *
  16. * This software component is licensed by ST under BSD 3-Clause license,
  17. * the "License"; You may not use this file except in compliance with the
  18. * License. You may obtain a copy of the License at:
  19. * opensource.org/licenses/BSD-3-Clause
  20. *
  21. ******************************************************************************
  22. */
  23. /* Includes ------------------------------------------------------------------*/
  24. #include "stm32h7xx_hal.h"
  25. /** @addtogroup STM32H7xx_HAL_Driver
  26. * @{
  27. */
  28. /** @defgroup PCDEx PCDEx
  29. * @brief PCD Extended HAL module driver
  30. * @{
  31. */
  32. #ifdef HAL_PCD_MODULE_ENABLED
  33. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  34. /* Private types -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private constants ---------------------------------------------------------*/
  37. /* Private macros ------------------------------------------------------------*/
  38. /* Private functions ---------------------------------------------------------*/
  39. /* Exported functions --------------------------------------------------------*/
  40. /** @defgroup PCDEx_Exported_Functions PCDEx Exported Functions
  41. * @{
  42. */
  43. /** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions
  44. * @brief PCDEx control functions
  45. *
  46. @verbatim
  47. ===============================================================================
  48. ##### Extended features functions #####
  49. ===============================================================================
  50. [..] This section provides functions allowing to:
  51. (+) Update FIFO configuration
  52. @endverbatim
  53. * @{
  54. */
  55. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  56. /**
  57. * @brief Set Tx FIFO
  58. * @param hpcd PCD handle
  59. * @param fifo The number of Tx fifo
  60. * @param size Fifo size
  61. * @retval HAL status
  62. */
  63. HAL_StatusTypeDef HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo, uint16_t size)
  64. {
  65. uint8_t i;
  66. uint32_t Tx_Offset;
  67. /* TXn min size = 16 words. (n : Transmit FIFO index)
  68. When a TxFIFO is not used, the Configuration should be as follows:
  69. case 1 : n > m and Txn is not used (n,m : Transmit FIFO indexes)
  70. --> Txm can use the space allocated for Txn.
  71. case2 : n < m and Txn is not used (n,m : Transmit FIFO indexes)
  72. --> Txn should be configured with the minimum space of 16 words
  73. The FIFO is used optimally when used TxFIFOs are allocated in the top
  74. of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones.
  75. When DMA is used 3n * FIFO locations should be reserved for internal DMA registers */
  76. Tx_Offset = hpcd->Instance->GRXFSIZ;
  77. if (fifo == 0U)
  78. {
  79. hpcd->Instance->DIEPTXF0_HNPTXFSIZ = ((uint32_t)size << 16) | Tx_Offset;
  80. }
  81. else
  82. {
  83. Tx_Offset += (hpcd->Instance->DIEPTXF0_HNPTXFSIZ) >> 16;
  84. for (i = 0U; i < (fifo - 1U); i++)
  85. {
  86. Tx_Offset += (hpcd->Instance->DIEPTXF[i] >> 16);
  87. }
  88. /* Multiply Tx_Size by 2 to get higher performance */
  89. hpcd->Instance->DIEPTXF[fifo - 1U] = ((uint32_t)size << 16) | Tx_Offset;
  90. }
  91. return HAL_OK;
  92. }
  93. /**
  94. * @brief Set Rx FIFO
  95. * @param hpcd PCD handle
  96. * @param size Size of Rx fifo
  97. * @retval HAL status
  98. */
  99. HAL_StatusTypeDef HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef *hpcd, uint16_t size)
  100. {
  101. hpcd->Instance->GRXFSIZ = size;
  102. return HAL_OK;
  103. }
  104. /**
  105. * @brief Activate LPM feature.
  106. * @param hpcd PCD handle
  107. * @retval HAL status
  108. */
  109. HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
  110. {
  111. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  112. hpcd->lpm_active = 1U;
  113. hpcd->LPM_State = LPM_L0;
  114. USBx->GINTMSK |= USB_OTG_GINTMSK_LPMINTM;
  115. USBx->GLPMCFG |= (USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
  116. return HAL_OK;
  117. }
  118. /**
  119. * @brief Deactivate LPM feature.
  120. * @param hpcd PCD handle
  121. * @retval HAL status
  122. */
  123. HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd)
  124. {
  125. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  126. hpcd->lpm_active = 0U;
  127. USBx->GINTMSK &= ~USB_OTG_GINTMSK_LPMINTM;
  128. USBx->GLPMCFG &= ~(USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
  129. return HAL_OK;
  130. }
  131. /**
  132. * @brief Handle BatteryCharging Process.
  133. * @param hpcd PCD handle
  134. * @retval HAL status
  135. */
  136. void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd)
  137. {
  138. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  139. uint32_t tickstart = HAL_GetTick();
  140. /* Enable DCD : Data Contact Detect */
  141. USBx->GCCFG |= USB_OTG_GCCFG_DCDEN;
  142. /* Wait Detect flag or a timeout is happen*/
  143. while ((USBx->GCCFG & USB_OTG_GCCFG_DCDET) == 0U)
  144. {
  145. /* Check for the Timeout */
  146. if ((HAL_GetTick() - tickstart) > 1000U)
  147. {
  148. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  149. hpcd->BCDCallback(hpcd, PCD_BCD_ERROR);
  150. #else
  151. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR);
  152. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  153. return;
  154. }
  155. }
  156. /* Right response got */
  157. HAL_Delay(200U);
  158. /* Check Detect flag*/
  159. if ((USBx->GCCFG & USB_OTG_GCCFG_DCDET) == USB_OTG_GCCFG_DCDET)
  160. {
  161. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  162. hpcd->BCDCallback(hpcd, PCD_BCD_CONTACT_DETECTION);
  163. #else
  164. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION);
  165. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  166. }
  167. /*Primary detection: checks if connected to Standard Downstream Port
  168. (without charging capability) */
  169. USBx->GCCFG &= ~ USB_OTG_GCCFG_DCDEN;
  170. HAL_Delay(50U);
  171. USBx->GCCFG |= USB_OTG_GCCFG_PDEN;
  172. HAL_Delay(50U);
  173. if ((USBx->GCCFG & USB_OTG_GCCFG_PDET) == 0U)
  174. {
  175. /* Case of Standard Downstream Port */
  176. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  177. hpcd->BCDCallback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  178. #else
  179. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  180. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  181. }
  182. else
  183. {
  184. /* start secondary detection to check connection to Charging Downstream
  185. Port or Dedicated Charging Port */
  186. USBx->GCCFG &= ~ USB_OTG_GCCFG_PDEN;
  187. HAL_Delay(50U);
  188. USBx->GCCFG |= USB_OTG_GCCFG_SDEN;
  189. HAL_Delay(50U);
  190. if ((USBx->GCCFG & USB_OTG_GCCFG_SDET) == USB_OTG_GCCFG_SDET)
  191. {
  192. /* case Dedicated Charging Port */
  193. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  194. hpcd->BCDCallback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
  195. #else
  196. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
  197. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  198. }
  199. else
  200. {
  201. /* case Charging Downstream Port */
  202. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  203. hpcd->BCDCallback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
  204. #else
  205. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
  206. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  207. }
  208. }
  209. /* Battery Charging capability discovery finished */
  210. (void)HAL_PCDEx_DeActivateBCD(hpcd);
  211. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  212. hpcd->BCDCallback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
  213. #else
  214. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
  215. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  216. }
  217. /**
  218. * @brief Activate BatteryCharging feature.
  219. * @param hpcd PCD handle
  220. * @retval HAL status
  221. */
  222. HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd)
  223. {
  224. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  225. USBx->GCCFG &= ~(USB_OTG_GCCFG_PDEN);
  226. USBx->GCCFG &= ~(USB_OTG_GCCFG_SDEN);
  227. /* Power Down USB tranceiver */
  228. USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
  229. /* Enable Battery charging */
  230. USBx->GCCFG |= USB_OTG_GCCFG_BCDEN;
  231. hpcd->battery_charging_active = 1U;
  232. return HAL_OK;
  233. }
  234. /**
  235. * @brief Deactivate BatteryCharging feature.
  236. * @param hpcd PCD handle
  237. * @retval HAL status
  238. */
  239. HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd)
  240. {
  241. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  242. USBx->GCCFG &= ~(USB_OTG_GCCFG_SDEN);
  243. USBx->GCCFG &= ~(USB_OTG_GCCFG_PDEN);
  244. /* Disable Battery charging */
  245. USBx->GCCFG &= ~(USB_OTG_GCCFG_BCDEN);
  246. hpcd->battery_charging_active = 0U;
  247. return HAL_OK;
  248. }
  249. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  250. /**
  251. * @brief Send LPM message to user layer callback.
  252. * @param hpcd PCD handle
  253. * @param msg LPM message
  254. * @retval HAL status
  255. */
  256. __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
  257. {
  258. /* Prevent unused argument(s) compilation warning */
  259. UNUSED(hpcd);
  260. UNUSED(msg);
  261. /* NOTE : This function should not be modified, when the callback is needed,
  262. the HAL_PCDEx_LPM_Callback could be implemented in the user file
  263. */
  264. }
  265. /**
  266. * @brief Send BatteryCharging message to user layer callback.
  267. * @param hpcd PCD handle
  268. * @param msg LPM message
  269. * @retval HAL status
  270. */
  271. __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
  272. {
  273. /* Prevent unused argument(s) compilation warning */
  274. UNUSED(hpcd);
  275. UNUSED(msg);
  276. /* NOTE : This function should not be modified, when the callback is needed,
  277. the HAL_PCDEx_BCD_Callback could be implemented in the user file
  278. */
  279. }
  280. /**
  281. * @}
  282. */
  283. /**
  284. * @}
  285. */
  286. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  287. #endif /* HAL_PCD_MODULE_ENABLED */
  288. /**
  289. * @}
  290. */
  291. /**
  292. * @}
  293. */
  294. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/