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