1 /**
2   ******************************************************************************
3   * @file    stm32f0xx_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   *           + Extended features functions
9   *
10   @verbatim
11   ==============================================================================
12                ##### I2C peripheral Extended features  #####
13   ==============================================================================
14 
15   [..] Comparing to other previous devices, the I2C interface for STM32F0xx
16        devices contains the following additional features
17 
18        (+) Possibility to disable or enable Analog Noise Filter
19        (+) Use of a configured Digital Noise Filter
20        (+) Disable or enable wakeup from Stop mode
21 
22                      ##### How to use this driver #####
23   ==============================================================================
24   [..] This driver provides functions to configure Noise Filter and Wake Up Feature
25     (#) Configure I2C Analog noise filter using the function HAL_I2CEx_ConfigAnalogFilter()
26     (#) Configure I2C Digital noise filter using the function HAL_I2CEx_ConfigDigitalFilter()
27     (#) Configure the enable or disable of I2C Wake Up Mode using the functions :
28           (++) HAL_I2CEx_EnableWakeUp()
29           (++) HAL_I2CEx_DisableWakeUp()
30     (#) Configure the enable or disable of fast mode plus driving capability using the functions :
31           (++) HAL_I2CEx_EnableFastModePlus()
32           (++) HAL_I2CEx_DisableFastModePlus()
33   @endverbatim
34   ******************************************************************************
35   * @attention
36   *
37   * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
38   *
39   * Redistribution and use in source and binary forms, with or without modification,
40   * are permitted provided that the following conditions are met:
41   *   1. Redistributions of source code must retain the above copyright notice,
42   *      this list of conditions and the following disclaimer.
43   *   2. Redistributions in binary form must reproduce the above copyright notice,
44   *      this list of conditions and the following disclaimer in the documentation
45   *      and/or other materials provided with the distribution.
46   *   3. Neither the name of STMicroelectronics nor the names of its contributors
47   *      may be used to endorse or promote products derived from this software
48   *      without specific prior written permission.
49   *
50   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
51   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
54   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
56   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
57   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
58   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
59   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
60   *
61   ******************************************************************************
62   */
63 
64 /* Includes ------------------------------------------------------------------*/
65 #include "stm32f0xx_hal.h"
66 
67 /** @addtogroup STM32F0xx_HAL_Driver
68   * @{
69   */
70 
71 /** @defgroup I2CEx I2CEx
72   * @brief I2C Extended HAL module driver
73   * @{
74   */
75 
76 #ifdef HAL_I2C_MODULE_ENABLED
77 
78 /* Private typedef -----------------------------------------------------------*/
79 /* Private define ------------------------------------------------------------*/
80 /* Private macro -------------------------------------------------------------*/
81 /* Private variables ---------------------------------------------------------*/
82 /* Private function prototypes -----------------------------------------------*/
83 /* Private functions ---------------------------------------------------------*/
84 
85 /** @defgroup I2CEx_Exported_Functions I2C Extended Exported Functions
86   * @{
87   */
88 
89 /** @defgroup I2CEx_Exported_Functions_Group1 Extended features functions
90   * @brief    Extended features functions
91  *
92 @verbatim
93  ===============================================================================
94                       ##### Extended features functions #####
95  ===============================================================================
96     [..] This section provides functions allowing to:
97       (+) Configure Noise Filters
98       (+) Configure Wake Up Feature
99 
100 @endverbatim
101   * @{
102   */
103 
104 /**
105   * @brief  Configure I2C Analog noise filter.
106   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
107   *                the configuration information for the specified I2Cx peripheral.
108   * @param  AnalogFilter New state of the Analog filter.
109   * @retval HAL status
110   */
HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef * hi2c,uint32_t AnalogFilter)111 HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
112 {
113   /* Check the parameters */
114   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
115   assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
116 
117   if (hi2c->State == HAL_I2C_STATE_READY)
118   {
119     /* Process Locked */
120     __HAL_LOCK(hi2c);
121 
122     hi2c->State = HAL_I2C_STATE_BUSY;
123 
124     /* Disable the selected I2C peripheral */
125     __HAL_I2C_DISABLE(hi2c);
126 
127     /* Reset I2Cx ANOFF bit */
128     hi2c->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
129 
130     /* Set analog filter bit*/
131     hi2c->Instance->CR1 |= AnalogFilter;
132 
133     __HAL_I2C_ENABLE(hi2c);
134 
135     hi2c->State = HAL_I2C_STATE_READY;
136 
137     /* Process Unlocked */
138     __HAL_UNLOCK(hi2c);
139 
140     return HAL_OK;
141   }
142   else
143   {
144     return HAL_BUSY;
145   }
146 }
147 
148 /**
149   * @brief  Configure I2C Digital noise filter.
150   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
151   *                the configuration information for the specified I2Cx peripheral.
152   * @param  DigitalFilter Coefficient of digital noise filter between Min_Data=0x00 and Max_Data=0x0F.
153   * @retval HAL status
154   */
HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef * hi2c,uint32_t DigitalFilter)155 HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
156 {
157   uint32_t tmpreg = 0U;
158 
159   /* Check the parameters */
160   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
161   assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
162 
163   if (hi2c->State == HAL_I2C_STATE_READY)
164   {
165     /* Process Locked */
166     __HAL_LOCK(hi2c);
167 
168     hi2c->State = HAL_I2C_STATE_BUSY;
169 
170     /* Disable the selected I2C peripheral */
171     __HAL_I2C_DISABLE(hi2c);
172 
173     /* Get the old register value */
174     tmpreg = hi2c->Instance->CR1;
175 
176     /* Reset I2Cx DNF bits [11:8] */
177     tmpreg &= ~(I2C_CR1_DNF);
178 
179     /* Set I2Cx DNF coefficient */
180     tmpreg |= DigitalFilter << 8U;
181 
182     /* Store the new register value */
183     hi2c->Instance->CR1 = tmpreg;
184 
185     __HAL_I2C_ENABLE(hi2c);
186 
187     hi2c->State = HAL_I2C_STATE_READY;
188 
189     /* Process Unlocked */
190     __HAL_UNLOCK(hi2c);
191 
192     return HAL_OK;
193   }
194   else
195   {
196     return HAL_BUSY;
197   }
198 }
199 #if defined(I2C_CR1_WUPEN)
200 
201 /**
202   * @brief  Enable I2C wakeup from stop mode.
203   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
204   *                the configuration information for the specified I2Cx peripheral.
205   * @retval HAL status
206   */
HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef * hi2c)207 HAL_StatusTypeDef HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef *hi2c)
208 {
209   /* Check the parameters */
210   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
211 
212   if (hi2c->State == HAL_I2C_STATE_READY)
213   {
214     /* Process Locked */
215     __HAL_LOCK(hi2c);
216 
217     hi2c->State = HAL_I2C_STATE_BUSY;
218 
219     /* Disable the selected I2C peripheral */
220     __HAL_I2C_DISABLE(hi2c);
221 
222     /* Enable wakeup from stop mode */
223     hi2c->Instance->CR1 |= I2C_CR1_WUPEN;
224 
225     __HAL_I2C_ENABLE(hi2c);
226 
227     hi2c->State = HAL_I2C_STATE_READY;
228 
229     /* Process Unlocked */
230     __HAL_UNLOCK(hi2c);
231 
232     return HAL_OK;
233   }
234   else
235   {
236     return HAL_BUSY;
237   }
238 }
239 
240 /**
241   * @brief  Disable I2C wakeup from stop mode.
242   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
243   *                the configuration information for the specified I2Cx peripheral.
244   * @retval HAL status
245   */
HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef * hi2c)246 HAL_StatusTypeDef HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef *hi2c)
247 {
248   /* Check the parameters */
249   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
250 
251   if (hi2c->State == HAL_I2C_STATE_READY)
252   {
253     /* Process Locked */
254     __HAL_LOCK(hi2c);
255 
256     hi2c->State = HAL_I2C_STATE_BUSY;
257 
258     /* Disable the selected I2C peripheral */
259     __HAL_I2C_DISABLE(hi2c);
260 
261     /* Enable wakeup from stop mode */
262     hi2c->Instance->CR1 &= ~(I2C_CR1_WUPEN);
263 
264     __HAL_I2C_ENABLE(hi2c);
265 
266     hi2c->State = HAL_I2C_STATE_READY;
267 
268     /* Process Unlocked */
269     __HAL_UNLOCK(hi2c);
270 
271     return HAL_OK;
272   }
273   else
274   {
275     return HAL_BUSY;
276   }
277 }
278 #endif
279 
280 /**
281   * @brief Enable the I2C fast mode plus driving capability.
282   * @param ConfigFastModePlus Selects the pin.
283   *   This parameter can be one of the @ref I2CEx_FastModePlus values
284   * @note  For I2C1, fast mode plus driving capability can be enabled on all selected
285   *        I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
286   *        on each one of the following pins PB6, PB7, PB8 and PB9.
287   * @note  For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
288   *        can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
289   * @note  For all I2C2 pins fast mode plus driving capability can be enabled
290   *        only by using I2C_FASTMODEPLUS_I2C2 parameter.
291   * @retval None
292   */
HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)293 void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
294 {
295   /* Check the parameter */
296   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
297 
298   /* Enable SYSCFG clock */
299   __HAL_RCC_SYSCFG_CLK_ENABLE();
300 
301   /* Enable fast mode plus driving capability for selected pin */
302   SET_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
303 }
304 
305 /**
306   * @brief Disable the I2C fast mode plus driving capability.
307   * @param ConfigFastModePlus Selects the pin.
308   *   This parameter can be one of the @ref I2CEx_FastModePlus values
309   * @note  For I2C1, fast mode plus driving capability can be disabled on all selected
310   *        I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
311   *        on each one of the following pins PB6, PB7, PB8 and PB9.
312   * @note  For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
313   *        can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
314   * @note  For all I2C2 pins fast mode plus driving capability can be disabled
315   *        only by using I2C_FASTMODEPLUS_I2C2 parameter.
316   * @retval None
317   */
HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)318 void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
319 {
320   /* Check the parameter */
321   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
322 
323   /* Enable SYSCFG clock */
324   __HAL_RCC_SYSCFG_CLK_ENABLE();
325 
326   /* Disable fast mode plus driving capability for selected pin */
327   CLEAR_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
328 }
329 
330 /**
331   * @}
332   */
333 
334 /**
335   * @}
336   */
337 
338 #endif /* HAL_I2C_MODULE_ENABLED */
339 /**
340   * @}
341   */
342 
343 /**
344   * @}
345   */
346 
347 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
348