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.
 
 
 

339 lines
9.1 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32wbxx_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) 2019 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 "stm32wbxx_hal.h"
  25. /** @addtogroup STM32WBxx_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)
  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. /**
  56. * @brief Configure PMA for EP
  57. * @param hpcd Device instance
  58. * @param ep_addr endpoint address
  59. * @param ep_kind endpoint Kind
  60. * USB_SNG_BUF: Single Buffer used
  61. * USB_DBL_BUF: Double Buffer used
  62. * @param pmaadress: EP address in The PMA: In case of single buffer endpoint
  63. * this parameter is 16-bit value providing the address
  64. * in PMA allocated to endpoint.
  65. * In case of double buffer endpoint this parameter
  66. * is a 32-bit value providing the endpoint buffer 0 address
  67. * in the LSB part of 32-bit value and endpoint buffer 1 address
  68. * in the MSB part of 32-bit value.
  69. * @retval HAL status
  70. */
  71. HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd,
  72. uint16_t ep_addr,
  73. uint16_t ep_kind,
  74. uint32_t pmaadress)
  75. {
  76. PCD_EPTypeDef *ep;
  77. /* initialize ep structure*/
  78. if ((0x80U & ep_addr) == 0x80U)
  79. {
  80. ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
  81. }
  82. else
  83. {
  84. ep = &hpcd->OUT_ep[ep_addr];
  85. }
  86. /* Here we check if the endpoint is single or double Buffer*/
  87. if (ep_kind == PCD_SNG_BUF)
  88. {
  89. /* Single Buffer */
  90. ep->doublebuffer = 0U;
  91. /* Configure the PMA */
  92. ep->pmaadress = (uint16_t)pmaadress;
  93. }
  94. else /* USB_DBL_BUF */
  95. {
  96. /* Double Buffer Endpoint */
  97. ep->doublebuffer = 1U;
  98. /* Configure the PMA */
  99. ep->pmaaddr0 = (uint16_t)(pmaadress & 0xFFFFU);
  100. ep->pmaaddr1 = (uint16_t)((pmaadress & 0xFFFF0000U) >> 16);
  101. }
  102. return HAL_OK;
  103. }
  104. /**
  105. * @brief Activate BatteryCharging feature.
  106. * @param hpcd PCD handle
  107. * @retval HAL status
  108. */
  109. HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd)
  110. {
  111. USB_TypeDef *USBx = hpcd->Instance;
  112. hpcd->battery_charging_active = 1U;
  113. /* Enable BCD feature */
  114. USBx->BCDR |= USB_BCDR_BCDEN;
  115. /* Enable DCD : Data Contact Detect */
  116. USBx->BCDR &= ~(USB_BCDR_PDEN);
  117. USBx->BCDR &= ~(USB_BCDR_SDEN);
  118. USBx->BCDR |= USB_BCDR_DCDEN;
  119. return HAL_OK;
  120. }
  121. /**
  122. * @brief Deactivate BatteryCharging feature.
  123. * @param hpcd PCD handle
  124. * @retval HAL status
  125. */
  126. HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd)
  127. {
  128. USB_TypeDef *USBx = hpcd->Instance;
  129. hpcd->battery_charging_active = 0U;
  130. /* Disable BCD feature */
  131. USBx->BCDR &= ~(USB_BCDR_BCDEN);
  132. return HAL_OK;
  133. }
  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_TypeDef *USBx = hpcd->Instance;
  142. uint32_t tickstart = HAL_GetTick();
  143. /* Wait Detect flag or a timeout is happen*/
  144. while ((USBx->BCDR & USB_BCDR_DCDET) == 0U)
  145. {
  146. /* Check for the Timeout */
  147. if ((HAL_GetTick() - tickstart) > 1000U)
  148. {
  149. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  150. hpcd->BCDCallback(hpcd, PCD_BCD_ERROR);
  151. #else
  152. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR);
  153. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  154. return;
  155. }
  156. }
  157. HAL_Delay(200U);
  158. /* Data Pin Contact ? Check Detect flag */
  159. if ((USBx->BCDR & USB_BCDR_DCDET) == USB_BCDR_DCDET)
  160. {
  161. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  162. hpcd->BCDCallback(hpcd, PCD_BCD_CONTACT_DETECTION);
  163. #else
  164. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION);
  165. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  166. }
  167. /* Primary detection: checks if connected to Standard Downstream Port
  168. (without charging capability) */
  169. USBx->BCDR &= ~(USB_BCDR_DCDEN);
  170. HAL_Delay(50U);
  171. USBx->BCDR |= (USB_BCDR_PDEN);
  172. HAL_Delay(50U);
  173. /* If Charger detect ? */
  174. if ((USBx->BCDR & USB_BCDR_PDET) == USB_BCDR_PDET)
  175. {
  176. /* Start secondary detection to check connection to Charging Downstream
  177. Port or Dedicated Charging Port */
  178. USBx->BCDR &= ~(USB_BCDR_PDEN);
  179. HAL_Delay(50U);
  180. USBx->BCDR |= (USB_BCDR_SDEN);
  181. HAL_Delay(50U);
  182. /* If CDP ? */
  183. if ((USBx->BCDR & USB_BCDR_SDET) == USB_BCDR_SDET)
  184. {
  185. /* Dedicated Downstream Port DCP */
  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. /* Charging Downstream Port CDP */
  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. else /* NO */
  203. {
  204. /* Standard Downstream Port */
  205. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  206. hpcd->BCDCallback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  207. #else
  208. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  209. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  210. }
  211. /* Battery Charging capability discovery finished Start Enumeration */
  212. (void)HAL_PCDEx_DeActivateBCD(hpcd);
  213. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  214. hpcd->BCDCallback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
  215. #else
  216. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
  217. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  218. }
  219. /**
  220. * @brief Activate LPM feature.
  221. * @param hpcd PCD handle
  222. * @retval HAL status
  223. */
  224. HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
  225. {
  226. USB_TypeDef *USBx = hpcd->Instance;
  227. hpcd->lpm_active = 1U;
  228. hpcd->LPM_State = LPM_L0;
  229. USBx->LPMCSR |= USB_LPMCSR_LMPEN;
  230. USBx->LPMCSR |= USB_LPMCSR_LPMACK;
  231. return HAL_OK;
  232. }
  233. /**
  234. * @brief Deactivate LPM feature.
  235. * @param hpcd PCD handle
  236. * @retval HAL status
  237. */
  238. HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd)
  239. {
  240. USB_TypeDef *USBx = hpcd->Instance;
  241. hpcd->lpm_active = 0U;
  242. USBx->LPMCSR &= ~(USB_LPMCSR_LMPEN);
  243. USBx->LPMCSR &= ~(USB_LPMCSR_LPMACK);
  244. return HAL_OK;
  245. }
  246. /**
  247. * @brief Send LPM message to user layer callback.
  248. * @param hpcd PCD handle
  249. * @param msg LPM message
  250. * @retval HAL status
  251. */
  252. __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
  253. {
  254. /* Prevent unused argument(s) compilation warning */
  255. UNUSED(hpcd);
  256. UNUSED(msg);
  257. /* NOTE : This function should not be modified, when the callback is needed,
  258. the HAL_PCDEx_LPM_Callback could be implemented in the user file
  259. */
  260. }
  261. /**
  262. * @brief Send BatteryCharging message to user layer callback.
  263. * @param hpcd PCD handle
  264. * @param msg LPM message
  265. * @retval HAL status
  266. */
  267. __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
  268. {
  269. /* Prevent unused argument(s) compilation warning */
  270. UNUSED(hpcd);
  271. UNUSED(msg);
  272. /* NOTE : This function should not be modified, when the callback is needed,
  273. the HAL_PCDEx_BCD_Callback could be implemented in the user file
  274. */
  275. }
  276. /**
  277. * @}
  278. */
  279. /**
  280. * @}
  281. */
  282. #endif /* defined (USB) */
  283. #endif /* HAL_PCD_MODULE_ENABLED */
  284. /**
  285. * @}
  286. */
  287. /**
  288. * @}
  289. */
  290. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/