stm32g0xx_hal_pcd_ex.c 9.3 KB

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