stm32l4xx_hal_pcd_ex.c 16 KB

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