STM32G474xx HAL用户手册
stm32g4xx_hal_pcd_ex.c
跳转到此文件的文档。
00001 /**
00002   ******************************************************************************
00003   * @file    stm32g4xx_hal_pcd_ex.c
00004   * @author  MCD Application Team
00005   * @brief   PCD Extended HAL module driver.
00006   *          This file provides firmware functions to manage the following
00007   *          functionalities of the USB Peripheral Controller:
00008   *           + Extended features functions
00009   *
00010   ******************************************************************************
00011   * @attention
00012   *
00013   * Copyright (c) 2019 STMicroelectronics.
00014   * All rights reserved.
00015   *
00016   * This software is licensed under terms that can be found in the LICENSE file
00017   * in the root directory of this software component.
00018   * If no LICENSE file comes with this software, it is provided AS-IS.
00019   *
00020   ******************************************************************************
00021   */
00022 
00023 /* Includes ------------------------------------------------------------------*/
00024 #include "stm32g4xx_hal.h"
00025 
00026 /** @addtogroup STM32G4xx_HAL_Driver
00027   * @{
00028   */
00029 
00030 /** @defgroup PCDEx PCDEx
00031   * @brief PCD Extended HAL module driver
00032   * @{
00033   */
00034 
00035 #ifdef HAL_PCD_MODULE_ENABLED
00036 
00037 #if defined (USB)
00038 /* Private types -------------------------------------------------------------*/
00039 /* Private variables ---------------------------------------------------------*/
00040 /* Private constants ---------------------------------------------------------*/
00041 /* Private macros ------------------------------------------------------------*/
00042 /* Private functions ---------------------------------------------------------*/
00043 /* Exported functions --------------------------------------------------------*/
00044 
00045 /** @defgroup PCDEx_Exported_Functions PCDEx Exported Functions
00046   * @{
00047   */
00048 
00049 /** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions
00050   * @brief    PCDEx control functions
00051   *
00052 @verbatim
00053  ===============================================================================
00054                  ##### Extended features functions #####
00055  ===============================================================================
00056     [..]  This section provides functions allowing to:
00057       (+) Update FIFO configuration
00058 
00059 @endverbatim
00060   * @{
00061   */
00062 
00063 /**
00064   * @brief  Configure PMA for EP
00065   * @param  hpcd  Device instance
00066   * @param  ep_addr endpoint address
00067   * @param  ep_kind endpoint Kind
00068   *                  USB_SNG_BUF: Single Buffer used
00069   *                  USB_DBL_BUF: Double Buffer used
00070   * @param  pmaadress: EP address in The PMA: In case of single buffer endpoint
00071   *                   this parameter is 16-bit value providing the address
00073   *                   in PMA allocated to endpoint.
00075   *                   In case of double buffer endpoint this parameter
00076   *                   is a 32-bit value providing the endpoint buffer 0 address
00078   *                   in the LSB part of 32-bit value and endpoint buffer 1 address
00079   *                   in the MSB part of 32-bit value.
00080   * @retval HAL status
00081   */
00082 
00083 HAL_StatusTypeDef  HAL_PCDEx_PMAConfig(PCD_HandleTypeDef *hpcd, uint16_t ep_addr,
00084 if ((0x80U & ep_addr) == 0x80U)
00085   {
00086     ep = &hpcd->IN_ep[ep_addr & EP_ADDR_MSK];
00087   }
00088   else
00089   {
00090     ep = &hpcd->OUT_ep[ep_addr];
00091   }
00092 
00093   /* Here we check if the endpoint is single or double Buffer*/
00094   if (ep_kind == PCD_SNG_BUF)
00095   {
00096     /* Single Buffer */
00097     ep->doublebuffer = 0U;
00098 
00099     /* Configure the PMA */
00100     ep->pmaadress = (uint16_t)pmaadress;
00101   }
00102 #if (USE_USB_DOUBLE_BUFFER == 1U)
00103   else /* USB_DBL_BUF */
00104   {
00105     /* Double Buffer Endpoint */
00106     ep->doublebuffer = 1U;
00108 
00109     /* Configure the PMA */
00110     ep->pmaaddr0 = (uint16_t)(pmaadress & 0xFFFFU);
00111     ep->pmaaddr1 = (uint16_t)((pmaadress & 0xFFFF0000U) >> 16);
00112   }
00113 #endif /* (USE_USB_DOUBLE_BUFFER == 1U) */
00114 
00115   return HAL_OK;
00116 }
00117 
00118 /**
00119   * @brief  Activate BatteryCharging feature.
00120   * @param  hpcd PCD handle
00121   * @retval HAL status
00122   */
00123 00124 HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd)
00125 {
00126   USB_TypeDef *USBx = hpcd->Instance;
00127   hpcd->battery_charging_active = 1U;
00128 
00129   USBx->BCDR &= ~(USB_BCDR_PDEN);
00130   USBx->BCDR &= ~(USB_BCDR_SDEN);
00131 
00132   /* Enable BCD feature */
00133   USBx->BCDR |= USB_BCDR_BCDEN;
00135 
00136   return HAL_OK;
00137 }
00138 
00139 /**
00140   * @brief  Deactivate BatteryCharging feature.
00141   * @param  hpcd PCD handle
00142   * @retval HAL status
00143   */
00144 00145 HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd)
00146 {
00147   USB_TypeDef *USBx = hpcd->Instance;
00148   hpcd->battery_charging_active = 0U;
00149 
00150   /* Disable BCD feature */
00151   USBx->BCDR &= ~(USB_BCDR_BCDEN);
00153 
00155   return HAL_OK;
00157 }
00158 
00159 /**
00160   * @brief  Handle BatteryCharging Process.
00161   * @param  hpcd PCD handle
00162   * @retval HAL status
00163   */
00164 00165 void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd)
00166 {
00167   USB_TypeDef *USBx = hpcd->Instance;
00168   uint32_t tickstart = HAL_GetTick();
00169 
00170   /* Wait for Min DCD Timeout */
00171   HAL_Delay(350U);
00173 
00174   /* Primary detection: checks if connected to Standard Downstream Port
00175   (without charging capability) */
00176   USBx->BCDR |= (USB_BCDR_PDEN);
00177   HAL_Delay(50U);
00178 
00178   /* If Charger detect ? */
00179   if ((USBx->BCDR & USB_BCDR_PDET) == USB_BCDR_PDET)
00180   {
00181     /* Start secondary detection to check connection to Charging Downstream
00182     Port or Dedicated Charging Port */
00183     USBx->BCDR &= ~(USB_BCDR_PDEN);
00184     HAL_Delay(50U);
00185     USBx->BCDR |= (USB_BCDR_SDEN);
00186     HAL_Delay(50U);
00187 
00188     /* If CDP ? */
00189     if ((USBx->BCDR & USB_BCDR_SDET) == USB_BCDR_SDET)
00191     {
00191       /* Dedicated Downstream Port DCP */
00192 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
00193       hpcd->BCDCallback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
00194 #else
00195       HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
00196 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
00197     }
00198     else
00199     {
00200       /* Charging Downstream Port CDP */
00201 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
00202       hpcd->BCDCallback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
00203 #else
00204       HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
00205 #endif /* USE_HAL_PCD_REGISTER_CALLBACKS */
00206     }
00207   }
00208   else /* NO */
00209   {
00210     /* Standard Downstream Port */
00211 #if (USE_HAL_PCD_REGISTER_CALLBACKS == 1U)
00212     hpcd->BCDCallback(hpcd,