1 /**
2   ******************************************************************************
3   * @file    stm32f7xx_hal_i2c_ex.c
4   * @author  MCD Application Team
5   * @brief   I2C Extended HAL module driver.
6   *          This file provides firmware functions to manage the following
7   *          functionalities of I2C Extended peripheral:
8   *           + Filter Mode Functions
9   *           + FastModePlus Functions
10   *
11   ******************************************************************************
12   * @attention
13   *
14   * Copyright (c) 2017 STMicroelectronics.
15   * All rights reserved.
16   *
17   * This software is licensed under terms that can be found in the LICENSE file
18   * in the root directory of this software component.
19   * If no LICENSE file comes with this software, it is provided AS-IS.
20   *
21   ******************************************************************************
22   @verbatim
23   ==============================================================================
24                ##### I2C peripheral Extended features  #####
25   ==============================================================================
26 
27   [..] Comparing to other previous devices, the I2C interface for STM32F7xx
28        devices contains the following additional features
29 
30        (+) Possibility to disable or enable Analog Noise Filter
31        (+) Use of a configured Digital Noise Filter
32        (+) Disable or enable Fast Mode Plus
33 
34                      ##### How to use this driver #####
35   ==============================================================================
36   [..] This driver provides functions to:
37     (#) Configure I2C Analog noise filter using the function HAL_I2CEx_ConfigAnalogFilter()
38     (#) Configure I2C Digital noise filter using the function HAL_I2CEx_ConfigDigitalFilter()
39     (#) Configure the enable or disable of fast mode plus driving capability using the functions :
40           (++) HAL_I2CEx_EnableFastModePlus()
41           (++) HAL_I2CEx_DisableFastModePlus()
42   @endverbatim
43   */
44 
45 /* Includes ------------------------------------------------------------------*/
46 #include "stm32f7xx_hal.h"
47 
48 /** @addtogroup STM32F7xx_HAL_Driver
49   * @{
50   */
51 
52 /** @defgroup I2CEx I2CEx
53   * @brief I2C Extended HAL module driver
54   * @{
55   */
56 
57 #ifdef HAL_I2C_MODULE_ENABLED
58 
59 /* Private typedef -----------------------------------------------------------*/
60 /* Private define ------------------------------------------------------------*/
61 /* Private macro -------------------------------------------------------------*/
62 /* Private variables ---------------------------------------------------------*/
63 /* Private function prototypes -----------------------------------------------*/
64 /* Private functions ---------------------------------------------------------*/
65 
66 /** @defgroup I2CEx_Exported_Functions I2C Extended Exported Functions
67   * @{
68   */
69 
70 /** @defgroup I2CEx_Exported_Functions_Group1 Filter Mode Functions
71   * @brief    Filter Mode Functions
72   *
73 @verbatim
74  ===============================================================================
75                       ##### Filter Mode Functions #####
76  ===============================================================================
77     [..] This section provides functions allowing to:
78       (+) Configure Noise Filters
79 
80 @endverbatim
81   * @{
82   */
83 
84 /**
85   * @brief  Configure I2C Analog noise filter.
86   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
87   *                the configuration information for the specified I2Cx peripheral.
88   * @param  AnalogFilter New state of the Analog filter.
89   * @retval HAL status
90   */
HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef * hi2c,uint32_t AnalogFilter)91 HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
92 {
93   /* Check the parameters */
94   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
95   assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
96 
97   if (hi2c->State == HAL_I2C_STATE_READY)
98   {
99     /* Process Locked */
100     __HAL_LOCK(hi2c);
101 
102     hi2c->State = HAL_I2C_STATE_BUSY;
103 
104     /* Disable the selected I2C peripheral */
105     __HAL_I2C_DISABLE(hi2c);
106 
107     /* Reset I2Cx ANOFF bit */
108     hi2c->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
109 
110     /* Set analog filter bit*/
111     hi2c->Instance->CR1 |= AnalogFilter;
112 
113     __HAL_I2C_ENABLE(hi2c);
114 
115     hi2c->State = HAL_I2C_STATE_READY;
116 
117     /* Process Unlocked */
118     __HAL_UNLOCK(hi2c);
119 
120     return HAL_OK;
121   }
122   else
123   {
124     return HAL_BUSY;
125   }
126 }
127 
128 /**
129   * @brief  Configure I2C Digital noise filter.
130   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
131   *                the configuration information for the specified I2Cx peripheral.
132   * @param  DigitalFilter Coefficient of digital noise filter between Min_Data=0x00 and Max_Data=0x0F.
133   * @retval HAL status
134   */
HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef * hi2c,uint32_t DigitalFilter)135 HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
136 {
137   uint32_t tmpreg;
138 
139   /* Check the parameters */
140   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
141   assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
142 
143   if (hi2c->State == HAL_I2C_STATE_READY)
144   {
145     /* Process Locked */
146     __HAL_LOCK(hi2c);
147 
148     hi2c->State = HAL_I2C_STATE_BUSY;
149 
150     /* Disable the selected I2C peripheral */
151     __HAL_I2C_DISABLE(hi2c);
152 
153     /* Get the old register value */
154     tmpreg = hi2c->Instance->CR1;
155 
156     /* Reset I2Cx DNF bits [11:8] */
157     tmpreg &= ~(I2C_CR1_DNF);
158 
159     /* Set I2Cx DNF coefficient */
160     tmpreg |= DigitalFilter << 8U;
161 
162     /* Store the new register value */
163     hi2c->Instance->CR1 = tmpreg;
164 
165     __HAL_I2C_ENABLE(hi2c);
166 
167     hi2c->State = HAL_I2C_STATE_READY;
168 
169     /* Process Unlocked */
170     __HAL_UNLOCK(hi2c);
171 
172     return HAL_OK;
173   }
174   else
175   {
176     return HAL_BUSY;
177   }
178 }
179 /**
180   * @}
181   */
182 #if  (defined(SYSCFG_PMC_I2C_PB6_FMP) || defined(SYSCFG_PMC_I2C_PB7_FMP)) || (defined(SYSCFG_PMC_I2C_PB8_FMP) || defined(SYSCFG_PMC_I2C_PB9_FMP)) || (defined(SYSCFG_PMC_I2C1_FMP)) || (defined(SYSCFG_PMC_I2C2_FMP)) || defined(SYSCFG_PMC_I2C3_FMP) || defined(SYSCFG_PMC_I2C4_FMP)
183 
184 /** @defgroup I2CEx_Exported_Functions_Group3 Fast Mode Plus Functions
185   * @brief    Fast Mode Plus Functions
186   *
187 @verbatim
188  ===============================================================================
189                       ##### Fast Mode Plus Functions #####
190  ===============================================================================
191     [..] This section provides functions allowing to:
192       (+) Configure Fast Mode Plus
193 
194 @endverbatim
195   * @{
196   */
197 
198 /**
199   * @brief Enable the I2C fast mode plus driving capability.
200   * @param ConfigFastModePlus Selects the pin.
201   *   This parameter can be one of the @ref I2CEx_FastModePlus values
202   * @note  For I2C1, fast mode plus driving capability can be enabled on all selected
203   *        I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
204   *        on each one of the following pins PB6, PB7, PB8 and PB9.
205   * @note  For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
206   *        can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
207   * @note  For all I2C2 pins fast mode plus driving capability can be enabled
208   *        only by using I2C_FASTMODEPLUS_I2C2 parameter.
209   * @note  For all I2C3 pins fast mode plus driving capability can be enabled
210   *        only by using I2C_FASTMODEPLUS_I2C3 parameter.
211   * @note  For all I2C4 pins fast mode plus driving capability can be enabled
212   *        only by using I2C_FASTMODEPLUS_I2C4 parameter.
213   * @retval None
214   */
HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)215 void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
216 {
217   /* Check the parameter */
218   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
219 
220   /* Enable SYSCFG clock */
221   __HAL_RCC_SYSCFG_CLK_ENABLE();
222 
223   /* Enable fast mode plus driving capability for selected pin */
224   SET_BIT(SYSCFG->PMC, (uint32_t)ConfigFastModePlus);
225 }
226 
227 /**
228   * @brief Disable the I2C fast mode plus driving capability.
229   * @param ConfigFastModePlus Selects the pin.
230   *   This parameter can be one of the @ref I2CEx_FastModePlus values
231   * @note  For I2C1, fast mode plus driving capability can be disabled on all selected
232   *        I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
233   *        on each one of the following pins PB6, PB7, PB8 and PB9.
234   * @note  For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
235   *        can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
236   * @note  For all I2C2 pins fast mode plus driving capability can be disabled
237   *        only by using I2C_FASTMODEPLUS_I2C2 parameter.
238   * @note  For all I2C3 pins fast mode plus driving capability can be disabled
239   *        only by using I2C_FASTMODEPLUS_I2C3 parameter.
240   * @note  For all I2C4 pins fast mode plus driving capability can be disabled
241   *        only by using I2C_FASTMODEPLUS_I2C4 parameter.
242   * @retval None
243   */
HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)244 void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
245 {
246   /* Check the parameter */
247   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
248 
249   /* Enable SYSCFG clock */
250   __HAL_RCC_SYSCFG_CLK_ENABLE();
251 
252   /* Disable fast mode plus driving capability for selected pin */
253   CLEAR_BIT(SYSCFG->PMC, (uint32_t)ConfigFastModePlus);
254 }
255 /**
256   * @}
257   */
258 #endif /* Fast Mode Plus Availability */
259 /**
260   * @}
261   */
262 
263 #endif /* HAL_I2C_MODULE_ENABLED */
264 /**
265   * @}
266   */
267 
268 /**
269   * @}
270   */
271