Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 

349 строки
10 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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) 2016 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 "stm32f4xx_hal.h"
  25. /** @addtogroup STM32F4xx_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. #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  105. /**
  106. * @brief Activate LPM feature.
  107. * @param hpcd PCD handle
  108. * @retval HAL status
  109. */
  110. HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
  111. {
  112. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  113. hpcd->lpm_active = 1U;
  114. hpcd->LPM_State = LPM_L0;
  115. USBx->GINTMSK |= USB_OTG_GINTMSK_LPMINTM;
  116. USBx->GLPMCFG |= (USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
  117. return HAL_OK;
  118. }
  119. /**
  120. * @brief Deactivate LPM feature.
  121. * @param hpcd PCD handle
  122. * @retval HAL status
  123. */
  124. HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd)
  125. {
  126. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  127. hpcd->lpm_active = 0U;
  128. USBx->GINTMSK &= ~USB_OTG_GINTMSK_LPMINTM;
  129. USBx->GLPMCFG &= ~(USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
  130. return HAL_OK;
  131. }
  132. #endif /* defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx) */
  133. #if defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  134. /**
  135. * @brief Handle BatteryCharging Process.
  136. * @param hpcd PCD handle
  137. * @retval HAL status
  138. */
  139. void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd)
  140. {
  141. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  142. uint32_t tickstart = HAL_GetTick();
  143. /* Enable DCD : Data Contact Detect */
  144. USBx->GCCFG |= USB_OTG_GCCFG_DCDEN;
  145. /* Wait Detect flag or a timeout is happen*/
  146. while ((USBx->GCCFG & USB_OTG_GCCFG_DCDET) == 0U)
  147. {
  148. /* Check for the Timeout */
  149. if ((HAL_GetTick() - tickstart) > 1000U)
  150. {
  151. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  152. hpcd->BCDCallback(hpcd, PCD_BCD_ERROR);
  153. #else
  154. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR);
  155. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  156. return;
  157. }
  158. }
  159. /* Right response got */
  160. HAL_Delay(200U);
  161. /* Check Detect flag*/
  162. if ((USBx->GCCFG & USB_OTG_GCCFG_DCDET) == USB_OTG_GCCFG_DCDET)
  163. {
  164. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  165. hpcd->BCDCallback(hpcd, PCD_BCD_CONTACT_DETECTION);
  166. #else
  167. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION);
  168. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  169. }
  170. /*Primary detection: checks if connected to Standard Downstream Port
  171. (without charging capability) */
  172. USBx->GCCFG &= ~ USB_OTG_GCCFG_DCDEN;
  173. HAL_Delay(50U);
  174. USBx->GCCFG |= USB_OTG_GCCFG_PDEN;
  175. HAL_Delay(50U);
  176. if ((USBx->GCCFG & USB_OTG_GCCFG_PDET) == 0U)
  177. {
  178. /* Case of Standard Downstream Port */
  179. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  180. hpcd->BCDCallback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  181. #else
  182. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  183. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  184. }
  185. else
  186. {
  187. /* start secondary detection to check connection to Charging Downstream
  188. Port or Dedicated Charging Port */
  189. USBx->GCCFG &= ~ USB_OTG_GCCFG_PDEN;
  190. HAL_Delay(50U);
  191. USBx->GCCFG |= USB_OTG_GCCFG_SDEN;
  192. HAL_Delay(50U);
  193. if ((USBx->GCCFG & USB_OTG_GCCFG_SDET) == USB_OTG_GCCFG_SDET)
  194. {
  195. /* case Dedicated Charging Port */
  196. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  197. hpcd->BCDCallback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
  198. #else
  199. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
  200. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  201. }
  202. else
  203. {
  204. /* case Charging Downstream Port */
  205. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  206. hpcd->BCDCallback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
  207. #else
  208. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
  209. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  210. }
  211. }
  212. /* Battery Charging capability discovery finished */
  213. (void)HAL_PCDEx_DeActivateBCD(hpcd);
  214. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  215. hpcd->BCDCallback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
  216. #else
  217. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
  218. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  219. }
  220. /**
  221. * @brief Activate BatteryCharging feature.
  222. * @param hpcd PCD handle
  223. * @retval HAL status
  224. */
  225. HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd)
  226. {
  227. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  228. USBx->GCCFG &= ~(USB_OTG_GCCFG_PDEN);
  229. USBx->GCCFG &= ~(USB_OTG_GCCFG_SDEN);
  230. /* Power Down USB tranceiver */
  231. USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
  232. /* Enable Battery charging */
  233. USBx->GCCFG |= USB_OTG_GCCFG_BCDEN;
  234. hpcd->battery_charging_active = 1U;
  235. return HAL_OK;
  236. }
  237. /**
  238. * @brief Deactivate BatteryCharging feature.
  239. * @param hpcd PCD handle
  240. * @retval HAL status
  241. */
  242. HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd)
  243. {
  244. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  245. USBx->GCCFG &= ~(USB_OTG_GCCFG_SDEN);
  246. USBx->GCCFG &= ~(USB_OTG_GCCFG_PDEN);
  247. /* Disable Battery charging */
  248. USBx->GCCFG &= ~(USB_OTG_GCCFG_BCDEN);
  249. hpcd->battery_charging_active = 0U;
  250. return HAL_OK;
  251. }
  252. #endif /* defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx) */
  253. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  254. /**
  255. * @brief Send LPM message to user layer callback.
  256. * @param hpcd PCD handle
  257. * @param msg LPM message
  258. * @retval HAL status
  259. */
  260. __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
  261. {
  262. /* Prevent unused argument(s) compilation warning */
  263. UNUSED(hpcd);
  264. UNUSED(msg);
  265. /* NOTE : This function should not be modified, when the callback is needed,
  266. the HAL_PCDEx_LPM_Callback could be implemented in the user file
  267. */
  268. }
  269. /**
  270. * @brief Send BatteryCharging message to user layer callback.
  271. * @param hpcd PCD handle
  272. * @param msg LPM message
  273. * @retval HAL status
  274. */
  275. __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
  276. {
  277. /* Prevent unused argument(s) compilation warning */
  278. UNUSED(hpcd);
  279. UNUSED(msg);
  280. /* NOTE : This function should not be modified, when the callback is needed,
  281. the HAL_PCDEx_BCD_Callback could be implemented in the user file
  282. */
  283. }
  284. /**
  285. * @}
  286. */
  287. /**
  288. * @}
  289. */
  290. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  291. #endif /* HAL_PCD_MODULE_ENABLED */
  292. /**
  293. * @}
  294. */
  295. /**
  296. * @}
  297. */
  298. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/