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   *           + Filter Mode Functions
9   *           + WakeUp Mode Functions
10   *           + FastModePlus Functions
11   *
12   @verbatim
13   ==============================================================================
14                ##### I2C peripheral Extended features  #####
15   ==============================================================================
16 
17   [..] Comparing to other previous devices, the I2C interface for STM32F0xx
18        devices contains the following additional features
19 
20        (+) Possibility to disable or enable Analog Noise Filter
21        (+) Use of a configured Digital Noise Filter
22        (+) Disable or enable wakeup from Stop mode(s)
23        (+) Disable or enable Fast Mode Plus
24 
25                      ##### How to use this driver #####
26   ==============================================================================
27   [..] This driver provides functions to configure Noise Filter and Wake Up Feature
28     (#) Configure I2C Analog noise filter using the function HAL_I2CEx_ConfigAnalogFilter()
29     (#) Configure I2C Digital noise filter using the function HAL_I2CEx_ConfigDigitalFilter()
30     (#) Configure the enable or disable of I2C Wake Up Mode using the functions :
31           (++) HAL_I2CEx_EnableWakeUp()
32           (++) HAL_I2CEx_DisableWakeUp()
33     (#) Configure the enable or disable of fast mode plus driving capability using the functions :
34           (++) HAL_I2CEx_EnableFastModePlus()
35           (++) HAL_I2CEx_DisableFastModePlus()
36   @endverbatim
37   ******************************************************************************
38   * @attention
39   *
40   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
41   * All rights reserved.</center></h2>
42   *
43   * This software component is licensed by ST under BSD 3-Clause license,
44   * the "License"; You may not use this file except in compliance with the
45   * License. You may obtain a copy of the License at:
46   *                        opensource.org/licenses/BSD-3-Clause
47   *
48   ******************************************************************************
49   */
50 
51 /* Includes ------------------------------------------------------------------*/
52 #include "stm32f0xx_hal.h"
53 
54 /** @addtogroup STM32F0xx_HAL_Driver
55   * @{
56   */
57 
58 /** @defgroup I2CEx I2CEx
59   * @brief I2C Extended HAL module driver
60   * @{
61   */
62 
63 #ifdef HAL_I2C_MODULE_ENABLED
64 
65 /* Private typedef -----------------------------------------------------------*/
66 /* Private define ------------------------------------------------------------*/
67 /* Private macro -------------------------------------------------------------*/
68 /* Private variables ---------------------------------------------------------*/
69 /* Private function prototypes -----------------------------------------------*/
70 /* Private functions ---------------------------------------------------------*/
71 
72 /** @defgroup I2CEx_Exported_Functions I2C Extended Exported Functions
73   * @{
74   */
75 
76 /** @defgroup I2CEx_Exported_Functions_Group1 Filter Mode Functions
77   * @brief    Filter Mode Functions
78   *
79 @verbatim
80  ===============================================================================
81                       ##### Filter Mode Functions #####
82  ===============================================================================
83     [..] This section provides functions allowing to:
84       (+) Configure Noise Filters
85 
86 @endverbatim
87   * @{
88   */
89 
90 /**
91   * @brief  Configure I2C Analog noise filter.
92   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
93   *                the configuration information for the specified I2Cx peripheral.
94   * @param  AnalogFilter New state of the Analog filter.
95   * @retval HAL status
96   */
HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef * hi2c,uint32_t AnalogFilter)97 HAL_StatusTypeDef HAL_I2CEx_ConfigAnalogFilter(I2C_HandleTypeDef *hi2c, uint32_t AnalogFilter)
98 {
99   /* Check the parameters */
100   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
101   assert_param(IS_I2C_ANALOG_FILTER(AnalogFilter));
102 
103   if (hi2c->State == HAL_I2C_STATE_READY)
104   {
105     /* Process Locked */
106     __HAL_LOCK(hi2c);
107 
108     hi2c->State = HAL_I2C_STATE_BUSY;
109 
110     /* Disable the selected I2C peripheral */
111     __HAL_I2C_DISABLE(hi2c);
112 
113     /* Reset I2Cx ANOFF bit */
114     hi2c->Instance->CR1 &= ~(I2C_CR1_ANFOFF);
115 
116     /* Set analog filter bit*/
117     hi2c->Instance->CR1 |= AnalogFilter;
118 
119     __HAL_I2C_ENABLE(hi2c);
120 
121     hi2c->State = HAL_I2C_STATE_READY;
122 
123     /* Process Unlocked */
124     __HAL_UNLOCK(hi2c);
125 
126     return HAL_OK;
127   }
128   else
129   {
130     return HAL_BUSY;
131   }
132 }
133 
134 /**
135   * @brief  Configure I2C Digital noise filter.
136   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
137   *                the configuration information for the specified I2Cx peripheral.
138   * @param  DigitalFilter Coefficient of digital noise filter between Min_Data=0x00 and Max_Data=0x0F.
139   * @retval HAL status
140   */
HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef * hi2c,uint32_t DigitalFilter)141 HAL_StatusTypeDef HAL_I2CEx_ConfigDigitalFilter(I2C_HandleTypeDef *hi2c, uint32_t DigitalFilter)
142 {
143   uint32_t tmpreg;
144 
145   /* Check the parameters */
146   assert_param(IS_I2C_ALL_INSTANCE(hi2c->Instance));
147   assert_param(IS_I2C_DIGITAL_FILTER(DigitalFilter));
148 
149   if (hi2c->State == HAL_I2C_STATE_READY)
150   {
151     /* Process Locked */
152     __HAL_LOCK(hi2c);
153 
154     hi2c->State = HAL_I2C_STATE_BUSY;
155 
156     /* Disable the selected I2C peripheral */
157     __HAL_I2C_DISABLE(hi2c);
158 
159     /* Get the old register value */
160     tmpreg = hi2c->Instance->CR1;
161 
162     /* Reset I2Cx DNF bits [11:8] */
163     tmpreg &= ~(I2C_CR1_DNF);
164 
165     /* Set I2Cx DNF coefficient */
166     tmpreg |= DigitalFilter << 8U;
167 
168     /* Store the new register value */
169     hi2c->Instance->CR1 = tmpreg;
170 
171     __HAL_I2C_ENABLE(hi2c);
172 
173     hi2c->State = HAL_I2C_STATE_READY;
174 
175     /* Process Unlocked */
176     __HAL_UNLOCK(hi2c);
177 
178     return HAL_OK;
179   }
180   else
181   {
182     return HAL_BUSY;
183   }
184 }
185 /**
186   * @}
187   */
188 #if defined(I2C_CR1_WUPEN)
189 
190 /** @defgroup I2CEx_Exported_Functions_Group2 WakeUp Mode Functions
191   * @brief    WakeUp Mode Functions
192   *
193 @verbatim
194  ===============================================================================
195                       ##### WakeUp Mode Functions #####
196  ===============================================================================
197     [..] This section provides functions allowing to:
198       (+) Configure Wake Up Feature
199 
200 @endverbatim
201   * @{
202   */
203 
204 /**
205   * @brief  Enable I2C wakeup from Stop mode(s).
206   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
207   *                the configuration information for the specified I2Cx peripheral.
208   * @retval HAL status
209   */
HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef * hi2c)210 HAL_StatusTypeDef HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef *hi2c)
211 {
212   /* Check the parameters */
213   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
214 
215   if (hi2c->State == HAL_I2C_STATE_READY)
216   {
217     /* Process Locked */
218     __HAL_LOCK(hi2c);
219 
220     hi2c->State = HAL_I2C_STATE_BUSY;
221 
222     /* Disable the selected I2C peripheral */
223     __HAL_I2C_DISABLE(hi2c);
224 
225     /* Enable wakeup from stop mode */
226     hi2c->Instance->CR1 |= I2C_CR1_WUPEN;
227 
228     __HAL_I2C_ENABLE(hi2c);
229 
230     hi2c->State = HAL_I2C_STATE_READY;
231 
232     /* Process Unlocked */
233     __HAL_UNLOCK(hi2c);
234 
235     return HAL_OK;
236   }
237   else
238   {
239     return HAL_BUSY;
240   }
241 }
242 
243 /**
244   * @brief  Disable I2C wakeup from Stop mode(s).
245   * @param  hi2c Pointer to a I2C_HandleTypeDef structure that contains
246   *                the configuration information for the specified I2Cx peripheral.
247   * @retval HAL status
248   */
HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef * hi2c)249 HAL_StatusTypeDef HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef *hi2c)
250 {
251   /* Check the parameters */
252   assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
253 
254   if (hi2c->State == HAL_I2C_STATE_READY)
255   {
256     /* Process Locked */
257     __HAL_LOCK(hi2c);
258 
259     hi2c->State = HAL_I2C_STATE_BUSY;
260 
261     /* Disable the selected I2C peripheral */
262     __HAL_I2C_DISABLE(hi2c);
263 
264     /* Enable wakeup from stop mode */
265     hi2c->Instance->CR1 &= ~(I2C_CR1_WUPEN);
266 
267     __HAL_I2C_ENABLE(hi2c);
268 
269     hi2c->State = HAL_I2C_STATE_READY;
270 
271     /* Process Unlocked */
272     __HAL_UNLOCK(hi2c);
273 
274     return HAL_OK;
275   }
276   else
277   {
278     return HAL_BUSY;
279   }
280 }
281 /**
282   * @}
283   */
284 #endif /* I2C_CR1_WUPEN */
285 
286 /** @defgroup I2CEx_Exported_Functions_Group3 Fast Mode Plus Functions
287   * @brief    Fast Mode Plus Functions
288   *
289 @verbatim
290  ===============================================================================
291                       ##### Fast Mode Plus Functions #####
292  ===============================================================================
293     [..] This section provides functions allowing to:
294       (+) Configure Fast Mode Plus
295 
296 @endverbatim
297   * @{
298   */
299 
300 /**
301   * @brief Enable the I2C fast mode plus driving capability.
302   * @param ConfigFastModePlus Selects the pin.
303   *   This parameter can be one of the @ref I2CEx_FastModePlus values
304   * @note  For I2C1, fast mode plus driving capability can be enabled on all selected
305   *        I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
306   *        on each one of the following pins PB6, PB7, PB8 and PB9.
307   * @note  For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
308   *        can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
309   * @note  For all I2C2 pins fast mode plus driving capability can be enabled
310   *        only by using I2C_FASTMODEPLUS_I2C2 parameter.
311   * @retval None
312   */
HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)313 void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
314 {
315   /* Check the parameter */
316   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
317 
318   /* Enable SYSCFG clock */
319   __HAL_RCC_SYSCFG_CLK_ENABLE();
320 
321   /* Enable fast mode plus driving capability for selected pin */
322   SET_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
323 }
324 
325 /**
326   * @brief Disable the I2C fast mode plus driving capability.
327   * @param ConfigFastModePlus Selects the pin.
328   *   This parameter can be one of the @ref I2CEx_FastModePlus values
329   * @note  For I2C1, fast mode plus driving capability can be disabled on all selected
330   *        I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
331   *        on each one of the following pins PB6, PB7, PB8 and PB9.
332   * @note  For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
333   *        can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
334   * @note  For all I2C2 pins fast mode plus driving capability can be disabled
335   *        only by using I2C_FASTMODEPLUS_I2C2 parameter.
336   * @retval None
337   */
HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)338 void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
339 {
340   /* Check the parameter */
341   assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
342 
343   /* Enable SYSCFG clock */
344   __HAL_RCC_SYSCFG_CLK_ENABLE();
345 
346   /* Disable fast mode plus driving capability for selected pin */
347   CLEAR_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
348 }
349 /**
350   * @}
351   */
352 /**
353   * @}
354   */
355 
356 #endif /* HAL_I2C_MODULE_ENABLED */
357 /**
358   * @}
359   */
360 
361 /**
362   * @}
363   */
364 
365 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
366