Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:08

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_pcd_ex.c
0004   * @author  MCD Application Team
0005   * @brief   PCD Extended HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of the USB Peripheral Controller:
0008   *           + Extended features functions
0009   *
0010   ******************************************************************************
0011   * @attention
0012   *
0013   * Copyright (c) 2017 STMicroelectronics.
0014   * All rights reserved.
0015   *
0016   * This software is licensed under terms that can be found in the LICENSE file
0017   * in the root directory of this software component.
0018   * If no LICENSE file comes with this software, it is provided AS-IS.
0019   *
0020   ******************************************************************************
0021   */
0022 
0023 /* Includes ------------------------------------------------------------------*/
0024 #include "stm32h7xx_hal.h"
0025 
0026 /** @addtogroup STM32H7xx_HAL_Driver
0027   * @{
0028   */
0029 
0030 /** @defgroup PCDEx PCDEx
0031   * @ingroup RTEMSBSPsARMSTM32H7
0032   * @brief PCD Extended HAL module driver
0033   * @{
0034   */
0035 
0036 #ifdef HAL_PCD_MODULE_ENABLED
0037 
0038 #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
0039 /* Private types -------------------------------------------------------------*/
0040 /* Private variables ---------------------------------------------------------*/
0041 /* Private constants ---------------------------------------------------------*/
0042 /* Private macros ------------------------------------------------------------*/
0043 /* Private functions ---------------------------------------------------------*/
0044 /* Exported functions --------------------------------------------------------*/
0045 
0046 /** @defgroup PCDEx_Exported_Functions PCDEx Exported Functions
0047   * @ingroup RTEMSBSPsARMSTM32H7
0048   * @{
0049   */
0050 
0051 /** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions
0052   * @ingroup RTEMSBSPsARMSTM32H7
0053   * @brief    PCDEx control functions
0054   *
0055 @verbatim
0056  ===============================================================================
0057                  ##### Extended features functions #####
0058  ===============================================================================
0059     [..]  This section provides functions allowing to:
0060       (+) Update FIFO configuration
0061 
0062 @endverbatim
0063   * @{
0064   */
0065 #if defined (USB_OTG_FS) || defined (USB_OTG_HS)
0066 /**
0067   * @brief  Set Tx FIFO
0068   * @param  hpcd PCD handle
0069   * @param  fifo The number of Tx fifo
0070   * @param  size Fifo size
0071   * @retval HAL status
0072   */
0073 HAL_StatusTypeDef HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo, uint16_t size)
0074 {
0075   uint8_t i;
0076   uint32_t Tx_Offset;
0077 
0078   /*  TXn min size = 16 words. (n  : Transmit FIFO index)
0079       When a TxFIFO is not used, the Configuration should be as follows:
0080           case 1 :  n > m    and Txn is not used    (n,m  : Transmit FIFO indexes)
0081          --> Txm can use the space allocated for Txn.
0082          case2  :  n < m    and Txn is not used    (n,m  : Transmit FIFO indexes)
0083          --> Txn should be configured with the minimum space of 16 words
0084      The FIFO is used optimally when used TxFIFOs are allocated in the top
0085          of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones.
0086      When DMA is used 3n * FIFO locations should be reserved for internal DMA registers */
0087 
0088   Tx_Offset = hpcd->Instance->GRXFSIZ;
0089 
0090   if (fifo == 0U)
0091   {
0092     hpcd->Instance->DIEPTXF0_HNPTXFSIZ = ((uint32_t)size << 16) | Tx_Offset;
0093   }
0094   else
0095   {
0096     Tx_Offset += (hpcd->Instance->DIEPTXF0_HNPTXFSIZ) >> 16;
0097     for (i = 0U; i < (fifo - 1U); i++)
0098     {
0099       Tx_Offset += (hpcd->Instance->DIEPTXF[i] >> 16);
0100     }
0101 
0102     /* Multiply Tx_Size by 2 to get higher performance */
0103     hpcd->Instance->DIEPTXF[fifo - 1U] = ((uint32_t)size << 16) | Tx_Offset;
0104   }
0105 
0106   return HAL_OK;
0107 }
0108 
0109 /**
0110   * @brief  Set Rx FIFO
0111   * @param  hpcd PCD handle
0112   * @param  size Size of Rx fifo
0113   * @retval HAL status
0114   */
0115 HAL_StatusTypeDef HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef *hpcd, uint16_t size)
0116 {
0117   hpcd->Instance->GRXFSIZ = size;
0118 
0119   return HAL_OK;
0120 }
0121 
0122 /**
0123   * @brief  Activate LPM feature.
0124   * @param  hpcd PCD handle
0125   * @retval HAL status
0126   */
0127 HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
0128 {
0129   USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
0130 
0131   hpcd->lpm_active = 1U;
0132   hpcd->LPM_State = LPM_L0;
0133   USBx->GINTMSK |= USB_OTG_GINTMSK_LPMINTM;
0134   USBx->GLPMCFG |= (USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
0135 
0136   return HAL_OK;
0137 }
0138 
0139 /**
0140   * @brief  Deactivate LPM feature.
0141   * @param  hpcd PCD handle
0142   * @retval HAL status
0143   */
0144 HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd)
0145 {
0146   USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
0147 
0148   hpcd->lpm_active = 0U;
0149   USBx->GINTMSK &= ~USB_OTG_GINTMSK_LPMINTM;
0150   USBx->GLPMCFG &= ~(USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
0151 
0152   return HAL_OK;
0153 }
0154 
0155 
0156 /**
0157   * @brief  Handle BatteryCharging Process.
0158   * @param  hpcd PCD handle
0159   * @retval HAL status
0160   */
0161 void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd)
0162 {
0163   USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
0164   uint32_t tickstart = HAL_GetTick();
0165 
0166   /* Enable DCD : Data Contact Detect */
0167   USBx->GCCFG |= USB_OTG_GCCFG_DCDEN;
0168 
0169   /* Wait for Min DCD Timeout */
0170   HAL_Delay(300U);
0171 
0172   /* Check Detect flag */
0173   if ((USBx->GCCFG & USB_OTG_GCCFG_DCDET) == USB_OTG_GCCFG_DCDET)
0174   {
0175 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
0176     hpcd->BCDCallback(hpcd, PCD_BCD_CONTACT_DETECTION);
0177 #else
0178     HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION);
0179 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
0180   }
0181 
0182   /* Primary detection: checks if connected to Standard Downstream Port
0183   (without charging capability) */
0184   USBx->GCCFG &= ~USB_OTG_GCCFG_DCDEN;
0185   HAL_Delay(50U);
0186   USBx->GCCFG |= USB_OTG_GCCFG_PDEN;
0187   HAL_Delay(50U);
0188 
0189   if ((USBx->GCCFG & USB_OTG_GCCFG_PDET) == 0U)
0190   {
0191     /* Case of Standard Downstream Port */
0192 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
0193     hpcd->BCDCallback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
0194 #else
0195     HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
0196 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
0197   }
0198   else
0199   {
0200     /* start secondary detection to check connection to Charging Downstream
0201     Port or Dedicated Charging Port */
0202     USBx->GCCFG &= ~(USB_OTG_GCCFG_PDEN);
0203     HAL_Delay(50U);
0204     USBx->GCCFG |= USB_OTG_GCCFG_SDEN;
0205     HAL_Delay(50U);
0206 
0207     if ((USBx->GCCFG & USB_OTG_GCCFG_SDET) == USB_OTG_GCCFG_SDET)
0208     {
0209       /* case Dedicated Charging Port  */
0210 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
0211       hpcd->BCDCallback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
0212 #else
0213       HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
0214 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
0215     }
0216     else
0217     {
0218       /* case Charging Downstream Port */
0219 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
0220       hpcd->BCDCallback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
0221 #else
0222       HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
0223 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
0224     }
0225   }
0226 
0227   /* Battery Charging capability discovery finished */
0228   (void)HAL_PCDEx_DeActivateBCD(hpcd);
0229 
0230   /* Check for the Timeout, else start USB Device */
0231   if ((HAL_GetTick() - tickstart) > 1000U)
0232   {
0233 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
0234     hpcd->BCDCallback(hpcd, PCD_BCD_ERROR);
0235 #else
0236     HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR);
0237 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
0238   }
0239   else
0240   {
0241 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
0242     hpcd->BCDCallback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
0243 #else
0244     HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
0245 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
0246   }
0247 }
0248 
0249 /**
0250   * @brief  Activate BatteryCharging feature.
0251   * @param  hpcd PCD handle
0252   * @retval HAL status
0253   */
0254 HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd)
0255 {
0256   USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
0257 
0258   USBx->GCCFG &= ~(USB_OTG_GCCFG_PDEN);
0259   USBx->GCCFG &= ~(USB_OTG_GCCFG_SDEN);
0260 
0261   /* Power Down USB transceiver  */
0262   USBx->GCCFG &= ~(USB_OTG_GCCFG_PWRDWN);
0263 
0264   /* Enable Battery charging */
0265   USBx->GCCFG |= USB_OTG_GCCFG_BCDEN;
0266 
0267   hpcd->battery_charging_active = 1U;
0268 
0269   return HAL_OK;
0270 }
0271 
0272 /**
0273   * @brief  Deactivate BatteryCharging feature.
0274   * @param  hpcd PCD handle
0275   * @retval HAL status
0276   */
0277 HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd)
0278 {
0279   USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;
0280 
0281   USBx->GCCFG &= ~(USB_OTG_GCCFG_SDEN);
0282   USBx->GCCFG &= ~(USB_OTG_GCCFG_PDEN);
0283 
0284   /* Disable Battery charging */
0285   USBx->GCCFG &= ~(USB_OTG_GCCFG_BCDEN);
0286 
0287   hpcd->battery_charging_active = 0U;
0288 
0289   return HAL_OK;
0290 }
0291 
0292 #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
0293 
0294 /**
0295   * @brief  Send LPM message to user layer callback.
0296   * @param  hpcd PCD handle
0297   * @param  msg LPM message
0298   * @retval HAL status
0299   */
0300 __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
0301 {
0302   /* Prevent unused argument(s) compilation warning */
0303   UNUSED(hpcd);
0304   UNUSED(msg);
0305 
0306   /* NOTE : This function should not be modified, when the callback is needed,
0307             the HAL_PCDEx_LPM_Callback could be implemented in the user file
0308    */
0309 }
0310 
0311 /**
0312   * @brief  Send BatteryCharging message to user layer callback.
0313   * @param  hpcd PCD handle
0314   * @param  msg LPM message
0315   * @retval HAL status
0316   */
0317 __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
0318 {
0319   /* Prevent unused argument(s) compilation warning */
0320   UNUSED(hpcd);
0321   UNUSED(msg);
0322 
0323   /* NOTE : This function should not be modified, when the callback is needed,
0324             the HAL_PCDEx_BCD_Callback could be implemented in the user file
0325    */
0326 }
0327 
0328 /**
0329   * @}
0330   */
0331 
0332 /**
0333   * @}
0334   */
0335 #endif /* defined (USB_OTG_FS) || defined (USB_OTG_HS) */
0336 #endif /* HAL_PCD_MODULE_ENABLED */
0337 
0338 /**
0339   * @}
0340   */
0341 
0342 /**
0343   * @}
0344   */