STM32F439xx HAL User Manual
stm32f4xx_hal_pcd_ex.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_hal_pcd_ex.c
00004   * @author  MCD Application Team
00005   * @brief   PCD 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   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00014   *
00015   * Redistribution and use in source and binary forms, with or without modification,
00016   * are permitted provided that the following conditions are met:
00017   *   1. Redistributions of source code must retain the above copyright notice,
00018   *      this list of conditions and the following disclaimer.
00019   *   2. Redistributions in binary form must reproduce the above copyright notice,
00020   *      this list of conditions and the following disclaimer in the documentation
00021   *      and/or other materials provided with the distribution.
00022   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00023   *      may be used to endorse or promote products derived from this software
00024   *      without specific prior written permission.
00025   *
00026   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00027   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00029   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00030   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00031   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00032   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00033   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00034   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00035   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036   *
00037   ******************************************************************************
00038   */ 
00039 
00040 /* Includes ------------------------------------------------------------------*/
00041 #include "stm32f4xx_hal.h"
00042 
00043 /** @addtogroup STM32F4xx_HAL_Driver
00044   * @{
00045   */
00046 
00047 /** @defgroup PCDEx PCDEx
00048   * @brief PCD Extended HAL module driver
00049   * @{
00050   */
00051 #ifdef HAL_PCD_MODULE_ENABLED
00052 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || \
00053     defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
00054     defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE) || defined(STM32F446xx) || \
00055     defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
00056     defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
00057 /* Private types -------------------------------------------------------------*/
00058 /* Private variables ---------------------------------------------------------*/
00059 /* Private constants ---------------------------------------------------------*/
00060 /* Private macros ------------------------------------------------------------*/
00061 /* Private functions ---------------------------------------------------------*/
00062 /* Exported functions --------------------------------------------------------*/
00063 
00064 /** @defgroup PCDEx_Exported_Functions PCD Extended Exported Functions
00065   * @{
00066   */
00067 
00068 /** @defgroup PCDEx_Exported_Functions_Group1 Peripheral Control functions
00069   * @brief    PCDEx control functions 
00070   *
00071 @verbatim  
00072  ===============================================================================
00073                  ##### Extended features functions #####
00074  ===============================================================================  
00075     [..]  This section provides functions allowing to:
00076       (+) Update FIFO configuration
00077 
00078 @endverbatim
00079   * @{
00080   */
00081 
00082 /**
00083   * @brief  Set Tx FIFO
00084   * @param  hpcd PCD handle
00085   * @param  fifo The number of Tx fifo
00086   * @param  size Fifo size
00087   * @retval HAL status
00088   */
00089 HAL_StatusTypeDef HAL_PCDEx_SetTxFiFo(PCD_HandleTypeDef *hpcd, uint8_t fifo, uint16_t size)
00090 {
00091   uint8_t i = 0;
00092   uint32_t Tx_Offset = 0U;
00093 
00094   /*  TXn min size = 16 words. (n  : Transmit FIFO index)
00095       When a TxFIFO is not used, the Configuration should be as follows: 
00096           case 1 :  n > m    and Txn is not used    (n,m  : Transmit FIFO indexes)
00097          --> Txm can use the space allocated for Txn.
00098          case2  :  n < m    and Txn is not used    (n,m  : Transmit FIFO indexes)
00099          --> Txn should be configured with the minimum space of 16 words
00100      The FIFO is used optimally when used TxFIFOs are allocated in the top 
00101          of the FIFO.Ex: use EP1 and EP2 as IN instead of EP1 and EP3 as IN ones.
00102      When DMA is used 3n * FIFO locations should be reserved for internal DMA registers */
00103   
00104   Tx_Offset = hpcd->Instance->GRXFSIZ;
00105   
00106   if(fifo == 0)
00107   {
00108     hpcd->Instance->DIEPTXF0_HNPTXFSIZ = (uint32_t)(((uint32_t)size << 16U) | Tx_Offset);
00109   }
00110   else
00111   {
00112     Tx_Offset += (hpcd->Instance->DIEPTXF0_HNPTXFSIZ) >> 16U;
00113     for (i = 0; i < (fifo - 1); i++)
00114     {
00115       Tx_Offset += (hpcd->Instance->DIEPTXF[i] >> 16U);
00116     }
00117     
00118     /* Multiply Tx_Size by 2 to get higher performance */
00119     hpcd->Instance->DIEPTXF[fifo - 1] = (uint32_t)(((uint32_t)size << 16U) | Tx_Offset);        
00120   }
00121   
00122   return HAL_OK;
00123 }
00124 
00125 /**
00126   * @brief  Set Rx FIFO
00127   * @param  hpcd PCD handle
00128   * @param  size Size of Rx fifo
00129   * @retval HAL status
00130   */
00131 HAL_StatusTypeDef HAL_PCDEx_SetRxFiFo(PCD_HandleTypeDef *hpcd, uint16_t size)
00132 {
00133   hpcd->Instance->GRXFSIZ = size;
00134   
00135   return HAL_OK;
00136 }
00137 
00138 #if defined(STM32F446xx) || defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
00139     defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
00140 /**
00141   * @brief  Activate LPM feature
00142   * @param  hpcd PCD handle
00143   * @retval HAL status
00144   */
00145 HAL_StatusTypeDef HAL_PCDEx_ActivateLPM(PCD_HandleTypeDef *hpcd)
00146 {
00147   USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;  
00148   
00149   hpcd->lpm_active = ENABLE;
00150   hpcd->LPM_State = LPM_L0;
00151   USBx->GINTMSK |= USB_OTG_GINTMSK_LPMINTM;
00152   USBx->GLPMCFG |= (USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
00153   
00154   return HAL_OK;  
00155 }
00156 
00157 /**
00158   * @brief  Deactivate LPM feature.
00159   * @param  hpcd PCD handle
00160   * @retval HAL status
00161   */
00162 HAL_StatusTypeDef HAL_PCDEx_DeActivateLPM(PCD_HandleTypeDef *hpcd)
00163 {
00164   USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;  
00165   
00166   hpcd->lpm_active = DISABLE;
00167   USBx->GINTMSK &= ~USB_OTG_GINTMSK_LPMINTM;
00168   USBx->GLPMCFG &= ~(USB_OTG_GLPMCFG_LPMEN | USB_OTG_GLPMCFG_LPMACK | USB_OTG_GLPMCFG_ENBESL);
00169   
00170   return HAL_OK;  
00171 }
00172 
00173 /**
00174   * @brief  Send LPM message to user layer callback.
00175   * @param  hpcd PCD handle
00176   * @param  msg LPM message
00177   * @retval HAL status
00178   */
00179 __weak void HAL_PCDEx_LPM_Callback(PCD_HandleTypeDef *hpcd, PCD_LPM_MsgTypeDef msg)
00180 {
00181   /* Prevent unused argument(s) compilation warning */
00182   UNUSED(hpcd);
00183   UNUSED(msg);
00184 }
00185 #endif /* STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Rx || STM32F412Vx || STM32F412Cx || STM32F413xx || STM32F423xx  */
00186 
00187 #if defined(STM32F412Zx) || defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
00188 /**
00189   * @brief  HAL_PCDEx_BCD_VBUSDetect : handle BatteryCharging Process
00190   * @param  hpcd PCD handle
00191   * @retval HAL status
00192   */
00193 void HAL_PCDEx_BCD_VBUSDetect(PCD_HandleTypeDef *hpcd)
00194 {
00195   USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;  
00196   uint32_t tickstart = HAL_GetTick();
00197   
00198   /* Start BCD When device is connected */
00199   if (USBx_DEVICE->DCTL & USB_OTG_DCTL_SDIS)
00200   {
00201     /* Enable DCD : Data Contact Detect */
00202     USBx->GCCFG |= USB_OTG_GCCFG_DCDEN;
00203     
00204     /* Wait Detect flag or a timeout is happen*/
00205     while ((USBx->GCCFG & USB_OTG_GCCFG_DCDET) == 0U)
00206     {
00207       /* Check for the Timeout */
00208       if((HAL_GetTick() - tickstart ) > 1000U)
00209       {
00210         HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_ERROR);
00211         return;
00212       }
00213     }
00214     
00215     /* Right response got */
00216     HAL_Delay(100U); 
00217     
00218     /* Check Detect flag*/
00219     if (USBx->GCCFG & USB_OTG_GCCFG_DCDET)
00220     {
00221       HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CONTACT_DETECTION);
00222     }
00223     
00224     /*Primary detection: checks if connected to Standard Downstream Port  
00225     (without charging capability) */
00226     USBx->GCCFG &=~ USB_OTG_GCCFG_DCDEN;
00227     USBx->GCCFG |=  USB_OTG_GCCFG_PDEN;
00228     HAL_Delay(100U); 
00229     
00230     if (!(USBx->GCCFG & USB_OTG_GCCFG_PDET))
00231     {
00232       /* Case of Standard Downstream Port */
00233       HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_STD_DOWNSTREAM_PORT);
00234     }
00235     else  
00236     {
00237       /* start secondary detection to check connection to Charging Downstream 
00238       Port or Dedicated Charging Port */
00239       USBx->GCCFG &=~ USB_OTG_GCCFG_PDEN;
00240       USBx->GCCFG |=  USB_OTG_GCCFG_SDEN;
00241       HAL_Delay(100U); 
00242       
00243       if ((USBx->GCCFG) & USB_OTG_GCCFG_SDET)
00244       { 
00245         /* case Dedicated Charging Port  */
00246         HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DEDICATED_CHARGING_PORT);
00247       }
00248       else
00249       {
00250         /* case Charging Downstream Port  */
00251         HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_CHARGING_DOWNSTREAM_PORT);
00252       }
00253     }
00254     /* Battery Charging capability discovery finished */
00255     HAL_PCDEx_BCD_Callback(hpcd, PCD_BCD_DISCOVERY_COMPLETED);
00256   }
00257 }
00258 
00259 /**
00260   * @brief  HAL_PCDEx_ActivateBCD : active BatteryCharging feature
00261   * @param  hpcd PCD handle
00262   * @retval HAL status
00263   */
00264 HAL_StatusTypeDef HAL_PCDEx_ActivateBCD(PCD_HandleTypeDef *hpcd)
00265 {
00266   USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;  
00267 
00268   hpcd->battery_charging_active = ENABLE; 
00269   USBx->GCCFG |= (USB_OTG_GCCFG_BCDEN);
00270   
00271   return HAL_OK;  
00272 }
00273 
00274 /**
00275   * @brief  HAL_PCDEx_DeActivateBCD : de-active BatteryCharging feature
00276   * @param  hpcd PCD handle
00277   * @retval HAL status
00278   */
00279 HAL_StatusTypeDef HAL_PCDEx_DeActivateBCD(PCD_HandleTypeDef *hpcd)
00280 {
00281   USB_OTG_GlobalTypeDef *USBx = hpcd->Instance;  
00282   hpcd->battery_charging_active = DISABLE; 
00283   USBx->GCCFG &= ~(USB_OTG_GCCFG_BCDEN);
00284   return HAL_OK;  
00285 }
00286 
00287 /**
00288   * @brief  HAL_PCDEx_BatteryCharging_Callback : Send BatteryCharging message to user layer
00289   * @param  hpcd PCD handle
00290   * @param  msg LPM message
00291   * @retval HAL status
00292   */
00293 __weak void HAL_PCDEx_BCD_Callback(PCD_HandleTypeDef *hpcd, PCD_BCD_MsgTypeDef msg)
00294 {
00295   /* Prevent unused argument(s) compilation warning */
00296   UNUSED(hpcd);
00297   UNUSED(msg);
00298 }
00299 
00300 #endif /* STM32F412Zx || STM32F412Rx || STM32F412Vx || STM32F412Cx || STM32F413xx || STM32F423xx */
00301 
00302 /**
00303   * @}
00304   */
00305 
00306 /**
00307   * @}
00308   */
00309 
00310 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||
00311           STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Rx ||
00312           STM32F412Vx || STM32F412Cx || STM32F413xx || STM32F423xx  */
00313 #endif /* HAL_PCD_MODULE_ENABLED */
00314 /**
00315   * @}
00316   */
00317 
00318 /**
00319   * @}
00320   */
00321 
00322 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/