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.
 
 
 

522 lines
15 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_pcd_ex.c
  4. * @author MCD Application Team
  5. * @version V1.7.2
  6. * @date 16-June-2017
  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) 2017 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. /** @addtogroup STM32L4xx_HAL_Driver
  44. * @{
  45. */
  46. #ifdef HAL_PCD_MODULE_ENABLED
  47. #if defined(STM32L432xx) || defined(STM32L433xx) || defined(STM32L442xx) || defined(STM32L443xx) || \
  48. defined(STM32L452xx) || defined(STM32L462xx) || \
  49. defined(STM32L475xx) || defined(STM32L476xx) || defined(STM32L485xx) || defined(STM32L486xx) || \
  50. defined(STM32L496xx) || defined(STM32L4A6xx)
  51. /** @defgroup PCDEx PCDEx
  52. * @brief PCD Extended HAL module driver
  53. * @{
  54. */
  55. /* Private types -------------------------------------------------------------*/
  56. /* Private variables ---------------------------------------------------------*/
  57. /* Private constants ---------------------------------------------------------*/
  58. /* Private macros ------------------------------------------------------------*/
  59. /* Private functions ---------------------------------------------------------*/
  60. /* Exported functions --------------------------------------------------------*/
  61. /** @defgroup PCDEx_Exported_Functions PCDEx Exported Functions
  62. * @{
  63. */
  64. /** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions
  65. * @brief PCDEx control functions
  66. *
  67. @verbatim
  68. ===============================================================================
  69. ##### Extended features functions #####
  70. ===============================================================================
  71. [..] This section provides functions allowing to:
  72. (+) Update FIFO configuration
  73. @endverbatim
  74. * @{
  75. */
  76. #if defined (USB_OTG_FS)
  77. /**
  78. * @brief Set Tx FIFO
  79. * @param hpcd: PCD handle
  80. * @param fifo: The number of Tx fifo
  81. * @param size: Fifo size
  82. * @retval HAL status
  83. */
  84. HAL_StatusTypeDef HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo, uint16_t size)
  85. {
  86. uint8_t index = 0;
  87. uint32_t Tx_Offset = 0;
  88. /* TXn min size = 16 words. (n : Transmit FIFO index)
  89. When a TxFIFO is not used, the Configuration should be as follows:
  90. case 1 : n > m and Txn is not used (n,m : Transmit FIFO indexes)
  91. --> Txm can use the space allocated for Txn.
  92. case2 : n < m and Txn is not used (n,m : Transmit FIFO indexes)
  93. --> Txn should be configured with the minimum space of 16 words
  94. The FIFO is used optimally when used TxFIFOs are allocated in the top
  95. of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones.
  96. When DMA is used 3n * FIFO locations should be reserved for internal DMA registers */
  97. Tx_Offset = hpcd->Instance->GRXFSIZ;
  98. if(fifo == 0)
  99. {
  100. hpcd->Instance->DIEPTXF0_HNPTXFSIZ = (size << 16) | Tx_Offset;
  101. }
  102. else
  103. {
  104. Tx_Offset += (hpcd->Instance->DIEPTXF0_HNPTXFSIZ) >> 16;
  105. for (index = 0; index < (fifo - 1); index++)
  106. {
  107. Tx_Offset += (hpcd->Instance->DIEPTXF[index] >> 16);
  108. }
  109. /* Multiply Tx_Size by 2 to get higher performance */
  110. hpcd->Instance->DIEPTXF[fifo - 1] = (size << 16) | Tx_Offset;
  111. }
  112. return HAL_OK;
  113. }
  114. /**
  115. * @brief Set Rx FIFO
  116. * @param hpcd: PCD handle
  117. * @param size: Size of Rx fifo
  118. * @retval HAL status
  119. */
  120. HAL_StatusTypeDef HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef *hpcd, uint16_t size)
  121. {
  122. hpcd->Instance->GRXFSIZ = size;
  123. return HAL_OK;
  124. }
  125. /**
  126. * @brief Activate LPM feature.
  127. * @param hpcd: PCD handle
  128. * @retval HAL status
  129. */
  130. HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
  131. {
  132. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  133. hpcd->lpm_active = ENABLE;
  134. hpcd->LPM_State = LPM_L0;
  135. USBx->GINTMSK |= USB_OTG_GINTMSK_LPMINTM;
  136. USBx->GLPMCFG |= (USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
  137. return HAL_OK;
  138. }
  139. /**
  140. * @brief Deactivate LPM feature.
  141. * @param hpcd: PCD handle
  142. * @retval HAL status
  143. */
  144. HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd)
  145. {
  146. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  147. hpcd->lpm_active = DISABLE;
  148. USBx->GINTMSK &= ~USB_OTG_GINTMSK_LPMINTM;
  149. USBx->GLPMCFG &= ~(USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
  150. return HAL_OK;
  151. }
  152. /**
  153. * @brief Handle BatteryCharging Process.
  154. * @param hpcd: PCD handle
  155. * @retval HAL status
  156. */
  157. void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd)
  158. {
  159. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  160. uint32_t tickstart = HAL_GetTick();
  161. /* Start BCD When device is connected */
  162. if (USBx_DEVICE->DCTL & USB_OTG_DCTL_SDIS)
  163. {
  164. /* Enable DCD : Data Contact Detect */
  165. USBx->GCCFG |= USB_OTG_GCCFG_DCDEN;
  166. /* Wait Detect flag or a timeout is happen*/
  167. while ((USBx->GCCFG & USB_OTG_GCCFG_DCDET) == 0)
  168. {
  169. /* Check for the Timeout */
  170. if((HAL_GetTick() - tickstart ) > 1000)
  171. {
  172. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR);
  173. return;
  174. }
  175. }
  176. /* Right response got */
  177. HAL_Delay(100);
  178. /* Check Detect flag*/
  179. if (USBx->GCCFG & USB_OTG_GCCFG_DCDET)
  180. {
  181. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION);
  182. }
  183. /*Primary detection: checks if connected to Standard Downstream Port
  184. (without charging capability) */
  185. USBx->GCCFG &=~ USB_OTG_GCCFG_DCDEN;
  186. USBx->GCCFG |= USB_OTG_GCCFG_PDEN;
  187. HAL_Delay(100);
  188. if (!(USBx->GCCFG & USB_OTG_GCCFG_PDET))
  189. {
  190. /* Case of Standard Downstream Port */
  191. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  192. }
  193. else
  194. {
  195. /* start secondary detection to check connection to Charging Downstream
  196. Port or Dedicated Charging Port */
  197. USBx->GCCFG &=~ USB_OTG_GCCFG_PDEN;
  198. USBx->GCCFG |= USB_OTG_GCCFG_SDEN;
  199. HAL_Delay(100);
  200. if ((USBx->GCCFG) & USB_OTG_GCCFG_SDET)
  201. {
  202. /* case Dedicated Charging Port */
  203. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
  204. }
  205. else
  206. {
  207. /* case Charging Downstream Port */
  208. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
  209. }
  210. }
  211. /* Battery Charging capability discovery finished */
  212. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
  213. }
  214. }
  215. /**
  216. * @brief Activate BatteryCharging feature.
  217. * @param hpcd: PCD handle
  218. * @retval HAL status
  219. */
  220. HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd)
  221. {
  222. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  223. hpcd->battery_charging_active = ENABLE;
  224. USBx->GCCFG |= (USB_OTG_GCCFG_BCDEN);
  225. return HAL_OK;
  226. }
  227. /**
  228. * @brief Deactivate BatteryCharging feature.
  229. * @param hpcd: PCD handle
  230. * @retval HAL status
  231. */
  232. HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd)
  233. {
  234. USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
  235. hpcd->battery_charging_active = DISABLE;
  236. USBx->GCCFG &= ~(USB_OTG_GCCFG_BCDEN);
  237. return HAL_OK;
  238. }
  239. #endif /* USB_OTG_FS */
  240. #if defined (USB)
  241. /**
  242. * @brief Configure PMA for EP
  243. * @param hpcd : Device instance
  244. * @param ep_addr: endpoint address
  245. * @param ep_kind: endpoint Kind
  246. * USB_SNG_BUF: Single Buffer used
  247. * USB_DBL_BUF: Double Buffer used
  248. * @param pmaadress: EP address in The PMA: In case of single buffer endpoint
  249. * this parameter is 16-bit value providing the address
  250. * in PMA allocated to endpoint.
  251. * In case of double buffer endpoint this parameter
  252. * is a 32-bit value providing the endpoint buffer 0 address
  253. * in the LSB part of 32-bit value and endpoint buffer 1 address
  254. * in the MSB part of 32-bit value.
  255. * @retval HAL status
  256. */
  257. HAL_StatusTypeDef HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd,
  258. uint16_t ep_addr,
  259. uint16_t ep_kind,
  260. uint32_t pmaadress)
  261. {
  262. PCD_EPTypeDef *ep = NULL;
  263. /* initialize ep structure*/
  264. if ((0x80 & ep_addr) == 0x80)
  265. {
  266. ep = &hpcd->IN_ep[ep_addr & 0x7F];
  267. }
  268. else
  269. {
  270. ep = &hpcd->OUT_ep[ep_addr];
  271. }
  272. /* Here we check if the endpoint is single or double Buffer*/
  273. if (ep_kind == PCD_SNG_BUF)
  274. {
  275. /*Single Buffer*/
  276. ep->doublebuffer = 0;
  277. /*Configure te PMA*/
  278. ep->pmaadress = (uint16_t)pmaadress;
  279. }
  280. else /*USB_DBL_BUF*/
  281. {
  282. /*Double Buffer Endpoint*/
  283. ep->doublebuffer = 1;
  284. /*Configure the PMA*/
  285. ep->pmaaddr0 = pmaadress & 0xFFFF;
  286. ep->pmaaddr1 = (pmaadress & 0xFFFF0000) >> 16;
  287. }
  288. return HAL_OK;
  289. }
  290. /**
  291. * @brief Activate BatteryCharging feature.
  292. * @param hpcd: PCD handle
  293. * @retval HAL status
  294. */
  295. HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd)
  296. {
  297. USB_TypeDef *USBx = hpcd->Instance;
  298. hpcd->battery_charging_active = ENABLE;
  299. USBx->BCDR |= (USB_BCDR_BCDEN);
  300. /* Enable DCD : Data Contact Detect */
  301. USBx->BCDR |= (USB_BCDR_DCDEN);
  302. return HAL_OK;
  303. }
  304. /**
  305. * @brief Deactivate BatteryCharging feature.
  306. * @param hpcd: PCD handle
  307. * @retval HAL status
  308. */
  309. HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd)
  310. {
  311. USB_TypeDef *USBx = hpcd->Instance;
  312. hpcd->battery_charging_active = DISABLE;
  313. USBx->BCDR &= ~(USB_BCDR_BCDEN);
  314. return HAL_OK;
  315. }
  316. /**
  317. * @brief Handle BatteryCharging Process.
  318. * @param hpcd: PCD handle
  319. * @retval HAL status
  320. */
  321. void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd)
  322. {
  323. USB_TypeDef *USBx = hpcd->Instance;
  324. uint32_t tickstart = HAL_GetTick();
  325. /* Wait Detect flag or a timeout is happen*/
  326. while ((USBx->BCDR & USB_BCDR_DCDET) == 0)
  327. {
  328. /* Check for the Timeout */
  329. if((HAL_GetTick() - tickstart ) > 1000)
  330. {
  331. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR);
  332. return;
  333. }
  334. }
  335. HAL_Delay(300);
  336. /* Data Pin Contact ? Check Detect flag */
  337. if (USBx->BCDR & USB_BCDR_DCDET)
  338. {
  339. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION);
  340. }
  341. /* Primary detection: checks if connected to Standard Downstream Port
  342. (without charging capability) */
  343. USBx->BCDR &= ~(USB_BCDR_DCDEN);
  344. USBx->BCDR |= (USB_BCDR_PDEN);
  345. HAL_Delay(300);
  346. /* If Charger detect ? */
  347. if (USBx->BCDR & USB_BCDR_PDET)
  348. {
  349. /* Start secondary detection to check connection to Charging Downstream
  350. Port or Dedicated Charging Port */
  351. USBx->BCDR &= ~(USB_BCDR_PDEN);
  352. USBx->BCDR |= (USB_BCDR_SDEN);
  353. HAL_Delay(300);
  354. /* If CDP ? */
  355. if (USBx->BCDR & USB_BCDR_SDET)
  356. {
  357. /* Dedicated Downstream Port DCP */
  358. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
  359. }
  360. else
  361. {
  362. /* Charging Downstream Port CDP */
  363. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
  364. /* Battery Charging capability discovery finished
  365. Start Enumeration*/
  366. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
  367. }
  368. }
  369. else /* NO */
  370. {
  371. /* Standard Downstream Port */
  372. HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
  373. }
  374. }
  375. /**
  376. * @brief Activate LPM feature.
  377. * @param hpcd: PCD handle
  378. * @retval HAL status
  379. */
  380. HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
  381. {
  382. USB_TypeDef *USBx = hpcd->Instance;
  383. hpcd->lpm_active = ENABLE;
  384. hpcd->LPM_State = LPM_L0;
  385. USBx->LPMCSR |= (USB_LPMCSR_LMPEN);
  386. USBx->LPMCSR |= (USB_LPMCSR_LPMACK);
  387. return HAL_OK;
  388. }
  389. /**
  390. * @brief Deactivate LPM feature.
  391. * @param hpcd: PCD handle
  392. * @retval HAL status
  393. */
  394. HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd)
  395. {
  396. USB_TypeDef *USBx = hpcd->Instance;
  397. hpcd->lpm_active = DISABLE;
  398. USBx->LPMCSR &= ~ (USB_LPMCSR_LMPEN);
  399. USBx->LPMCSR &= ~ (USB_LPMCSR_LPMACK);
  400. return HAL_OK;
  401. }
  402. #endif /* USB */
  403. /**
  404. * @brief Send LPM message to user layer callback.
  405. * @param hpcd: PCD handle
  406. * @param msg: LPM message
  407. * @retval HAL status
  408. */
  409. __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
  410. {
  411. /* Prevent unused argument(s) compilation warning */
  412. UNUSED(hpcd);
  413. UNUSED(msg);
  414. /* NOTE : This function should not be modified, when the callback is needed,
  415. the HAL_PCDEx_LPM_Callback could be implemented in the user file
  416. */
  417. }
  418. /**
  419. * @brief Send BatteryCharging message to user layer callback.
  420. * @param hpcd: PCD handle
  421. * @param msg: LPM message
  422. * @retval HAL status
  423. */
  424. __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
  425. {
  426. /* Prevent unused argument(s) compilation warning */
  427. UNUSED(hpcd);
  428. UNUSED(msg);
  429. /* NOTE : This function should not be modified, when the callback is needed,
  430. the HAL_PCDEx_BCD_Callback could be implemented in the user file
  431. */
  432. }
  433. /**
  434. * @}
  435. */
  436. /**
  437. * @}
  438. */
  439. /**
  440. * @}
  441. */
  442. /**
  443. * @}
  444. */
  445. #endif /* STM32L432xx || STM32L433xx || STM32L442xx || STM32L443xx || */
  446. /* STM32L452xx || STM32L462xx || */
  447. /* STM32L475xx || STM32L476xx || STM32L485xx || STM32L486xx || */
  448. /* STM32L496xx || STM32L4A6xx */
  449. #endif /* HAL_PCD_MODULE_ENABLED */
  450. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/