训练营PLSR题目
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

342 wiersze
10 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_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) 2016 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 "stm32f4xx_hal.h"
  24. /** @addtogroup STM32F4xx_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_OTG_FS) || defined (USB_OTG_HS)
  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. #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
  55. /**
  56. * @brief Set Tx FIFO
  57. * @param hpcd PCD handle
  58. * @param fifo The number of Tx fifo
  59. * @param size Fifo size
  60. * @retval HAL status
  61. */
  62. HAL_StatusTypeDef HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo, uint16_t size)
  63. {
  64. uint8_t i;
  65. uint32_t Tx_Offset;
  66. /* TXn min size = 16 words. (n : Transmit FIFO index)
  67. When a TxFIFO is not used, the Configuration should be as follows:
  68. case 1 : n > m and Txn is not used (n,m : Transmit FIFO indexes)
  69. --> Txm can use the space allocated for Txn.
  70. case2 : n < m and Txn is not used (n,m : Transmit FIFO indexes)
  71. --> Txn should be configured with the minimum space of 16 words
  72. The FIFO is used optimally when used TxFIFOs are allocated in the top
  73. of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones.
  74. When DMA is used 3n * FIFO locations should be reserved for internal DMA registers */
  75. Tx_Offset = hpcd->Instance->GRXFSIZ;
  76. if (fifo == 0U)
  77. {
  78. hpcd->Instance->DIEPTXF0_HNPTXFSIZ = ((uint32_t)size << 16) | Tx_Offset;
  79. }
  80. else
  81. {
  82. Tx_Offset += (hpcd->Instance->DIEPTXF0_HNPTXFSIZ) >> 16;
  83. for (i = 0U; i < (fifo - 1U); i++)
  84. {
  85. Tx_Offset += (hpcd->Instance->DIEPTXF[i] >> 16);
  86. }
  87. /* Multiply Tx_Size by 2 to get higher performance */
  88. hpcd->Instance->DIEPTXF[fifo - 1U] = ((uint32_t)size << 16) | Tx_Offset;
  89. }
  90. return HAL_OK;
  91. }
  92. /**
  93. * @brief Set Rx FIFO
  94. * @param hpcd PCD handle
  95. * @param size Size of Rx fifo
  96. * @retval HAL status
  97. */
  98. HAL_StatusTypeDef HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef *hpcd, uint16_t size)
  99. {
  100. hpcd->Instance->GRXFSIZ = size;
  101. return HAL_OK;
  102. }
  103. #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  104. /**
  105. * @brief Activate LPM feature.
  106. * @param hpcd PCD handle
  107. * @retval HAL status
  108. */
  109. HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
  110. {
  111. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  112. hpcd->lpm_active = 1U;
  113. hpcd->LPM_State = LPM_L0;
  114. USBx->GINTMSK |= USB_OTG_GINTMSK_LPMINTM;
  115. USBx->GLPMCFG |= (USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
  116. return HAL_OK;
  117. }
  118. /**
  119. * @brief Deactivate LPM feature.
  120. * @param hpcd PCD handle
  121. * @retval HAL status
  122. */
  123. HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd)
  124. {
  125. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  126. hpcd->lpm_active = 0U;
  127. USBx->GINTMSK &= ~USB_OTG_GINTMSK_LPMINTM;
  128. USBx->GLPMCFG &= ~(USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
  129. return HAL_OK;
  130. }
  131. #endif /* defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx) */
  132. #if defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
  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_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  141. uint32_t tickstart = HAL_GetTick();
  142. /* Enable DCD : Data Contact Detect */
  143. USBx->GCCFG |= USB_OTG_GCCFG_DCDEN;
  144. /* Wait for Min DCD Timeout */
  145. HAL_Delay(300U);
  146. /* Check Detect flag */
  147. if ((USBx->GCCFG & USB_OTG_GCCFG_DCDET) == USB_OTG_GCCFG_DCDET)
  148. {
  149. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  150. hpcd->BCDCallback(hpcd, PCD_BCD_CONTACT_DETECTION);
  151. #else
  152. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION);
  153. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  154. }
  155. /* Primary detection: checks if connected to Standard Downstream Port
  156. (without charging capability) */
  157. USBx->GCCFG &= ~ USB_OTG_GCCFG_DCDEN;
  158. HAL_Delay(50U);
  159. USBx->GCCFG |= USB_OTG_GCCFG_PDEN;
  160. HAL_Delay(50U);
  161. if ((USBx->GCCFG & USB_OTG_GCCFG_PDET) == 0U)
  162. {
  163. /* Case of Standard Downstream Port */
  164. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  165. hpcd->BCDCallback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  166. #else
  167. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  168. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  169. }
  170. else
  171. {
  172. /* start secondary detection to check connection to Charging Downstream
  173. Port or Dedicated Charging Port */
  174. USBx->GCCFG &= ~ USB_OTG_GCCFG_PDEN;
  175. HAL_Delay(50U);
  176. USBx->GCCFG |= USB_OTG_GCCFG_SDEN;
  177. HAL_Delay(50U);
  178. if ((USBx->GCCFG & USB_OTG_GCCFG_SDET) == USB_OTG_GCCFG_SDET)
  179. {
  180. /* case Dedicated Charging Port */
  181. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  182. hpcd->BCDCallback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
  183. #else
  184. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
  185. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  186. }
  187. else
  188. {
  189. /* case Charging Downstream Port */
  190. #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
  191. hpcd->BCDCallback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
  192. #else
  193. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
  194. #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
  195. }
  196. }
  197. /* Battery Charging capability discovery finished */
  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 BatteryCharging feature.
  219. * @param hpcd PCD handle
  220. * @retval HAL status
  221. */
  222. HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd)
  223. {
  224. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  225. USBx->GCCFG &= ~(USB_OTG_GCCFG_PDEN);
  226. USBx->GCCFG &= ~(USB_OTG_GCCFG_SDEN);
  227. /* Power Down USB transceiver */
  228. USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
  229. /* Enable Battery charging */
  230. USBx->GCCFG |= USB_OTG_GCCFG_BCDEN;
  231. hpcd->battery_charging_active = 1U;
  232. return HAL_OK;
  233. }
  234. /**
  235. * @brief Deactivate BatteryCharging feature.
  236. * @param hpcd PCD handle
  237. * @retval HAL status
  238. */
  239. HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd)
  240. {
  241. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  242. USBx->GCCFG &= ~(USB_OTG_GCCFG_SDEN);
  243. USBx->GCCFG &= ~(USB_OTG_GCCFG_PDEN);
  244. /* Disable Battery charging */
  245. USBx->GCCFG &= ~(USB_OTG_GCCFG_BCDEN);
  246. hpcd->battery_charging_active = 0U;
  247. return HAL_OK;
  248. }
  249. #endif /* defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx) */
  250. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  251. /**
  252. * @brief Send LPM message to user layer callback.
  253. * @param hpcd PCD handle
  254. * @param msg LPM message
  255. * @retval HAL status
  256. */
  257. __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
  258. {
  259. /* Prevent unused argument(s) compilation warning */
  260. UNUSED(hpcd);
  261. UNUSED(msg);
  262. /* NOTE : This function should not be modified, when the callback is needed,
  263. the HAL_PCDEx_LPM_Callback could be implemented in the user file
  264. */
  265. }
  266. /**
  267. * @brief Send BatteryCharging message to user layer callback.
  268. * @param hpcd PCD handle
  269. * @param msg LPM message
  270. * @retval HAL status
  271. */
  272. __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
  273. {
  274. /* Prevent unused argument(s) compilation warning */
  275. UNUSED(hpcd);
  276. UNUSED(msg);
  277. /* NOTE : This function should not be modified, when the callback is needed,
  278. the HAL_PCDEx_BCD_Callback could be implemented in the user file
  279. */
  280. }
  281. /**
  282. * @}
  283. */
  284. /**
  285. * @}
  286. */
  287. #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
  288. #endif /* HAL_PCD_MODULE_ENABLED */
  289. /**
  290. * @}
  291. */
  292. /**
  293. * @}
  294. */