stm32h7xx_hal_pcd_ex.c 9.8 KB

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