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.
 
 
 

325 lines
11 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_pcd_ex.c
  4. * @author MCD Application Team
  5. * @version V1.5.2
  6. * @date 22-September-2016
  7. * @brief PCD HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the USB Peripheral Controller:
  10. * + Extended features functions
  11. *
  12. ******************************************************************************
  13. * @attention
  14. *
  15. * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
  16. *
  17. * Redistribution and use in source and binary forms, with or without modification,
  18. * are permitted provided that the following conditions are met:
  19. * 1. Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  25. * may be used to endorse or promote products derived from this software
  26. * without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  32. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  34. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  35. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  36. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  37. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. ******************************************************************************
  40. */
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "stm32f4xx_hal.h"
  43. /** @addtogroup STM32F4xx_HAL_Driver
  44. * @{
  45. */
  46. /** @defgroup PCDEx PCDEx
  47. * @brief PCD Extended HAL module driver
  48. * @{
  49. */
  50. #ifdef HAL_PCD_MODULE_ENABLED
  51. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || \
  52. defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
  53. defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE) || defined(STM32F446xx) || \
  54. defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
  55. defined(STM32F412Rx) || defined(STM32F412Cx)
  56. /* Private types -------------------------------------------------------------*/
  57. /* Private variables ---------------------------------------------------------*/
  58. /* Private constants ---------------------------------------------------------*/
  59. /* Private macros ------------------------------------------------------------*/
  60. /* Private functions ---------------------------------------------------------*/
  61. /* Exported functions --------------------------------------------------------*/
  62. /** @defgroup PCDEx_Exported_Functions PCD Extended Exported Functions
  63. * @{
  64. */
  65. /** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions
  66. * @brief PCDEx control functions
  67. *
  68. @verbatim
  69. ===============================================================================
  70. ##### Extended features functions #####
  71. ===============================================================================
  72. [..] This section provides functions allowing to:
  73. (+) Update FIFO configuration
  74. @endverbatim
  75. * @{
  76. */
  77. /**
  78. * @brief Set Tx FIFO
  79. * @param hpcd: PCD handle
  80. * @param fifo: The number of Tx fifo
  81. * @param size: Fifo size
  82. * @retval HAL status
  83. */
  84. HAL_StatusTypeDef HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo, uint16_t size)
  85. {
  86. uint8_t i = 0U;
  87. uint32_t Tx_Offset = 0U;
  88. /* TXn min size = 16 words. (n : Transmit FIFO index)
  89. When a TxFIFO is not used, the Configuration should be as follows:
  90. case 1 : n > m and Txn is not used (n,m : Transmit FIFO indexes)
  91. --> Txm can use the space allocated for Txn.
  92. case2 : n < m and Txn is not used (n,m : Transmit FIFO indexes)
  93. --> Txn should be configured with the minimum space of 16 words
  94. The FIFO is used optimally when used TxFIFOs are allocated in the top
  95. of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones.
  96. When DMA is used 3n * FIFO locations should be reserved for internal DMA registers */
  97. Tx_Offset = hpcd->Instance->GRXFSIZ;
  98. if(fifo == 0U)
  99. {
  100. hpcd->Instance->DIEPTXF0_HNPTXFSIZ = (uint32_t)(((uint32_t)size << 16U) | Tx_Offset);
  101. }
  102. else
  103. {
  104. Tx_Offset += (hpcd->Instance->DIEPTXF0_HNPTXFSIZ) >> 16U;
  105. for (i = 0U; i < (fifo - 1U); i++)
  106. {
  107. Tx_Offset += (hpcd->Instance->DIEPTXF[i] >> 16U);
  108. }
  109. /* Multiply Tx_Size by 2 to get higher performance */
  110. hpcd->Instance->DIEPTXF[fifo - 1U] = (uint32_t)(((uint32_t)size << 16U) | Tx_Offset);
  111. }
  112. return HAL_OK;
  113. }
  114. /**
  115. * @brief Set Rx FIFO
  116. * @param hpcd: PCD handle
  117. * @param size: Size of Rx fifo
  118. * @retval HAL status
  119. */
  120. HAL_StatusTypeDef HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef *hpcd, uint16_t size)
  121. {
  122. hpcd->Instance->GRXFSIZ = size;
  123. return HAL_OK;
  124. }
  125. #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
  126. defined(STM32F412Rx) || defined(STM32F412Cx)
  127. /**
  128. * @brief Activate LPM feature
  129. * @param hpcd: PCD handle
  130. * @retval HAL status
  131. */
  132. HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
  133. {
  134. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  135. hpcd->lpm_active = ENABLE;
  136. hpcd->LPM_State = LPM_L0;
  137. USBx->GINTMSK |= USB_OTG_GINTMSK_LPMINTM;
  138. USBx->GLPMCFG |= (USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
  139. return HAL_OK;
  140. }
  141. /**
  142. * @brief Deactivate LPM feature.
  143. * @param hpcd: PCD handle
  144. * @retval HAL status
  145. */
  146. HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd)
  147. {
  148. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  149. hpcd->lpm_active = DISABLE;
  150. USBx->GINTMSK &= ~USB_OTG_GINTMSK_LPMINTM;
  151. USBx->GLPMCFG &= ~(USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
  152. return HAL_OK;
  153. }
  154. /**
  155. * @brief Send LPM message to user layer callback.
  156. * @param hpcd: PCD handle
  157. * @param msg: LPM message
  158. * @retval HAL status
  159. */
  160. __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
  161. {
  162. /* Prevent unused argument(s) compilation warning */
  163. UNUSED(hpcd);
  164. UNUSED(msg);
  165. }
  166. #endif /* STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Rx || STM32F412Vx || STM32F412Cx */
  167. #if defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx)
  168. /**
  169. * @brief HAL_PCDEx_BCD_VBUSDetect : handle BatteryCharging Process
  170. * @param hpcd: PCD handle
  171. * @retval HAL status
  172. */
  173. void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd)
  174. {
  175. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  176. uint32_t tickstart = HAL_GetTick();
  177. /* Start BCD When device is connected */
  178. if (USBx_DEVICE->DCTL & USB_OTG_DCTL_SDIS)
  179. {
  180. /* Enable DCD : Data Contact Detect */
  181. USBx->GCCFG |= USB_OTG_GCCFG_DCDEN;
  182. /* Wait Detect flag or a timeout is happen*/
  183. while ((USBx->GCCFG & USB_OTG_GCCFG_DCDET) == 0U)
  184. {
  185. /* Check for the Timeout */
  186. if((HAL_GetTick() - tickstart ) > 1000U)
  187. {
  188. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR);
  189. return;
  190. }
  191. }
  192. /* Right response got */
  193. HAL_Delay(100U);
  194. /* Check Detect flag*/
  195. if (USBx->GCCFG & USB_OTG_GCCFG_DCDET)
  196. {
  197. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION);
  198. }
  199. /*Primary detection: checks if connected to Standard Downstream Port
  200. (without charging capability) */
  201. USBx->GCCFG &=~ USB_OTG_GCCFG_DCDEN;
  202. USBx->GCCFG |= USB_OTG_GCCFG_PDEN;
  203. HAL_Delay(100U);
  204. if (!(USBx->GCCFG & USB_OTG_GCCFG_PDET))
  205. {
  206. /* Case of Standard Downstream Port */
  207. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  208. }
  209. else
  210. {
  211. /* start secondary detection to check connection to Charging Downstream
  212. Port or Dedicated Charging Port */
  213. USBx->GCCFG &=~ USB_OTG_GCCFG_PDEN;
  214. USBx->GCCFG |= USB_OTG_GCCFG_SDEN;
  215. HAL_Delay(100U);
  216. if ((USBx->GCCFG) & USB_OTG_GCCFG_SDET)
  217. {
  218. /* case Dedicated Charging Port */
  219. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
  220. }
  221. else
  222. {
  223. /* case Charging Downstream Port */
  224. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
  225. }
  226. }
  227. /* Battery Charging capability discovery finished */
  228. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
  229. }
  230. }
  231. /**
  232. * @brief HAL_PCDEx_ActivateBCD : active BatteryCharging feature
  233. * @param hpcd: PCD handle
  234. * @retval HAL status
  235. */
  236. HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd)
  237. {
  238. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  239. hpcd->battery_charging_active = ENABLE;
  240. USBx->GCCFG |= (USB_OTG_GCCFG_BCDEN);
  241. return HAL_OK;
  242. }
  243. /**
  244. * @brief HAL_PCDEx_DeActivateBCD : de-active BatteryCharging feature
  245. * @param hpcd: PCD handle
  246. * @retval HAL status
  247. */
  248. HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd)
  249. {
  250. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  251. hpcd->battery_charging_active = DISABLE;
  252. USBx->GCCFG &= ~(USB_OTG_GCCFG_BCDEN);
  253. return HAL_OK;
  254. }
  255. /**
  256. * @brief HAL_PCDEx_BatteryCharging_Callback : Send BatteryCharging message to user layer
  257. * @param hpcd: PCD handle
  258. * @param msg: LPM message
  259. * @retval HAL status
  260. */
  261. __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
  262. {
  263. /* Prevent unused argument(s) compilation warning */
  264. UNUSED(hpcd);
  265. UNUSED(msg);
  266. }
  267. #endif /* STM32F412Zx || STM32F412Rx || STM32F412Vx || STM32F412Cx */
  268. /**
  269. * @}
  270. */
  271. /**
  272. * @}
  273. */
  274. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||
  275. STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Rx ||
  276. STM32F412Vx || STM32F412Cx */
  277. #endif /* HAL_PCD_MODULE_ENABLED */
  278. /**
  279. * @}
  280. */
  281. /**
  282. * @}
  283. */
  284. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/