Hal Drivers for L4

Dependents:   BSP OneHopeOnePrayer FINAL_AUDIO_RECORD AudioDemo

Fork of STM32L4xx_HAL_Driver by Senior Design: Sound Monitor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32l4xx_hal_i2c_ex.c Source File

stm32l4xx_hal_i2c_ex.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4xx_hal_i2c_ex.c
00004   * @author  MCD Application Team
00005   * @version V1.1.0
00006   * @date    16-September-2015
00007   * @brief   I2C Extended HAL module driver.
00008   *          This file provides firmware functions to manage the following 
00009   *          functionalities of I2C Extended peripheral:
00010   *           + Extended features functions
00011   *         
00012   @verbatim
00013   ==============================================================================
00014                ##### I2C peripheral Extended features  #####
00015   ==============================================================================
00016            
00017   [..] Comparing to other previous devices, the I2C interface for STM32L4xx
00018        devices contains the following additional features
00019        
00020        (+) Possibility to disable or enable Analog Noise Filter
00021        (+) Use of a configured Digital Noise Filter
00022        (+) Disable or enable wakeup from Stop modes
00023    
00024                      ##### How to use this driver #####
00025   ==============================================================================
00026   [..] This driver provides functions to configure Noise Filter
00027     (#) Configure I2C Analog noise filter using the function HAL_I2CEx_ConfigAnalogFilter()
00028     (#) Configure I2C Digital noise filter using the function HAL_I2CEx_ConfigDigitalFilter()
00029     (#) Configure the enable or disable of I2C Wake Up Mode using the functions :
00030           (++) HAL_I2CEx_EnableWakeUp()
00031           (++) HAL_I2CEx_DisableWakeUp()
00032     (#) Configure the enable or disable of fast mode plus driving capability using the functions :
00033           (++) HAL_I2CEx_EnableFastModePlus()
00034           (++) HAL_I2CEx_DisbleFastModePlus()
00035   @endverbatim
00036   ******************************************************************************
00037   * @attention
00038   *
00039   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00040   *
00041   * Redistribution and use in source and binary forms, with or without modification,
00042   * are permitted provided that the following conditions are met:
00043   *   1. Redistributions of source code must retain the above copyright notice,
00044   *      this list of conditions and the following disclaimer.
00045   *   2. Redistributions in binary form must reproduce the above copyright notice,
00046   *      this list of conditions and the following disclaimer in the documentation
00047   *      and/or other materials provided with the distribution.
00048   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00049   *      may be used to endorse or promote products derived from this software
00050   *      without specific prior written permission.
00051   *
00052   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00053   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00054   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00055   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00056   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00057   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00058   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00059   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00060   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00061   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00062   *
00063   ******************************************************************************  
00064   */ 
00065 
00066 /* Includes ------------------------------------------------------------------*/
00067 #include "stm32l4xx_hal.h"
00068 
00069 /** @addtogroup STM32L4xx_HAL_Driver
00070   * @{
00071   */
00072 
00073   
00074 
00075 /** @defgroup I2CEx I2CEx
00076   * @brief I2C Extended HAL module driver
00077   * @{
00078   */
00079 
00080 #ifdef HAL_I2C_MODULE_ENABLED
00081 
00082 /* Private typedef -----------------------------------------------------------*/
00083 /* Private define ------------------------------------------------------------*/
00084 /* Private macro -------------------------------------------------------------*/
00085 /* Private variables ---------------------------------------------------------*/
00086 /* Private function prototypes -----------------------------------------------*/
00087 /* Private functions ---------------------------------------------------------*/
00088 
00089 /** @defgroup I2CEx_Exported_Functions I2C Extended Exported Functions
00090   * @{
00091   */
00092 
00093 /** @defgroup I2CEx_Exported_Functions_Group1 Extended features functions
00094   * @brief    Extended features functions
00095  *
00096 @verbatim   
00097  ===============================================================================
00098                       ##### Extended features functions #####
00099  ===============================================================================  
00100     [..] This section provides functions allowing to:
00101       (+) Configure Noise Filters 
00102 
00103 @endverbatim
00104   * @{
00105   */
00106   
00107 /**
00108   * @brief  Configures I2C Analog noise filter. 
00109   * @param  hi2c : pointer to a I2C_HandleTypeDef structure that contains
00110   *                the configuration information for the specified I2Cx peripheral.
00111   * @param  AnalogFilter : new state of the Analog filter.
00112   * @retval HAL status
00113   */
00114     
00115 HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
00116 {
00117   /* Check the parameters */
00118   #define  I2C_CR1_DNF                         ((uint32_t)0x00000F00)        
00119   
00120   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
00121   assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
00122   
00123   if((hi2c->State == HAL_I2C_STATE_BUSY) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_RX)
00124      || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_RX))
00125   {
00126     return HAL_BUSY;
00127   }
00128   
00129   /* Process Locked */
00130   __HAL_LOCK(hi2c);
00131 
00132   hi2c->State = HAL_I2C_STATE_BUSY;
00133   
00134   /* Disable the selected I2C peripheral */
00135   __HAL_I2C_DISABLE(hi2c);    
00136   
00137   /* Reset I2Cx ANOFF bit */
00138   hi2c->Instance->CR1 &= ~(I2C_CR1_ANFOFF);    
00139   
00140   /* Set analog filter bit*/
00141   hi2c->Instance->CR1 |= AnalogFilter;
00142   
00143   __HAL_I2C_ENABLE(hi2c); 
00144   
00145   hi2c->State = HAL_I2C_STATE_READY;
00146   
00147   /* Process Unlocked */
00148   __HAL_UNLOCK(hi2c);
00149 
00150   return HAL_OK; 
00151 }
00152 
00153 /**
00154   * @brief  Configures I2C Digital noise filter. 
00155   * @param  hi2c : pointer to a I2C_HandleTypeDef structure that contains
00156   *                the configuration information for the specified I2Cx peripheral.
00157   * @param  DigitalFilter : Coefficient of digital noise filter between 0x00 and 0x0F.
00158   * @retval HAL status
00159   */
00160 HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
00161 {
00162   uint32_t tmpreg = 0;
00163   
00164   /* Check the parameters */
00165   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
00166   assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
00167   
00168   if((hi2c->State == HAL_I2C_STATE_BUSY) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_RX)
00169      || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_RX))
00170   {
00171     return HAL_BUSY;
00172   }
00173   
00174   /* Process Locked */
00175   __HAL_LOCK(hi2c);
00176 
00177   hi2c->State = HAL_I2C_STATE_BUSY;
00178   
00179   /* Disable the selected I2C peripheral */
00180   __HAL_I2C_DISABLE(hi2c);  
00181   
00182   /* Get the old register value */
00183   tmpreg = hi2c->Instance->CR1;
00184   
00185   /* Reset I2Cx DNF bits [11:8] */
00186   tmpreg &= ~(I2C_CR1_DNF);
00187   
00188   /* Set I2Cx DNF coefficient */
00189   tmpreg |= DigitalFilter << 8;
00190   
00191   /* Store the new register value */
00192   hi2c->Instance->CR1 = tmpreg;
00193   
00194   __HAL_I2C_ENABLE(hi2c); 
00195   
00196   hi2c->State = HAL_I2C_STATE_READY;
00197   
00198   /* Process Unlocked */
00199   __HAL_UNLOCK(hi2c);
00200 
00201   return HAL_OK; 
00202 }  
00203 
00204 /**
00205   * @brief  Enables I2C wakeup from stop mode.
00206   * @param  hi2c : pointer to a I2C_HandleTypeDef structure that contains
00207   *                the configuration information for the specified I2Cx peripheral.
00208   * @retval HAL status
00209   */
00210 HAL_StatusTypeDef HAL_I2CEx_EnableWakeUp (I2C_HandleTypeDef *hi2c)
00211 {
00212   /* Check the parameters */
00213   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
00214   
00215   if((hi2c->State == HAL_I2C_STATE_BUSY) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_RX)
00216      || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_RX))
00217   {
00218     return HAL_BUSY;
00219   }
00220   
00221   /* Process Locked */
00222   __HAL_LOCK(hi2c);
00223 
00224   hi2c->State = HAL_I2C_STATE_BUSY;
00225   
00226   /* Disable the selected I2C peripheral */
00227   __HAL_I2C_DISABLE(hi2c);  
00228   
00229   /* Enable wakeup from stop mode */
00230   hi2c->Instance->CR1 |= I2C_CR1_WUPEN;   
00231   
00232   __HAL_I2C_ENABLE(hi2c); 
00233   
00234   hi2c->State = HAL_I2C_STATE_READY;
00235   
00236   /* Process Unlocked */
00237   __HAL_UNLOCK(hi2c);
00238 
00239   return HAL_OK; 
00240 }  
00241 
00242 
00243 /**
00244   * @brief  Disables I2C wakeup from stop mode.
00245   * @param  hi2c : pointer to a I2C_HandleTypeDef structure that contains
00246   *                the configuration information for the specified I2Cx peripheral.
00247   * @retval HAL status
00248   */
00249 HAL_StatusTypeDef HAL_I2CEx_DisableWakeUp (I2C_HandleTypeDef *hi2c)
00250 {
00251   /* Check the parameters */
00252   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
00253   
00254   if((hi2c->State == HAL_I2C_STATE_BUSY) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_MASTER_BUSY_RX)
00255      || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_TX) || (hi2c->State == HAL_I2C_STATE_SLAVE_BUSY_RX))
00256   {
00257     return HAL_BUSY;
00258   }
00259   
00260   /* Process Locked */
00261   __HAL_LOCK(hi2c);
00262 
00263   hi2c->State = HAL_I2C_STATE_BUSY;
00264   
00265   /* Disable the selected I2C peripheral */
00266   __HAL_I2C_DISABLE(hi2c);  
00267   
00268   /* Enable wakeup from stop mode */
00269   hi2c->Instance->CR1 &= ~(I2C_CR1_WUPEN);   
00270   
00271   __HAL_I2C_ENABLE(hi2c); 
00272   
00273   hi2c->State = HAL_I2C_STATE_READY;
00274   
00275   /* Process Unlocked */
00276   __HAL_UNLOCK(hi2c);
00277 
00278   return HAL_OK; 
00279 }  
00280 
00281 /**
00282   * @brief Enable the I2C fast mode plus driving capability.
00283   * @param ConfigFastModePlus: selects the pin.
00284   *   This parameter can be one of the @ref I2CEx_FastModePlus values
00285   * @note  For I2C1, fast mode plus driving capability can be enabled on all selected
00286   *        I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
00287   *        on each one of the following pins PB6, PB7, PB8 and PB9.
00288   * @note  For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
00289   *        can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
00290   * @note  For all I2C2 pins fast mode plus driving capability can be enabled
00291   *        only by using I2C_FASTMODEPLUS_I2C2 parameter.
00292   * @note  For all I2C3 pins fast mode plus driving capability can be enabled
00293   *        only by using I2C_FASTMODEPLUS_I2C3 parameter.
00294   * @retval None
00295   */
00296 void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
00297 {
00298   /* Check the parameter */
00299   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
00300   
00301   /* Enable SYSCFG clock */
00302   __HAL_RCC_SYSCFG_CLK_ENABLE();
00303   
00304   /* Enable fast mode plus driving capability for selected pin */
00305   SET_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
00306 }
00307 
00308 /**
00309   * @brief Disable the I2C fast mode plus driving capability.
00310   * @param ConfigFastModePlus: selects the pin.
00311   *   This parameter can be one of the @ref I2CEx_FastModePlus values
00312   * @note  For I2C1, fast mode plus driving capability can be disabled on all selected
00313   *        I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
00314   *        on each one of the following pins PB6, PB7, PB8 and PB9.
00315   * @note  For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
00316   *        can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
00317   * @note  For all I2C2 pins fast mode plus driving capability can be disabled
00318   *        only by using I2C_FASTMODEPLUS_I2C2 parameter.
00319   * @note  For all I2C3 pins fast mode plus driving capability can be disabled
00320   *        only by using I2C_FASTMODEPLUS_I2C3 parameter.
00321   * @retval None
00322   */
00323 void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
00324 {
00325   /* Check the parameter */
00326   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
00327   
00328   /* Enable SYSCFG clock */
00329   __HAL_RCC_SYSCFG_CLK_ENABLE();
00330 
00331   /* Disable fast mode plus driving capability for selected pin */
00332   CLEAR_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
00333 }
00334 
00335 /**
00336   * @}
00337   */  
00338 
00339 /**
00340   * @}
00341   */  
00342 
00343 #endif /* HAL_I2C_MODULE_ENABLED */
00344 /**
00345   * @}
00346   */
00347 
00348 /**
00349   * @}
00350   */
00351 
00352 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00353