Back to home page

LXR

 
 

    


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

0001 /**
0002   ******************************************************************************
0003   * @file    stm32h7xx_hal_i2c_ex.c
0004   * @author  MCD Application Team
0005   * @brief   I2C Extended HAL module driver.
0006   *          This file provides firmware functions to manage the following
0007   *          functionalities of I2C Extended peripheral:
0008   *           + Filter Mode Functions
0009   *           + WakeUp Mode Functions
0010   *           + FastModePlus Functions
0011   *
0012   ******************************************************************************
0013   * @attention
0014   *
0015   * Copyright (c) 2017 STMicroelectronics.
0016   * All rights reserved.
0017   *
0018   * This software is licensed under terms that can be found in the LICENSE file
0019   * in the root directory of this software component.
0020   * If no LICENSE file comes with this software, it is provided AS-IS.
0021   *
0022   ******************************************************************************
0023   @verbatim
0024   ==============================================================================
0025                ##### I2C peripheral Extended features  #####
0026   ==============================================================================
0027 
0028   [..] Comparing to other previous devices, the I2C interface for STM32H7xx
0029        devices contains the following additional features
0030 
0031        (+) Possibility to disable or enable Analog Noise Filter
0032        (+) Use of a configured Digital Noise Filter
0033        (+) Disable or enable wakeup from Stop mode(s)
0034        (+) Disable or enable Fast Mode Plus
0035 
0036                      ##### How to use this driver #####
0037   ==============================================================================
0038   [..] This driver provides functions to configure Noise Filter and Wake Up Feature
0039     (#) Configure I2C Analog noise filter using the function HAL_I2CEx_ConfigAnalogFilter()
0040     (#) Configure I2C Digital noise filter using the function HAL_I2CEx_ConfigDigitalFilter()
0041     (#) Configure the enable or disable of I2C Wake Up Mode using the functions :
0042           (++) HAL_I2CEx_EnableWakeUp()
0043           (++) HAL_I2CEx_DisableWakeUp()
0044     (#) Configure the enable or disable of fast mode plus driving capability using the functions :
0045           (++) HAL_I2CEx_EnableFastModePlus()
0046           (++) HAL_I2CEx_DisableFastModePlus()
0047   @endverbatim
0048   */
0049 
0050 /* Includes ------------------------------------------------------------------*/
0051 #include "stm32h7xx_hal.h"
0052 
0053 /** @addtogroup STM32H7xx_HAL_Driver
0054   * @{
0055   */
0056 
0057 /** @defgroup I2CEx I2CEx
0058   * @ingroup RTEMSBSPsARMSTM32H7
0059   * @brief I2C Extended HAL module driver
0060   * @{
0061   */
0062 
0063 #ifdef HAL_I2C_MODULE_ENABLED
0064 
0065 /* Private typedef -----------------------------------------------------------*/
0066 /* Private define ------------------------------------------------------------*/
0067 /* Private macro -------------------------------------------------------------*/
0068 /* Private variables ---------------------------------------------------------*/
0069 /* Private function prototypes -----------------------------------------------*/
0070 /* Private functions ---------------------------------------------------------*/
0071 
0072 /** @defgroup I2CEx_Exported_Functions I2C Extended Exported Functions
0073   * @ingroup RTEMSBSPsARMSTM32H7
0074   * @{
0075   */
0076 
0077 /** @defgroup I2CEx_Exported_Functions_Group1 Filter Mode Functions
0078   * @ingroup RTEMSBSPsARMSTM32H7
0079   * @brief    Filter Mode Functions
0080   *
0081 @verbatim
0082  ===============================================================================
0083                       ##### Filter Mode Functions #####
0084  ===============================================================================
0085     [..] This section provides functions allowing to:
0086       (+) Configure Noise Filters
0087 
0088 @endverbatim
0089   * @{
0090   */
0091 
0092 /**
0093   * @brief  Configure I2C Analog noise filter.
0094   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
0095   *                the configuration information for the specified I2Cx peripheral.
0096   * @param  AnalogFilter New state of the Analog filter.
0097   * @retval HAL status
0098   */
0099 HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
0100 {
0101   /* Check the parameters */
0102   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
0103   assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
0104 
0105   if (hi2c->State == HAL_I2C_STATE_READY)
0106   {
0107     /* Process Locked */
0108     __HAL_LOCK(hi2c);
0109 
0110     hi2c->State = HAL_I2C_STATE_BUSY;
0111 
0112     /* Disable the selected I2C peripheral */
0113     __HAL_I2C_DISABLE(hi2c);
0114 
0115     /* Reset I2Cx ANOFF bit */
0116     hi2c->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
0117 
0118     /* Set analog filter bit*/
0119     hi2c->Instance->CR1 |= AnalogFilter;
0120 
0121     __HAL_I2C_ENABLE(hi2c);
0122 
0123     hi2c->State = HAL_I2C_STATE_READY;
0124 
0125     /* Process Unlocked */
0126     __HAL_UNLOCK(hi2c);
0127 
0128     return HAL_OK;
0129   }
0130   else
0131   {
0132     return HAL_BUSY;
0133   }
0134 }
0135 
0136 /**
0137   * @brief  Configure I2C Digital noise filter.
0138   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
0139   *                the configuration information for the specified I2Cx peripheral.
0140   * @param  DigitalFilter Coefficient of digital noise filter between Min_Data=0x00 and Max_Data=0x0F.
0141   * @retval HAL status
0142   */
0143 HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
0144 {
0145   uint32_t tmpreg;
0146 
0147   /* Check the parameters */
0148   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
0149   assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
0150 
0151   if (hi2c->State == HAL_I2C_STATE_READY)
0152   {
0153     /* Process Locked */
0154     __HAL_LOCK(hi2c);
0155 
0156     hi2c->State = HAL_I2C_STATE_BUSY;
0157 
0158     /* Disable the selected I2C peripheral */
0159     __HAL_I2C_DISABLE(hi2c);
0160 
0161     /* Get the old register value */
0162     tmpreg = hi2c->Instance->CR1;
0163 
0164     /* Reset I2Cx DNF bits [11:8] */
0165     tmpreg &= ~(I2C_CR1_DNF);
0166 
0167     /* Set I2Cx DNF coefficient */
0168     tmpreg |= DigitalFilter << 8U;
0169 
0170     /* Store the new register value */
0171     hi2c->Instance->CR1 = tmpreg;
0172 
0173     __HAL_I2C_ENABLE(hi2c);
0174 
0175     hi2c->State = HAL_I2C_STATE_READY;
0176 
0177     /* Process Unlocked */
0178     __HAL_UNLOCK(hi2c);
0179 
0180     return HAL_OK;
0181   }
0182   else
0183   {
0184     return HAL_BUSY;
0185   }
0186 }
0187 /**
0188   * @}
0189   */
0190 
0191 /** @defgroup I2CEx_Exported_Functions_Group2 WakeUp Mode Functions
0192   * @ingroup RTEMSBSPsARMSTM32H7
0193   * @brief    WakeUp Mode Functions
0194   *
0195 @verbatim
0196  ===============================================================================
0197                       ##### WakeUp Mode Functions #####
0198  ===============================================================================
0199     [..] This section provides functions allowing to:
0200       (+) Configure Wake Up Feature
0201 
0202 @endverbatim
0203   * @{
0204   */
0205 
0206 /**
0207   * @brief  Enable I2C wakeup from Stop mode(s).
0208   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
0209   *                the configuration information for the specified I2Cx peripheral.
0210   * @retval HAL status
0211   */
0212 HAL_StatusTypeDef HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef *hi2c)
0213 {
0214   /* Check the parameters */
0215   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
0216 
0217   if (hi2c->State == HAL_I2C_STATE_READY)
0218   {
0219     /* Process Locked */
0220     __HAL_LOCK(hi2c);
0221 
0222     hi2c->State = HAL_I2C_STATE_BUSY;
0223 
0224     /* Disable the selected I2C peripheral */
0225     __HAL_I2C_DISABLE(hi2c);
0226 
0227     /* Enable wakeup from stop mode */
0228     hi2c->Instance->CR1 |= I2C_CR1_WUPEN;
0229 
0230     __HAL_I2C_ENABLE(hi2c);
0231 
0232     hi2c->State = HAL_I2C_STATE_READY;
0233 
0234     /* Process Unlocked */
0235     __HAL_UNLOCK(hi2c);
0236 
0237     return HAL_OK;
0238   }
0239   else
0240   {
0241     return HAL_BUSY;
0242   }
0243 }
0244 
0245 /**
0246   * @brief  Disable I2C wakeup from Stop mode(s).
0247   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
0248   *                the configuration information for the specified I2Cx peripheral.
0249   * @retval HAL status
0250   */
0251 HAL_StatusTypeDef HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef *hi2c)
0252 {
0253   /* Check the parameters */
0254   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
0255 
0256   if (hi2c->State == HAL_I2C_STATE_READY)
0257   {
0258     /* Process Locked */
0259     __HAL_LOCK(hi2c);
0260 
0261     hi2c->State = HAL_I2C_STATE_BUSY;
0262 
0263     /* Disable the selected I2C peripheral */
0264     __HAL_I2C_DISABLE(hi2c);
0265 
0266     /* Enable wakeup from stop mode */
0267     hi2c->Instance->CR1 &= ~(I2C_CR1_WUPEN);
0268 
0269     __HAL_I2C_ENABLE(hi2c);
0270 
0271     hi2c->State = HAL_I2C_STATE_READY;
0272 
0273     /* Process Unlocked */
0274     __HAL_UNLOCK(hi2c);
0275 
0276     return HAL_OK;
0277   }
0278   else
0279   {
0280     return HAL_BUSY;
0281   }
0282 }
0283 /**
0284   * @}
0285   */
0286 
0287 /** @defgroup I2CEx_Exported_Functions_Group3 Fast Mode Plus Functions
0288   * @ingroup RTEMSBSPsARMSTM32H7
0289   * @brief    Fast Mode Plus Functions
0290   *
0291 @verbatim
0292  ===============================================================================
0293                       ##### Fast Mode Plus Functions #####
0294  ===============================================================================
0295     [..] This section provides functions allowing to:
0296       (+) Configure Fast Mode Plus
0297 
0298 @endverbatim
0299   * @{
0300   */
0301 
0302 /**
0303   * @brief Enable the I2C fast mode plus driving capability.
0304   * @param ConfigFastModePlus Selects the pin.
0305   *   This parameter can be one of the @ref I2CEx_FastModePlus values
0306   * @note  For I2C1, fast mode plus driving capability can be enabled on all selected
0307   *        I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
0308   *        on each one of the following pins PB6, PB7, PB8 and PB9.
0309   * @note  For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
0310   *        can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
0311   * @note  For all I2C2 pins fast mode plus driving capability can be enabled
0312   *        only by using I2C_FASTMODEPLUS_I2C2 parameter.
0313   * @note  For all I2C3 pins fast mode plus driving capability can be enabled
0314   *        only by using I2C_FASTMODEPLUS_I2C3 parameter.
0315   * @note  For all I2C4 pins fast mode plus driving capability can be enabled
0316   *        only by using I2C_FASTMODEPLUS_I2C4 parameter.
0317   * @note  For all I2C5 pins fast mode plus driving capability can be enabled
0318   *        only by using I2C_FASTMODEPLUS_I2C5 parameter.
0319   * @retval None
0320   */
0321 void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
0322 {
0323   /* Check the parameter */
0324   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
0325 
0326   /* Enable SYSCFG clock */
0327   __HAL_RCC_SYSCFG_CLK_ENABLE();
0328 
0329   /* Enable fast mode plus driving capability for selected pin */
0330   SET_BIT(SYSCFG->PMCR, (uint32_t)ConfigFastModePlus);
0331 }
0332 
0333 /**
0334   * @brief Disable the I2C fast mode plus driving capability.
0335   * @param ConfigFastModePlus Selects the pin.
0336   *   This parameter can be one of the @ref I2CEx_FastModePlus values
0337   * @note  For I2C1, fast mode plus driving capability can be disabled on all selected
0338   *        I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
0339   *        on each one of the following pins PB6, PB7, PB8 and PB9.
0340   * @note  For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
0341   *        can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
0342   * @note  For all I2C2 pins fast mode plus driving capability can be disabled
0343   *        only by using I2C_FASTMODEPLUS_I2C2 parameter.
0344   * @note  For all I2C3 pins fast mode plus driving capability can be disabled
0345   *        only by using I2C_FASTMODEPLUS_I2C3 parameter.
0346   * @note  For all I2C4 pins fast mode plus driving capability can be disabled
0347   *        only by using I2C_FASTMODEPLUS_I2C4 parameter.
0348   * @note  For all I2C5 pins fast mode plus driving capability can be disabled
0349   *        only by using I2C_FASTMODEPLUS_I2C5 parameter.
0350   * @retval None
0351   */
0352 void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
0353 {
0354   /* Check the parameter */
0355   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
0356 
0357   /* Enable SYSCFG clock */
0358   __HAL_RCC_SYSCFG_CLK_ENABLE();
0359 
0360   /* Disable fast mode plus driving capability for selected pin */
0361   CLEAR_BIT(SYSCFG->PMCR, (uint32_t)ConfigFastModePlus);
0362 }
0363 /**
0364   * @}
0365   */
0366 /**
0367   * @}
0368   */
0369 
0370 #endif /* HAL_I2C_MODULE_ENABLED */
0371 /**
0372   * @}
0373   */
0374 
0375 /**
0376   * @}
0377   */