No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 

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