stm32f4xx_hal_pcd_ex.c 11 KB

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