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.
 
 
 

332 lines
9.0 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_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 "stm32l0xx_hal.h"
  25. /** @addtogroup STM32L0xx_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. USBx->BCDR |= (USB_BCDR_BCDEN);
  114. /* Enable DCD : Data Contact Detect */
  115. USBx->BCDR |= (USB_BCDR_DCDEN);
  116. return HAL_OK;
  117. }
  118. /**
  119. * @brief Deactivate BatteryCharging feature.
  120. * @param hpcd PCD handle
  121. * @retval HAL status
  122. */
  123. HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd)
  124. {
  125. USB_TypeDef *USBx = hpcd->Instance;
  126. hpcd->battery_charging_active = 0U;
  127. USBx->BCDR &= ~(USB_BCDR_BCDEN);
  128. return HAL_OK;
  129. }
  130. /**
  131. * @brief Handle BatteryCharging Process.
  132. * @param hpcd PCD handle
  133. * @retval HAL status
  134. */
  135. void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd)
  136. {
  137. USB_TypeDef *USBx = hpcd->Instance;
  138. uint32_t tickstart = HAL_GetTick();
  139. /* Wait Detect flag or a timeout is happen*/
  140. while ((USBx->BCDR & USB_BCDR_DCDET) == 0U)
  141. {
  142. /* Check for the Timeout */
  143. if ((HAL_GetTick() - tickstart) > 1000U)
  144. {
  145. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  146. hpcd->BCDCallback(hpcd, PCD_BCD_ERROR);
  147. #else
  148. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR);
  149. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  150. return;
  151. }
  152. }
  153. HAL_Delay(300U);
  154. /* Data Pin Contact ? Check Detect flag */
  155. if ((USBx->BCDR & USB_BCDR_DCDET) == USB_BCDR_DCDET)
  156. {
  157. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  158. hpcd->BCDCallback(hpcd, PCD_BCD_CONTACT_DETECTION);
  159. #else
  160. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION);
  161. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  162. }
  163. /* Primary detection: checks if connected to Standard Downstream Port
  164. (without charging capability) */
  165. USBx->BCDR &= ~(USB_BCDR_DCDEN);
  166. USBx->BCDR |= (USB_BCDR_PDEN);
  167. HAL_Delay(300U);
  168. /* If Charger detect ? */
  169. if ((USBx->BCDR & USB_BCDR_PDET) == USB_BCDR_PDET)
  170. {
  171. /* Start secondary detection to check connection to Charging Downstream
  172. Port or Dedicated Charging Port */
  173. USBx->BCDR &= ~(USB_BCDR_PDEN);
  174. USBx->BCDR |= (USB_BCDR_SDEN);
  175. HAL_Delay(300U);
  176. /* If CDP ? */
  177. if ((USBx->BCDR & USB_BCDR_SDET) == USB_BCDR_SDET)
  178. {
  179. /* Dedicated Downstream Port DCP */
  180. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  181. hpcd->BCDCallback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
  182. #else
  183. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
  184. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  185. }
  186. else
  187. {
  188. /* Charging Downstream Port CDP */
  189. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  190. hpcd->BCDCallback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
  191. #else
  192. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
  193. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  194. }
  195. }
  196. else /* NO */
  197. {
  198. /* Standard Downstream Port */
  199. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  200. hpcd->BCDCallback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  201. #else
  202. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  203. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  204. }
  205. /* Battery Charging capability discovery finished Start Enumeration */
  206. (void)HAL_PCDEx_DeActivateBCD(hpcd);
  207. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  208. hpcd->BCDCallback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
  209. #else
  210. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
  211. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  212. }
  213. /**
  214. * @brief Activate LPM feature.
  215. * @param hpcd PCD handle
  216. * @retval HAL status
  217. */
  218. HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
  219. {
  220. USB_TypeDef *USBx = hpcd->Instance;
  221. hpcd->lpm_active = 1U;
  222. hpcd->LPM_State = LPM_L0;
  223. USBx->LPMCSR |= USB_LPMCSR_LMPEN;
  224. USBx->LPMCSR |= USB_LPMCSR_LPMACK;
  225. return HAL_OK;
  226. }
  227. /**
  228. * @brief Deactivate LPM feature.
  229. * @param hpcd PCD handle
  230. * @retval HAL status
  231. */
  232. HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd)
  233. {
  234. USB_TypeDef *USBx = hpcd->Instance;
  235. hpcd->lpm_active = 0U;
  236. USBx->LPMCSR &= ~(USB_LPMCSR_LMPEN);
  237. USBx->LPMCSR &= ~(USB_LPMCSR_LPMACK);
  238. return HAL_OK;
  239. }
  240. /**
  241. * @brief Send LPM message to user layer callback.
  242. * @param hpcd PCD handle
  243. * @param msg LPM message
  244. * @retval HAL status
  245. */
  246. __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
  247. {
  248. /* Prevent unused argument(s) compilation warning */
  249. UNUSED(hpcd);
  250. UNUSED(msg);
  251. /* NOTE : This function should not be modified, when the callback is needed,
  252. the HAL_PCDEx_LPM_Callback could be implemented in the user file
  253. */
  254. }
  255. /**
  256. * @brief Send BatteryCharging message to user layer callback.
  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. /* NOTE : This function should not be modified, when the callback is needed,
  267. the HAL_PCDEx_BCD_Callback could be implemented in the user file
  268. */
  269. }
  270. /**
  271. * @}
  272. */
  273. /**
  274. * @}
  275. */
  276. #endif /* defined (USB) */
  277. #endif /* HAL_PCD_MODULE_ENABLED */
  278. /**
  279. * @}
  280. */
  281. /**
  282. * @}
  283. */
  284. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/