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.
 
 
 

324 lines
9.9 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_pcd_ex.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 29-January-2016
  7. * @brief PCD Extended 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 "stm32l4xx_hal.h"
  43. #if defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx)
  44. /** @addtogroup STM32L4xx_HAL_Driver
  45. * @{
  46. */
  47. /** @defgroup PCDEx PCDEx
  48. * @brief PCD Extended HAL module driver
  49. * @{
  50. */
  51. #ifdef HAL_PCD_MODULE_ENABLED
  52. /* Private types -------------------------------------------------------------*/
  53. /* Private variables ---------------------------------------------------------*/
  54. /* Private constants ---------------------------------------------------------*/
  55. /* Private macros ------------------------------------------------------------*/
  56. /* Private functions ---------------------------------------------------------*/
  57. /* Exported functions --------------------------------------------------------*/
  58. /** @defgroup PCDEx_Exported_Functions PCDEx Exported Functions
  59. * @{
  60. */
  61. /** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions
  62. * @brief PCDEx control functions
  63. *
  64. @verbatim
  65. ===============================================================================
  66. ##### Extended features functions #####
  67. ===============================================================================
  68. [..] This section provides functions allowing to:
  69. (+) Update FIFO configuration
  70. @endverbatim
  71. * @{
  72. */
  73. /**
  74. * @brief Set Tx FIFO
  75. * @param hpcd: PCD handle
  76. * @param fifo: The number of Tx fifo
  77. * @param size: Fifo size
  78. * @retval HAL status
  79. */
  80. HAL_StatusTypeDef HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo, uint16_t size)
  81. {
  82. uint8_t i = 0;
  83. uint32_t Tx_Offset = 0;
  84. /* TXn min size = 16 words. (n : Transmit FIFO index)
  85. When a TxFIFO is not used, the Configuration should be as follows:
  86. case 1 : n > m and Txn is not used (n,m : Transmit FIFO indexes)
  87. --> Txm can use the space allocated for Txn.
  88. case2 : n < m and Txn is not used (n,m : Transmit FIFO indexes)
  89. --> Txn should be configured with the minimum space of 16 words
  90. The FIFO is used optimally when used TxFIFOs are allocated in the top
  91. of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones.
  92. When DMA is used 3n * FIFO locations should be reserved for internal DMA registers */
  93. Tx_Offset = hpcd->Instance->GRXFSIZ;
  94. if(fifo == 0)
  95. {
  96. hpcd->Instance->DIEPTXF0_HNPTXFSIZ = (size << 16) | Tx_Offset;
  97. }
  98. else
  99. {
  100. Tx_Offset += (hpcd->Instance->DIEPTXF0_HNPTXFSIZ) >> 16;
  101. for (i = 0; i < (fifo - 1); i++)
  102. {
  103. Tx_Offset += (hpcd->Instance->DIEPTXF[i] >> 16);
  104. }
  105. /* Multiply Tx_Size by 2 to get higher performance */
  106. hpcd->Instance->DIEPTXF[fifo - 1] = (size << 16) | Tx_Offset;
  107. }
  108. return HAL_OK;
  109. }
  110. /**
  111. * @brief Set Rx FIFO
  112. * @param hpcd: PCD handle
  113. * @param size: Size of Rx fifo
  114. * @retval HAL status
  115. */
  116. HAL_StatusTypeDef HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef *hpcd, uint16_t size)
  117. {
  118. hpcd->Instance->GRXFSIZ = size;
  119. return HAL_OK;
  120. }
  121. /**
  122. * @brief Activate LPM feature.
  123. * @param hpcd: PCD handle
  124. * @retval HAL status
  125. */
  126. HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
  127. {
  128. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  129. hpcd->lpm_active = ENABLE;
  130. hpcd->LPM_State = LPM_L0;
  131. USBx->GINTMSK |= USB_OTG_GINTMSK_LPMINTM;
  132. USBx->GLPMCFG |= (USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
  133. return HAL_OK;
  134. }
  135. /**
  136. * @brief Deactivate LPM feature.
  137. * @param hpcd: PCD handle
  138. * @retval HAL status
  139. */
  140. HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd)
  141. {
  142. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  143. hpcd->lpm_active = DISABLE;
  144. USBx->GINTMSK &= ~USB_OTG_GINTMSK_LPMINTM;
  145. USBx->GLPMCFG &= ~(USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
  146. return HAL_OK;
  147. }
  148. /**
  149. * @brief Handle BatteryCharging Process.
  150. * @param hpcd: PCD handle
  151. * @retval HAL status
  152. */
  153. void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd)
  154. {
  155. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  156. uint32_t tickstart = HAL_GetTick();
  157. /* Start BCD When device is connected */
  158. if (USBx_DEVICE->DCTL & USB_OTG_DCTL_SDIS)
  159. {
  160. /* Enable DCD : Data Contact Detect */
  161. USBx->GCCFG |= USB_OTG_GCCFG_DCDEN;
  162. /* Wait Detect flag or a timeout is happen*/
  163. while ((USBx->GCCFG & USB_OTG_GCCFG_DCDET) == 0)
  164. {
  165. /* Check for the Timeout */
  166. if((HAL_GetTick() - tickstart ) > 1000)
  167. {
  168. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR);
  169. return;
  170. }
  171. }
  172. /* Right response got */
  173. HAL_Delay(100);
  174. /* Check Detect flag*/
  175. if (USBx->GCCFG & USB_OTG_GCCFG_DCDET)
  176. {
  177. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION);
  178. }
  179. /*Primary detection: checks if connected to Standard Downstream Port
  180. (without charging capability) */
  181. USBx->GCCFG &=~ USB_OTG_GCCFG_DCDEN;
  182. USBx->GCCFG |= USB_OTG_GCCFG_PDEN;
  183. HAL_Delay(100);
  184. if (!(USBx->GCCFG & USB_OTG_GCCFG_PDET))
  185. {
  186. /* Case of Standard Downstream Port */
  187. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  188. }
  189. else
  190. {
  191. /* start secondary detection to check connection to Charging Downstream
  192. Port or Dedicated Charging Port */
  193. USBx->GCCFG &=~ USB_OTG_GCCFG_PDEN;
  194. USBx->GCCFG |= USB_OTG_GCCFG_SDEN;
  195. HAL_Delay(100);
  196. if ((USBx->GCCFG) & USB_OTG_GCCFG_SDET)
  197. {
  198. /* case Dedicated Charging Port */
  199. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
  200. }
  201. else
  202. {
  203. /* case Charging Downstream Port */
  204. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
  205. }
  206. }
  207. /* Battery Charging capability discovery finished */
  208. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
  209. }
  210. }
  211. /**
  212. * @brief Activate BatteryCharging feature.
  213. * @param hpcd: PCD handle
  214. * @retval HAL status
  215. */
  216. HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd)
  217. {
  218. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  219. hpcd->battery_charging_active = ENABLE;
  220. USBx->GCCFG |= (USB_OTG_GCCFG_BCDEN);
  221. return HAL_OK;
  222. }
  223. /**
  224. * @brief Deactivate BatteryCharging feature.
  225. * @param hpcd: PCD handle
  226. * @retval HAL status
  227. */
  228. HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd)
  229. {
  230. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  231. hpcd->battery_charging_active = DISABLE;
  232. USBx->GCCFG &= ~(USB_OTG_GCCFG_BCDEN);
  233. return HAL_OK;
  234. }
  235. /**
  236. * @brief Send LPM message to user layer callback.
  237. * @param hpcd: PCD handle
  238. * @param msg: LPM message
  239. * @retval HAL status
  240. */
  241. __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
  242. {
  243. /* Prevent unused argument(s) compilation warning */
  244. UNUSED(hpcd);
  245. UNUSED(msg);
  246. /* NOTE : This function should not be modified, when the callback is needed,
  247. the HAL_PCDEx_LPM_Callback could be implemented in the user file
  248. */
  249. }
  250. /**
  251. * @brief Send BatteryCharging message to user layer callback.
  252. * @param hpcd: PCD handle
  253. * @param msg: LPM message
  254. * @retval HAL status
  255. */
  256. __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
  257. {
  258. /* Prevent unused argument(s) compilation warning */
  259. UNUSED(hpcd);
  260. UNUSED(msg);
  261. /* NOTE : This function should not be modified, when the callback is needed,
  262. the HAL_PCDEx_BCD_Callback could be implemented in the user file
  263. */
  264. }
  265. /**
  266. * @}
  267. */
  268. /**
  269. * @}
  270. */
  271. #endif /* HAL_PCD_MODULE_ENABLED */
  272. /**
  273. * @}
  274. */
  275. /**
  276. * @}
  277. */
  278. #endif /* STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx */
  279. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/