1 /**
2 ******************************************************************************
3 * @file stm32l4xx_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) 2017 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 STM32L4xx
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 "stm32l4xx_hal.h"
52
53 /** @addtogroup STM32L4xx_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
188 /** @defgroup I2CEx_Exported_Functions_Group2 WakeUp Mode Functions
189 * @brief WakeUp Mode Functions
190 *
191 @verbatim
192 ===============================================================================
193 ##### WakeUp Mode Functions #####
194 ===============================================================================
195 [..] This section provides functions allowing to:
196 (+) Configure Wake Up Feature
197
198 @endverbatim
199 * @{
200 */
201
202 /**
203 * @brief Enable I2C wakeup from Stop mode(s).
204 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
205 * the configuration information for the specified I2Cx peripheral.
206 * @retval HAL status
207 */
HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef * hi2c)208 HAL_StatusTypeDef HAL_I2CEx_EnableWakeUp(I2C_HandleTypeDef *hi2c)
209 {
210 /* Check the parameters */
211 assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
212
213 if (hi2c->State == HAL_I2C_STATE_READY)
214 {
215 /* Process Locked */
216 __HAL_LOCK(hi2c);
217
218 hi2c->State = HAL_I2C_STATE_BUSY;
219
220 /* Disable the selected I2C peripheral */
221 __HAL_I2C_DISABLE(hi2c);
222
223 /* Enable wakeup from stop mode */
224 hi2c->Instance->CR1 |= I2C_CR1_WUPEN;
225
226 __HAL_I2C_ENABLE(hi2c);
227
228 hi2c->State = HAL_I2C_STATE_READY;
229
230 /* Process Unlocked */
231 __HAL_UNLOCK(hi2c);
232
233 return HAL_OK;
234 }
235 else
236 {
237 return HAL_BUSY;
238 }
239 }
240
241 /**
242 * @brief Disable I2C wakeup from Stop mode(s).
243 * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
244 * the configuration information for the specified I2Cx peripheral.
245 * @retval HAL status
246 */
HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef * hi2c)247 HAL_StatusTypeDef HAL_I2CEx_DisableWakeUp(I2C_HandleTypeDef *hi2c)
248 {
249 /* Check the parameters */
250 assert_param(IS_I2C_WAKEUP_FROMSTOP_INSTANCE(hi2c->Instance));
251
252 if (hi2c->State == HAL_I2C_STATE_READY)
253 {
254 /* Process Locked */
255 __HAL_LOCK(hi2c);
256
257 hi2c->State = HAL_I2C_STATE_BUSY;
258
259 /* Disable the selected I2C peripheral */
260 __HAL_I2C_DISABLE(hi2c);
261
262 /* Enable wakeup from stop mode */
263 hi2c->Instance->CR1 &= ~(I2C_CR1_WUPEN);
264
265 __HAL_I2C_ENABLE(hi2c);
266
267 hi2c->State = HAL_I2C_STATE_READY;
268
269 /* Process Unlocked */
270 __HAL_UNLOCK(hi2c);
271
272 return HAL_OK;
273 }
274 else
275 {
276 return HAL_BUSY;
277 }
278 }
279 /**
280 * @}
281 */
282
283 /** @defgroup I2CEx_Exported_Functions_Group3 Fast Mode Plus Functions
284 * @brief Fast Mode Plus Functions
285 *
286 @verbatim
287 ===============================================================================
288 ##### Fast Mode Plus Functions #####
289 ===============================================================================
290 [..] This section provides functions allowing to:
291 (+) Configure Fast Mode Plus
292
293 @endverbatim
294 * @{
295 */
296
297 /**
298 * @brief Enable the I2C fast mode plus driving capability.
299 * @param ConfigFastModePlus Selects the pin.
300 * This parameter can be one of the @ref I2CEx_FastModePlus values
301 * @note For I2C1, fast mode plus driving capability can be enabled on all selected
302 * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
303 * on each one of the following pins PB6, PB7, PB8 and PB9.
304 * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
305 * can be enabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
306 * @note For all I2C2 pins fast mode plus driving capability can be enabled
307 * only by using I2C_FASTMODEPLUS_I2C2 parameter.
308 * @note For all I2C3 pins fast mode plus driving capability can be enabled
309 * only by using I2C_FASTMODEPLUS_I2C3 parameter.
310 * @note For all I2C4 pins fast mode plus driving capability can be enabled
311 * only by using I2C_FASTMODEPLUS_I2C4 parameter.
312 * @retval None
313 */
HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)314 void HAL_I2CEx_EnableFastModePlus(uint32_t ConfigFastModePlus)
315 {
316 /* Check the parameter */
317 assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
318
319 /* Enable SYSCFG clock */
320 __HAL_RCC_SYSCFG_CLK_ENABLE();
321
322 /* Enable fast mode plus driving capability for selected pin */
323 SET_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
324 }
325
326 /**
327 * @brief Disable the I2C fast mode plus driving capability.
328 * @param ConfigFastModePlus Selects the pin.
329 * This parameter can be one of the @ref I2CEx_FastModePlus values
330 * @note For I2C1, fast mode plus driving capability can be disabled on all selected
331 * I2C1 pins using I2C_FASTMODEPLUS_I2C1 parameter or independently
332 * on each one of the following pins PB6, PB7, PB8 and PB9.
333 * @note For remaining I2C1 pins (PA14, PA15...) fast mode plus driving capability
334 * can be disabled only by using I2C_FASTMODEPLUS_I2C1 parameter.
335 * @note For all I2C2 pins fast mode plus driving capability can be disabled
336 * only by using I2C_FASTMODEPLUS_I2C2 parameter.
337 * @note For all I2C3 pins fast mode plus driving capability can be disabled
338 * only by using I2C_FASTMODEPLUS_I2C3 parameter.
339 * @note For all I2C4 pins fast mode plus driving capability can be disabled
340 * only by using I2C_FASTMODEPLUS_I2C4 parameter.
341 * @retval None
342 */
HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)343 void HAL_I2CEx_DisableFastModePlus(uint32_t ConfigFastModePlus)
344 {
345 /* Check the parameter */
346 assert_param(IS_I2C_FASTMODEPLUS(ConfigFastModePlus));
347
348 /* Enable SYSCFG clock */
349 __HAL_RCC_SYSCFG_CLK_ENABLE();
350
351 /* Disable fast mode plus driving capability for selected pin */
352 CLEAR_BIT(SYSCFG->CFGR1, (uint32_t)ConfigFastModePlus);
353 }
354 /**
355 * @}
356 */
357 /**
358 * @}
359 */
360
361 #endif /* HAL_I2C_MODULE_ENABLED */
362 /**
363 * @}
364 */
365
366 /**
367 * @}
368 */
369